1 //
2 //  Copyright 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/fake_bluetooth_gatt_interface.h"
18 
19 namespace bluetooth {
20 namespace hal {
21 namespace {
22 
23 // The global test handler instances. We have to have globals since the HAL
24 // interface methods all have to be global and their signatures don't allow us
25 // to pass in user_data.
26 std::shared_ptr<BleAdvertiserInterface> g_advertiser_handler;
27 std::shared_ptr<BleScannerInterface> g_scanner_handler;
28 std::shared_ptr<FakeBluetoothGattInterface::TestClientHandler> g_client_handler;
29 std::shared_ptr<FakeBluetoothGattInterface::TestServerHandler> g_server_handler;
30 
FakeRegisterClient(const bluetooth::Uuid & app_uuid)31 bt_status_t FakeRegisterClient(const bluetooth::Uuid& app_uuid) {
32   if (g_client_handler) return g_client_handler->RegisterClient(app_uuid);
33 
34   return BT_STATUS_FAIL;
35 }
36 
FakeUnregisterClient(int client_if)37 bt_status_t FakeUnregisterClient(int client_if) {
38   if (g_client_handler) return g_client_handler->UnregisterClient(client_if);
39 
40   return BT_STATUS_FAIL;
41 }
42 
FakeConnect(int client_if,const RawAddress & bd_addr,bool is_direct,int transport,bool opportunistic,int phy)43 bt_status_t FakeConnect(int client_if, const RawAddress& bd_addr,
44                         bool is_direct, int transport, bool opportunistic,
45                         int phy) {
46   if (g_client_handler)
47     return g_client_handler->Connect(client_if, bd_addr, is_direct, transport);
48 
49   return BT_STATUS_FAIL;
50 }
51 
FakeDisconnect(int client_if,const RawAddress & bd_addr,int conn_id)52 bt_status_t FakeDisconnect(int client_if, const RawAddress& bd_addr,
53                            int conn_id) {
54   if (g_client_handler)
55     return g_client_handler->Disconnect(client_if, bd_addr, conn_id);
56 
57   return BT_STATUS_FAIL;
58 }
59 
FakeRegisterServer(const bluetooth::Uuid & app_uuid)60 bt_status_t FakeRegisterServer(const bluetooth::Uuid& app_uuid) {
61   if (g_server_handler) return g_server_handler->RegisterServer(app_uuid);
62 
63   return BT_STATUS_FAIL;
64 }
65 
FakeUnregisterServer(int server_if)66 bt_status_t FakeUnregisterServer(int server_if) {
67   if (g_server_handler) return g_server_handler->UnregisterServer(server_if);
68 
69   return BT_STATUS_FAIL;
70 }
71 
FakeAddService(int server_if,std::vector<btgatt_db_element_t> service)72 bt_status_t FakeAddService(int server_if,
73                            std::vector<btgatt_db_element_t> service) {
74   if (g_server_handler)
75     return g_server_handler->AddService(server_if, std::move(service));
76 
77   return BT_STATUS_FAIL;
78 }
79 
FakeDeleteService(int server_if,int srvc_handle)80 bt_status_t FakeDeleteService(int server_if, int srvc_handle) {
81   if (g_server_handler)
82     return g_server_handler->DeleteService(server_if, srvc_handle);
83 
84   return BT_STATUS_FAIL;
85 }
86 
FakeSendIndication(int server_if,int attribute_handle,int conn_id,int confirm,std::vector<uint8_t> value)87 bt_status_t FakeSendIndication(int server_if, int attribute_handle, int conn_id,
88                                int confirm, std::vector<uint8_t> value) {
89   if (g_server_handler)
90     return g_server_handler->SendIndication(server_if, attribute_handle,
91                                             conn_id, confirm, std::move(value));
92 
93   return BT_STATUS_FAIL;
94 }
95 
FakeSendResponse(int conn_id,int trans_id,int status,const btgatt_response_t & response)96 bt_status_t FakeSendResponse(int conn_id, int trans_id, int status,
97                              const btgatt_response_t& response) {
98   if (g_server_handler)
99     return g_server_handler->SendResponse(conn_id, trans_id, status, response);
100 
101   return BT_STATUS_FAIL;
102 }
103 
104 btgatt_client_interface_t fake_btgattc_iface = {
105     FakeRegisterClient,
106     FakeUnregisterClient,
107     FakeConnect,
108     FakeDisconnect,
109     nullptr,  // refresh
110     nullptr,  // search_service
111     nullptr,  // discover_service_by_uuid
112     nullptr,  // read_characteristic
113     nullptr,  // read_using_characteristic_uuid
114     nullptr,  // write_characteristic
115     nullptr,  // read_descriptor
116     nullptr,  // write_descriptor
117     nullptr,  // execute_write
118     nullptr,  // register_for_notification
119     nullptr,  // deregister_for_notification
120     nullptr,  // read_remote_rssi
121     nullptr,  // get_device_type
122     nullptr,  // configure_mtu
123     nullptr,  // conn_parameter_update
124     nullptr,  // set_phy
125     nullptr,  // read_phy
126     nullptr,  // test_command
127     nullptr,  // get_gatt_db
128 };
129 
130 btgatt_server_interface_t fake_btgatts_iface = {
131     FakeRegisterServer,
132     FakeUnregisterServer,
133     nullptr,  // connect
134     nullptr,  // disconnect
135     FakeAddService,
136     nullptr,  // stop_service
137     FakeDeleteService,
138     FakeSendIndication,
139     FakeSendResponse,
140     nullptr,  // set_phy
141     nullptr,  // read_phy
142 };
143 
144 }  // namespace
145 
FakeBluetoothGattInterface(std::shared_ptr<BleAdvertiserInterface> advertiser_handler,std::shared_ptr<BleScannerInterface> scanner_handler,std::shared_ptr<TestClientHandler> client_handler,std::shared_ptr<TestServerHandler> server_handler)146 FakeBluetoothGattInterface::FakeBluetoothGattInterface(
147     std::shared_ptr<BleAdvertiserInterface> advertiser_handler,
148     std::shared_ptr<BleScannerInterface> scanner_handler,
149     std::shared_ptr<TestClientHandler> client_handler,
150     std::shared_ptr<TestServerHandler> server_handler)
151     : client_handler_(client_handler) {
152   CHECK(!g_advertiser_handler);
153   CHECK(!g_scanner_handler);
154   CHECK(!g_client_handler);
155   CHECK(!g_server_handler);
156 
157   // We allow passing NULL. In this case all calls we fail by default.
158   if (advertiser_handler) g_advertiser_handler = advertiser_handler;
159 
160   if (scanner_handler) g_scanner_handler = scanner_handler;
161 
162   if (client_handler) g_client_handler = client_handler;
163 
164   if (server_handler) g_server_handler = server_handler;
165 }
166 
~FakeBluetoothGattInterface()167 FakeBluetoothGattInterface::~FakeBluetoothGattInterface() {
168   if (g_advertiser_handler) g_advertiser_handler = nullptr;
169 
170   if (g_scanner_handler) g_scanner_handler = nullptr;
171 
172   if (g_client_handler) g_client_handler = nullptr;
173 
174   if (g_server_handler) g_server_handler = nullptr;
175 }
176 
177 // The methods below can be used to notify observers with certain events and
178 // given parameters.
NotifyScanResultCallback(const RawAddress & bda,int rssi,std::vector<uint8_t> adv_data)179 void FakeBluetoothGattInterface::NotifyScanResultCallback(
180     const RawAddress& bda, int rssi, std::vector<uint8_t> adv_data) {
181   for (auto& observer : scanner_observers_) {
182     observer.ScanResultCallback(this, bda, rssi, adv_data);
183   }
184 }
185 
NotifyRegisterClientCallback(int status,int client_if,const bluetooth::Uuid & app_uuid)186 void FakeBluetoothGattInterface::NotifyRegisterClientCallback(
187     int status, int client_if, const bluetooth::Uuid& app_uuid) {
188   for (auto& observer : client_observers_) {
189     observer.RegisterClientCallback(this, status, client_if, app_uuid);
190   }
191 }
192 
NotifyConnectCallback(int conn_id,int status,int client_if,const RawAddress & bda)193 void FakeBluetoothGattInterface::NotifyConnectCallback(int conn_id, int status,
194                                                        int client_if,
195                                                        const RawAddress& bda) {
196   for (auto& observer : client_observers_) {
197     observer.ConnectCallback(this, conn_id, status, client_if, bda);
198   }
199 }
200 
NotifyDisconnectCallback(int conn_id,int status,int client_if,const RawAddress & bda)201 void FakeBluetoothGattInterface::NotifyDisconnectCallback(
202     int conn_id, int status, int client_if, const RawAddress& bda) {
203   for (auto& observer : client_observers_) {
204     observer.DisconnectCallback(this, conn_id, status, client_if, bda);
205   }
206 }
207 
NotifyRegisterServerCallback(int status,int server_if,const Uuid & app_uuid)208 void FakeBluetoothGattInterface::NotifyRegisterServerCallback(
209     int status, int server_if, const Uuid& app_uuid) {
210   for (auto& observer : server_observers_) {
211     observer.RegisterServerCallback(this, status, server_if, app_uuid);
212   }
213 }
214 
NotifyServerConnectionCallback(int conn_id,int server_if,int connected,const RawAddress & bda)215 void FakeBluetoothGattInterface::NotifyServerConnectionCallback(
216     int conn_id, int server_if, int connected, const RawAddress& bda) {
217   for (auto& observer : server_observers_) {
218     observer.ConnectionCallback(this, conn_id, server_if, connected, bda);
219   }
220 }
221 
NotifyServiceAddedCallback(int status,int server_if,std::vector<btgatt_db_element_t> service)222 void FakeBluetoothGattInterface::NotifyServiceAddedCallback(
223     int status, int server_if, std::vector<btgatt_db_element_t> service) {
224   for (auto& observer : server_observers_) {
225     observer.ServiceAddedCallback(this, status, server_if, service);
226   }
227 }
228 
NotifyRequestReadCharacteristicCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool is_long)229 void FakeBluetoothGattInterface::NotifyRequestReadCharacteristicCallback(
230     int conn_id, int trans_id, const RawAddress& bda, int attr_handle,
231     int offset, bool is_long) {
232   for (auto& observer : server_observers_) {
233     observer.RequestReadCharacteristicCallback(this, conn_id, trans_id, bda,
234                                                attr_handle, offset, is_long);
235   }
236 }
237 
NotifyRequestReadDescriptorCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool is_long)238 void FakeBluetoothGattInterface::NotifyRequestReadDescriptorCallback(
239     int conn_id, int trans_id, const RawAddress& bda, int attr_handle,
240     int offset, bool is_long) {
241   for (auto& observer : server_observers_) {
242     observer.RequestReadDescriptorCallback(this, conn_id, trans_id, bda,
243                                            attr_handle, offset, is_long);
244   }
245 }
246 
NotifyRequestWriteCharacteristicCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool need_rsp,bool is_prep,std::vector<uint8_t> value)247 void FakeBluetoothGattInterface::NotifyRequestWriteCharacteristicCallback(
248     int conn_id, int trans_id, const RawAddress& bda, int attr_handle,
249     int offset, bool need_rsp, bool is_prep, std::vector<uint8_t> value) {
250   for (auto& observer : server_observers_) {
251     observer.RequestWriteCharacteristicCallback(this, conn_id, trans_id, bda,
252                                                 attr_handle, offset, need_rsp,
253                                                 is_prep, value);
254   }
255 }
256 
NotifyRequestWriteDescriptorCallback(int conn_id,int trans_id,const RawAddress & bda,int attr_handle,int offset,bool need_rsp,bool is_prep,std::vector<uint8_t> value)257 void FakeBluetoothGattInterface::NotifyRequestWriteDescriptorCallback(
258     int conn_id, int trans_id, const RawAddress& bda, int attr_handle,
259     int offset, bool need_rsp, bool is_prep, std::vector<uint8_t> value) {
260   for (auto& observer : server_observers_) {
261     observer.RequestWriteDescriptorCallback(this, conn_id, trans_id, bda,
262                                             attr_handle, offset, need_rsp,
263                                             is_prep, value);
264   }
265 }
266 
NotifyRequestExecWriteCallback(int conn_id,int trans_id,const RawAddress & bda,int exec_write)267 void FakeBluetoothGattInterface::NotifyRequestExecWriteCallback(
268     int conn_id, int trans_id, const RawAddress& bda, int exec_write) {
269   for (auto& observer : server_observers_) {
270     observer.RequestExecWriteCallback(this, conn_id, trans_id, bda, exec_write);
271   }
272 }
273 
NotifyIndicationSentCallback(int conn_id,int status)274 void FakeBluetoothGattInterface::NotifyIndicationSentCallback(int conn_id,
275                                                               int status) {
276   for (auto& observer : server_observers_) {
277     observer.IndicationSentCallback(this, conn_id, status);
278   }
279 }
280 
AddScannerObserver(ScannerObserver * observer)281 void FakeBluetoothGattInterface::AddScannerObserver(ScannerObserver* observer) {
282   CHECK(observer);
283   scanner_observers_.AddObserver(observer);
284 }
285 
RemoveScannerObserver(ScannerObserver * observer)286 void FakeBluetoothGattInterface::RemoveScannerObserver(
287     ScannerObserver* observer) {
288   CHECK(observer);
289   scanner_observers_.RemoveObserver(observer);
290 }
291 
AddClientObserver(ClientObserver * observer)292 void FakeBluetoothGattInterface::AddClientObserver(ClientObserver* observer) {
293   CHECK(observer);
294   client_observers_.AddObserver(observer);
295 }
296 
RemoveClientObserver(ClientObserver * observer)297 void FakeBluetoothGattInterface::RemoveClientObserver(
298     ClientObserver* observer) {
299   CHECK(observer);
300   client_observers_.RemoveObserver(observer);
301 }
302 
AddServerObserver(ServerObserver * observer)303 void FakeBluetoothGattInterface::AddServerObserver(ServerObserver* observer) {
304   CHECK(observer);
305   server_observers_.AddObserver(observer);
306 }
307 
RemoveServerObserver(ServerObserver * observer)308 void FakeBluetoothGattInterface::RemoveServerObserver(
309     ServerObserver* observer) {
310   CHECK(observer);
311   server_observers_.RemoveObserver(observer);
312 }
313 
GetAdvertiserHALInterface() const314 BleAdvertiserInterface* FakeBluetoothGattInterface::GetAdvertiserHALInterface()
315     const {
316   return g_advertiser_handler.get();
317 }
318 
GetScannerHALInterface() const319 BleScannerInterface* FakeBluetoothGattInterface::GetScannerHALInterface()
320     const {
321   return g_scanner_handler.get();
322 }
323 
324 const btgatt_client_interface_t*
GetClientHALInterface() const325 FakeBluetoothGattInterface::GetClientHALInterface() const {
326   return &fake_btgattc_iface;
327 }
328 
329 const btgatt_server_interface_t*
GetServerHALInterface() const330 FakeBluetoothGattInterface::GetServerHALInterface() const {
331   return &fake_btgatts_iface;
332 }
333 
334 }  // namespace hal
335 }  // namespace bluetooth
336