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/fake_bluetooth_gatt_interface.h"
18
19namespace bluetooth {
20namespace hal {
21namespace {
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.
26std::shared_ptr<FakeBluetoothGattInterface::TestClientHandler> g_client_handler;
27std::shared_ptr<FakeBluetoothGattInterface::TestServerHandler> g_server_handler;
28
29bt_status_t FakeRegisterClient(bt_uuid_t* app_uuid) {
30  if (g_client_handler)
31    return g_client_handler->RegisterClient(app_uuid);
32
33  return BT_STATUS_FAIL;
34}
35
36bt_status_t FakeUnregisterClient(int client_if) {
37  if (g_client_handler)
38    return g_client_handler->UnregisterClient(client_if);
39
40  return BT_STATUS_FAIL;
41}
42
43bt_status_t FakeScan(bool start) {
44  if (g_client_handler)
45    return g_client_handler->Scan(start);
46
47  return BT_STATUS_FAIL;
48}
49
50bt_status_t FakeConnect(int client_if, const bt_bdaddr_t *bd_addr,
51                        bool is_direct, int transport) {
52  if (g_client_handler)
53    return g_client_handler->Connect(client_if, bd_addr, is_direct, transport);
54
55  return BT_STATUS_FAIL;
56}
57
58bt_status_t FakeDisconnect(int client_if, const bt_bdaddr_t *bd_addr,
59                           int conn_id) {
60  if (g_client_handler)
61    return g_client_handler->Disconnect(client_if, bd_addr, conn_id);
62
63  return BT_STATUS_FAIL;
64}
65
66bt_status_t FakeMultiAdvEnable(
67    int client_if, int min_interval, int max_interval, int adv_type,
68    int chnl_map, int tx_power, int timeout_s) {
69  if (g_client_handler)
70    return g_client_handler->MultiAdvEnable(client_if, min_interval, max_interval,
71                                     adv_type, chnl_map, tx_power, timeout_s);
72
73  return BT_STATUS_FAIL;
74}
75
76bt_status_t FakeMultiAdvSetInstData(
77    int client_if, bool set_scan_rsp, bool include_name,
78    bool incl_txpower, int appearance,
79    int manufacturer_len, char* manufacturer_data,
80    int service_data_len, char* service_data,
81    int service_uuid_len, char* service_uuid) {
82  if (g_client_handler)
83    return g_client_handler->MultiAdvSetInstData(
84        client_if, set_scan_rsp, include_name,
85        incl_txpower, appearance,
86        manufacturer_len, manufacturer_data,
87        service_data_len, service_data,
88        service_uuid_len, service_uuid);
89
90  return BT_STATUS_FAIL;
91}
92
93bt_status_t FakeMultiAdvDisable(int client_if) {
94  if (g_client_handler)
95    return g_client_handler->MultiAdvDisable(client_if);
96
97  return BT_STATUS_FAIL;
98}
99
100bt_status_t FakeRegisterServer(bt_uuid_t* app_uuid) {
101  if (g_server_handler)
102    return g_server_handler->RegisterServer(app_uuid);
103
104  return BT_STATUS_FAIL;
105}
106
107bt_status_t FakeUnregisterServer(int server_if) {
108  if (g_server_handler)
109    return g_server_handler->UnregisterServer(server_if);
110
111  return BT_STATUS_FAIL;
112}
113
114bt_status_t FakeAddService(
115    int server_if, btgatt_srvc_id_t* srvc_id, int num_handles) {
116  if (g_server_handler)
117    return g_server_handler->AddService(server_if, srvc_id, num_handles);
118
119  return BT_STATUS_FAIL;
120}
121
122bt_status_t FakeAddCharacteristic(int server_if, int srvc_handle,
123                                  bt_uuid_t *uuid,
124                                  int properties, int permissions) {
125  if (g_server_handler)
126    return g_server_handler->AddCharacteristic(server_if, srvc_handle, uuid,
127                                               properties, permissions);
128
129  return BT_STATUS_FAIL;
130}
131
132bt_status_t FakeAddDescriptor(int server_if, int srvc_handle,
133                              bt_uuid_t* uuid,
134                              int permissions) {
135  if (g_server_handler)
136    return g_server_handler->AddDescriptor(
137        server_if, srvc_handle, uuid, permissions);
138
139  return BT_STATUS_FAIL;
140}
141
142bt_status_t FakeStartService(
143    int server_if, int srvc_handle, int transport) {
144  if (g_server_handler)
145    return g_server_handler->StartService(server_if, srvc_handle, transport);
146
147  return BT_STATUS_FAIL;
148}
149
150bt_status_t FakeDeleteService(int server_if, int srvc_handle) {
151  if (g_server_handler)
152    return g_server_handler->DeleteService(server_if, srvc_handle);
153
154  return BT_STATUS_FAIL;
155}
156
157bt_status_t FakeSendIndication(int server_if, int attribute_handle,
158                               int conn_id, int len, int confirm,
159                               char* value) {
160  if (g_server_handler)
161    return g_server_handler->SendIndication(server_if, attribute_handle,
162                                            conn_id, len, confirm, value);
163
164  return BT_STATUS_FAIL;
165}
166
167bt_status_t FakeSendResponse(int conn_id, int trans_id, int status,
168                             btgatt_response_t* response) {
169  if (g_server_handler)
170    return g_server_handler->SendResponse(conn_id, trans_id, status, response);
171
172  return BT_STATUS_FAIL;
173}
174
175btgatt_client_interface_t fake_btgattc_iface = {
176  FakeRegisterClient,
177  FakeUnregisterClient,
178  FakeScan,
179  FakeConnect,
180  FakeDisconnect,
181  nullptr,  // listen
182  nullptr,  // refresh
183  nullptr,  // search_service
184  nullptr,  // read_characteristic
185  nullptr,  // write_characteristic
186  nullptr,  // read_descriptor
187  nullptr,  // write_descriptor
188  nullptr,  // execute_write
189  nullptr,  // register_for_notification
190  nullptr,  // deregister_for_notification
191  nullptr,  // read_remote_rssi
192  nullptr,  // scan_filter_param_setup
193  nullptr,  // scan_filter_add_remove
194  nullptr,  // scan_filter_clear
195  nullptr,  // scan_filter_enable
196  nullptr,  // get_device_type
197  nullptr,  // set_adv_data
198  nullptr,  // configure_mtu
199  nullptr,  // conn_parameter_update
200  nullptr,  // set_scan_parameters
201  FakeMultiAdvEnable,
202  nullptr,  // multi_adv_update
203  FakeMultiAdvSetInstData,
204  FakeMultiAdvDisable,
205  nullptr,  // batchscan_cfg_storate
206  nullptr,  // batchscan_enb_batch_scan
207  nullptr,  // batchscan_dis_batch_scan
208  nullptr,  // batchscan_read_reports
209  nullptr,  // test_command
210  nullptr,  // get_gatt_db
211};
212
213btgatt_server_interface_t fake_btgatts_iface = {
214  FakeRegisterServer,
215  FakeUnregisterServer,
216  nullptr,  // connect
217  nullptr,  // disconnect
218  FakeAddService,
219  nullptr,  // add_included_service
220  FakeAddCharacteristic,
221  FakeAddDescriptor,
222  FakeStartService,
223  nullptr,  // stop_service
224  FakeDeleteService,
225  FakeSendIndication,
226  FakeSendResponse,
227};
228
229}  // namespace
230
231FakeBluetoothGattInterface::FakeBluetoothGattInterface(
232    std::shared_ptr<TestClientHandler> client_handler,
233    std::shared_ptr<TestServerHandler> server_handler)
234    : client_handler_(client_handler) {
235  CHECK(!g_client_handler);
236  CHECK(!g_server_handler);
237
238  // We allow passing NULL. In this case all calls we fail by default.
239  if (client_handler)
240    g_client_handler = client_handler;
241
242  if (server_handler)
243    g_server_handler = server_handler;
244}
245
246FakeBluetoothGattInterface::~FakeBluetoothGattInterface() {
247  if (g_client_handler)
248    g_client_handler = nullptr;
249
250  if (g_server_handler)
251    g_server_handler = nullptr;
252}
253
254// The methods below can be used to notify observers with certain events and
255// given parameters.
256void FakeBluetoothGattInterface::NotifyRegisterClientCallback(
257    int status, int client_if,
258    const bt_uuid_t& app_uuid) {
259  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
260                    RegisterClientCallback(this, status, client_if, app_uuid));
261}
262
263void FakeBluetoothGattInterface::NotifyConnectCallback(
264    int conn_id, int status, int client_if, const bt_bdaddr_t& bda) {
265  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
266                    ConnectCallback(this, conn_id, status, client_if, bda));
267}
268
269void FakeBluetoothGattInterface::NotifyDisconnectCallback(
270    int conn_id, int status, int client_if, const bt_bdaddr_t& bda) {
271  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
272                    DisconnectCallback(this, conn_id, status, client_if, bda));
273}
274
275void FakeBluetoothGattInterface::NotifyScanResultCallback(
276    const bt_bdaddr_t& bda, int rssi, uint8_t* adv_data) {
277  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
278                    ScanResultCallback(this, bda, rssi, adv_data));
279}
280
281void FakeBluetoothGattInterface::NotifyMultiAdvEnableCallback(
282    int client_if, int status) {
283  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
284                    MultiAdvEnableCallback(this, client_if, status));
285}
286
287void FakeBluetoothGattInterface::NotifyMultiAdvDataCallback(
288    int client_if, int status) {
289  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
290                    MultiAdvDataCallback(this, client_if, status));
291}
292
293void FakeBluetoothGattInterface::NotifyMultiAdvDisableCallback(
294    int client_if, int status) {
295  FOR_EACH_OBSERVER(ClientObserver, client_observers_,
296                    MultiAdvDisableCallback(this, client_if, status));
297}
298
299void FakeBluetoothGattInterface::NotifyRegisterServerCallback(
300    int status, int server_if,
301    const bt_uuid_t& app_uuid) {
302  FOR_EACH_OBSERVER(ServerObserver, server_observers_,
303                    RegisterServerCallback(this, status, server_if, app_uuid));
304}
305
306void FakeBluetoothGattInterface::NotifyServerConnectionCallback(
307    int conn_id, int server_if, int connected, const bt_bdaddr_t& bda) {
308  FOR_EACH_OBSERVER(
309      ServerObserver, server_observers_,
310      ConnectionCallback(this, conn_id, server_if, connected, bda));
311}
312
313void FakeBluetoothGattInterface::NotifyServiceAddedCallback(
314    int status, int server_if,
315    const btgatt_srvc_id_t& srvc_id,
316    int srvc_handle) {
317  FOR_EACH_OBSERVER(
318      ServerObserver, server_observers_,
319      ServiceAddedCallback(this, status, server_if, srvc_id, srvc_handle));
320}
321
322void FakeBluetoothGattInterface::NotifyCharacteristicAddedCallback(
323    int status, int server_if,
324    const bt_uuid_t& uuid,
325    int srvc_handle, int char_handle) {
326  FOR_EACH_OBSERVER(
327      ServerObserver, server_observers_,
328      CharacteristicAddedCallback(
329          this, status, server_if, uuid, srvc_handle, char_handle));
330}
331
332void FakeBluetoothGattInterface::NotifyDescriptorAddedCallback(
333    int status, int server_if,
334    const bt_uuid_t& uuid,
335    int srvc_handle, int desc_handle) {
336  FOR_EACH_OBSERVER(
337      ServerObserver, server_observers_,
338      DescriptorAddedCallback(
339          this, status, server_if, uuid, srvc_handle, desc_handle));
340}
341
342void FakeBluetoothGattInterface::NotifyServiceStartedCallback(
343    int status, int server_if, int srvc_handle) {
344  FOR_EACH_OBSERVER(
345      ServerObserver, server_observers_,
346      ServiceStartedCallback(this, status, server_if, srvc_handle));
347}
348
349void FakeBluetoothGattInterface::NotifyRequestReadCallback(
350    int conn_id, int trans_id, const bt_bdaddr_t& bda, int attr_handle,
351    int offset, bool is_long) {
352  FOR_EACH_OBSERVER(
353      ServerObserver, server_observers_,
354      RequestReadCallback(
355          this, conn_id, trans_id, bda, attr_handle, offset, is_long));
356}
357
358void FakeBluetoothGattInterface::NotifyRequestWriteCallback(
359    int conn_id, int trans_id,
360    const bt_bdaddr_t& bda, int attr_handle,
361    int offset, int length,
362    bool need_rsp, bool is_prep, uint8_t* value) {
363  FOR_EACH_OBSERVER(
364      ServerObserver, server_observers_,
365      RequestWriteCallback(
366          this, conn_id, trans_id, bda, attr_handle, offset, length, need_rsp,
367          is_prep, value));
368}
369
370void FakeBluetoothGattInterface::NotifyRequestExecWriteCallback(
371    int conn_id, int trans_id, const bt_bdaddr_t& bda, int exec_write) {
372  FOR_EACH_OBSERVER(
373      ServerObserver, server_observers_,
374      RequestExecWriteCallback(this, conn_id, trans_id, bda, exec_write));
375}
376
377void FakeBluetoothGattInterface::NotifyIndicationSentCallback(
378    int conn_id, int status) {
379  FOR_EACH_OBSERVER(ServerObserver, server_observers_,
380                    IndicationSentCallback(this, conn_id, status));
381}
382
383void FakeBluetoothGattInterface::AddClientObserver(ClientObserver* observer) {
384  CHECK(observer);
385  client_observers_.AddObserver(observer);
386}
387
388void FakeBluetoothGattInterface::RemoveClientObserver(
389    ClientObserver* observer) {
390  CHECK(observer);
391  client_observers_.RemoveObserver(observer);
392}
393
394void FakeBluetoothGattInterface::AddServerObserver(ServerObserver* observer) {
395  CHECK(observer);
396  server_observers_.AddObserver(observer);
397}
398
399void FakeBluetoothGattInterface::RemoveServerObserver(
400    ServerObserver* observer) {
401  CHECK(observer);
402  server_observers_.RemoveObserver(observer);
403}
404
405const btgatt_client_interface_t*
406FakeBluetoothGattInterface::GetClientHALInterface() const {
407  return &fake_btgattc_iface;
408}
409
410const btgatt_server_interface_t*
411FakeBluetoothGattInterface::GetServerHALInterface() const {
412  return &fake_btgatts_iface;
413}
414
415}  // namespace hal
416}  // namespace bluetooth
417