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