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"
21 #include <base/logging.h>
22 #include <base/observer_list.h>
24 #include "service/hal/bluetooth_interface.h"
26 using std::lock_guard;
34 // The global BluetoothGattInterface instance.
35 BluetoothGattInterface* g_interface = nullptr;
37 // Mutex used by callbacks to access |g_interface|.
38 mutex g_instance_lock;
40 // Helper for obtaining the client observer list. This is forward declared here
41 // and defined below since it depends on BluetoothInterfaceImpl.
42 base::ObserverList<BluetoothGattInterface::ClientObserver>*
45 #define FOR_EACH_CLIENT_OBSERVER(func) \
46 FOR_EACH_OBSERVER(BluetoothGattInterface::ClientObserver, \
47 *GetClientObservers(), func)
49 void RegisterClientCallback(int status, int client_if, bt_uuid_t* app_uuid) {
50 lock_guard<mutex> lock(g_instance_lock);
52 LOG(WARNING) << "Callback received after global instance was destroyed";
56 VLOG(2) << "RegisterClientCallback status: " << status
57 << " client_if: " << client_if;
59 LOG(WARNING) << "|app_uuid| is NULL; ignoring RegisterClientCallback";
63 FOR_EACH_CLIENT_OBSERVER(
64 RegisterClientCallback(status, client_if, *app_uuid));
67 // The HAL Bluetooth GATT client interface callbacks. These signal a mixture of
68 // GATT client-role and GAP events.
69 const btgatt_client_callbacks_t gatt_client_callbacks = {
70 RegisterClientCallback,
71 nullptr, // scan_result_cb
74 nullptr, // search_complete_cb
75 nullptr, // search_result_cb
76 nullptr, // get_characteristic_cb
77 nullptr, // get_descriptor_cb
78 nullptr, // get_included_service_cb
79 nullptr, // register_for_notification_cb
81 nullptr, // read_characteristic_cb
82 nullptr, // write_characteristic_cb
83 nullptr, // read_descriptor_cb
84 nullptr, // write_descriptor_cb
85 nullptr, // execute_write_cb
86 nullptr, // read_remote_rssi_cb
88 nullptr, // configure_mtu_cb
89 nullptr, // scan_filter_cfg_cb
90 nullptr, // scan_filter_param_cb
91 nullptr, // scan_filter_status_cb
92 nullptr, // multi_adv_enable_cb
93 nullptr, // multi_adv_update_cb
94 nullptr, // multi_adv_data_cb
95 nullptr, // multi_adv_disable_cb
96 nullptr, // congestion_cb
97 nullptr, // batchscan_cfg_storage_cb
98 nullptr, // batchscan_enb_disable_cb
99 nullptr, // batchscan_reports_cb
100 nullptr, // batchscan_threshold_cb
101 nullptr, // track_adv_event_cb
102 nullptr, // scan_parameter_setup_completed_cb
105 const btgatt_server_callbacks_t gatt_server_callbacks = {
106 nullptr, // register_server_cb
107 nullptr, // connection_cb
108 nullptr, // service_added_cb,
109 nullptr, // included_service_added_cb,
110 nullptr, // characteristic_added_cb,
111 nullptr, // descriptor_added_cb,
112 nullptr, // service_started_cb,
113 nullptr, // service_stopped_cb,
114 nullptr, // service_deleted_cb,
115 nullptr, // request_read_cb,
116 nullptr, // request_write_cb,
117 nullptr, // request_exec_write_cb,
118 nullptr, // response_confirmation_cb,
119 nullptr, // indication_sent_cb
120 nullptr, // congestion_cb
121 nullptr, // mtu_changed_cb
124 const btgatt_callbacks_t gatt_callbacks = {
125 sizeof(btgatt_callbacks_t),
126 &gatt_client_callbacks,
127 &gatt_server_callbacks
132 // BluetoothGattInterface implementation for production.
133 class BluetoothGattInterfaceImpl : public BluetoothGattInterface {
135 BluetoothGattInterfaceImpl() : hal_iface_(nullptr) {
138 ~BluetoothGattInterfaceImpl() override {
140 hal_iface_->cleanup();
143 // BluetoothGattInterface overrides:
144 void AddClientObserver(ClientObserver* observer) override {
145 lock_guard<mutex> lock(g_instance_lock);
146 client_observers_.AddObserver(observer);
149 void RemoveClientObserver(ClientObserver* observer) override {
150 lock_guard<mutex> lock(g_instance_lock);
151 client_observers_.RemoveObserver(observer);
154 const btgatt_client_interface_t* GetClientHALInterface() const override {
155 return hal_iface_->client;
158 // Initialize the interface.
160 const bt_interface_t* bt_iface =
161 BluetoothInterface::Get()->GetHALInterface();
164 const btgatt_interface_t* gatt_iface =
165 reinterpret_cast<const btgatt_interface_t*>(
166 bt_iface->get_profile_interface(BT_PROFILE_GATT_ID));
168 LOG(ERROR) << "Failed to obtain HAL GATT interface handle";
172 bt_status_t status = gatt_iface->init(&gatt_callbacks);
173 if (status != BT_STATUS_SUCCESS) {
174 LOG(ERROR) << "Failed to initialize HAL GATT interface";
178 hal_iface_ = gatt_iface;
183 base::ObserverList<ClientObserver>* client_observers() {
184 return &client_observers_;
188 // List of observers that are interested in client notifications from us.
189 // We're not using a base::ObserverListThreadSafe, which it posts observer
190 // events automatically on the origin threads, as we want to avoid that
191 // overhead and simply forward the events to the upper layer.
192 base::ObserverList<ClientObserver> client_observers_;
194 // The HAL handle obtained from the shared library. We hold a weak reference
195 // to this since the actual data resides in the shared Bluetooth library.
196 const btgatt_interface_t* hal_iface_;
198 DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterfaceImpl);
203 base::ObserverList<BluetoothGattInterface::ClientObserver>*
204 GetClientObservers() {
206 return static_cast<BluetoothGattInterfaceImpl*>(
207 g_interface)->client_observers();
212 // Default observer implementations. These are provided so that the methods
213 // themselves are optional.
214 void BluetoothGattInterface::ClientObserver::RegisterClientCallback(
215 int status, int client_if, const bt_uuid_t& app_uuid) {
220 bool BluetoothGattInterface::Initialize() {
221 lock_guard<mutex> lock(g_instance_lock);
224 std::unique_ptr<BluetoothGattInterfaceImpl> impl(
225 new BluetoothGattInterfaceImpl());
226 if (!impl->Initialize()) {
227 LOG(ERROR) << "Failed to initialize BluetoothGattInterface";
231 g_interface = impl.release();
237 void BluetoothGattInterface::CleanUp() {
238 lock_guard<mutex> lock(g_instance_lock);
242 g_interface = nullptr;
246 bool BluetoothGattInterface::IsInitialized() {
247 lock_guard<mutex> lock(g_instance_lock);
249 return g_interface != nullptr;
253 BluetoothGattInterface* BluetoothGattInterface::Get() {
254 lock_guard<mutex> lock(g_instance_lock);
260 void BluetoothGattInterface::InitializeForTesting(
261 BluetoothGattInterface* test_instance) {
262 lock_guard<mutex> lock(g_instance_lock);
263 CHECK(test_instance);
266 g_interface = test_instance;
270 } // namespace bluetooth