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