Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1053)

Side by Side Diff: device/bluetooth/bluetooth_device.cc

Issue 2244693002: bluetooth: Refactor how we update based on Advertising Data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <iterator>
7 #include <memory> 8 #include <memory>
8 #include <string> 9 #include <string>
9 10
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "device/bluetooth/bluetooth_adapter.h" 17 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_gatt_connection.h" 18 #include "device/bluetooth/bluetooth_gatt_connection.h"
18 #include "device/bluetooth/bluetooth_remote_gatt_service.h" 19 #include "device/bluetooth/bluetooth_remote_gatt_service.h"
19 #include "grit/bluetooth_strings.h" 20 #include "grit/bluetooth_strings.h"
20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
21 22
22 namespace device { 23 namespace device {
23 24
25 BluetoothDevice::DeviceUUIDs::DeviceUUIDs() {}
26
27 BluetoothDevice::DeviceUUIDs::~DeviceUUIDs() {}
28
29 void BluetoothDevice::DeviceUUIDs::ReplaceAdvertisedUUIDs(
30 UUIDList new_advertised_uuids) {
31 advertised_uuids_.clear();
32 for (auto& it : new_advertised_uuids) {
33 advertised_uuids_.insert(std::move(it));
34 }
35 UpdateDeviceUUIDs();
36 }
37
38 void BluetoothDevice::DeviceUUIDs::ClearAdvertisedUUIDs() {
39 advertised_uuids_.clear();
40 UpdateDeviceUUIDs();
41 }
42
43 void BluetoothDevice::DeviceUUIDs::ReplaceServiceUUIDs(
44 const GattServiceMap& gatt_services) {
45 service_uuids_.clear();
46 for (const auto& gatt_service_pair : gatt_services) {
47 service_uuids_.insert(gatt_service_pair.second->GetUUID());
48 }
49 UpdateDeviceUUIDs();
50 }
51
52 void BluetoothDevice::DeviceUUIDs::ClearServiceUUIDs() {
53 service_uuids_.clear();
54 UpdateDeviceUUIDs();
55 }
56
57 const BluetoothDevice::UUIDSet& BluetoothDevice::DeviceUUIDs::GetUUIDs() const {
58 return device_uuids_;
59 }
60
61 void BluetoothDevice::DeviceUUIDs::UpdateDeviceUUIDs() {
62 device_uuids_.clear();
63 std::set_union(advertised_uuids_.begin(), advertised_uuids_.end(),
64 service_uuids_.begin(), service_uuids_.end(),
65 std::inserter(device_uuids_, device_uuids_.begin()));
66 }
67
24 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) 68 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter)
25 : adapter_(adapter), 69 : adapter_(adapter),
26 gatt_services_discovery_complete_(false), 70 gatt_services_discovery_complete_(false),
27 services_data_(new base::DictionaryValue()),
28 last_update_time_(base::Time()) {} 71 last_update_time_(base::Time()) {}
29 72
30 BluetoothDevice::~BluetoothDevice() { 73 BluetoothDevice::~BluetoothDevice() {
31 for (BluetoothGattConnection* connection : gatt_connections_) { 74 for (BluetoothGattConnection* connection : gatt_connections_) {
32 connection->InvalidateConnectionReference(); 75 connection->InvalidateConnectionReference();
33 } 76 }
34 } 77 }
35 78
36 BluetoothDevice::ConnectionInfo::ConnectionInfo() 79 BluetoothDevice::ConnectionInfo::ConnectionInfo()
37 : rssi(kUnknownPower), 80 : rssi(kUnknownPower),
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 279
237 bool BluetoothDevice::IsTrustable() const { 280 bool BluetoothDevice::IsTrustable() const {
238 // Sony PlayStation Dualshock3 281 // Sony PlayStation Dualshock3
239 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 && 282 if ((GetVendorID() == 0x054c && GetProductID() == 0x0268 &&
240 GetName() == std::string("PLAYSTATION(R)3 Controller"))) 283 GetName() == std::string("PLAYSTATION(R)3 Controller")))
241 return true; 284 return true;
242 285
243 return false; 286 return false;
244 } 287 }
245 288
246 BluetoothDevice::UUIDList BluetoothDevice::GetUUIDs() const { 289 BluetoothDevice::UUIDSet BluetoothDevice::GetUUIDs() const {
247 if (!IsGattConnected()) { 290 return device_uuids_.GetUUIDs();
248 DCHECK(service_uuids_.empty()); 291 }
249 return advertised_uuids_; 292
293 const BluetoothDevice::ServiceDataMap& BluetoothDevice::GetServiceData() const {
294 return service_data_;
295 }
296
297 BluetoothDevice::UUIDSet BluetoothDevice::GetServiceDataUUIDs() const {
298 UUIDSet service_data_uuids;
299 for (const auto& uuid_service_data_pair : service_data_) {
300 service_data_uuids.insert(uuid_service_data_pair.first);
250 } 301 }
302 return service_data_uuids;
303 }
251 304
252 if (IsGattServicesDiscoveryComplete()) { 305 const std::vector<uint8_t>* BluetoothDevice::GetServiceDataForUUID(
253 DCHECK(advertised_uuids_.empty()); 306 const BluetoothUUID& uuid) const {
254 return service_uuids_; 307 auto it = service_data_.find(uuid);
308 if (it != service_data_.end()) {
309 return &it->second;
255 } 310 }
256 311 return nullptr;
257 DCHECK(service_uuids_.empty());
258 DCHECK(advertised_uuids_.empty());
259 return BluetoothDevice::UUIDList();
260 } 312 }
261 313
262 void BluetoothDevice::CreateGattConnection( 314 void BluetoothDevice::CreateGattConnection(
263 const GattConnectionCallback& callback, 315 const GattConnectionCallback& callback,
264 const ConnectErrorCallback& error_callback) { 316 const ConnectErrorCallback& error_callback) {
265 create_gatt_connection_success_callbacks_.push_back(callback); 317 create_gatt_connection_success_callbacks_.push_back(callback);
266 create_gatt_connection_error_callbacks_.push_back(error_callback); 318 create_gatt_connection_error_callbacks_.push_back(error_callback);
267 319
268 if (IsGattConnected()) 320 if (IsGattConnected())
269 return DidConnectGatt(); 321 return DidConnectGatt();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 374
323 canonicalized[i] = base::ToUpperASCII(canonicalized[i]); 375 canonicalized[i] = base::ToUpperASCII(canonicalized[i]);
324 } 376 }
325 } 377 }
326 378
327 return canonicalized; 379 return canonicalized;
328 } 380 }
329 381
330 std::string BluetoothDevice::GetIdentifier() const { return GetAddress(); } 382 std::string BluetoothDevice::GetIdentifier() const { return GetAddress(); }
331 383
332 base::BinaryValue* BluetoothDevice::GetServiceData( 384 void BluetoothDevice::UpdateAdvertisementData(UUIDList advertised_uuids,
333 BluetoothUUID serviceUUID) const { 385 ServiceDataMap service_data) {
334 base::BinaryValue* value; 386 UpdateTimestamp();
335 if (!services_data_->GetBinary(serviceUUID.value(), &value)) 387 device_uuids_.ReplaceAdvertisedUUIDs(std::move(advertised_uuids));
336 return NULL; 388 service_data_ = std::move(service_data);
337 return value;
338 } 389 }
339 390
340 BluetoothDevice::UUIDList BluetoothDevice::GetServiceDataUUIDs() const { 391 void BluetoothDevice::ClearAdvertisementData() {
341 std::vector<device::BluetoothUUID> uuids; 392 device_uuids_.ClearAdvertisedUUIDs();
342 base::DictionaryValue::Iterator iter(*services_data_); 393 service_data_.clear();
343 while (!iter.IsAtEnd()) { 394 GetAdapter()->NotifyDeviceChanged(this);
344 BluetoothUUID uuid(iter.key());
345 uuids.push_back(uuid);
346 iter.Advance();
347 }
348 return uuids;
349 } 395 }
350 396
351 void BluetoothDevice::DidConnectGatt() { 397 void BluetoothDevice::DidConnectGatt() {
352 advertised_uuids_.clear();
353 for (const auto& callback : create_gatt_connection_success_callbacks_) { 398 for (const auto& callback : create_gatt_connection_success_callbacks_) {
354 callback.Run( 399 callback.Run(
355 base::WrapUnique(new BluetoothGattConnection(adapter_, GetAddress()))); 400 base::WrapUnique(new BluetoothGattConnection(adapter_, GetAddress())));
356 } 401 }
357 create_gatt_connection_success_callbacks_.clear(); 402 create_gatt_connection_success_callbacks_.clear();
358 create_gatt_connection_error_callbacks_.clear(); 403 create_gatt_connection_error_callbacks_.clear();
359 GetAdapter()->NotifyDeviceChanged(this); 404 GetAdapter()->NotifyDeviceChanged(this);
360 } 405 }
361 406
362 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) { 407 void BluetoothDevice::DidFailToConnectGatt(ConnectErrorCode error) {
(...skipping 26 matching lines...) Expand all
389 } 434 }
390 435
391 void BluetoothDevice::RemoveGattConnection( 436 void BluetoothDevice::RemoveGattConnection(
392 BluetoothGattConnection* connection) { 437 BluetoothGattConnection* connection) {
393 size_t erased_count = gatt_connections_.erase(connection); 438 size_t erased_count = gatt_connections_.erase(connection);
394 DCHECK(erased_count); 439 DCHECK(erased_count);
395 if (gatt_connections_.size() == 0) 440 if (gatt_connections_.size() == 0)
396 DisconnectGatt(); 441 DisconnectGatt();
397 } 442 }
398 443
399 void BluetoothDevice::UpdateServiceUUIDs() {
400 std::unordered_set<BluetoothUUID, BluetoothUUIDHash> uuid_set;
401 for (const auto& gatt_service_pair : gatt_services_) {
402 uuid_set.insert(gatt_service_pair.second->GetUUID());
403 }
404 service_uuids_ = UUIDList(uuid_set.begin(), uuid_set.end());
405 }
406
407 void BluetoothDevice::ClearServiceData() { services_data_->Clear(); }
408
409 void BluetoothDevice::SetServiceData(BluetoothUUID serviceUUID,
410 const char* buffer, size_t size) {
411 services_data_->Set(serviceUUID.value(),
412 base::BinaryValue::CreateWithCopiedBuffer(buffer, size));
413 }
414
415 void BluetoothDevice::SetAsExpiredForTesting() { 444 void BluetoothDevice::SetAsExpiredForTesting() {
416 last_update_time_ = 445 last_update_time_ =
417 base::Time::NowFromSystemTime() - 446 base::Time::NowFromSystemTime() -
418 (BluetoothAdapter::timeoutSec + base::TimeDelta::FromSeconds(1)); 447 (BluetoothAdapter::timeoutSec + base::TimeDelta::FromSeconds(1));
419 } 448 }
420 449
421 void BluetoothDevice::Pair(PairingDelegate* pairing_delegate, 450 void BluetoothDevice::Pair(PairingDelegate* pairing_delegate,
422 const base::Closure& callback, 451 const base::Closure& callback,
423 const ConnectErrorCallback& error_callback) { 452 const ConnectErrorCallback& error_callback) {
424 NOTREACHED(); 453 NOTREACHED();
425 } 454 }
426 455
427 void BluetoothDevice::UpdateTimestamp() { 456 void BluetoothDevice::UpdateTimestamp() {
428 last_update_time_ = base::Time::NowFromSystemTime(); 457 last_update_time_ = base::Time::NowFromSystemTime();
429 } 458 }
430 459
431 } // namespace device 460 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698