2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/hal/bluetooth_gatt_interface.h"
20 #define _LIBCPP_BUILDING_SHARED_MUTEX
21 #include <shared_mutex>
22 #undef _LIBCPP_BUILDING_SHARED_MUTEX
24 #include <base/logging.h>
25 #include <base/observer_list.h>
27 #include "service/hal/bluetooth_interface.h"
28 #include "service/logging_helpers.h"
30 using std::lock_guard;
31 using std::unique_lock;
32 using std::shared_lock;
34 using std::shared_timed_mutex;
41 // The global BluetoothGattInterface instance.
42 BluetoothGattInterface* g_interface = nullptr;
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;
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>*
54 base::ObserverList<BluetoothGattInterface::ServerObserver>*
57 #define FOR_EACH_CLIENT_OBSERVER(func) \
58 FOR_EACH_OBSERVER(BluetoothGattInterface::ClientObserver, \
59 *GetClientObservers(), func)
61 #define FOR_EACH_SERVER_OBSERVER(func) \
62 FOR_EACH_OBSERVER(BluetoothGattInterface::ServerObserver, \
63 *GetServerObservers(), func)
65 #define VERIFY_INTERFACE_OR_RETURN() \
68 LOG(WARNING) << "Callback received while |g_interface| is NULL"; \
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();
79 FOR_EACH_CLIENT_OBSERVER(
80 RegisterClientCallback(g_interface, status, client_if, *app_uuid));
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();
89 VLOG(2) << __func__ << " - BD_ADDR: " << BtAddrString(bda)
91 FOR_EACH_CLIENT_OBSERVER(
92 ScanResultCallback(g_interface, *bda, rssi, adv_data));
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();
100 VLOG(2) << __func__ << " - status: " << status
101 << " client_if: " << client_if
102 << " - BD_ADDR: " << BtAddrString(bda)
103 << " - conn_id: " << conn_id;
105 FOR_EACH_CLIENT_OBSERVER(
106 ConnectCallback(g_interface, conn_id, status, client_if, *bda));
109 void DisconnectCallback(int conn_id, int status, int client_if,
111 shared_lock<shared_timed_mutex> lock(g_instance_lock);
112 VERIFY_INTERFACE_OR_RETURN();
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));
123 void SearchCompleteCallback(int conn_id, int status) {
124 shared_lock<shared_timed_mutex> lock(g_instance_lock);
125 VERIFY_INTERFACE_OR_RETURN();
127 VLOG(2) << __func__ << " - conn_id: " << conn_id
128 << " - status: " << status;
129 FOR_EACH_CLIENT_OBSERVER(
130 SearchCompleteCallback(g_interface, conn_id, status));
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();
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,
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();
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;
160 FOR_EACH_CLIENT_OBSERVER(
161 NotifyCallback(g_interface, conn_id, p_data));
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();
169 VLOG(2) << __func__ << " - conn_id: " << conn_id
170 << " - status: " << status;
172 FOR_EACH_CLIENT_OBSERVER(
173 WriteCharacteristicCallback(g_interface, conn_id, status, p_data));
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();
181 VLOG(2) << __func__ << " - conn_id: " << conn_id
182 << " - status: " << status;
184 FOR_EACH_CLIENT_OBSERVER(
185 WriteDescriptorCallback(g_interface, conn_id, status, p_data));
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();
193 FOR_EACH_CLIENT_OBSERVER(ListenCallback(g_interface, status, client_if));
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();
200 VLOG(2) << __func__ << " - conn_id: " << conn_id
201 << " status: " << status
204 FOR_EACH_CLIENT_OBSERVER(MtuChangedCallback(g_interface, conn_id, status, mtu));
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();
212 FOR_EACH_CLIENT_OBSERVER(
213 MultiAdvEnableCallback(g_interface, client_if, status));
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();
221 FOR_EACH_CLIENT_OBSERVER(
222 MultiAdvUpdateCallback(g_interface, client_if, status));
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();
230 FOR_EACH_CLIENT_OBSERVER(
231 MultiAdvDataCallback(g_interface, client_if, status));
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();
239 FOR_EACH_CLIENT_OBSERVER(
240 MultiAdvDisableCallback(g_interface, client_if, status));
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();
248 FOR_EACH_CLIENT_OBSERVER(
249 GetGattDbCallback(g_interface, conn_id, db, size));
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();
259 FOR_EACH_CLIENT_OBSERVER(
260 ServicesRemovedCallback(g_interface, conn_id, start_handle, end_handle));
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();
269 FOR_EACH_CLIENT_OBSERVER(
270 ServicesAddedCallback(g_interface, conn_id, added, added_count));
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();
279 FOR_EACH_SERVER_OBSERVER(
280 RegisterServerCallback(g_interface, status, server_if, *app_uuid));
283 void ConnectionCallback(int conn_id, int server_if, int connected,
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();
291 FOR_EACH_SERVER_OBSERVER(
292 ConnectionCallback(g_interface, conn_id, server_if, connected, *bda));
295 void ServiceAddedCallback(
298 btgatt_srvc_id_t* srvc_id,
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();
306 FOR_EACH_SERVER_OBSERVER(ServiceAddedCallback(
307 g_interface, status, server_if, *srvc_id, srvc_handle));
310 void CharacteristicAddedCallback(
311 int status, int server_if,
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();
321 FOR_EACH_SERVER_OBSERVER(CharacteristicAddedCallback(
322 g_interface, status, server_if, *uuid, srvc_handle, char_handle));
325 void DescriptorAddedCallback(
326 int status, int server_if,
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();
336 FOR_EACH_SERVER_OBSERVER(DescriptorAddedCallback(
337 g_interface, status, server_if, *uuid, srvc_handle, desc_handle));
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();
346 FOR_EACH_SERVER_OBSERVER(ServiceStartedCallback(
347 g_interface, status, server_if, srvc_handle));
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();
356 FOR_EACH_SERVER_OBSERVER(ServiceStoppedCallback(
357 g_interface, status, server_if, srvc_handle));
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();
366 FOR_EACH_SERVER_OBSERVER(ServiceDeletedCallback(
367 g_interface, status, server_if, srvc_handle));
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();
379 FOR_EACH_SERVER_OBSERVER(RequestReadCallback(
380 g_interface, conn_id, trans_id, *bda, attr_handle, offset, is_long));
383 void RequestWriteCallback(int conn_id, int trans_id,
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();
395 FOR_EACH_SERVER_OBSERVER(RequestWriteCallback(
396 g_interface, conn_id, trans_id, *bda, attr_handle, offset, length,
397 need_rsp, is_prep, value));
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();
408 FOR_EACH_SERVER_OBSERVER(RequestExecWriteCallback(
409 g_interface, conn_id, trans_id, *bda, exec_write));
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();
417 FOR_EACH_SERVER_OBSERVER(ResponseConfirmationCallback(
418 g_interface, status, handle));
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();
426 FOR_EACH_SERVER_OBSERVER(IndicationSentCallback(
427 g_interface, conn_id, status));
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();
435 FOR_EACH_SERVER_OBSERVER(MtuChangedCallback(g_interface, conn_id, mtu));
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,
445 SearchCompleteCallback,
446 RegisterForNotificationCallback,
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
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
471 ServicesRemovedCallback,
472 ServicesAddedCallback,
475 const btgatt_server_callbacks_t gatt_server_callbacks = {
476 RegisterServerCallback,
478 ServiceAddedCallback,
479 nullptr, // included_service_added_cb
480 CharacteristicAddedCallback,
481 DescriptorAddedCallback,
482 ServiceStartedCallback,
483 ServiceStoppedCallback,
484 ServiceDeletedCallback,
486 RequestWriteCallback,
487 RequestExecWriteCallback,
488 ResponseConfirmationCallback,
489 IndicationSentCallback,
490 nullptr, // congestion_cb
494 const btgatt_callbacks_t gatt_callbacks = {
495 sizeof(btgatt_callbacks_t),
496 &gatt_client_callbacks,
497 &gatt_server_callbacks
502 // BluetoothGattInterface implementation for production.
503 class BluetoothGattInterfaceImpl : public BluetoothGattInterface {
505 BluetoothGattInterfaceImpl() : hal_iface_(nullptr) {
508 ~BluetoothGattInterfaceImpl() override {
510 hal_iface_->cleanup();
513 // BluetoothGattInterface overrides:
514 void AddClientObserver(ClientObserver* observer) override {
515 client_observers_.AddObserver(observer);
518 void RemoveClientObserver(ClientObserver* observer) override {
519 client_observers_.RemoveObserver(observer);
522 void AddServerObserver(ServerObserver* observer) override {
523 server_observers_.AddObserver(observer);
526 void RemoveServerObserver(ServerObserver* observer) override {
527 server_observers_.RemoveObserver(observer);
530 const btgatt_client_interface_t* GetClientHALInterface() const override {
531 return hal_iface_->client;
534 const btgatt_server_interface_t* GetServerHALInterface() const override {
535 return hal_iface_->server;
538 // Initialize the interface.
540 const bt_interface_t* bt_iface =
541 BluetoothInterface::Get()->GetHALInterface();
544 const btgatt_interface_t* gatt_iface =
545 reinterpret_cast<const btgatt_interface_t*>(
546 bt_iface->get_profile_interface(BT_PROFILE_GATT_ID));
548 LOG(ERROR) << "Failed to obtain HAL GATT interface handle";
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";
558 hal_iface_ = gatt_iface;
563 base::ObserverList<ClientObserver>* client_observers() {
564 return &client_observers_;
567 base::ObserverList<ServerObserver>* server_observers() {
568 return &server_observers_;
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_;
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_;
583 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterfaceImpl);
588 base::ObserverList<BluetoothGattInterface::ClientObserver>*
589 GetClientObservers() {
591 return static_cast<BluetoothGattInterfaceImpl*>(
592 g_interface)->client_observers();
595 base::ObserverList<BluetoothGattInterface::ServerObserver>*
596 GetServerObservers() {
598 return static_cast<BluetoothGattInterfaceImpl*>(
599 g_interface)->server_observers();
604 // Default observer implementations. These are provided so that the methods
605 // themselves are optional.
606 void BluetoothGattInterface::ClientObserver::RegisterClientCallback(
607 BluetoothGattInterface* /* gatt_iface */,
610 const bt_uuid_t& /* app_uuid */) {
613 void BluetoothGattInterface::ClientObserver::ScanResultCallback(
614 BluetoothGattInterface* /* gatt_iface */,
615 const bt_bdaddr_t& /* bda */,
617 uint8_t* /* adv_data */) {
621 void BluetoothGattInterface::ClientObserver::ConnectCallback(
622 BluetoothGattInterface* /* gatt_iface */,
626 const bt_bdaddr_t& /* bda */) {
630 void BluetoothGattInterface::ClientObserver::DisconnectCallback(
631 BluetoothGattInterface* /* gatt_iface */,
635 const bt_bdaddr_t& /* bda */) {
639 void BluetoothGattInterface::ClientObserver::SearchCompleteCallback(
640 BluetoothGattInterface* /* gatt_iface */,
646 void BluetoothGattInterface::ClientObserver::RegisterForNotificationCallback(
647 BluetoothGattInterface* /* gatt_iface */,
650 int /* registered */,
651 btgatt_srvc_id_t* /* srvc_id */,
652 btgatt_gatt_id_t* /* char_id */) {
656 void BluetoothGattInterface::ClientObserver::NotifyCallback(
657 BluetoothGattInterface* /* gatt_iface */,
659 btgatt_notify_params_t* /* p_data */) {
663 void BluetoothGattInterface::ClientObserver::WriteCharacteristicCallback(
664 BluetoothGattInterface* /* gatt_iface */,
667 btgatt_write_params_t* /* p_data */) {
671 void BluetoothGattInterface::ClientObserver::WriteDescriptorCallback(
672 BluetoothGattInterface* /* gatt_iface */,
675 btgatt_write_params_t* /* p_data */) {
679 void BluetoothGattInterface::ClientObserver::ListenCallback(
680 BluetoothGattInterface* /* gatt_iface */,
682 int /* client_if */) {
686 void BluetoothGattInterface::ClientObserver::MtuChangedCallback(
687 BluetoothGattInterface* /* gatt_iface */,
694 void BluetoothGattInterface::ClientObserver::MultiAdvEnableCallback(
695 BluetoothGattInterface* /* gatt_iface */,
697 int /* client_if */) {
700 void BluetoothGattInterface::ClientObserver::MultiAdvUpdateCallback(
701 BluetoothGattInterface* /* gatt_iface */,
703 int /* client_if */) {
706 void BluetoothGattInterface::ClientObserver::MultiAdvDataCallback(
707 BluetoothGattInterface* /* gatt_iface */,
709 int /* client_if */) {
712 void BluetoothGattInterface::ClientObserver::MultiAdvDisableCallback(
713 BluetoothGattInterface* /* gatt_iface */,
715 int /* client_if */) {
719 void BluetoothGattInterface::ClientObserver::GetGattDbCallback(
720 BluetoothGattInterface* /* gatt_iface */,
722 btgatt_db_element_t* /* gatt_db */,
727 void BluetoothGattInterface::ClientObserver::ServicesRemovedCallback(
728 BluetoothGattInterface* /* gatt_iface */,
730 uint16_t /* start_handle */,
731 uint16_t /* end_handle */) {
735 void BluetoothGattInterface::ClientObserver::ServicesAddedCallback(
736 BluetoothGattInterface* /* gatt_iface */,
738 btgatt_db_element_t* /* added */,
739 int /* added_count */) {
743 void BluetoothGattInterface::ServerObserver::RegisterServerCallback(
744 BluetoothGattInterface* /* gatt_iface */,
747 const bt_uuid_t& /* app_uuid */) {
751 void BluetoothGattInterface::ServerObserver::ConnectionCallback(
752 BluetoothGattInterface* /* gatt_iface */,
756 const bt_bdaddr_t& /* bda */) {
760 void BluetoothGattInterface::ServerObserver::ServiceAddedCallback(
761 BluetoothGattInterface* /* gatt_iface */,
764 const btgatt_srvc_id_t& /* srvc_id */,
765 int /* srvc_handle */) {
769 void BluetoothGattInterface::ServerObserver::CharacteristicAddedCallback(
770 BluetoothGattInterface* /* gatt_iface */,
773 const bt_uuid_t& /* uuid */,
774 int /* srvc_handle */,
775 int /* char_handle */) {
779 void BluetoothGattInterface::ServerObserver::DescriptorAddedCallback(
780 BluetoothGattInterface* /* gatt_iface */,
783 const bt_uuid_t& /* uuid */,
784 int /* srvc_handle */,
785 int /* desc_handle */) {
789 void BluetoothGattInterface::ServerObserver::ServiceStartedCallback(
790 BluetoothGattInterface* /* gatt_iface */,
793 int /* srvc_handle */) {
797 void BluetoothGattInterface::ServerObserver::ServiceStoppedCallback(
798 BluetoothGattInterface* /* gatt_iface */,
801 int /* srvc_handle */) {
805 void BluetoothGattInterface::ServerObserver::ServiceDeletedCallback(
806 BluetoothGattInterface* /* gatt_iface */,
809 int /* srvc_handle */) {
813 void BluetoothGattInterface::ServerObserver::RequestReadCallback(
814 BluetoothGattInterface* /* gatt_iface */,
817 const bt_bdaddr_t& /* bda */,
818 int /* attr_handle */,
820 bool /* is_long */) {
824 void BluetoothGattInterface::ServerObserver::RequestWriteCallback(
825 BluetoothGattInterface* /* gatt_iface */,
828 const bt_bdaddr_t& /* bda */,
829 int /* attr_handle */,
834 uint8_t* /* value */) {
838 void BluetoothGattInterface::ServerObserver::RequestExecWriteCallback(
839 BluetoothGattInterface* /* gatt_iface */,
842 const bt_bdaddr_t& /* bda */,
843 int /* exec_write */) {
847 void BluetoothGattInterface::ServerObserver::ResponseConfirmationCallback(
848 BluetoothGattInterface* /* gatt_iface */,
854 void BluetoothGattInterface::ServerObserver::IndicationSentCallback(
855 BluetoothGattInterface* /* gatt_iface */,
861 void BluetoothGattInterface::ServerObserver::MtuChangedCallback(
862 BluetoothGattInterface* /* gatt_iface */,
869 bool BluetoothGattInterface::Initialize() {
870 unique_lock<shared_timed_mutex> lock(g_instance_lock);
873 std::unique_ptr<BluetoothGattInterfaceImpl> impl(
874 new BluetoothGattInterfaceImpl());
875 if (!impl->Initialize()) {
876 LOG(ERROR) << "Failed to initialize BluetoothGattInterface";
880 g_interface = impl.release();
886 void BluetoothGattInterface::CleanUp() {
887 unique_lock<shared_timed_mutex> lock(g_instance_lock);
891 g_interface = nullptr;
895 bool BluetoothGattInterface::IsInitialized() {
896 shared_lock<shared_timed_mutex> lock(g_instance_lock);
898 return g_interface != nullptr;
902 BluetoothGattInterface* BluetoothGattInterface::Get() {
903 shared_lock<shared_timed_mutex> lock(g_instance_lock);
909 void BluetoothGattInterface::InitializeForTesting(
910 BluetoothGattInterface* test_instance) {
911 unique_lock<shared_timed_mutex> lock(g_instance_lock);
912 CHECK(test_instance);
915 g_interface = test_instance;
918 bt_status_t BluetoothGattInterface::StartScan(int client_id) {
919 lock_guard<mutex> lock(scan_clients_lock_);
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;
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";
938 scan_client_set_.insert(client_id);
940 return BT_STATUS_SUCCESS;
943 bt_status_t BluetoothGattInterface::StopScan(int client_id) {
944 lock_guard<mutex> lock(scan_clients_lock_);
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;
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";
962 scan_client_set_.erase(iter);
963 return BT_STATUS_SUCCESS;
967 } // namespace bluetooth