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
17 #include "service/hal/bluetooth_gatt_interface.h"
18
19 #include <mutex>
20 #include <shared_mutex>
21
22 #include <base/logging.h>
23 #include <base/observer_list.h>
24
25 #include "abstract_observer_list.h"
26 #include "service/hal/bluetooth_interface.h"
27 #include "service/logging_helpers.h"
28
29 using std::lock_guard;
30 using std::unique_lock;
31 using std::shared_lock;
32 using std::mutex;
33 #if defined(OS_GENERIC) && defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION < 3500)
34 using shared_mutex_impl = std::shared_mutex;
35 #else
36 using shared_mutex_impl = std::shared_timed_mutex;
37 #endif
38
39 namespace bluetooth {
40 namespace hal {
41
42 namespace {
43
44 // The global BluetoothGattInterface instance.
45 BluetoothGattInterface* g_interface = nullptr;
46
47 // Mutex used by callbacks to access |g_interface|. If we initialize or clean it
48 // use unique_lock. If only accessing |g_interface| use shared lock.
49 // TODO(jpawlowski): this should be just shared_mutex, as we currently don't use
50 // timed methods. Change to shared_mutex when we upgrade to C++14
51 shared_mutex_impl g_instance_lock;
52
53 // Helper for obtaining the observer lists. This is forward declared here
54 // and defined below since it depends on BluetoothInterfaceImpl.
55 btbase::AbstractObserverList<BluetoothGattInterface::ScannerObserver>*
56 GetScannerObservers();
57 btbase::AbstractObserverList<BluetoothGattInterface::ClientObserver>*
58 GetClientObservers();
59 btbase::AbstractObserverList<BluetoothGattInterface::ServerObserver>*
60 GetServerObservers();
61
62 #define FOR_EACH_SCANNER_OBSERVER(func) \
63 for (auto& observer : *GetScannerObservers()) { \
64 observer.func; \
65 }
66
67 #define FOR_EACH_CLIENT_OBSERVER(func) \
68 for (auto& observer : *GetClientObservers()) { \
69 observer.func; \
70 }
71
72 #define FOR_EACH_SERVER_OBSERVER(func) \
73 for (auto& observer : *GetServerObservers()) { \
74 observer.func; \
75 }
76
77 #define VERIFY_INTERFACE_OR_RETURN() \
78 do { \
79 if (!g_interface) { \
80 LOG(WARNING) << "Callback received while |g_interface| is NULL"; \
81 return; \
82 } \
83 } while (0)
84
RegisterClientCallback(int status,int client_if,const bluetooth::Uuid & app_uuid)85 void RegisterClientCallback(int status, int client_if,
86 const bluetooth::Uuid& app_uuid) {
87 shared_lock<shared_mutex_impl> lock(g_instance_lock);
88 VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if;
89 VERIFY_INTERFACE_OR_RETURN();
90
91 FOR_EACH_CLIENT_OBSERVER(
92 RegisterClientCallback(g_interface, status, client_if, app_uuid));
93 }
94
ScanResultCallback(uint16_t ble_evt_type,uint8_t addr_type,RawAddress * bda,uint8_t ble_primary_phy,uint8_t ble_secondary_phy,uint8_t ble_advertising_sid,int8_t ble_tx_power,int8_t rssi,uint16_t ble_periodic_adv_int,std::vector<uint8_t> adv_data)95 void ScanResultCallback(
96 uint16_t ble_evt_type, uint8_t addr_type, RawAddress* bda,
97 uint8_t ble_primary_phy, uint8_t ble_secondary_phy,
98 uint8_t ble_advertising_sid, int8_t ble_tx_power, int8_t rssi,
99 uint16_t ble_periodic_adv_int,
100 std::vector<uint8_t> adv_data) { // NOLINT(pass-by-value)
101 shared_lock<shared_mutex_impl> lock(g_instance_lock);
102 VERIFY_INTERFACE_OR_RETURN();
103 CHECK(bda);
104
105 VLOG(2) << __func__ << " - BD_ADDR: " << BtAddrString(bda)
106 << " RSSI: " << rssi;
107 FOR_EACH_SCANNER_OBSERVER(
108 ScanResultCallback(g_interface, *bda, rssi, adv_data));
109 }
110
ConnectCallback(int conn_id,int status,int client_if,const RawAddress & bda)111 void ConnectCallback(int conn_id, int status, int client_if,
112 const RawAddress& bda) {
113 shared_lock<shared_mutex_impl> lock(g_instance_lock);
114 VERIFY_INTERFACE_OR_RETURN();
115
116 VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if
117 << " - BD_ADDR: " << BtAddrString(&bda) << " - conn_id: " << conn_id;
118
119 FOR_EACH_CLIENT_OBSERVER(
120 ConnectCallback(g_interface, conn_id, status, client_if, bda));
121 }
122
DisconnectCallback(int conn_id,int status,int client_if,const RawAddress & bda)123 void DisconnectCallback(int conn_id, int status, int client_if,
124 const RawAddress& bda) {
125 shared_lock<shared_mutex_impl> lock(g_instance_lock);
126 VERIFY_INTERFACE_OR_RETURN();
127
128 VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status
129 << " client_if: " << client_if
130 << " - BD_ADDR: " << BtAddrString(&bda);
131 FOR_EACH_CLIENT_OBSERVER(
132 DisconnectCallback(g_interface, conn_id, status, client_if, bda));
133 }
134
SearchCompleteCallback(int conn_id,int status)135 void SearchCompleteCallback(int conn_id, int status) {
136 shared_lock<shared_mutex_impl> lock(g_instance_lock);
137 VERIFY_INTERFACE_OR_RETURN();
138
139 VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
140 FOR_EACH_CLIENT_OBSERVER(
141 SearchCompleteCallback(g_interface, conn_id, status));
142 }
143
RegisterForNotificationCallback(int conn_id,int registered,int status,uint16_t handle)144 void RegisterForNotificationCallback(int conn_id, int registered, int status,
145 uint16_t handle) {
146 shared_lock<shared_mutex_impl> lock(g_instance_lock);
147 VERIFY_INTERFACE_OR_RETURN();
148
149 LOG(INFO) << __func__ << " - conn_id: " << conn_id << " - status: " << status
150 << " - registered: " << registered << " - handle: " << handle;
151 FOR_EACH_CLIENT_OBSERVER(RegisterForNotificationCallback(
152 g_interface, conn_id, registered, status, handle));
153 }
154
NotifyCallback(int conn_id,const btgatt_notify_params_t & p_data)155 void NotifyCallback(int conn_id, const btgatt_notify_params_t& p_data) {
156 shared_lock<shared_mutex_impl> lock(g_instance_lock);
157 VERIFY_INTERFACE_OR_RETURN();
158
159 VLOG(2) << __func__ << " - conn_id: " << conn_id
160 << " - address: " << BtAddrString(&p_data.bda)
161 << " - handle: " << p_data.handle << " - len: " << p_data.len
162 << " - is_notify: " << p_data.is_notify;
163
164 FOR_EACH_CLIENT_OBSERVER(NotifyCallback(g_interface, conn_id, p_data));
165 }
166
WriteCharacteristicCallback(int conn_id,int status,uint16_t handle)167 void WriteCharacteristicCallback(int conn_id, int status, uint16_t handle) {
168 shared_lock<shared_mutex_impl> lock(g_instance_lock);
169 VERIFY_INTERFACE_OR_RETURN();
170
171 VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
172
173 FOR_EACH_CLIENT_OBSERVER(
174 WriteCharacteristicCallback(g_interface, conn_id, status, handle));
175 }
176
WriteDescriptorCallback(int conn_id,int status,uint16_t handle)177 void WriteDescriptorCallback(int conn_id, int status, uint16_t handle) {
178 shared_lock<shared_mutex_impl> lock(g_instance_lock);
179 VERIFY_INTERFACE_OR_RETURN();
180
181 VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
182
183 FOR_EACH_CLIENT_OBSERVER(
184 WriteDescriptorCallback(g_interface, conn_id, status, handle));
185 }
186
MtuChangedCallback(int conn_id,int status,int mtu)187 void MtuChangedCallback(int conn_id, int status, int mtu) {
188 shared_lock<shared_mutex_impl> lock(g_instance_lock);
189 VERIFY_INTERFACE_OR_RETURN();
190
191 VLOG(2) << __func__ << " - conn_id: " << conn_id << " status: " << status
192 << " mtu: " << mtu;
193
194 FOR_EACH_CLIENT_OBSERVER(
195 MtuChangedCallback(g_interface, conn_id, status, mtu));
196 }
197
GetGattDbCallback(int conn_id,const btgatt_db_element_t * db,int size)198 void GetGattDbCallback(int conn_id, const btgatt_db_element_t* db, int size) {
199 shared_lock<shared_mutex_impl> lock(g_instance_lock);
200 VLOG(2) << __func__ << " - conn_id: " << conn_id << " size: " << size;
201 VERIFY_INTERFACE_OR_RETURN();
202
203 FOR_EACH_CLIENT_OBSERVER(GetGattDbCallback(g_interface, conn_id, db, size));
204 }
205
ServicesRemovedCallback(int conn_id,uint16_t start_handle,uint16_t end_handle)206 void ServicesRemovedCallback(int conn_id, uint16_t start_handle,
207 uint16_t end_handle) {
208 shared_lock<shared_mutex_impl> lock(g_instance_lock);
209 VLOG(2) << __func__ << " - conn_id: " << conn_id
210 << " start_handle: " << start_handle << " end_handle: " << end_handle;
211 VERIFY_INTERFACE_OR_RETURN();
212
213 FOR_EACH_CLIENT_OBSERVER(
214 ServicesRemovedCallback(g_interface, conn_id, start_handle, end_handle));
215 }
216
ServicesAddedCallback(int conn_id,const btgatt_db_element_t & added,int added_count)217 void ServicesAddedCallback(int conn_id, const btgatt_db_element_t& added,
218 int added_count) {
219 shared_lock<shared_mutex_impl> lock(g_instance_lock);
220 VLOG(2) << __func__ << " - conn_id: " << conn_id
221 << " added_count: " << added_count;
222 VERIFY_INTERFACE_OR_RETURN();
223
224 FOR_EACH_CLIENT_OBSERVER(
225 ServicesAddedCallback(g_interface, conn_id, added, added_count));
226 }
227
RegisterServerCallback(int status,int server_if,const bluetooth::Uuid & app_uuid)228 void RegisterServerCallback(int status, int server_if,
229 const bluetooth::Uuid& app_uuid) {
230 shared_lock<shared_mutex_impl> lock(g_instance_lock);
231 VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if;
232 VERIFY_INTERFACE_OR_RETURN();
233
234 FOR_EACH_SERVER_OBSERVER(
235 RegisterServerCallback(g_interface, status, server_if, app_uuid));
236 }
237
ConnectionCallback(int conn_id,int server_if,int connected,const RawAddress & bda)238 void ConnectionCallback(int conn_id, int server_if, int connected,
239 const RawAddress& bda) {
240 shared_lock<shared_mutex_impl> lock(g_instance_lock);
241 VLOG(2) << __func__ << " - conn_id: " << conn_id
242 << " server_if: " << server_if << " connected: " << connected;
243 VERIFY_INTERFACE_OR_RETURN();
244
245 FOR_EACH_SERVER_OBSERVER(
246 ConnectionCallback(g_interface, conn_id, server_if, connected, bda));
247 }
248
ServiceAddedCallback(int status,int server_if,std::vector<btgatt_db_element_t> service)249 void ServiceAddedCallback(
250 int status, int server_if,
251 std::vector<btgatt_db_element_t> service) { // NOLINT(pass-by-value)
252 shared_lock<shared_mutex_impl> lock(g_instance_lock);
253 VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
254 << " count: " << service.size();
255 VERIFY_INTERFACE_OR_RETURN();
256 CHECK(service.size());
257
258 FOR_EACH_SERVER_OBSERVER(
259 ServiceAddedCallback(g_interface, status, server_if, service));
260 }
261
ServiceStoppedCallback(int status,int server_if,int srvc_handle)262 void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
263 shared_lock<shared_mutex_impl> lock(g_instance_lock);
264 VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
265 << " handle: " << srvc_handle;
266 VERIFY_INTERFACE_OR_RETURN();
267
268 FOR_EACH_SERVER_OBSERVER(
269 ServiceStoppedCallback(g_interface, status, server_if, srvc_handle));
270 }
271
ServiceDeletedCallback(int status,int server_if,int srvc_handle)272 void ServiceDeletedCallback(int status, int server_if, int srvc_handle) {
273 shared_lock<shared_mutex_impl> lock(g_instance_lock);
274 VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
275 << " handle: " << srvc_handle;
276 VERIFY_INTERFACE_OR_RETURN();
277
278 FOR_EACH_SERVER_OBSERVER(
279 ServiceDeletedCallback(g_interface, status, server_if, srvc_handle));
280 }
281
RequestReadCharacteristicCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool is_long)282 void RequestReadCharacteristicCallback(int conn_id, int trans_id,
283 const RawAddress& bda, int attr_handle,
284 int offset, bool is_long) {
285 shared_lock<shared_mutex_impl> lock(g_instance_lock);
286 VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
287 << " attr_handle: " << attr_handle << " offset: " << offset
288 << " is_long: " << is_long;
289 VERIFY_INTERFACE_OR_RETURN();
290
291 FOR_EACH_SERVER_OBSERVER(RequestReadCharacteristicCallback(
292 g_interface, conn_id, trans_id, bda, attr_handle, offset, is_long));
293 }
294
RequestReadDescriptorCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool is_long)295 void RequestReadDescriptorCallback(int conn_id, int trans_id,
296 const RawAddress& bda, int attr_handle,
297 int offset, bool is_long) {
298 shared_lock<shared_mutex_impl> lock(g_instance_lock);
299 VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
300 << " attr_handle: " << attr_handle << " offset: " << offset
301 << " is_long: " << is_long;
302 VERIFY_INTERFACE_OR_RETURN();
303
304 FOR_EACH_SERVER_OBSERVER(RequestReadDescriptorCallback(
305 g_interface, conn_id, trans_id, bda, attr_handle, offset, is_long));
306 }
307
RequestWriteCharacteristicCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool need_rsp,bool is_prep,std::vector<uint8_t> value)308 void RequestWriteCharacteristicCallback(int conn_id, int trans_id,
309 const RawAddress& bda, int attr_handle,
310 int offset, bool need_rsp, bool is_prep,
311 std::vector<uint8_t> value) {
312 shared_lock<shared_mutex_impl> lock(g_instance_lock);
313 VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
314 << " attr_handle: " << attr_handle << " offset: " << offset
315 << " length: " << value.size() << " need_rsp: " << need_rsp
316 << " is_prep: " << is_prep;
317 VERIFY_INTERFACE_OR_RETURN();
318
319 FOR_EACH_SERVER_OBSERVER(RequestWriteCharacteristicCallback(
320 g_interface, conn_id, trans_id, bda, attr_handle, offset, need_rsp,
321 is_prep, value));
322 }
323
RequestWriteDescriptorCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool need_rsp,bool is_prep,std::vector<uint8_t> value)324 void RequestWriteDescriptorCallback(
325 int conn_id, int trans_id, const RawAddress& bda, int attr_handle,
326 int offset, bool need_rsp, bool is_prep,
327 std::vector<uint8_t> value) { // NOLINT(pass-by-value)
328 shared_lock<shared_mutex_impl> lock(g_instance_lock);
329 VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
330 << " attr_handle: " << attr_handle << " offset: " << offset
331 << " length: " << value.size() << " need_rsp: " << need_rsp
332 << " is_prep: " << is_prep;
333 VERIFY_INTERFACE_OR_RETURN();
334
335 FOR_EACH_SERVER_OBSERVER(RequestWriteDescriptorCallback(
336 g_interface, conn_id, trans_id, bda, attr_handle, offset, need_rsp,
337 is_prep, value));
338 }
339
RequestExecWriteCallback(int conn_id,int trans_id,const RawAddress & bda,int exec_write)340 void RequestExecWriteCallback(int conn_id, int trans_id, const RawAddress& bda,
341 int exec_write) {
342 shared_lock<shared_mutex_impl> lock(g_instance_lock);
343 VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
344 << " exec_write: " << exec_write;
345 VERIFY_INTERFACE_OR_RETURN();
346
347 FOR_EACH_SERVER_OBSERVER(RequestExecWriteCallback(g_interface, conn_id,
348 trans_id, bda, exec_write));
349 }
350
ResponseConfirmationCallback(int status,int handle)351 void ResponseConfirmationCallback(int status, int handle) {
352 shared_lock<shared_mutex_impl> lock(g_instance_lock);
353 VLOG(2) << __func__ << " - status: " << status << " handle: " << handle;
354 VERIFY_INTERFACE_OR_RETURN();
355
356 FOR_EACH_SERVER_OBSERVER(
357 ResponseConfirmationCallback(g_interface, status, handle));
358 }
359
IndicationSentCallback(int conn_id,int status)360 void IndicationSentCallback(int conn_id, int status) {
361 shared_lock<shared_mutex_impl> lock(g_instance_lock);
362 VLOG(2) << __func__ << " - conn_id: " << conn_id << " status: " << status;
363 VERIFY_INTERFACE_OR_RETURN();
364
365 FOR_EACH_SERVER_OBSERVER(
366 IndicationSentCallback(g_interface, conn_id, status));
367 }
368
MtuChangedCallback(int conn_id,int mtu)369 void MtuChangedCallback(int conn_id, int mtu) {
370 shared_lock<shared_mutex_impl> lock(g_instance_lock);
371 VLOG(2) << __func__ << " - conn_id: " << conn_id << " mtu: " << mtu;
372 VERIFY_INTERFACE_OR_RETURN();
373
374 FOR_EACH_SERVER_OBSERVER(MtuChangedCallback(g_interface, conn_id, mtu));
375 }
376
377 // The HAL Bluetooth GATT client interface callbacks. These signal a mixture of
378 // GATT client-role and GAP events.
379
380 const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
381 ScanResultCallback,
382 nullptr, // batchscan_reports_cb
383 nullptr, // batchscan_threshold_cb
384 nullptr, // track_adv_event_cb
385 };
386
387 const btgatt_client_callbacks_t gatt_client_callbacks = {
388 RegisterClientCallback,
389 ConnectCallback,
390 DisconnectCallback,
391 SearchCompleteCallback,
392 RegisterForNotificationCallback,
393 NotifyCallback,
394 nullptr, // read_characteristic_cb
395 WriteCharacteristicCallback,
396 nullptr, // read_descriptor_cb
397 WriteDescriptorCallback,
398 nullptr, // execute_write_cb
399 nullptr, // read_remote_rssi_cb
400 MtuChangedCallback,
401 nullptr, // congestion_cb
402 GetGattDbCallback,
403 ServicesRemovedCallback,
404 ServicesAddedCallback,
405 nullptr,
406 nullptr,
407 nullptr, // service_changed_cb
408 };
409
410 const btgatt_server_callbacks_t gatt_server_callbacks = {
411 RegisterServerCallback,
412 ConnectionCallback,
413 ServiceAddedCallback,
414 ServiceStoppedCallback,
415 ServiceDeletedCallback,
416 RequestReadCharacteristicCallback,
417 RequestReadDescriptorCallback,
418 RequestWriteCharacteristicCallback,
419 RequestWriteDescriptorCallback,
420 RequestExecWriteCallback,
421 ResponseConfirmationCallback,
422 IndicationSentCallback,
423 nullptr, // congestion_cb
424 MtuChangedCallback,
425 nullptr,
426 nullptr,
427 };
428
429 const btgatt_callbacks_t gatt_callbacks = {
430 sizeof(btgatt_callbacks_t), &gatt_client_callbacks, &gatt_server_callbacks,
431 &gatt_scanner_callbacks,
432 };
433
434 } // namespace
435
436 // BluetoothGattInterface implementation for production.
437 class BluetoothGattInterfaceImpl : public BluetoothGattInterface {
438 public:
BluetoothGattInterfaceImpl()439 BluetoothGattInterfaceImpl() : hal_iface_(nullptr) {}
440
~BluetoothGattInterfaceImpl()441 ~BluetoothGattInterfaceImpl() override {
442 if (hal_iface_) hal_iface_->cleanup();
443 }
444
AddScannerObserver(ScannerObserver * observer)445 void AddScannerObserver(ScannerObserver* observer) override {
446 scanner_observers_.AddObserver(observer);
447 }
448
RemoveScannerObserver(ScannerObserver * observer)449 void RemoveScannerObserver(ScannerObserver* observer) override {
450 scanner_observers_.RemoveObserver(observer);
451 }
452
AddClientObserver(ClientObserver * observer)453 void AddClientObserver(ClientObserver* observer) override {
454 client_observers_.AddObserver(observer);
455 }
456
RemoveClientObserver(ClientObserver * observer)457 void RemoveClientObserver(ClientObserver* observer) override {
458 client_observers_.RemoveObserver(observer);
459 }
460
AddServerObserver(ServerObserver * observer)461 void AddServerObserver(ServerObserver* observer) override {
462 server_observers_.AddObserver(observer);
463 }
464
RemoveServerObserver(ServerObserver * observer)465 void RemoveServerObserver(ServerObserver* observer) override {
466 server_observers_.RemoveObserver(observer);
467 }
468
GetAdvertiserHALInterface() const469 BleAdvertiserInterface* GetAdvertiserHALInterface() const override {
470 return hal_iface_->advertiser;
471 }
472
GetScannerHALInterface() const473 BleScannerInterface* GetScannerHALInterface() const override {
474 return hal_iface_->scanner;
475 }
476
GetClientHALInterface() const477 const btgatt_client_interface_t* GetClientHALInterface() const override {
478 return hal_iface_->client;
479 }
480
GetServerHALInterface() const481 const btgatt_server_interface_t* GetServerHALInterface() const override {
482 return hal_iface_->server;
483 }
484
485 // Initialize the interface.
Initialize()486 bool Initialize() {
487 const bt_interface_t* bt_iface =
488 BluetoothInterface::Get()->GetHALInterface();
489 CHECK(bt_iface);
490
491 const btgatt_interface_t* gatt_iface =
492 reinterpret_cast<const btgatt_interface_t*>(
493 bt_iface->get_profile_interface(BT_PROFILE_GATT_ID));
494 if (!gatt_iface) {
495 LOG(ERROR) << "Failed to obtain HAL GATT interface handle";
496 return false;
497 }
498
499 bt_status_t status = gatt_iface->init(&gatt_callbacks);
500 if (status != BT_STATUS_SUCCESS) {
501 LOG(ERROR) << "Failed to initialize HAL GATT interface";
502 return false;
503 }
504
505 hal_iface_ = gatt_iface;
506
507 return true;
508 }
509
scanner_observers()510 btbase::AbstractObserverList<ScannerObserver>* scanner_observers() {
511 return &scanner_observers_;
512 }
513
client_observers()514 btbase::AbstractObserverList<ClientObserver>* client_observers() {
515 return &client_observers_;
516 }
517
server_observers()518 btbase::AbstractObserverList<ServerObserver>* server_observers() {
519 return &server_observers_;
520 }
521
522 private:
523 // List of observers that are interested in notifications from us.
524 // We're not using a base::ObserverListThreadSafe, which it posts observer
525 // events automatically on the origin threads, as we want to avoid that
526 // overhead and simply forward the events to the upper layer.
527 btbase::AbstractObserverList<ScannerObserver> scanner_observers_;
528 btbase::AbstractObserverList<ClientObserver> client_observers_;
529 btbase::AbstractObserverList<ServerObserver> server_observers_;
530
531 // The HAL handle obtained from the shared library. We hold a weak reference
532 // to this since the actual data resides in the shared Bluetooth library.
533 const btgatt_interface_t* hal_iface_;
534
535 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterfaceImpl);
536 };
537
538 namespace {
539
540 btbase::AbstractObserverList<BluetoothGattInterface::ScannerObserver>*
GetScannerObservers()541 GetScannerObservers() {
542 CHECK(g_interface);
543 return static_cast<BluetoothGattInterfaceImpl*>(g_interface)
544 ->scanner_observers();
545 }
546
547 btbase::AbstractObserverList<BluetoothGattInterface::ClientObserver>*
GetClientObservers()548 GetClientObservers() {
549 CHECK(g_interface);
550 return static_cast<BluetoothGattInterfaceImpl*>(g_interface)
551 ->client_observers();
552 }
553
554 btbase::AbstractObserverList<BluetoothGattInterface::ServerObserver>*
GetServerObservers()555 GetServerObservers() {
556 CHECK(g_interface);
557 return static_cast<BluetoothGattInterfaceImpl*>(g_interface)
558 ->server_observers();
559 }
560
561 } // namespace
562
563 // Default observer implementations. These are provided so that the methods
564 // themselves are optional.
565
ScanResultCallback(BluetoothGattInterface *,const RawAddress &,int,std::vector<uint8_t>)566 void BluetoothGattInterface::ScannerObserver::ScanResultCallback(
567 BluetoothGattInterface* /* gatt_iface */, const RawAddress& /* bda */,
568 int /* rssi */,
569 std::vector<uint8_t> /* adv_data */) { // NOLINT(pass-by-value)
570 // Do Nothing.
571 }
572
RegisterClientCallback(BluetoothGattInterface *,int,int,const bluetooth::Uuid &)573 void BluetoothGattInterface::ClientObserver::RegisterClientCallback(
574 BluetoothGattInterface* /* gatt_iface */, int /* status */,
575 int /* client_if */, const bluetooth::Uuid& /* app_uuid */) {
576 // Do nothing.
577 }
578
ConnectCallback(BluetoothGattInterface *,int,int,int,const RawAddress &)579 void BluetoothGattInterface::ClientObserver::ConnectCallback(
580 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
581 int /* status */, int /* client_if */, const RawAddress& /* bda */) {
582 // Do nothing
583 }
584
DisconnectCallback(BluetoothGattInterface *,int,int,int,const RawAddress &)585 void BluetoothGattInterface::ClientObserver::DisconnectCallback(
586 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
587 int /* status */, int /* client_if */, const RawAddress& /* bda */) {
588 // Do nothing
589 }
590
SearchCompleteCallback(BluetoothGattInterface *,int,int)591 void BluetoothGattInterface::ClientObserver::SearchCompleteCallback(
592 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
593 int /* status */) {
594 // Do nothing
595 }
596
RegisterForNotificationCallback(BluetoothGattInterface *,int,int,int,uint16_t)597 void BluetoothGattInterface::ClientObserver::RegisterForNotificationCallback(
598 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
599 int /* registered */, int /* status */, uint16_t /* handle */) {
600 // Do nothing
601 }
602
NotifyCallback(BluetoothGattInterface *,int,const btgatt_notify_params_t &)603 void BluetoothGattInterface::ClientObserver::NotifyCallback(
604 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
605 const btgatt_notify_params_t& /* p_data */) {
606 // Do nothing
607 }
608
WriteCharacteristicCallback(BluetoothGattInterface *,int,int,uint16_t)609 void BluetoothGattInterface::ClientObserver::WriteCharacteristicCallback(
610 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
611 int /* status */, uint16_t /* handle */) {
612 // Do nothing
613 }
614
WriteDescriptorCallback(BluetoothGattInterface *,int,int,uint16_t)615 void BluetoothGattInterface::ClientObserver::WriteDescriptorCallback(
616 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
617 int /* status */, uint16_t /* handle */) {
618 // Do nothing
619 }
620
MtuChangedCallback(BluetoothGattInterface *,int,int,int)621 void BluetoothGattInterface::ClientObserver::MtuChangedCallback(
622 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
623 int /* statis*/, int /* mtu */) {
624 // Do nothing.
625 }
626
GetGattDbCallback(BluetoothGattInterface *,int,const btgatt_db_element_t *,int)627 void BluetoothGattInterface::ClientObserver::GetGattDbCallback(
628 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
629 const btgatt_db_element_t* /* gatt_db */, int /* size */) {
630 // Do nothing.
631 }
632
ServicesRemovedCallback(BluetoothGattInterface *,int,uint16_t,uint16_t)633 void BluetoothGattInterface::ClientObserver::ServicesRemovedCallback(
634 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
635 uint16_t /* start_handle */, uint16_t /* end_handle */) {
636 // Do nothing.
637 }
638
ServicesAddedCallback(BluetoothGattInterface *,int,const btgatt_db_element_t &,int)639 void BluetoothGattInterface::ClientObserver::ServicesAddedCallback(
640 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
641 const btgatt_db_element_t& /* added */, int /* added_count */) {
642 // Do nothing.
643 }
644
RegisterServerCallback(BluetoothGattInterface *,int,int,const bluetooth::Uuid &)645 void BluetoothGattInterface::ServerObserver::RegisterServerCallback(
646 BluetoothGattInterface* /* gatt_iface */, int /* status */,
647 int /* server_if */, const bluetooth::Uuid& /* app_uuid */) {
648 // Do nothing.
649 }
650
ConnectionCallback(BluetoothGattInterface *,int,int,int,const RawAddress &)651 void BluetoothGattInterface::ServerObserver::ConnectionCallback(
652 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
653 int /* server_if */, int /* connected */, const RawAddress& /* bda */) {
654 // Do nothing.
655 }
656
ServiceAddedCallback(BluetoothGattInterface *,int,int,std::vector<btgatt_db_element_t>)657 void BluetoothGattInterface::ServerObserver::ServiceAddedCallback(
658 BluetoothGattInterface* /* gatt_iface */, int /* status */,
659 int /* server_if */,
660 std::vector<btgatt_db_element_t> /* service */) { // NOLINT(pass-by-value)
661 // Do nothing.
662 }
663
ServiceStoppedCallback(BluetoothGattInterface *,int,int,int)664 void BluetoothGattInterface::ServerObserver::ServiceStoppedCallback(
665 BluetoothGattInterface* /* gatt_iface */, int /* status */,
666 int /* server_if */, int /* srvc_handle */) {
667 // Do nothing.
668 }
669
ServiceDeletedCallback(BluetoothGattInterface *,int,int,int)670 void BluetoothGattInterface::ServerObserver::ServiceDeletedCallback(
671 BluetoothGattInterface* /* gatt_iface */, int /* status */,
672 int /* server_if */, int /* srvc_handle */) {
673 // Do nothing.
674 }
675
RequestReadCharacteristicCallback(BluetoothGattInterface *,int,int,const RawAddress &,int,int,bool)676 void BluetoothGattInterface::ServerObserver::RequestReadCharacteristicCallback(
677 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
678 int /* trans_id */, const RawAddress& /* bda */, int /* attr_handle */,
679 int /* offset */, bool /* is_long */) {
680 // Do nothing.
681 }
682
RequestReadDescriptorCallback(BluetoothGattInterface *,int,int,const RawAddress &,int,int,bool)683 void BluetoothGattInterface::ServerObserver::RequestReadDescriptorCallback(
684 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
685 int /* trans_id */, const RawAddress& /* bda */, int /* attr_handle */,
686 int /* offset */, bool /* is_long */) {
687 // Do nothing.
688 }
689
RequestWriteCharacteristicCallback(BluetoothGattInterface *,int,int,const RawAddress &,int,int,bool,bool,std::vector<uint8_t>)690 void BluetoothGattInterface::ServerObserver::RequestWriteCharacteristicCallback(
691 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
692 int /* trans_id */, const RawAddress& /* bda */, int /* attr_handle */,
693 int /* offset */, bool /* need_rsp */, bool /* is_prep */,
694 std::vector<uint8_t> /* value */) { // NOLINT(pass-by-value)
695 // Do nothing.
696 }
697
RequestWriteDescriptorCallback(BluetoothGattInterface *,int,int,const RawAddress &,int,int,bool,bool,std::vector<uint8_t>)698 void BluetoothGattInterface::ServerObserver::RequestWriteDescriptorCallback(
699 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
700 int /* trans_id */, const RawAddress& /* bda */, int /* attr_handle */,
701 int /* offset */, bool /* need_rsp */, bool /* is_prep */,
702 std::vector<uint8_t> /* value */) { // NOLINT(pass-by-value)
703 // Do nothing.
704 }
705
RequestExecWriteCallback(BluetoothGattInterface *,int,int,const RawAddress &,int)706 void BluetoothGattInterface::ServerObserver::RequestExecWriteCallback(
707 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
708 int /* trans_id */, const RawAddress& /* bda */, int /* exec_write */) {
709 // Do nothing.
710 }
711
ResponseConfirmationCallback(BluetoothGattInterface *,int,int)712 void BluetoothGattInterface::ServerObserver::ResponseConfirmationCallback(
713 BluetoothGattInterface* /* gatt_iface */, int /* status */,
714 int /* handle */) {
715 // Do nothing
716 }
717
IndicationSentCallback(BluetoothGattInterface *,int,int)718 void BluetoothGattInterface::ServerObserver::IndicationSentCallback(
719 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
720 int /* status */) {
721 // Do nothing.
722 }
723
MtuChangedCallback(BluetoothGattInterface *,int,int)724 void BluetoothGattInterface::ServerObserver::MtuChangedCallback(
725 BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
726 int /* mtu */) {
727 // Do nothing.
728 }
729
730 // static
Initialize()731 bool BluetoothGattInterface::Initialize() {
732 unique_lock<shared_mutex_impl> lock(g_instance_lock);
733 CHECK(!g_interface);
734
735 std::unique_ptr<BluetoothGattInterfaceImpl> impl(
736 new BluetoothGattInterfaceImpl());
737 if (!impl->Initialize()) {
738 LOG(ERROR) << "Failed to initialize BluetoothGattInterface";
739 return false;
740 }
741
742 g_interface = impl.release();
743
744 return true;
745 }
746
747 // static
CleanUp()748 void BluetoothGattInterface::CleanUp() {
749 unique_lock<shared_mutex_impl> lock(g_instance_lock);
750 CHECK(g_interface);
751
752 delete g_interface;
753 g_interface = nullptr;
754 }
755
756 // static
IsInitialized()757 bool BluetoothGattInterface::IsInitialized() {
758 shared_lock<shared_mutex_impl> lock(g_instance_lock);
759
760 return g_interface != nullptr;
761 }
762
763 // static
Get()764 BluetoothGattInterface* BluetoothGattInterface::Get() {
765 shared_lock<shared_mutex_impl> lock(g_instance_lock);
766 CHECK(g_interface);
767 return g_interface;
768 }
769
770 // static
InitializeForTesting(BluetoothGattInterface * test_instance)771 void BluetoothGattInterface::InitializeForTesting(
772 BluetoothGattInterface* test_instance) {
773 unique_lock<shared_mutex_impl> lock(g_instance_lock);
774 CHECK(test_instance);
775 CHECK(!g_interface);
776
777 g_interface = test_instance;
778 }
779
StartScan(int client_id)780 bt_status_t BluetoothGattInterface::StartScan(int client_id) {
781 lock_guard<mutex> lock(scan_clients_lock_);
782
783 // Scan already initiated for this client.
784 if (scan_client_set_.find(client_id) != scan_client_set_.end()) {
785 // Assume starting scan multiple times is not error, but warn user.
786 LOG(WARNING) << "Scan already initiated for client";
787 return BT_STATUS_SUCCESS;
788 }
789
790 // If this is the first scan client, then make a call into the stack. We
791 // only do this when the reference count changes to or from 0.
792 if (scan_client_set_.empty()) {
793 GetScannerHALInterface()->Scan(true);
794 }
795
796 scan_client_set_.insert(client_id);
797
798 return BT_STATUS_SUCCESS;
799 }
800
StopScan(int client_id)801 bt_status_t BluetoothGattInterface::StopScan(int client_id) {
802 lock_guard<mutex> lock(scan_clients_lock_);
803
804 // Scan not initiated for this client.
805 auto iter = scan_client_set_.find(client_id);
806 if (iter == scan_client_set_.end()) {
807 // Assume stopping scan multiple times is not error, but warn user.
808 LOG(WARNING) << "Scan already stopped or not initiated for client";
809 return BT_STATUS_SUCCESS;
810 }
811
812 if (scan_client_set_.size() == 1) {
813 GetScannerHALInterface()->Scan(false);
814 }
815
816 scan_client_set_.erase(iter);
817 return BT_STATUS_SUCCESS;
818 }
819
820 } // namespace hal
821 } // namespace bluetooth
822