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