Merge "Use milisec unit instead of sec for power duration" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-gatt.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <dlog.h>
20 #include <bluetooth.h>
21 #include "bt_gatt.h"
22 #include "oal-event.h"
23 #include "oal-internal.h"
24 #include "oal-manager.h"
25 #include "oal-adapter-mgr.h"
26 #include "oal-utils.h"
27 #include "oal-gatt.h"
28 #include "oal-common.h"
29
30 #define DEV_APPEARANCE  0x0000
31 #define DEFAULT_GATT_CLIENT_UUID "0000A00A-1111-1111-0123-456789ABCDEF"
32 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
33 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
34
35 #define NUM_SERVER_INST 10 // Maximum slots supported by stack is set to 10
36 #define CHECK_OAL_GATT_ENABLED() \
37         do { \
38                 if (gatt_api == NULL) { \
39                         BT_ERR("GATT Not Enabled"); \
40                         return OAL_STATUS_NOT_READY; \
41                 } \
42         } while (0)
43
44 #define CHECK_SERVER_INSTANCE(instance_id) \
45         do { \
46                 if (instance_id < 1 || instance_id > NUM_SERVER_INST) { \
47                         BT_ERR("Invalid Instance"); \
48                         return OAL_STATUS_INVALID_PARAM; \
49                 } \
50         } while (0)
51
52 #define CHECK_SERVER_REGISTRATION(instance_id) \
53         do { \
54                 if (gatt_servers[instance_id - 1].server_id < 0)  {\
55                         BT_INFO("GATT server registeration is not done for server Instance : %d", instance_id);\
56                         return OAL_STATUS_INTERNAL_ERROR;\
57                 } \
58         } while (0)
59
60 #define CHECK_CORRECT_SERVER_ID(instance_id, server_if, server_inst)\
61         do { \
62                 for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) { \
63                         if (gatt_servers[instance_id - 1].server_id == server_if) { \
64                                 server_inst = instance_id; \
65                                 goto sendevent;\
66                         } \
67                 } \
68         } while (0)
69
70 #define PRINT_ADV_DATA(length, manuf_data, data_type) \
71         do { \
72                 char * buf_str = g_malloc0(3*length + 1);\
73                 if (NULL != buf_str) { \
74                         convert_hex_2_str(manuf_data, length, buf_str);\
75                         if (TRUE == data_type) \
76                         BT_INFO("Scan Response Data:%s\n", buf_str);\
77                         else \
78                         BT_INFO("Advertising Data:%s\n", buf_str);\
79                         g_free(buf_str);\
80                 } \
81         } while (0)
82
83 #define CHECK_CLIENT_REGISTRATION(client_id) \
84         do { \
85                 if (client_id <= 0) { \
86                         BT_DBG("No client registered yet...");\
87                         return OAL_STATUS_INTERNAL_ERROR;\
88                 } \
89         } while (0)
90
91 #define CHECK_CLIENT_CONNECTION(conn_id) \
92         do { \
93                 if (conn_id <= 0) {\
94                         BT_ERR("No active connection");\
95                         return OAL_STATUS_INTERNAL_ERROR;\
96                 } \
97         } while (0)
98
99
100 typedef enum {
101         GATT_INS_DISABLED,
102         GATT_INS_DATA_SETTING,
103         GATT_INS_DATA_SET,
104         GATT_INS_ENABLING,
105         GATT_INS_ENABLED,
106 } gatt_data_state;
107
108 typedef struct {
109         int server_id;
110         oal_uuid_t uuid;
111         int state;
112 } gatt_server_t;
113
114 const char *oal_device_type[] = {
115         "UNKNOWN",
116         "BR/EDR",
117         "BLE",
118         "DUAL_MODE",
119 };
120
121 typedef enum {
122         OAL_REQ_NONE,
123         OAL_REQ_LE_SCAN,
124         OAL_REQ_LE_CONNECT,
125 } oal_pending_gattc_req_e;
126
127 static const btgatt_interface_t * gatt_api;
128 static gboolean cur_adv_state[NUM_SERVER_INST];
129 static gatt_server_t gatt_servers[NUM_SERVER_INST];
130
131 /* Forward declarations of GATT Server callbacks */
132 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
133 static void cb_gatts_multi_adv_enable(int server_if, int status);
134 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status);
135 static void cb_gatts_multi_adv_disable(int server_if, int status);
136 static void cb_gatts_multi_adv_update(int server_if, int status);
137
138 static void cb_gatts_listen(int status, int server_if);
139
140
141 static void cb_gatts_service_added(int status, int server_if, btgatt_srvc_id_t *srvc_id, int srvc_handle);
142 static void cb_gatts_included_service_added(int status, int server_if, int srvc_handle, int incl_srvc_handle);
143 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id, int srvc_handle, int char_handle);
144 static void cb_gatts_descriptor_added(int status, int server_if, bt_uuid_t *descr_id, int srvc_handle, int descr_handle);
145
146 static void cb_gatts_service_started(int status, int server_if, int srvc_handle);
147 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle);
148 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle);
149
150 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, bool is_long);
151 static void cb_gatts_request_write(int conn_id, int trans_id, bt_bdaddr_t *bda, int attr_handle, int offset, int length,
152                                         bool need_rsp, bool is_prep, uint8_t* value);
153 static void cb_gatts_response_confirmation(int status, int handle);
154
155 static void cb_gatts_acquire_write(int fd, int conn_id, int trans_id, int attr_handle, bt_bdaddr_t*);
156 static void cb_gatts_acquire_notify(int fd, int conn_id, int trans_id, int attr_handle);
157
158 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda);
159 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda);
160
161 static void cb_gatts_mtu_changed(int conn_id, int mtu);
162
163 #ifdef TIZEN_BT_HAL
164 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify,  bt_bdaddr_t *bda);
165 #endif
166
167 /************************************HAL Interface *************************************/
168
169 /*TODO GATT Server callbacks will be implemented in subsequent patches */
170 static const btgatt_server_callbacks_t btgatt_server_callbacks = {
171         .register_server_cb = cb_gatts_register_app,
172         .connection_cb = cb_gatts_connection,
173         .service_added_cb = cb_gatts_service_added,
174         .included_service_added_cb = cb_gatts_included_service_added,
175         .characteristic_added_cb = cb_gatts_characteristic_added,
176         .descriptor_added_cb = cb_gatts_descriptor_added,
177         .service_started_cb = cb_gatts_service_started,
178         .service_stopped_cb = cb_gatts_service_stopped,
179         .service_deleted_cb = cb_gatts_service_deleted,
180         .indication_confirmation_cb = cb_indication_confirmation,
181         .request_read_cb = cb_gatts_request_read,
182         .request_write_cb = cb_gatts_request_write,
183         .request_exec_write_cb = NULL,
184         .response_confirmation_cb = cb_gatts_response_confirmation,
185         .listen_cb = cb_gatts_listen,
186         .multi_adv_enable_cb = cb_gatts_multi_adv_enable,
187         .multi_adv_update_cb = cb_gatts_multi_adv_update,
188         .multi_adv_data_cb = cb_gatts_multi_adv_set_inst_data,
189         .multi_adv_disable_cb = cb_gatts_multi_adv_disable,
190         .mtu_changed_cb = cb_gatts_mtu_changed,
191 #ifdef TIZEN_BT_HAL
192         .notif_enabled_cb = cb_notifcation_changed,
193 #endif
194         .request_acquire_write_cb = cb_gatts_acquire_write,
195         .request_acquire_notify_cb = cb_gatts_acquire_notify
196 };
197
198 /* Forward declaration for GATT client callbacks */
199 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid);
200 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
201                 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len);
202 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
203 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
204 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id);
205 static void cb_gattc_search_complete(int conn_id, int status);
206 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
207                                                 btgatt_gatt_id_t *char_id, int char_prop);
208 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
209                                 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id);
210 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data);
211 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data);
212 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data);
213 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data);
214 static void cb_gattc_register_for_notification(int conn_id, int registered, int status,
215                         btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id);
216 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data);
217 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id);
218 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu);
219
220 /*TODO GATT CLient callbacks will be implemented in subsequent patches */
221 static const btgatt_client_callbacks_t btgatt_client_callbacks = {
222         .register_client_cb = cb_gattc_register_app,
223         .scan_result_cb = cb_gattc_scan_result,
224         .open_cb = cb_gattc_connection,
225         .close_cb = cb_gattc_disconnect,
226         .search_complete_cb = cb_gattc_search_complete,
227         .search_result_cb = cb_gattc_search_result,
228         .get_characteristic_cb = cb_gattc_get_characteristics,
229         .get_descriptor_cb = cb_gattc_get_descriptor,
230         .get_included_service_cb = NULL,
231         .register_for_notification_cb = cb_gattc_register_for_notification,
232         .notify_cb = cb_gattc_notify,
233         .service_changed_cb = cb_gattc_service_changed,
234         .read_characteristic_cb = cb_gattc_read_characteristic,
235         .write_characteristic_cb = cb_gattc_write_characteristic,
236         .read_descriptor_cb = cb_gattc_read_descriptor,
237         .write_descriptor_cb = cb_gattc_write_descriptor,
238         .execute_write_cb = NULL,
239         .read_remote_rssi_cb = NULL,
240         .configure_mtu_cb = cb_gattc_configure_mtu_cmpl,
241 #ifdef PLATFORM_ANDROID_HAL
242         .scan_filter_cfg_cb = NULL,
243         .scan_filter_param_cb = NULL,
244         .scan_filter_status_cb = NULL,
245         .congestion_cb = NULL,
246         .batchscan_cfg_storage_cb = NULL,
247         .batchscan_enb_disable_cb = NULL,
248         .batchscan_reports_cb = NULL,
249         .batchscan_threshold_cb = NULL,
250         .track_adv_event_cb = NULL,
251 #endif
252 };
253
254 static btgatt_callbacks_t btgatt_callbacks = {
255         sizeof(btgatt_callbacks_t),
256         &btgatt_client_callbacks,
257         &btgatt_server_callbacks
258 };
259
260 /*******************************GATT Initialisation - Deinitialisation********************************/
261 oal_status_t gatt_enable(void)
262 {
263         const bt_interface_t * blued_api;
264         int ret;
265         int i = 0;
266
267         for (i = 0; i < NUM_SERVER_INST; i++) {
268                 gatt_servers[i].server_id = -1;
269                 gatt_servers[i].state = GATT_INS_DISABLED;
270                 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
271                 cur_adv_state[i] = FALSE;
272         }
273
274         /* Get stack interface */
275         blued_api = (const bt_interface_t *) adapter_get_stack_interface();
276
277         if (blued_api == NULL) {
278                 BT_ERR("Stack is not initialized");
279                 return OAL_STATUS_NOT_READY;
280         }
281
282         if (gatt_api) {
283                 BT_WARN("GATT Interface is already initialized...");
284                 return OAL_STATUS_ALREADY_DONE;
285         }
286
287         gatt_api = (const btgatt_interface_t *)blued_api->get_profile_interface(BT_PROFILE_GATT_ID);
288         if (gatt_api == NULL) {
289                 BT_ERR("GATT interface failed");
290                 return OAL_STATUS_INTERNAL_ERROR;
291         }
292
293         if ((ret = gatt_api->init(&btgatt_callbacks)) != BT_STATUS_SUCCESS) {
294                 BT_ERR("Error: Unable to initialise GATT :%s", status2string(ret));
295                 gatt_api->cleanup();
296                 gatt_api = NULL;
297                 return convert_to_oal_status(ret);
298         }
299
300         BT_INFO("GATT successfully initialized");
301         return OAL_STATUS_SUCCESS;
302 }
303
304 oal_status_t gatt_disable(void)
305 {
306         int i;
307         if (gatt_api) {
308                 gatt_api->cleanup();
309                 gatt_api = NULL;
310         }
311
312         for (i = 0; i < NUM_SERVER_INST; i++) {
313                 gatt_servers[i].server_id = -1;
314                 gatt_servers[i].state = GATT_INS_DISABLED;
315                 memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
316         }
317         return OAL_STATUS_SUCCESS;
318 }
319
320 /************************************GATT Server Functions*************************************/
321 /*Public */
322 oal_status_t gatts_register(oal_uuid_t* server_uuid)
323 {
324         char str[2*BT_UUID_STRING_MAX];
325         int ret = OAL_STATUS_SUCCESS;
326         int i;
327
328         CHECK_OAL_GATT_ENABLED();
329         uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
330         API_TRACE("Register the server instance: UUID: [%s]", str);
331
332         for (i = 0; i < NUM_SERVER_INST; i++) {
333                 if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
334                         if (gatt_servers[i].server_id != -1) {
335                                 BT_ERR("This is resevered UUID for easy set up application i = %d \
336                                                 gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
337                                 return OAL_STATUS_ALREADY_DONE;
338                         }
339                 }
340         }
341
342         ret = gatt_api->server->register_server((bt_uuid_t*)server_uuid);
343
344         if (ret != BT_STATUS_SUCCESS) {
345                 BT_ERR("GATT server registration with UUID:%s failed: %s", str, status2string(ret));
346                 return convert_to_oal_status(ret);
347         }
348         return OAL_STATUS_SUCCESS;
349 }
350
351 oal_status_t gatts_unregister(int instance_id)
352 {
353         int ret = OAL_STATUS_SUCCESS;
354         API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
355
356         CHECK_OAL_GATT_ENABLED();
357         CHECK_SERVER_INSTANCE(instance_id);
358         CHECK_SERVER_REGISTRATION(instance_id);
359
360         ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
361
362         if (ret != BT_STATUS_SUCCESS) {
363                 BT_ERR("GATT server unregistration failed: %d", instance_id);
364                 return convert_to_oal_status(ret);
365         }
366
367         gatt_servers[instance_id-1].server_id = -1;
368         gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
369
370         if (instance_id != 1 && instance_id != 2)
371                 memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
372
373         return OAL_STATUS_SUCCESS;
374 }
375
376 /* Legacy Advertisement */
377 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
378 {
379         int ret = OAL_STATUS_SUCCESS;
380         API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
381
382         CHECK_OAL_GATT_ENABLED();
383         CHECK_SERVER_INSTANCE(instance_id);
384         CHECK_SERVER_REGISTRATION(instance_id);
385
386         ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
387
388         if (ret != BT_STATUS_SUCCESS) {
389                 BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
390                                 gatt_servers[instance_id-1].server_id, status2string(ret));
391                 return convert_to_oal_status(ret);
392         }
393
394         return OAL_STATUS_SUCCESS;
395 }
396
397 oal_status_t gatts_stop_advertising(int instance_id)
398 {
399         int ret = OAL_STATUS_SUCCESS;
400         API_TRACE("Stop advertising");
401
402         CHECK_OAL_GATT_ENABLED();
403         CHECK_SERVER_INSTANCE(instance_id);
404         CHECK_SERVER_REGISTRATION(instance_id);
405
406         ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
407
408         if (ret != BT_STATUS_SUCCESS) {
409                 BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
410                         instance_id, status2string(ret));
411                 return convert_to_oal_status(ret);
412         }
413         return OAL_STATUS_SUCCESS;
414 }
415
416 #if 0
417 /* Below API's need to be removed as they are hardcoded API's.
418    If at all, product specific API's are required to set specific data, then new naming should
419    be adopted for these API's */
420 oal_status_t gatts_set_advertising_params(int instance_id, float min_intv, float max_intv)
421 {
422         int ret = OAL_STATUS_SUCCESS;
423
424         API_TRACE("Set advertising params min_int:%f, max_int:%f", min_intv, max_intv);
425         CHECK_OAL_GATT_ENABLED();
426         CHECK_SERVER_INSTANCE(instance_id);
427         CHECK_SERVER_REGISTRATION(instance_id);
428
429         /* Historically used for Legacy Advertising */
430         return OAL_STATUS_NOT_SUPPORT;
431 }
432
433 oal_status_t gatts_set_advertising_data(int instance_id, uint16_t manufac_len, char* manufac_data)
434 {
435         int ret = OAL_STATUS_SUCCESS;
436
437         API_TRACE("Set advertising data");
438         CHECK_OAL_GATT_ENABLED();
439         CHECK_SERVER_INSTANCE(instance_id);
440         CHECK_SERVER_REGISTRATION(instance_id);
441
442         /* Historically used for Legacy Advertising */
443         return OAL_STATUS_NOT_SUPPORT;
444 }
445
446 oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char* manufac_data)
447 {
448         int ret = OAL_STATUS_SUCCESS;
449
450         API_TRACE("Set Scan Response data");
451         CHECK_OAL_GATT_ENABLED();
452         CHECK_SERVER_INSTANCE(instance_id);
453         CHECK_SERVER_REGISTRATION(instance_id);
454
455         /* Historically used for Legacy Advertising */
456         return OAL_STATUS_NOT_SUPPORT;
457 }
458 #endif
459
460 oal_status_t gatts_multi_adv_enable(int instance_id)
461 {
462         int ret = OAL_STATUS_SUCCESS;
463         API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
464                         gatt_servers[instance_id - 1].server_id, instance_id);
465
466         CHECK_OAL_GATT_ENABLED();
467         CHECK_SERVER_INSTANCE(instance_id);
468         CHECK_SERVER_REGISTRATION(instance_id);
469
470         if (gatt_api->server->multi_adv_enable == NULL)
471                 return OAL_STATUS_NOT_SUPPORT;
472
473 /* Platform Requirement: Advertising to be enabled even when no data is set: i.e Empty Advertisement
474         if (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET){
475                 BT_ERR("Not allowed, state: %d, instance_id: %d",
476                                 gatt_servers[instance_id - 1].state, instance_id);
477                 return OAL_STATUS_BUSY;
478         }
479 */
480         if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
481                 BT_ERR("Not allowed, state: %d, instance_id: %d",
482                                 gatt_servers[instance_id - 1].state, instance_id);
483                 return OAL_STATUS_ALREADY_DONE;
484         }
485
486         if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
487                 BT_ERR("Not allowed, state: %d, instance_id: %d",
488                                 gatt_servers[instance_id - 1].state, instance_id);
489                 return OAL_STATUS_BUSY;
490         }
491
492         gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
493
494         ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
495
496         if (ret != BT_STATUS_SUCCESS) {
497                 BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
498                                 instance_id, status2string(ret));
499 //              gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
500                 return convert_to_oal_status(ret);
501         }
502         return OAL_STATUS_SUCCESS;
503 }
504
505 oal_status_t gatts_multi_adv_disable(int instance_id)
506 {
507         int ret = OAL_STATUS_SUCCESS;
508         API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
509                         gatt_servers[instance_id - 1].server_id, instance_id);
510
511         CHECK_OAL_GATT_ENABLED();
512         CHECK_SERVER_INSTANCE(instance_id);
513         CHECK_SERVER_REGISTRATION(instance_id);
514
515         if (gatt_api->server->multi_adv_disable == NULL)
516                 return OAL_STATUS_NOT_SUPPORT;
517
518         if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
519                 BT_ERR("Not Allowed, state: %d, instance_id: %d",
520                         gatt_servers[instance_id - 1].state, instance_id);
521                 return OAL_STATUS_BUSY;
522         }
523
524         ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
525
526         if (ret != BT_STATUS_SUCCESS) {
527                 BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
528                                 instance_id, status2string(ret));
529                 return convert_to_oal_status(ret);
530         }
531         return OAL_STATUS_SUCCESS;
532 }
533
534 oal_status_t gatts_set_filter_policy(int filter_policy)
535 {
536         int ret = OAL_STATUS_SUCCESS;
537
538         CHECK_OAL_GATT_ENABLED();
539
540         /* send the filter_policy value to the HAL Layer */
541         ret = gatt_api->server->set_filter_policy(filter_policy);
542
543         if (ret != BT_STATUS_SUCCESS) {
544                 BT_ERR("set_filter_policy failed: %d",ret);
545                 return convert_to_oal_status(ret);
546         }
547
548         return OAL_STATUS_SUCCESS;
549 }
550
551 oal_status_t gatts_multi_adv_update(int instance_id,
552                 int min_intv, int max_intv,
553                 int adv_type, int chnl_map,
554                 int tx_power, int timeout_s)
555 {
556         int ret = OAL_STATUS_SUCCESS;
557         API_TRACE("Multi advertising Update");
558
559         CHECK_OAL_GATT_ENABLED();
560         CHECK_SERVER_INSTANCE(instance_id);
561         CHECK_SERVER_REGISTRATION(instance_id);
562
563         if (gatt_api->server->multi_adv_update == NULL)
564                 return OAL_STATUS_NOT_SUPPORT;
565
566         ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
567                         min_intv, max_intv,
568                         adv_type, chnl_map,
569                         tx_power, timeout_s);
570
571         if (ret != BT_STATUS_SUCCESS) {
572                 BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
573                                 instance_id, status2string(ret));
574                 return convert_to_oal_status(ret);
575         }
576         return OAL_STATUS_SUCCESS;
577 }
578
579 oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
580                 oal_ble_multi_adv_param_setup_t *adv_param_setup)
581 {
582         int ret = OAL_STATUS_SUCCESS;
583         btgatt_adv_param_setup_t adv_setup;
584         API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
585                         gatt_servers[instance_id - 1].server_id, instance_id);
586
587         CHECK_OAL_GATT_ENABLED();
588         CHECK_SERVER_INSTANCE(instance_id);
589         CHECK_SERVER_REGISTRATION(instance_id);
590
591         if (gatt_api->server->multi_adv_set_inst_data == NULL)
592                 return OAL_STATUS_NOT_SUPPORT;
593
594         if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
595                 BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
596                 return OAL_STATUS_BUSY;
597         }
598
599         gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
600
601
602         adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
603         adv_setup.include_name = adv_param_setup->include_name;
604         adv_setup.include_txpower = adv_param_setup->include_txpower;
605         adv_setup.appearance = adv_param_setup->appearance;
606         adv_setup.manufacturer_data = adv_param_setup->manufacturer_data;
607         adv_setup.manufacturer_data_len = adv_param_setup->manufacturer_data_len;
608         adv_setup.service_data = adv_param_setup->service_data;
609         adv_setup.service_data_len = adv_param_setup->service_data_len;
610         adv_setup.service_uuid = adv_param_setup->service_uuid;
611         adv_setup.service_uuid_len = adv_param_setup->service_uuid_len;
612         /* Solicit UUID handler: Start */
613         adv_setup.solicit_uuid = adv_param_setup->solicit_uuid;
614         adv_setup.solicit_uuid_len = adv_param_setup->solicit_uuid_len;
615         /* Solicit UUID handler: End */
616         adv_setup.min_interval = adv_param_setup->min_interval;
617         adv_setup.max_interval = adv_param_setup->min_interval;
618         adv_setup.adv_type = adv_param_setup->adv_type;
619         adv_setup.chnl_map = adv_param_setup->chnl_map;
620         adv_setup.tx_power = adv_param_setup->tx_power;
621         adv_setup.timeout_s = adv_param_setup->timeout_s;
622
623         adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
624
625         ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
626         if (ret != BT_STATUS_SUCCESS) {
627                 BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
628                 gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
629                 return convert_to_oal_status(ret);
630         }
631         return OAL_STATUS_SUCCESS;
632 }
633
634 oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int conn_id)
635 {
636         int ret = OAL_STATUS_SUCCESS;
637         bdstr_t bdstr;
638
639         API_TRACE("Server Disonnect: instance_id = %d, conn_id = %d", instance_id, conn_id);
640
641         CHECK_OAL_GATT_ENABLED();
642         CHECK_SERVER_INSTANCE(instance_id);
643         CHECK_SERVER_REGISTRATION(instance_id);
644
645         API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
646
647         ret = gatt_api->server->disconnect(gatt_servers[instance_id - 1].server_id, (bt_bdaddr_t *) device_address, conn_id);
648         if (ret != BT_STATUS_SUCCESS) {
649                 BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
650                 return convert_to_oal_status(ret);
651         }
652         return OAL_STATUS_SUCCESS;
653 }
654
655
656 /************************************GATT Server Functions*************************************/
657 /*Server Callbacks*/
658 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
659 {
660         int i;
661         char str[2*BT_UUID_STRING_MAX];
662         uuid_to_stringname((service_uuid_t*)uuid, str);
663
664         event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
665
666         BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
667         BT_INFO("UUID: [%s]", str);
668
669         for (i = 0; i < NUM_SERVER_INST; i++) {
670                 if (gatt_servers[i].server_id == -1) {
671                         BT_INFO("Server Instance registered with stack [%d]", i+1);
672                         gatt_servers[i].server_id = server_if;
673                         event->server_inst = i+1;
674                         memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
675                         memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
676                         break;
677                 }
678         }
679         send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
680 }
681
682 static void cb_gatts_listen(int status, int server_if)
683 {
684         gboolean prev_state[NUM_SERVER_INST];
685         gboolean new_state;
686         event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
687         int instance_id;
688
689         for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
690                 if (gatt_servers[instance_id - 1].server_id == server_if) {
691                         event->server_inst = instance_id;
692                         break;
693                 } else {
694                         BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
695                 }
696         }
697         if ((event->server_inst > 0) && (event->server_inst <= 10)) {
698                 prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
699                 new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
700                 cur_adv_state[event->server_inst - 1] = new_state;
701         } else {
702                 BT_ERR("Invalid Callback...");
703                 g_free(event);
704                 return;
705         }
706
707         BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
708
709         if (TRUE == new_state)
710                 send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
711         else
712                 send_event(OAL_EVENT_BLE_ADVERTISING_STOPPED, event, sizeof(event_ble_adv_status));
713 }
714
715 static void cb_gatts_multi_adv_enable(int server_if, int status)
716 {
717
718         BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
719         event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
720         int instance_id;
721
722         if (status != BT_STATUS_SUCCESS)
723                 BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
724
725         for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
726                 if (gatt_servers[instance_id - 1].server_id == server_if) {
727                         event->server_inst = instance_id;
728                         gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
729                         goto sendevent;
730                 }
731         }
732         BT_ERR("Invalid Interface, srv_if: %d", server_if);
733         event->server_inst = -1;
734 sendevent:
735         event->status = convert_to_oal_status(status);
736         send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_ENABLE, event, sizeof(event_ble_multiadv_status));
737 }
738
739 static void cb_gatts_multi_adv_disable(int server_if, int status)
740 {
741         BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
742         event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
743         int instance_id;
744
745         if (status != BT_STATUS_SUCCESS)
746                 BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
747
748         for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
749                 if (gatt_servers[instance_id - 1].server_id == server_if) {
750                         event->server_inst = instance_id;
751                         gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
752                         goto sendevent;
753                 }
754         }
755         BT_ERR("Invalid Interface, srv_if: %d", server_if);
756         event->server_inst = -1;
757 sendevent:
758         event->status = convert_to_oal_status(status);
759         send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_DISABLE, event, sizeof(event_ble_multiadv_status));
760 }
761
762 static void cb_gatts_multi_adv_update(int server_if, int status)
763 {
764         BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
765         event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
766         int instance_id;
767
768         if (status != BT_STATUS_SUCCESS)
769                 BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
770
771         for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
772                 if (gatt_servers[instance_id - 1].server_id == server_if) {
773                         event->server_inst = instance_id;
774                         goto sendevent;
775                 }
776         }
777         BT_ERR("Invalid Interface, srv_if: %d", server_if);
778         event->server_inst = -1;
779 sendevent:
780         event->status = convert_to_oal_status(status);
781         send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_UPDATE, event, sizeof(event_ble_multiadv_status));
782 }
783
784 static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
785 {
786         BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
787         event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
788         int instance_id;
789
790         if (status != BT_STATUS_SUCCESS)
791                 BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
792
793         for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
794                 if (gatt_servers[instance_id - 1].server_id == server_if) {
795                         event->server_inst = instance_id;
796                         gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
797                         goto sendevent;
798                 }
799         }
800         BT_ERR("Invalid Interface, srv_if: %d", server_if);
801         event->server_inst = -1;
802 sendevent:
803         event->status = convert_to_oal_status(status);
804         send_event(OAL_EVENT_BLE_MULTI_ADVERTISING_SET_INST_DATA, event, sizeof(event_ble_multiadv_status));
805 }
806
807 oal_status_t gatts_get_att_mtu(int conn_id, int *mtu)
808 {
809         int ret = OAL_STATUS_SUCCESS;
810         API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
811         CHECK_OAL_GATT_ENABLED();
812         OAL_CHECK_PARAMETER(mtu, return);
813
814         /* To prevent crash in case other libraries not support this api */
815         if (gatt_api->server->get_att_mtu == NULL) {
816                 BT_WARN("get_att_mtu is NULL");
817                 return OAL_STATUS_NOT_SUPPORT;
818         }
819
820         ret = gatt_api->server->get_att_mtu(conn_id, mtu);
821         if (ret != BT_STATUS_SUCCESS) {
822                 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
823                 return convert_to_oal_status(ret);
824         }
825
826         BT_INFO("Current ATT MTU Size: %d", *mtu);
827         return OAL_STATUS_SUCCESS;
828 }
829 oal_status_t gatt_send_response_acquire(int conn_id, int trans_id,
830                 int status, int fd, int mtu , void * fdlist)
831 {
832         int ret = OAL_STATUS_SUCCESS;
833
834         API_TRACE("Server Send Response : ConnId = %d, TransId = %d,  %d", conn_id, trans_id, fd);
835         CHECK_OAL_GATT_ENABLED();
836
837         ret = gatt_api->server->send_response_acquire(conn_id, trans_id, status, fd, mtu, fdlist);
838         if (ret != BT_STATUS_SUCCESS) {
839                 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
840                 return convert_to_oal_status(ret);
841         }
842         return OAL_STATUS_SUCCESS;
843
844 }
845 oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id,
846                 int num_handles)
847 {
848         int ret = OAL_STATUS_SUCCESS;
849         btgatt_srvc_id_t btgatt_srvc_id;
850         char str[2*BT_UUID_STRING_MAX];
851
852         API_TRACE("Server Add Service : instance_id = %d, num_handles = %d", instance_id, num_handles);
853         CHECK_OAL_GATT_ENABLED();
854
855         CHECK_SERVER_INSTANCE(instance_id);
856         CHECK_SERVER_REGISTRATION(instance_id);
857
858         if (gatt_serv_id != NULL) {
859                 uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
860                 API_TRACE("Service uuid: [%s]", str);
861                 memcpy(btgatt_srvc_id.id.uuid.uu,  gatt_serv_id->id.uuid.uuid, sizeof(gatt_serv_id->id.uuid));
862                 btgatt_srvc_id.id.inst_id  = gatt_serv_id->id.inst_id;
863                 btgatt_srvc_id.is_primary = gatt_serv_id->is_prmry;
864         } else {
865                 BT_INFO("GATT Server Service Id is NULL");
866                 return OAL_STATUS_INVALID_PARAM;
867         }
868
869         ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
870
871         if (ret != BT_STATUS_SUCCESS) {
872                 BT_ERR("GATT Server add service failed: %s", status2string(ret));
873                 return convert_to_oal_status(ret);
874         }
875         return OAL_STATUS_SUCCESS;
876 }
877
878 oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
879                 int incl_handle)
880 {
881         int ret = OAL_STATUS_SUCCESS;
882         API_TRACE("Server Add included Service : instance_id = %d, serv_handle = %d, incl_handle = %d",
883                         instance_id, serv_handle, incl_handle);
884         CHECK_OAL_GATT_ENABLED();
885
886         CHECK_SERVER_INSTANCE(instance_id);
887
888         CHECK_SERVER_REGISTRATION(instance_id);
889
890         ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
891         if (ret != BT_STATUS_SUCCESS) {
892                 BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
893                 return convert_to_oal_status(ret);
894         }
895         return OAL_STATUS_SUCCESS;
896 }
897
898 oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
899                         oal_uuid_t* charc_uuid, int propts, int permsn)
900 {
901         int ret = OAL_STATUS_SUCCESS;
902         char str[2*BT_UUID_STRING_MAX];
903         char prop[MAX_PROP_LEN], perm[MAX_PERM_LEN];
904         char_prop_to_string(propts, prop);
905         char_perm_to_string(permsn, perm);
906         API_TRACE("Server Add Characteristic : instance_id = %d, serv_handle = %d, Properties =%s, Permisison = %s",
907                 instance_id, serv_handle, prop, perm);
908         CHECK_OAL_GATT_ENABLED();
909
910         CHECK_SERVER_INSTANCE(instance_id);
911
912         CHECK_SERVER_REGISTRATION(instance_id);
913
914         if (charc_uuid != NULL) {
915                 uuid_to_stringname(charc_uuid, str);
916                 API_TRACE("uuid: [%s]", str);
917         }
918
919         ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
920                         serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
921         if (ret != BT_STATUS_SUCCESS) {
922                 BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
923                 return convert_to_oal_status(ret);
924         }
925         return OAL_STATUS_SUCCESS;
926 }
927
928 oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
929                 oal_uuid_t* desc_uuid, int permsn)
930 {
931         int ret = OAL_STATUS_SUCCESS;
932         char str[2*BT_UUID_STRING_MAX];
933         char perm[MAX_PERM_LEN];
934         char_perm_to_string(permsn, perm);
935         API_TRACE("Server Add Descriptor : instance_id = %d, serv_handle = %d, Permisison = %s",
936                         instance_id, serv_handle, perm);
937         CHECK_OAL_GATT_ENABLED();
938
939         CHECK_SERVER_INSTANCE(instance_id);
940
941         CHECK_SERVER_REGISTRATION(instance_id);
942
943         if (desc_uuid != NULL) {
944                 uuid_to_stringname(desc_uuid, str);
945                 API_TRACE("uuid: [%s]", str);
946         }
947
948         ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
949                                 serv_handle, (bt_uuid_t*)desc_uuid, permsn);
950
951         if (ret != BT_STATUS_SUCCESS) {
952                 BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
953                 return convert_to_oal_status(ret);
954         }
955         return OAL_STATUS_SUCCESS;
956 }
957
958 oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
959 {
960         int ret = OAL_STATUS_SUCCESS;
961
962         API_TRACE("Server Start Service : Ins_id = %d, srvHndl = %d, transport = %d",
963                                 instance_id, svc_handle, transport);
964         CHECK_OAL_GATT_ENABLED();
965
966         CHECK_SERVER_INSTANCE(instance_id);
967
968         CHECK_SERVER_REGISTRATION(instance_id);
969
970         ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
971
972         if (ret != BT_STATUS_SUCCESS) {
973                 BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
974                 return convert_to_oal_status(ret);
975         }
976         return OAL_STATUS_SUCCESS;
977 }
978
979 oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
980 {
981         int ret = OAL_STATUS_SUCCESS;
982
983         API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
984         CHECK_OAL_GATT_ENABLED();
985
986         CHECK_SERVER_INSTANCE(ins_id);
987         CHECK_SERVER_REGISTRATION(ins_id);
988
989         ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
990         if (ret != BT_STATUS_SUCCESS) {
991                 BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
992                 return convert_to_oal_status(ret);
993         }
994         return OAL_STATUS_SUCCESS;
995 }
996
997 oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
998 {
999         int ret = OAL_STATUS_SUCCESS;
1000
1001         API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
1002         CHECK_OAL_GATT_ENABLED();
1003
1004         CHECK_SERVER_INSTANCE(ins_id);
1005
1006         CHECK_SERVER_REGISTRATION(ins_id);
1007
1008         ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
1009         if (ret != BT_STATUS_SUCCESS) {
1010                 BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
1011                 return convert_to_oal_status(ret);
1012         }
1013         return OAL_STATUS_SUCCESS;
1014 }
1015
1016 oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal_gatt_response_t *response)
1017 {
1018         int ret = OAL_STATUS_SUCCESS;
1019
1020         API_TRACE("Server Send Response : ConnId = %d, TransId = %d, ", conn_id, trans_id);
1021         CHECK_OAL_GATT_ENABLED();
1022
1023         if (response == NULL) {
1024                 BT_ERR("GATT Server attribute value is empty");
1025                 return OAL_STATUS_INVALID_PARAM;
1026         }
1027
1028         ret = gatt_api->server->send_response(conn_id, trans_id, resp_status, (btgatt_response_t *)response);
1029         if (ret != BT_STATUS_SUCCESS) {
1030                 BT_ERR("GATT Server Send Response failed: %s", status2string(ret));
1031                 return convert_to_oal_status(ret);
1032         }
1033         return OAL_STATUS_SUCCESS;
1034 }
1035
1036 oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
1037 {
1038         int ret = OAL_STATUS_SUCCESS;
1039
1040         API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
1041                         ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
1042         CHECK_OAL_GATT_ENABLED();
1043
1044         CHECK_SERVER_INSTANCE(ins_id);
1045
1046         CHECK_SERVER_REGISTRATION(ins_id);
1047
1048         if (value == NULL || len == 0) {
1049                 BT_ERR("GATT Server attribute value is empty");
1050                 return OAL_STATUS_INVALID_PARAM;
1051         }
1052
1053         ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
1054                         attr_hndl, conn_id, len, confirm, value);
1055
1056         if (ret != BT_STATUS_SUCCESS) {
1057                 BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
1058                 return convert_to_oal_status(ret);
1059         }
1060         return OAL_STATUS_SUCCESS;
1061 }
1062
1063 oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
1064 {
1065         int ret = OAL_STATUS_SUCCESS;
1066
1067         CHECK_OAL_GATT_ENABLED();
1068
1069         CHECK_SERVER_INSTANCE(ins_id);
1070
1071         CHECK_SERVER_REGISTRATION(ins_id);
1072
1073         if (value == NULL || value->len == 0) {
1074                 BT_ERR("GATT Server attribute value is empty");
1075                 return OAL_STATUS_INVALID_PARAM;
1076         }
1077
1078         API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
1079                         ins_id, value->handle, value->len);
1080
1081         ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
1082                         (int)value->handle, (int)value->len, (char*)value->value);
1083
1084         if (ret != BT_STATUS_SUCCESS) {
1085                 BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
1086                 return convert_to_oal_status(ret);
1087         }
1088         return OAL_STATUS_SUCCESS;
1089 }
1090
1091 /* GATT Server Callbacks:Start */
1092 static void cb_gatts_service_added(int status, int server_if,
1093                 btgatt_srvc_id_t *psrvc_id, int srvc_handle)
1094 {
1095         int instance_id;
1096
1097         BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
1098         event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
1099
1100         CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
1101
1102         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1103         event->gatt_srvc_stat.server_inst = -1;
1104 sendevent:
1105         event->gatt_srvc_stat.servic_hndl = srvc_handle;
1106         if (psrvc_id != NULL) {
1107                 event->gatt_srvc_id.id.inst_id = psrvc_id->id.inst_id;
1108                 //event->gatt_srvc_id.id.uuid = psrvc_id->id.uuid;
1109                 memcpy(event->gatt_srvc_id.id.uuid.uuid,  psrvc_id->id.uuid.uu, sizeof(psrvc_id->id.uuid));
1110                 event->gatt_srvc_id.is_prmry = psrvc_id->is_primary;
1111         }
1112
1113         event->gatt_srvc_stat.status = convert_to_oal_status(status);
1114
1115         send_event(OAL_EVENT_GATTS_SERVICE_ADDED, event, sizeof(event_gatts_srvc_prm_t));
1116 }
1117
1118 static void cb_gatts_included_service_added(int status, int server_if,
1119                 int srvc_handle,
1120                 int incl_srvc_handle)
1121 {
1122         int ins_id ;
1123         BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
1124                         status, srvc_handle, incl_srvc_handle);
1125
1126         event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
1127
1128         CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
1129
1130         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1131         event->gatt_srvc_stat.server_inst = -1;
1132 sendevent:
1133         event->gatt_srvc_stat.servic_hndl = srvc_handle;
1134         event->incl_srvc_hndl = incl_srvc_handle;
1135         event->gatt_srvc_stat.status = convert_to_oal_status(status);
1136         send_event(OAL_EVENT_GATTS_INCLUDED_SERVICE_ADDED, event, sizeof(event_gatts_incld_srvc_t));
1137 }
1138
1139 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
1140                 int srvc_handle, int char_handle)
1141 {
1142         int Ins_id ;
1143
1144         BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
1145                         status, srvc_handle, char_handle);
1146
1147         event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
1148
1149         CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1150
1151         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1152         event->gatt_srvc_stat.server_inst = -1;
1153 sendevent:
1154         event->gatt_srvc_stat.servic_hndl = srvc_handle;
1155         event->charctr_hndl = char_handle;
1156         event->gatt_srvc_stat.status = convert_to_oal_status(status);
1157         memcpy(event->charctr_uuid.uuid, char_id->uu, sizeof(bt_uuid_t));
1158         send_event(OAL_EVENT_GATTS_CHARACTERISTIC_ADDED, event, sizeof(event_gatts_srvc_charctr_t));
1159 }
1160
1161 static void cb_gatts_descriptor_added(int status, int server_if,
1162                 bt_uuid_t *descr_id, int srvc_handle,
1163                 int descr_handle)
1164 {
1165         int Ins_id ;
1166         BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
1167                         status, srvc_handle, descr_handle);
1168
1169         event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
1170
1171         CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
1172
1173         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1174         event->gatt_srvc_stat.server_inst = -1;
1175 sendevent:
1176         event->gatt_srvc_stat.servic_hndl = srvc_handle;
1177         event->descrptr_hndl = descr_handle;
1178         event->gatt_srvc_stat.status = convert_to_oal_status(status);
1179         memcpy(event->descrptr_uuid.uuid, descr_id->uu, sizeof(bt_uuid_t));
1180         send_event(OAL_EVENT_GATTS_DESCRIPTOR_ADDED, event, sizeof(event_gatts_srvc_descr_t));
1181 }
1182
1183 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
1184 {
1185         int Ins_id ;
1186         BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1187
1188         event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1189
1190         CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
1191
1192         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1193         event->server_inst = -1;
1194 sendevent:
1195         event->servic_hndl = srvc_handle;
1196         event->status = convert_to_oal_status(status);
1197         send_event(OAL_EVENT_GATTS_SERVICE_STARTED, event, sizeof(event_gatts_srvc_t));
1198 }
1199
1200 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
1201 {
1202         int ins_id ;
1203         BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1204
1205         event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1206
1207         CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1208
1209         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1210         event->server_inst = -1;
1211 sendevent:
1212         event->servic_hndl = srvc_handle;
1213         event->status = convert_to_oal_status(status);
1214         send_event(OAL_EVENT_GATTS_SERVICE_STOPED, event, sizeof(event_gatts_srvc_t));
1215 }
1216
1217 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
1218 {
1219         int ins_id;
1220         BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
1221
1222         event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
1223
1224         CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
1225
1226         BT_ERR("Invalid Interface, srv_if: %d", server_if);
1227         event->server_inst = -1;
1228 sendevent:
1229         event->servic_hndl = srvc_handle;
1230         event->status = convert_to_oal_status(status);
1231         send_event(OAL_EVENT_GATTS_SERVICE_DELETED, event, sizeof(event_gatts_srvc_t));
1232 }
1233
1234 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
1235 {
1236         int ins_id ;
1237         oal_event_t event_type;
1238
1239         BT_INFO("BTGATT SERVER CONNECTION  connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
1240         event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
1241
1242         if (connected == TRUE) {
1243                 for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
1244                         if ((gatt_servers[ins_id - 1].server_id == server_if)) {
1245                                 event->server_inst = ins_id;
1246                                 break;
1247                         } else {
1248                                 BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
1249                         }
1250                 }
1251                 if (ins_id == NUM_SERVER_INST+1) {
1252                         BT_WARN("Incorrect Server Interface received: %d\n",  server_if);
1253                         g_free(event);
1254                         return;
1255                 }
1256         } else {
1257                 /* server_inst is not required in disconnected case */
1258         }
1259         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1260         event->conn_id = conn_id;
1261         event_type = ((connected == TRUE) ? OAL_EVENT_GATTS_CONNECTION_COMPLETED : OAL_EVENT_GATTS_DISCONNECTION_COMPLETED);
1262         event->status = OAL_STATUS_SUCCESS;
1263
1264         send_event(event_type, event, sizeof(event_gatts_conn_t));
1265 }
1266
1267 static void cb_gatts_request_read(int conn_id, int trans_id, bt_bdaddr_t *bda,
1268                 int attr_handle, int offset, bool is_long)
1269 {
1270         BT_INFO("BTGATT SERVER REQUEST READ CB: conn_id:%d", conn_id);
1271
1272         event_gatts_srvc_read_attr_t* event = g_new0(event_gatts_srvc_read_attr_t, 1);
1273
1274         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1275
1276         event->attr_trans.attr_handle = attr_handle;
1277         event->attr_trans.conn_id = conn_id;
1278         event->attr_trans.trans_id = trans_id;
1279         event->attr_trans.offset = offset;
1280         event->is_long = is_long;
1281
1282         send_event(OAL_EVENT_GATTS_REQUEST_READ, event, sizeof(event_gatts_srvc_read_attr_t));
1283 }
1284
1285 static void cb_gatts_request_write(int conn_id, int trans_id,
1286                 bt_bdaddr_t *bda, int attr_handle,
1287                 int offset, int length,
1288                 bool need_rsp, bool is_prep, uint8_t* value)
1289 {
1290         BT_INFO("BTGATT SERVER REQUEST WRITE conn_id:%d", conn_id);
1291
1292         event_gatts_srvc_write_attr_t* event = g_new0(event_gatts_srvc_write_attr_t, 1);
1293
1294         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1295
1296         event->attr_trans.attr_handle = attr_handle;
1297         event->attr_trans.conn_id = conn_id;
1298         event->attr_trans.trans_id = trans_id;
1299         event->attr_trans.offset = offset;
1300         event->need_rsp = need_rsp;
1301         event->is_prep = is_prep;
1302
1303         if (length > 0 && value != NULL) {
1304                 if (length > OAL_GATT_MAX_ATTR_LEN)
1305                         length = OAL_GATT_MAX_ATTR_LEN;
1306                 memcpy(event->value, value, length);
1307                 event->length = length;
1308         }
1309         send_event(OAL_EVENT_GATTS_REQUEST_WRITE, event, sizeof(event_gatts_srvc_write_attr_t));
1310 }
1311
1312 static void cb_gatts_acquire_write(int mtu, int conn_id, int trans_id, int attr_handle , bt_bdaddr_t *bda)
1313
1314 {
1315         BT_INFO("BTGATT SERVER REQUEST ACQUIRE WRITE conn_id:%d", conn_id);
1316
1317         event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1318
1319         event->attr_trans.attr_handle = attr_handle;
1320         event->attr_trans.conn_id = conn_id;
1321         event->attr_trans.trans_id = trans_id;
1322         event->mtu = mtu;
1323         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1324
1325         send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE, event, sizeof(event_gatts_srvc_acquire_attr_t));
1326 }
1327
1328 static void cb_gatts_acquire_notify(int mtu, int conn_id, int trans_id, int attr_handle)
1329 {
1330         BT_INFO("BTGATT SERVER REQUEST ACQUIRE NOTIFY conn_id:%d", conn_id);
1331
1332         event_gatts_srvc_acquire_attr_t* event = g_new0(event_gatts_srvc_acquire_attr_t, 1);
1333
1334         event->attr_trans.attr_handle = attr_handle;
1335         event->attr_trans.conn_id = conn_id;
1336         event->attr_trans.trans_id = trans_id;
1337         event->mtu = mtu;
1338
1339         send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1340
1341 }
1342
1343 static void cb_gatts_response_confirmation(int status, int handle)
1344 {
1345         BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1346
1347         event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1348
1349         event->hndl = handle;
1350         event->status = convert_to_oal_status(status);
1351
1352         send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1353 }
1354
1355 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1356 {
1357         BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1358                         conn_id, trans_id, attr_handle);
1359
1360         event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1361
1362         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1363
1364         event->attr_handle = attr_handle;
1365         event->conn_id = conn_id;
1366         event->trans_id = trans_id;
1367         send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1368 }
1369
1370 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1371 {
1372         BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1373         event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1374         event->conn_id = conn_id;
1375         event->mtu_size = mtu;
1376         send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1377 }
1378
1379 #ifdef TIZEN_BT_HAL
1380 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify,  bt_bdaddr_t *bda)
1381 {
1382         BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1383                         conn_id, trans_id, attr_handle, notify);
1384
1385         event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1386
1387         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1388
1389         event->attr_handle = attr_handle;
1390         event->conn_id = conn_id;
1391         event->trans_id = trans_id;
1392         event->notify = notify;
1393         send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1394 }
1395 #endif
1396
1397
1398 /* GATT Server Callbacks: End */
1399 /************************************ GATT Client ***********************************/
1400 /* Client Callbacks */
1401 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1402 {
1403         char uuid_str[BT_UUID_STRING_MAX];
1404         event_gattc_register_t *event;
1405
1406         /* Check if GATT client registered for Default GATT client UUID */
1407         uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1408
1409         BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1410         event = g_new0(event_gattc_register_t, 1);
1411         event->client_if = clientIf;
1412         event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1413         memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1414         send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1415 }
1416
1417 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1418                 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1419 {
1420         event_ble_scan_result_info *event;
1421
1422         event = g_new0(event_ble_scan_result_info, 1);
1423         event->addr_type = addr_type;
1424         event->rssi = rssi;
1425         memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1426         memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1427         event->adv_data_len = adv_data_len;
1428         memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1429         event->scan_rsp_data_len = scan_rsp_data_len;
1430         send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1431                         sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1432 }
1433
1434 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1435 {
1436         event_gattc_conn_t *event;
1437         BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1438                                                         status, client_if, conn_id);
1439
1440         event = g_new0(event_gattc_conn_t, 1);
1441         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1442         event->client_if = client_if;
1443
1444         if (BT_STATUS_SUCCESS != status) {
1445                 event->status = OAL_STATUS_INTERNAL_ERROR;
1446                 BT_ERR("gattc connection Error: %d", status);
1447         } else {
1448                 event->conn_id = conn_id;
1449                 event->status = OAL_STATUS_SUCCESS;
1450         }
1451
1452         send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1453                                         sizeof(*event), (bt_address_t *)bda);
1454 }
1455
1456
1457 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1458 {
1459         event_gattc_conn_t *event;
1460         BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1461                                                         status, client_if, conn_id);
1462         event = g_new0(event_gattc_conn_t, 1);
1463         event->status = convert_to_oal_status(status);
1464         event->client_if = client_if;
1465         event->conn_id = conn_id;
1466         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1467         send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1468                                         sizeof(*event), (bt_address_t *)bda);
1469 }
1470
1471 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1472 {
1473         char uuid_str[2*BT_UUID_STRING_MAX];
1474
1475         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1476         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1477                 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1478
1479         event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1480         event->conn_status.status = OAL_STATUS_SUCCESS;
1481         event->conn_status.conn_id = conn_id;
1482         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1483
1484         send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1485 }
1486
1487 static void cb_gattc_search_complete(int conn_id, int status)
1488 {
1489         BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1490         status, conn_id);
1491
1492         event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1493         event->conn_id = conn_id;
1494         event->status = convert_to_oal_status(status);
1495
1496         send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1497 }
1498
1499
1500 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1501                                                 btgatt_gatt_id_t *char_id, int char_prop)
1502 {
1503         char uuid_str1[2*BT_UUID_STRING_MAX];
1504         char uuid_str2[2*BT_UUID_STRING_MAX];
1505         char str[50];
1506
1507         BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1508         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1509         BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1510                 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1511
1512         event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1513         event->conn_status.conn_id = conn_id;
1514         event->conn_status.status = convert_to_oal_status(status);
1515         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1516
1517         if (status == 0) {
1518                 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1519                 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1520                         uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1521                 event->char_prop = char_prop;
1522                 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1523         }
1524         send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1525 }
1526
1527
1528
1529 static void cb_gattc_get_descriptor(int conn_id, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id)
1530 {
1531         char uuid_str1[2*BT_UUID_STRING_MAX];
1532         char uuid_str2[2*BT_UUID_STRING_MAX];
1533         char uuid_str3[2*BT_UUID_STRING_MAX];
1534         BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1535         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1536         uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1537         BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1538                 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1539         BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1540
1541         event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1542         event->conn_status.conn_id = conn_id;
1543         event->conn_status.status = convert_to_oal_status(status);
1544         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1545         memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1546
1547         if (status == 0) {
1548                 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1549                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1550                 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1551         }
1552         send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1553 }
1554
1555 static void cb_gattc_register_for_notification(int conn_id, int registered, int status, btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1556 {
1557         char uuid_str1[2*BT_UUID_STRING_MAX];
1558         char uuid_str2[2*BT_UUID_STRING_MAX];
1559         BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1560                 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1561         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1562         uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1563         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1564         BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1565
1566         event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1567         oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1568         event->conn_id = conn_id;
1569         event->status = convert_to_oal_status(status);
1570         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1571         memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1572
1573         send_event(event_type, event, sizeof(*event));
1574 }
1575
1576 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1577 {
1578         bdstr_t bdstr;
1579         char uuid_str1[2*BT_UUID_STRING_MAX];
1580         char uuid_str2[2*BT_UUID_STRING_MAX];
1581
1582         BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1583         BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1584                 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1585
1586         uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1587         uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1588
1589         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1590                 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1591         BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1592
1593         if (p_data->len > 0) {
1594                 char *data = NULL;
1595                 data = g_malloc(3*p_data->len+1);
1596                 if (!data) {
1597                         BT_ERR("memory allocation failed");
1598                         return;
1599                 }
1600
1601                 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1602                 BT_INFO("Notified Data: [%s]", data);
1603
1604                 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1605                 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1606                 event->is_notify = p_data->is_notify;
1607                 event->data_len = p_data->len;
1608                 memcpy(event->data, p_data->value, event->data_len);
1609                 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1610                 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1611
1612                 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1613                 g_free(data);
1614         }
1615 }
1616
1617 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1618 {
1619         event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1620
1621         memcpy(event->address.addr, bd_addr->address, 6);
1622         event->change_type = change_type;
1623         memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1624         event->conn_id = conn_id;
1625         event->inst_id = inst_id;
1626
1627         send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1628 }
1629
1630 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1631 {
1632         char uuid_str1[2*BT_UUID_STRING_MAX];
1633         char uuid_str2[2*BT_UUID_STRING_MAX];
1634         BT_INFO("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1635
1636         event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1637         event->uuid_status.conn_status.conn_id = conn_id;
1638         event->uuid_status.conn_status.status = convert_to_oal_status(status);
1639         event->value_type = p_data->value_type;
1640         memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1641         memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1642         if (status == 0) {
1643                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1644                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1645                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1646                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1647                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1648                 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1649                 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1650                         event->data_len = p_data->value.len;
1651                         memcpy(&(event->data), &(p_data->value.value), event->data_len);
1652                 }
1653         }
1654         send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1655 }
1656
1657 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1658 {
1659         char uuid_str1[2*BT_UUID_STRING_MAX];
1660         char uuid_str2[2*BT_UUID_STRING_MAX];
1661         BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1662
1663         event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1664         event->conn_status.conn_id = conn_id;
1665         event->conn_status.status = convert_to_oal_status(status);
1666         memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1667         memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1668         if (status == 0) {
1669                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1670                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1671                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1672                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1673                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1674         }
1675         send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1676 }
1677
1678 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1679 {
1680         char uuid_str1[2*BT_UUID_STRING_MAX];
1681         char uuid_str2[2*BT_UUID_STRING_MAX];
1682         char uuid_str3[2*BT_UUID_STRING_MAX];
1683         BT_INFO("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1684
1685         event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1686         event->conn_status.conn_id = conn_id;
1687         event->conn_status.status = convert_to_oal_status(status);
1688         memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1689         memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1690         memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1691         if (status == 0) {
1692                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1693                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1694                 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1695                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1696                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1697                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1698                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1699         }
1700         send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1701 }
1702
1703
1704
1705 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1706 {
1707         char uuid_str1[2*BT_UUID_STRING_MAX];
1708         char uuid_str2[2*BT_UUID_STRING_MAX];
1709         char uuid_str3[2*BT_UUID_STRING_MAX];
1710         BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1711
1712         event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1713         event->uuid_status.conn_status.conn_id = conn_id;
1714         event->uuid_status.conn_status.status = convert_to_oal_status(status);
1715         event->value_type = p_data->value_type;
1716         memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1717         memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1718         memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1719
1720         if (status == 0) {
1721                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1722                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1723                 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1724                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1725                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1726                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1727                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1728                 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1729                 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1730                         char *data = NULL;
1731                         data = g_malloc(3*p_data->value.len+1);
1732                         if (!data) {
1733                                 BT_ERR("memory allocation failed");
1734                                 g_free(event);
1735                                 return;
1736                         }
1737                         convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1738                         BT_DBG("Read Data: [%s]", data);
1739                         event->data_len = p_data->value.len;
1740                         memcpy(&(event->data), &(p_data->value.value), event->data_len);
1741                         g_free(data);
1742                 }
1743         }
1744         send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1745 }
1746
1747 oal_status_t gattc_start_le_discovery(int client_id)
1748 {
1749
1750         int ret = OAL_STATUS_SUCCESS;
1751
1752         API_TRACE("BTGATT CLIENT SCAN START");
1753         CHECK_OAL_GATT_ENABLED();
1754         CHECK_CLIENT_REGISTRATION(client_id);
1755
1756         ret = gatt_api->client->scan(client_id, 1);
1757         if (ret != BT_STATUS_SUCCESS) {
1758                 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1759                 return convert_to_oal_status(ret);
1760         }
1761         return OAL_STATUS_SUCCESS;
1762 }
1763
1764 oal_status_t gattc_stop_le_discovery(int client_id)
1765 {
1766
1767         int ret = OAL_STATUS_SUCCESS;
1768
1769         API_TRACE("Scan is stopped");
1770         CHECK_OAL_GATT_ENABLED();
1771         CHECK_CLIENT_REGISTRATION(client_id);
1772         ret = gatt_api->client->scan(client_id, 0);
1773         if (ret != BT_STATUS_SUCCESS) {
1774                 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1775                 return convert_to_oal_status(ret);
1776         }
1777         return OAL_STATUS_SUCCESS;
1778 }
1779
1780 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1781 {
1782         int ret;
1783
1784         API_TRACE("Scan is stopped");
1785         CHECK_OAL_GATT_ENABLED();
1786
1787 #ifdef TIZEN_BT_HAL
1788         ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1789         if (ret != BT_STATUS_SUCCESS) {
1790                 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1791                 return convert_to_oal_status(ret);
1792         }
1793 #else
1794         ret = gatt_api->client->set_scan_parameters(itv, win);
1795         if (ret != BT_STATUS_SUCCESS) {
1796                 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1797                 return convert_to_oal_status(ret);
1798         }
1799 #endif
1800
1801         return OAL_STATUS_SUCCESS;
1802 }
1803
1804 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1805 {
1806         event_gattc_mtu_configured_t *event;
1807         BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1808                         conn_id, status, mtu);
1809         event = g_new0(event_gattc_mtu_configured_t, 1);
1810         event->conn_id = conn_id;
1811         event->mtu = mtu;
1812         event->status = convert_to_oal_status(status);
1813         send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1814 }
1815
1816 /************************************ GATT Client ***********************************/
1817 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1818 {
1819
1820         char str[2*BT_UUID_STRING_MAX];
1821         int ret = OAL_STATUS_SUCCESS;
1822
1823         CHECK_OAL_GATT_ENABLED();
1824         uuid_to_stringname(client_uuid, str);
1825         API_TRACE("uuid: [%s]", str);
1826         /* We will perform actual registration in cb_gattc_register_app callback */
1827         ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1828         if (ret != BT_STATUS_SUCCESS) {
1829                 BT_ERR("GATT client register failed: %s", status2string(ret));
1830                 return convert_to_oal_status(ret);
1831         }
1832         return OAL_STATUS_SUCCESS;
1833 }
1834
1835 oal_status_t gattc_deregister(int client_id)
1836 {
1837         int ret = OAL_STATUS_SUCCESS;
1838
1839         API_TRACE("GATT client deregister");
1840         CHECK_OAL_GATT_ENABLED();
1841         CHECK_CLIENT_REGISTRATION(client_id);
1842
1843         ret = gatt_api->client->unregister_client(client_id);
1844         if (ret != BT_STATUS_SUCCESS) {
1845                 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1846                 return convert_to_oal_status(ret);
1847         }
1848
1849         return OAL_STATUS_SUCCESS;
1850 }
1851
1852 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1853 {
1854         int ret = OAL_STATUS_SUCCESS;
1855         bdstr_t bdstr;
1856
1857         OAL_CHECK_PARAMETER(device_address, return);
1858         API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr));
1859         CHECK_OAL_GATT_ENABLED();
1860         CHECK_CLIENT_REGISTRATION(client_id);
1861
1862         /* Handle the actual connection in cb_gattc_connection callback */
1863         ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1864         if (ret != BT_STATUS_SUCCESS) {
1865                 BT_ERR("GATT client connect failed: %s", status2string(ret));
1866                 return convert_to_oal_status(ret);
1867         }
1868         return OAL_STATUS_SUCCESS;
1869 }
1870
1871 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1872 {
1873
1874         int ret = OAL_STATUS_SUCCESS;
1875         bdstr_t bdstr;
1876
1877         OAL_CHECK_PARAMETER(device_address, return);
1878         API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr));
1879         CHECK_OAL_GATT_ENABLED();
1880         CHECK_CLIENT_REGISTRATION(client_id);
1881         CHECK_CLIENT_CONNECTION(conn_id);
1882
1883         /* Handle actual disconnection in callback */
1884         ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1885         if (ret != BT_STATUS_SUCCESS) {
1886                 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1887                 return convert_to_oal_status(ret);
1888         }
1889         return OAL_STATUS_SUCCESS;
1890 }
1891
1892
1893 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1894 {
1895         int ret = OAL_STATUS_SUCCESS;
1896         char uuid_str[2*BT_UUID_STRING_MAX];
1897
1898         if (service_uuid) {
1899                 uuid_to_stringname(service_uuid, uuid_str);
1900                 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1901         } else
1902                 API_TRACE("Client Service Search All");
1903
1904         CHECK_OAL_GATT_ENABLED();
1905         CHECK_CLIENT_CONNECTION(conn_id);
1906         ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1907
1908         if (ret != BT_STATUS_SUCCESS) {
1909                 BT_ERR("GATT client service search failed: %s", status2string(ret));
1910                 return convert_to_oal_status(ret);
1911         }
1912         return OAL_STATUS_SUCCESS;
1913 }
1914
1915 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1916 {
1917         int ret = OAL_STATUS_SUCCESS;
1918         char uuid_str[2*BT_UUID_STRING_MAX];
1919
1920         OAL_CHECK_PARAMETER(srvc_id, return);
1921         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1922         API_TRACE("Client Get Characteristic [%s]", uuid_str);
1923         CHECK_OAL_GATT_ENABLED();
1924         CHECK_CLIENT_CONNECTION(conn_id);
1925         ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1926                                         (btgatt_gatt_id_t *)char_id);
1927         if (ret != BT_STATUS_SUCCESS) {
1928                 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1929                 return convert_to_oal_status(ret);
1930         }
1931         return OAL_STATUS_SUCCESS;
1932 }
1933
1934 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1935                                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1936 {
1937         int ret = OAL_STATUS_SUCCESS;
1938         char uuid_str1[2*BT_UUID_STRING_MAX];
1939         char uuid_str2[2*BT_UUID_STRING_MAX];
1940
1941         OAL_CHECK_PARAMETER(srvc_id, return);
1942         OAL_CHECK_PARAMETER(char_id, return);
1943         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1944         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1945         API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1946         CHECK_OAL_GATT_ENABLED();
1947         CHECK_CLIENT_CONNECTION(conn_id);
1948         ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1949                                 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1950         if (ret != BT_STATUS_SUCCESS) {
1951                 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1952                 return convert_to_oal_status(ret);
1953         }
1954         return OAL_STATUS_SUCCESS;
1955 }
1956
1957 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1958                         oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1959         int ret = OAL_STATUS_SUCCESS;
1960         char uuid_str1[2*BT_UUID_STRING_MAX];
1961         char uuid_str2[2*BT_UUID_STRING_MAX];
1962         bdstr_t bdstr;
1963
1964         OAL_CHECK_PARAMETER(address, return);
1965         OAL_CHECK_PARAMETER(srvc_id, return);
1966         OAL_CHECK_PARAMETER(char_id, return);
1967         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1968         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1969         API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1970         CHECK_OAL_GATT_ENABLED();
1971         CHECK_CLIENT_CONNECTION(conn_id);
1972
1973         ret = gatt_api->client->register_for_notification(conn_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1974         if (ret != BT_STATUS_SUCCESS) {
1975                 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1976                 return convert_to_oal_status(ret);
1977         }
1978
1979         return OAL_STATUS_SUCCESS;
1980 }
1981
1982 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1983                         oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1984         int ret = OAL_STATUS_SUCCESS;
1985         char uuid_str1[2*BT_UUID_STRING_MAX];
1986         char uuid_str2[2*BT_UUID_STRING_MAX];
1987         bdstr_t bdstr;
1988
1989         OAL_CHECK_PARAMETER(address, return);
1990         OAL_CHECK_PARAMETER(srvc_id, return);
1991         OAL_CHECK_PARAMETER(char_id, return);
1992         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1993         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1994         API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1995         CHECK_OAL_GATT_ENABLED();
1996         CHECK_CLIENT_CONNECTION(conn_id);
1997
1998         ret = gatt_api->client->deregister_for_notification(conn_id, (bt_bdaddr_t *)address, (btgatt_srvc_id_t *)srvc_id, (btgatt_gatt_id_t *)char_id);
1999         if (ret != BT_STATUS_SUCCESS) {
2000                 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
2001                 return convert_to_oal_status(ret);
2002         }
2003
2004         return OAL_STATUS_SUCCESS;
2005 }
2006
2007
2008 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2009                                 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
2010 {
2011         int ret = OAL_STATUS_SUCCESS;
2012         char uuid_str1[2*BT_UUID_STRING_MAX];
2013         char uuid_str2[2*BT_UUID_STRING_MAX];
2014
2015         OAL_CHECK_PARAMETER(srvc_id, return);
2016         OAL_CHECK_PARAMETER(char_id, return);
2017         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2018         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2019         API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2020         CHECK_OAL_GATT_ENABLED();
2021         CHECK_CLIENT_CONNECTION(conn_id);
2022
2023         ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2024                                         (btgatt_gatt_id_t *)char_id, auth_req);
2025         if (ret != BT_STATUS_SUCCESS) {
2026                 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
2027                 return convert_to_oal_status(ret);
2028         }
2029
2030         return OAL_STATUS_SUCCESS;
2031 }
2032
2033 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2034                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
2035 {
2036         int ret = OAL_STATUS_SUCCESS;
2037         char uuid_str1[2*BT_UUID_STRING_MAX];
2038         char uuid_str2[2*BT_UUID_STRING_MAX];
2039         char uuid_str3[2*BT_UUID_STRING_MAX];
2040
2041         OAL_CHECK_PARAMETER(srvc_id, return);
2042         OAL_CHECK_PARAMETER(char_id, return);
2043         OAL_CHECK_PARAMETER(desc_id, return);
2044         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2045         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2046         uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2047         API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2048                         uuid_str1, uuid_str2, uuid_str3);
2049         CHECK_OAL_GATT_ENABLED();
2050         CHECK_CLIENT_CONNECTION(conn_id);
2051
2052         ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2053                                 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2054         if (ret != BT_STATUS_SUCCESS) {
2055                 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2056                 return convert_to_oal_status(ret);
2057         }
2058
2059         return OAL_STATUS_SUCCESS;
2060 }
2061
2062 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2063                 oal_gatt_id_t *char_id,  int *fd, int *mtu)
2064 {
2065         int ret = OAL_STATUS_SUCCESS;
2066         char uuid_str1[2*BT_UUID_STRING_MAX];
2067         char uuid_str2[2*BT_UUID_STRING_MAX];
2068         OAL_CHECK_PARAMETER(srvc_id, return);
2069         OAL_CHECK_PARAMETER(char_id, return);
2070         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2071         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2072         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2073         CHECK_OAL_GATT_ENABLED();
2074         CHECK_CLIENT_CONNECTION(conn_id);
2075
2076         ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2077                         (btgatt_gatt_id_t *)char_id, fd, mtu);
2078         if (ret != BT_STATUS_SUCCESS) {
2079                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2080                 return convert_to_oal_status(ret);
2081         }
2082
2083         return OAL_STATUS_SUCCESS;
2084 }
2085
2086 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2087                 oal_gatt_id_t *char_id,  oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2088 {
2089         int ret = OAL_STATUS_SUCCESS;
2090         char uuid_str1[2*BT_UUID_STRING_MAX];
2091         char uuid_str2[2*BT_UUID_STRING_MAX];
2092         OAL_CHECK_PARAMETER(srvc_id, return);
2093         OAL_CHECK_PARAMETER(char_id, return);
2094         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2095         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2096         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2097         CHECK_OAL_GATT_ENABLED();
2098         CHECK_CLIENT_CONNECTION(conn_id);
2099
2100         ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2101                         (btgatt_gatt_id_t *)char_id,  auth_req, fd, mtu);
2102         if (ret != BT_STATUS_SUCCESS) {
2103                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2104                 return convert_to_oal_status(ret);
2105         }
2106
2107         return OAL_STATUS_SUCCESS;
2108 }
2109
2110 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2111                 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2112                 int len, oal_gatt_auth_req_t auth_req, char* data)
2113 {
2114         int ret = OAL_STATUS_SUCCESS;
2115         char uuid_str1[2*BT_UUID_STRING_MAX];
2116         char uuid_str2[2*BT_UUID_STRING_MAX];
2117         OAL_CHECK_PARAMETER(srvc_id, return);
2118         OAL_CHECK_PARAMETER(char_id, return);
2119         OAL_CHECK_PARAMETER(data, return);
2120         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2121         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2122         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2123         CHECK_OAL_GATT_ENABLED();
2124         CHECK_CLIENT_CONNECTION(conn_id);
2125
2126         ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2127                         (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2128         if (ret != BT_STATUS_SUCCESS) {
2129                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2130                 return convert_to_oal_status(ret);
2131         }
2132
2133         return OAL_STATUS_SUCCESS;
2134 }
2135
2136 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2137                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2138                 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2139 {
2140         int ret = OAL_STATUS_SUCCESS;
2141         char uuid_str1[2*BT_UUID_STRING_MAX];
2142         char uuid_str2[2*BT_UUID_STRING_MAX];
2143         char uuid_str3[2*BT_UUID_STRING_MAX];
2144         OAL_CHECK_PARAMETER(srvc_id, return);
2145         OAL_CHECK_PARAMETER(char_id, return);
2146         OAL_CHECK_PARAMETER(desc_id, return);
2147         OAL_CHECK_PARAMETER(data, return);
2148         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2149         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2150         uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2151         API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2152                                 uuid_str1, uuid_str2, uuid_str3);
2153         CHECK_OAL_GATT_ENABLED();
2154         CHECK_CLIENT_CONNECTION(conn_id);
2155
2156         ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2157                         (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2158         if (ret != BT_STATUS_SUCCESS) {
2159                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2160                 return convert_to_oal_status(ret);
2161         }
2162
2163         return OAL_STATUS_SUCCESS;
2164 }
2165
2166 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2167 {
2168         int ret;
2169         bdstr_t bdstr;
2170
2171         OAL_CHECK_PARAMETER(address, return);
2172         API_TRACE("Conn Param Update: [%s]", bdt_bd2str(address, &bdstr));
2173         CHECK_OAL_GATT_ENABLED();
2174
2175         BT_INFO("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2176         ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2177         if (ret != BT_STATUS_SUCCESS) {
2178                 BT_ERR("error: %s", status2string(ret));
2179                 return convert_to_oal_status(ret);
2180         }
2181         return OAL_STATUS_SUCCESS;
2182 }
2183
2184 oal_status_t gattc_unregister_scan_filter(int slot_id)
2185 {
2186         int ret;
2187         int client_if = 0;
2188
2189         API_TRACE("");
2190         CHECK_OAL_GATT_ENABLED();
2191
2192         BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2193
2194         ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2195         if (ret != BT_STATUS_SUCCESS) {
2196                 BT_ERR("error: %s", status2string(ret));
2197                 return convert_to_oal_status(ret);
2198         }
2199         return OAL_STATUS_SUCCESS;
2200 }
2201
2202 oal_status_t gattc_enable_scan_filter(int client_if)
2203 {
2204         int ret;
2205
2206         API_TRACE("");
2207         CHECK_OAL_GATT_ENABLED();
2208
2209         BT_INFO("Enable Scan filter. Client If: %d", client_if);
2210
2211         ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2212         if (ret != BT_STATUS_SUCCESS) {
2213                 BT_ERR("error: %s", status2string(ret));
2214                 return convert_to_oal_status(ret);
2215         }
2216         return OAL_STATUS_SUCCESS;
2217 }
2218
2219 oal_status_t gattc_disable_scan_filter(int client_if)
2220 {
2221         int ret;
2222
2223         API_TRACE("");
2224         CHECK_OAL_GATT_ENABLED();
2225
2226         BT_INFO("Disable Scan filter. Client If: %d", client_if);
2227
2228         ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2229         if (ret != BT_STATUS_SUCCESS) {
2230                 BT_ERR("error: %s", status2string(ret));
2231                 return convert_to_oal_status(ret);
2232         }
2233         return OAL_STATUS_SUCCESS;
2234 }
2235
2236 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2237 {
2238         int ret;
2239         int client_info = 0;
2240         int action = 0;
2241         int company_id = 0;
2242         int company_id_mask = 0;
2243         int address_type = 0;
2244         int feature_selection = 0;
2245
2246         oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2247                                                                 .filt_logic_type = 1,
2248                                                                 .rssi_high_thres = -127,
2249                                                                 .rssi_low_thres = -127,
2250                                                                 .dely_mode = 0,
2251                                                                 .found_timeout = 0,
2252                                                                 .lost_timeout = 0,
2253                                                                 .found_timeout_cnt = 0
2254                                                                 };
2255
2256         OAL_CHECK_PARAMETER(filter_data, return);
2257         API_TRACE();
2258         CHECK_OAL_GATT_ENABLED();
2259
2260         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2261                 bdstr_t bdstr;
2262                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2263                 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2264                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2265                                                                 action,
2266                                                                 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2267                                                                 filter_data->slot_id,
2268                                                                 company_id,
2269                                                                 company_id_mask,
2270                                                                 NULL,
2271                                                                 NULL,
2272                                                                 (bt_bdaddr_t*)filter_data->device_address,
2273                                                                 address_type,
2274                                                                 0,
2275                                                                 NULL,
2276                                                                 0,
2277                                                                 NULL
2278                                                                 );
2279                 if (ret != BT_STATUS_SUCCESS){
2280                         BT_ERR("error: %s", status2string(ret));
2281                         BT_INFO("unregistering already set filter features.");
2282                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2283                         return convert_to_oal_status(ret);
2284                 }
2285
2286                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2287         }
2288         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2289                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2290                                                                 action,
2291                                                                 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2292                                                                 filter_data->slot_id,
2293                                                                 company_id,
2294                                                                 company_id_mask,
2295                                                                 NULL,
2296                                                                 NULL,
2297                                                                 NULL,
2298                                                                 address_type,
2299                                                                 0,
2300                                                                 filter_data->device_name,     // device_name as p_data in HAL
2301                                                                 0,
2302                                                                 NULL
2303                                                                 );
2304                 if (ret != BT_STATUS_SUCCESS){
2305                         BT_ERR("error: %s", status2string(ret));
2306                         BT_INFO("unregistering already set filter features.");
2307                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2308                         return convert_to_oal_status(ret);
2309                 }
2310
2311                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2312         }
2313         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2314                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2315                 char uuid_str1[2*BT_UUID_STRING_MAX];
2316                 char uuid_str2[2*BT_UUID_STRING_MAX];
2317
2318                 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2319                 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2320
2321                 BT_INFO("Service UUID  is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2322                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2323                                                                 action,
2324                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2325                                                                 filter_data->slot_id,
2326                                                                 company_id,
2327                                                                 company_id_mask,
2328                                                                 (bt_uuid_t*)filter_data->service_uuid,
2329                                                                 (bt_uuid_t*)filter_data->service_uuid_mask,
2330                                                                 NULL,
2331                                                                 address_type,
2332                                                                 filter_data->service_uuid_len,   // service_uuid_len as data_len in HAL
2333                                                                 NULL,
2334                                                                 filter_data->service_uuid_mask_len,
2335                                                                 NULL
2336                                                                 );
2337                 if (ret != BT_STATUS_SUCCESS){
2338                         BT_ERR("error: %s", status2string(ret));
2339                         BT_INFO("unregistering already set filter features.");
2340                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2341                         return convert_to_oal_status(ret);
2342                 }
2343
2344                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2345         }
2346         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2347                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2348                 char uuid_str1[2*BT_UUID_STRING_MAX];
2349                 char uuid_str2[2*BT_UUID_STRING_MAX];
2350                 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2351                 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2352                 BT_INFO("Service Solicitation UUID  is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2353                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2354                                                                 action,
2355                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2356                                                                 filter_data->slot_id,
2357                                                                 company_id,
2358                                                                 company_id_mask,
2359                                                                 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2360                                                                 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2361                                                                 NULL,
2362                                                                 address_type,
2363                                                                 filter_data->service_solicitation_uuid_len,   // service_solicitation_uuid_len as data_len in HAL
2364                                                                 NULL,
2365                                                                 filter_data->service_solicitation_uuid_mask_len,
2366                                                                 NULL
2367                                                                 );
2368                 if (ret != BT_STATUS_SUCCESS){
2369                         BT_ERR("error: %s", status2string(ret));
2370                         BT_INFO("unregistering already set filter features.");
2371                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2372                         return convert_to_oal_status(ret);
2373                 }
2374
2375                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2376         }
2377         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2378                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2379                                                                 action,
2380                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2381                                                                 filter_data->slot_id,
2382                                                                 company_id,
2383                                                                 company_id_mask,
2384                                                                 NULL,
2385                                                                 NULL,
2386                                                                 NULL,
2387                                                                 address_type,
2388                                                                 filter_data->service_data_len,    //service_data_len as data_len in HAL
2389                                                                 (char*)filter_data->service_data,
2390                                                                 filter_data->service_data_mask_len,
2391                                                                 (char*)filter_data->service_data_mask
2392                                                                 );
2393                 if (ret != BT_STATUS_SUCCESS){
2394                         BT_ERR("error: %s", status2string(ret));
2395                         BT_INFO("unregistering already set filter features.");
2396                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2397                         return convert_to_oal_status(ret);
2398                 }
2399
2400                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2401         }
2402         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2403                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2404                                                                 action,
2405                                                                 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2406                                                                 filter_data->slot_id,
2407                                                                 filter_data->manufacturer_id,
2408                                                                 company_id_mask,
2409                                                                 NULL,
2410                                                                 NULL,
2411                                                                 NULL,
2412                                                                 address_type,
2413                                                                 filter_data->manufacturer_data_len,    //manufacturer_data_len as data_len in HAL
2414                                                                 (char*)filter_data->manufacturer_data,
2415                                                                 filter_data->manufacturer_data_mask_len,
2416                                                                 (char*)filter_data->manufacturer_data_mask
2417                                                                 );
2418                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2419                 if (ret != BT_STATUS_SUCCESS){
2420                         BT_ERR("error: %s", status2string(ret));
2421                         BT_INFO("unregistering already set filter features.");
2422                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2423                         return convert_to_oal_status(ret);
2424                 }
2425
2426                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2427         }
2428
2429         BT_DBG("Filter selection 0x%.2x", feature_selection);
2430
2431         ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2432                                                         scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2433                                                         scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2434                                                         scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2435                                                         scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2436         if (ret != BT_STATUS_SUCCESS){
2437                 BT_ERR("error: %s", status2string(ret));
2438                 return convert_to_oal_status(ret);
2439         }
2440         return OAL_STATUS_SUCCESS;
2441 }
2442
2443 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2444 {
2445         int ret;
2446
2447         API_TRACE("Configure MTU Size: [%d]", mtu);
2448         CHECK_OAL_GATT_ENABLED();
2449         CHECK_CLIENT_CONNECTION(conn_id);
2450
2451         /* To prevent crash in case other libraries not support this api */
2452         if (gatt_api->client->configure_mtu == NULL) {
2453                 BT_WARN("configure_mtu is NULL");
2454                 return OAL_STATUS_NOT_SUPPORT;
2455         }
2456
2457         ret = gatt_api->client->configure_mtu(conn_id, mtu);
2458         if (ret != BT_STATUS_SUCCESS) {
2459                 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2460                 return convert_to_oal_status(ret);
2461         }
2462         return OAL_STATUS_SUCCESS;
2463 }
2464
2465 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int server_inst_id)
2466 {
2467         int ret = OAL_STATUS_SUCCESS;
2468         bdstr_t bdstr;
2469
2470         OAL_CHECK_PARAMETER(device_address, return);
2471         API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr));
2472         CHECK_OAL_GATT_ENABLED();
2473         CHECK_SERVER_INSTANCE(server_inst_id);
2474         CHECK_SERVER_REGISTRATION(server_inst_id);
2475
2476         ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, server_inst_id);
2477         if (ret != BT_STATUS_SUCCESS) {
2478                 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2479                 return convert_to_oal_status(ret);
2480         }
2481         return OAL_STATUS_SUCCESS;
2482 }
2483