OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[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
21 #include <base/logging.h>
22 #include <base/observer_list.h>
23
24 #include "service/hal/bluetooth_interface.h"
25
26 using std::lock_guard;
27 using std::mutex;
28
29 namespace bluetooth {
30 namespace hal {
31
32 namespace {
33
34 // The global BluetoothGattInterface instance.
35 BluetoothGattInterface* g_interface = nullptr;
36
37 // Mutex used by callbacks to access |g_interface|.
38 mutex g_instance_lock;
39
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>*
43     GetClientObservers();
44
45 #define FOR_EACH_CLIENT_OBSERVER(func) \
46   FOR_EACH_OBSERVER(BluetoothGattInterface::ClientObserver, \
47                     *GetClientObservers(), func)
48
49 void RegisterClientCallback(int status, int client_if, bt_uuid_t* app_uuid) {
50   lock_guard<mutex> lock(g_instance_lock);
51   if (!g_interface) {
52     LOG(WARNING) << "Callback received after global instance was destroyed";
53     return;
54   }
55
56   VLOG(2) << "RegisterClientCallback status: " << status
57           << " client_if: " << client_if;
58   if (!app_uuid) {
59     LOG(WARNING) << "|app_uuid| is NULL; ignoring RegisterClientCallback";
60     return;
61   }
62
63   FOR_EACH_CLIENT_OBSERVER(
64       RegisterClientCallback(status, client_if, *app_uuid));
65 }
66
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
72     nullptr,  // open_cb
73     nullptr,  // close_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
80     nullptr,  // notify_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
87     nullptr,  // listen_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
103 };
104
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
122 };
123
124 const btgatt_callbacks_t gatt_callbacks = {
125   sizeof(btgatt_callbacks_t),
126   &gatt_client_callbacks,
127   &gatt_server_callbacks
128 };
129
130 }  // namespace
131
132 // BluetoothGattInterface implementation for production.
133 class BluetoothGattInterfaceImpl : public BluetoothGattInterface {
134  public:
135   BluetoothGattInterfaceImpl() : hal_iface_(nullptr) {
136   }
137
138   ~BluetoothGattInterfaceImpl() override {
139     CHECK(hal_iface_);
140     hal_iface_->cleanup();
141   }
142
143   // BluetoothGattInterface overrides:
144   void AddClientObserver(ClientObserver* observer) override {
145     lock_guard<mutex> lock(g_instance_lock);
146     client_observers_.AddObserver(observer);
147   }
148
149   void RemoveClientObserver(ClientObserver* observer) override {
150     lock_guard<mutex> lock(g_instance_lock);
151     client_observers_.RemoveObserver(observer);
152   }
153
154   const btgatt_client_interface_t* GetClientHALInterface() const override {
155     return hal_iface_->client;
156   }
157
158   // Initialize the interface.
159   bool Initialize() {
160     const bt_interface_t* bt_iface =
161         BluetoothInterface::Get()->GetHALInterface();
162     CHECK(bt_iface);
163
164     const btgatt_interface_t* gatt_iface =
165         reinterpret_cast<const btgatt_interface_t*>(
166             bt_iface->get_profile_interface(BT_PROFILE_GATT_ID));
167     if (!gatt_iface) {
168       LOG(ERROR) << "Failed to obtain HAL GATT interface handle";
169       return false;
170     }
171
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";
175       return false;
176     }
177
178     hal_iface_ = gatt_iface;
179
180     return true;
181   }
182
183   base::ObserverList<ClientObserver>* client_observers() {
184     return &client_observers_;
185   }
186
187  private:
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_;
193
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_;
197
198   DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterfaceImpl);
199 };
200
201 namespace {
202
203 base::ObserverList<BluetoothGattInterface::ClientObserver>*
204 GetClientObservers() {
205   CHECK(g_interface);
206   return static_cast<BluetoothGattInterfaceImpl*>(
207       g_interface)->client_observers();
208 }
209
210 }  // namespace
211
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) {
216   // Do nothing.
217 }
218
219 // static
220 bool BluetoothGattInterface::Initialize() {
221   lock_guard<mutex> lock(g_instance_lock);
222   CHECK(!g_interface);
223
224   std::unique_ptr<BluetoothGattInterfaceImpl> impl(
225       new BluetoothGattInterfaceImpl());
226   if (!impl->Initialize()) {
227     LOG(ERROR) << "Failed to initialize BluetoothGattInterface";
228     return false;
229   }
230
231   g_interface = impl.release();
232
233   return true;
234 }
235
236 // static
237 void BluetoothGattInterface::CleanUp() {
238   lock_guard<mutex> lock(g_instance_lock);
239   CHECK(g_interface);
240
241   delete g_interface;
242   g_interface = nullptr;
243 }
244
245 // static
246 bool BluetoothGattInterface::IsInitialized() {
247   lock_guard<mutex> lock(g_instance_lock);
248
249   return g_interface != nullptr;
250 }
251
252 // static
253 BluetoothGattInterface* BluetoothGattInterface::Get() {
254   lock_guard<mutex> lock(g_instance_lock);
255   CHECK(g_interface);
256   return g_interface;
257 }
258
259 // static
260 void BluetoothGattInterface::InitializeForTesting(
261     BluetoothGattInterface* test_instance) {
262   lock_guard<mutex> lock(g_instance_lock);
263   CHECK(test_instance);
264   CHECK(!g_interface);
265
266   g_interface = test_instance;
267 }
268
269 }  // namespace hal
270 }  // namespace bluetooth