OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "device/bluetooth/bluetooth_adapter.h" |
| 14 #include "device/bluetooth/bluetooth_gatt_connection.h" |
13 #include "device/bluetooth/bluetooth_gatt_service.h" | 15 #include "device/bluetooth/bluetooth_gatt_service.h" |
14 #include "grit/bluetooth_strings.h" | 16 #include "grit/bluetooth_strings.h" |
15 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
16 | 18 |
17 namespace device { | 19 namespace device { |
18 | 20 |
19 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) | 21 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) |
20 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} | 22 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} |
21 | 23 |
22 BluetoothDevice::~BluetoothDevice() { | 24 BluetoothDevice::~BluetoothDevice() { |
23 STLDeleteValues(&gatt_services_); | 25 STLDeleteValues(&gatt_services_); |
| 26 DidDisconnectGatt(); |
24 } | 27 } |
25 | 28 |
26 BluetoothDevice::ConnectionInfo::ConnectionInfo() | 29 BluetoothDevice::ConnectionInfo::ConnectionInfo() |
27 : rssi(kUnknownPower), | 30 : rssi(kUnknownPower), |
28 transmit_power(kUnknownPower), | 31 transmit_power(kUnknownPower), |
29 max_transmit_power(kUnknownPower) {} | 32 max_transmit_power(kUnknownPower) {} |
30 | 33 |
31 BluetoothDevice::ConnectionInfo::ConnectionInfo( | 34 BluetoothDevice::ConnectionInfo::ConnectionInfo( |
32 int rssi, int transmit_power, int max_transmit_power) | 35 int rssi, int transmit_power, int max_transmit_power) |
33 : rssi(rssi), | 36 : rssi(rssi), |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 196 |
194 bool BluetoothDevice::IsTrustable() const { | 197 bool BluetoothDevice::IsTrustable() const { |
195 // Sony PlayStation Dualshock3 | 198 // Sony PlayStation Dualshock3 |
196 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && | 199 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && |
197 GetDeviceName() == "PLAYSTATION(R)3 Controller")) | 200 GetDeviceName() == "PLAYSTATION(R)3 Controller")) |
198 return true; | 201 return true; |
199 | 202 |
200 return false; | 203 return false; |
201 } | 204 } |
202 | 205 |
| 206 void BluetoothDevice::CreateGattConnection( |
| 207 const GattConnectionCallback& callback, |
| 208 const ConnectErrorCallback& error_callback) { |
| 209 create_gatt_connection_success_callbacks_.push_back(callback); |
| 210 create_gatt_connection_error_callbacks_.push_back(error_callback); |
| 211 |
| 212 if (IsGattConnected()) |
| 213 DidConnectGatt(); |
| 214 |
| 215 CreateGattConnectionImpl(); |
| 216 } |
| 217 |
203 std::vector<BluetoothGattService*> | 218 std::vector<BluetoothGattService*> |
204 BluetoothDevice::GetGattServices() const { | 219 BluetoothDevice::GetGattServices() const { |
205 std::vector<BluetoothGattService*> services; | 220 std::vector<BluetoothGattService*> services; |
206 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); | 221 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); |
207 iter != gatt_services_.end(); ++iter) | 222 iter != gatt_services_.end(); ++iter) |
208 services.push_back(iter->second); | 223 services.push_back(iter->second); |
209 return services; | 224 return services; |
210 } | 225 } |
211 | 226 |
212 BluetoothGattService* BluetoothDevice::GetGattService( | 227 BluetoothGattService* BluetoothDevice::GetGattService( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 std::vector<device::BluetoothUUID> uuids; | 281 std::vector<device::BluetoothUUID> uuids; |
267 base::DictionaryValue::Iterator iter(*services_data_); | 282 base::DictionaryValue::Iterator iter(*services_data_); |
268 while (!iter.IsAtEnd()) { | 283 while (!iter.IsAtEnd()) { |
269 BluetoothUUID uuid(iter.key()); | 284 BluetoothUUID uuid(iter.key()); |
270 uuids.push_back(uuid); | 285 uuids.push_back(uuid); |
271 iter.Advance(); | 286 iter.Advance(); |
272 } | 287 } |
273 return uuids; | 288 return uuids; |
274 } | 289 } |
275 | 290 |
| 291 void BluetoothDevice::DidConnectGatt() { |
| 292 for (const auto& callback : create_gatt_connection_success_callbacks_) { |
| 293 callback.Run( |
| 294 make_scoped_ptr(new BluetoothGattConnection(adapter_, GetAddress()))); |
| 295 } |
| 296 create_gatt_connection_success_callbacks_.clear(); |
| 297 create_gatt_connection_error_callbacks_.clear(); |
| 298 } |
| 299 |
| 300 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { |
| 301 for (const auto& error_callback : create_gatt_connection_error_callbacks_) |
| 302 error_callback.Run(error); |
| 303 create_gatt_connection_success_callbacks_.clear(); |
| 304 create_gatt_connection_error_callbacks_.clear(); |
| 305 } |
| 306 |
| 307 void BluetoothDevice::DidDisconnectGatt() { |
| 308 // Pending calls to connect GATT are not expected, if they were then |
| 309 // DidFailToConnectGatt should be called. But in case callbacks exist |
| 310 // flush them to ensure a consistent state. |
| 311 if (create_gatt_connection_error_callbacks_.size() > 0) { |
| 312 VLOG(1) << "Unexpected / unexplained DidDisconnectGatt call while " |
| 313 "create_gatt_connection_error_callbacks_ are pending."; |
| 314 } |
| 315 DidFailToConnectGatt(ERROR_FAILED); |
| 316 |
| 317 // Invalidate all BluetoothGattConnection objects. |
| 318 for (BluetoothGattConnection* connection : gatt_connections_) { |
| 319 connection->InvalidateConnectionReference(); |
| 320 } |
| 321 gatt_connections_.clear(); |
| 322 } |
| 323 |
| 324 void BluetoothDevice::AddGattConnection(BluetoothGattConnection* connection) { |
| 325 auto result = gatt_connections_.insert(connection); |
| 326 DCHECK(result.second); // Check insert happened; there was no duplicate. |
| 327 } |
| 328 |
| 329 void BluetoothDevice::RemoveGattConnection( |
| 330 BluetoothGattConnection* connection) { |
| 331 size_t erased_count = gatt_connections_.erase(connection); |
| 332 DCHECK(erased_count); |
| 333 if (gatt_connections_.size() == 0) |
| 334 DisconnectGatt(); |
| 335 } |
| 336 |
276 void BluetoothDevice::ClearServiceData() { services_data_->Clear(); } | 337 void BluetoothDevice::ClearServiceData() { services_data_->Clear(); } |
277 | 338 |
278 void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID, | 339 void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID, |
279 const char* buffer, size_t size) { | 340 const char* buffer, size_t size) { |
280 services_data_->Set(serviceUUID.value(), | 341 services_data_->Set(serviceUUID.value(), |
281 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); | 342 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); |
282 } | 343 } |
283 | 344 |
284 } // namespace device | 345 } // namespace device |
OLD | NEW |