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