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