blob: 3b7c297e1272ec8efcb1fa629baf4f0795fc12fb [file] [log] [blame]
Arman Uguray9ded7b62015-08-31 16:29:07 -07001//
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 Uguray3f6aa072015-11-30 14:58:11 -080019#include <mutex>
20#include <unordered_set>
21#include <vector>
22
Arman Uguray9ded7b62015-08-31 16:29:07 -070023#include <base/macros.h>
24#include <hardware/bluetooth.h>
25#include <hardware/bt_gatt.h>
26
27namespace bluetooth {
28namespace 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.
40class BluetoothGattInterface {
41 public:
Jakub Pawlowski67d5a252016-07-13 11:55:16 -070042
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070043 // 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 Uguray9ded7b62015-08-31 16:29:07 -070050 public:
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070051
52 virtual ~ScannerObserver() = default;
Arman Uguray9ded7b62015-08-31 16:29:07 -070053
54 // All of the events below correspond to callbacks defined in
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070055 // "btgatt_scanner_callbacks_t" in the HAL API definitions.
Arman Ugurayd1ed1f52015-09-16 17:53:05 -070056
Jakub Pawlowskif3246f12016-10-26 15:31:24 -070057 virtual void RegisterScannerCallback(
58 BluetoothGattInterface* gatt_iface,
59 int status, int scanner_id,
60 const bt_uuid_t& app_uuid);
61
Ajay Panickerf261cf82015-12-07 14:15:33 -080062 virtual void ScanResultCallback(
63 BluetoothGattInterface* gatt_iface,
64 const bt_bdaddr_t& bda, int rssi,
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -070065 vector<uint8_t> adv_data); // NOLINT(pass-by-value)
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070066 };
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 Panickerf261cf82015-12-07 14:15:33 -080081
Jakub Pawlowskice021dd2016-01-19 16:35:20 -080082 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 Pawlowski9c68cdf2016-01-21 19:44:43 -080096 virtual void SearchCompleteCallback(
97 BluetoothGattInterface* gatt_iface,
98 int conn_id,
99 int status);
100
Jakub Pawlowskic0b16982016-01-26 09:53:06 -0800101 virtual void RegisterForNotificationCallback(
102 BluetoothGattInterface* gatt_iface,
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700103 int conn_id, int status, int registered, uint16_t handle);
Jakub Pawlowskic0b16982016-01-26 09:53:06 -0800104
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 Pawlowski9dc99852016-03-17 12:05:54 -0700111 int conn_id, int status, uint16_t handle);
Jakub Pawlowskic0b16982016-01-26 09:53:06 -0800112
113 virtual void WriteDescriptorCallback(
114 BluetoothGattInterface* gatt_iface,
Jakub Pawlowski9dc99852016-03-17 12:05:54 -0700115 int conn_id, int status, uint16_t handle);
Jakub Pawlowskic0b16982016-01-26 09:53:06 -0800116
Jakub Pawlowski9c68cdf2016-01-21 19:44:43 -0800117 virtual void MtuChangedCallback(
118 BluetoothGattInterface* gatt_iface,
119 int conn_id, int status, int mtu);
120
Jakub Pawlowski6859e0c2016-01-22 00:09:10 -0800121 virtual void GetGattDbCallback(
122 BluetoothGattInterface* gatt_iface,
123 int conn_id,
124 btgatt_db_element_t* gatt_db,
125 int size);
Jakub Pawlowskib937cc02016-02-11 09:05:14 -0800126
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 Uguray9ded7b62015-08-31 16:29:07 -0700138 };
139
Arman Uguray9e520532015-09-22 14:12:50 -0700140 // 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 Ugurayb10f96f2015-09-28 17:38:52 -0700150 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 Uguray78a44cd2015-09-23 17:23:54 -0700156 virtual void ServiceAddedCallback(
157 BluetoothGattInterface* gatt_iface,
158 int status, int server_if,
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -0700159 vector<btgatt_db_element_t> service); // NOLINT(pass-by-value)
Arman Uguray78a44cd2015-09-23 17:23:54 -0700160
161 virtual void ServiceStoppedCallback(
162 BluetoothGattInterface* gatt_iface,
163 int status, int server_if,
164 int srvc_handle);
165
Ajay Panickerf261cf82015-12-07 14:15:33 -0800166 virtual void ServiceDeletedCallback(
167 BluetoothGattInterface* gatt_iface,
168 int status, int server_if,
169 int srvc_handle);
170
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700171 virtual void RequestReadCharacteristicCallback(
Arman Ugurayb10f96f2015-09-28 17:38:52 -0700172 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 Pawlowskia641b6f2016-03-26 00:47:23 -0700178 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 Hsieh5dc0d152016-08-17 14:12:51 -0700191 vector<uint8_t> value); // NOLINT(pass-by-value)
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700192
193 virtual void RequestWriteDescriptorCallback(
Arman Uguray4ebcbd92015-09-29 22:09:46 -0700194 BluetoothGattInterface* gatt_iface,
195 int conn_id, int trans_id,
196 const bt_bdaddr_t& bda,
Jakub Pawlowski16102de2016-05-31 13:14:13 -0700197 int attr_handle, int offset,
198 bool need_rsp, bool is_prep,
Chih-Hung Hsieh5dc0d152016-08-17 14:12:51 -0700199 vector<uint8_t> value); // NOLINT(pass-by-alue)
Arman Uguray4ebcbd92015-09-29 22:09:46 -0700200
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 Pawlowskid0235492016-01-26 07:12:46 -0800206 virtual void ResponseConfirmationCallback(
207 BluetoothGattInterface* gatt_iface,
208 int status,
209 int handle);
210
Arman Uguraycd644e32015-10-01 16:36:38 -0700211 virtual void IndicationSentCallback(
212 BluetoothGattInterface* gatt_iface, int conn_id, int status);
213
Jakub Pawlowski326b6022016-01-27 11:38:54 -0800214 virtual void MtuChangedCallback(
215 BluetoothGattInterface* gatt_iface, int conn_id, int mtu);
Arman Uguray9e520532015-09-22 14:12:50 -0700216 };
217
Arman Uguray9ded7b62015-08-31 16:29:07 -0700218 // 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 Ugurayd1ed1f52015-09-16 17:53:05 -0700235 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700238 static BluetoothGattInterface* Get();
239
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700240 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700245 // Add or remove an observer that is interested in GATT client interface
Jakub Pawlowski25689c12016-01-20 16:24:03 -0800246 // notifications from us. Thread-safety is guaranteed by ObserverList.
Arman Uguray9ded7b62015-08-31 16:29:07 -0700247 virtual void AddClientObserver(ClientObserver* observer) = 0;
248 virtual void RemoveClientObserver(ClientObserver* observer) = 0;
249
Arman Uguray9e520532015-09-22 14:12:50 -0700250 // Add or remove an observer that is interested in GATT server interface
Jakub Pawlowski25689c12016-01-20 16:24:03 -0800251 // notifications from us. Thread-safety is guaranteed by ObserverList.
Arman Uguray9e520532015-09-22 14:12:50 -0700252 virtual void AddServerObserver(ServerObserver* observer) = 0;
253 virtual void RemoveServerObserver(ServerObserver* observer) = 0;
254
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700255 // 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 Pawlowski74ef54b2016-10-06 16:52:30 -0700261 virtual BleAdvertiserInterface* GetAdvertiserHALInterface() const = 0;
Jakub Pawlowski67d5a252016-07-13 11:55:16 -0700262
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700263 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700271 // 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 Uguray9e520532015-09-22 14:12:50 -0700279 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700286
Arman Uguray3f6aa072015-11-30 14:58:11 -0800287 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700293 protected:
294 BluetoothGattInterface() = default;
295 virtual ~BluetoothGattInterface() = default;
296
297 private:
Arman Uguray3f6aa072015-11-30 14:58:11 -0800298 // 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 Uguray9ded7b62015-08-31 16:29:07 -0700302 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterface);
303};
304
305} // namespace hal
306} // namespace bluetooth