| 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; |
| 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, |
| 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 an empty map if the |
| 320 // adapter is not discovering. |
| 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 discovering. |
| 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(UUIDList advertised_uuids, |
| 516 ServiceDataMap service_data); |
| 517 |
| 518 // Called by BluetoothAdapter when it stops discoverying. |
| 519 void ClearAdvertisementData(); |
| 496 | 520 |
| 497 // Return associated BluetoothAdapter. | 521 // Return associated BluetoothAdapter. |
| 498 BluetoothAdapter* GetAdapter() { return adapter_; } | 522 BluetoothAdapter* GetAdapter() { return adapter_; } |
| 499 | 523 |
| 500 protected: | 524 protected: |
| 501 // BluetoothGattConnection is a friend to call Add/RemoveGattConnection. | 525 // BluetoothGattConnection is a friend to call Add/RemoveGattConnection. |
| 502 friend BluetoothGattConnection; | 526 friend BluetoothGattConnection; |
| 503 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetUUIDs); | |
| 504 FRIEND_TEST_ALL_PREFIXES( | 527 FRIEND_TEST_ALL_PREFIXES( |
| 505 BluetoothTest, | 528 BluetoothTest, |
| 506 BluetoothGattConnection_DisconnectGatt_SimulateConnect); | 529 BluetoothGattConnection_DisconnectGatt_SimulateConnect); |
| 507 FRIEND_TEST_ALL_PREFIXES( | 530 FRIEND_TEST_ALL_PREFIXES( |
| 508 BluetoothTest, | 531 BluetoothTest, |
| 509 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect); | 532 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect); |
| 510 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, | 533 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, |
| 511 BluetoothGattConnection_ErrorAfterConnection); | 534 BluetoothGattConnection_ErrorAfterConnection); |
| 512 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, | 535 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, |
| 513 BluetoothGattConnection_DisconnectGatt_Cleanup); | 536 BluetoothGattConnection_DisconnectGatt_Cleanup); |
| 514 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetName_NullName); | 537 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, GetName_NullName); |
| 515 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDevices); | 538 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDevices); |
| 516 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDeviceGattConnect); | 539 FRIEND_TEST_ALL_PREFIXES(BluetoothTest, RemoveOutdatedDeviceGattConnect); |
| 517 | 540 |
| 541 // Helper class to easily update the sets of UUIDs and keep them in sync with |
| 542 // the set of all the device's UUIDs. |
| 543 class DeviceUUIDs { |
| 544 public: |
| 545 DeviceUUIDs(); |
| 546 ~DeviceUUIDs(); |
| 547 |
| 548 // Advertised Service UUIDs functions |
| 549 void ReplaceAdvertisedUUIDs(UUIDList new_advertised_uuids); |
| 550 |
| 551 void ClearAdvertisedUUIDs(); |
| 552 |
| 553 // Service UUIDs functions |
| 554 void ReplaceServiceUUIDs( |
| 555 const BluetoothDevice::GattServiceMap& gatt_services); |
| 556 |
| 557 void ClearServiceUUIDs(); |
| 558 |
| 559 // Returns the union of Advertised UUIDs and Service UUIDs. |
| 560 const UUIDSet& GetUUIDs() const; |
| 561 |
| 562 private: |
| 563 void UpdateDeviceUUIDs(); |
| 564 |
| 565 BluetoothDevice::UUIDSet advertised_uuids_; |
| 566 BluetoothDevice::UUIDSet service_uuids_; |
| 567 BluetoothDevice::UUIDSet device_uuids_; |
| 568 }; |
| 569 |
| 518 BluetoothDevice(BluetoothAdapter* adapter); | 570 BluetoothDevice(BluetoothAdapter* adapter); |
| 519 | 571 |
| 572 // Update the last time this device was seen. |
| 573 void UpdateTimestamp(); |
| 574 |
| 520 // Implements platform specific operations to initiate a GATT connection. | 575 // Implements platform specific operations to initiate a GATT connection. |
| 521 // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or | 576 // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or |
| 522 // DidDisconnectGatt immediately or asynchronously as the connection state | 577 // DidDisconnectGatt immediately or asynchronously as the connection state |
| 523 // changes. | 578 // changes. |
| 524 virtual void CreateGattConnectionImpl() = 0; | 579 virtual void CreateGattConnectionImpl() = 0; |
| 525 | 580 |
| 526 // Disconnects GATT connection on platforms that maintain a specific GATT | 581 // Disconnects GATT connection on platforms that maintain a specific GATT |
| 527 // connection. | 582 // connection. |
| 528 virtual void DisconnectGatt() = 0; | 583 virtual void DisconnectGatt() = 0; |
| 529 | 584 |
| 530 // Calls any pending callbacks for CreateGattConnection based on result of | 585 // Calls any pending callbacks for CreateGattConnection based on result of |
| 531 // subclasses actions initiated in CreateGattConnectionImpl or related | 586 // subclasses actions initiated in CreateGattConnectionImpl or related |
| 532 // disconnection events. These may be called at any time, even multiple times, | 587 // disconnection events. These may be called at any time, even multiple times, |
| 533 // to ensure a change in platform state is correctly tracked. | 588 // to ensure a change in platform state is correctly tracked. |
| 534 // | 589 // |
| 535 // Under normal behavior it is expected that after CreateGattConnectionImpl | 590 // Under normal behavior it is expected that after CreateGattConnectionImpl |
| 536 // an platform will call DidConnectGatt or DidFailToConnectGatt, but not | 591 // an platform will call DidConnectGatt or DidFailToConnectGatt, but not |
| 537 // DidDisconnectGatt. | 592 // DidDisconnectGatt. |
| 538 void DidConnectGatt(); | 593 void DidConnectGatt(); |
| 539 void DidFailToConnectGatt(ConnectErrorCode); | 594 void DidFailToConnectGatt(ConnectErrorCode); |
| 540 void DidDisconnectGatt(); | 595 void DidDisconnectGatt(); |
| 541 | 596 |
| 542 // Tracks BluetoothGattConnection instances that act as a reference count | 597 // Tracks BluetoothGattConnection instances that act as a reference count |
| 543 // keeping the GATT connection open. Instances call Add/RemoveGattConnection | 598 // keeping the GATT connection open. Instances call Add/RemoveGattConnection |
| 544 // at creation & deletion. | 599 // at creation & deletion. |
| 545 void AddGattConnection(BluetoothGattConnection*); | 600 void AddGattConnection(BluetoothGattConnection*); |
| 546 void RemoveGattConnection(BluetoothGattConnection*); | 601 void RemoveGattConnection(BluetoothGattConnection*); |
| 547 | 602 |
| 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. | 603 // Update last_update_time_ so that the device appears as expired. |
| 558 void SetAsExpiredForTesting(); | 604 void SetAsExpiredForTesting(); |
| 559 | 605 |
| 560 // Raw pointer to adapter owning this device object. Subclasses use platform | 606 // Raw pointer to adapter owning this device object. Subclasses use platform |
| 561 // specific pointers via adapter_. | 607 // specific pointers via adapter_. |
| 562 BluetoothAdapter* adapter_; | 608 BluetoothAdapter* adapter_; |
| 563 | 609 |
| 564 // Callbacks for pending success and error result of CreateGattConnection. | 610 // Callbacks for pending success and error result of CreateGattConnection. |
| 565 std::vector<GattConnectionCallback> create_gatt_connection_success_callbacks_; | 611 std::vector<GattConnectionCallback> create_gatt_connection_success_callbacks_; |
| 566 std::vector<ConnectErrorCallback> create_gatt_connection_error_callbacks_; | 612 std::vector<ConnectErrorCallback> create_gatt_connection_error_callbacks_; |
| 567 | 613 |
| 568 // BluetoothGattConnection objects keeping the GATT connection alive. | 614 // BluetoothGattConnection objects keeping the GATT connection alive. |
| 569 std::set<BluetoothGattConnection*> gatt_connections_; | 615 std::set<BluetoothGattConnection*> gatt_connections_; |
| 570 | 616 |
| 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_; | 617 GattServiceMap gatt_services_; |
| 577 bool gatt_services_discovery_complete_; | 618 bool gatt_services_discovery_complete_; |
| 578 | 619 |
| 579 // Last advertised service UUIDs. Should be empty if the device is connected. | 620 // Class that holds the union of Advertised UUIDs and Service UUIDs. |
| 580 UUIDList advertised_uuids_; | 621 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 | 622 |
| 585 // Mapping from service UUID represented as a std::string of a bluetooth | 623 // Map of BluetoothUUIDs to their advertised Service Data. |
| 586 // service to | 624 ServiceDataMap service_data_; |
| 587 // the specific data. The data is stored as BinaryValue. | |
| 588 std::unique_ptr<base::DictionaryValue> services_data_; | |
| 589 | 625 |
| 590 // Timestamp for when an advertisement was last seen. | 626 // Timestamp for when an advertisement was last seen. |
| 591 base::Time last_update_time_; | 627 base::Time last_update_time_; |
| 592 | 628 |
| 593 private: | 629 private: |
| 594 // Returns a localized string containing the device's bluetooth address and | 630 // Returns a localized string containing the device's bluetooth address and |
| 595 // a device type for display when |name_| is empty. | 631 // a device type for display when |name_| is empty. |
| 596 base::string16 GetAddressWithLocalizedDeviceTypeName() const; | 632 base::string16 GetAddressWithLocalizedDeviceTypeName() const; |
| 597 }; | 633 }; |
| 598 | 634 |
| 599 } // namespace device | 635 } // namespace device |
| 600 | 636 |
| 601 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ | 637 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ |
| OLD | NEW |