• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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_EACH_OBSERVER(BluetoothGattInterface::ScannerObserver, \
63                     *GetScannerObservers(), func)
64 
65 #define FOR_EACH_CLIENT_OBSERVER(func)                      \
66   FOR_EACH_OBSERVER(BluetoothGattInterface::ClientObserver, \
67                     *GetClientObservers(), func)
68 
69 #define FOR_EACH_SERVER_OBSERVER(func)                      \
70   FOR_EACH_OBSERVER(BluetoothGattInterface::ServerObserver, \
71                     *GetServerObservers(), func)
72 
73 #define VERIFY_INTERFACE_OR_RETURN()                                   \
74   do {                                                                 \
75     if (!g_interface) {                                                \
76       LOG(WARNING) << "Callback received while |g_interface| is NULL"; \
77       return;                                                          \
78     }                                                                  \
79   } while (0)
80 
RegisterClientCallback(int status,int client_if,bt_uuid_t * app_uuid)81 void RegisterClientCallback(int status, int client_if, bt_uuid_t* app_uuid) {
82   shared_lock<shared_mutex_impl> lock(g_instance_lock);
83   VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if;
84   VERIFY_INTERFACE_OR_RETURN();
85   CHECK(app_uuid);
86 
87   FOR_EACH_CLIENT_OBSERVER(
88       RegisterClientCallback(g_interface, status, client_if, *app_uuid));
89 }
90 
ScanResultCallback(uint16_t ble_evt_type,uint8_t addr_type,bt_bdaddr_t * 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)91 void ScanResultCallback(
92     uint16_t ble_evt_type, uint8_t addr_type, bt_bdaddr_t* bda,
93     uint8_t ble_primary_phy, uint8_t ble_secondary_phy,
94     uint8_t ble_advertising_sid, int8_t ble_tx_power, int8_t rssi,
95     uint16_t ble_periodic_adv_int,
96     std::vector<uint8_t> adv_data) {  // NOLINT(pass-by-value)
97   shared_lock<shared_mutex_impl> lock(g_instance_lock);
98   VERIFY_INTERFACE_OR_RETURN();
99   CHECK(bda);
100 
101   VLOG(2) << __func__ << " - BD_ADDR: " << BtAddrString(bda)
102           << " RSSI: " << rssi;
103   FOR_EACH_SCANNER_OBSERVER(
104       ScanResultCallback(g_interface, *bda, rssi, adv_data));
105 }
106 
ConnectCallback(int conn_id,int status,int client_if,bt_bdaddr_t * bda)107 void ConnectCallback(int conn_id, int status, int client_if, bt_bdaddr_t* bda) {
108   shared_lock<shared_mutex_impl> lock(g_instance_lock);
109   VERIFY_INTERFACE_OR_RETURN();
110   CHECK(bda);
111 
112   VLOG(2) << __func__ << " - status: " << status << " client_if: " << client_if
113           << " - BD_ADDR: " << BtAddrString(bda) << " - conn_id: " << conn_id;
114 
115   FOR_EACH_CLIENT_OBSERVER(
116       ConnectCallback(g_interface, conn_id, status, client_if, *bda));
117 }
118 
DisconnectCallback(int conn_id,int status,int client_if,bt_bdaddr_t * bda)119 void DisconnectCallback(int conn_id, int status, int client_if,
120                         bt_bdaddr_t* bda) {
121   shared_lock<shared_mutex_impl> lock(g_instance_lock);
122   VERIFY_INTERFACE_OR_RETURN();
123   CHECK(bda);
124 
125   VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status
126           << " client_if: " << client_if << " - BD_ADDR: " << BtAddrString(bda);
127   FOR_EACH_CLIENT_OBSERVER(
128       DisconnectCallback(g_interface, conn_id, status, client_if, *bda));
129 }
130 
SearchCompleteCallback(int conn_id,int status)131 void SearchCompleteCallback(int conn_id, int status) {
132   shared_lock<shared_mutex_impl> lock(g_instance_lock);
133   VERIFY_INTERFACE_OR_RETURN();
134 
135   VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
136   FOR_EACH_CLIENT_OBSERVER(
137       SearchCompleteCallback(g_interface, conn_id, status));
138 }
139 
RegisterForNotificationCallback(int conn_id,int registered,int status,uint16_t handle)140 void RegisterForNotificationCallback(int conn_id, int registered, int status,
141                                      uint16_t handle) {
142   shared_lock<shared_mutex_impl> lock(g_instance_lock);
143   VERIFY_INTERFACE_OR_RETURN();
144 
145   LOG(INFO) << __func__ << " - conn_id: " << conn_id << " - status: " << status
146             << " - registered: " << registered << " - handle: " << handle;
147   FOR_EACH_CLIENT_OBSERVER(RegisterForNotificationCallback(
148       g_interface, conn_id, status, registered, handle));
149 }
150 
NotifyCallback(int conn_id,btgatt_notify_params_t * p_data)151 void NotifyCallback(int conn_id, btgatt_notify_params_t* p_data) {
152   shared_lock<shared_mutex_impl> lock(g_instance_lock);
153   VERIFY_INTERFACE_OR_RETURN();
154 
155   VLOG(2) << __func__ << " - conn_id: " << conn_id
156           << " - address: " << BtAddrString(&p_data->bda)
157           << " - handle: " << p_data->handle << " - len: " << p_data->len
158           << " - is_notify: " << p_data->is_notify;
159 
160   FOR_EACH_CLIENT_OBSERVER(NotifyCallback(g_interface, conn_id, p_data));
161 }
162 
WriteCharacteristicCallback(int conn_id,int status,uint16_t handle)163 void WriteCharacteristicCallback(int conn_id, int status, uint16_t handle) {
164   shared_lock<shared_mutex_impl> lock(g_instance_lock);
165   VERIFY_INTERFACE_OR_RETURN();
166 
167   VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
168 
169   FOR_EACH_CLIENT_OBSERVER(
170       WriteCharacteristicCallback(g_interface, conn_id, status, handle));
171 }
172 
WriteDescriptorCallback(int conn_id,int status,uint16_t handle)173 void WriteDescriptorCallback(int conn_id, int status, uint16_t handle) {
174   shared_lock<shared_mutex_impl> lock(g_instance_lock);
175   VERIFY_INTERFACE_OR_RETURN();
176 
177   VLOG(2) << __func__ << " - conn_id: " << conn_id << " - status: " << status;
178 
179   FOR_EACH_CLIENT_OBSERVER(
180       WriteDescriptorCallback(g_interface, conn_id, status, handle));
181 }
182 
MtuChangedCallback(int conn_id,int status,int mtu)183 void MtuChangedCallback(int conn_id, int status, int mtu) {
184   shared_lock<shared_mutex_impl> lock(g_instance_lock);
185   VERIFY_INTERFACE_OR_RETURN();
186 
187   VLOG(2) << __func__ << " - conn_id: " << conn_id << " status: " << status
188           << " mtu: " << mtu;
189 
190   FOR_EACH_CLIENT_OBSERVER(
191       MtuChangedCallback(g_interface, conn_id, status, mtu));
192 }
193 
GetGattDbCallback(int conn_id,btgatt_db_element_t * db,int size)194 void GetGattDbCallback(int conn_id, btgatt_db_element_t* db, int size) {
195   shared_lock<shared_mutex_impl> lock(g_instance_lock);
196   VLOG(2) << __func__ << " - conn_id: " << conn_id << " size: " << size;
197   VERIFY_INTERFACE_OR_RETURN();
198 
199   FOR_EACH_CLIENT_OBSERVER(GetGattDbCallback(g_interface, conn_id, db, size));
200 }
201 
ServicesRemovedCallback(int conn_id,uint16_t start_handle,uint16_t end_handle)202 void ServicesRemovedCallback(int conn_id, uint16_t start_handle,
203                              uint16_t end_handle) {
204   shared_lock<shared_mutex_impl> lock(g_instance_lock);
205   VLOG(2) << __func__ << " - conn_id: " << conn_id
206           << " start_handle: " << start_handle << " end_handle: " << end_handle;
207   VERIFY_INTERFACE_OR_RETURN();
208 
209   FOR_EACH_CLIENT_OBSERVER(
210       ServicesRemovedCallback(g_interface, conn_id, start_handle, end_handle));
211 }
212 
ServicesAddedCallback(int conn_id,btgatt_db_element_t * added,int added_count)213 void ServicesAddedCallback(int conn_id, btgatt_db_element_t* added,
214                            int added_count) {
215   shared_lock<shared_mutex_impl> lock(g_instance_lock);
216   VLOG(2) << __func__ << " - conn_id: " << conn_id
217           << " added_count: " << added_count;
218   VERIFY_INTERFACE_OR_RETURN();
219 
220   FOR_EACH_CLIENT_OBSERVER(
221       ServicesAddedCallback(g_interface, conn_id, added, added_count));
222 }
223 
RegisterServerCallback(int status,int server_if,bt_uuid_t * app_uuid)224 void RegisterServerCallback(int status, int server_if, bt_uuid_t* app_uuid) {
225   shared_lock<shared_mutex_impl> lock(g_instance_lock);
226   VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if;
227   VERIFY_INTERFACE_OR_RETURN();
228   CHECK(app_uuid);
229 
230   FOR_EACH_SERVER_OBSERVER(
231       RegisterServerCallback(g_interface, status, server_if, *app_uuid));
232 }
233 
ConnectionCallback(int conn_id,int server_if,int connected,bt_bdaddr_t * bda)234 void ConnectionCallback(int conn_id, int server_if, int connected,
235                         bt_bdaddr_t* bda) {
236   shared_lock<shared_mutex_impl> lock(g_instance_lock);
237   VLOG(2) << __func__ << " - conn_id: " << conn_id
238           << " server_if: " << server_if << " connected: " << connected;
239   VERIFY_INTERFACE_OR_RETURN();
240   CHECK(bda);
241 
242   FOR_EACH_SERVER_OBSERVER(
243       ConnectionCallback(g_interface, conn_id, server_if, connected, *bda));
244 }
245 
ServiceAddedCallback(int status,int server_if,std::vector<btgatt_db_element_t> service)246 void ServiceAddedCallback(
247     int status, int server_if,
248     std::vector<btgatt_db_element_t> service) {  // NOLINT(pass-by-value)
249   shared_lock<shared_mutex_impl> lock(g_instance_lock);
250   VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
251           << " count: " << service.size();
252   VERIFY_INTERFACE_OR_RETURN();
253   CHECK(service.size());
254 
255   FOR_EACH_SERVER_OBSERVER(
256       ServiceAddedCallback(g_interface, status, server_if, service));
257 }
258 
ServiceStoppedCallback(int status,int server_if,int srvc_handle)259 void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
260   shared_lock<shared_mutex_impl> lock(g_instance_lock);
261   VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
262           << " handle: " << srvc_handle;
263   VERIFY_INTERFACE_OR_RETURN();
264 
265   FOR_EACH_SERVER_OBSERVER(
266       ServiceStoppedCallback(g_interface, status, server_if, srvc_handle));
267 }
268 
ServiceDeletedCallback(int status,int server_if,int srvc_handle)269 void ServiceDeletedCallback(int status, int server_if, int srvc_handle) {
270   shared_lock<shared_mutex_impl> lock(g_instance_lock);
271   VLOG(2) << __func__ << " - status: " << status << " server_if: " << server_if
272           << " handle: " << srvc_handle;
273   VERIFY_INTERFACE_OR_RETURN();
274 
275   FOR_EACH_SERVER_OBSERVER(
276       ServiceDeletedCallback(g_interface, status, server_if, srvc_handle));
277 }
278 
RequestReadCharacteristicCallback(int conn_id,int trans_id,bt_bdaddr_t * bda,int attr_handle,int offset,bool is_long)279 void RequestReadCharacteristicCallback(int conn_id, int trans_id,
280                                        bt_bdaddr_t* bda, int attr_handle,
281                                        int offset, bool is_long) {
282   shared_lock<shared_mutex_impl> lock(g_instance_lock);
283   VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
284           << " attr_handle: " << attr_handle << " offset: " << offset
285           << " is_long: " << is_long;
286   VERIFY_INTERFACE_OR_RETURN();
287   CHECK(bda);
288 
289   FOR_EACH_SERVER_OBSERVER(RequestReadCharacteristicCallback(
290       g_interface, conn_id, trans_id, *bda, attr_handle, offset, is_long));
291 }
292 
RequestReadDescriptorCallback(int conn_id,int trans_id,bt_bdaddr_t * bda,int attr_handle,int offset,bool is_long)293 void RequestReadDescriptorCallback(int conn_id, int trans_id, bt_bdaddr_t* bda,
294                                    int attr_handle, int offset, bool is_long) {
295   shared_lock<shared_mutex_impl> lock(g_instance_lock);
296   VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
297           << " attr_handle: " << attr_handle << " offset: " << offset
298           << " is_long: " << is_long;
299   VERIFY_INTERFACE_OR_RETURN();
300   CHECK(bda);
301 
302   FOR_EACH_SERVER_OBSERVER(RequestReadDescriptorCallback(
303       g_interface, conn_id, trans_id, *bda, attr_handle, offset, is_long));
304 }
305 
RequestWriteCharacteristicCallback(int conn_id,int trans_id,bt_bdaddr_t * bda,int attr_handle,int offset,bool need_rsp,bool is_prep,std::vector<uint8_t> value)306 void RequestWriteCharacteristicCallback(int conn_id, int trans_id,
307                                         bt_bdaddr_t* bda, int attr_handle,
308                                         int offset, bool need_rsp, bool is_prep,
309                                         std::vector<uint8_t> value) {
310   shared_lock<shared_mutex_impl> lock(g_instance_lock);
311   VLOG(2) << __func__ << " - conn_id: " << conn_id << " trans_id: " << trans_id
312           << " attr_handle: " << attr_handle << " offset: " << offset
313           << " length: " << value.size() << " need_rsp: " << need_rsp
314           << " is_prep: " << is_prep;
315   VERIFY_INTERFACE_OR_RETURN();
316   CHECK(bda);
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,bt_bdaddr_t * 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, bt_bdaddr_t* bda, int attr_handle, int offset,
325     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   CHECK(bda);
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,bt_bdaddr_t * bda,int exec_write)340 void RequestExecWriteCallback(int conn_id, int trans_id, bt_bdaddr_t* 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   CHECK(bda);
347 
348   FOR_EACH_SERVER_OBSERVER(RequestExecWriteCallback(
349       g_interface, conn_id, trans_id, *bda, exec_write));
350 }
351 
ResponseConfirmationCallback(int status,int handle)352 void ResponseConfirmationCallback(int status, int handle) {
353   shared_lock<shared_mutex_impl> lock(g_instance_lock);
354   VLOG(2) << __func__ << " - status: " << status << " handle: " << handle;
355   VERIFY_INTERFACE_OR_RETURN();
356 
357   FOR_EACH_SERVER_OBSERVER(
358       ResponseConfirmationCallback(g_interface, status, handle));
359 }
360 
IndicationSentCallback(int conn_id,int status)361 void IndicationSentCallback(int conn_id, int status) {
362   shared_lock<shared_mutex_impl> lock(g_instance_lock);
363   VLOG(2) << __func__ << " - conn_id: " << conn_id << " status: " << status;
364   VERIFY_INTERFACE_OR_RETURN();
365 
366   FOR_EACH_SERVER_OBSERVER(
367       IndicationSentCallback(g_interface, conn_id, status));
368 }
369 
MtuChangedCallback(int conn_id,int mtu)370 void MtuChangedCallback(int conn_id, int mtu) {
371   shared_lock<shared_mutex_impl> lock(g_instance_lock);
372   VLOG(2) << __func__ << " - conn_id: " << conn_id << " mtu: " << mtu;
373   VERIFY_INTERFACE_OR_RETURN();
374 
375   FOR_EACH_SERVER_OBSERVER(MtuChangedCallback(g_interface, conn_id, mtu));
376 }
377 
378 // The HAL Bluetooth GATT client interface callbacks. These signal a mixture of
379 // GATT client-role and GAP events.
380 
381 const btgatt_scanner_callbacks_t gatt_scanner_callbacks = {
382     ScanResultCallback,
383     nullptr,  // batchscan_reports_cb
384     nullptr,  // batchscan_threshold_cb
385     nullptr,  // track_adv_event_cb
386 };
387 
388 const btgatt_client_callbacks_t gatt_client_callbacks = {
389     RegisterClientCallback,
390     ConnectCallback,
391     DisconnectCallback,
392     SearchCompleteCallback,
393     RegisterForNotificationCallback,
394     NotifyCallback,
395     nullptr,  // read_characteristic_cb
396     WriteCharacteristicCallback,
397     nullptr,  // read_descriptor_cb
398     WriteDescriptorCallback,
399     nullptr,  // execute_write_cb
400     nullptr,  // read_remote_rssi_cb
401     MtuChangedCallback,
402     nullptr,  // congestion_cb
403     GetGattDbCallback,
404     ServicesRemovedCallback,
405     ServicesAddedCallback,
406     nullptr,
407     nullptr,
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   base::ObserverList<ScannerObserver>* scanner_observers() {
511     return &scanner_observers_;
512   }
513 
client_observers()514   base::ObserverList<ClientObserver>* client_observers() {
515     return &client_observers_;
516   }
517 
server_observers()518   base::ObserverList<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   base::ObserverList<ScannerObserver> scanner_observers_;
528   base::ObserverList<ClientObserver> client_observers_;
529   base::ObserverList<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 base::ObserverList<BluetoothGattInterface::ScannerObserver>*
GetScannerObservers()541 GetScannerObservers() {
542   CHECK(g_interface);
543   return static_cast<BluetoothGattInterfaceImpl*>(g_interface)
544       ->scanner_observers();
545 }
546 
547 base::ObserverList<BluetoothGattInterface::ClientObserver>*
GetClientObservers()548 GetClientObservers() {
549   CHECK(g_interface);
550   return static_cast<BluetoothGattInterfaceImpl*>(g_interface)
551       ->client_observers();
552 }
553 
554 base::ObserverList<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 bt_bdaddr_t &,int,std::vector<uint8_t>)566 void BluetoothGattInterface::ScannerObserver::ScanResultCallback(
567     BluetoothGattInterface* /* gatt_iface */, const bt_bdaddr_t& /* 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 bt_uuid_t &)573 void BluetoothGattInterface::ClientObserver::RegisterClientCallback(
574     BluetoothGattInterface* /* gatt_iface */, int /* status */,
575     int /* client_if */, const bt_uuid_t& /* app_uuid */) {
576   // Do nothing.
577 }
578 
ConnectCallback(BluetoothGattInterface *,int,int,int,const bt_bdaddr_t &)579 void BluetoothGattInterface::ClientObserver::ConnectCallback(
580     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
581     int /* status */, int /* client_if */, const bt_bdaddr_t& /* bda */) {
582   // Do nothing
583 }
584 
DisconnectCallback(BluetoothGattInterface *,int,int,int,const bt_bdaddr_t &)585 void BluetoothGattInterface::ClientObserver::DisconnectCallback(
586     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
587     int /* status */, int /* client_if */, const bt_bdaddr_t& /* 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 /* status */, int /* registered */, uint16_t /* handle */) {
600   // Do nothing
601 }
602 
NotifyCallback(BluetoothGattInterface *,int,btgatt_notify_params_t *)603 void BluetoothGattInterface::ClientObserver::NotifyCallback(
604     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
605     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,btgatt_db_element_t *,int)627 void BluetoothGattInterface::ClientObserver::GetGattDbCallback(
628     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
629     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,btgatt_db_element_t *,int)639 void BluetoothGattInterface::ClientObserver::ServicesAddedCallback(
640     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
641     btgatt_db_element_t* /* added */, int /* added_count */) {
642   // Do nothing.
643 }
644 
RegisterServerCallback(BluetoothGattInterface *,int,int,const bt_uuid_t &)645 void BluetoothGattInterface::ServerObserver::RegisterServerCallback(
646     BluetoothGattInterface* /* gatt_iface */, int /* status */,
647     int /* server_if */, const bt_uuid_t& /* app_uuid */) {
648   // Do nothing.
649 }
650 
ConnectionCallback(BluetoothGattInterface *,int,int,int,const bt_bdaddr_t &)651 void BluetoothGattInterface::ServerObserver::ConnectionCallback(
652     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
653     int /* server_if */, int /* connected */, const bt_bdaddr_t& /* 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 bt_bdaddr_t &,int,int,bool)676 void BluetoothGattInterface::ServerObserver::RequestReadCharacteristicCallback(
677     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
678     int /* trans_id */, const bt_bdaddr_t& /* bda */, int /* attr_handle */,
679     int /* offset */, bool /* is_long */) {
680   // Do nothing.
681 }
682 
RequestReadDescriptorCallback(BluetoothGattInterface *,int,int,const bt_bdaddr_t &,int,int,bool)683 void BluetoothGattInterface::ServerObserver::RequestReadDescriptorCallback(
684     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
685     int /* trans_id */, const bt_bdaddr_t& /* bda */, int /* attr_handle */,
686     int /* offset */, bool /* is_long */) {
687   // Do nothing.
688 }
689 
RequestWriteCharacteristicCallback(BluetoothGattInterface *,int,int,const bt_bdaddr_t &,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 bt_bdaddr_t& /* 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 bt_bdaddr_t &,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 bt_bdaddr_t& /* 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 bt_bdaddr_t &,int)706 void BluetoothGattInterface::ServerObserver::RequestExecWriteCallback(
707     BluetoothGattInterface* /* gatt_iface */, int /* conn_id */,
708     int /* trans_id */, const bt_bdaddr_t& /* 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