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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <unordered_set> | |
| 14 #include <vector> | 15 #include <vector> |
| 15 | 16 |
| 16 #include "base/callback.h" | 17 #include "base/callback.h" |
| 17 #include "base/containers/scoped_ptr_hash_map.h" | 18 #include "base/containers/scoped_ptr_hash_map.h" |
| 18 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
| 19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 20 #include "base/optional.h" | 21 #include "base/optional.h" |
| 21 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "device/bluetooth/bluetooth_common.h" | 24 #include "device/bluetooth/bluetooth_common.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 ERROR_OFFSET_INVALID, | 111 ERROR_OFFSET_INVALID, |
| 111 ERROR_READ_NOT_PERMITTED, | 112 ERROR_READ_NOT_PERMITTED, |
| 112 ERROR_REQUEST_NOT_SUPPORTED, | 113 ERROR_REQUEST_NOT_SUPPORTED, |
| 113 ERROR_UNKNOWN, | 114 ERROR_UNKNOWN, |
| 114 ERROR_UNSUPPORTED_DEVICE, | 115 ERROR_UNSUPPORTED_DEVICE, |
| 115 ERROR_WRITE_NOT_PERMITTED, | 116 ERROR_WRITE_NOT_PERMITTED, |
| 116 NUM_CONNECT_ERROR_CODES // Keep as last enum. | 117 NUM_CONNECT_ERROR_CODES // Keep as last enum. |
| 117 }; | 118 }; |
| 118 | 119 |
| 119 typedef std::vector<BluetoothUUID> UUIDList; | 120 typedef std::vector<BluetoothUUID> UUIDList; |
| 121 typedef std::unordered_set<BluetoothUUID, BluetoothUUIDHash> UUIDSet; | |
|
Jeffrey Yasskin
2016/08/18 19:56:31
This is where I'd really like to have a vector-bac
ortuno
2016/08/19 20:50:33
Is that so that we would always return the uuids i
Jeffrey Yasskin
2016/08/19 22:08:09
No, I'm thinking of the implementation guts of an
| |
| 122 typedef std::unordered_map<BluetoothUUID, | |
| 123 std::vector<uint8_t>, | |
| 124 BluetoothUUIDHash> | |
| 125 ServiceDataMap; | |
| 126 | |
| 127 // Mapping from the platform-specific GATT service identifiers to | |
| 128 // BluetoothRemoteGattService objects. | |
| 129 typedef base::ScopedPtrHashMap<std::string, | |
|
Jeffrey Yasskin
2016/08/18 19:56:31
Use unordered_map instead: https://cs.chromium.org
ortuno
2016/08/19 20:50:33
I had to fight the urge to convert this to an unor
Jeffrey Yasskin
2016/08/19 22:08:09
Oh, ok.
| |
| 130 std::unique_ptr<BluetoothRemoteGattService>> | |
| 131 GattServiceMap; | |
| 120 | 132 |
| 121 // Interface for negotiating pairing of bluetooth devices. | 133 // Interface for negotiating pairing of bluetooth devices. |
| 122 class PairingDelegate { | 134 class PairingDelegate { |
| 123 public: | 135 public: |
| 124 virtual ~PairingDelegate() {} | 136 virtual ~PairingDelegate() {} |
| 125 | 137 |
| 126 // This method will be called when the Bluetooth daemon requires a | 138 // This method will be called when the Bluetooth daemon requires a |
| 127 // PIN Code for authentication of the device |device|, the delegate should | 139 // PIN Code for authentication of the device |device|, the delegate should |
| 128 // obtain the code from the user and call SetPinCode() on the device to | 140 // obtain the code from the user and call SetPinCode() on the device to |
| 129 // provide it, or RejectPairing() or CancelPairing() to reject or cancel | 141 // provide it, or RejectPairing() or CancelPairing() to reject or cancel |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 // UUIDs. | 307 // UUIDs. |
| 296 // * For connected Low Energy Devices for which services have not been | 308 // * For connected Low Energy Devices for which services have not been |
| 297 // discovered returns an empty list. | 309 // discovered returns an empty list. |
| 298 // * For connected Low Energy Devices for which services have been discovered | 310 // * For connected Low Energy Devices for which services have been discovered |
| 299 // returns the UUIDs of the device's services. | 311 // returns the UUIDs of the device's services. |
| 300 // * For dual mode devices this may be collected from both. | 312 // * For dual mode devices this may be collected from both. |
| 301 // | 313 // |
| 302 // Note: On ChromeOS and Linux, Bluez persists all services meaning if | 314 // Note: On ChromeOS and Linux, Bluez persists all services meaning if |
| 303 // a device stops advertising a service this function will still return | 315 // a device stops advertising a service this function will still return |
| 304 // its UUID. | 316 // its UUID. |
| 305 virtual UUIDList GetUUIDs() const; | 317 virtual UUIDSet GetUUIDs() const; |
| 318 | |
| 319 // Returns the last advertised Service Data. Returns nullptr if the adapter | |
| 320 // is not discoverying. | |
|
Jeffrey Yasskin
2016/08/18 19:56:31
sp: discoverying->discovering
ortuno
2016/08/19 20:50:33
Done.
| |
| 321 const ServiceDataMap* GetServiceData() const; | |
| 322 | |
| 323 // Returns the UUIDs of services for which the device advertises Service Data. | |
| 324 // Returns an empty set if the adapter is not discoverying. | |
|
Jeffrey Yasskin
2016/08/18 19:56:31
I think I'd rather return something consistent bet
ortuno
2016/08/19 20:50:33
An empty array means that the service just has no
Jeffrey Yasskin
2016/08/19 22:08:09
I think that's for GetServiceDataForUUID()? I was
ortuno
2016/08/23 00:38:04
Ah right. Changed GetServiceData to return an empt
| |
| 325 UUIDSet GetServiceDataUUIDs() const; | |
| 326 | |
| 327 // Returns a pointer to the Service Data for Service with |uuid|. Returns | |
| 328 // nullptr if |uuid| has no Service Data. | |
| 329 const std::vector<uint8_t>* GetServiceDataForUUID( | |
| 330 const BluetoothUUID& uuid) const; | |
| 306 | 331 |
| 307 // The received signal strength, in dBm. This field is avaliable and valid | 332 // The received signal strength, in dBm. This field is avaliable and valid |
| 308 // only during discovery. | 333 // only during discovery. |
| 309 virtual base::Optional<int8_t> GetInquiryRSSI() const = 0; | 334 virtual base::Optional<int8_t> GetInquiryRSSI() const = 0; |
| 310 | 335 |
| 311 // The transmitted power level. This field is avaliable only for LE devices | 336 // The transmitted power level. This field is avaliable only for LE devices |
| 312 // that include this field in AD. It is avaliable and valid only during | 337 // that include this field in AD. It is avaliable and valid only during |
| 313 // discovery. | 338 // discovery. |
| 314 virtual base::Optional<int8_t> GetInquiryTxPower() const = 0; | 339 virtual base::Optional<int8_t> GetInquiryTxPower() const = 0; |
| 315 | 340 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 virtual bool IsGattServicesDiscoveryComplete() const; | 495 virtual bool IsGattServicesDiscoveryComplete() const; |
| 471 | 496 |
| 472 // Returns the list of discovered GATT services. | 497 // Returns the list of discovered GATT services. |
| 473 virtual std::vector<BluetoothRemoteGattService*> GetGattServices() const; | 498 virtual std::vector<BluetoothRemoteGattService*> GetGattServices() const; |
| 474 | 499 |
| 475 // Returns the GATT service with device-specific identifier |identifier|. | 500 // Returns the GATT service with device-specific identifier |identifier|. |
| 476 // Returns NULL, if no such service exists. | 501 // Returns NULL, if no such service exists. |
| 477 virtual BluetoothRemoteGattService* GetGattService( | 502 virtual BluetoothRemoteGattService* GetGattService( |
| 478 const std::string& identifier) const; | 503 const std::string& identifier) const; |
| 479 | 504 |
| 480 // Returns service data of a service given its UUID. | |
| 481 virtual base::BinaryValue* GetServiceData(BluetoothUUID serviceUUID) const; | |
| 482 | |
| 483 // Returns the list UUIDs of services that have service data. | |
| 484 virtual UUIDList GetServiceDataUUIDs() const; | |
| 485 | |
| 486 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where | 505 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where |
| 487 // each 'X' is a hex digit. If the input |address| is invalid, returns an | 506 // each 'X' is a hex digit. If the input |address| is invalid, returns an |
| 488 // empty string. | 507 // empty string. |
| 489 static std::string CanonicalizeAddress(const std::string& address); | 508 static std::string CanonicalizeAddress(const std::string& address); |
| 490 | 509 |
| 491 // Return the timestamp for when this device was last seen. | 510 // Return the timestamp for when this device was last seen. |
| 492 base::Time GetLastUpdateTime() const { return last_update_time_; } | 511 base::Time GetLastUpdateTime() const { return last_update_time_; } |
| 493 | 512 |
| 494 // Update the last time this device was seen. | 513 // Called by BluetoothAdapter when a new Advertisement is seen for this |
| 495 void UpdateTimestamp(); | 514 // device. This replaces previously seen Advertisement Data. |
| 515 void UpdateAdvertisementData( | |
| 516 std::vector<std::string> advertised_uuids, | |
| 517 std::unordered_map<std::string, std::vector<uint8_t>> service_data); | |
| 518 | |
| 519 // Called by BluetoothAdapter when it stops discoverying. | |
| 520 void ClearAdvertisementData(); | |
| 496 | 521 |
| 497 // Return associated BluetoothAdapter. | 522 // Return associated BluetoothAdapter. |
| 498 BluetoothAdapter* GetAdapter() { return adapter_; } | 523 BluetoothAdapter* GetAdapter() { return adapter_; } |
| 499 | 524 |
| 500 protected: | 525 protected: |
| 501 // BluetoothGattConnection is a friend to call Add/RemoveGattConnection. | 526 // BluetoothGattConnection is a friend to call Add/RemoveGattConnection. |
| 502 friend BluetoothGattConnection; | 527 friend BluetoothGattConnection; |
| 503 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetUUIDs); | |
| 504 FRIEND_TEST_ALL_PREFIXES( | 528 FRIEND_TEST_ALL_PREFIXES( |
| 505 BluetoothTest, | 529 BluetoothTest, |
| 506 BluetoothGattConnection_DisconnectGatt_SimulateConnect); | 530 BluetoothGattConnection_DisconnectGatt_SimulateConnect); |
| 507 FRIEND_TEST_ALL_PREFIXES( | 531 FRIEND_TEST_ALL_PREFIXES( |
| 508 BluetoothTest, | 532 BluetoothTest, |
| 509 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect); | 533 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect); |
| 510 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, | 534 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, |
| 511 BluetoothGattConnection_ErrorAfterConnection); | 535 BluetoothGattConnection_ErrorAfterConnection); |
| 512 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, | 536 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, |
| 513 BluetoothGattConnection_DisconnectGatt_Cleanup); | 537 BluetoothGattConnection_DisconnectGatt_Cleanup); |
| 514 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetName_NullName); | 538 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetName_NullName); |
| 515 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDevices); | 539 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDevices); |
| 516 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDeviceGattConnect); | 540 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDeviceGattConnect); |
| 517 | 541 |
| 542 // Helper class to easily update the sets of UUIDs and them in sycn with the | |
|
Jeffrey Yasskin
2016/08/18 19:56:31
sp: sycn
ortuno
2016/08/19 20:50:33
Done. Heh, I was also missing "keep".
| |
| 543 // set of all the device's UUIDs | |
| 544 class DeviceUUIDs { | |
| 545 public: | |
| 546 DeviceUUIDs(); | |
| 547 ~DeviceUUIDs(); | |
| 548 | |
| 549 // Advertised Service UUIDs functions | |
| 550 void ReplaceAdvertisedUUIDs(std::vector<std::string> new_advertised_uuids); | |
| 551 | |
| 552 void ClearAdvertisedUUIDs(); | |
| 553 | |
| 554 // Service UUIDs functions | |
| 555 void ReplaceServiceUUIDs( | |
| 556 const BluetoothDevice::GattServiceMap& gatt_services); | |
| 557 | |
| 558 void ClearServiceUUIDs(); | |
| 559 | |
| 560 // Returns the union of Advertised UUIDs and Service UUIDs. | |
| 561 UUIDSet GetUUIDs() const; | |
| 562 | |
| 563 private: | |
| 564 void UpdateDeviceUUIDs(); | |
| 565 | |
| 566 BluetoothDevice::UUIDSet advertised_uuids_; | |
| 567 BluetoothDevice::UUIDSet service_uuids_; | |
| 568 BluetoothDevice::UUIDSet device_uuids_; | |
| 569 }; | |
| 570 | |
| 518 BluetoothDevice(BluetoothAdapter* adapter); | 571 BluetoothDevice(BluetoothAdapter* adapter); |
| 519 | 572 |
| 573 // Update the last time this device was seen. | |
| 574 void UpdateTimestamp(); | |
| 575 | |
| 520 // Implements platform specific operations to initiate a GATT connection. | 576 // Implements platform specific operations to initiate a GATT connection. |
| 521 // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or | 577 // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or |
| 522 // DidDisconnectGatt immediately or asynchronously as the connection state | 578 // DidDisconnectGatt immediately or asynchronously as the connection state |
| 523 // changes. | 579 // changes. |
| 524 virtual void CreateGattConnectionImpl() = 0; | 580 virtual void CreateGattConnectionImpl() = 0; |
| 525 | 581 |
| 526 // Disconnects GATT connection on platforms that maintain a specific GATT | 582 // Disconnects GATT connection on platforms that maintain a specific GATT |
| 527 // connection. | 583 // connection. |
| 528 virtual void DisconnectGatt() = 0; | 584 virtual void DisconnectGatt() = 0; |
| 529 | 585 |
| 530 // Calls any pending callbacks for CreateGattConnection based on result of | 586 // Calls any pending callbacks for CreateGattConnection based on result of |
| 531 // subclasses actions initiated in CreateGattConnectionImpl or related | 587 // subclasses actions initiated in CreateGattConnectionImpl or related |
| 532 // disconnection events. These may be called at any time, even multiple times, | 588 // disconnection events. These may be called at any time, even multiple times, |
| 533 // to ensure a change in platform state is correctly tracked. | 589 // to ensure a change in platform state is correctly tracked. |
| 534 // | 590 // |
| 535 // Under normal behavior it is expected that after CreateGattConnectionImpl | 591 // Under normal behavior it is expected that after CreateGattConnectionImpl |
| 536 // an platform will call DidConnectGatt or DidFailToConnectGatt, but not | 592 // an platform will call DidConnectGatt or DidFailToConnectGatt, but not |
| 537 // DidDisconnectGatt. | 593 // DidDisconnectGatt. |
| 538 void DidConnectGatt(); | 594 void DidConnectGatt(); |
| 539 void DidFailToConnectGatt(ConnectErrorCode); | 595 void DidFailToConnectGatt(ConnectErrorCode); |
| 540 void DidDisconnectGatt(); | 596 void DidDisconnectGatt(); |
| 541 | 597 |
| 542 // Tracks BluetoothGattConnection instances that act as a reference count | 598 // Tracks BluetoothGattConnection instances that act as a reference count |
| 543 // keeping the GATT connection open. Instances call Add/RemoveGattConnection | 599 // keeping the GATT connection open. Instances call Add/RemoveGattConnection |
| 544 // at creation & deletion. | 600 // at creation & deletion. |
| 545 void AddGattConnection(BluetoothGattConnection*); | 601 void AddGattConnection(BluetoothGattConnection*); |
| 546 void RemoveGattConnection(BluetoothGattConnection*); | 602 void RemoveGattConnection(BluetoothGattConnection*); |
| 547 | 603 |
| 548 void UpdateServiceUUIDs(); | |
| 549 | |
| 550 // Clears the list of service data. | |
| 551 void ClearServiceData(); | |
| 552 | |
| 553 // Set the data of a given service designated by its UUID. | |
| 554 void SetServiceData(BluetoothUUID serviceUUID, const char* buffer, | |
| 555 size_t size); | |
| 556 | |
| 557 // Update last_update_time_ so that the device appears as expired. | 604 // Update last_update_time_ so that the device appears as expired. |
| 558 void SetAsExpiredForTesting(); | 605 void SetAsExpiredForTesting(); |
| 559 | 606 |
| 560 // Raw pointer to adapter owning this device object. Subclasses use platform | 607 // Raw pointer to adapter owning this device object. Subclasses use platform |
| 561 // specific pointers via adapter_. | 608 // specific pointers via adapter_. |
| 562 BluetoothAdapter* adapter_; | 609 BluetoothAdapter* adapter_; |
| 563 | 610 |
| 564 // Callbacks for pending success and error result of CreateGattConnection. | 611 // Callbacks for pending success and error result of CreateGattConnection. |
| 565 std::vector<GattConnectionCallback> create_gatt_connection_success_callbacks_; | 612 std::vector<GattConnectionCallback> create_gatt_connection_success_callbacks_; |
| 566 std::vector<ConnectErrorCallback> create_gatt_connection_error_callbacks_; | 613 std::vector<ConnectErrorCallback> create_gatt_connection_error_callbacks_; |
| 567 | 614 |
| 568 // BluetoothGattConnection objects keeping the GATT connection alive. | 615 // BluetoothGattConnection objects keeping the GATT connection alive. |
| 569 std::set<BluetoothGattConnection*> gatt_connections_; | 616 std::set<BluetoothGattConnection*> gatt_connections_; |
| 570 | 617 |
| 571 // Mapping from the platform-specific GATT service identifiers to | |
| 572 // BluetoothRemoteGattService objects. | |
| 573 typedef base::ScopedPtrHashMap<std::string, | |
| 574 std::unique_ptr<BluetoothRemoteGattService>> | |
| 575 GattServiceMap; | |
| 576 GattServiceMap gatt_services_; | 618 GattServiceMap gatt_services_; |
| 577 bool gatt_services_discovery_complete_; | 619 bool gatt_services_discovery_complete_; |
| 578 | 620 |
| 579 // Last advertised service UUIDs. Should be empty if the device is connected. | 621 // Class that holds the union of Advertised UUIDs and Service UUIDs. |
| 580 UUIDList advertised_uuids_; | 622 DeviceUUIDs device_uuids_; |
| 581 // UUIDs of the device's services. Should be empty if the device is not | |
| 582 // connected or it's services have not been discovered yet. | |
| 583 UUIDList service_uuids_; | |
| 584 | 623 |
| 585 // Mapping from service UUID represented as a std::string of a bluetooth | 624 // Map of BluetoothUUIDs to their advertised Service Data. |
| 586 // service to | 625 ServiceDataMap service_data_; |
| 587 // the specific data. The data is stored as BinaryValue. | |
| 588 std::unique_ptr<base::DictionaryValue> services_data_; | |
| 589 | 626 |
| 590 // Timestamp for when an advertisement was last seen. | 627 // Timestamp for when an advertisement was last seen. |
| 591 base::Time last_update_time_; | 628 base::Time last_update_time_; |
| 592 | 629 |
| 593 private: | 630 private: |
| 594 // Returns a localized string containing the device's bluetooth address and | 631 // Returns a localized string containing the device's bluetooth address and |
| 595 // a device type for display when |name_| is empty. | 632 // a device type for display when |name_| is empty. |
| 596 base::string16 GetAddressWithLocalizedDeviceTypeName() const; | 633 base::string16 GetAddressWithLocalizedDeviceTypeName() const; |
| 597 }; | 634 }; |
| 598 | 635 |
| 599 } // namespace device | 636 } // namespace device |
| 600 | 637 |
| 601 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 638 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
| OLD | NEW |