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