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