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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
12 #include "device/bluetooth/bluetooth_gatt_service.h" | 14 #include "device/bluetooth/bluetooth_gatt_service.h" |
13 #include "grit/device_bluetooth_strings.h" | 15 #include "grit/device_bluetooth_strings.h" |
14 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
15 | 17 |
16 namespace device { | 18 namespace device { |
17 | 19 |
18 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) | 20 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) |
19 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} | 21 : adapter_(adapter), services_data_(new base::DictionaryValue()) {} |
20 | 22 |
21 BluetoothDevice::~BluetoothDevice() { | 23 BluetoothDevice::~BluetoothDevice() { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 | 194 |
193 bool BluetoothDevice::IsTrustable() const { | 195 bool BluetoothDevice::IsTrustable() const { |
194 // Sony PlayStation Dualshock3 | 196 // Sony PlayStation Dualshock3 |
195 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && | 197 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && |
196 GetDeviceName() == "PLAYSTATION(R)3 Controller")) | 198 GetDeviceName() == "PLAYSTATION(R)3 Controller")) |
197 return true; | 199 return true; |
198 | 200 |
199 return false; | 201 return false; |
200 } | 202 } |
201 | 203 |
204 void BluetoothDevice::CreateGattConnection( | |
205 const GattConnectionCallback& callback, | |
206 const ConnectErrorCallback& error_callback) { | |
207 create_gatt_connection_success_callbacks_.push_back(callback); | |
208 create_gatt_connection_error_callbacks_.push_back(error_callback); | |
209 | |
210 if (IsGattConnected()) | |
211 DidConnectGatt(); | |
212 | |
213 CreateGattConnectionImpl(); | |
214 } | |
215 | |
202 std::vector<BluetoothGattService*> | 216 std::vector<BluetoothGattService*> |
203 BluetoothDevice::GetGattServices() const { | 217 BluetoothDevice::GetGattServices() const { |
204 std::vector<BluetoothGattService*> services; | 218 std::vector<BluetoothGattService*> services; |
205 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); | 219 for (GattServiceMap::const_iterator iter = gatt_services_.begin(); |
206 iter != gatt_services_.end(); ++iter) | 220 iter != gatt_services_.end(); ++iter) |
207 services.push_back(iter->second); | 221 services.push_back(iter->second); |
208 return services; | 222 return services; |
209 } | 223 } |
210 | 224 |
211 BluetoothGattService* BluetoothDevice::GetGattService( | 225 BluetoothGattService* BluetoothDevice::GetGattService( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 std::vector<device::BluetoothUUID> uuids; | 279 std::vector<device::BluetoothUUID> uuids; |
266 base::DictionaryValue::Iterator iter(*services_data_); | 280 base::DictionaryValue::Iterator iter(*services_data_); |
267 while (!iter.IsAtEnd()) { | 281 while (!iter.IsAtEnd()) { |
268 BluetoothUUID uuid(iter.key()); | 282 BluetoothUUID uuid(iter.key()); |
269 uuids.push_back(uuid); | 283 uuids.push_back(uuid); |
270 iter.Advance(); | 284 iter.Advance(); |
271 } | 285 } |
272 return uuids; | 286 return uuids; |
273 } | 287 } |
274 | 288 |
289 void BluetoothDevice::DidConnectGatt() { | |
290 for (const auto& callback : create_gatt_connection_success_callbacks_) { | |
291 callback.Run( | |
292 make_scoped_ptr(new BluetoothGattConnection(adapter_, GetAddress()))); | |
293 } | |
294 create_gatt_connection_success_callbacks_.clear(); | |
295 create_gatt_connection_error_callbacks_.clear(); | |
296 } | |
297 | |
298 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { | |
299 for (const auto& error_callback : create_gatt_connection_error_callbacks_) | |
300 error_callback.Run(error); | |
301 create_gatt_connection_success_callbacks_.clear(); | |
302 create_gatt_connection_error_callbacks_.clear(); | |
303 } | |
304 | |
305 void BluetoothDevice::DidDisconnectGatt() { | |
306 // If pending calls to connect GATT existed, flush them to ensure a consistent | |
307 // state. | |
308 DidFailToConnectGatt(ERROR_UNKNOWN); | |
Jeffrey Yasskin
2015/08/19 22:49:45
Don't use "UNKNOWN" here (or ever, really). If you
scheib
2015/09/13 02:40:27
Ok, though this scenario is a situation where we d
Jeffrey Yasskin
2015/09/16 00:45:39
We have places where we convert a string to an enu
| |
309 } | |
310 | |
311 void BluetoothDevice::IncrementGattConnectionReferenceCount() { | |
312 CHECK(gatt_connection_reference_count_ < | |
Jeffrey Yasskin
2015/08/19 22:49:45
Use base/numerics/safe_math.h instead of implement
scheib
2015/09/13 02:40:27
Done. IDMap does check: after increment and next s
| |
313 std::numeric_limits<decltype(gatt_connection_reference_count_)>::max()); | |
314 gatt_connection_reference_count_++; | |
315 } | |
316 | |
317 void BluetoothDevice::DecrementGattConnectionReferenceCount() { | |
318 CHECK(gatt_connection_reference_count_ > 0); | |
319 gatt_connection_reference_count_--; | |
320 if (gatt_connection_reference_count_ == 0) | |
321 DisconnectGatt(); | |
322 } | |
323 | |
275 void BluetoothDevice::ClearServiceData() { services_data_->Clear(); } | 324 void BluetoothDevice::ClearServiceData() { services_data_->Clear(); } |
276 | 325 |
277 void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID, | 326 void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID, |
278 const char* buffer, size_t size) { | 327 const char* buffer, size_t size) { |
279 services_data_->Set(serviceUUID.value(), | 328 services_data_->Set(serviceUUID.value(), |
280 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); | 329 base::BinaryValue::CreateWithCopiedBuffer(buffer, size)); |
281 } | 330 } |
282 | 331 |
283 } // namespace device | 332 } // namespace device |
OLD | NEW |