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