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