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