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