OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_profile_win.h" | 5 #include "device/bluetooth/bluetooth_profile_win.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/strings/stringprintf.h" |
10 #include "device/bluetooth/bluetooth_adapter_factory.h" | 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
11 #include "device/bluetooth/bluetooth_device_win.h" | 14 #include "device/bluetooth/bluetooth_device_win.h" |
12 #include "device/bluetooth/bluetooth_service_record.h" | 15 #include "device/bluetooth/bluetooth_service_record.h" |
13 #include "device/bluetooth/bluetooth_socket_thread_win.h" | 16 #include "device/bluetooth/bluetooth_socket_thread_win.h" |
14 #include "device/bluetooth/bluetooth_socket_win.h" | 17 #include "device/bluetooth/bluetooth_socket_win.h" |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 using device::BluetoothAdapter; | 21 using device::BluetoothAdapter; |
19 using device::BluetoothDevice; | 22 using device::BluetoothDevice; |
20 using device::BluetoothProfileWin; | 23 using device::BluetoothProfileWin; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 socket)); | 58 socket)); |
56 } | 59 } |
57 | 60 |
58 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 61 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
59 const BluetoothProfileWin::ErrorCallback& error_callback, | 62 const BluetoothProfileWin::ErrorCallback& error_callback, |
60 const std::string& error) { | 63 const std::string& error) { |
61 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 64 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
62 error_callback.Run(error); | 65 error_callback.Run(error); |
63 } | 66 } |
64 | 67 |
| 68 std::string IPEndPointToBluetoothAddress(const net::IPEndPoint& end_point) { |
| 69 const size_t kBtAddressSize = 6; |
| 70 if (end_point.address().size() != kBtAddressSize) |
| 71 return std::string(); |
| 72 return base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
| 73 end_point.address()[5], |
| 74 end_point.address()[4], |
| 75 end_point.address()[3], |
| 76 end_point.address()[2], |
| 77 end_point.address()[1], |
| 78 end_point.address()[0]); |
| 79 } |
| 80 |
65 } // namespace | 81 } // namespace |
66 | 82 |
67 namespace device { | 83 namespace device { |
68 | 84 |
69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, | 85 BluetoothProfileWin::BluetoothProfileWin() |
70 const std::string& name) | 86 : BluetoothProfile(), rfcomm_channel_(0), weak_ptr_factory_(this) { |
71 : BluetoothProfile(), uuid_(uuid), name_(name) { | |
72 } | 87 } |
73 | 88 |
74 BluetoothProfileWin::~BluetoothProfileWin() { | 89 BluetoothProfileWin::~BluetoothProfileWin() { |
75 } | 90 } |
76 | 91 |
77 void BluetoothProfileWin::Unregister() { | 92 void BluetoothProfileWin::Unregister() { |
| 93 if (profile_socket_) |
| 94 profile_socket_->Close(); |
| 95 |
78 delete this; | 96 delete this; |
79 } | 97 } |
80 | 98 |
81 void BluetoothProfileWin::SetConnectionCallback( | 99 void BluetoothProfileWin::SetConnectionCallback( |
82 const ConnectionCallback& callback) { | 100 const ConnectionCallback& callback) { |
83 connection_callback_ = callback; | 101 connection_callback_ = callback; |
84 } | 102 } |
85 | 103 |
| 104 void BluetoothProfileWin::Init(const BluetoothUUID& uuid, |
| 105 const BluetoothProfile::Options& options, |
| 106 const ProfileCallback& callback) { |
| 107 uuid_ = uuid; |
| 108 name_ = options.name; |
| 109 rfcomm_channel_ = options.channel; |
| 110 |
| 111 BluetoothAdapterFactory::GetAdapter( |
| 112 base::Bind(&BluetoothProfileWin::OnGetAdapter, |
| 113 weak_ptr_factory_.GetWeakPtr(), |
| 114 callback)); |
| 115 } |
| 116 |
86 void BluetoothProfileWin::Connect( | 117 void BluetoothProfileWin::Connect( |
87 const BluetoothDeviceWin* device, | 118 const BluetoothDeviceWin* device, |
88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 119 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
89 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | 120 scoped_refptr<BluetoothSocketThreadWin> socket_thread, |
90 net::NetLog* net_log, | 121 net::NetLog* net_log, |
91 const net::NetLog::Source& source, | 122 const net::NetLog::Source& source, |
92 const base::Closure& success_callback, | 123 const base::Closure& success_callback, |
93 const ErrorCallback& error_callback) { | 124 const ErrorCallback& error_callback) { |
94 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 125 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
95 if (connection_callback_.is_null()) { | 126 if (connection_callback_.is_null()) { |
96 error_callback.Run(kNoConnectionCallback); | 127 error_callback.Run(kNoConnectionCallback); |
97 return; | 128 return; |
98 } | 129 } |
99 | 130 |
100 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); | 131 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); |
101 if (!record) { | 132 if (!record) { |
102 error_callback.Run(kProfileNotFound); | 133 error_callback.Run(kProfileNotFound); |
103 return; | 134 return; |
104 } | 135 } |
105 | 136 |
106 scoped_refptr<BluetoothSocketWin> socket( | 137 scoped_refptr<BluetoothSocketWin> socket( |
107 BluetoothSocketWin::CreateBluetoothSocket( | 138 BluetoothSocketWin::CreateBluetoothSocket( |
108 *record, ui_task_runner, socket_thread, net_log, source)); | 139 ui_task_runner, socket_thread, net_log, source)); |
109 | 140 |
110 socket->Connect(base::Bind(&OnConnectSuccessUI, | 141 socket->Connect(*record, |
| 142 base::Bind(&OnConnectSuccessUI, |
111 ui_task_runner, | 143 ui_task_runner, |
112 success_callback, | 144 success_callback, |
113 connection_callback_, | 145 connection_callback_, |
114 device->GetAddress(), | 146 device->GetAddress(), |
115 socket), | 147 socket), |
116 error_callback); | 148 error_callback); |
117 } | 149 } |
118 | 150 |
| 151 void BluetoothProfileWin::OnGetAdapter( |
| 152 const ProfileCallback& callback, |
| 153 scoped_refptr<BluetoothAdapter> in_adapter) { |
| 154 DCHECK(!adapter_); |
| 155 DCHECK(!profile_socket_); |
| 156 |
| 157 adapter_ = in_adapter; |
| 158 profile_socket_ = BluetoothSocketWin::CreateBluetoothSocket( |
| 159 adapter()->ui_task_runner(), |
| 160 adapter()->socket_thread(), |
| 161 NULL, |
| 162 net::NetLog::Source()); |
| 163 profile_socket_->StartService( |
| 164 uuid_, |
| 165 name_, |
| 166 rfcomm_channel_, |
| 167 base::Bind(&BluetoothProfileWin::OnRegisterProfileSuccess, |
| 168 weak_ptr_factory_.GetWeakPtr(), |
| 169 callback), |
| 170 base::Bind(&BluetoothProfileWin::OnRegisterProfileError, |
| 171 weak_ptr_factory_.GetWeakPtr(), |
| 172 callback), |
| 173 base::Bind(&BluetoothProfileWin::OnNewConnection, |
| 174 weak_ptr_factory_.GetWeakPtr())); |
| 175 } |
| 176 |
| 177 void BluetoothProfileWin::OnRegisterProfileSuccess( |
| 178 const ProfileCallback& callback) { |
| 179 callback.Run(this); |
| 180 } |
| 181 |
| 182 void BluetoothProfileWin::OnRegisterProfileError( |
| 183 const ProfileCallback& callback, |
| 184 const std::string& error_message) { |
| 185 callback.Run(NULL); |
| 186 delete this; |
| 187 } |
| 188 |
| 189 void BluetoothProfileWin::OnNewConnection( |
| 190 scoped_refptr<BluetoothSocketWin> connected, |
| 191 const net::IPEndPoint& peer_address) { |
| 192 DCHECK(adapter()->ui_task_runner()->RunsTasksOnCurrentThread()); |
| 193 if (connection_callback_.is_null()) |
| 194 return; |
| 195 |
| 196 std::string device_address = IPEndPointToBluetoothAddress(peer_address); |
| 197 if (device_address.empty()) { |
| 198 LOG(WARNING) << "Failed to accept connection for profile " |
| 199 << "uuid=" << uuid_.value() |
| 200 << ", unexpected peer device address."; |
| 201 return; |
| 202 } |
| 203 |
| 204 BluetoothDevice* device = adapter_->GetDevice(device_address); |
| 205 if (!device) { |
| 206 LOG(WARNING) << "Failed to accept connection for profile" |
| 207 << ",uuid=" << uuid_.value() |
| 208 << ", unknown device=" << device_address; |
| 209 return; |
| 210 } |
| 211 |
| 212 connection_callback_.Run(device, connected); |
| 213 } |
| 214 |
| 215 BluetoothAdapterWin* BluetoothProfileWin::adapter() const { |
| 216 DCHECK(adapter_); |
| 217 return static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 218 } |
| 219 |
119 } // namespace device | 220 } // namespace device |
OLD | NEW |