Chromium Code Reviews| 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 <limits> | |
| 7 #include <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "device/bluetooth/bluetooth_adapter.h" | |
| 15 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 13 #include "device/bluetooth/bluetooth_gatt_service.h" | 16 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 14 #include "grit/bluetooth_strings.h" | 17 #include "grit/bluetooth_strings.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 16 | 19 |
| 17 namespace device { | 20 namespace device { |
| 18 | 21 |
| 19 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) | 22 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) |
| 20 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} | 23 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} |
| 21 | 24 |
| 22 BluetoothDevice::~BluetoothDevice() { | 25 BluetoothDevice::~BluetoothDevice() { |
| 23 STLDeleteValues(&gatt_services_); | 26 STLDeleteValues(&gatt_services_); |
| 27 DidDisconnectGatt(); | |
| 24 } | 28 } |
| 25 | 29 |
| 26 BluetoothDevice::ConnectionInfo::ConnectionInfo() | 30 BluetoothDevice::ConnectionInfo::ConnectionInfo() |
| 27 : rssi(kUnknownPower), | 31 : rssi(kUnknownPower), |
| 28 transmit_power(kUnknownPower), | 32 transmit_power(kUnknownPower), |
| 29 max_transmit_power(kUnknownPower) {} | 33 max_transmit_power(kUnknownPower) {} |
| 30 | 34 |
| 31 BluetoothDevice::ConnectionInfo::ConnectionInfo( | 35 BluetoothDevice::ConnectionInfo::ConnectionInfo( |
| 32 int rssi, int transmit_power, int max_transmit_power) | 36 int rssi, int transmit_power, int max_transmit_power) |
| 33 : rssi(rssi), | 37 : rssi(rssi), |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 197 |
| 194 bool BluetoothDevice::IsTrustable() const { | 198 bool BluetoothDevice::IsTrustable() const { |
| 195 // Sony PlayStation Dualshock3 | 199 // Sony PlayStation Dualshock3 |
| 196 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && | 200 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && |
| 197 GetDeviceName() == "PLAYSTATION(R)3 Controller")) | 201 GetDeviceName() == "PLAYSTATION(R)3 Controller")) |
| 198 return true; | 202 return true; |
| 199 | 203 |
| 200 return false; | 204 return false; |
| 201 } | 205 } |
| 202 | 206 |
| 207 void BluetoothDevice::CreateGattConnection( | |
| 208 const GattConnectionCallback& callback, | |
| 209 const ConnectErrorCallback& error_callback) { | |
| 210 create_gatt_connection_success_callbacks_.push_back(callback); | |
| 211 create_gatt_connection_error_callbacks_.push_back(error_callback); | |
| 212 | |
| 213 if (IsGattConnected()) | |
| 214 DidConnectGatt(); | |
| 215 | |
| 216 CreateGattConnectionImpl(); | |
| 217 } | |
| 218 | |
| 203 std::vector<BluetoothGattService*> | 219 std::vector<BluetoothGattService*> |
| 204 BluetoothDevice::GetGattServices() const { | 220 BluetoothDevice::GetGattServices() const { |
| 205 std::vector<BluetoothGattService*> services; | 221 std::vector<BluetoothGattService*> services; |
| 206 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); | 222 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); |
| 207 iter != gatt_services_.end(); ++iter) | 223 iter != gatt_services_.end(); ++iter) |
| 208 services.push_back(iter->second); | 224 services.push_back(iter->second); |
| 209 return services; | 225 return services; |
| 210 } | 226 } |
| 211 | 227 |
| 212 BluetoothGattService* BluetoothDevice::GetGattService( | 228 BluetoothGattService* BluetoothDevice::GetGattService( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 std::vector<device::BluetoothUUID> uuids; | 282 std::vector<device::BluetoothUUID> uuids; |
| 267 base::DictionaryValue::Iterator iter(*services_data_); | 283 base::DictionaryValue::Iterator iter(*services_data_); |
| 268 while (!iter.IsAtEnd()) { | 284 while (!iter.IsAtEnd()) { |
| 269 BluetoothUUID uuid(iter.key()); | 285 BluetoothUUID uuid(iter.key()); |
| 270 uuids.push_back(uuid); | 286 uuids.push_back(uuid); |
| 271 iter.Advance(); | 287 iter.Advance(); |
| 272 } | 288 } |
| 273 return uuids; | 289 return uuids; |
| 274 } | 290 } |
| 275 | 291 |
| 292 void BluetoothDevice::DidConnectGatt() { | |
| 293 for (const auto& callback : create_gatt_connection_success_callbacks_) { | |
| 294 callback.Run( | |
| 295 make_scoped_ptr(new BluetoothGattConnection(adapter_, GetAddress()))); | |
| 296 } | |
| 297 create_gatt_connection_success_callbacks_.clear(); | |
| 298 create_gatt_connection_error_callbacks_.clear(); | |
| 299 } | |
| 300 | |
| 301 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { | |
| 302 for (const auto& error_callback : create_gatt_connection_error_callbacks_) | |
| 303 error_callback.Run(error); | |
| 304 create_gatt_connection_success_callbacks_.clear(); | |
| 305 create_gatt_connection_error_callbacks_.clear(); | |
| 306 } | |
| 307 | |
| 308 void BluetoothDevice::DidDisconnectGatt() { | |
| 309 // Pending calls to connect GATT are not expected, if they were then | |
| 310 // DidFailToConnectGatt should be called. But in case callbacks exist | |
| 311 // flush them to ensure a consistent state. | |
| 312 if (create_gatt_connection_error_callbacks_.size() > 0) { | |
| 313 VLOG(1) << "Unexpected / unexplained DidDisconnectGatt call while " | |
| 314 "create_gatt_connection_error_callbacks_ are pending."; | |
| 315 } | |
| 316 DidFailToConnectGatt(ERROR_FAILED); | |
| 317 | |
| 318 // Invalidate all BluetoothGattConnection objects. | |
| 319 for (auto connection : gatt_connections_) { | |
|
Jeffrey Yasskin
2015/09/16 00:45:39
I think you need to gatt_connections_.clear() sinc
Jeffrey Yasskin
2015/09/16 00:45:39
'auto' often causes extra copies in range-based fo
scheib
2015/09/16 21:19:07
Done.
scheib
2015/09/16 21:19:08
Done.
| |
| 320 connection->InvalidateConnectionReference(); | |
| 321 } | |
| 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 |