b87a352d9ec3ccaa4782ab7f29674aeb04d2087a
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-client.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dlog.h>
29
30 #include "bt-hal-log.h"
31 #include "bt-hal-msg.h"
32 #include "bt-hal-event-receiver.h"
33 #include "bt-hal-adapter-le.h"
34
35 #include "bt-hal-gatt-client.h"
36
37 /************************************************************************************
38  **  Static variables
39  ************************************************************************************/
40 extern const btgatt_callbacks_t *bt_gatt_callbacks;
41 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
42 {\
43         ERR("%s: BTGATT not initialized", __FUNCTION__);\
44         return BT_STATUS_NOT_READY;\
45 } else {\
46         DBG("%s", __FUNCTION__);\
47 }
48
49 typedef struct {
50         uint32_t client_if;
51         bt_uuid_t uuid;
52 } hal_registered_client_t;
53
54 #ifdef TIZEN_BT_HAL
55 int le_scan_type = BT_GATTC_LE_SCAN_TYPE_PASSIVE;
56 #endif
57
58 static handle_stack_msg event_cb = NULL;
59
60 /* To send stack event to hal-av handler */
61 void _bt_hal_register_gatt_client_handler_cb(handle_stack_msg cb)
62 {
63         event_cb = cb;
64 }
65
66 void _bt_hal_unregister_gatt_client_handler_cb(void)
67 {
68         event_cb = NULL;
69 }
70
71 #ifdef TIZEN_BT_HAL
72 int _bt_hal_gatt_client_get_le_scan_type(void)
73 {
74         return le_scan_type;
75 }
76 #endif
77
78 static gboolean __bt_hal_register_client_cb(gpointer user_data)
79 {
80         struct hal_ev_gatt_client_registered ev;
81         hal_registered_client_t *client_info = user_data;
82
83         /* Prepare to send AV connecting event */
84         memset(&ev, 0, sizeof(ev));
85         ev.status = BT_STATUS_SUCCESS;
86         ev.client_if = client_info->client_if;
87         memcpy(ev.app_uuid, client_info->uuid.uu, sizeof(ev.app_uuid));
88
89         if (!event_cb)
90                 ERR("GATT Callback not registered");
91         else {
92                 DBG("GATT client registered, client_if: [%d]", client_info->client_if);
93                 event_cb(HAL_EV_GATT_CLIENT_REGISTERED, (void *)&ev, sizeof(ev));
94         }
95
96         g_free(user_data);
97         return FALSE;
98 }
99
100 /** Registers a GATT client application with the stack */
101 bt_status_t register_client(bt_uuid_t *uuid)
102 {
103         hal_registered_client_t *client_info = NULL;
104
105         client_info = g_malloc0(sizeof(hal_registered_client_t));
106         /*
107          * TODO: Actual client_if will be used here later when GATT register_client
108          * is implemented compeltely. For now return 1 as client_if.
109          */
110         client_info->client_if = 1;
111         memcpy(client_info->uuid.uu, uuid->uu, sizeof(bt_uuid_t));
112
113         /*
114          * As we need to provide async callback to user from HAL, simply schedule a
115          * callback method which will carry actual result
116          */
117         g_idle_add(__bt_hal_register_client_cb, (gpointer)client_info);
118
119         /* If available, then return success, else return error */
120         return BT_STATUS_SUCCESS;
121 }
122
123 /* TODO: APIs will be implemented in subsequent patches whenever required */
124 /** Unregister a client application from the stack */
125 bt_status_t unregister_client(int client_if)
126 {
127         CHECK_BTGATT_INIT();
128         return BT_STATUS_UNSUPPORTED;
129 }
130
131 /** Start or stop LE device scanning */
132 bt_status_t scan(int client_if, bool start)
133 {
134         int ret;
135
136         CHECK_BTGATT_INIT();
137
138         if (start)
139                 ret = _bt_hal_adapter_le_start_scan();
140         else
141                 ret = _bt_hal_adapter_le_stop_scan();
142
143         return ret;
144 }
145
146 /** Create a connection to a remote LE or dual-mode device */
147 bt_status_t connect(int client_if, const bt_bdaddr_t *bd_addr,
148                 bool is_direct)
149 {
150         CHECK_BTGATT_INIT();
151         return BT_STATUS_UNSUPPORTED;
152 }
153
154 /** Disconnect a remote device or cancel a pending connection */
155 bt_status_t disconnect(int client_if, const bt_bdaddr_t *bd_addr,
156                 int conn_id)
157 {
158         CHECK_BTGATT_INIT();
159         return BT_STATUS_UNSUPPORTED;
160 }
161
162 /** Clear the attribute cache for a given device */
163 bt_status_t refresh(int client_if, const bt_bdaddr_t *bd_addr)
164 {
165         CHECK_BTGATT_INIT();
166         return BT_STATUS_UNSUPPORTED;
167 }
168
169 /**
170  * Enumerate all GATT services on a connected device.
171  * Optionally, the results can be filtered for a given UUID.
172  */
173 bt_status_t search_service(int conn_id, bt_uuid_t *filter_uuid)
174 {
175         CHECK_BTGATT_INIT();
176         return BT_STATUS_UNSUPPORTED;
177 }
178
179 /**
180  * Enumerate included services for a given service.
181  * Set start_incl_srvc_id to NULL to get the first included service.
182  */
183 bt_status_t get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
184                 btgatt_srvc_id_t *start_incl_srvc_id)
185 {
186         CHECK_BTGATT_INIT();
187         return BT_STATUS_UNSUPPORTED;
188 }
189
190 /**
191  * Enumerate characteristics for a given service.
192  * Set start_char_id to NULL to get the first characteristic.
193  */
194 bt_status_t get_characteristic(int conn_id,
195                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
196 {
197         CHECK_BTGATT_INIT();
198         return BT_STATUS_UNSUPPORTED;
199 }
200
201 /**
202  * Enumerate descriptors for a given characteristic.
203  * Set start_descr_id to NULL to get the first descriptor.
204  */
205 bt_status_t get_descriptor(int conn_id,
206                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
207                 btgatt_gatt_id_t *start_descr_id)
208 {
209         CHECK_BTGATT_INIT();
210         return BT_STATUS_UNSUPPORTED;
211 }
212
213 /** Read a characteristic on a remote device */
214 bt_status_t read_characteristic(int conn_id,
215                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
216                 int auth_req)
217 {
218         CHECK_BTGATT_INIT();
219         return BT_STATUS_UNSUPPORTED;
220 }
221
222 /** Write a remote characteristic */
223 bt_status_t write_characteristic(int conn_id,
224                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
225                 int write_type, int len, int auth_req,
226                 char* p_value)
227 {
228         CHECK_BTGATT_INIT();
229         return BT_STATUS_UNSUPPORTED;
230 }
231
232 /** Read the descriptor for a given characteristic */
233 bt_status_t read_descriptor(int conn_id,
234                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
235                 btgatt_gatt_id_t *descr_id, int auth_req)
236 {
237         CHECK_BTGATT_INIT();
238         return BT_STATUS_UNSUPPORTED;
239 }
240 /** Write a remote descriptor for a given characteristic */
241 bt_status_t write_descriptor(int conn_id,
242                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
243                 btgatt_gatt_id_t *descr_id, int write_type, int len,
244                 int auth_req, char* p_value)
245 {
246         CHECK_BTGATT_INIT();
247         return BT_STATUS_UNSUPPORTED;
248 }
249
250 /** Execute a prepared write operation */
251 bt_status_t execute_write(int conn_id, int execute)
252 {
253         CHECK_BTGATT_INIT();
254         return BT_STATUS_UNSUPPORTED;
255 }
256
257 /**
258  * Register to receive notifications or indications for a given
259  * characteristic
260  */
261 bt_status_t register_for_notification(int client_if,
262                 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
263                 btgatt_gatt_id_t *char_id)
264 {
265         CHECK_BTGATT_INIT();
266         return BT_STATUS_UNSUPPORTED;
267 }
268
269 /** Deregister a previous request for notifications/indications */
270 bt_status_t deregister_for_notification(int client_if,
271                 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
272                 btgatt_gatt_id_t *char_id)
273 {
274         CHECK_BTGATT_INIT();
275         return BT_STATUS_UNSUPPORTED;
276 }
277
278 /** Request RSSI for a given remote device */
279 bt_status_t read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
280 {
281         CHECK_BTGATT_INIT();
282         return BT_STATUS_UNSUPPORTED;
283 }
284
285 /** OTA firmware download */
286 bt_status_t ota_fw_update(int client_if, int conn_id, const bt_bdaddr_t *bd_addr, char* path)
287 {
288         CHECK_BTGATT_INIT();
289         return BT_STATUS_UNSUPPORTED;
290 }
291
292 /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
293 int get_device_type(const bt_bdaddr_t *bd_addr)
294 {
295         CHECK_BTGATT_INIT();
296         return BT_STATUS_UNSUPPORTED;
297 }
298
299 /** Request a connection parameter update */
300 bt_status_t conn_parameter_update(bt_bdaddr_t *bd, int min_int, int max_int, int latency, int timeout)
301 {
302         CHECK_BTGATT_INIT();
303         return BT_STATUS_UNSUPPORTED;
304 }
305
306 /** Test mode interface */
307 bt_status_t test_command(int command, btgatt_test_params_t* params)
308 {
309         CHECK_BTGATT_INIT();
310         return BT_STATUS_UNSUPPORTED;
311 }
312
313 /** MTU Exchange request from client */
314 bt_status_t configure_mtu(int conn_id, int mtu)
315 {
316         CHECK_BTGATT_INIT();
317         return BT_STATUS_UNSUPPORTED;
318 }
319
320 /** Setup scan filter params */
321 bt_status_t scan_filter_param_setup(int client_if, int action, int filt_index, int feat_seln,
322                 int list_logic_type, int filt_logic_type, int rssi_high_thres,
323                 int rssi_low_thres, int dely_mode, int found_timeout,
324                 int lost_timeout, int found_timeout_cnt)
325 {
326         CHECK_BTGATT_INIT();
327         return BT_STATUS_UNSUPPORTED;
328 }
329
330
331 /** Configure a scan filter condition */
332 bt_status_t scan_filter_add_remove(int client_if, int action, int filt_type,
333                 int filt_index, int company_id,
334                 int company_id_mask, const bt_uuid_t *p_uuid,
335                 const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
336                 char addr_type, int data_len, char* p_data, int mask_len,
337                 char* p_mask)
338 {
339         CHECK_BTGATT_INIT();
340         return BT_STATUS_UNSUPPORTED;
341 }
342
343 /** Clear all scan filter conditions for specific filter index*/
344 bt_status_t scan_filter_clear(int client_if, int filt_index)
345 {
346         CHECK_BTGATT_INIT();
347         return BT_STATUS_UNSUPPORTED;
348 }
349
350 /** Enable / disable scan filter feature*/
351 bt_status_t scan_filter_enable(int client_if, bool enable)
352 {
353         CHECK_BTGATT_INIT();
354         return BT_STATUS_UNSUPPORTED;
355 }
356
357 /** Sets the LE scan interval and window in units of N*0.625 msec */
358 #ifdef TIZEN_BT_HAL
359 bt_status_t set_scan_parameters(int scan_type, int scan_interval, int scan_window)
360 {
361         int ret;
362
363         CHECK_BTGATT_INIT();
364
365         le_scan_type = scan_type;
366         ret = _bt_hal_adapter_le_set_scan_parameters(
367                         scan_type, scan_interval, scan_window);
368         return ret;
369 }
370 #else
371 bt_status_t set_scan_parameters(int scan_interval, int scan_window)
372 {
373         CHECK_BTGATT_INIT();
374         return BT_STATUS_UNSUPPORTED;
375 }
376 #endif
377
378 /* Configure the batchscan storage */
379 bt_status_t batchscan_cfg_storage(int client_if, int batch_scan_full_max,
380                 int batch_scan_trunc_max, int batch_scan_notify_threshold)
381 {
382         CHECK_BTGATT_INIT();
383         return BT_STATUS_UNSUPPORTED;
384 }
385
386 /* Enable batchscan */
387 bt_status_t batchscan_enb_batch_scan(int client_if, int scan_mode,
388                 int scan_interval, int scan_window, int addr_type, int discard_rule)
389 {
390         CHECK_BTGATT_INIT();
391         return BT_STATUS_UNSUPPORTED;
392 }
393
394 /* Disable batchscan */
395 bt_status_t batchscan_dis_batch_scan(int client_if)
396 {
397         CHECK_BTGATT_INIT();
398         return BT_STATUS_UNSUPPORTED;
399 }
400
401 /* Read out batchscan reports */
402 bt_status_t batchscan_read_reports(int client_if, int scan_mode)
403 {
404         CHECK_BTGATT_INIT();
405         return BT_STATUS_UNSUPPORTED;
406 }
407
408 const btgatt_client_interface_t btgatt_client_interface = {
409         register_client,
410         unregister_client,
411         scan,
412         connect,
413         disconnect,
414         refresh,
415         search_service,
416         get_included_service,
417         get_characteristic,
418         get_descriptor,
419         read_characteristic,
420         write_characteristic,
421         read_descriptor,
422         write_descriptor,
423         execute_write,
424         register_for_notification,
425         deregister_for_notification,
426         read_remote_rssi,
427         ota_fw_update,
428         get_device_type,
429         conn_parameter_update,
430         test_command,
431         configure_mtu,
432         scan_filter_param_setup,
433         scan_filter_add_remove,
434         scan_filter_clear,
435         scan_filter_enable,
436         set_scan_parameters,
437         batchscan_cfg_storage,
438         batchscan_enb_batch_scan,
439         batchscan_dis_batch_scan,
440         batchscan_read_reports
441 };