2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <unordered_set>
23 #include <base/macros.h>
24 #include <hardware/bluetooth.h>
25 #include <hardware/bt_gatt.h>
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.
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 {
42 // The standard BT-GATT client callback interface. The HAL interface doesn't
43 // allow registering "user data" that carries context beyond the callback
44 // parameters, forcing implementations to deal with global variables. The
45 // Observer interface is to redirect these events to interested parties in an
46 // object-oriented manner.
47 class ClientObserver {
49 virtual ~ClientObserver() = default;
51 // All of the events below correspond to callbacks defined in
52 // "bt_gatt_client_callbacks_t" in the HAL API definitions.
54 virtual void RegisterClientCallback(
55 BluetoothGattInterface* gatt_iface,
56 int status, int client_if,
57 const bt_uuid_t& app_uuid);
59 virtual void ScanResultCallback(
60 BluetoothGattInterface* gatt_iface,
61 const bt_bdaddr_t& bda, int rssi,
64 virtual void ConnectCallback(
65 BluetoothGattInterface* gatt_iface,
69 const bt_bdaddr_t& bda);
71 virtual void DisconnectCallback(
72 BluetoothGattInterface* gatt_iface,
76 const bt_bdaddr_t& bda);
78 virtual void SearchCompleteCallback(
79 BluetoothGattInterface* gatt_iface,
83 virtual void RegisterForNotificationCallback(
84 BluetoothGattInterface* gatt_iface,
85 int conn_id, int status, int registered, uint16_t handle);
87 virtual void NotifyCallback(
88 BluetoothGattInterface* gatt_iface,
89 int conn_id, btgatt_notify_params_t* p_data);
91 virtual void WriteCharacteristicCallback(
92 BluetoothGattInterface* gatt_iface,
93 int conn_id, int status, uint16_t handle);
95 virtual void WriteDescriptorCallback(
96 BluetoothGattInterface* gatt_iface,
97 int conn_id, int status, uint16_t handle);
99 virtual void ListenCallback(
100 BluetoothGattInterface* gatt_iface,
101 int status, int client_if);
103 virtual void MtuChangedCallback(
104 BluetoothGattInterface* gatt_iface,
105 int conn_id, int status, int mtu);
107 virtual void MultiAdvEnableCallback(
108 BluetoothGattInterface* gatt_iface,
109 int client_if, int status);
111 virtual void MultiAdvUpdateCallback(
112 BluetoothGattInterface* gatt_iface,
113 int client_if, int status);
115 virtual void MultiAdvDataCallback(
116 BluetoothGattInterface* gatt_iface,
117 int client_if, int status);
119 virtual void MultiAdvDisableCallback(
120 BluetoothGattInterface* gatt_iface,
121 int client_if, int status);
123 virtual void GetGattDbCallback(
124 BluetoothGattInterface* gatt_iface,
126 btgatt_db_element_t* gatt_db,
129 virtual void ServicesRemovedCallback(
130 BluetoothGattInterface* gatt_iface,
132 uint16_t start_handle,
133 uint16_t end_handle);
135 virtual void ServicesAddedCallback(
136 BluetoothGattInterface* gatt_iface,
138 btgatt_db_element_t *added,
142 // The standard BT-GATT server callback interface.
143 class ServerObserver {
145 virtual ~ServerObserver() = default;
147 virtual void RegisterServerCallback(
148 BluetoothGattInterface* gatt_iface,
149 int status, int server_if,
150 const bt_uuid_t& app_uuid);
152 virtual void ConnectionCallback(
153 BluetoothGattInterface* gatt_iface,
154 int conn_id, int server_if,
156 const bt_bdaddr_t& bda);
158 virtual void ServiceAddedCallback(
159 BluetoothGattInterface* gatt_iface,
160 int status, int server_if,
161 const btgatt_srvc_id_t& srvc_id,
164 virtual void CharacteristicAddedCallback(
165 BluetoothGattInterface* gatt_iface,
166 int status, int server_if,
167 const bt_uuid_t& uuid,
171 virtual void DescriptorAddedCallback(
172 BluetoothGattInterface* gatt_iface,
173 int status, int server_if,
174 const bt_uuid_t& uuid,
178 virtual void ServiceStartedCallback(
179 BluetoothGattInterface* gatt_iface,
180 int status, int server_if,
183 virtual void ServiceStoppedCallback(
184 BluetoothGattInterface* gatt_iface,
185 int status, int server_if,
188 virtual void ServiceDeletedCallback(
189 BluetoothGattInterface* gatt_iface,
190 int status, int server_if,
193 virtual void RequestReadCallback(
194 BluetoothGattInterface* gatt_iface,
195 int conn_id, int trans_id,
196 const bt_bdaddr_t& bda,
197 int attr_handle, int offset,
200 virtual void RequestWriteCallback(
201 BluetoothGattInterface* gatt_iface,
202 int conn_id, int trans_id,
203 const bt_bdaddr_t& bda,
204 int attr_handle, int offset, int length,
205 bool need_rsp, bool is_prep, uint8_t* value);
207 virtual void RequestExecWriteCallback(
208 BluetoothGattInterface* gatt_iface,
209 int conn_id, int trans_id,
210 const bt_bdaddr_t& bda, int exec_write);
212 virtual void ResponseConfirmationCallback(
213 BluetoothGattInterface* gatt_iface,
217 virtual void IndicationSentCallback(
218 BluetoothGattInterface* gatt_iface, int conn_id, int status);
220 virtual void MtuChangedCallback(
221 BluetoothGattInterface* gatt_iface, int conn_id, int mtu);
224 // Initialize and clean up the BluetoothInterface singleton. Returns false if
225 // the underlying HAL interface failed to initialize, and true on success.
226 static bool Initialize();
228 // Shuts down and cleans up the interface. CleanUp must be called on the same
229 // thread that called Initialize.
230 static void CleanUp();
232 // Returns true if the interface was initialized and a global singleton has
234 static bool IsInitialized();
236 // Initialize for testing. Use this to inject a test version of
237 // BluetoothGattInterface. To be used from unit tests only.
238 static void InitializeForTesting(BluetoothGattInterface* test_instance);
240 // Returns the BluetoothGattInterface singleton. If the interface has
241 // not been initialized, returns nullptr. This method is thread-safe, in that
242 // it will block if the internal lock is being held by another thread. Don't
243 // call this re-entrantly from an observer event as this may cause a deadlock.
244 static BluetoothGattInterface* Get();
246 // Add or remove an observer that is interested in GATT client interface
247 // notifications from us. Thread-safety is guaranteed by ObserverList.
248 virtual void AddClientObserver(ClientObserver* observer) = 0;
249 virtual void RemoveClientObserver(ClientObserver* observer) = 0;
251 // Add or remove an observer that is interested in GATT server interface
252 // notifications from us. Thread-safety is guaranteed by ObserverList.
253 virtual void AddServerObserver(ServerObserver* observer) = 0;
254 virtual void RemoveServerObserver(ServerObserver* observer) = 0;
256 // The HAL module pointer that represents the standard BT-GATT client
257 // interface. This is implemented in and provided by the shared Bluetooth
258 // library, so this isn't owned by us.
260 // Upper layers can make btgatt_client_interface_t API calls through this
262 virtual const btgatt_client_interface_t* GetClientHALInterface() const = 0;
264 // The HAL module pointer that represents the standard BT-GATT server
265 // interface. This is implemented in and provided by the shared Bluetooth
266 // library, so this isn't owned by us.
268 // Upper layers can make btgatt_server_interface_t API calls through this
270 virtual const btgatt_server_interface_t* GetServerHALInterface() const = 0;
272 // Initiates a regular BLE device scan. This is called internally from each
273 // LowEnergyClient. This function synchronizes the scan requests and maintains
274 // an internal reference count for each scan client that is interested.
275 bt_status_t StartScan(int client_id);
276 bt_status_t StopScan(int client_id);
279 BluetoothGattInterface() = default;
280 virtual ~BluetoothGattInterface() = default;
283 // Used to keep a reference count for the different BLE scan clients.
284 std::mutex scan_clients_lock_;
285 std::unordered_set<int> scan_client_set_;
287 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterface);
291 } // namespace bluetooth