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 19 #include <mutex> 20 #include <unordered_set> 21 #include <vector> 22 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: 42 // The HAL interface doesn't allow registering "user data" that carries 43 // context beyond the callback parameters, forcing implementations to deal 44 // with global variables. The *Observer interface is to redirect these events 45 // to interested parties in an object-oriented manner. 46 47 // The standard LE scanner callback interface. 48 class ScannerObserver { 49 public: 50 virtual ~ScannerObserver() = default; 51 52 // All of the events below correspond to callbacks defined in 53 // "btgatt_scanner_callbacks_t" in the HAL API definitions. 54 55 virtual void ScanResultCallback( 56 BluetoothGattInterface* gatt_iface, const bt_bdaddr_t& bda, int rssi, 57 std::vector<uint8_t> adv_data); // NOLINT(pass-by-value) 58 }; 59 60 // The standard BT-GATT client callback interface. 61 class ClientObserver { 62 public: 63 virtual ~ClientObserver() = default; 64 65 // All of the events below correspond to callbacks defined in 66 // "bt_gatt_client_callbacks_t" in the HAL API definitions. 67 68 virtual void RegisterClientCallback(BluetoothGattInterface* gatt_iface, 69 int status, int client_if, 70 const bt_uuid_t& app_uuid); 71 72 virtual void ConnectCallback(BluetoothGattInterface* gatt_iface, 73 int conn_id, int status, int client_if, 74 const bt_bdaddr_t& bda); 75 76 virtual void DisconnectCallback(BluetoothGattInterface* gatt_iface, 77 int conn_id, int status, int client_if, 78 const bt_bdaddr_t& bda); 79 80 virtual void SearchCompleteCallback(BluetoothGattInterface* gatt_iface, 81 int conn_id, int status); 82 83 virtual void RegisterForNotificationCallback( 84 BluetoothGattInterface* gatt_iface, int conn_id, int status, 85 int registered, uint16_t handle); 86 87 virtual void NotifyCallback(BluetoothGattInterface* gatt_iface, int conn_id, 88 btgatt_notify_params_t* p_data); 89 90 virtual void WriteCharacteristicCallback(BluetoothGattInterface* gatt_iface, 91 int conn_id, int status, 92 uint16_t handle); 93 94 virtual void WriteDescriptorCallback(BluetoothGattInterface* gatt_iface, 95 int conn_id, int status, 96 uint16_t handle); 97 98 virtual void MtuChangedCallback(BluetoothGattInterface* gatt_iface, 99 int conn_id, int status, int mtu); 100 101 virtual void GetGattDbCallback(BluetoothGattInterface* gatt_iface, 102 int conn_id, btgatt_db_element_t* gatt_db, 103 int size); 104 105 virtual void ServicesRemovedCallback(BluetoothGattInterface* gatt_iface, 106 int conn_id, uint16_t start_handle, 107 uint16_t end_handle); 108 109 virtual void ServicesAddedCallback(BluetoothGattInterface* gatt_iface, 110 int conn_id, btgatt_db_element_t* added, 111 int added_count); 112 }; 113 114 // The standard BT-GATT server callback interface. 115 class ServerObserver { 116 public: 117 virtual ~ServerObserver() = default; 118 119 virtual void RegisterServerCallback(BluetoothGattInterface* gatt_iface, 120 int status, int server_if, 121 const bt_uuid_t& app_uuid); 122 123 virtual void ConnectionCallback(BluetoothGattInterface* gatt_iface, 124 int conn_id, int server_if, int connected, 125 const bt_bdaddr_t& bda); 126 127 virtual void ServiceAddedCallback( 128 BluetoothGattInterface* gatt_iface, int status, int server_if, 129 std::vector<btgatt_db_element_t> service); // NOLINT(pass-by-value) 130 131 virtual void ServiceStoppedCallback(BluetoothGattInterface* gatt_iface, 132 int status, int server_if, 133 int srvc_handle); 134 135 virtual void ServiceDeletedCallback(BluetoothGattInterface* gatt_iface, 136 int status, int server_if, 137 int srvc_handle); 138 139 virtual void RequestReadCharacteristicCallback( 140 BluetoothGattInterface* gatt_iface, int conn_id, int trans_id, 141 const bt_bdaddr_t& bda, int attr_handle, int offset, bool is_long); 142 143 virtual void RequestReadDescriptorCallback( 144 BluetoothGattInterface* gatt_iface, int conn_id, int trans_id, 145 const bt_bdaddr_t& bda, int attr_handle, int offset, bool is_long); 146 147 virtual void RequestWriteCharacteristicCallback( 148 BluetoothGattInterface* gatt_iface, int conn_id, int trans_id, 149 const bt_bdaddr_t& bda, int attr_handle, int offset, bool need_rsp, 150 bool is_prep, 151 std::vector<uint8_t> value); // NOLINT(pass-by-value) 152 153 virtual void RequestWriteDescriptorCallback( 154 BluetoothGattInterface* gatt_iface, int conn_id, int trans_id, 155 const bt_bdaddr_t& bda, int attr_handle, int offset, bool need_rsp, 156 bool is_prep, 157 std::vector<uint8_t> value); // NOLINT(pass-by-alue) 158 159 virtual void RequestExecWriteCallback(BluetoothGattInterface* gatt_iface, 160 int conn_id, int trans_id, 161 const bt_bdaddr_t& bda, 162 int exec_write); 163 164 virtual void ResponseConfirmationCallback( 165 BluetoothGattInterface* gatt_iface, int status, int handle); 166 167 virtual void IndicationSentCallback(BluetoothGattInterface* gatt_iface, 168 int conn_id, int status); 169 170 virtual void MtuChangedCallback(BluetoothGattInterface* gatt_iface, 171 int conn_id, int mtu); 172 }; 173 174 // Initialize and clean up the BluetoothInterface singleton. Returns false if 175 // the underlying HAL interface failed to initialize, and true on success. 176 static bool Initialize(); 177 178 // Shuts down and cleans up the interface. CleanUp must be called on the same 179 // thread that called Initialize. 180 static void CleanUp(); 181 182 // Returns true if the interface was initialized and a global singleton has 183 // been created. 184 static bool IsInitialized(); 185 186 // Initialize for testing. Use this to inject a test version of 187 // BluetoothGattInterface. To be used from unit tests only. 188 static void InitializeForTesting(BluetoothGattInterface* test_instance); 189 190 // Returns the BluetoothGattInterface singleton. If the interface has 191 // not been initialized, returns nullptr. This method is thread-safe, in that 192 // it will block if the internal lock is being held by another thread. Don't 193 // call this re-entrantly from an observer event as this may cause a deadlock. 194 static BluetoothGattInterface* Get(); 195 196 // Add or remove an observer that is interested in LE scanner interface 197 // notifications from us. Thread-safety is guaranteed by ObserverList. 198 virtual void AddScannerObserver(ScannerObserver* observer) = 0; 199 virtual void RemoveScannerObserver(ScannerObserver* observer) = 0; 200 201 // Add or remove an observer that is interested in GATT client interface 202 // notifications from us. Thread-safety is guaranteed by ObserverList. 203 virtual void AddClientObserver(ClientObserver* observer) = 0; 204 virtual void RemoveClientObserver(ClientObserver* observer) = 0; 205 206 // Add or remove an observer that is interested in GATT server interface 207 // notifications from us. Thread-safety is guaranteed by ObserverList. 208 virtual void AddServerObserver(ServerObserver* observer) = 0; 209 virtual void RemoveServerObserver(ServerObserver* observer) = 0; 210 211 // The HAL module pointer that represents the standard BT LE advertiser 212 // interface. This is implemented in and provided by the shared Bluetooth 213 // library, so this isn't owned by us. 214 // 215 // Upper layers can make ble_advertiser_interface_t API calls through this 216 // structure. 217 virtual BleAdvertiserInterface* GetAdvertiserHALInterface() const = 0; 218 219 // The HAL module pointer that represents the standard BT LE scanner 220 // interface. This is implemented in and provided by the shared Bluetooth 221 // library, so this isn't owned by us. 222 // 223 // Upper layers can make ble_scanner_interface_t API calls through this 224 // structure. 225 virtual BleScannerInterface* GetScannerHALInterface() const = 0; 226 227 // The HAL module pointer that represents the standard BT-GATT client 228 // interface. This is implemented in and provided by the shared Bluetooth 229 // library, so this isn't owned by us. 230 // 231 // Upper layers can make btgatt_client_interface_t API calls through this 232 // structure. 233 virtual const btgatt_client_interface_t* GetClientHALInterface() const = 0; 234 235 // The HAL module pointer that represents the standard BT-GATT server 236 // interface. This is implemented in and provided by the shared Bluetooth 237 // library, so this isn't owned by us. 238 // 239 // Upper layers can make btgatt_server_interface_t API calls through this 240 // structure. 241 virtual const btgatt_server_interface_t* GetServerHALInterface() const = 0; 242 243 // Initiates a regular BLE device scan. This is called internally from each 244 // LowEnergyClient. This function synchronizes the scan requests and maintains 245 // an internal reference count for each scan client that is interested. 246 bt_status_t StartScan(int client_id); 247 bt_status_t StopScan(int client_id); 248 249 protected: 250 BluetoothGattInterface() = default; 251 virtual ~BluetoothGattInterface() = default; 252 253 private: 254 // Used to keep a reference count for the different BLE scan clients. 255 std::mutex scan_clients_lock_; 256 std::unordered_set<int> scan_client_set_; 257 258 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterface); 259 }; 260 261 } // namespace hal 262 } // namespace bluetooth 263