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