low_energy_client.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright 2015 Google, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at:
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #pragma once
  17. #include <atomic>
  18. #include <functional>
  19. #include <map>
  20. #include <mutex>
  21. #include <base/macros.h>
  22. #include <bluetooth/uuid.h>
  23. #include "service/bluetooth_instance.h"
  24. #include "service/common/bluetooth/low_energy_constants.h"
  25. #include "service/common/bluetooth/scan_filter.h"
  26. #include "service/common/bluetooth/scan_result.h"
  27. #include "service/common/bluetooth/scan_settings.h"
  28. #include "service/hal/bluetooth_gatt_interface.h"
  29. namespace bluetooth {
  30. struct ConnComparator {
  31. bool operator()(const RawAddress& a, const RawAddress& b) const {
  32. return memcmp(a.address, b.address, RawAddress::kLength) < 0;
  33. }
  34. };
  35. class Adapter;
  36. // A LowEnergyClient represents an application's handle to perform various
  37. // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
  38. // should be obtained through the factory.
  39. class LowEnergyClient : private hal::BluetoothGattInterface::ClientObserver,
  40. public BluetoothInstance {
  41. public:
  42. // The Delegate interface is used to notify asynchronous events related to BLE
  43. // GAP operations.
  44. class Delegate {
  45. public:
  46. Delegate() = default;
  47. virtual ~Delegate() = default;
  48. // Called asynchronously to notify the delegate of connection state change
  49. virtual void OnConnectionState(LowEnergyClient* client, int status,
  50. const char* address, bool connected) = 0;
  51. // Called asynchronously to notify the delegate of mtu change
  52. virtual void OnMtuChanged(LowEnergyClient* client, int status,
  53. const char* address, int mtu) = 0;
  54. private:
  55. DISALLOW_COPY_AND_ASSIGN(Delegate);
  56. };
  57. // The destructor automatically unregisters this client instance from the
  58. // stack.
  59. ~LowEnergyClient() override;
  60. // Assigns a delegate to this instance. |delegate| must out-live this
  61. // LowEnergyClient instance.
  62. void SetDelegate(Delegate* delegate);
  63. // Callback type used to return the result of asynchronous operations below.
  64. using StatusCallback = std::function<void(BLEStatus)>;
  65. // Initiates a BLE connection do device with address |address|. If
  66. // |is_direct| is set, use direct connect procedure. Return true on success
  67. //, false otherwise.
  68. bool Connect(const std::string& address, bool is_direct);
  69. // Disconnect from previously connected BLE device with address |address|.
  70. // Return true on success, false otherwise.
  71. bool Disconnect(const std::string& address);
  72. // Sends request to set MTU to |mtu| for device with address |address|.
  73. // Return true on success, false otherwise.
  74. bool SetMtu(const std::string& address, int mtu);
  75. // BluetoothClientInstace overrides:
  76. const Uuid& GetAppIdentifier() const override;
  77. int GetInstanceId() const override;
  78. private:
  79. friend class LowEnergyClientFactory;
  80. // Constructor shouldn't be called directly as instances are meant to be
  81. // obtained from the factory.
  82. LowEnergyClient(Adapter& adapter, const Uuid& uuid, int client_id);
  83. // BluetoothGattInterface::ClientObserver overrides:
  84. void ConnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
  85. int status, int client_id,
  86. const RawAddress& bda) override;
  87. void DisconnectCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
  88. int status, int client_id,
  89. const RawAddress& bda) override;
  90. void MtuChangedCallback(hal::BluetoothGattInterface* gatt_iface, int conn_id,
  91. int status, int mtu) override;
  92. // Calls and clears the pending callbacks.
  93. void InvokeAndClearStartCallback(BLEStatus status);
  94. void InvokeAndClearStopCallback(BLEStatus status);
  95. // Raw pointer to the Bluetooth Adapter.
  96. Adapter& adapter_;
  97. // See getters above for documentation.
  98. Uuid app_identifier_;
  99. int client_id_;
  100. // Raw handle to the Delegate, which must outlive this LowEnergyClient
  101. // instance.
  102. std::mutex delegate_mutex_;
  103. Delegate* delegate_;
  104. // Protects device connection related members below.
  105. std::mutex connection_fields_lock_;
  106. // Maps bluetooth address to connection id
  107. // TODO(jpawlowski): change type to bimap
  108. std::map<const RawAddress, int, ConnComparator> connection_ids_;
  109. DISALLOW_COPY_AND_ASSIGN(LowEnergyClient);
  110. };
  111. // LowEnergyClientFactory is used to register and obtain a per-application
  112. // LowEnergyClient instance. Users should call RegisterInstance to obtain their
  113. // own unique LowEnergyClient instance that has been registered with the
  114. // Bluetooth stack.
  115. class LowEnergyClientFactory
  116. : private hal::BluetoothGattInterface::ClientObserver,
  117. public BluetoothInstanceFactory {
  118. public:
  119. // Don't construct/destruct directly except in tests. Instead, obtain a handle
  120. // from an Adapter instance.
  121. explicit LowEnergyClientFactory(Adapter& adapter);
  122. ~LowEnergyClientFactory() override;
  123. // BluetoothInstanceFactory override:
  124. bool RegisterInstance(const Uuid& uuid,
  125. const RegisterCallback& callback) override;
  126. private:
  127. friend class LowEnergyClient;
  128. // BluetoothGattInterface::ClientObserver overrides:
  129. void RegisterClientCallback(hal::BluetoothGattInterface* gatt_iface,
  130. int status, int client_id,
  131. const bluetooth::Uuid& app_uuid) override;
  132. // Map of pending calls to register.
  133. std::mutex pending_calls_lock_;
  134. std::map<Uuid, RegisterCallback> pending_calls_;
  135. // Raw pointer to the Adapter that owns this factory.
  136. Adapter& adapter_;
  137. DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory);
  138. };
  139. } // namespace bluetooth