Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 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 | |
| 17 | #pragma once |
| 18 | |
Arman Uguray | 3f6aa07 | 2015-11-30 14:58:11 -0800 | [diff] [blame] | 19 | #include <mutex> |
| 20 | #include <unordered_set> |
| 21 | #include <vector> |
| 22 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 23 | #include <base/macros.h> |
| 24 | #include <hardware/bluetooth.h> |
| 25 | #include <hardware/bt_gatt.h> |
| 26 | |
| 27 | namespace bluetooth { |
| 28 | namespace hal { |
| 29 | |
| 30 | // This class represents the standard BT-GATT interface. This class combines |
| 31 | // GATT profile server and client role operations with general GAP profile |
| 32 | // operations of various roles (central, scanner, peripheral, advertiser), |
| 33 | // wrapping around the underlying bt_gatt_interface_t structure. A single |
| 34 | // instance of this class exists per application and it allows multiple classes |
| 35 | // to interface with the global HAL interface by multiplexing callbacks among |
| 36 | // registered clients. |
| 37 | // |
| 38 | // This is declared as an abstract interface so that a fake implementation can |
| 39 | // be injected for testing the upper layer. |
| 40 | class BluetoothGattInterface { |
| 41 | public: |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 42 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 43 | // The HAL interface doesn't allow registering "user data" that carries |
| 44 | // context beyond the callback parameters, forcing implementations to deal |
| 45 | // with global variables. The *Observer interface is to redirect these events |
| 46 | // to interested parties in an object-oriented manner. |
| 47 | |
| 48 | // The standard LE scanner callback interface. |
| 49 | class ScannerObserver { |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 50 | public: |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 51 | |
| 52 | virtual ~ScannerObserver() = default; |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 53 | |
| 54 | // All of the events below correspond to callbacks defined in |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 55 | // "btgatt_scanner_callbacks_t" in the HAL API definitions. |
Arman Uguray | d1ed1f5 | 2015-09-16 17:53:05 -0700 | [diff] [blame] | 56 | |
Jakub Pawlowski | f3246f1 | 2016-10-26 15:31:24 -0700 | [diff] [blame] | 57 | virtual void RegisterScannerCallback( |
| 58 | BluetoothGattInterface* gatt_iface, |
| 59 | int status, int scanner_id, |
| 60 | const bt_uuid_t& app_uuid); |
| 61 | |
Ajay Panicker | f261cf8 | 2015-12-07 14:15:33 -0800 | [diff] [blame] | 62 | virtual void ScanResultCallback( |
| 63 | BluetoothGattInterface* gatt_iface, |
| 64 | const bt_bdaddr_t& bda, int rssi, |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 65 | vector<uint8_t> adv_data); // NOLINT(pass-by-value) |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // The standard BT-GATT client callback interface. |
| 69 | class ClientObserver { |
| 70 | public: |
| 71 | |
| 72 | virtual ~ClientObserver() = default; |
| 73 | |
| 74 | // All of the events below correspond to callbacks defined in |
| 75 | // "bt_gatt_client_callbacks_t" in the HAL API definitions. |
| 76 | |
| 77 | virtual void RegisterClientCallback( |
| 78 | BluetoothGattInterface* gatt_iface, |
| 79 | int status, int client_if, |
| 80 | const bt_uuid_t& app_uuid); |
Ajay Panicker | f261cf8 | 2015-12-07 14:15:33 -0800 | [diff] [blame] | 81 | |
Jakub Pawlowski | ce021dd | 2016-01-19 16:35:20 -0800 | [diff] [blame] | 82 | virtual void ConnectCallback( |
| 83 | BluetoothGattInterface* gatt_iface, |
| 84 | int conn_id, |
| 85 | int status, |
| 86 | int client_if, |
| 87 | const bt_bdaddr_t& bda); |
| 88 | |
| 89 | virtual void DisconnectCallback( |
| 90 | BluetoothGattInterface* gatt_iface, |
| 91 | int conn_id, |
| 92 | int status, |
| 93 | int client_if, |
| 94 | const bt_bdaddr_t& bda); |
| 95 | |
Jakub Pawlowski | 9c68cdf | 2016-01-21 19:44:43 -0800 | [diff] [blame] | 96 | virtual void SearchCompleteCallback( |
| 97 | BluetoothGattInterface* gatt_iface, |
| 98 | int conn_id, |
| 99 | int status); |
| 100 | |
Jakub Pawlowski | c0b1698 | 2016-01-26 09:53:06 -0800 | [diff] [blame] | 101 | virtual void RegisterForNotificationCallback( |
| 102 | BluetoothGattInterface* gatt_iface, |
Jakub Pawlowski | 9dc9985 | 2016-03-17 12:05:54 -0700 | [diff] [blame] | 103 | int conn_id, int status, int registered, uint16_t handle); |
Jakub Pawlowski | c0b1698 | 2016-01-26 09:53:06 -0800 | [diff] [blame] | 104 | |
| 105 | virtual void NotifyCallback( |
| 106 | BluetoothGattInterface* gatt_iface, |
| 107 | int conn_id, btgatt_notify_params_t* p_data); |
| 108 | |
| 109 | virtual void WriteCharacteristicCallback( |
| 110 | BluetoothGattInterface* gatt_iface, |
Jakub Pawlowski | 9dc9985 | 2016-03-17 12:05:54 -0700 | [diff] [blame] | 111 | int conn_id, int status, uint16_t handle); |
Jakub Pawlowski | c0b1698 | 2016-01-26 09:53:06 -0800 | [diff] [blame] | 112 | |
| 113 | virtual void WriteDescriptorCallback( |
| 114 | BluetoothGattInterface* gatt_iface, |
Jakub Pawlowski | 9dc9985 | 2016-03-17 12:05:54 -0700 | [diff] [blame] | 115 | int conn_id, int status, uint16_t handle); |
Jakub Pawlowski | c0b1698 | 2016-01-26 09:53:06 -0800 | [diff] [blame] | 116 | |
Jakub Pawlowski | 9c68cdf | 2016-01-21 19:44:43 -0800 | [diff] [blame] | 117 | virtual void MtuChangedCallback( |
| 118 | BluetoothGattInterface* gatt_iface, |
| 119 | int conn_id, int status, int mtu); |
| 120 | |
Jakub Pawlowski | 6859e0c | 2016-01-22 00:09:10 -0800 | [diff] [blame] | 121 | virtual void GetGattDbCallback( |
| 122 | BluetoothGattInterface* gatt_iface, |
| 123 | int conn_id, |
| 124 | btgatt_db_element_t* gatt_db, |
| 125 | int size); |
Jakub Pawlowski | b937cc0 | 2016-02-11 09:05:14 -0800 | [diff] [blame] | 126 | |
| 127 | virtual void ServicesRemovedCallback( |
| 128 | BluetoothGattInterface* gatt_iface, |
| 129 | int conn_id, |
| 130 | uint16_t start_handle, |
| 131 | uint16_t end_handle); |
| 132 | |
| 133 | virtual void ServicesAddedCallback( |
| 134 | BluetoothGattInterface* gatt_iface, |
| 135 | int conn_id, |
| 136 | btgatt_db_element_t *added, |
| 137 | int added_count); |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Arman Uguray | 9e52053 | 2015-09-22 14:12:50 -0700 | [diff] [blame] | 140 | // The standard BT-GATT server callback interface. |
| 141 | class ServerObserver { |
| 142 | public: |
| 143 | virtual ~ServerObserver() = default; |
| 144 | |
| 145 | virtual void RegisterServerCallback( |
| 146 | BluetoothGattInterface* gatt_iface, |
| 147 | int status, int server_if, |
| 148 | const bt_uuid_t& app_uuid); |
| 149 | |
Arman Uguray | b10f96f | 2015-09-28 17:38:52 -0700 | [diff] [blame] | 150 | virtual void ConnectionCallback( |
| 151 | BluetoothGattInterface* gatt_iface, |
| 152 | int conn_id, int server_if, |
| 153 | int connected, |
| 154 | const bt_bdaddr_t& bda); |
| 155 | |
Arman Uguray | 78a44cd | 2015-09-23 17:23:54 -0700 | [diff] [blame] | 156 | virtual void ServiceAddedCallback( |
| 157 | BluetoothGattInterface* gatt_iface, |
| 158 | int status, int server_if, |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 159 | vector<btgatt_db_element_t> service); // NOLINT(pass-by-value) |
Arman Uguray | 78a44cd | 2015-09-23 17:23:54 -0700 | [diff] [blame] | 160 | |
| 161 | virtual void ServiceStoppedCallback( |
| 162 | BluetoothGattInterface* gatt_iface, |
| 163 | int status, int server_if, |
| 164 | int srvc_handle); |
| 165 | |
Ajay Panicker | f261cf8 | 2015-12-07 14:15:33 -0800 | [diff] [blame] | 166 | virtual void ServiceDeletedCallback( |
| 167 | BluetoothGattInterface* gatt_iface, |
| 168 | int status, int server_if, |
| 169 | int srvc_handle); |
| 170 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 171 | virtual void RequestReadCharacteristicCallback( |
Arman Uguray | b10f96f | 2015-09-28 17:38:52 -0700 | [diff] [blame] | 172 | BluetoothGattInterface* gatt_iface, |
| 173 | int conn_id, int trans_id, |
| 174 | const bt_bdaddr_t& bda, |
| 175 | int attr_handle, int offset, |
| 176 | bool is_long); |
| 177 | |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 178 | virtual void RequestReadDescriptorCallback( |
| 179 | BluetoothGattInterface* gatt_iface, |
| 180 | int conn_id, int trans_id, |
| 181 | const bt_bdaddr_t& bda, |
| 182 | int attr_handle, int offset, |
| 183 | bool is_long); |
| 184 | |
| 185 | virtual void RequestWriteCharacteristicCallback( |
| 186 | BluetoothGattInterface* gatt_iface, |
| 187 | int conn_id, int trans_id, |
| 188 | const bt_bdaddr_t& bda, |
| 189 | int attr_handle, int offset, |
| 190 | bool need_rsp, bool is_prep, |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 191 | vector<uint8_t> value); // NOLINT(pass-by-value) |
Jakub Pawlowski | a641b6f | 2016-03-26 00:47:23 -0700 | [diff] [blame] | 192 | |
| 193 | virtual void RequestWriteDescriptorCallback( |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 194 | BluetoothGattInterface* gatt_iface, |
| 195 | int conn_id, int trans_id, |
| 196 | const bt_bdaddr_t& bda, |
Jakub Pawlowski | 16102de | 2016-05-31 13:14:13 -0700 | [diff] [blame] | 197 | int attr_handle, int offset, |
| 198 | bool need_rsp, bool is_prep, |
Chih-Hung Hsieh | 5dc0d15 | 2016-08-17 14:12:51 -0700 | [diff] [blame] | 199 | vector<uint8_t> value); // NOLINT(pass-by-alue) |
Arman Uguray | 4ebcbd9 | 2015-09-29 22:09:46 -0700 | [diff] [blame] | 200 | |
| 201 | virtual void RequestExecWriteCallback( |
| 202 | BluetoothGattInterface* gatt_iface, |
| 203 | int conn_id, int trans_id, |
| 204 | const bt_bdaddr_t& bda, int exec_write); |
| 205 | |
Jakub Pawlowski | d023549 | 2016-01-26 07:12:46 -0800 | [diff] [blame] | 206 | virtual void ResponseConfirmationCallback( |
| 207 | BluetoothGattInterface* gatt_iface, |
| 208 | int status, |
| 209 | int handle); |
| 210 | |
Arman Uguray | cd644e3 | 2015-10-01 16:36:38 -0700 | [diff] [blame] | 211 | virtual void IndicationSentCallback( |
| 212 | BluetoothGattInterface* gatt_iface, int conn_id, int status); |
| 213 | |
Jakub Pawlowski | 326b602 | 2016-01-27 11:38:54 -0800 | [diff] [blame] | 214 | virtual void MtuChangedCallback( |
| 215 | BluetoothGattInterface* gatt_iface, int conn_id, int mtu); |
Arman Uguray | 9e52053 | 2015-09-22 14:12:50 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 218 | // Initialize and clean up the BluetoothInterface singleton. Returns false if |
| 219 | // the underlying HAL interface failed to initialize, and true on success. |
| 220 | static bool Initialize(); |
| 221 | |
| 222 | // Shuts down and cleans up the interface. CleanUp must be called on the same |
| 223 | // thread that called Initialize. |
| 224 | static void CleanUp(); |
| 225 | |
| 226 | // Returns true if the interface was initialized and a global singleton has |
| 227 | // been created. |
| 228 | static bool IsInitialized(); |
| 229 | |
| 230 | // Initialize for testing. Use this to inject a test version of |
| 231 | // BluetoothGattInterface. To be used from unit tests only. |
| 232 | static void InitializeForTesting(BluetoothGattInterface* test_instance); |
| 233 | |
| 234 | // Returns the BluetoothGattInterface singleton. If the interface has |
Arman Uguray | d1ed1f5 | 2015-09-16 17:53:05 -0700 | [diff] [blame] | 235 | // not been initialized, returns nullptr. This method is thread-safe, in that |
| 236 | // it will block if the internal lock is being held by another thread. Don't |
| 237 | // call this re-entrantly from an observer event as this may cause a deadlock. |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 238 | static BluetoothGattInterface* Get(); |
| 239 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 240 | // Add or remove an observer that is interested in LE scanner interface |
| 241 | // notifications from us. Thread-safety is guaranteed by ObserverList. |
| 242 | virtual void AddScannerObserver(ScannerObserver* observer) = 0; |
| 243 | virtual void RemoveScannerObserver(ScannerObserver* observer) = 0; |
| 244 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 245 | // Add or remove an observer that is interested in GATT client interface |
Jakub Pawlowski | 25689c1 | 2016-01-20 16:24:03 -0800 | [diff] [blame] | 246 | // notifications from us. Thread-safety is guaranteed by ObserverList. |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 247 | virtual void AddClientObserver(ClientObserver* observer) = 0; |
| 248 | virtual void RemoveClientObserver(ClientObserver* observer) = 0; |
| 249 | |
Arman Uguray | 9e52053 | 2015-09-22 14:12:50 -0700 | [diff] [blame] | 250 | // Add or remove an observer that is interested in GATT server interface |
Jakub Pawlowski | 25689c1 | 2016-01-20 16:24:03 -0800 | [diff] [blame] | 251 | // notifications from us. Thread-safety is guaranteed by ObserverList. |
Arman Uguray | 9e52053 | 2015-09-22 14:12:50 -0700 | [diff] [blame] | 252 | virtual void AddServerObserver(ServerObserver* observer) = 0; |
| 253 | virtual void RemoveServerObserver(ServerObserver* observer) = 0; |
| 254 | |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 255 | // The HAL module pointer that represents the standard BT LE advertiser |
| 256 | // interface. This is implemented in and provided by the shared Bluetooth |
| 257 | // library, so this isn't owned by us. |
| 258 | // |
| 259 | // Upper layers can make ble_advertiser_interface_t API calls through this |
| 260 | // structure. |
Jakub Pawlowski | 74ef54b | 2016-10-06 16:52:30 -0700 | [diff] [blame] | 261 | virtual BleAdvertiserInterface* GetAdvertiserHALInterface() const = 0; |
Jakub Pawlowski | 67d5a25 | 2016-07-13 11:55:16 -0700 | [diff] [blame] | 262 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 263 | // The HAL module pointer that represents the standard BT LE scanner |
| 264 | // interface. This is implemented in and provided by the shared Bluetooth |
| 265 | // library, so this isn't owned by us. |
| 266 | // |
| 267 | // Upper layers can make ble_scanner_interface_t API calls through this |
| 268 | // structure. |
| 269 | virtual const btgatt_scanner_interface_t* GetScannerHALInterface() const = 0; |
| 270 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 271 | // The HAL module pointer that represents the standard BT-GATT client |
| 272 | // interface. This is implemented in and provided by the shared Bluetooth |
| 273 | // library, so this isn't owned by us. |
| 274 | // |
| 275 | // Upper layers can make btgatt_client_interface_t API calls through this |
| 276 | // structure. |
| 277 | virtual const btgatt_client_interface_t* GetClientHALInterface() const = 0; |
| 278 | |
Arman Uguray | 9e52053 | 2015-09-22 14:12:50 -0700 | [diff] [blame] | 279 | // The HAL module pointer that represents the standard BT-GATT server |
| 280 | // interface. This is implemented in and provided by the shared Bluetooth |
| 281 | // library, so this isn't owned by us. |
| 282 | // |
| 283 | // Upper layers can make btgatt_server_interface_t API calls through this |
| 284 | // structure. |
| 285 | virtual const btgatt_server_interface_t* GetServerHALInterface() const = 0; |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 286 | |
Arman Uguray | 3f6aa07 | 2015-11-30 14:58:11 -0800 | [diff] [blame] | 287 | // Initiates a regular BLE device scan. This is called internally from each |
| 288 | // LowEnergyClient. This function synchronizes the scan requests and maintains |
| 289 | // an internal reference count for each scan client that is interested. |
| 290 | bt_status_t StartScan(int client_id); |
| 291 | bt_status_t StopScan(int client_id); |
| 292 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 293 | protected: |
| 294 | BluetoothGattInterface() = default; |
| 295 | virtual ~BluetoothGattInterface() = default; |
| 296 | |
| 297 | private: |
Arman Uguray | 3f6aa07 | 2015-11-30 14:58:11 -0800 | [diff] [blame] | 298 | // Used to keep a reference count for the different BLE scan clients. |
| 299 | std::mutex scan_clients_lock_; |
| 300 | std::unordered_set<int> scan_client_set_; |
| 301 | |
Arman Uguray | 9ded7b6 | 2015-08-31 16:29:07 -0700 | [diff] [blame] | 302 | DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterface); |
| 303 | }; |
| 304 | |
| 305 | } // namespace hal |
| 306 | } // namespace bluetooth |