OSDN Git Service

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