Fix the coverity issues
[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);
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)
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
1288         send_event(OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY, event, sizeof(event_gatts_srvc_acquire_attr_t));
1289
1290 }
1291
1292 static void cb_gatts_response_confirmation(int status, int handle)
1293 {
1294         BT_INFO("BTGATT SERVER RESPONSE CONFIRMATION CB, status:%d, handle:%d", status, handle);
1295
1296         event_gatts_respons_t* event = g_new0(event_gatts_respons_t, 1);
1297
1298         event->hndl = handle;
1299         event->status = convert_to_oal_status(status);
1300
1301         send_event(OAL_EVENT_GATTS_RESPONSE_CONFIRMED, event, sizeof(event_gatts_respons_t));
1302 }
1303
1304 static void cb_indication_confirmation(int conn_id, int trans_id, int attr_handle, bt_bdaddr_t *bda)
1305 {
1306         BT_INFO("BTGATT SERVER Indication Confirmation CB, conn_id:%d, trans_id:%d attr_handle:%d",
1307                         conn_id, trans_id, attr_handle);
1308
1309         event_gatts_ind_cnfrm_t* event = g_new0(event_gatts_ind_cnfrm_t, 1);
1310
1311         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1312
1313         event->attr_handle = attr_handle;
1314         event->conn_id = conn_id;
1315         event->trans_id = trans_id;
1316         send_event(OAL_EVENT_GATTS_IND_CONFIRM, event, sizeof(event_gatts_ind_cnfrm_t));
1317 }
1318
1319 static void cb_gatts_mtu_changed(int conn_id, int mtu)
1320 {
1321         BT_INFO("BTGATT SERVER MTU CHANGED CB, conn_id: %d, mtu_size: %d", conn_id, mtu);
1322         event_gatts_mtu_changed_t *event = g_new0(event_gatts_mtu_changed_t, 1);
1323         event->conn_id = conn_id;
1324         event->mtu_size = mtu;
1325         send_event(OAL_EVENT_GATTS_MTU_CHANGED, event, sizeof(event_gatts_mtu_changed_t));
1326 }
1327
1328 #ifdef TIZEN_BT_HAL
1329 static void cb_notifcation_changed(int conn_id, int trans_id, int attr_handle, bool notify,  bt_bdaddr_t *bda)
1330 {
1331         BT_INFO("BTGATT SERVER Notication CB, conn_id:%d, trans_id:%d attr_handle:%d Notify:%d",
1332                         conn_id, trans_id, attr_handle, notify);
1333
1334         event_gatts_notif_t* event = g_new0(event_gatts_notif_t, 1);
1335
1336         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1337
1338         event->attr_handle = attr_handle;
1339         event->conn_id = conn_id;
1340         event->trans_id = trans_id;
1341         event->notify = notify;
1342         send_event(OAL_EVENT_GATTS_NOTIFICATION, event, sizeof(event_gatts_notif_t));
1343 }
1344 #endif
1345
1346
1347 /* GATT Server Callbacks: End */
1348 /************************************ GATT Client ***********************************/
1349 /* Client Callbacks */
1350 static void cb_gattc_register_app(int status, int clientIf, bt_uuid_t *app_uuid)
1351 {
1352         char uuid_str[BT_UUID_STRING_MAX];
1353         event_gattc_register_t *event;
1354
1355         /* Check if GATT client registered for Default GATT client UUID */
1356         uuid_to_string((service_uuid_t *)app_uuid, uuid_str);
1357
1358         BT_INFO("GATT CLIENT REGISTER APP CB, status:%d, clientIf:%d, %s", status, clientIf, uuid_str);
1359         event = g_new0(event_gattc_register_t, 1);
1360         event->client_if = clientIf;
1361         event->status = (status == 0 ? OAL_STATUS_SUCCESS : OAL_STATUS_INTERNAL_ERROR);
1362         memcpy(event->client_uuid.uuid, app_uuid->uu, sizeof(oal_uuid_t));
1363         send_event(OAL_EVENT_GATTC_REGISTRATION, event, sizeof(event_gattc_register_t));
1364 }
1365
1366 static void cb_gattc_scan_result(bt_bdaddr_t* bdaddress, uint8_t addr_type, int rssi,
1367                 uint8_t *adv_data, int adv_data_len, uint8_t *scan_rsp_data, int scan_rsp_data_len)
1368 {
1369         event_ble_scan_result_info *event;
1370
1371         event = g_new0(event_ble_scan_result_info, 1);
1372         event->addr_type = addr_type;
1373         event->rssi = rssi;
1374         memcpy(event->address.addr, bdaddress->address, BT_ADDRESS_BYTES_NUM);
1375         memcpy(event->adv_data, adv_data, BLE_ADV_DATA_LENGTH);
1376         event->adv_data_len = adv_data_len;
1377         memcpy(event->scan_rsp_data, scan_rsp_data, BLE_ADV_DATA_LENGTH);
1378         event->scan_rsp_data_len = scan_rsp_data_len;
1379         send_event_bda_trace(OAL_EVENT_BLE_REMOTE_DEVICE_FOUND, event,
1380                         sizeof(event_ble_scan_result_info), (bt_address_t *)bdaddress);
1381 }
1382
1383 static void cb_gattc_connection(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1384 {
1385         event_gattc_conn_t *event;
1386         BT_INFO("BTGATT CLIENT CONNECTED CB, status:[%d] client_if[%d] id[%d]",
1387                                                         status, client_if, conn_id);
1388
1389         event = g_new0(event_gattc_conn_t, 1);
1390         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1391         event->client_if = client_if;
1392
1393         if (BT_STATUS_SUCCESS != status) {
1394                 event->status = OAL_STATUS_INTERNAL_ERROR;
1395                 BT_ERR("gattc connection Error: %d", status);
1396         } else {
1397                 event->conn_id = conn_id;
1398                 event->status = OAL_STATUS_SUCCESS;
1399         }
1400
1401         send_event_bda_trace(OAL_EVENT_GATTC_CONNECTION_COMPLETED, event,
1402                                         sizeof(*event), (bt_address_t *)bda);
1403 }
1404
1405
1406 static void cb_gattc_disconnect(int conn_id, int status, int client_if, bt_bdaddr_t* bda)
1407 {
1408         event_gattc_conn_t *event;
1409         BT_INFO("BTGATT Client Disconnect Complete cb, status:[%d], client_if[%d] id[%d]",
1410                                                         status, client_if, conn_id);
1411         event = g_new0(event_gattc_conn_t, 1);
1412         event->status = convert_to_oal_status(status);
1413         event->client_if = client_if;
1414         event->conn_id = conn_id;
1415         memcpy(event->address.addr, bda->address, BT_ADDRESS_BYTES_NUM);
1416         send_event_bda_trace(OAL_EVENT_GATTC_DISCONNECTION_COMPLETED, event,
1417                                         sizeof(*event), (bt_address_t *)bda);
1418 }
1419
1420 static void cb_gattc_search_result(int conn_id, btgatt_srvc_id_t *srvc_id)
1421 {
1422         char uuid_str[2*BT_UUID_STRING_MAX];
1423
1424         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str);
1425         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1426                 uuid_str, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1427
1428         event_gattc_service_result_t *event = g_new0(event_gattc_service_result_t, 1);
1429         event->conn_status.status = OAL_STATUS_SUCCESS;
1430         event->conn_status.conn_id = conn_id;
1431         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1432
1433         send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT, event, sizeof(*event));
1434 }
1435
1436 static void cb_gattc_search_complete(int conn_id, int status)
1437 {
1438         BT_INFO("BTGATT Client Service Search Complete cb, status:%d, conn_id:%d",
1439         status, conn_id);
1440
1441         event_gattc_conn_status_t *event = g_new0(event_gattc_conn_status_t, 1);
1442         event->conn_id = conn_id;
1443         event->status = convert_to_oal_status(status);
1444
1445         send_event(OAL_EVENT_GATTC_SERVICE_SEARCH_DONE, event, sizeof(*event));
1446 }
1447
1448
1449 static void cb_gattc_get_characteristics(int conn_id, int status, btgatt_srvc_id_t *srvc_id,
1450                                                 btgatt_gatt_id_t *char_id, int char_prop)
1451 {
1452         char uuid_str1[2*BT_UUID_STRING_MAX];
1453         char uuid_str2[2*BT_UUID_STRING_MAX];
1454         char str[50];
1455
1456         BT_DBG("BTGATT Client Get Characteristic Callback, conn_id:%d, status:%d", conn_id, status);
1457         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1458         BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1459                 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1460
1461         event_gattc_characteristic_result_t *event = g_new0(event_gattc_characteristic_result_t, 1);
1462         event->conn_status.conn_id = conn_id;
1463         event->conn_status.status = convert_to_oal_status(status);
1464         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1465
1466         if (status == 0) {
1467                 uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1468                 BT_INFO("Charac => [%s], Inst_id [%u], Prop [%s]",
1469                         uuid_str2, char_id->inst_id, char_prop_to_string(char_prop, str));
1470                 event->char_prop = char_prop;
1471                 memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1472         }
1473         send_event(OAL_EVENT_GATTC_CHARAC_SERACH_RESULT, event, sizeof(*event));
1474 }
1475
1476
1477
1478 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)
1479 {
1480         char uuid_str1[2*BT_UUID_STRING_MAX];
1481         char uuid_str2[2*BT_UUID_STRING_MAX];
1482         char uuid_str3[2*BT_UUID_STRING_MAX];
1483         BT_DBG("BTGATT Client Get Descriptor Callback, conn_id:%d, status:%d", conn_id, status);
1484         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1485         uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1486         BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]",
1487                 uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1488         BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1489
1490         event_gattc_descriptor_result_t *event = g_new0(event_gattc_descriptor_result_t, 1);
1491         event->conn_status.conn_id = conn_id;
1492         event->conn_status.status = convert_to_oal_status(status);
1493         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1494         memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1495
1496         if (status == 0) {
1497                 uuid_to_stringname((oal_uuid_t *)&(descr_id->uuid), uuid_str3);
1498                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, descr_id->inst_id);
1499                 memcpy(&(event->descr_id), descr_id, sizeof(oal_gatt_id_t));
1500         }
1501         send_event(OAL_EVENT_GATTC_DESC_SERACH_RESULT, event, sizeof(*event));
1502 }
1503
1504 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)
1505 {
1506         char uuid_str1[2*BT_UUID_STRING_MAX];
1507         char uuid_str2[2*BT_UUID_STRING_MAX];
1508         BT_INFO("BTGATT Client Register For Notification Callback, conn_id:%d, status:%d, registered: %s",
1509                 conn_id, status, registered == 1 ? "REGISTERED" : "DEREGISTERED");
1510         uuid_to_stringname((oal_uuid_t *)&(srvc_id->id.uuid), uuid_str1);
1511         uuid_to_stringname((oal_uuid_t *)&(char_id->uuid), uuid_str2);
1512         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1, srvc_id->id.inst_id, srvc_id->is_primary ? "Primary" : "Secondary");
1513         BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, char_id->inst_id);
1514
1515         event_gattc_regdereg_notify_t *event = g_new0(event_gattc_regdereg_notify_t, 1);
1516         oal_event_t event_type = (registered == 1 ? OAL_EVENT_GATTC_NOTIFICATION_REGISTERED : OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED);
1517         event->conn_id = conn_id;
1518         event->status = convert_to_oal_status(status);
1519         memcpy(&(event->srvc_id), srvc_id, sizeof(oal_gatt_srvc_id_t));
1520         memcpy(&(event->char_id), char_id, sizeof(oal_gatt_id_t));
1521
1522         send_event(event_type, event, sizeof(*event));
1523 }
1524
1525 static void cb_gattc_notify(int conn_id, btgatt_notify_params_t *p_data)
1526 {
1527         bdstr_t bdstr;
1528         char uuid_str1[2*BT_UUID_STRING_MAX];
1529         char uuid_str2[2*BT_UUID_STRING_MAX];
1530
1531         BT_INFO("BTGATT Client Notify Callback, conn_id:%d", conn_id);
1532         BT_INFO("Server Address:[%s], is_notify:[%d], len:[%d]",
1533                 bdt_bd2str((bt_address_t *)&(p_data->bda), &bdstr), p_data->is_notify, p_data->len);
1534
1535         uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1536         uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1537
1538         BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]",
1539                 uuid_str1, p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1540         BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1541
1542         if (p_data->len > 0) {
1543                 char *data = NULL;
1544                 data = g_malloc(3*p_data->len+1);
1545                 if (!data) {
1546                         BT_ERR("memory allocation failed");
1547                         return;
1548                 }
1549
1550                 convert_hex_2_str((unsigned char *)p_data->value, p_data->len, data);
1551                 BT_INFO("Notified Data: [%s]", data);
1552
1553                 event_gattc_notify_data *event = g_new0(event_gattc_notify_data, 1);
1554                 memcpy(event->address.addr, p_data->bda.address, BT_ADDRESS_BYTES_NUM);
1555                 event->is_notify = p_data->is_notify;
1556                 event->data_len = p_data->len;
1557                 memcpy(event->data, p_data->value, event->data_len);
1558                 memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1559                 memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1560
1561                 send_event_bda_trace(OAL_EVENT_GATTC_NOTIFY_DATA, event, sizeof(*event), (bt_address_t *)&p_data->bda);
1562                 g_free(data);
1563         }
1564 }
1565
1566 static void cb_gattc_service_changed(bt_bdaddr_t *bd_addr, int change_type, uint8_t *uuid, int conn_id, int inst_id)
1567 {
1568         event_gattc_service_changed_data *event = g_new0(event_gattc_service_changed_data, 1);
1569
1570         memcpy(event->address.addr, bd_addr->address, 6);
1571         event->change_type = change_type;
1572         memcpy(event->uuid.uuid, uuid, sizeof(event->uuid.uuid));
1573         event->conn_id = conn_id;
1574         event->inst_id = inst_id;
1575
1576         send_event_bda_trace(OAL_EVENT_GATTC_SERVICE_CHANGED_IND, event, sizeof(*event), (bt_address_t *)bd_addr);
1577 }
1578
1579 static void cb_gattc_read_characteristic(int conn_id, int status, btgatt_read_params_t *p_data)
1580 {
1581         char uuid_str1[2*BT_UUID_STRING_MAX];
1582         char uuid_str2[2*BT_UUID_STRING_MAX];
1583         BT_DBG("BTGATT Client Read Charcateristic Callback, conn_id:%d, read_status:%d", conn_id, status);
1584
1585         event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1586         event->uuid_status.conn_status.conn_id = conn_id;
1587         event->uuid_status.conn_status.status = convert_to_oal_status(status);
1588         event->value_type = p_data->value_type;
1589         memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1590         memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1591         if (status == 0) {
1592                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1593                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1594                 BT_DBG("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1595                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1596                 BT_DBG("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1597                 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1598                 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1599                         event->data_len = p_data->value.len;
1600                         memcpy(&(event->data), &(p_data->value.value), event->data_len);
1601                 }
1602         }
1603         send_event(OAL_EVENT_GATTC_READ_CHARAC, event, sizeof(*event));
1604 }
1605
1606 static void cb_gattc_write_characteristic(int conn_id, int status, btgatt_write_params_t *p_data)
1607 {
1608         char uuid_str1[2*BT_UUID_STRING_MAX];
1609         char uuid_str2[2*BT_UUID_STRING_MAX];
1610         BT_INFO("BTGATT Client Write Charcateristic Callback, conn_id:%d, write_status:%d", conn_id, status);
1611
1612         event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1613         event->conn_status.conn_id = conn_id;
1614         event->conn_status.status = convert_to_oal_status(status);
1615         memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1616         memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1617         if (status == 0) {
1618                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1619                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1620                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1621                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1622                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1623         }
1624         send_event(OAL_EVENT_GATTC_WRITE_CHARAC, event, sizeof(*event));
1625 }
1626
1627 static void cb_gattc_write_descriptor(int conn_id, int status, btgatt_write_params_t *p_data)
1628 {
1629         char uuid_str1[2*BT_UUID_STRING_MAX];
1630         char uuid_str2[2*BT_UUID_STRING_MAX];
1631         char uuid_str3[2*BT_UUID_STRING_MAX];
1632         BT_DBG("BTGATT Client Write Descriptor Callback, conn_id:%d, write_status:%d", conn_id, status);
1633
1634         event_gattc_write_data *event = g_new0(event_gattc_write_data, 1);
1635         event->conn_status.conn_id = conn_id;
1636         event->conn_status.status = convert_to_oal_status(status);
1637         memcpy(&(event->srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1638         memcpy(&(event->char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1639         memcpy(&(event->descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1640         if (status == 0) {
1641                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1642                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1643                 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1644                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1645                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1646                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1647                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1648         }
1649         send_event(OAL_EVENT_GATTC_WRITE_DESCR, event, sizeof(*event));
1650 }
1651
1652
1653
1654 static void cb_gattc_read_descriptor(int conn_id, int status, btgatt_read_params_t *p_data)
1655 {
1656         char uuid_str1[2*BT_UUID_STRING_MAX];
1657         char uuid_str2[2*BT_UUID_STRING_MAX];
1658         char uuid_str3[2*BT_UUID_STRING_MAX];
1659         BT_INFO("BTGATT Client Read Descriptor Callback, conn_id:%d, read_status:%d", conn_id, status);
1660
1661         event_gattc_read_data *event = g_new0(event_gattc_read_data, 1);
1662         event->uuid_status.conn_status.conn_id = conn_id;
1663         event->uuid_status.conn_status.status = convert_to_oal_status(status);
1664         event->value_type = p_data->value_type;
1665         memcpy(&(event->uuid_status.srvc_id), &(p_data->srvc_id), sizeof(oal_gatt_srvc_id_t));
1666         memcpy(&(event->uuid_status.char_id), &(p_data->char_id), sizeof(oal_gatt_id_t));
1667         memcpy(&(event->uuid_status.descr_id), &(p_data->descr_id), sizeof(oal_gatt_id_t));
1668
1669         if (status == 0) {
1670                 uuid_to_stringname((oal_uuid_t *)&(p_data->srvc_id.id.uuid), uuid_str1);
1671                 uuid_to_stringname((oal_uuid_t *)&(p_data->char_id.uuid), uuid_str2);
1672                 uuid_to_stringname((oal_uuid_t *)&(p_data->descr_id.uuid), uuid_str3);
1673                 BT_INFO("Service=> [%s], Inst_id [%u], Type [%s]", uuid_str1,
1674                         p_data->srvc_id.id.inst_id, p_data->srvc_id.is_primary ? "Primary" : "Secondary");
1675                 BT_INFO("Charac => [%s], Inst_id [%u]", uuid_str2, p_data->char_id.inst_id);
1676                 BT_INFO("Desc   => [%s], Inst_id [%u]", uuid_str3, p_data->descr_id.inst_id);
1677                 BT_DBG("Len: %u, value_type: %u", p_data->value.len, p_data->value_type);
1678                 if (p_data->value.len > 0 && event->value_type == GATTC_READ_VALUE_TYPE_VALUE) {
1679                         char *data = NULL;
1680                         data = g_malloc(3*p_data->value.len+1);
1681                         if (!data) {
1682                                 BT_ERR("memory allocation failed");
1683                                 g_free(event);
1684                                 return;
1685                         }
1686                         convert_hex_2_str((unsigned char *)p_data->value.value, p_data->value.len, data);
1687                         BT_DBG("Read Data: [%s]", data);
1688                         event->data_len = p_data->value.len;
1689                         memcpy(&(event->data), &(p_data->value.value), event->data_len);
1690                         g_free(data);
1691                 }
1692         }
1693         send_event(OAL_EVENT_GATTC_READ_DESCR, event, sizeof(*event));
1694 }
1695
1696 oal_status_t gattc_start_le_discovery(int client_id)
1697 {
1698
1699         int ret = OAL_STATUS_SUCCESS;
1700
1701         API_TRACE("BTGATT CLIENT SCAN START");
1702         CHECK_OAL_GATT_ENABLED();
1703         CHECK_CLIENT_REGISTRATION(client_id);
1704
1705         ret = gatt_api->client->scan(client_id, 1);
1706         if (ret != BT_STATUS_SUCCESS) {
1707                 BT_ERR("Error:Start LE Discovery failed: %s", status2string(ret));
1708                 return convert_to_oal_status(ret);
1709         }
1710         return OAL_STATUS_SUCCESS;
1711 }
1712
1713 oal_status_t gattc_stop_le_discovery(int client_id)
1714 {
1715
1716         int ret = OAL_STATUS_SUCCESS;
1717
1718         API_TRACE("BTGATT CLIENT SCAN STOP");
1719         CHECK_OAL_GATT_ENABLED();
1720         CHECK_CLIENT_REGISTRATION(client_id);
1721         ret = gatt_api->client->scan(client_id, 0);
1722         if (ret != BT_STATUS_SUCCESS) {
1723                 BT_ERR("Error:Stop LE Discovery failed: %s", status2string(ret));
1724                 return convert_to_oal_status(ret);
1725         }
1726         return OAL_STATUS_SUCCESS;
1727 }
1728
1729 oal_status_t gattc_set_le_scan_param(int scan_type, int itv, int win)
1730 {
1731         int ret;
1732
1733         API_TRACE("GATT client set le scan param");
1734         CHECK_OAL_GATT_ENABLED();
1735
1736 #ifdef TIZEN_BT_HAL
1737         ret = gatt_api->client->set_scan_parameters(scan_type, itv, win);
1738         if (ret != BT_STATUS_SUCCESS) {
1739                 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1740                 return convert_to_oal_status(ret);
1741         }
1742 #else
1743         ret = gatt_api->client->set_scan_parameters(itv, win);
1744         if (ret != BT_STATUS_SUCCESS) {
1745                 BT_ERR("Error:Set scan parameters failed: %s", status2string(ret));
1746                 return convert_to_oal_status(ret);
1747         }
1748 #endif
1749
1750         return OAL_STATUS_SUCCESS;
1751 }
1752
1753 static void cb_gattc_configure_mtu_cmpl(int conn_id, int status, int mtu)
1754 {
1755         event_gattc_mtu_configured_t *event;
1756         BT_INFO("BTGATT Client configure mtu complete Callback, conn_id: %d, status: %d, mtu: %d",
1757                         conn_id, status, mtu);
1758         event = g_new0(event_gattc_mtu_configured_t, 1);
1759         event->conn_id = conn_id;
1760         event->mtu = mtu;
1761         event->status = convert_to_oal_status(status);
1762         send_event(OAL_EVENT_GATTC_MTU_EXCHANGE_COMPLETED, event, sizeof(*event));
1763 }
1764
1765 /************************************ GATT Client ***********************************/
1766 oal_status_t gattc_register(oal_uuid_t* client_uuid)
1767 {
1768
1769         char str[2*BT_UUID_STRING_MAX];
1770         int ret = OAL_STATUS_SUCCESS;
1771
1772         CHECK_OAL_GATT_ENABLED();
1773         uuid_to_stringname(client_uuid, str);
1774         API_TRACE("uuid: [%s]", str);
1775         /* We will perform actual registration in cb_gattc_register_app callback */
1776         ret = gatt_api->client->register_client((bt_uuid_t *)client_uuid);
1777         if (ret != BT_STATUS_SUCCESS) {
1778                 BT_ERR("GATT client register failed: %s", status2string(ret));
1779                 return convert_to_oal_status(ret);
1780         }
1781         return OAL_STATUS_SUCCESS;
1782 }
1783
1784 oal_status_t gattc_deregister(int client_id)
1785 {
1786         int ret = OAL_STATUS_SUCCESS;
1787
1788         API_TRACE("GATT client deregister");
1789         CHECK_OAL_GATT_ENABLED();
1790         CHECK_CLIENT_REGISTRATION(client_id);
1791
1792         ret = gatt_api->client->unregister_client(client_id);
1793         if (ret != BT_STATUS_SUCCESS) {
1794                 BT_ERR("GATT client deregister failed: %s", status2string(ret));
1795                 return convert_to_oal_status(ret);
1796         }
1797
1798         return OAL_STATUS_SUCCESS;
1799 }
1800
1801 oal_status_t gattc_connect(int client_id, bt_address_t *device_address, int isDirect)
1802 {
1803         int ret = OAL_STATUS_SUCCESS;
1804         bdstr_t bdstr;
1805
1806         OAL_CHECK_PARAMETER(device_address, return);
1807         API_TRACE("Client Connect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1808         CHECK_OAL_GATT_ENABLED();
1809         CHECK_CLIENT_REGISTRATION(client_id);
1810
1811         /* Handle the actual connection in cb_gattc_connection callback */
1812         ret = gatt_api->client->connect(client_id, (bt_bdaddr_t *)device_address, isDirect);
1813         if (ret != BT_STATUS_SUCCESS) {
1814                 BT_ERR("GATT client connect failed: %s", status2string(ret));
1815                 return convert_to_oal_status(ret);
1816         }
1817         return OAL_STATUS_SUCCESS;
1818 }
1819
1820 oal_status_t gattc_disconnect(int client_id, bt_address_t *device_address, int conn_id)
1821 {
1822
1823         int ret = OAL_STATUS_SUCCESS;
1824         bdstr_t bdstr;
1825
1826         OAL_CHECK_PARAMETER(device_address, return);
1827         API_TRACE("Client Disconnect: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
1828         CHECK_OAL_GATT_ENABLED();
1829         CHECK_CLIENT_REGISTRATION(client_id);
1830         CHECK_CLIENT_CONNECTION(conn_id);
1831
1832         /* Handle actual disconnection in callback */
1833         ret = gatt_api->client->disconnect(client_id, (bt_bdaddr_t *) device_address, conn_id);
1834         if (ret != BT_STATUS_SUCCESS) {
1835                 BT_ERR("GATT client disconnect failed: %s", status2string(ret));
1836                 return convert_to_oal_status(ret);
1837         }
1838         return OAL_STATUS_SUCCESS;
1839 }
1840
1841
1842 oal_status_t gattc_search_service(int conn_id, oal_uuid_t *service_uuid)
1843 {
1844         int ret = OAL_STATUS_SUCCESS;
1845         char uuid_str[2*BT_UUID_STRING_MAX];
1846
1847         if (service_uuid) {
1848                 uuid_to_stringname(service_uuid, uuid_str);
1849                 API_TRACE("Client Service Search UUID: [%s]", uuid_str);
1850         } else
1851                 API_TRACE("Client Service Search All");
1852
1853         CHECK_OAL_GATT_ENABLED();
1854         CHECK_CLIENT_CONNECTION(conn_id);
1855         ret = gatt_api->client->search_service(conn_id, (bt_uuid_t *)service_uuid);
1856
1857         if (ret != BT_STATUS_SUCCESS) {
1858                 BT_ERR("GATT client service search failed: %s", status2string(ret));
1859                 return convert_to_oal_status(ret);
1860         }
1861         return OAL_STATUS_SUCCESS;
1862 }
1863
1864 oal_status_t gattc_get_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id)
1865 {
1866         int ret = OAL_STATUS_SUCCESS;
1867         char uuid_str[2*BT_UUID_STRING_MAX];
1868
1869         OAL_CHECK_PARAMETER(srvc_id, return);
1870         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str);
1871         API_TRACE("Client Get Characteristic [%s]", uuid_str);
1872         CHECK_OAL_GATT_ENABLED();
1873         CHECK_CLIENT_CONNECTION(conn_id);
1874         ret = gatt_api->client->get_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1875                                         (btgatt_gatt_id_t *)char_id);
1876         if (ret != BT_STATUS_SUCCESS) {
1877                 BT_ERR("GATT client get characteristic failed: %s", status2string(ret));
1878                 return convert_to_oal_status(ret);
1879         }
1880         return OAL_STATUS_SUCCESS;
1881 }
1882
1883 oal_status_t gattc_get_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1884                                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id)
1885 {
1886         int ret = OAL_STATUS_SUCCESS;
1887         char uuid_str1[2*BT_UUID_STRING_MAX];
1888         char uuid_str2[2*BT_UUID_STRING_MAX];
1889
1890         OAL_CHECK_PARAMETER(srvc_id, return);
1891         OAL_CHECK_PARAMETER(char_id, return);
1892         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1893         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1894         API_TRACE("Client Get Descriptor, Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1895         CHECK_OAL_GATT_ENABLED();
1896         CHECK_CLIENT_CONNECTION(conn_id);
1897         ret = gatt_api->client->get_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
1898                                 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id);
1899         if (ret != BT_STATUS_SUCCESS) {
1900                 BT_ERR("GATT client get descriptor failed: %s", status2string(ret));
1901                 return convert_to_oal_status(ret);
1902         }
1903         return OAL_STATUS_SUCCESS;
1904 }
1905
1906 oal_status_t gattc_register_for_notification(int conn_id, bt_address_t * address,
1907                         oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1908         int ret = OAL_STATUS_SUCCESS;
1909         char uuid_str1[2*BT_UUID_STRING_MAX];
1910         char uuid_str2[2*BT_UUID_STRING_MAX];
1911         bdstr_t bdstr;
1912
1913         OAL_CHECK_PARAMETER(address, return);
1914         OAL_CHECK_PARAMETER(srvc_id, return);
1915         OAL_CHECK_PARAMETER(char_id, return);
1916         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1917         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1918         API_TRACE("Client Register Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1919         CHECK_OAL_GATT_ENABLED();
1920         CHECK_CLIENT_CONNECTION(conn_id);
1921
1922         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);
1923         if (ret != BT_STATUS_SUCCESS) {
1924                 BT_ERR("GATT client register notification failed: %s", status2string(ret));
1925                 return convert_to_oal_status(ret);
1926         }
1927
1928         return OAL_STATUS_SUCCESS;
1929 }
1930
1931 oal_status_t gattc_deregister_for_notification(int conn_id, bt_address_t * address,
1932                         oal_gatt_srvc_id_t *srvc_id, oal_gatt_id_t *char_id) {
1933         int ret = OAL_STATUS_SUCCESS;
1934         char uuid_str1[2*BT_UUID_STRING_MAX];
1935         char uuid_str2[2*BT_UUID_STRING_MAX];
1936         bdstr_t bdstr;
1937
1938         OAL_CHECK_PARAMETER(address, return);
1939         OAL_CHECK_PARAMETER(srvc_id, return);
1940         OAL_CHECK_PARAMETER(char_id, return);
1941         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1942         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1943         API_TRACE("Client Deregister Notification: [%s], Service_uuid: [%s], Char_uuid: [%s]", bdt_bd2str(address, &bdstr), uuid_str1, uuid_str2);
1944         CHECK_OAL_GATT_ENABLED();
1945         CHECK_CLIENT_CONNECTION(conn_id);
1946
1947         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);
1948         if (ret != BT_STATUS_SUCCESS) {
1949                 BT_ERR("GATT client deregister notification failed: %s", status2string(ret));
1950                 return convert_to_oal_status(ret);
1951         }
1952
1953         return OAL_STATUS_SUCCESS;
1954 }
1955
1956
1957 oal_status_t gattc_read_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1958                                 oal_gatt_id_t *char_id, oal_gatt_auth_req_t auth_req)
1959 {
1960         int ret = OAL_STATUS_SUCCESS;
1961         char uuid_str1[2*BT_UUID_STRING_MAX];
1962         char uuid_str2[2*BT_UUID_STRING_MAX];
1963
1964         OAL_CHECK_PARAMETER(srvc_id, return);
1965         OAL_CHECK_PARAMETER(char_id, return);
1966         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1967         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1968         API_TRACE("Client Read Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
1969         CHECK_OAL_GATT_ENABLED();
1970         CHECK_CLIENT_CONNECTION(conn_id);
1971
1972         ret = gatt_api->client->read_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
1973                                         (btgatt_gatt_id_t *)char_id, auth_req);
1974         if (ret != BT_STATUS_SUCCESS) {
1975                 BT_ERR("GATT client read characteristic failed: %s", status2string(ret));
1976                 return convert_to_oal_status(ret);
1977         }
1978
1979         return OAL_STATUS_SUCCESS;
1980 }
1981
1982 oal_status_t gattc_read_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
1983                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id, oal_gatt_auth_req_t auth_req)
1984 {
1985         int ret = OAL_STATUS_SUCCESS;
1986         char uuid_str1[2*BT_UUID_STRING_MAX];
1987         char uuid_str2[2*BT_UUID_STRING_MAX];
1988         char uuid_str3[2*BT_UUID_STRING_MAX];
1989
1990         OAL_CHECK_PARAMETER(srvc_id, return);
1991         OAL_CHECK_PARAMETER(char_id, return);
1992         OAL_CHECK_PARAMETER(desc_id, return);
1993         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
1994         uuid_to_stringname(&(char_id->uuid), uuid_str2);
1995         uuid_to_stringname(&(desc_id->uuid), uuid_str3);
1996         API_TRACE("Client Read Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
1997                         uuid_str1, uuid_str2, uuid_str3);
1998         CHECK_OAL_GATT_ENABLED();
1999         CHECK_CLIENT_CONNECTION(conn_id);
2000
2001         ret = gatt_api->client->read_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2002                                 (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, auth_req);
2003         if (ret != BT_STATUS_SUCCESS) {
2004                 BT_ERR("GATT client read descriptor failed: %s", status2string(ret));
2005                 return convert_to_oal_status(ret);
2006         }
2007
2008         return OAL_STATUS_SUCCESS;
2009 }
2010
2011 oal_status_t gattc_acquire_notify(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2012                 oal_gatt_id_t *char_id,  int *fd, int *mtu)
2013 {
2014         int ret = OAL_STATUS_SUCCESS;
2015         char uuid_str1[2*BT_UUID_STRING_MAX];
2016         char uuid_str2[2*BT_UUID_STRING_MAX];
2017         OAL_CHECK_PARAMETER(srvc_id, return);
2018         OAL_CHECK_PARAMETER(char_id, return);
2019         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2020         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2021         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2022         CHECK_OAL_GATT_ENABLED();
2023         CHECK_CLIENT_CONNECTION(conn_id);
2024
2025         ret = gatt_api->client->acquire_notify(conn_id, (btgatt_srvc_id_t *)srvc_id,
2026                         (btgatt_gatt_id_t *)char_id, fd, mtu);
2027         if (ret != BT_STATUS_SUCCESS) {
2028                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2029                 return convert_to_oal_status(ret);
2030         }
2031
2032         return OAL_STATUS_SUCCESS;
2033 }
2034
2035 oal_status_t gattc_acquire_write(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2036                 oal_gatt_id_t *char_id,  oal_gatt_auth_req_t auth_req , int *fd, int*mtu)
2037 {
2038         int ret = OAL_STATUS_SUCCESS;
2039         char uuid_str1[2*BT_UUID_STRING_MAX];
2040         char uuid_str2[2*BT_UUID_STRING_MAX];
2041         OAL_CHECK_PARAMETER(srvc_id, return);
2042         OAL_CHECK_PARAMETER(char_id, return);
2043         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2044         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2045         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2046         CHECK_OAL_GATT_ENABLED();
2047         CHECK_CLIENT_CONNECTION(conn_id);
2048
2049         ret = gatt_api->client->acquire_write(conn_id, (btgatt_srvc_id_t *)srvc_id,
2050                         (btgatt_gatt_id_t *)char_id,  auth_req, fd, mtu);
2051         if (ret != BT_STATUS_SUCCESS) {
2052                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2053                 return convert_to_oal_status(ret);
2054         }
2055
2056         return OAL_STATUS_SUCCESS;
2057 }
2058
2059 oal_status_t gattc_write_characteristic(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2060                 oal_gatt_id_t *char_id, oal_gatt_write_type_t write_type,
2061                 int len, oal_gatt_auth_req_t auth_req, char* data)
2062 {
2063         int ret = OAL_STATUS_SUCCESS;
2064         char uuid_str1[2*BT_UUID_STRING_MAX];
2065         char uuid_str2[2*BT_UUID_STRING_MAX];
2066         OAL_CHECK_PARAMETER(srvc_id, return);
2067         OAL_CHECK_PARAMETER(char_id, return);
2068         OAL_CHECK_PARAMETER(data, return);
2069         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2070         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2071         API_TRACE("Client Write Characteristic: Service_uuid: [%s], Char_uuid: [%s]", uuid_str1, uuid_str2);
2072         CHECK_OAL_GATT_ENABLED();
2073         CHECK_CLIENT_CONNECTION(conn_id);
2074
2075         ret = gatt_api->client->write_characteristic(conn_id, (btgatt_srvc_id_t *)srvc_id,
2076                         (btgatt_gatt_id_t *)char_id, write_type, len, auth_req, data);
2077         if (ret != BT_STATUS_SUCCESS) {
2078                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2079                 return convert_to_oal_status(ret);
2080         }
2081
2082         return OAL_STATUS_SUCCESS;
2083 }
2084
2085 oal_status_t gattc_write_descriptor(int conn_id, oal_gatt_srvc_id_t *srvc_id,
2086                 oal_gatt_id_t *char_id, oal_gatt_id_t *desc_id,
2087                 oal_gatt_write_type_t write_type, int len, oal_gatt_auth_req_t auth_req, char* data)
2088 {
2089         int ret = OAL_STATUS_SUCCESS;
2090         char uuid_str1[2*BT_UUID_STRING_MAX];
2091         char uuid_str2[2*BT_UUID_STRING_MAX];
2092         char uuid_str3[2*BT_UUID_STRING_MAX];
2093         OAL_CHECK_PARAMETER(srvc_id, return);
2094         OAL_CHECK_PARAMETER(char_id, return);
2095         OAL_CHECK_PARAMETER(desc_id, return);
2096         OAL_CHECK_PARAMETER(data, return);
2097         uuid_to_stringname(&(srvc_id->id.uuid), uuid_str1);
2098         uuid_to_stringname(&(char_id->uuid), uuid_str2);
2099         uuid_to_stringname(&(desc_id->uuid), uuid_str3);
2100         API_TRACE("Client Write Descriptor: Service_uuid: [%s], Char_uuid: [%s], Desc_uuid: [%s]",
2101                                 uuid_str1, uuid_str2, uuid_str3);
2102         CHECK_OAL_GATT_ENABLED();
2103         CHECK_CLIENT_CONNECTION(conn_id);
2104
2105         ret = gatt_api->client->write_descriptor(conn_id, (btgatt_srvc_id_t *)srvc_id,
2106                         (btgatt_gatt_id_t *)char_id, (btgatt_gatt_id_t *)desc_id, write_type, len, auth_req, data);
2107         if (ret != BT_STATUS_SUCCESS) {
2108                 BT_ERR("GATT client write characteristic failed: %s", status2string(ret));
2109                 return convert_to_oal_status(ret);
2110         }
2111
2112         return OAL_STATUS_SUCCESS;
2113 }
2114
2115 oal_status_t gattc_conn_param_update(bt_address_t * address, int min, int max, int latency, int timeout)
2116 {
2117         int ret;
2118         bdstr_t bdstr;
2119
2120         OAL_CHECK_PARAMETER(address, return);
2121         CHECK_OAL_GATT_ENABLED();
2122
2123         BT_DBG("[%s] min[%d] max[%d] latency[%d] timeout[%d]", bdt_bd2str(address, &bdstr), min, max, latency, timeout);
2124         ret = gatt_api->client->conn_parameter_update((bt_bdaddr_t *)address, min, max, latency, timeout);
2125         if (ret != BT_STATUS_SUCCESS) {
2126                 BT_ERR("error: %s", status2string(ret));
2127                 return convert_to_oal_status(ret);
2128         }
2129         return OAL_STATUS_SUCCESS;
2130 }
2131
2132 oal_status_t gattc_unregister_scan_filter(int slot_id)
2133 {
2134         int ret;
2135         int client_if = 0;
2136
2137         API_TRACE("");
2138         CHECK_OAL_GATT_ENABLED();
2139
2140         BT_INFO("Remove Scan filter Slot ID is: [%d]", slot_id);
2141
2142         ret = gatt_api->client->scan_filter_clear(client_if, slot_id);
2143         if (ret != BT_STATUS_SUCCESS) {
2144                 BT_ERR("error: %s", status2string(ret));
2145                 return convert_to_oal_status(ret);
2146         }
2147         return OAL_STATUS_SUCCESS;
2148 }
2149
2150 oal_status_t gattc_enable_scan_filter(int client_if)
2151 {
2152         int ret;
2153
2154         API_TRACE("");
2155         CHECK_OAL_GATT_ENABLED();
2156
2157         BT_INFO("Enable Scan filter. Client If: %d", client_if);
2158
2159         ret = gatt_api->client->scan_filter_enable(client_if, TRUE);
2160         if (ret != BT_STATUS_SUCCESS) {
2161                 BT_ERR("error: %s", status2string(ret));
2162                 return convert_to_oal_status(ret);
2163         }
2164         return OAL_STATUS_SUCCESS;
2165 }
2166
2167 oal_status_t gattc_disable_scan_filter(int client_if)
2168 {
2169         int ret;
2170
2171         API_TRACE("");
2172         CHECK_OAL_GATT_ENABLED();
2173
2174         BT_INFO("Disable Scan filter. Client If: %d", client_if);
2175
2176         ret = gatt_api->client->scan_filter_enable(client_if, FALSE);
2177         if (ret != BT_STATUS_SUCCESS) {
2178                 BT_ERR("error: %s", status2string(ret));
2179                 return convert_to_oal_status(ret);
2180         }
2181         return OAL_STATUS_SUCCESS;
2182 }
2183
2184 oal_status_t gattc_register_scan_filter(oal_ble_scan_filter_t* filter_data)
2185 {
2186         int ret;
2187         int client_info = 0;
2188         int action = 0;
2189         int company_id = 0;
2190         int company_id_mask = 0;
2191         int address_type = 0;
2192         int feature_selection = 0;
2193
2194         oal_ble_scan_filter_param_setup_t scan_filter_setup = {.list_logic_type = 0,
2195                                                                 .filt_logic_type = 1,
2196                                                                 .rssi_high_thres = -127,
2197                                                                 .rssi_low_thres = -127,
2198                                                                 .dely_mode = 0,
2199                                                                 .found_timeout = 0,
2200                                                                 .lost_timeout = 0,
2201                                                                 .found_timeout_cnt = 0
2202                                                                 };
2203
2204         OAL_CHECK_PARAMETER(filter_data, return);
2205         API_TRACE();
2206         CHECK_OAL_GATT_ENABLED();
2207
2208         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS){
2209                 bdstr_t bdstr;
2210                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS is being added");
2211                 BT_INFO("BT remote Device address is %s", bdt_bd2str(filter_data->device_address, &bdstr));
2212                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2213                                                                 action,
2214                                                                 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS,
2215                                                                 filter_data->slot_id,
2216                                                                 company_id,
2217                                                                 company_id_mask,
2218                                                                 NULL,
2219                                                                 NULL,
2220                                                                 (bt_bdaddr_t*)filter_data->device_address,
2221                                                                 address_type,
2222                                                                 0,
2223                                                                 NULL,
2224                                                                 0,
2225                                                                 NULL
2226                                                                 );
2227                 if (ret != BT_STATUS_SUCCESS){
2228                         BT_ERR("error: %s", status2string(ret));
2229                         BT_INFO("unregistering already set filter features.");
2230                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2231                         return convert_to_oal_status(ret);
2232                 }
2233
2234                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
2235         }
2236         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME){
2237                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2238                                                                 action,
2239                                                                 OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME,
2240                                                                 filter_data->slot_id,
2241                                                                 company_id,
2242                                                                 company_id_mask,
2243                                                                 NULL,
2244                                                                 NULL,
2245                                                                 NULL,
2246                                                                 address_type,
2247                                                                 0,
2248                                                                 filter_data->device_name,     // device_name as p_data in HAL
2249                                                                 0,
2250                                                                 NULL
2251                                                                 );
2252                 if (ret != BT_STATUS_SUCCESS){
2253                         BT_ERR("error: %s", status2string(ret));
2254                         BT_INFO("unregistering already set filter features.");
2255                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2256                         return convert_to_oal_status(ret);
2257                 }
2258
2259                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_DEVICE_NAME;
2260         }
2261         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID){
2262                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID is being added");
2263                 char uuid_str1[2*BT_UUID_STRING_MAX];
2264                 char uuid_str2[2*BT_UUID_STRING_MAX];
2265
2266                 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid, uuid_str1);
2267                 uuid_to_stringname((service_uuid_t*)filter_data->service_uuid_mask, uuid_str2);
2268
2269                 BT_INFO("Service UUID  is [%s] and Service UUID mask is [%s]", uuid_str1, uuid_str2);
2270                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2271                                                                 action,
2272                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID,
2273                                                                 filter_data->slot_id,
2274                                                                 company_id,
2275                                                                 company_id_mask,
2276                                                                 (bt_uuid_t*)filter_data->service_uuid,
2277                                                                 (bt_uuid_t*)filter_data->service_uuid_mask,
2278                                                                 NULL,
2279                                                                 address_type,
2280                                                                 filter_data->service_uuid_len,   // service_uuid_len as data_len in HAL
2281                                                                 NULL,
2282                                                                 filter_data->service_uuid_mask_len,
2283                                                                 NULL
2284                                                                 );
2285                 if (ret != BT_STATUS_SUCCESS){
2286                         BT_ERR("error: %s", status2string(ret));
2287                         BT_INFO("unregistering already set filter features.");
2288                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2289                         return convert_to_oal_status(ret);
2290                 }
2291
2292                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_UUID;
2293         }
2294         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID){
2295                 BT_INFO("OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID is being added");
2296                 char uuid_str1[2*BT_UUID_STRING_MAX];
2297                 char uuid_str2[2*BT_UUID_STRING_MAX];
2298                 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid, uuid_str1);
2299                 uuid_to_stringname((service_uuid_t*)filter_data->service_solicitation_uuid_mask, uuid_str2);
2300                 BT_INFO("Service Solicitation UUID  is [%s] and Service Solicitation UUID mask is [%s]", uuid_str1, uuid_str2);
2301                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2302                                                                 action,
2303                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID,
2304                                                                 filter_data->slot_id,
2305                                                                 company_id,
2306                                                                 company_id_mask,
2307                                                                 (bt_uuid_t*)filter_data->service_solicitation_uuid,
2308                                                                 (bt_uuid_t*)filter_data->service_solicitation_uuid_mask,
2309                                                                 NULL,
2310                                                                 address_type,
2311                                                                 filter_data->service_solicitation_uuid_len,   // service_solicitation_uuid_len as data_len in HAL
2312                                                                 NULL,
2313                                                                 filter_data->service_solicitation_uuid_mask_len,
2314                                                                 NULL
2315                                                                 );
2316                 if (ret != BT_STATUS_SUCCESS){
2317                         BT_ERR("error: %s", status2string(ret));
2318                         BT_INFO("unregistering already set filter features.");
2319                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2320                         return convert_to_oal_status(ret);
2321                 }
2322
2323                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
2324         }
2325         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA){
2326                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2327                                                                 action,
2328                                                                 OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA,
2329                                                                 filter_data->slot_id,
2330                                                                 company_id,
2331                                                                 company_id_mask,
2332                                                                 NULL,
2333                                                                 NULL,
2334                                                                 NULL,
2335                                                                 address_type,
2336                                                                 filter_data->service_data_len,    //service_data_len as data_len in HAL
2337                                                                 (char*)filter_data->service_data,
2338                                                                 filter_data->service_data_mask_len,
2339                                                                 (char*)filter_data->service_data_mask
2340                                                                 );
2341                 if (ret != BT_STATUS_SUCCESS){
2342                         BT_ERR("error: %s", status2string(ret));
2343                         BT_INFO("unregistering already set filter features.");
2344                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2345                         return convert_to_oal_status(ret);
2346                 }
2347
2348                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_SERVICE_DATA;
2349         }
2350         if (filter_data->added_features & OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA){
2351                 ret = gatt_api->client->scan_filter_add_remove(client_info,
2352                                                                 action,
2353                                                                 OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA,
2354                                                                 filter_data->slot_id,
2355                                                                 filter_data->manufacturer_id,
2356                                                                 company_id_mask,
2357                                                                 NULL,
2358                                                                 NULL,
2359                                                                 NULL,
2360                                                                 address_type,
2361                                                                 filter_data->manufacturer_data_len,    //manufacturer_data_len as data_len in HAL
2362                                                                 (char*)filter_data->manufacturer_data,
2363                                                                 filter_data->manufacturer_data_mask_len,
2364                                                                 (char*)filter_data->manufacturer_data_mask
2365                                                                 );
2366                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2367                 if (ret != BT_STATUS_SUCCESS){
2368                         BT_ERR("error: %s", status2string(ret));
2369                         BT_INFO("unregistering already set filter features.");
2370                         gatt_api->client->scan_filter_clear(client_info, filter_data->slot_id);
2371                         return convert_to_oal_status(ret);
2372                 }
2373
2374                 feature_selection |= OAL_BLE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
2375         }
2376
2377         BT_DBG("Filter selection 0x%.2x", feature_selection);
2378
2379         ret = gatt_api->client->scan_filter_param_setup(client_info, action, filter_data->slot_id, feature_selection,
2380                                                         scan_filter_setup.list_logic_type, scan_filter_setup.filt_logic_type,
2381                                                         scan_filter_setup.rssi_high_thres, scan_filter_setup.rssi_low_thres,
2382                                                         scan_filter_setup.dely_mode, scan_filter_setup.found_timeout,
2383                                                         scan_filter_setup.lost_timeout, scan_filter_setup.found_timeout_cnt);
2384         if (ret != BT_STATUS_SUCCESS){
2385                 BT_ERR("error: %s", status2string(ret));
2386                 return convert_to_oal_status(ret);
2387         }
2388         return OAL_STATUS_SUCCESS;
2389 }
2390
2391 oal_status_t gattc_get_att_mtu(int conn_id, int *mtu)
2392 {
2393         int ret = OAL_STATUS_SUCCESS;
2394         API_TRACE("Get ATT MTU, conn_id: %d", conn_id);
2395         CHECK_OAL_GATT_ENABLED();
2396         CHECK_CLIENT_CONNECTION(conn_id);
2397         OAL_CHECK_PARAMETER(mtu, return);
2398
2399         /* To prevent crash in case other libraries not support this api */
2400         if (gatt_api->client->get_att_mtu == NULL) {
2401                 BT_WARN("get_att_mtu is NULL");
2402                 return OAL_STATUS_NOT_SUPPORT;
2403         }
2404
2405         ret = gatt_api->client->get_att_mtu(conn_id, mtu);
2406         if (ret != BT_STATUS_SUCCESS) {
2407                 BT_ERR("GATT MTU Size failed, status: %s", status2string(ret));
2408                 return convert_to_oal_status(ret);
2409         }
2410
2411         BT_INFO("Current ATT MTU Size: %d", *mtu);
2412         return OAL_STATUS_SUCCESS;
2413 }
2414
2415 oal_status_t gattc_configure_mtu(int conn_id, int mtu)
2416 {
2417         int ret;
2418
2419         API_TRACE("Configure MTU Size: [%d]", mtu);
2420         CHECK_OAL_GATT_ENABLED();
2421         CHECK_CLIENT_CONNECTION(conn_id);
2422
2423         /* To prevent crash in case other libraries not support this api */
2424         if (gatt_api->client->configure_mtu == NULL) {
2425                 BT_WARN("configure_mtu is NULL");
2426                 return OAL_STATUS_NOT_SUPPORT;
2427         }
2428
2429         ret = gatt_api->client->configure_mtu(conn_id, mtu);
2430         if (ret != BT_STATUS_SUCCESS) {
2431                 BT_ERR("Gatt client configure_mtu error: %s", status2string(ret));
2432                 return convert_to_oal_status(ret);
2433         }
2434
2435         return OAL_STATUS_SUCCESS;
2436 }
2437
2438 oal_status_t gatt_get_data_batching_available_packets(
2439                 unsigned int *available_packets)
2440 {
2441         int ret;
2442
2443         ret = gatt_api->client->get_data_batching_available_packets(available_packets);
2444         if (ret != BT_STATUS_SUCCESS) {
2445                 BT_ERR("GATT data batching failed: %s", status2string(ret));
2446                 return convert_to_oal_status(ret);
2447         }
2448
2449         return OAL_STATUS_SUCCESS;
2450 }
2451
2452 oal_status_t gatt_enable_data_batching(bt_address_t * address,
2453                 int packet_threshold, int timeout)
2454 {
2455         int ret;
2456
2457         ret = gatt_api->client->enable_data_batching((bt_bdaddr_t *)address, packet_threshold, timeout);
2458         if (ret != BT_STATUS_SUCCESS) {
2459                 BT_ERR("GATT data batching failed: %s", status2string(ret));
2460                 return convert_to_oal_status(ret);
2461         }
2462
2463         return OAL_STATUS_SUCCESS;
2464 }
2465
2466 oal_status_t gatt_disable_data_batching(bt_address_t * address)
2467 {
2468         int ret;
2469
2470         ret = gatt_api->client->disable_data_batching((bt_bdaddr_t *)address);
2471         if (ret != BT_STATUS_SUCCESS) {
2472                 BT_ERR("GATT data batching failed: %s", status2string(ret));
2473                 return convert_to_oal_status(ret);
2474         }
2475
2476         return OAL_STATUS_SUCCESS;
2477 }
2478
2479 oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
2480 {
2481         int ret = OAL_STATUS_SUCCESS;
2482         bdstr_t bdstr;
2483
2484         OAL_CHECK_PARAMETER(device_address, return);
2485         API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr) + 12);
2486         CHECK_OAL_GATT_ENABLED();
2487
2488         ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
2489         if (ret != BT_STATUS_SUCCESS) {
2490                 BT_ERR("GATT connection info add failed: %s", status2string(ret));
2491                 return convert_to_oal_status(ret);
2492         }
2493         return OAL_STATUS_SUCCESS;
2494 }
2495