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