310387b331170b3cec2efcd347679ac2dde766af
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / gatt / bt-service-gatt.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
5  *
6  * Contact: Anupam Roy (anupam.r@samsung.com)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <glib.h>
22 #include <dlog.h>
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
28
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
34
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
40 #ifdef TIZEN_GATT_CLIENT
41 #include "bluetooth-gatt-client-api.h"
42 #endif
43
44 #include <oal-hardware.h>
45 #include <oal-manager.h>
46 #include <oal-event.h>
47 #include <oal-adapter-mgr.h>
48 #include <oal-device-mgr.h>
49 #include <oal-gatt.h>
50
51 #define BT_GATT_TRANSPORT_LE 0
52 #define BT_GATT_TRANSPORT_BR_EDR 1
53 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
54 #define BT_UUID_STRING_MAX 64
55 #define BT_SENDER_MAX_LENGTH 50
56 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
57
58 #ifdef TIZEN_GATT_CLIENT
59 #define NUM_UUID 20
60 #else
61 #define NUM_UUID 10
62 #endif
63
64
65 #define UUID_MAX_LEN 50
66
67
68 #define BDADDR_ANY   (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
69
70 #ifdef TIZEN_GATT_CLIENT
71 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
72         "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
73         "0000b00b-2222-1111-0123-456789ab0cd2",
74         "0000b00b-3333-1111-0123-456789ab0cd2",
75         "0000b00b-4444-1111-0123-456789ab0cd2",
76         "0000b00b-5555-1111-0123-456789ab0cd2",
77         "0000b00b-6666-1111-0123-456789ab0cd2",
78         "0000b00b-7777-1111-0123-456789ab0cd2",
79         "0000b00b-8888-1111-0123-456789ab0cd2",
80         "0000b00b-9999-1111-0123-456789ab0cd2",
81         "0000b00b-aaaa-1111-0123-456789ab0cd2",
82         "0000b00b-bbbb-1111-0123-456789ab0cd2",
83         "0000b00b-cccc-1111-0123-456789ab0cd2",
84         "0000b00b-dddd-1111-0123-456789ab0cd2",
85         "0000b00b-eeee-1111-0123-456789ab0cd2",
86         "0000b00b-ffff-1111-0123-456789ab0cd2",
87         "0000b00c-0000-1111-0123-456789ab0cd2",
88         "0000b00c-1111-1111-0123-456789ab0cd2",
89         "0000b00c-2222-1111-0123-456789ab0cd2",
90         "0000b00c-3333-1111-0123-456789ab0cd2"};
91 #else
92 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
93         "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
94         "0000b00b-2222-1111-0123-456789ab0cd2",
95         "0000b00b-3333-1111-0123-456789ab0cd2",
96         "0000b00b-4444-1111-0123-456789ab0cd2",
97         "0000b00b-5555-1111-0123-456789ab0cd2",
98         "0000b00b-6666-1111-0123-456789ab0cd2",
99         "0000b00b-7777-1111-0123-456789ab0cd2",
100         "0000b00b-8888-1111-0123-456789ab0cd2",
101         "0000b00b-9999-1111-0123-456789ab0cd2"};
102 #endif
103
104 #ifdef TIZEN_GATT_CLIENT
105 /* Reserved GATT client Instance UUID. This is used only internally by bt-service */
106 #define DEFAULT_GATT_CLIENT_UUID "0000a00a-1111-1111-0123-456789abcdef"
107
108 static int gatt_default_client = -1;
109
110 typedef struct {
111         gboolean is_registered;
112         bluetooth_device_address_t addr;
113         unsigned char svc_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
114         int svc_inst;
115         unsigned char char_uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
116         int char_inst;
117 } bt_gatt_notif_reg_info_t;
118
119 struct gatt_out_conn_info_t {
120         int client_id;                                   /* This value unique identifies a GATT Client instance */
121         char *addr;                                      /* Remote GATT Server address */
122 };
123
124 /* Linked List of outgoing gatt connection list
125    Note: This is valid only for local GATT client  */
126 static GSList *outgoing_gatt_conn_list = NULL;
127
128 /* GATT Server Info(Local Client) List Structure */
129 struct gatt_server_info_t {
130         int connection_id;                               /* This value will uniquely identify a GATT client-server connection */
131         int client_id;                                   /* This value unique identifies a GATT Client instance */
132         char *addr;                                      /* Remote GATT Server address */
133 };
134
135 /* Linked List of connected Remote GATT Servers */
136 static GSList *gatt_server_info_list = NULL;
137
138 typedef struct {
139         gboolean is_changed;
140         unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN]; /* If any service added */
141 } bt_gatt_svc_changed_info_t;
142
143
144 typedef struct {
145         int conn_id;
146         //      bluetooth_device_address_t address; /* Remote BLE Device Address */
147         GSList *services;                   /* List of all services of above remote device */
148         int count;                          /* Number of services browsed from remote device */
149         bt_gatt_svc_changed_info_t info;
150 } bt_gatt_service_info_list_t;
151
152 typedef struct {
153         unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
154         int inst_id;
155         int is_primary;
156         GSList *chars;
157         GSList *included_svcs;
158         gboolean is_removed;            /* 0 => Service is added, 1=> removed */
159 } bt_gatt_service_info_t;
160
161 typedef struct {
162         unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
163         int inst_id;
164         int props;
165         int val_len;
166         unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
167         GSList *descs;
168 } bt_gatt_char_info_t;
169
170 typedef struct {
171         unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
172         int inst_id;
173         int val_len;
174         unsigned char val[BLUETOOTH_GATT_ATT_DATA_LENGTH_MAX];
175 } bt_gatt_descriptor_info_t;
176
177 typedef struct {
178         unsigned char uuid[BLUETOOTH_UUID_HEX_MAX_LEN];
179         int inst_id;
180 } bt_gatt_included_service_info_t;
181
182 GSList *list_gatt_info;
183
184
185 #endif
186
187 /* App Information structure */
188 typedef struct {
189         int adv_handle;
190         char sender[BT_SENDER_MAX_LENGTH];
191         char uuid[BT_UUID_STRING_MAX];
192         int instance_id;
193         int adv_instance;
194         bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
195         int adv_data_len;
196         bluetooth_scan_resp_data_t scan_rsp;   /* Will store scan rsp data for specific slot */
197         int scan_rsp_len;
198         gboolean is_initialized;
199         GSList *service_handles;
200 #ifdef TIZEN_GATT_CLIENT
201         int client_id;                      /* GATT Client instance ID */
202         bluetooth_device_address_t address; /* Remote BLE Device Address */
203         gboolean is_watcher_enabled;
204 #endif
205 } bt_service_app_info_t;
206
207 /* GATT Server Request Info Structure */
208 struct gatt_server_req_info {
209         int connection_id;                               /* This value will uniquely identify a GATT client-server connection */
210         int request_id;                                  /* This is an unique transaction ID assigned against each request by stack */
211         int attribute_handle;                            /* GATT server attribute handle */
212         int offset;                                      /* GATT server attribute offset on which request is invoked by GATT client */
213         bluetooth_gatt_att_request_type_e request_type;  /* Read or Write request */
214         char *addr;                                      /* Remote GATT client address */
215 };
216
217 /* GATT Client Info List Structure */
218 struct gatt_client_info_t {
219         int connection_id;                               /* This value will uniquely identify a GATT client-server connection */
220         int instance_id;                                 /* This value unique identifies a GATT server instance */
221         char *addr;                                      /* Remote GATT client address */
222 };
223
224 /* GATT Indicate confirm result  */
225 struct gatt_indicate_cfm_result_info_t {
226         int result;                                      /* Result of event */
227         char *addr;                                      /* Remote GATT client address */
228         int att_hdl;                                     /* Characteristic Attribute handle */
229         int completed;                                   /* 1 if last event, otheriwse 0 */
230 };
231
232 /* Request Search Utility method */
233 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
234                 bluetooth_gatt_att_request_type_e req_type);
235
236 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
237                 bluetooth_gatt_server_indication_params_t *param);
238
239 static void __bt_remove_all_service_handles(int instance_id);
240
241 #ifdef TIZEN_GATT_CLIENT
242 static void __bt_handle_client_instance_registered(event_gattc_register_t *data);
243 static void __bt_handle_client_connected(event_gattc_conn_t *event_data);
244 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data);
245 static void __bt_handle_client_service_search_completed(event_gattc_conn_status_t *event_data);
246 static void __bt_handle_client_service_search_result(event_gattc_service_result_t *event_data);
247 static void __bt_handle_client_characteristic_search_result(
248                 event_gattc_characteristic_result_t *event_data);
249 static void __bt_handle_client_descriptor_search_result(event_gattc_descriptor_result_t *event_data);
250 static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *event_data);
251 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
252 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
253 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
254 static int __bt_do_unregister_gatt_instance(int instance_id);
255 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
256 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
257                 gboolean is_registered);
258 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data);
259
260 #endif
261
262 static int __bt_do_unregister_server_instance(int server_instance);
263
264 /* Linked List of GATT requests from Remote GATT Clients */
265 static GSList *gatt_server_requests = NULL;
266
267 /* Linked List of connected Remote GATT clients */
268 static GSList *gatt_client_info_list = NULL;
269
270 /* Number of clients to be notified to */
271 static int num_indicate_clients;
272
273 /* List of applications */
274 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
275
276 static void __bt_gatt_handle_pending_request_info(int result,
277                 int service_function, void *data, unsigned int size);
278
279 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
280
281 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
282
283 void _bt_check_adv_app_termination(const char *name)
284 {
285         bt_service_app_info_t *app = NULL;
286         int k;
287         int apps[MAX_APPS_SUPPORTED];
288
289         ret_if(NULL == name);
290
291         memset(&apps, -1, sizeof(apps));
292
293         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
294                 app = &numapps[k];
295
296                 /* Search for a app which has same sender and stop adv is running */
297                 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
298                         BT_DBG("Match found, name: %s", name);
299
300                         /* Unregister all service handles with stack */
301                         __bt_remove_all_service_handles(app->instance_id);
302
303                         /* If Advertising is enabled, stop it */
304                         if (app->adv_handle > 0) {
305                                 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
306                                 /* Disable adv if running */
307                                 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
308                                                 app->adv_handle, name);
309                                 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
310                         }
311
312                         /* Save instances of all apps that need to be unregistered */
313                         apps[app->instance_id] = 1;
314                 }
315         }
316
317         /* Unregister all apps one by one */
318         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
319                 if (apps[k] == 1) {
320                         BT_INFO("Unregister app[%d]", k);
321                         /* Unregister server instance */
322                         __bt_do_unregister_server_instance(k);
323                 }
324         }
325 }
326
327 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
328                 bluetooth_gatt_server_indication_params_t *param)
329 {
330         GSList *l;
331         int ret = OAL_STATUS_SUCCESS;
332         int result = OAL_STATUS_INTERNAL_ERROR;
333
334         BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
335         for (l = gatt_client_info_list; l != NULL; l = l->next) {
336                 struct gatt_client_info_t *info = l->data;
337
338                 if (info) {
339                         BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
340
341                         ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
342                                         info->connection_id, data->length,
343                                         param->need_confirmation, (char *)(&data->data[0]));
344
345                         BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
346                         if (ret == OAL_STATUS_SUCCESS) {
347                                 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
348                                 result = ret;
349                                 num_indicate_clients++;
350                         }
351                 }
352         }
353         BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
354         return result;
355 }
356
357 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
358                 bluetooth_gatt_att_request_type_e req_type)
359 {
360         GSList *l;
361
362         for (l = gatt_server_requests; l != NULL; l = l->next) {
363                 struct gatt_server_req_info *req_info = l->data;
364
365                 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
366                         BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
367                                         req_info->request_id, req_info->attribute_handle, req_info->connection_id);
368                         return req_info;
369                 }
370         }
371         BT_ERR("Gatt Request not found");
372         return NULL;
373 }
374
375 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
376 {
377         BT_DBG("+");
378         int k;
379         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
380                 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
381                         *adv_handle = numapps[k].adv_handle;
382                         break;
383                 }
384         }
385 }
386
387 #ifdef TIZEN_GATT_CLIENT
388 char * _bt_gatt_get_default_gatt_client_uuid(void)
389 {
390         return g_strdup(DEFAULT_GATT_CLIENT_UUID);
391 }
392
393
394 static void __bt_register_default_gatt_client()
395 {
396         char *uuid_str;
397         oal_uuid_t uuid;
398
399         uuid_str = _bt_gatt_get_default_gatt_client_uuid();
400         _bt_string_to_uuid(uuid_str, (service_uuid_t*)&uuid);
401
402         BT_INFO("Register Default GATT client uuid [%s]", uuid_str);
403
404         if (OAL_STATUS_SUCCESS != gattc_register(&uuid)) /* for only Smart Control */
405                 BT_ERR("gattc register failed");
406
407         g_free(uuid_str);
408 }
409 #endif
410
411 int _bt_gatt_init(void)
412 {
413         BT_DBG("+");
414
415         if (OAL_STATUS_SUCCESS != gatt_enable()) {
416                 BT_ERR("gatt Init failed");
417                 return BLUETOOTH_ERROR_INTERNAL;
418         }
419
420         /* Register gatt event handler */
421         _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
422 #ifdef TIZEN_GATT_CLIENT
423         /*In bluedroid product, defacult gatt client is handled differently*/
424         __bt_register_default_gatt_client();
425 #endif
426         BT_DBG("-");
427         return BLUETOOTH_ERROR_NONE;
428 }
429
430 static void __bt_service_reset_gatt_data()
431 {
432         int k;
433
434         BT_INFO("Rest numapp");
435
436         /* Reset data */
437         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
438                         numapps[k].is_initialized = 0;
439                         numapps[k].instance_id = -1;
440                         numapps[k].adv_handle = 0;
441                         numapps[k].adv_instance = -1;
442                         memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
443                         memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
444                         memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
445                         memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
446                         numapps[k].adv_data_len = 0;
447                         numapps[k].scan_rsp_len = 0;
448         }
449 }
450
451 void _bt_gatt_deinit(void)
452 {
453         BT_INFO("GATT deinit");
454
455         if (OAL_STATUS_SUCCESS != gatt_disable())
456                 BT_ERR("gatt deinit failed");
457
458         /* Un-register gatt event handler */
459         _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
460
461         __bt_service_reset_gatt_data();
462 }
463
464 void _bt_update_adv_handle(const char *sender, int adv_handle)
465 {
466         int k;
467         BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
468         bt_service_app_info_t *info = NULL;
469
470         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
471                 info = &numapps[k];
472                 /* Search for a app which has same sender and adv handle as 0 */
473                 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
474                         info->adv_handle = adv_handle;
475         }
476 }
477
478 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
479 {
480         int k;
481         BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
482         bt_service_app_info_t *info = NULL;
483
484         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
485                 info = &numapps[k];
486                 /* Search for a app which has same sender and adv handle as 0
487                    It is possible that same sender but different adv handle */
488                 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
489                         //info->adv_handle = adv_handle;
490                         return info->instance_id;
491                 }
492         }
493         return -1;
494 }
495
496 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
497 {
498         int k;
499         BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
500         bt_service_app_info_t *info = NULL;
501
502         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
503                 info = &numapps[k];
504
505                 /* Exact matching of Adv handle + sender combination */
506                 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
507                         return info->instance_id;
508 #if 0
509                 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
510                         return info->instance_id;
511 #endif
512         }
513         return -1;
514 }
515
516 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
517 {
518         int k;
519         bt_service_app_info_t *info = NULL;
520
521         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
522                 info = &numapps[k];
523
524                 if (info->instance_id == server_instance && info->adv_handle != -1) {
525                         *adv_handle = info->adv_handle;
526                         BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
527                         return g_strdup(info->sender);
528                 }
529         }
530         return NULL;
531 }
532
533 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
534 {
535         int k;
536         BT_DBG("+");
537         bt_service_app_info_t *info = NULL;
538         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
539                 info = &numapps[k];
540
541                 if (info->instance_id == instance) {
542                         memcpy(info->adv_data.data, &adv->data, len);
543                         break;
544                 }
545         }
546         BT_DBG("-");
547 }
548
549 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
550 {
551         int k;
552         BT_DBG("+");
553         bt_service_app_info_t *info = NULL;
554         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
555                 info = &numapps[k];
556
557                 if (info->instance_id == instance) {
558                         memcpy(info->scan_rsp.data, &scan->data, len);
559                         break;
560                 }
561         }
562         BT_DBG("-");
563 }
564
565 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
566 {
567         int k;
568         BT_DBG("+");
569         bt_service_app_info_t *info = NULL;
570
571         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
572                 info = &numapps[k];
573
574                 if (info->instance_id == instance) {
575                         memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
576                         *len = info->adv_data_len;
577                         break;
578                 }
579         }
580 }
581
582 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
583 {
584         int k;
585         BT_DBG("+");
586         bt_service_app_info_t *info = NULL;
587
588         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
589                 info = &numapps[k];
590
591                 if (info->instance_id == instance) {
592                         memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
593                         *len = info->scan_rsp_len;
594                         break;
595                 }
596         }
597 }
598
599 #ifdef TIZEN_GATT_CLIENT
600 static int __bt_do_unregister_gatt_instance(int instance_id)
601 {
602         int ret = OAL_STATUS_SUCCESS;
603         int k;
604
605         BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
606
607         /* Reset data: instance_id parameter could be either for GATT Server or for GATT client  */
608         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
609                 if (numapps[k].instance_id == instance_id) {
610                         BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
611                         numapps[k].is_initialized = FALSE;
612                         numapps[k].instance_id = -1;
613                         numapps[k].adv_handle = 0;
614                         numapps[k].adv_instance = -1;
615                         memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
616                         memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
617                         memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
618                         memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
619                         numapps[k].adv_data_len = 0;
620                         numapps[k].scan_rsp_len = 0;
621
622                         /* Its a GATT Server Instance */
623                         ret = gatts_unregister(instance_id);
624                         if (ret != OAL_STATUS_SUCCESS) {
625                                 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
626                                 return BLUETOOTH_ERROR_INTERNAL;
627                         }
628                         break;
629                 } else if (numapps[k].client_id == instance_id) {
630                         BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
631                         numapps[k].client_id = -1;
632                         numapps[k].is_initialized = FALSE;
633                         memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
634                         memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
635                         memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
636
637                         /* Its a GATT Client Instance */
638                         ret = gattc_deregister(instance_id);
639                         if (ret != OAL_STATUS_SUCCESS) {
640                                 BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
641                                 return BLUETOOTH_ERROR_INTERNAL;
642                         }
643                         break;
644                 }
645         }
646         return BLUETOOTH_ERROR_NONE;
647 }
648 #endif
649
650 static int __bt_do_unregister_server_instance(int server_instance)
651 {
652         int ret = OAL_STATUS_SUCCESS;
653         int k;
654
655         /* Unregister the server instance */
656         ret = gatts_unregister(server_instance);
657         if (ret != OAL_STATUS_SUCCESS) {
658                 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
659                 return BLUETOOTH_ERROR_INTERNAL;
660         }
661         BT_INFO("DeAllocated server instance with stack successful..");
662
663         /* Reset data */
664         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
665                 if (numapps[k].instance_id == server_instance) {
666                         numapps[k].is_initialized = 0;
667                         numapps[k].instance_id = -1;
668                         numapps[k].adv_handle = 0;
669                         numapps[k].adv_instance = -1;
670                         memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
671                         memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
672                         memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
673                         memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
674                         numapps[k].adv_data_len = 0;
675                         numapps[k].scan_rsp_len = 0;
676                         break;
677                 }
678         }
679         BT_DBG("Going8 to reset numapp block num [%d]", k);
680         return BLUETOOTH_ERROR_NONE;
681 }
682
683 static void __bt_remove_all_service_handles(int instance_id)
684 {
685         GSList *l;
686         int *handle = NULL;
687         int ret = OAL_STATUS_SUCCESS;
688         int count;
689
690         count = g_slist_length(numapps[instance_id].service_handles);
691         BT_INFO("Before handle removal: current count [%d]", count);
692
693         for (l = numapps[instance_id].service_handles; l != NULL;) {
694                 handle = l->data;
695                 l = g_slist_next(l);
696
697                 if (handle) {
698                         BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
699                         if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
700                                 continue;
701
702                         ret = gatts_delete_service(instance_id, *handle);
703                         if (ret != OAL_STATUS_SUCCESS) {
704                                 BT_ERR("ret: %d", ret);
705                                 continue;
706                         } else {
707                                 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
708                                 g_free(handle);
709                                 handle = NULL;
710                                 count = g_slist_length(numapps[instance_id].service_handles);
711                                 BT_INFO("After deleting current count [%d]", count);
712                         }
713                 }
714         }
715 }
716
717 int _bt_unregister_server_instance(const char *sender, int adv_handle)
718 {
719         BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
720         int result = BLUETOOTH_ERROR_NONE;
721         int apps[MAX_APPS_SUPPORTED];
722         int server_instance;
723         int k;
724
725         memset(&apps, -1, sizeof(apps));
726
727         if (adv_handle == 0) {
728                 BT_DBG("Its a direct GATT Server app request to unregister");
729                 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
730                 bt_service_app_info_t *info = NULL;
731
732                 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
733                         info = &numapps[k];
734
735                         /* Exact matching of sender */
736                         if (!g_strcmp0(info->sender, sender)) {
737                                 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
738                                 /* Unregister all service handles with stack */
739                                 __bt_remove_all_service_handles(info->instance_id);
740
741                                 /* Disable adv if running */
742                                 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
743                                                 info->adv_handle, sender);
744                                 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
745
746                                 /* Save all instances which need to be unregistered */
747                                 apps[info->instance_id] = 1;
748                         }
749                 }
750         } else {
751                 BT_DBG("Its an Internal unregister request by adv application");
752                 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
753                 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
754                 if (server_instance == -1) {
755                         BT_ERR("No allocated server instance to be removed");
756                         return BLUETOOTH_ERROR_INVALID_PARAM;
757                 }
758                 if (!numapps[server_instance].service_handles) {
759                         BT_INFO("There are no Service handles with this app, safe to unregister");
760                         /* Unregister server instance only if this sender does not have any gatt services in it */
761                         result = __bt_do_unregister_server_instance(server_instance);
762                 } else {
763                         numapps[server_instance].adv_handle = 0;
764                         memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
765                         numapps[server_instance].adv_data_len = 0;
766                         memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
767                         numapps[server_instance].scan_rsp_len = 0;
768                 }
769                 return result;
770         }
771
772         /* Unregister all apps one by one */
773         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
774                 if (apps[k] == 1) {
775                         BT_INFO("Unregister app[%d]", k);
776                         /* Unregister server instance */
777                         __bt_do_unregister_server_instance(k);
778                 }
779         }
780
781         return result;
782 }
783
784 int _bt_register_server_instance(const char *sender, int adv_handle)
785 {
786         int ret = OAL_STATUS_SUCCESS;
787         char *uuid_string = NULL;
788         int slot = -1;
789         int k;
790         oal_uuid_t uuid;
791
792         BT_INFO("###Check on which instance Server instance can be initialized....");
793         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
794                 if (numapps[k].is_initialized == 1) {
795                         BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
796                 } else {
797                         slot = k;
798                         BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
799                         break;
800                 }
801         }
802
803         if (slot == -1) {
804                 BT_ERR("No Slot if free for GATT Server registration..");
805                 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
806         }
807
808         uuid_string = g_malloc0(BT_UUID_STRING_MAX);
809         _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
810         g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
811         BT_INFO("Copied UUID string [%s]", uuid_string);
812
813         /* Register GATT Server */
814         ret = gatts_register(&uuid);
815         if (ret != OAL_STATUS_SUCCESS) {
816                 BT_ERR("ret: %d", ret);
817                 g_free(uuid_string);
818                 return BLUETOOTH_ERROR_INTERNAL;
819         }
820         BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
821         /* Return & wait for GATT Server Instance Initialization event */
822         memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
823         memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
824
825         g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
826         g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
827
828         numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
829         numapps[slot].adv_handle = adv_handle;
830
831         g_free(uuid_string);
832         return BLUETOOTH_ERROR_NONE;
833 }
834
835 /* Event handlers */
836 static void __bt_gatt_handle_pending_request_info(int result,
837                 int service_function, void *data, unsigned int size)
838 {
839         GSList *l;
840         GArray *out_param;
841         invocation_info_t *req_info = NULL;
842         ret_if(data == NULL);
843         BT_DBG("+");
844
845         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
846                 req_info = l->data;
847                 if (req_info == NULL || req_info->service_function != service_function)
848                         continue;
849
850                 switch (service_function) {
851 #ifndef GATT_DIRECT
852                 case BT_GATT_SERVER_REGISTER: {
853                         bt_service_app_info_t *param = (bt_service_app_info_t*)data;
854                         BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
855                         if (!g_strcmp0(req_info->sender, param->sender)) {
856                                 BT_DBG("GATT Server app found [%s]", req_info->sender);
857
858                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
859                                 g_array_append_vals(out_param, &param->instance_id, sizeof(int));
860                                 _bt_service_method_return(req_info->context, out_param, result);
861
862                                 g_free(req_info->user_data);
863                                 _bt_free_info_from_invocation_list(req_info);
864                                 g_array_free(out_param, TRUE);
865                         }
866                         break;
867                 }
868                 case BT_GATT_SERVER_START_SERVICE:
869                 case BT_GATT_SERVER_DELETE_SERVICE: {
870                         bt_service_app_info_t *param = (bt_service_app_info_t*)data;
871                         BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
872
873                         int *saved_instance_id = (int*)req_info->user_data;
874                         if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
875                                 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
876                                                 req_info->sender, *saved_instance_id);
877
878                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
879                                 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
880                                 _bt_service_method_return(req_info->context, out_param, result);
881
882                                 g_free(req_info->user_data);
883                                 _bt_free_info_from_invocation_list(req_info);
884                                 g_array_free(out_param, TRUE);
885                         }
886                         break;
887                 }
888                 case BT_GATT_SERVER_ADD_SERVICE:
889                 case BT_GATT_SERVER_ADD_DESCRIPTOR:
890                 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
891                         int *handle = (int*)data;
892                         BT_INFO("Characteristic added: Handle [%d]", *handle);
893                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
894
895                         g_array_append_vals(out_param, handle, sizeof(int));
896                         _bt_service_method_return(req_info->context, out_param, result);
897
898                         g_free(req_info->user_data);
899                         _bt_free_info_from_invocation_list(req_info);
900                         g_array_free(out_param, TRUE);
901                         break;
902                 }
903 #endif
904                 case BT_CONNECT_LE:
905                 case BT_DISCONNECT_LE: {
906 #ifdef TIZEN_GATT_CLIENT
907                        char *addr = (char*)req_info->user_data;
908                        bluetooth_device_address_t address;
909
910                         if (!g_strcmp0(addr, (char*)data)) {
911                                 BT_INFO("GATT Client connect-disconnect call pending for app [%s] addr [%s]",
912                                                 req_info->sender, addr);
913                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
914                                 _bt_convert_addr_string_to_type(address.addr, addr);
915
916                                 g_array_append_vals(out_param, (bluetooth_device_address_t*)&address,
917                                                 sizeof(bluetooth_device_address_t));
918                                 _bt_service_method_return(req_info->context, out_param, result);
919
920                                 g_free(req_info->user_data);
921                                 _bt_free_info_from_invocation_list(req_info);
922                                 g_array_free(out_param, TRUE);
923                         }
924 #else
925                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
926
927                         g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
928                                         sizeof(bluetooth_device_address_t));
929                         _bt_service_method_return(req_info->context, out_param, result);
930
931                         g_free(req_info->user_data);
932                         _bt_free_info_from_invocation_list(req_info);
933
934                         g_array_free(out_param, TRUE);
935 #endif
936                         break;
937                 }
938 #ifdef TIZEN_GATT_CLIENT
939                 case BT_GATT_CLIENT_REGISTER: {
940                         bt_service_app_info_t *param = (bt_service_app_info_t*)data;
941                         BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
942                         if (!g_strcmp0(req_info->sender, param->sender)) {
943                                 BT_DBG("GATT Client app found [%s] created client ID [%d]",
944                                                 req_info->sender, param->client_id);
945
946                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
947                                 g_array_append_vals(out_param, &param->client_id, sizeof(int));
948                                 _bt_service_method_return(req_info->context, out_param, result);
949
950                                 g_free(req_info->user_data);
951                                 _bt_free_info_from_invocation_list(req_info);
952                                 g_array_free(out_param, TRUE);
953                         }
954                         break;
955                 }
956                 case BT_GATT_GET_PRIMARY_SERVICES: {
957                         bt_services_browse_info_t *param = (bt_services_browse_info_t*)data;
958                         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
959                         _bt_convert_addr_type_to_string(address,
960                                         (unsigned char *)(&param->device_addr.addr));
961
962                         BT_INFO("Request Sender [%s] address [%s]", req_info->sender, address);
963
964                         /* Match address to determine same request */
965                         if (!g_strcmp0((char*)req_info->user_data, address)) {
966                                 BT_DBG("GATT Client app found [%s] Remote address [%s]",
967                                                 req_info->sender, address);
968
969                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
970                                 g_array_append_vals(out_param, param,
971                                                 sizeof(bt_services_browse_info_t));
972
973                                 //g_array_append_vals(out_param, &param->client_id, sizeof(int));
974                                 _bt_service_method_return(req_info->context, out_param, result);
975
976                                 g_free(req_info->user_data);
977                                 _bt_free_info_from_invocation_list(req_info);
978                                 g_array_free(out_param, TRUE);
979                         }
980                         g_free(address);
981                         break;
982                 }
983                 case BT_GATT_GET_SERVICE_PROPERTIES: {
984                         bt_char_browse_info_t param;
985                         memcpy((void*)&param, data, sizeof(bt_char_browse_info_t));
986                         //bt_char_browse_info_t *param = (bt_char_browse_info_t*)data;
987                         BT_INFO("Request Sender [%s]", req_info->sender);
988                         bluetooth_gatt_client_svc_prop_info_t *prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
989
990                         /* Match both address and service properties to determine same request */
991                         if (!memcmp(param.device_addr.addr,
992                                         prop->device_address.addr,
993                                         sizeof(bluetooth_device_address_t)) &&
994                                         !memcmp(param.svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
995                                         param.svc_inst_id == prop->svc.instance_id) {
996                                 BT_INFO("Returning Service properties");
997
998                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
999                                 g_array_append_vals(out_param, &param, sizeof(bt_char_browse_info_t));
1000                                 _bt_service_method_return(req_info->context, out_param, result);
1001
1002                                 g_free(req_info->user_data);
1003                                 _bt_free_info_from_invocation_list(req_info);
1004                                 g_array_free(out_param, TRUE);
1005                         }
1006                         break;
1007                 }
1008                 case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
1009                         bt_descriptor_browse_info_t *param = (bt_descriptor_browse_info_t*)data;
1010
1011                         BT_INFO("Request Sender [%s]", req_info->sender);
1012                         bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1013
1014                         /* Match both address, service properties &char properties to determine same request */
1015                         if (!memcmp(param->device_addr.addr,
1016                                         prop->device_address.addr,
1017                                         sizeof(bluetooth_device_address_t)) &&
1018                                         !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1019                                         param->svc_inst_id == prop->svc.instance_id &&
1020                                         !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1021                                         param->char_inst_id == prop->characteristic.instance_id) {
1022                                 BT_INFO("Returning Characteristic properties");
1023                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1024                                                 g_array_append_vals(out_param, param, sizeof(bt_descriptor_browse_info_t));
1025                                 _bt_service_method_return(req_info->context, out_param, result);
1026
1027                                 g_free(req_info->user_data);
1028                                 _bt_free_info_from_invocation_list(req_info);
1029                                 g_array_free(out_param, TRUE);
1030                         }
1031                         break;
1032                 }
1033                 case BT_GATT_WATCH_CHARACTERISTIC: {
1034                         bt_gatt_notif_reg_info_t *param = (bt_gatt_notif_reg_info_t*)data;
1035                         bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1036                         BT_INFO("Request Sender [%s]", req_info->sender);
1037                         /* Match both address, service properties &char properties to determine same request */
1038                         if (!memcmp(param->addr.addr,
1039                                         prop->device_address.addr,
1040                                         sizeof(bluetooth_device_address_t)) &&
1041                                         !memcmp(param->svc_uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1042                                         param->svc_inst == prop->svc.instance_id &&
1043                                         !memcmp(param->char_uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) &&
1044                                         param->char_inst == prop->characteristic.instance_id) {
1045                                 BT_INFO("Characteristic Watch Successful: Is registered [%d]",
1046                                                 param->is_registered);
1047                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1048                                 g_array_append_vals(out_param, param, sizeof(bt_gatt_notif_reg_info_t));
1049                                 _bt_service_method_return(req_info->context, out_param, result);
1050                                 g_free(req_info->user_data);
1051                                 _bt_free_info_from_invocation_list(req_info);
1052                                 g_array_free(out_param, TRUE);
1053                         }
1054                         break;
1055                 }
1056                 case BT_GATT_READ_CHARACTERISTIC:
1057                 case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
1058                          bluetooth_gatt_client_char_prop_info_t *param = (bluetooth_gatt_client_char_prop_info_t*)data;
1059                          BT_INFO("Request Sender [%s]", req_info->sender);
1060
1061                          char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1062                          bluetooth_gatt_client_char_prop_info_t *prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
1063                          _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1064
1065                          if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_char_prop_info_t))) {
1066                                  BT_INFO("Gatt Char read or write request matched for address [%s]", addr);
1067                                  out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1068                                  g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_char_prop_info_t));
1069                                  _bt_service_method_return(req_info->context, out_param, result);
1070
1071                                  g_free(req_info->user_data);
1072                                  _bt_free_info_from_invocation_list(req_info);
1073                                  g_array_free(out_param, TRUE);
1074                         }
1075                         g_free(addr);
1076                          break;
1077                 }
1078                 case BT_GATT_READ_DESCRIPTOR_VALUE:
1079                 case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
1080                          bluetooth_gatt_client_desc_prop_info_t *param = (bluetooth_gatt_client_desc_prop_info_t*)data;
1081                          BT_INFO("Request Sender [%s]", req_info->sender);
1082
1083                          char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1084                          bluetooth_gatt_client_desc_prop_info_t *prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
1085                          _bt_convert_addr_type_to_string(addr, prop->device_address.addr);
1086
1087                          if (!memcmp(param, prop, sizeof(bluetooth_gatt_client_desc_prop_info_t))) {
1088                                  BT_INFO("Descriptor read or write request matched for address [%s]", addr);
1089                                  out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1090                                  g_array_append_vals(out_param, param, sizeof(bluetooth_gatt_client_desc_prop_info_t));
1091                                  _bt_service_method_return(req_info->context, out_param, result);
1092
1093                                  g_free(req_info->user_data);
1094                                  _bt_free_info_from_invocation_list(req_info);
1095                                  g_array_free(out_param, TRUE);
1096                         }
1097                         g_free(addr);
1098                         break;
1099                 }
1100 #endif
1101                 default:
1102                         break;
1103                 }
1104         }
1105         BT_DBG("-");
1106 }
1107
1108 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
1109 {
1110         bt_service_app_info_t *info = NULL;
1111         int k;
1112         char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
1113
1114         _bt_uuid_to_string(&(data->server_uuid), uuid_string);
1115         BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
1116
1117         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1118         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1119                 info = &numapps[k];
1120
1121                 if (g_strcmp0(info->uuid, uuid_string) == 0) {
1122                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1123                         info->is_initialized = TRUE;
1124                         info->instance_id = data->server_inst;
1125                         info->adv_instance = data->server_inst;
1126                         __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
1127                                         (void*)info, sizeof(bt_service_app_info_t));
1128                         break;
1129                 }
1130         }
1131         g_free(uuid_string);
1132 }
1133
1134 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
1135 {
1136         int result = BLUETOOTH_ERROR_NONE;
1137         int svc_handle = 0;
1138         int *handle;
1139         int count;
1140         bt_service_app_info_t *info = NULL;
1141         int k;
1142
1143         char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1144         _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
1145         BT_INFO("GATT Added Service  UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1146                         uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1147
1148         if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1149                 result = BLUETOOTH_ERROR_INTERNAL;
1150                 svc_handle = 0;  /* Service handle set to 0 indicates.
1151                                     0 is reserved by BT SIG, cant be used by app*/
1152         } else
1153                 svc_handle = event->gatt_srvc_stat.servic_hndl;
1154
1155         BT_INFO("GATT Added Service  Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
1156                         event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
1157
1158         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1159         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1160                 info = &numapps[k];
1161
1162                 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1163                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1164                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
1165                                         (int*)&svc_handle, sizeof(int));
1166
1167                         /* Add Service Handle */
1168                         if (svc_handle > 0) {
1169                                 handle = g_malloc0(sizeof(int));
1170                                 *handle = svc_handle;
1171                                 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
1172                                 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
1173                                 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
1174                         }
1175                         break;
1176                 }
1177         }
1178
1179         g_free(uuid_str);
1180 }
1181
1182 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
1183 {
1184         int result = BLUETOOTH_ERROR_NONE;
1185         int char_handle;
1186         bt_service_app_info_t *info = NULL;
1187         int k;
1188         char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1189
1190         BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
1191         BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1192         BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1193         BT_INFO("GATT Add characteristic  Status: [%d]", event->gatt_srvc_stat.status);
1194         BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
1195
1196         if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1197                 result = BLUETOOTH_ERROR_INTERNAL;
1198                 char_handle = 0;  /* characteristic handle set to 0 indicates.
1199                                      0 is reserved by BT SIG, cant be used by app*/
1200         } else
1201                 char_handle = event->charctr_hndl;
1202
1203
1204         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1205         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1206                 info = &numapps[k];
1207
1208                 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1209                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1210                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
1211                                         (int*)&char_handle, sizeof(int));
1212                         break;
1213                 }
1214         }
1215
1216
1217         _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
1218         BT_INFO("GATT Added Characteristic:  UUID: [%s]", uuid_str);
1219
1220         g_free(uuid_str);
1221 }
1222
1223 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
1224 {
1225         int result = BLUETOOTH_ERROR_NONE;
1226         int desc_handle;
1227         bt_service_app_info_t *info = NULL;
1228         int k;
1229         char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
1230
1231         BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
1232         BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
1233         BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
1234         BT_INFO("GATT Add Descriptor  Status: [%d]", event->gatt_srvc_stat.status);
1235         BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
1236
1237         if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
1238                 result = BLUETOOTH_ERROR_INTERNAL;
1239                 desc_handle = 0;  /* Service handle set to 0 indicates.
1240                                      0 is reserved by BT SIG, cant be used by app*/
1241         } else
1242                 desc_handle = event->descrptr_hndl;
1243
1244         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1245         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1246                 info = &numapps[k];
1247
1248                 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
1249                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1250                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
1251                                         (int*)&desc_handle, sizeof(int));
1252                         break;
1253                 }
1254         }
1255
1256         _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
1257         BT_INFO("GATT Added Descriptor:  UUID: [%s]", uuid_str);
1258
1259         g_free(uuid_str);
1260 }
1261
1262 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
1263 {
1264         bt_service_app_info_t *info = NULL;
1265         int result = BLUETOOTH_ERROR_NONE;
1266         int k;
1267         BT_INFO("GATT Server Service Started..");
1268
1269
1270         BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1271         BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1272         BT_INFO("GATT Service Started Status: [%d]", event->status);
1273
1274         if (event->status != OAL_STATUS_SUCCESS) {
1275                 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
1276                 result = BLUETOOTH_ERROR_INTERNAL;
1277         }
1278
1279         /* Check if the just registered Instance ID belongs to requester */
1280         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1281                 info = &numapps[k];
1282
1283                 if (info->instance_id == event->server_inst) {
1284                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1285                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
1286                                         (void*)info, sizeof(bt_service_app_info_t));
1287                         break;
1288                 }
1289         }
1290 }
1291
1292 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
1293 {
1294         int result = BLUETOOTH_ERROR_NONE;
1295         bt_service_app_info_t *info = NULL;
1296         int k;
1297         BT_INFO("GATT Server Service Stopped..");
1298
1299         BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1300         BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1301         BT_INFO("GATT Service Stopped Status: [%d]", event->status);
1302
1303         if (event->status != OAL_STATUS_SUCCESS) {
1304                 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
1305                 result = BLUETOOTH_ERROR_INTERNAL;
1306         }
1307
1308         /* Check if the just registered Instance ID belongs to requester */
1309         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1310                 info = &numapps[k];
1311
1312                 if (info->instance_id == event->server_inst) {
1313                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1314                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
1315                                         (void*)info, sizeof(bt_service_app_info_t));
1316                         break;
1317                 }
1318         }
1319 }
1320
1321 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
1322 {
1323         int result = BLUETOOTH_ERROR_NONE;
1324         bt_service_app_info_t *info = NULL;
1325         int k;
1326         BT_INFO("GATT Server Service Deleted..");
1327
1328         BT_INFO("GATT Server_inst: [%d]", event->server_inst);
1329         BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
1330         BT_INFO("GATT Service Deleted Status: [%d]", event->status);
1331
1332         if (event->status != OAL_STATUS_SUCCESS) {
1333                 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
1334                 result = BLUETOOTH_ERROR_INTERNAL;
1335         }
1336
1337         /* Check if the just registered Instance ID belongs to requester */
1338         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1339                 info = &numapps[k];
1340
1341                 if (info->instance_id == event->server_inst) {
1342                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1343                         __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
1344                                         (void*)info, sizeof(bt_service_app_info_t));
1345                         break;
1346                 }
1347         }
1348 }
1349
1350 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
1351 {
1352         GSList *l;
1353         struct gatt_client_info_t *info = NULL;
1354         for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1355                 info = (struct gatt_client_info_t*)l->data;
1356                 if (info == NULL)
1357                         continue;
1358
1359                 if (!g_strcmp0(info->addr, address)) {
1360                         BT_INFO("Remote GATT client found addr[%s]", info->addr);
1361                         return info;
1362                 }
1363         }
1364         return NULL;
1365 }
1366
1367 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
1368 {
1369         GSList *l;
1370         struct gatt_client_info_t *info = NULL;
1371
1372         for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
1373                 info = (struct gatt_client_info_t*)l->data;
1374                 if (info == NULL)
1375                         continue;
1376
1377                 if (info->connection_id == conn_id) {
1378                         BT_INFO("Remote GATT client found addr[%s]", info->addr);
1379                         return info;
1380                 }
1381         }
1382         return NULL;
1383 }
1384
1385 #ifdef TIZEN_GATT_CLIENT
1386 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info(char *address)
1387 {
1388         GSList *l;
1389         struct gatt_server_info_t *info = NULL;
1390         for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1391                 info = (struct gatt_server_info_t*)l->data;
1392                 if (info == NULL)
1393                         continue;
1394
1395                 if (!g_strcmp0(info->addr, address)) {
1396                         BT_INFO("Remote GATT Server found addr[%s]", info->addr);
1397                         return info;
1398                 }
1399         }
1400         return NULL;
1401 }
1402
1403 static struct gatt_out_conn_info_t* __bt_find_gatt_outgoing_conn_info(char *address)
1404 {
1405         GSList *l;
1406         struct gatt_out_conn_info_t *info = NULL;
1407         for (l = outgoing_gatt_conn_list; l != NULL; l = g_slist_next(l)) {
1408                 info = (struct gatt_out_conn_info_t*)l->data;
1409                 if (info == NULL)
1410                         continue;
1411
1412                 if (!g_strcmp0(info->addr, address)) {
1413                         BT_INFO("Outgoing connection info found addr[%s]", info->addr);
1414                         return info;
1415                 }
1416         }
1417         return NULL;
1418 }
1419
1420 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_client_if(int client_if)
1421 {
1422         GSList *l;
1423         struct gatt_server_info_t *info = NULL;
1424
1425         for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
1426                 info = (struct gatt_server_info_t*)l->data;
1427                 if (info == NULL)
1428                         continue;
1429
1430                 if (info->client_id == client_if) {
1431                         BT_INFO("Remote GATT server found addr[%s]", info->addr);
1432                         return info;
1433                 }
1434         }
1435         return NULL;
1436 }
1437 #endif
1438
1439
1440
1441
1442 #ifdef TIZEN_GATT_CLIENT
1443 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1444 {
1445         int result = BLUETOOTH_ERROR_NONE;
1446         struct gatt_client_info_t *conn_info = NULL;
1447         bluetooth_device_address_t dev_addr;
1448         GVariant *param = NULL;
1449
1450         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1451
1452         memcpy(dev_addr.addr, event->address.addr, 6);
1453
1454         /* REPLY dbus Context to application which called BT_CONNECT_LE. There is status
1455            in this event */
1456         _bt_convert_addr_type_to_string(address,
1457                         (unsigned char *)dev_addr.addr);
1458         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1459                         address, BT_ADDRESS_STRING_SIZE);
1460
1461         BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1462                         address, event->server_inst, event->conn_id);
1463
1464
1465         /* Check if device is already in connected list */
1466         conn_info = __bt_find_remote_gatt_client_info(address);
1467
1468         if (!conn_info) {
1469                 BT_INFO("Conn Info absent: But no need to Send Local GATT Server Connected event to apps");
1470
1471                 param = g_variant_new("(is)", result, address);
1472
1473                 /* Send event to application */
1474                 _bt_send_event(BT_DEVICE_EVENT,
1475                                 BLUETOOTH_EVENT_GATT_SERVER_CONNECTED, /* Local device is GATT server */
1476                                 param);
1477
1478                 /* Save Connection info */
1479                 conn_info = g_new0(struct gatt_client_info_t, 1);
1480                 conn_info->addr = g_strdup(address);
1481                 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1482                 conn_info->connection_id = event->conn_id;
1483                 conn_info->instance_id = event->server_inst;
1484                 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1485                 BT_INFO("Total num of connected Remote GATT Clients [%d]", g_slist_length(gatt_client_info_list));
1486         }
1487
1488         g_free(address);
1489 }
1490
1491 /* GATT Server Dis connected */
1492 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1493 {
1494         int result = BLUETOOTH_ERROR_NONE;
1495         struct gatt_client_info_t *conn_info = NULL;
1496         bluetooth_device_address_t dev_addr;
1497         GVariant *param = NULL;
1498         char address[BT_ADDRESS_STRING_SIZE];
1499
1500         memcpy(dev_addr.addr, event->address.addr, 6);
1501
1502         /* REPLY dbus Context to application which called BT_DISCONNECT_LE. There is status
1503            in this event */
1504         _bt_convert_addr_type_to_string(address,
1505                         (unsigned char *)dev_addr.addr);
1506
1507         if (NULL ==  _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
1508                 if (NULL !=  _bt_get_request_info_data(BT_CONNECT_LE, address)) {
1509                         result = BLUETOOTH_ERROR_INTERNAL;
1510
1511                         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1512                                         address, BT_ADDRESS_STRING_SIZE);
1513
1514                         BT_ERR("Failed to connect Local GATT Server Remote Client addr[%s]", address);
1515                         return;
1516                 }
1517         } else {
1518                 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1519                                 address, BT_ADDRESS_STRING_SIZE);
1520         }
1521
1522         BT_INFO("Local GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1523                         address, event->server_inst, event->conn_id);
1524         /* Remove Connection info */
1525         conn_info = __bt_find_remote_gatt_client_info(address);
1526         if (conn_info) {
1527                 BT_INFO("No need to Send Local GATT Server Disconnected event to apps, just remove remote client info");
1528
1529                 param = g_variant_new("(is)", result, address);
1530                 /* Send event to application */
1531                 _bt_send_event(BT_DEVICE_EVENT,
1532                                 BLUETOOTH_EVENT_GATT_SERVER_DISCONNECTED, /* Local device is GATT server */
1533                                 param);
1534
1535                 /* Remove info from List */
1536                 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1537                 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1538                 g_free(conn_info->addr);
1539                 g_free(conn_info);
1540         }
1541 }
1542 #else
1543
1544 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
1545 {
1546         int result = BLUETOOTH_ERROR_NONE;
1547         struct gatt_client_info_t *conn_info = NULL;
1548         bt_service_app_info_t *info = NULL;
1549         bluetooth_device_address_t dev_addr;
1550         GVariant *param = NULL;
1551         int k;
1552
1553         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1554
1555         memcpy(dev_addr.addr, event->address.addr, 6);
1556
1557
1558         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1559         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1560                 info = &numapps[k];
1561
1562                 if (info->instance_id == event->server_inst) {
1563                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1564                         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1565                                         (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1566
1567                         break;
1568                 }
1569         }
1570
1571         /* Send event to BT-API */
1572         _bt_convert_addr_type_to_string(address,
1573                         (unsigned char *)dev_addr.addr);
1574
1575         BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1576                         address, event->server_inst, event->conn_id);
1577
1578
1579         /* Check if device is already in connected list */
1580         conn_info = __bt_find_remote_gatt_client_info(address);
1581
1582         if (!conn_info) {
1583                 param = g_variant_new("(is)", result, address);
1584                 /* Send event to application */
1585                 _bt_send_event(BT_GATT_SERVER_EVENT,
1586                                 BLUETOOTH_EVENT_GATT_CONNECTED,
1587                                 param);
1588
1589                 /* Save Connection info */
1590                 conn_info = g_new0(struct gatt_client_info_t, 1);
1591                 conn_info->addr = g_strdup(address);
1592                 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1593                 conn_info->connection_id = event->conn_id;
1594                 conn_info->instance_id = event->server_inst;
1595                 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1596                 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1597         }
1598
1599         g_free(address);
1600 }
1601
1602 /* GATT Server Dis connected */
1603 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1604 {
1605         int result = BLUETOOTH_ERROR_NONE;
1606         struct gatt_client_info_t *conn_info = NULL;
1607         bluetooth_device_address_t dev_addr;
1608         bt_service_app_info_t *info = NULL;
1609         GVariant *param = NULL;
1610         int k;
1611
1612         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1613
1614         memcpy(dev_addr.addr, event->address.addr, 6);
1615
1616         /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1617         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1618                 info = &numapps[k];
1619
1620                 if (info->instance_id == event->server_inst) {
1621                         BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1622                         __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1623                                         (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1624
1625                         break;
1626                 }
1627         }
1628
1629         /* Send event to BT-API */
1630         _bt_convert_addr_type_to_string(address,
1631                         (unsigned char *)dev_addr.addr);
1632
1633         BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1634                         address, event->server_inst, event->conn_id);
1635
1636         param = g_variant_new("(is)", result, address);
1637         /* Send event to application */
1638         _bt_send_event(BT_GATT_SERVER_EVENT,
1639                         BLUETOOTH_EVENT_GATT_DISCONNECTED,
1640                         param);
1641
1642         /* Remove Connection info */
1643         conn_info = __bt_find_remote_gatt_client_info(address);
1644         if (conn_info) {
1645                 BT_INFO("Remove GATT client info from List..");
1646                 /* Remove info from List */
1647                 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1648                 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1649                 g_free(conn_info->addr);
1650                 g_free(conn_info);
1651         }
1652
1653         g_free(address);
1654 }
1655 #endif
1656
1657
1658 static void __bt_handle_gatt_server_acquire_write_requested(event_gatts_srvc_acquire_attr_t *event)
1659 {
1660         GVariant *param = NULL;
1661         int result = BLUETOOTH_ERROR_NONE;
1662         struct gatt_server_req_info *req_info = NULL;
1663         bluetooth_device_address_t dev_addr;
1664         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1665
1666         BT_INFO("GATT Server ACQUIRE  Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1667         BT_INFO("GATT Server  ACQUIRE Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1668         BT_INFO("GATT Server ACQUIRE Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1669
1670         //address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1671         memcpy(dev_addr.addr, event->address.addr, 6);
1672
1673         req_info = g_new0(struct gatt_server_req_info, 1);
1674         req_info->request_id = event->attr_trans.trans_id;
1675         req_info->attribute_handle = event->attr_trans.attr_handle;
1676         req_info->connection_id = event->attr_trans.conn_id;
1677         req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
1678         gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1679
1680         _bt_convert_addr_type_to_string(address,
1681                         (unsigned char *)dev_addr.addr);
1682
1683         param = g_variant_new("(iiiiiis)", result,
1684                         event->attr_trans.conn_id,
1685                         event->attr_trans.trans_id,
1686                         event->attr_trans.attr_handle,
1687                         event->mtu, event->attr_trans.offset, address);
1688         BT_INFO("GATT Server ACQUIRE Write Req Attribute remote address : [%s]", address);
1689
1690         _bt_send_event(BT_GATT_SERVER_EVENT,
1691                         BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_WRITE,
1692                         param);
1693
1694 }
1695
1696 static void __bt_handle_gatt_server_acquire_notify_requested(event_gatts_srvc_acquire_attr_t *event)
1697 {
1698         GVariant *param = NULL;
1699         int result = BLUETOOTH_ERROR_NONE;
1700         struct gatt_server_req_info *req_info = NULL;
1701
1702         BT_INFO("GATT Server ACQUIRE  Notify Req Connection ID: [%d]", event->attr_trans.conn_id);
1703         BT_INFO("GATT Server ACQUIRE Notify  Req Transaction ID:[%d]", event->attr_trans.trans_id);
1704         BT_INFO("GATT Server ACQUIRE Notify Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1705
1706         req_info = g_new0(struct gatt_server_req_info, 1);
1707         req_info->request_id = event->attr_trans.trans_id;
1708         req_info->attribute_handle = event->attr_trans.attr_handle;
1709         req_info->connection_id = event->attr_trans.conn_id;
1710         req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
1711         gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1712
1713         param = g_variant_new("(iiiiii)", result,
1714                         event->attr_trans.conn_id,
1715                         event->attr_trans.trans_id,
1716                         event->attr_trans.attr_handle,
1717                         event->mtu, event->attr_trans.offset);
1718
1719         BT_INFO("GATT Server ACQUIRE Notify  Req Attribute : ");
1720
1721          _bt_send_event(BT_GATT_SERVER_EVENT,
1722                                  BLUETOOTH_EVENT_GATT_SERVER_ACQUIRE_NOTIFY,
1723                                  param);
1724 }
1725
1726 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1727 {
1728         char *address;
1729         bluetooth_device_address_t dev_addr;
1730         GVariant *param = NULL;
1731         int result = BLUETOOTH_ERROR_NONE;
1732         gboolean need_resp;
1733         gboolean is_prepare_write;
1734         char *write_val;
1735         GVariant *data = NULL;
1736         int i;
1737         struct gatt_server_req_info *req_info = NULL;
1738         BT_INFO("GATT Server Write Requested");
1739
1740         memcpy(dev_addr.addr, event->address.addr, 6);
1741
1742         BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1743         BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1744         BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1745         BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1746         BT_INFO("GATT Server Write Req value length [%d]", event->length);
1747         BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1748         BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1749
1750         if (event->length <= 0) {
1751                 BT_INFO("GATT Server write requested, but length of attr value is 0");
1752                 return;
1753         }
1754
1755         need_resp = event->need_rsp;
1756         is_prepare_write = event->is_prep;
1757
1758         write_val = g_memdup(&event->value[0], event->length);
1759
1760         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1761         _bt_convert_addr_type_to_string(address,
1762                         (unsigned char *)dev_addr.addr);
1763
1764         BT_INFO("GATT Server Write Request from remote client [%s]", address);
1765         /* DEBUG */
1766         if (event->length > 0) {
1767                 for (i = 0; i < event->length; i++)
1768                         BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1769         }
1770         /* Save Write Request Info */
1771         req_info = g_new0(struct gatt_server_req_info, 1);
1772         req_info->request_id = event->attr_trans.trans_id;
1773         req_info->attribute_handle = event->attr_trans.attr_handle;
1774         req_info->connection_id = event->attr_trans.conn_id;
1775         req_info->addr = address;
1776         req_info->offset = event->attr_trans.offset;
1777         req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1778         gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1779
1780         data = g_variant_new_from_data(
1781                         G_VARIANT_TYPE_BYTESTRING,
1782                         write_val,
1783                         event->length,
1784                         TRUE, NULL, NULL);
1785
1786         param = g_variant_new("(iiiiiibbsn@ay)", result,
1787                         event->attr_trans.conn_id,
1788                         event->attr_trans.trans_id,
1789                         event->attr_trans.attr_handle,
1790                         event->attr_trans.offset,
1791                         event->length,
1792                         need_resp,
1793                         is_prepare_write,
1794                         address,
1795                         event->length,
1796                         data);
1797
1798          _bt_send_event(BT_GATT_SERVER_EVENT,
1799                          BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1800                         param);
1801
1802         g_free(write_val);
1803 }
1804
1805 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1806 {
1807         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1808         bluetooth_device_address_t dev_addr;
1809         int result = BLUETOOTH_ERROR_NONE;
1810         struct gatt_server_req_info *req_info = NULL;
1811         GVariant *param = NULL;
1812         gboolean is_long;
1813         BT_INFO("GATT Server Read Requested");
1814
1815         memcpy(dev_addr.addr, event->address.addr, 6);
1816
1817         BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1818         BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1819         BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1820         BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1821         BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1822
1823         is_long = event->is_long;
1824
1825         /* Save Read Request Info */
1826         req_info = g_new0(struct gatt_server_req_info, 1);
1827         req_info->request_id = event->attr_trans.trans_id;
1828         req_info->attribute_handle = event->attr_trans.attr_handle;
1829         req_info->connection_id = event->attr_trans.conn_id;
1830         req_info->addr = address;
1831         req_info->offset = event->attr_trans.offset;
1832         req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1833         gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1834
1835         /* Send event to BT-API */
1836         _bt_convert_addr_type_to_string(address,
1837                         (unsigned char *)dev_addr.addr);
1838
1839         BT_INFO("GATT Server Read Request from remote client [%s]", address);
1840
1841         param = g_variant_new("(iiiiibs)", result,
1842                         event->attr_trans.conn_id,
1843                         event->attr_trans.trans_id,
1844                         event->attr_trans.attr_handle,
1845                         event->attr_trans.offset,
1846                         is_long,
1847                         address);
1848
1849         _bt_send_event(BT_GATT_SERVER_EVENT,
1850                         BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1851                         param);
1852 }
1853
1854 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1855 {
1856         bluetooth_device_address_t dev_addr;
1857         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1858         int cur_connected_clients;
1859         static int recvd = 0;
1860         gboolean completed = 0;
1861         GVariant *param = NULL;
1862
1863         /* OAL event does provide error, so MW assumes event will never contain wrong data,
1864            incase of any issues, check with OAL */
1865         int result = BLUETOOTH_ERROR_NONE;
1866
1867         memcpy(dev_addr.addr, event->address.addr, 6);
1868         _bt_convert_addr_type_to_string(address,
1869                         (unsigned char *)dev_addr.addr);
1870
1871         BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1872                         address, event->conn_id, event->trans_id, event->attr_handle);
1873
1874
1875         cur_connected_clients = g_slist_length(gatt_client_info_list);
1876         BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1877                         num_indicate_clients, cur_connected_clients);
1878
1879         recvd++;
1880         if (recvd == num_indicate_clients) {
1881                 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1882                 completed = 1; /* Last event */
1883                 recvd = 0; /* Reset */
1884                 num_indicate_clients = 0;
1885         }
1886
1887         param = g_variant_new("(isib)",
1888                         result,
1889                         address,
1890                         event->attr_handle,
1891                         completed);
1892
1893         /* Send event to BT-API */
1894         _bt_send_event(BT_GATT_SERVER_EVENT,
1895                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1896                         param);
1897
1898         BT_INFO("Received Indication confirm for client number [%d]", recvd);
1899         g_free(address);
1900 }
1901
1902 /* Tizen Platform Specific */
1903 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1904 {
1905         bluetooth_device_address_t dev_addr;
1906         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1907         GVariant *param = NULL;
1908         gboolean notify;
1909
1910         /* OAL event does provide error, so MW assumes event will never contain wrong data,
1911            incase of any issues, check with OAL */
1912         int result = BLUETOOTH_ERROR_NONE;
1913
1914         memcpy(dev_addr.addr, event->address.addr, 6);
1915         _bt_convert_addr_type_to_string(address,
1916                         (unsigned char *)dev_addr.addr);
1917
1918         BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1919                         address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1920
1921         /* Set Notifcation status */
1922         notify = event->notify;
1923
1924         param = g_variant_new("(isib)",
1925                         result,
1926                         address,
1927                         event->attr_handle,
1928                         notify);
1929
1930         /* Send event to BT-API */
1931         _bt_send_event(BT_GATT_SERVER_EVENT,
1932                         BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1933                         param);
1934
1935         g_free(address);
1936 }
1937
1938 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1939 {
1940         int result = BLUETOOTH_ERROR_NONE;
1941         struct gatt_client_info_t *conn_info = NULL;
1942         GVariant *param = NULL;
1943         guint8 status = 0;
1944         BT_INFO("GATT Server MTU changed event");
1945
1946         conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1947         if (conn_info == NULL) {
1948                 BT_ERR("Cant find connection Information");
1949                 return;
1950         }
1951         BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1952                         conn_info->addr, event->mtu_size);
1953
1954         param = g_variant_new("(isqy)",
1955                         result,
1956                         conn_info->addr,
1957                         event->mtu_size,
1958                         status);
1959
1960         /* Send event to BT-API */
1961         _bt_send_event(BT_GATT_SERVER_EVENT,
1962                         BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1963                         param);
1964 }
1965
1966 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1967 {
1968         BT_INFO("OAL event = 0x%x, \n", event_type);
1969         switch (event_type) {
1970                 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1971                 BT_INFO("OAL Event: Server Instance Registered");
1972                 /* GATT Server Registered event is handled in MAIN thread context */
1973                 __bt_handle_server_instance_registered((event_gatts_register_t *)event_data);
1974                 break;
1975                                                                 }
1976         case OAL_EVENT_GATTS_SERVICE_ADDED: {
1977                 BT_INFO("OAL Event: GATT Service added");
1978                 __bt_handle_gatt_server_service_added((event_gatts_srvc_prm_t *)event_data);
1979                 break;
1980         }
1981         case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1982                 BT_INFO("OAL Event: GATT characteristic added");
1983                 __bt_handle_gatt_server_characteristic_added((event_gatts_srvc_charctr_t *)event_data);
1984                 break;
1985         }
1986         case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1987                 BT_INFO("OAL Event: GATT descriptor added");
1988                 __bt_handle_gatt_server_descriptor_added((event_gatts_srvc_descr_t *)event_data);
1989                 break;
1990         }
1991         case OAL_EVENT_GATTS_SERVICE_STARTED: {
1992                 BT_INFO("OAL Event: GATT Service started");
1993                 __bt_handle_gatt_server_service_started((event_gatts_srvc_t *)event_data);
1994                 break;
1995         }
1996         case OAL_EVENT_GATTS_SERVICE_STOPED: {
1997                 BT_INFO("OAL Event: GATT Service stopped");
1998                 __bt_handle_gatt_server_service_stopped((event_gatts_srvc_t *)event_data);
1999                 break;
2000         }
2001         case OAL_EVENT_GATTS_SERVICE_DELETED: {
2002                 BT_INFO("OAL Event: GATT Service deleted");
2003                 __bt_handle_gatt_server_service_deleted((event_gatts_srvc_t *) event_data);
2004                 break;
2005         }
2006         case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
2007                 BT_INFO("OAL Event: GATT Server Connected");
2008                 __bt_handle_gatt_server_connection_state((event_gatts_conn_t *)event_data);
2009                 break;
2010         }
2011         case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
2012                 BT_INFO("OAL Event: GATT Server Disconnected");
2013                 __bt_handle_gatt_server_disconnection_state((event_gatts_conn_t *)event_data);
2014                 break;
2015         }
2016         case OAL_EVENT_GATTS_REQUEST_READ: {
2017                 BT_INFO("OAL Event: GATT Server Read Request");
2018                 __bt_handle_gatt_server_read_requested((event_gatts_srvc_read_attr_t *)event_data);
2019                 break;
2020         }
2021         case OAL_EVENT_GATTS_REQUEST_WRITE: {
2022                 BT_INFO("OAL Event: GATT Server Write Request");
2023                 __bt_handle_gatt_server_write_requested((event_gatts_srvc_write_attr_t *)event_data);
2024                 break;
2025         }
2026         case OAL_EVENT_GATTS_REQUEST_ACQUIRE_WRITE: {
2027                 BT_INFO("OAL Event: GATT Server Acquire  Write Request");
2028                 __bt_handle_gatt_server_acquire_write_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2029                 break;
2030         }
2031         case OAL_EVENT_GATTS_REQUEST_ACQUIRE_NOTIFY: {
2032                 BT_INFO("OAL Event: GATT ServerAcquire Notify  Request");
2033                 __bt_handle_gatt_server_acquire_notify_requested((event_gatts_srvc_acquire_attr_t*)event_data);
2034                 break;
2035         }
2036         case OAL_EVENT_GATTS_IND_CONFIRM: {
2037                 BT_INFO("OAL Event: GATT Server Indication confirmed");
2038                 __bt_handle_gatt_server_indicate_confirmed((event_gatts_ind_cnfrm_t *)event_data);
2039                 break;
2040         }
2041         case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
2042                 BT_INFO("OAL Event: GATT Server DisConnected");
2043                 __bt_handle_gatt_server_notification_changed((event_gatts_notif_t *)event_data);
2044                 break;
2045         }
2046         case OAL_EVENT_GATTS_MTU_CHANGED: {
2047                 BT_INFO("OAL Event: GATT Server MTU changed event callback");
2048                 __bt_handle_gatt_mtu_changed_event((event_gatts_mtu_changed_t *)event_data);
2049                 break;
2050         }
2051 #ifdef TIZEN_GATT_CLIENT
2052         case OAL_EVENT_GATTC_REGISTRATION: {
2053                 BT_INFO("OAL Event: GATT Client instance Registered");
2054                 __bt_handle_client_instance_registered((event_gattc_register_t *) event_data);
2055                 break;
2056         }
2057         case OAL_EVENT_GATTC_CONNECTION_COMPLETED: {
2058                 BT_INFO("OAL Event: GATT Client Connected");
2059                 __bt_handle_client_connected((event_gattc_conn_t *) event_data);
2060                 break;
2061         }
2062         case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED: {
2063                 BT_INFO("OAL Event: GATT Client DisConnected");
2064                 __bt_handle_client_disconnected((event_gattc_conn_t *) event_data);
2065                 break;
2066         }
2067         case OAL_EVENT_GATTC_SERVICE_SEARCH_RESULT: {
2068                 BT_INFO("OAL Event: GATT Client Service Search Result");
2069                 __bt_handle_client_service_search_result((event_gattc_service_result_t *) event_data);
2070                 break;
2071         }
2072         case OAL_EVENT_GATTC_SERVICE_SEARCH_DONE: {
2073                 BT_INFO("OAL Event: GATT Client Service Completed");
2074                 __bt_handle_client_service_search_completed((event_gattc_conn_status_t *) event_data);
2075                 break;
2076         }
2077         case OAL_EVENT_GATTC_CHARAC_SERACH_RESULT: {
2078                 BT_INFO("OAL Event: GATT Client Characteristic Search Result");
2079                 __bt_handle_client_characteristic_search_result((event_gattc_characteristic_result_t *) event_data);
2080                 break;
2081         }
2082         case OAL_EVENT_GATTC_DESC_SERACH_RESULT: {
2083                 BT_INFO("OAL Event: GATT Client Descriptor Search Result");
2084                 __bt_handle_client_descriptor_search_result((event_gattc_descriptor_result_t *) event_data);
2085                 break;
2086         }
2087         case OAL_EVENT_GATTC_READ_CHARAC: {
2088                 BT_INFO("OAL Event: GATT Client Characteristic Read Data");
2089                 __bt_handle_client_characteristic_read_data((event_gattc_read_data *) event_data);
2090                 break;
2091         }
2092         case OAL_EVENT_GATTC_READ_DESCR: {
2093                 BT_INFO("OAL Event: GATT Client Descriptor Read Data");
2094                 __bt_handle_client_descriptor_read_data((event_gattc_read_data *) event_data);
2095                 break;
2096         }
2097         case OAL_EVENT_GATTC_WRITE_CHARAC: {
2098                 BT_INFO("OAL Event: GATT Client Characteristic Write Data");
2099                 __bt_handle_client_characteristic_write_data((event_gattc_write_data *) event_data);
2100                 break;
2101         }
2102         case OAL_EVENT_GATTC_WRITE_DESCR: {
2103                 BT_INFO("OAL Event: GATT Client Descriptor Write Data");
2104                 __bt_handle_client_descriptor_write_data((event_gattc_write_data *) event_data);
2105                 break;
2106         }
2107         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
2108                 BT_INFO("OAL Event: LE device disconnected");
2109                 __bt_hanlde_le_device_disconnection((event_dev_conn_status_t *)event_data);
2110                 break;
2111         }
2112         case OAL_EVENT_GATTC_NOTIFICATION_REGISTERED: {
2113                 BT_INFO("OAL Event: GATT Client Notification Registered");
2114                 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, TRUE);
2115                 break;
2116         }
2117         case OAL_EVENT_GATTC_NOTIFICATION_DEREGISTERED: {
2118                 BT_INFO("OAL Event: GATT Client Notification Registered");
2119                 __bt_handle_client_notification_registered((event_gattc_regdereg_notify_t *) event_data, FALSE);
2120                 break;
2121         }
2122         case OAL_EVENT_GATTC_NOTIFY_DATA: {
2123                 BT_INFO("OAL Event: GATT Client Notification Data");
2124                 __bt_handle_client_notification_data((event_gattc_notify_data *) event_data);
2125
2126         }
2127 #endif
2128         default:
2129                 break;
2130         }
2131         BT_DBG("-");
2132 }
2133
2134 int _bt_gatt_server_add_service(char *sender, int service_type,
2135                 int num_handles, char *svc_uuid, int instance_id)
2136 {
2137         BT_CHECK_PARAMETER(svc_uuid, return);
2138         BT_CHECK_PARAMETER(sender, return);
2139         int ret = OAL_STATUS_SUCCESS;
2140
2141         oal_gatt_srvc_id_t svc_data;
2142
2143         svc_data.is_prmry = service_type;
2144         svc_data.id.inst_id = instance_id;
2145
2146         BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
2147         _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
2148
2149         ret = gatts_add_service(instance_id, &svc_data, num_handles);
2150         if (ret != OAL_STATUS_SUCCESS) {
2151                 BT_ERR("ret: %d", ret);
2152                 return BLUETOOTH_ERROR_INTERNAL;
2153         }
2154
2155         return BLUETOOTH_ERROR_NONE;
2156 }
2157
2158
2159 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
2160                 int service_handle, int included_svc_handle)
2161 {
2162         BT_CHECK_PARAMETER(sender, return);
2163         int ret = OAL_STATUS_SUCCESS;
2164
2165         ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
2166         if (ret != OAL_STATUS_SUCCESS) {
2167                 BT_ERR("ret: %d", ret);
2168                 return BLUETOOTH_ERROR_INTERNAL;
2169         }
2170         return BLUETOOTH_ERROR_NONE;
2171 }
2172
2173 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
2174                 bluetooth_gatt_server_attribute_params_t *param)
2175 {
2176         BT_CHECK_PARAMETER(char_uuid, return);
2177         BT_CHECK_PARAMETER(sender, return);
2178         BT_CHECK_PARAMETER(param, return);
2179         int ret = OAL_STATUS_SUCCESS;
2180
2181         oal_uuid_t uuid = {{0} };
2182
2183         BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
2184         _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
2185
2186         BT_INFO("Char permission From API [0x%x]", param->permissions);
2187
2188         ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
2189                         param->properties, (int)param->permissions);
2190         if (ret != OAL_STATUS_SUCCESS) {
2191                 BT_ERR("ret: %d", ret);
2192                 return BLUETOOTH_ERROR_INTERNAL;
2193         }
2194         return BLUETOOTH_ERROR_NONE;
2195 }
2196
2197 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
2198                 bt_gatt_permission_t *param, int service_handle, int instance_id)
2199 {
2200         BT_CHECK_PARAMETER(desc_uuid, return);
2201         BT_CHECK_PARAMETER(sender, return);
2202         BT_CHECK_PARAMETER(param, return);
2203         int ret = OAL_STATUS_SUCCESS;
2204
2205         oal_uuid_t uuid = {{0} };
2206
2207         BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
2208                         desc_uuid, service_handle, instance_id);
2209
2210         _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
2211
2212         BT_INFO("Descriptor permission From API [0x%x]", *param);
2213         ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
2214
2215         if (ret != OAL_STATUS_SUCCESS) {
2216                 BT_ERR("ret: %d", ret);
2217                 return BLUETOOTH_ERROR_INTERNAL;
2218         }
2219         return BLUETOOTH_ERROR_NONE;
2220 }
2221
2222 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
2223 {
2224         BT_CHECK_PARAMETER(sender, return);
2225         int ret = OAL_STATUS_SUCCESS;
2226
2227         ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
2228         if (ret != OAL_STATUS_SUCCESS) {
2229                 BT_ERR("ret: %d", ret);
2230                 return BLUETOOTH_ERROR_INTERNAL;
2231         }
2232         return BLUETOOTH_ERROR_NONE;
2233 }
2234
2235 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
2236 {
2237         BT_CHECK_PARAMETER(sender, return);
2238         int ret = OAL_STATUS_SUCCESS;
2239
2240         ret = gatts_stop_service(instance_id, service_handle);
2241         if (ret != OAL_STATUS_SUCCESS) {
2242                 BT_ERR("ret: %d", ret);
2243                 return BLUETOOTH_ERROR_INTERNAL;
2244         }
2245         return BLUETOOTH_ERROR_NONE;
2246 }
2247
2248 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
2249 {
2250         BT_CHECK_PARAMETER(sender, return);
2251         int ret = OAL_STATUS_SUCCESS;
2252         GSList *l;
2253         int *handle = NULL;
2254
2255         ret = gatts_delete_service(instance_id, service_handle);
2256         if (ret != OAL_STATUS_SUCCESS) {
2257                 BT_ERR("ret: %d", ret);
2258                 return BLUETOOTH_ERROR_INTERNAL;
2259         }
2260
2261         /* Remove the Service Handle */
2262         for (l = numapps[instance_id].service_handles; l != NULL;) {
2263                 handle = l->data;
2264                 l = g_slist_next(l);
2265                 if (handle && *handle == service_handle) {
2266                         BT_INFO("Remove Service handle [%d]", *handle);
2267                         numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
2268                         g_free(handle);
2269                         handle = NULL;
2270                 }
2271         }
2272
2273         return BLUETOOTH_ERROR_NONE;
2274 }
2275
2276 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
2277                 bluetooth_gatt_server_response_params_t *param)
2278 {
2279         BT_CHECK_PARAMETER(sender, return);
2280         BT_CHECK_PARAMETER(data, return);
2281         BT_CHECK_PARAMETER(param, return);
2282         struct gatt_server_req_info *req_info = NULL;
2283         int ret = OAL_STATUS_SUCCESS;
2284
2285         oal_gatt_response_t response;
2286
2287         BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
2288                         param->req_type, param->request_id,
2289                         param->response_status, param->auth_req,
2290                         data->offset, data->length);
2291
2292         /* Search for matching Request in List */
2293         req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2294         if (!req_info) {
2295                 BT_ERR("GATT Server Req Info not found for current response..return Error");
2296                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2297         }
2298
2299         memset(&response, 0x00, sizeof(oal_gatt_response_t));
2300
2301         response.handle = req_info->attribute_handle;
2302         response.attr_value.auth_req = param->auth_req;
2303         response.attr_value.handle = req_info->attribute_handle;
2304         response.attr_value.offset = data->offset;
2305         response.attr_value.len = data->length;
2306         memcpy(&response.attr_value.value, &data->data, data->length);
2307
2308
2309         ret = gatts_send_response(req_info->connection_id, param->request_id,
2310                         param->response_status, &response);
2311
2312         if (ret != OAL_STATUS_SUCCESS) {
2313                 BT_ERR("ret: %d", ret);
2314                 return BLUETOOTH_ERROR_INTERNAL;
2315         }
2316
2317         BT_INFO("GATT Server Response successfully sent");
2318         /* Remove GATT server request from list */
2319         gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2320         g_free(req_info->addr);
2321
2322         return BLUETOOTH_ERROR_NONE;
2323 }
2324
2325 int _bt_gatt_server_acquire_send_response(char *sender, bluetooth_gatt_server_acquire_response_params_t *param ,        void *fd_list)
2326 {
2327         BT_CHECK_PARAMETER(sender, return);
2328         BT_CHECK_PARAMETER(param, return);
2329         struct gatt_server_req_info *req_info = NULL;
2330         int ret = OAL_STATUS_SUCCESS;
2331
2332
2333         BT_INFO("GATT acquire Server Response: Req Type [%d] req_id [%d] fd  [%d] mtu[%d]",
2334                         param->req_type, param->request_id,
2335                          param->fd,
2336                         param->mtu);
2337
2338         /* Search for matching Request in List */
2339         req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
2340         if (!req_info) {
2341                 BT_ERR("GATT acquire Server Req Info not found for current response..return Error");
2342                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2343         }
2344
2345         ret = gatt_send_response_acquire(req_info->connection_id, param->request_id, 0, param->fd, param->mtu, fd_list);
2346
2347         if (ret != OAL_STATUS_SUCCESS) {
2348                 BT_ERR("ret: %d", ret);
2349                 return BLUETOOTH_ERROR_INTERNAL;
2350         }
2351
2352         BT_INFO("GATT acquire Server Response successfully sent");
2353         /* Remove GATT server request from list */
2354         gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
2355         g_free(req_info->addr);
2356
2357         return BLUETOOTH_ERROR_NONE;
2358 }
2359
2360
2361
2362 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
2363                 bluetooth_gatt_att_data_t *data,
2364                 bluetooth_gatt_server_indication_params_t *param)
2365 {
2366         BT_CHECK_PARAMETER(sender, return);
2367         BT_CHECK_PARAMETER(data, return);
2368         BT_CHECK_PARAMETER(param, return);
2369         char *address;
2370         gboolean all_send = FALSE;
2371         int ret = OAL_STATUS_SUCCESS;
2372         struct gatt_client_info_t *conn;
2373
2374         address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2375         _bt_convert_addr_type_to_string(address, dev_addr->addr);
2376
2377         if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
2378                 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
2379                 all_send = TRUE;
2380         } else {
2381                 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
2382         }
2383
2384         /* Attempt to send Notification/Indication to all Connected GATT clients */
2385         if (all_send) {
2386                 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
2387                 if (ret != OAL_STATUS_SUCCESS) {
2388                         BT_ERR("ret: %d", ret);
2389                         g_free(address);
2390                         return BLUETOOTH_ERROR_INTERNAL;
2391                 }
2392
2393         } else {
2394                 conn = __bt_find_remote_gatt_client_info(address);
2395                 if (conn) {
2396                         ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
2397                                         conn->connection_id, data->length,
2398                                         param->need_confirmation, (char *)(&data->data[0]));
2399
2400                         if (ret != OAL_STATUS_SUCCESS) {
2401                                 BT_ERR("ret: %d", ret);
2402                                 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
2403                                 g_free(address);
2404                                 return BLUETOOTH_ERROR_INTERNAL;
2405                         }
2406                         BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
2407                         g_free(address);
2408                         num_indicate_clients = 1;
2409                         return BLUETOOTH_ERROR_NONE;
2410                 }
2411                 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
2412                 g_free(address);
2413                 return BLUETOOTH_ERROR_NOT_CONNECTED;
2414         }
2415         g_free(address);
2416         return BLUETOOTH_ERROR_NONE;
2417 }
2418
2419 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
2420                 bluetooth_gatt_server_update_value_t *param)
2421 {
2422         BT_CHECK_PARAMETER(sender, return);
2423         BT_CHECK_PARAMETER(param, return);
2424         int ret = OAL_STATUS_SUCCESS;
2425
2426         oal_gatt_value_t value;
2427         BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
2428                         instance_id, param->attribute_handle, param->length);
2429
2430
2431         memset(&value, 0x00, sizeof(oal_gatt_value_t));
2432
2433         value.handle = param->attribute_handle;
2434         value.len = param->length;
2435         memcpy(&value.value, &param->data.data, param->length);
2436
2437         ret = gatts_update_att_value(instance_id, &value);
2438
2439         if (ret != OAL_STATUS_SUCCESS) {
2440                 BT_ERR("ret: %d", ret);
2441                 return BLUETOOTH_ERROR_INTERNAL;
2442         }
2443
2444         BT_INFO("GATT Server Update Attribute Value successfully");
2445         return BLUETOOTH_ERROR_NONE;
2446 }
2447
2448 int _bt_get_att_mtu(bluetooth_device_address_t *address,
2449                 unsigned int *mtu)
2450 {
2451         BT_CHECK_PARAMETER(address, return);
2452         BT_CHECK_PARAMETER(mtu, return);
2453         struct gatt_client_info_t *conn_info = NULL;
2454         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
2455         int ret = OAL_STATUS_SUCCESS;
2456         int stack_mtu;
2457
2458         _bt_convert_addr_type_to_string(addr, address->addr);
2459
2460         BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
2461
2462         conn_info = __bt_find_remote_gatt_client_info(addr);
2463         if (conn_info) {
2464                 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
2465                                 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
2466         } else {
2467                 BT_ERR("GATT Client [%s] is not yet connected..");
2468                 return BLUETOOTH_ERROR_NOT_CONNECTED;
2469         }
2470
2471         ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
2472         if (ret != OAL_STATUS_SUCCESS) {
2473                 BT_ERR("ret: %d", ret);
2474                 return BLUETOOTH_ERROR_INTERNAL;
2475         }
2476         BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
2477         *mtu = (unsigned int)stack_mtu;
2478         return BLUETOOTH_ERROR_NONE;
2479 }
2480
2481 #ifdef TIZEN_GATT_CLIENT
2482 /* GATT Client utility static functions */
2483 static bt_gatt_service_info_list_t * __bt_get_service_info_list(int conn_id)
2484 {
2485         GSList *l;
2486         bt_gatt_service_info_list_t *info = NULL;
2487
2488         for (l = list_gatt_info; l != NULL; l = g_slist_next(l)) {
2489                 info = (bt_gatt_service_info_list_t*)l->data;
2490                 if (info == NULL)
2491                         continue;
2492
2493                 if (info->conn_id == conn_id)
2494                         return info;
2495
2496         }
2497         return NULL;
2498 }
2499
2500 static bt_gatt_service_info_t* __bt_find_matching_service(
2501                 bt_gatt_service_info_list_t *svc_list, oal_gatt_srvc_id_t *svc)
2502 {
2503         GSList *l;
2504         bt_gatt_service_info_t *info = NULL;
2505
2506         for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2507                 info = (bt_gatt_service_info_t*)l->data;
2508                 if (info == NULL)
2509                         continue;
2510
2511                 /* Match UUID and instance ID */
2512                 if (!memcmp(&svc->id.uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2513                                 && (svc->id.inst_id == info->inst_id)) {
2514                         return info;
2515                 }
2516         }
2517         return NULL;
2518 }
2519
2520 static bt_gatt_char_info_t* __bt_find_matching_charc(
2521                 bt_gatt_service_info_t *svc_info, oal_gatt_id_t *charc)
2522 {
2523         GSList *l;
2524         bt_gatt_char_info_t *info = NULL;
2525
2526         for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2527                 info = (bt_gatt_char_info_t*)l->data;
2528                 if (info == NULL)
2529                         continue;
2530
2531                 /* Match UUID and instance ID */
2532                 if (!memcmp(&charc->uuid.uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2533                                 && (charc->inst_id == info->inst_id)) {
2534                         return info;
2535                 }
2536         }
2537         return NULL;
2538 }
2539
2540 static bt_gatt_descriptor_info_t* __bt_find_matching_desc(
2541                 bt_gatt_char_info_t *char_info, oal_gatt_id_t *desc)
2542 {
2543         GSList *l;
2544         bt_gatt_descriptor_info_t *info = NULL;
2545
2546         for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2547                 info = (bt_gatt_descriptor_info_t*)l->data;
2548                 if (info == NULL)
2549                         continue;
2550
2551                 /* Match UUID and instance ID */
2552                 if (!memcmp(&desc->uuid, &info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
2553                                 && (desc->inst_id == info->inst_id)) {
2554                         return info;
2555                 }
2556         }
2557         return NULL;
2558 }
2559
2560
2561 static struct gatt_server_info_t *__bt_find_remote_gatt_server_info_from_conn_id(int conn_id)
2562 {
2563         GSList *l;
2564         struct gatt_server_info_t *info = NULL;
2565
2566         for (l = gatt_server_info_list; l != NULL; l = g_slist_next(l)) {
2567                 info = (struct gatt_server_info_t*)l->data;
2568                 if (info == NULL)
2569                         continue;
2570
2571                 if (info->connection_id == conn_id) {
2572                         BT_INFO("Remote GATT server found addr[%s]", info->addr);
2573                         return info;
2574                 }
2575         }
2576         return NULL;
2577 }
2578
2579 static bt_gatt_service_info_t* __bt_find_removed_service(bt_gatt_service_info_list_t *svc_list)
2580 {
2581         GSList *l;
2582         bt_gatt_service_info_t *info = NULL;
2583
2584         for (l = svc_list->services; l != NULL; l = g_slist_next(l)) {
2585                 info = (bt_gatt_service_info_t*)l->data;
2586                 if (info == NULL)
2587                         continue;
2588
2589                 /* Service is marked a removed */
2590                 if (info->is_removed == 1)
2591                         return info;
2592         }
2593         return NULL;
2594 }
2595
2596 static void __bt_remove_service_info_from_list(bt_gatt_service_info_t *svc_info)
2597 {
2598 #if 0
2599         GSList *l;
2600         GSList *l1;
2601         GSList *l2;
2602         bt_gatt_char_info_t *charc = NULL;
2603         bt_gatt_included_service_info_t *incl = NULL;
2604         bt_gatt_descriptor_info_t *desc = NULL;
2605
2606         /* Remove all Characteristic and Descriptors within characteristic */
2607         for (l = svc_info->chars; l != NULL;) {
2608                 charc = (bt_gatt_char_info_t*)l->data;
2609                 l = g_slist_next(l); /* Incase if l is removed, saving next to l */
2610
2611                 if (charc == NULL)
2612                         continue;
2613
2614                 /* Inside Characteristic */
2615                 for (l1 = charc->descs; l1 != NULL;) {
2616
2617                         desc = (bt_gatt_descriptor_info_t*)l1->data;
2618                         l1 = g_slist_next(l1);
2619
2620                         if (desc == NULL)
2621                                 continue;
2622
2623                         /* Remove Descriptor */
2624                         charc->descs = g_slist_remove(charc->descs, desc);
2625                         g_free(desc);
2626                 }
2627                 /* Remove Characteristic */
2628                 svc_info->chars = g_slist_remove(svc_info->chars, charc);
2629                 g_free(charc);
2630         }
2631
2632         /* Remove all Included Services */
2633         for (l2 = svc_info->included_svcs; l2 != NULL;) {
2634                 incl = (bt_gatt_included_service_info_t*)l2->data;
2635                 l2 = g_slist_next(l2); /* Incase if l is removed, saving next to l */
2636
2637                 if (incl == NULL)
2638                         continue;
2639
2640                 /* Remove included service */
2641                 svc_info->included_svcs = g_slist_remove(svc_info->included_svcs, incl);
2642                 g_free(incl);
2643         }
2644 #endif
2645 }
2646
2647
2648 static void __bt_build_service_browse_info(int conn_id,
2649                 bt_services_browse_info_t* info)
2650 {
2651         GSList *l;
2652         bt_gatt_service_info_list_t *svc_info_list;
2653         bt_gatt_service_info_t *svc_info;
2654
2655         service_uuid_t uuid;
2656         struct gatt_server_info_t *conn_info = NULL;
2657         int count = 0;
2658         char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2659         BT_INFO("+");
2660
2661         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2662
2663         if (!conn_info)
2664                 return;
2665
2666         _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2667
2668         svc_info_list = __bt_get_service_info_list(conn_id);
2669
2670         if (!svc_info_list)
2671                 return;
2672
2673         info->count = g_slist_length(svc_info_list->services);
2674         BT_INFO("Total services present in the svc info list for this conn id [%d] is [%d]",
2675                         conn_id, info->count);
2676
2677         for (l = svc_info_list->services; l != NULL; l = g_slist_next(l)) {
2678                 svc_info = (bt_gatt_service_info_t*)l->data;
2679                 if (svc_info == NULL)
2680                         continue;
2681
2682                 memcpy(&uuid.uuid, &svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2683                 _bt_uuid_to_string(&uuid, uuid_string);
2684
2685                 BT_INFO("Service UUID formed [%s] strlen [%d]", uuid_string, strlen(uuid_string));
2686
2687                 /* Fill UUID of service */
2688                 g_strlcpy(info->uuids[count], uuid_string,
2689                                 BLUETOOTH_UUID_STRING_MAX);
2690
2691                 BT_INFO("Service UUID formed  TO be sent [%s] strlen [%d]",
2692                                 info->uuids[count], strlen(info->uuids[count]));
2693                 /* Fill instance ID of service */
2694                 info->inst_id[count] = svc_info->inst_id;
2695
2696                 /* Fill primary service or not info */
2697                 info->primary[count] = svc_info->is_primary;
2698
2699                 /* Increment count of services browsed */
2700                 count++;
2701         }
2702
2703         BT_INFO("Total services browsed [%d]", count);
2704 }
2705
2706 static void __bt_build_char_browse_info(int conn_id,
2707                 bt_gatt_service_info_t *svc_info,
2708                 bt_char_browse_info_t* info)
2709 {
2710         GSList *l;
2711         bt_gatt_char_info_t *char_info;
2712         service_uuid_t uuid;
2713
2714         struct gatt_server_info_t *conn_info = NULL;
2715         int count = 0;
2716         char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2717
2718         BT_INFO("+");
2719
2720         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2721         if (!conn_info)
2722                 return;
2723
2724         /* Fill default data, this will be required even in case of failure */
2725         _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2726         memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2727         info->svc_inst_id = svc_info->inst_id;
2728
2729         if (!svc_info->chars) {
2730                 BT_ERR("No Chars browsed for address [%s]", conn_info->addr);
2731                 return;
2732         }
2733
2734         info->count = g_slist_length(svc_info->chars);
2735         BT_INFO("Total count of Characteristics [%d]", info->count);
2736
2737         for (l = svc_info->chars; l != NULL; l = g_slist_next(l)) {
2738                 char_info = (bt_gatt_char_info_t*)l->data;
2739                 if (char_info == NULL)
2740                         continue;
2741
2742                 memcpy(&uuid.uuid, &char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2743                 _bt_uuid_to_string(&uuid, uuid_string);
2744
2745                 /* Fill UUID of characteristic */
2746                 g_strlcpy(info->uuids[count], uuid_string,
2747                                 BLUETOOTH_UUID_STRING_MAX);
2748
2749                 /* Fill instance ID of characteristic */
2750                 info->inst_id[count] = char_info->inst_id;
2751
2752                 /* Fill property of characteristic */
2753                 info->props[count] = char_info->props;
2754
2755                 /* Increment count of services browsed */
2756                 count++;
2757         }
2758         BT_INFO("Total characteristics browsed [%d]", count);
2759 }
2760
2761 static void __bt_build_descriptor_browse_info(int conn_id,
2762                 bt_gatt_service_info_t *svc_info,
2763                 bt_gatt_char_info_t *char_info,
2764                 bt_descriptor_browse_info_t* info)
2765 {
2766         GSList *l;
2767         bt_gatt_descriptor_info_t *desc_info;
2768
2769         service_uuid_t uuid;
2770         struct gatt_server_info_t *conn_info = NULL;
2771         int count = 0;
2772         char uuid_string[BLUETOOTH_UUID_STRING_MAX];
2773
2774         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(conn_id);
2775
2776         /* Fill default data, this will be required even in case of failure */
2777         _bt_convert_addr_string_to_type(info->device_addr.addr, conn_info->addr);
2778         memcpy(&info->svc_uuid, svc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2779         info->svc_inst_id = svc_info->inst_id;
2780         memcpy(&info->char_uuid, char_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2781         info->char_inst_id = char_info->inst_id;
2782
2783         /* Fill property of the parent characteristic of this descriptor */
2784         info->char_props_map = char_info->props;
2785
2786         info->count = g_slist_length(char_info->descs);
2787         BT_INFO("Total count of Descriptors [%d]", info->count);
2788
2789         if (!char_info->descs) {
2790                 BT_ERR("No Descriptors browsed for address [%s]", conn_info->addr);
2791                 return;
2792         }
2793
2794         for (l = char_info->descs; l != NULL; l = g_slist_next(l)) {
2795                 desc_info = (bt_gatt_descriptor_info_t*)l->data;
2796                 if (desc_info == NULL)
2797                         continue;
2798
2799                 memcpy(&uuid.uuid, &desc_info->uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
2800                 _bt_uuid_to_string(&uuid, uuid_string);
2801
2802                 /* Fill UUID of Descriptor */
2803                 g_strlcpy(info->uuids[count], uuid_string,
2804                                 BLUETOOTH_UUID_STRING_MAX);
2805
2806                 /* Fill instance ID of Descriptor */
2807                 info->inst_id[count] = desc_info->inst_id;
2808
2809
2810                 /* Increment count of Descriptor browsed */
2811                 count++;
2812         }
2813
2814         BT_INFO("Total descriptors browsed [%d]", count);
2815 }
2816
2817
2818 static void __bt_cleanup_remote_services(struct gatt_server_info_t *conn_info)
2819 {
2820         GSList *l;
2821         GSList *ll;
2822         GSList *lll;
2823         GSList *llll;
2824
2825         bt_gatt_service_info_list_t * svc_info_list = NULL;
2826         bt_gatt_service_info_t *svc = NULL;
2827         bt_gatt_char_info_t *chr = NULL;
2828         bt_gatt_descriptor_info_t *desc = NULL;
2829         bt_gatt_included_service_info_t *incl_svc = NULL;
2830
2831         BT_INFO("Start Cleanup of all services");
2832
2833         svc_info_list = __bt_get_service_info_list(conn_info->connection_id);
2834         if (!svc_info_list) {
2835                 BT_INFO("Could not find Svc Info list for the connection ID [%d]",
2836                                 conn_info->connection_id);
2837                 return;
2838         }
2839
2840         BT_INFO("Num Services [%d]", g_slist_length(svc_info_list->services));
2841         for (l = svc_info_list->services; l;) {
2842                 svc = (bt_gatt_service_info_t*)l->data;
2843                 l = g_slist_next(l);
2844                 if (svc == NULL)
2845                         continue;
2846
2847                 BT_INFO("Service info Is Prim[%d] Inst ID [%d]",
2848                                 svc->is_primary, svc->inst_id);
2849                 BT_INFO("Num chars [%d]", g_slist_length(svc->chars));
2850                 /* Delete all chars and its descriptors */
2851                 for (ll = svc->chars; ll;) {
2852                         chr = (bt_gatt_char_info_t*)ll->data;
2853                         ll = g_slist_next(ll);
2854                         if (chr == NULL)
2855                                 continue;
2856
2857                         BT_INFO("Num descs [%d]", g_slist_length(chr->descs));
2858                         for (lll = chr->descs; lll;) {
2859                                 desc = (bt_gatt_descriptor_info_t *)lll->data;
2860                                 lll = g_slist_next(lll);
2861                                 if (desc == NULL)
2862                                         continue;
2863                                 chr->descs = g_slist_remove(chr->descs, desc);
2864                                 g_free(desc);
2865                         }
2866                         svc->chars = g_slist_remove(svc->chars, chr);
2867                         g_free(chr);
2868                 }
2869
2870                 BT_INFO("Num incl svcs [%d]", g_slist_length(svc->included_svcs));
2871                 /* Delete all included services */
2872                 for (llll = svc->included_svcs; llll;) {
2873                         incl_svc = (bt_gatt_included_service_info_t*)llll->data;
2874                         llll = g_slist_next(llll);
2875                         if (incl_svc == NULL)
2876                                 continue;
2877
2878                         svc->included_svcs = g_slist_remove(svc->included_svcs, incl_svc);
2879                         g_free(incl_svc);
2880                 }
2881                 svc_info_list->services = g_slist_remove(svc_info_list->services, svc);
2882                 g_free(svc);
2883         }
2884         g_free(svc_info_list);
2885         BT_INFO("Cleanup of all services done");
2886 }
2887
2888 int _bt_register_gatt_client_instance(const char *sender,
2889                 bluetooth_device_address_t *address)
2890 {
2891         int ret = OAL_STATUS_SUCCESS;
2892         char *uuid_string = NULL;
2893         int slot = -1;
2894         int k;
2895         oal_uuid_t uuid;
2896
2897         /* App should ensure that it should not send */
2898         BT_INFO("###Check on which instance GATT Client instance can be initialized....");
2899         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2900                 if (numapps[k].is_initialized == 1) {
2901                         BT_INFO("Instance ID [%d] is already in use..Check next slot",
2902                                         numapps[k].instance_id);
2903                 } else {
2904                         slot = k;
2905                         BT_INFO("Time to register GATT client instancer..UUID to be used is [%s] slot [%d]",
2906                                         uuid_list[slot-1], slot);
2907                         break;
2908                 }
2909         }
2910
2911         if (slot == -1) {
2912                 BT_ERR("No Slot if free for GATT Client registration..");
2913                 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
2914         }
2915
2916         uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2917         _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
2918         g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
2919         BT_INFO("Copied UUID string [%s]", uuid_string);
2920
2921         /* Register GATT Client */
2922         ret = gattc_register(&uuid);
2923         if (ret != OAL_STATUS_SUCCESS) {
2924                 BT_ERR("ret: %d", ret);
2925                 g_free(uuid_string);
2926                 return BLUETOOTH_ERROR_INTERNAL;
2927         }
2928
2929         BT_INFO("GATT Client registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
2930
2931         /* Return & wait for GATT Client Instance Initialization event */
2932         memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
2933         memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
2934
2935         g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
2936         g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
2937
2938         /* Address is saved here. When event comes, sender + address are matched for replying pending
2939            request. It is impossible for same sender to have requests with two same addresses */
2940         memcpy(&numapps[slot].address.addr, address->addr, sizeof(bluetooth_device_address_t));
2941
2942         BT_INFO("Slot [%d] occupied", slot);
2943         numapps[slot].is_initialized = TRUE; /* Set initialization to true here itself */
2944
2945         g_free(uuid_string);
2946         return BLUETOOTH_ERROR_NONE;
2947
2948 }
2949
2950
2951
2952 /* GATT client events */
2953 static void __bt_handle_client_instance_registered(event_gattc_register_t *data)
2954 {
2955         bt_service_app_info_t *info = NULL;
2956         int k;
2957         char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
2958
2959         _bt_uuid_to_string(&(data->client_uuid), uuid_string);
2960         BT_INFO("CLient ID is Initialized [%d] UUID initialized [%s]", data->client_if, uuid_string);
2961
2962         /* Platform GATT client framwork does not use Default GATT client instance
2963            This GATT client instance is never deregistred in the lifetime of bt-service */
2964         BT_INFO("Default UUID [%s] current registered uuid [%s]",
2965                         DEFAULT_GATT_CLIENT_UUID, uuid_string);
2966         if (g_strcmp0(uuid_string, DEFAULT_GATT_CLIENT_UUID) == 0) {
2967                 BT_INFO("Default client Instance Registered [%s] Client instance [%d]",
2968                                 uuid_string, data->client_if);
2969                 gatt_default_client = data->client_if;
2970                 g_free(uuid_string);
2971                 return;
2972         }
2973
2974         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
2975                 info = &numapps[k];
2976
2977                 if (g_strcmp0(info->uuid, uuid_string) == 0) {
2978                         BT_INFO("Found GATT client.. UUID [%s], sender [%s]", info->uuid, info->sender);
2979                         BT_INFO("Slot [%d] occupied", k);
2980                         info->is_initialized = TRUE;
2981                         info->client_id = data->client_if;
2982                         __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_CLIENT_REGISTER,
2983                                         (void*)info, sizeof(bt_service_app_info_t));
2984                         break;
2985                 }
2986         }
2987         g_free(uuid_string);
2988 }
2989
2990 static void __bt_handle_client_connected(event_gattc_conn_t *event_data)
2991 {
2992         int result = BLUETOOTH_ERROR_NONE;
2993         struct gatt_server_info_t *conn_info = NULL;
2994         struct gatt_out_conn_info_t *out_conn_info = NULL;
2995
2996         GVariant *param = NULL;
2997
2998         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
2999         _bt_convert_addr_type_to_string(address,
3000                         (unsigned char *)event_data->address.addr);
3001
3002         if (event_data->status != OAL_STATUS_SUCCESS)
3003                 result = BLUETOOTH_ERROR_INTERNAL;
3004
3005         /* DBUS Return fo BT_CONNECT_LE for all the apps */
3006         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3007                         BT_ADDRESS_STRING_SIZE);
3008
3009         BT_INFO("Local GATT Client Connected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status[%d]",
3010                         address, event_data->client_if, event_data->conn_id, event_data->status);
3011
3012         if (result == BLUETOOTH_ERROR_NONE) {
3013                 /* Check if device is already in connected list */
3014                 conn_info = __bt_find_remote_gatt_server_info(address);
3015
3016                 if (!conn_info) {
3017                         /* Send event to BT-API */
3018                         param = g_variant_new("(is)", result, address);
3019                         _bt_send_event(BT_DEVICE_EVENT,
3020                                         BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED, /* Local device is GATT client */
3021                                         param);
3022
3023                         /* Save Connection info */
3024                         conn_info = g_new0(struct gatt_server_info_t, 1);
3025                         conn_info->addr = g_strdup(address);
3026                         conn_info->client_id = event_data->client_if;
3027                         BT_INFO("Added GATT server addr[%s]", conn_info->addr);
3028                         conn_info->connection_id = event_data->conn_id;
3029                         gatt_server_info_list = g_slist_append(gatt_server_info_list, conn_info);
3030                         BT_INFO("Total num of connected Remote GATT server devices [%d]",
3031                                         g_slist_length(gatt_server_info_list));
3032
3033 #if 0
3034                         BT_INFO("Do a Internal refresh");
3035                         if (OAL_STATUS_SUCCESS != gattc_refresh(conn_info->client_id, &event_data->address))
3036                                 BT_ERR("GATT database refresh failed!!");
3037                         else
3038                                 BT_INFO("GATT database refresh Success!!");
3039 #endif
3040                 } else
3041                         BT_ERR("Local GATT Client connected event for addr[%s], but device is in connected list already", address);
3042         } else
3043                 BT_ERR("GATT Client Connection failed!!");
3044
3045         /* If outgoing connection Info is present, then remove it */
3046         out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3047         if (out_conn_info) {
3048                 BT_ERR("Outgoing Client connect request was sent");
3049                 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3050                 g_free(out_conn_info->addr);
3051                 g_free(out_conn_info);
3052         }
3053         g_free(address);
3054 }
3055
3056 static void __bt_handle_client_disconnected(event_gattc_conn_t *event_data)
3057 {
3058         int result = BLUETOOTH_ERROR_NONE;
3059
3060         struct gatt_server_info_t *conn_info = NULL;
3061         struct gatt_out_conn_info_t *out_conn_info = NULL;
3062
3063         GVariant *param = NULL;
3064
3065         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3066         _bt_convert_addr_type_to_string(address,
3067                         (unsigned char *)event_data->address.addr);
3068
3069         if (event_data->status != OAL_STATUS_SUCCESS)
3070                 result = BLUETOOTH_ERROR_INTERNAL;
3071
3072         if (NULL ==  _bt_get_request_info_data(BT_DISCONNECT_LE, address)) {
3073                 if (NULL !=  _bt_get_request_info_data(BT_CONNECT_LE, address)) {
3074                         result = BLUETOOTH_ERROR_INTERNAL;
3075                         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
3076                                         address, BT_ADDRESS_STRING_SIZE);
3077                         BT_ERR("Failed to connect Local GATT Remote addr[%s]", address);
3078                         g_free(address);
3079                         return;
3080                 }
3081         } else {
3082                 /* DBUS Return for BT_DISCONNECT_LE for all the apps */
3083                 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE, address,
3084                                 BT_ADDRESS_STRING_SIZE);
3085         }
3086         BT_INFO("Local GATT Client DisConnected: Remote addr[%s] Client Interface [%d] Connection ID [%d] status [%d]",
3087                         address, event_data->client_if, event_data->conn_id, event_data->status);
3088
3089         /* Remove Connection info */
3090         conn_info = __bt_find_remote_gatt_server_info(address);
3091
3092         if (conn_info) {
3093                 param = g_variant_new("(is)", result, address);
3094                 /* Send event to application */
3095                 _bt_send_event(BT_DEVICE_EVENT,
3096                                 BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED,
3097                                 param);
3098
3099                 BT_INFO("Remove GATT server info from List..");
3100                 /* Remove all services from info list_gatt_info */
3101                 __bt_cleanup_remote_services(conn_info);
3102
3103                 /* Remove info from List */
3104                 gatt_server_info_list = g_slist_remove(gatt_server_info_list, conn_info);
3105                 BT_INFO("Total num of connected GATT servers [%d]", g_slist_length(gatt_server_info_list));
3106                 g_free(conn_info->addr);
3107                 g_free(conn_info);
3108         } else
3109                 BT_INFO("Can not find conn info, already removed!");
3110
3111         /* If outgoing connection Info is present, then remove it */
3112         out_conn_info = __bt_find_gatt_outgoing_conn_info(address);
3113         if (out_conn_info) {
3114                 BT_ERR("Client Disconnected event, but outgoing connect request was sent");
3115                 outgoing_gatt_conn_list = g_slist_remove(outgoing_gatt_conn_list, out_conn_info);
3116                 g_free(out_conn_info->addr);
3117                 g_free(out_conn_info);
3118         }
3119         g_free(address);
3120 }
3121
3122
3123 static void __bt_handle_client_service_search_result(
3124                 event_gattc_service_result_t *event_data)
3125 {
3126         BT_INFO("+");
3127         BT_INFO("received the gatt service search result");
3128         /* Pre: status is never fail from OAL */
3129
3130         /* Find service list from address */
3131         bt_gatt_service_info_list_t *svc_info_list;
3132         bt_gatt_service_info_t *svc_info;
3133         BT_INFO("Search Result: status [%d] conn_id [%d]",
3134                         event_data->conn_status.status,
3135                         event_data->conn_status.conn_id);
3136
3137         svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3138         if (!svc_info_list) {
3139                 BT_INFO("Service info list not present for this connection ID, means first time browse");
3140                 /* Means for this conn_id, no services are ever browsed, first time,
3141                    create service info list for this conn_id */
3142                 svc_info_list = g_malloc0(sizeof(bt_gatt_service_info_list_t));
3143                 svc_info_list->conn_id = event_data->conn_status.conn_id;
3144                 list_gatt_info = g_slist_append(list_gatt_info, svc_info_list);
3145         } else {
3146                 BT_INFO("Service info list Already present for this connection ID, means not first time browse for this conn ID ");
3147         }
3148
3149         /* send list and current service's uuid and instance id to find it  */
3150         svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3151
3152
3153         /* If not found, check if service changed, if yes, means this is a new service added
3154            in remote GATT device, update uuid info in svc info list structure, to be used when
3155            search is completed */
3156         if (!svc_info) {
3157                 BT_INFO("Service Not found in svc info list for this connection ID");
3158                 if (svc_info_list->info.is_changed) {
3159                         BT_INFO("Service Changed indication already found for this connection ID");
3160                         memcpy(svc_info_list->info.uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3161                 }
3162                 /* Create and add new service in service list */
3163                 svc_info = g_malloc0(sizeof(bt_gatt_service_info_t));
3164                 memcpy(svc_info->uuid, event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3165                 svc_info->inst_id = event_data->srvc_id.id.inst_id;
3166                 svc_info->is_primary = event_data->srvc_id.is_prmry;
3167                 svc_info_list->services = g_slist_append(svc_info_list->services, svc_info);
3168                 BT_INFO("Service created and added in Svc info listf or this connection ID");
3169         } else {
3170                 BT_INFO("Service Already found to be present inside the svc info list for this connection ID");
3171                 /* If returned matching service info, then just update service_rmeoved value inside it to 0 */
3172                 svc_info->is_removed = 0;
3173         }
3174 }
3175
3176 static void __bt_handle_client_service_search_completed(
3177                 event_gattc_conn_status_t *event_data)
3178 {
3179         BT_INFO("+");
3180         struct gatt_server_info_t *conn_info = NULL;
3181         bt_gatt_service_info_list_t *svc_info_list;
3182         bt_gatt_service_info_t *svc_info;
3183         bt_services_browse_info_t browse_info;
3184         unsigned char uuid_empty[BLUETOOTH_UUID_HEX_MAX_LEN];
3185
3186         memset(&uuid_empty, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3187         memset(&browse_info, 0x00, sizeof(bt_services_browse_info_t));
3188         BT_INFO("Primary Services browsing completed status[%d] conn ID [%d]",
3189                         event_data->status, event_data->conn_id);
3190
3191         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(event_data->conn_id);
3192
3193         svc_info_list = __bt_get_service_info_list(event_data->conn_id);
3194         if (!svc_info_list) {
3195                 BT_ERR("No services browsed ever for addr [%s]", conn_info->addr);
3196
3197                 /* Just build response and return ERROR */
3198                 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3199
3200                 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3201                                 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3202                                 sizeof(bt_services_browse_info_t));
3203                 return;
3204         }
3205
3206         /* If fail, then send event with error  */
3207         if (event_data->status != OAL_STATUS_SUCCESS) {
3208                 /* Just build response and return ERROR */
3209                 __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3210
3211                 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_INTERNAL,
3212                                 BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3213                                 sizeof(bt_services_browse_info_t));
3214                 return;
3215         }
3216
3217         /* If success, then find service info list from address */
3218
3219         /* If svc_changed == 1 and uuid valid, means a new service is added*/
3220         if (svc_info_list->info.is_changed && !memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3221                 /* TODO: Send event -Service added with instance ID and UUID of newly added service */
3222                 BT_INFO("new service added");
3223
3224                 BT_INFO("TODO new service added");
3225         }
3226
3227         /* If svc_changed == 1 and uuid invalid, then a service is removed */
3228         if (svc_info_list->info.is_changed && memcmp(uuid_empty, svc_info_list->info.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)) {
3229                 /* Scan through the service info list to find service with is_removed = 1*/
3230                 svc_info = __bt_find_removed_service(svc_info_list);
3231
3232                 /* TODO Send event - Service removed with instance ID and UUID of just rmeoved service */
3233
3234                 /* Remove that service info from service info list */
3235                 svc_info_list->services = g_slist_remove(svc_info_list->services, svc_info);
3236
3237                 /* Delete that service completely from svc_info list*/
3238                 __bt_remove_service_info_from_list(svc_info);
3239                 g_free(svc_info);
3240         }
3241
3242         /* Reset svc_changed = 0, and reset UUID = all 0's */
3243         svc_info_list->info.is_changed = 0;
3244         memset(&svc_info_list->info.uuid, 0x00, BLUETOOTH_UUID_HEX_MAX_LEN);
3245
3246         /* Build Reply and send to service browse primary services request of pending apps */
3247         __bt_build_service_browse_info(event_data->conn_id, &browse_info);
3248
3249         __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3250                         BT_GATT_GET_PRIMARY_SERVICES, &browse_info,
3251                         sizeof(bt_services_browse_info_t));
3252 }
3253
3254
3255 static void __bt_handle_client_characteristic_search_result(
3256                 event_gattc_characteristic_result_t *event_data)
3257 {
3258         bt_gatt_service_info_list_t *svc_info_list;
3259         bt_gatt_service_info_t *svc_info;
3260         bt_gatt_char_info_t *char_info;
3261         bt_char_browse_info_t browse_info;
3262
3263         BT_INFO("Characteristic search result status [%d]",
3264                         event_data->conn_status.status);
3265
3266         memset(&browse_info, 0x00, sizeof(bt_char_browse_info_t));
3267
3268         /* If success */
3269         if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3270                 /* Find service info list from address */
3271                 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3272                 if (svc_info_list == NULL) {
3273                         BT_ERR("svc_info_list is NULL");
3274                         return;
3275                 }
3276
3277
3278                 /* Find matching service info from svc info list */
3279                 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3280                 if (svc_info == NULL) {
3281                         BT_ERR("svc_info is NULL");
3282                         return;
3283                 }
3284
3285                 /* Find Matching char from service info in event */
3286                 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3287                 if (char_info == NULL)
3288                         BT_ERR("char_info is NULL");
3289
3290                 /* If not found, then add new characteristic and return */
3291                 if (!char_info) {
3292                         BT_INFO(" add new characteristic");
3293                         char_info = g_malloc0(sizeof(bt_gatt_char_info_t));
3294                         memcpy(char_info->uuid, event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3295
3296                         char_info->inst_id = event_data->char_id.inst_id;
3297                         char_info->props = event_data->char_prop;
3298                         svc_info->chars = g_slist_append(svc_info->chars, char_info);
3299                 } else {
3300                         /* If found, then return */
3301                         BT_INFO("update char property as Characteristic browsed is already present");
3302                         char_info->props |= event_data->char_prop;
3303                 }
3304         } else {
3305                 /* If Not success: Means Charc browse is completed  */
3306                 /* Find char list from service in event */
3307                 /* Find service list from address */
3308                 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3309
3310                 /* Find service info from service in event */
3311                 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3312
3313                 /* Build char list from service in event */
3314                 __bt_build_char_browse_info(event_data->conn_status.conn_id,
3315                                 svc_info, &browse_info);
3316
3317                 /* Create response and return by sending event*/
3318                 /* Build Reply and send to service browse All Included services request of pending apps */
3319                 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3320                                 BT_GATT_GET_SERVICE_PROPERTIES,
3321                                 &browse_info,
3322                                 sizeof(bt_char_browse_info_t));
3323         }
3324 }
3325
3326 static void __bt_handle_client_descriptor_search_result(
3327                 event_gattc_descriptor_result_t *event_data)
3328 {
3329         bt_gatt_service_info_list_t *svc_info_list;
3330         bt_gatt_service_info_t *svc_info;
3331         bt_gatt_char_info_t *char_info;
3332         bt_gatt_descriptor_info_t *desc_info;
3333         bt_descriptor_browse_info_t browse_info;
3334
3335         BT_INFO("descriptor search result status [%d]", event_data->conn_status.status);
3336
3337         memset(&browse_info, 0x00, sizeof(bt_descriptor_browse_info_t));
3338
3339         /* If success */
3340         if (event_data->conn_status.status == OAL_STATUS_SUCCESS) {
3341                 /* Find service list from address */
3342                 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3343                 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3344                 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3345                 desc_info = __bt_find_matching_desc(char_info, &event_data->descr_id);
3346
3347                 /* If not found, add new descriptor and return */
3348                 if (!desc_info) {
3349                         desc_info = g_malloc0(sizeof(bt_gatt_descriptor_info_t));
3350                         memcpy(desc_info->uuid, event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3351
3352                         desc_info->inst_id = event_data->descr_id.inst_id;
3353                         char_info->descs = g_slist_append(char_info->descs, desc_info);
3354
3355                 } else {
3356                         /* If found, then return */
3357                         BT_INFO("Characteristic browsed is already presesnt");
3358                 }
3359         } else {
3360                 /* If Not success */
3361                 /* Find service list from address */
3362                 /* Find included service list from service in event */
3363                 /* Create response and return by sending event*/
3364                 svc_info_list = __bt_get_service_info_list(event_data->conn_status.conn_id);
3365
3366                 /* Find service info from service in event */
3367                 svc_info = __bt_find_matching_service(svc_info_list, &event_data->srvc_id);
3368
3369                 /* Find char info from char in event */
3370                 char_info = __bt_find_matching_charc(svc_info, &event_data->char_id);
3371
3372                 /* Build descriptor list from char in event */
3373                 __bt_build_descriptor_browse_info(event_data->conn_status.conn_id,
3374                                 svc_info, char_info, &browse_info);
3375
3376                 /* DBUS returni */
3377                 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE,
3378                                 BT_GATT_GET_CHARACTERISTIC_PROPERTIES,
3379                                 &browse_info,
3380                                 sizeof(bt_descriptor_browse_info_t));
3381         }
3382 }
3383
3384 static void __bt_handle_client_characteristic_read_data(
3385                 event_gattc_read_data *event_data)
3386 {
3387         int result = BLUETOOTH_ERROR_NONE;
3388         struct gatt_server_info_t *conn_info = NULL;
3389         bluetooth_gatt_client_char_prop_info_t read_info;
3390
3391         /* Read Information data structures */
3392         GVariant *param = NULL;
3393         GVariant *data = NULL;
3394         GVariant *data_svc_uuid = NULL;
3395         GVariant *data_char_uuid = NULL;
3396         char *read_val = NULL;
3397         char *svc_uuid = NULL;
3398         char *char_uuid = NULL;
3399         int i;
3400         int uuid_len = 16;
3401
3402         BT_INFO("+");
3403
3404         //memset(&read_info, 0x00, sizeof(bt_gatt_handle_property_t));
3405         memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3406
3407         /* Extract Address from conn_id of event data */
3408         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3409                         event_data->uuid_status.conn_status.conn_id);
3410
3411         BT_INFO("Characteristic Read result from addr [%s] status [%d]",
3412                         conn_info->addr, event_data->uuid_status.conn_status.status);
3413
3414         /* Fill char in buffer */
3415         memcpy(&read_info.characteristic.uuid,
3416                         event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3417         read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3418
3419         /* Fill Service in buffer */
3420         memcpy(&read_info.svc.uuid,
3421                         event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3422         read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3423
3424         /* Fill remote device address */
3425         _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3426
3427         /* Fill data and reply to all apps waiting for Read result on the same characteristic
3428 Note: Even in case of failure, address, handles and result code should be returned */
3429         if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3430                 result = BLUETOOTH_ERROR_INTERNAL;
3431         else {
3432                 BT_ERR("read data len is [%d]", event_data->data_len);
3433                 if (event_data->data_len > 0) {
3434                         /* DEBUG */
3435                         for (i = 0; i < event_data->data_len; i++)
3436                                 BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3437
3438                         /* Read data */
3439                         read_val = g_memdup(&event_data->data[0], event_data->data_len);
3440
3441                         data = g_variant_new_from_data(
3442                                         G_VARIANT_TYPE_BYTESTRING,
3443                                         read_val,
3444                                         event_data->data_len,
3445                                         TRUE, NULL, NULL);
3446                 } else {
3447                         BT_ERR("Characteristic Read success, but no data!!!");
3448
3449                         data = g_variant_new_from_data(
3450                                         G_VARIANT_TYPE_BYTESTRING,
3451                                         NULL,
3452                                         0,
3453                                         FALSE, NULL, NULL);
3454                 }
3455
3456                 /* SVC uuid */
3457                 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3458
3459                 data_svc_uuid = g_variant_new_from_data(
3460                                 G_VARIANT_TYPE_BYTESTRING,
3461                                 svc_uuid,
3462                                 16,
3463                                 TRUE, NULL, NULL);
3464
3465                 /* Char uuid */
3466                 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3467
3468                 data_char_uuid = g_variant_new_from_data(
3469                                 G_VARIANT_TYPE_BYTESTRING,
3470                                 char_uuid,
3471                                 16,
3472                                 TRUE, NULL, NULL);
3473
3474                 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3475                                 conn_info->addr,
3476                                 16,
3477                                 data_svc_uuid,
3478                                 event_data->uuid_status.srvc_id.id.inst_id,
3479                                 16,
3480                                 data_char_uuid,
3481                                 event_data->uuid_status.char_id.inst_id,
3482                                 event_data->data_len,
3483                                 data);
3484
3485                 /* Send Event */
3486                 _bt_send_event(BT_GATT_CLIENT_EVENT,
3487                                 BLUETOOTH_EVENT_GATT_READ_CHAR,
3488                                 param);
3489         }
3490         /* Send DBUS return */
3491         __bt_gatt_handle_pending_request_info(result,
3492                         BT_GATT_READ_CHARACTERISTIC,
3493                         &read_info,
3494                         sizeof(bluetooth_gatt_client_char_prop_info_t));
3495
3496         if (read_val)
3497                 g_free(read_val);
3498         if (svc_uuid)
3499                 g_free(svc_uuid);
3500         if (char_uuid)
3501                 g_free(char_uuid);
3502 }
3503
3504 /* Modified */
3505 static void __bt_handle_client_descriptor_read_data(
3506                 event_gattc_read_data *event_data)
3507 {
3508         int result = BLUETOOTH_ERROR_NONE;
3509         struct gatt_server_info_t *conn_info = NULL;
3510         bluetooth_gatt_client_desc_prop_info_t read_info;
3511
3512         /* Read Information data structures */
3513         GVariant *param = NULL;
3514         GVariant *data = NULL;
3515         GVariant *data_svc_uuid = NULL;
3516         GVariant *data_char_uuid = NULL;
3517         GVariant *data_desc_uuid = NULL;
3518         char *read_val = NULL;
3519         char *svc_uuid = NULL;
3520         char *char_uuid = NULL;
3521         char *desc_uuid = NULL;
3522         int i;
3523         int uuid_len = 16;
3524         BT_INFO("+");
3525
3526         memset(&read_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3527
3528         /* Extract Address from conn_id of event data */
3529         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3530                         event_data->uuid_status.conn_status.conn_id);
3531
3532         BT_INFO("Descriptor Read result from addr [%s] status [%d]",
3533                         conn_info->addr, event_data->uuid_status.conn_status.status);
3534
3535         /* Fill descriptor informations in buffer */
3536         memcpy(&read_info.descriptor.uuid,
3537                         event_data->uuid_status.descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3538         read_info.descriptor.instance_id = event_data->uuid_status.descr_id.inst_id;
3539
3540         /* Fill Characteristic informations in buffer */
3541         memcpy(&read_info.characteristic.uuid,
3542                         event_data->uuid_status.char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3543         read_info.characteristic.instance_id = event_data->uuid_status.char_id.inst_id;
3544
3545         /* Fill Service informations in buffer */
3546         memcpy(&read_info.svc.uuid,
3547                         event_data->uuid_status.srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3548         read_info.svc.instance_id = event_data->uuid_status.srvc_id.id.inst_id;
3549
3550         /* Fill remote device address */
3551         _bt_convert_addr_string_to_type(read_info.device_address.addr, conn_info->addr);
3552
3553         /* Fill data and reply to all apps waiting for Read result on the same characteristic */
3554         if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
3555                 result = BLUETOOTH_ERROR_INTERNAL;
3556         else {
3557                 BT_INFO("desc data len:", event_data->data_len);
3558                 if (event_data->data_len > 0) {
3559                         /* DEBUG */
3560                         for (i = 0; i < event_data->data_len; i++)
3561                                 BT_DBG("Data[%d] = [0x%x]", i, event_data->data[i]);
3562
3563                         /* Read data */
3564                         read_val = g_memdup(&event_data->data[0], event_data->data_len);
3565
3566                         data = g_variant_new_from_data(
3567                                         G_VARIANT_TYPE_BYTESTRING,
3568                                         read_val,
3569                                         event_data->data_len,
3570                                         TRUE, NULL, NULL);
3571                 } else {
3572                         BT_INFO("Descriptor Read success, but no data!!!");
3573
3574                         data = g_variant_new_from_data(
3575                                         G_VARIANT_TYPE_BYTESTRING,
3576                                         NULL,
3577                                         0,
3578                                         FALSE, NULL, NULL);
3579                 }
3580                 /* SVC uuid */
3581                 svc_uuid = g_memdup(&event_data->uuid_status.srvc_id.id.uuid.uuid[0], uuid_len);
3582
3583                 data_svc_uuid = g_variant_new_from_data(
3584                                 G_VARIANT_TYPE_BYTESTRING,
3585                                 svc_uuid,
3586                                 16,
3587                                 TRUE, NULL, NULL);
3588
3589                 /* Char uuid */
3590                 char_uuid = g_memdup(&event_data->uuid_status.char_id.uuid.uuid[0], uuid_len);
3591
3592                 data_char_uuid = g_variant_new_from_data(
3593                                 G_VARIANT_TYPE_BYTESTRING,
3594                                 char_uuid,
3595                                 16,
3596                                 TRUE, NULL, NULL);
3597
3598                 /* Desc uuid */
3599                 desc_uuid = g_memdup(&event_data->uuid_status.descr_id.uuid.uuid[0], uuid_len);
3600
3601                 data_desc_uuid = g_variant_new_from_data(
3602                                 G_VARIANT_TYPE_BYTESTRING,
3603                                 desc_uuid,
3604                                 16,
3605                                 TRUE, NULL, NULL);
3606
3607                 param = g_variant_new("(isn@ayin@ayin@ayin@ay)", result,
3608                                 conn_info->addr,
3609                                 uuid_len,
3610                                 data_svc_uuid,
3611                                 event_data->uuid_status.srvc_id.id.inst_id,
3612                                 16,
3613                                 data_char_uuid,
3614                                 event_data->uuid_status.char_id.inst_id,
3615                                 16,
3616                                 data_desc_uuid,
3617                                 event_data->uuid_status.descr_id.inst_id,
3618                                 event_data->data_len,
3619                                 data);
3620
3621                 /* Send Event */
3622                 _bt_send_event(BT_GATT_CLIENT_EVENT,
3623                                         BLUETOOTH_EVENT_GATT_READ_DESC,
3624                                         param);
3625         }
3626         BT_INFO("Send DBUS rpely for GATT Read Descriptor");
3627         /* Send DBUS return */
3628         __bt_gatt_handle_pending_request_info(result,
3629                         BT_GATT_READ_DESCRIPTOR_VALUE,
3630                         &read_info,
3631                         sizeof(bluetooth_gatt_client_desc_prop_info_t));
3632         if (read_val)
3633                 g_free(read_val);
3634         if (svc_uuid)
3635                 g_free(svc_uuid);
3636         if (char_uuid)
3637                 g_free(char_uuid);
3638         if (desc_uuid)
3639                 g_free(desc_uuid);
3640 }
3641
3642 static void __bt_handle_client_characteristic_write_data(
3643                 event_gattc_write_data *event_data)
3644 {
3645         int result = BLUETOOTH_ERROR_NONE;
3646         struct gatt_server_info_t *conn_info = NULL;
3647         bluetooth_gatt_client_char_prop_info_t write_info;
3648
3649         /* Read Information data structures */
3650         GVariant *param = NULL;
3651         GVariant *data_svc_uuid = NULL;
3652         GVariant *data_char_uuid = NULL;
3653         char *svc_uuid = NULL;
3654         char *char_uuid = NULL;
3655         int uuid_len = 16;
3656         BT_INFO("+");
3657
3658         memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3659
3660         /* Extract Address from conn_id of event data */
3661         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3662                         event_data->conn_status.conn_id);
3663
3664         BT_INFO("Characteristic Write callback from addr [%s] status [%d]",
3665                         conn_info->addr, event_data->conn_status.status);
3666
3667         /* Fill char in buffer */
3668         memcpy(&write_info.characteristic.uuid,
3669                         event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3670         write_info.characteristic.instance_id = event_data->char_id.inst_id;
3671
3672         /* Fill Service in buffer */
3673         memcpy(&write_info.svc.uuid,
3674                         event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3675         write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3676
3677         /* Fill remote device address */
3678         _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3679
3680         if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3681                 result = BLUETOOTH_ERROR_INTERNAL;
3682                 goto done;
3683         }
3684
3685         /* Build event */
3686         /* SVC uuid */
3687         svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3688
3689         data_svc_uuid = g_variant_new_from_data(
3690                         G_VARIANT_TYPE_BYTESTRING,
3691                         svc_uuid,
3692                         uuid_len,
3693                         TRUE, NULL, NULL);
3694
3695         /* Char uuid */
3696         char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3697
3698         data_char_uuid = g_variant_new_from_data(
3699                         G_VARIANT_TYPE_BYTESTRING,
3700                         char_uuid,
3701                         uuid_len,
3702                         TRUE, NULL, NULL);
3703
3704         param = g_variant_new("(isn@ayin@ayi)", result,
3705                         conn_info->addr,
3706                         16,
3707                         data_svc_uuid,
3708                         event_data->srvc_id.id.inst_id,
3709                         16,
3710                         data_char_uuid,
3711                         event_data->char_id.inst_id);
3712
3713         /* Send Event */
3714         _bt_send_event(BT_GATT_CLIENT_EVENT,
3715                         BLUETOOTH_EVENT_GATT_WRITE_CHAR,
3716                         param);
3717
3718         /* Free data */
3719         if (svc_uuid)
3720                 g_free(svc_uuid);
3721         if (char_uuid)
3722                 g_free(char_uuid);
3723 done:
3724         /* Send DBUS return */
3725         __bt_gatt_handle_pending_request_info(result,
3726                         BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE,
3727                         &write_info,
3728                         sizeof(bluetooth_gatt_client_char_prop_info_t));
3729 }
3730
3731
3732 static void __bt_handle_client_descriptor_write_data(
3733                 event_gattc_write_data *event_data)
3734 {
3735         int result = BLUETOOTH_ERROR_NONE;
3736         struct gatt_server_info_t *conn_info = NULL;
3737         bluetooth_gatt_client_desc_prop_info_t write_info;
3738
3739         /* Write Information data structures */
3740         GVariant *param = NULL;
3741         GVariant *data_svc_uuid = NULL;
3742         GVariant *data_char_uuid = NULL;
3743         GVariant *data_desc_uuid = NULL;
3744         char *svc_uuid = NULL;
3745         char *char_uuid = NULL;
3746         char *desc_uuid = NULL;
3747         int uuid_len = 16;
3748         BT_INFO("+");
3749
3750         memset(&write_info, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3751
3752         /* Extract Address from conn_id of event data */
3753         conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
3754                         event_data->conn_status.conn_id);
3755
3756         if (NULL == conn_info) {
3757
3758                 BT_INFO("Failed to get the conn info for conn_id [%d]", event_data->conn_status.conn_id);
3759                 return;
3760         }
3761
3762         BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
3763                         conn_info->addr, event_data->conn_status.status);
3764
3765         /* Fill descriptor informations in buffer */
3766         memcpy(&write_info.descriptor.uuid,
3767                         event_data->descr_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3768         write_info.descriptor.instance_id = event_data->descr_id.inst_id;
3769
3770         /* Fill Characteristic informations in buffer */
3771         memcpy(&write_info.characteristic.uuid,
3772                         event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3773         write_info.characteristic.instance_id = event_data->char_id.inst_id;
3774
3775         /* Fill Service informations in buffer */
3776         memcpy(&write_info.svc.uuid,
3777                         event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3778         write_info.svc.instance_id = event_data->srvc_id.id.inst_id;
3779
3780         /* Fill remote device address */
3781         _bt_convert_addr_string_to_type(write_info.device_address.addr, conn_info->addr);
3782
3783         if (event_data->conn_status.status != OAL_STATUS_SUCCESS) {
3784                 result = BLUETOOTH_ERROR_INTERNAL;
3785                 goto done;
3786         }
3787
3788         /* Build event */
3789         /* SVC uuid */
3790         svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3791
3792         data_svc_uuid = g_variant_new_from_data(
3793                         G_VARIANT_TYPE_BYTESTRING,
3794                         svc_uuid,
3795                         uuid_len,
3796                         TRUE, NULL, NULL);
3797
3798         /* Char uuid */
3799         char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3800
3801         data_char_uuid = g_variant_new_from_data(
3802                         G_VARIANT_TYPE_BYTESTRING,
3803                         char_uuid,
3804                         uuid_len,
3805                         TRUE, NULL, NULL);
3806
3807         /* Desc uuid */
3808         desc_uuid = g_memdup(&event_data->descr_id.uuid.uuid[0], uuid_len);
3809
3810         data_desc_uuid = g_variant_new_from_data(
3811                         G_VARIANT_TYPE_BYTESTRING,
3812                         desc_uuid,
3813                         uuid_len,
3814                         TRUE, NULL, NULL);
3815
3816         param = g_variant_new("(isn@ayin@ayin@ayi)", result,
3817                         conn_info->addr,
3818                         16,
3819                         data_svc_uuid,
3820                         event_data->srvc_id.id.inst_id,
3821                         16,
3822                         data_char_uuid,
3823                         event_data->char_id.inst_id,
3824                         16,
3825                         data_desc_uuid,
3826                         event_data->descr_id.inst_id);
3827
3828         /* Send Event */
3829         _bt_send_event(BT_GATT_CLIENT_EVENT,
3830                         BLUETOOTH_EVENT_GATT_WRITE_DESC,
3831                         param);
3832
3833         /* Free data */
3834         if (svc_uuid)
3835                 g_free(svc_uuid);
3836         if (char_uuid)
3837                 g_free(char_uuid);
3838         if (desc_uuid)
3839                 g_free(desc_uuid);
3840 done:
3841         /* Send DBUS return */
3842         __bt_gatt_handle_pending_request_info(result,
3843                         BT_GATT_WRITE_DESCRIPTOR_VALUE,
3844                         &write_info,
3845                         sizeof(bluetooth_gatt_client_desc_prop_info_t));
3846 }
3847
3848 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data)
3849 {
3850         int result = BLUETOOTH_ERROR_INTERNAL;
3851         char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
3852
3853         _bt_convert_addr_type_to_string(address, (unsigned char *)event_data->address.addr);
3854
3855         /* DBUS Return with fail of pending BT_CONNECT_LE for all the apps */
3856         BT_INFO("Local GATT Client disconnected: Remote addr[%s] ", address);
3857
3858         __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE, address,
3859                                                  BT_ADDRESS_STRING_SIZE);
3860         g_free(address);
3861 }
3862
3863 static void __bt_handle_client_notification_registered(
3864                 event_gattc_regdereg_notify_t *event_data,
3865                 gboolean is_registered)
3866 {
3867         int result = BLUETOOTH_ERROR_NONE;
3868         struct gatt_server_info_t *conn_info = NULL;
3869         bt_gatt_notif_reg_info_t notif_info;
3870         BT_INFO("+");
3871
3872         memset(&notif_info, 0x00, sizeof(bt_gatt_notif_reg_info_t));
3873
3874         BT_INFO("Client Interface [%d] status [%d]",
3875                         event_data->client_if,
3876                         event_data->status);
3877
3878         /* Extract Address from conn_id of event data */
3879         conn_info = __bt_find_remote_gatt_server_info_from_client_if(
3880                         event_data->client_if);
3881
3882         if (!conn_info) {
3883                 BT_INFO("Connection Info is not present, return");
3884                 return;
3885         }
3886         BT_INFO("Notification Registered for addr [%s]",
3887                         conn_info->addr);
3888
3889
3890         /* Fill svc informations in buffer */
3891         memcpy(&notif_info.svc_uuid,
3892                         event_data->srvc_id.id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3893         notif_info.svc_inst = event_data->srvc_id.id.inst_id;
3894
3895         /* Fill char in buffer */
3896         memcpy(&notif_info.char_uuid,
3897                         event_data->char_id.uuid.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
3898         notif_info.char_inst = event_data->char_id.inst_id;
3899
3900         /* Fill remote device address */
3901         _bt_convert_addr_string_to_type(notif_info.addr.addr, conn_info->addr);
3902
3903         notif_info.is_registered = is_registered;
3904
3905         if (event_data->status != OAL_STATUS_SUCCESS)
3906                 result = BLUETOOTH_ERROR_INTERNAL;
3907
3908         /* Send DBUS Return for BT_GATT_WATCH_CHARACTERISTIC */
3909         __bt_gatt_handle_pending_request_info(result,
3910                         BT_GATT_WATCH_CHARACTERISTIC,
3911                         &notif_info,
3912                         sizeof(bt_gatt_notif_reg_info_t));
3913 }
3914
3915 static void __bt_handle_client_notification_data(event_gattc_notify_data *event_data)
3916 {
3917         /* No status in this event from OAL */
3918         int result = BLUETOOTH_ERROR_NONE;
3919
3920         /* Read Information data structures */
3921         GVariant *param = NULL;
3922         GVariant *data = NULL;
3923         GVariant *data_svc_uuid = NULL;
3924         GVariant *data_char_uuid = NULL;
3925         char *read_val = NULL;
3926         char *svc_uuid = NULL;
3927         char *char_uuid = NULL;
3928         char *addr = NULL;
3929         int i;
3930         int uuid_len = 16;
3931         BT_INFO("+");
3932
3933         BT_INFO("Notifcation of charc data changed");
3934
3935         if (event_data->data_len > 0) {
3936                 /* DEBUG */
3937                 for (i = 0; i < event_data->data_len; i++)
3938                         BT_INFO("Data[%d] = [0x%x]", i, event_data->data[i]);
3939
3940                 /* Fill address */
3941                 addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3942                 _bt_convert_addr_type_to_string(addr,
3943                                 (unsigned char *)&(event_data->address.addr));
3944
3945                 /* Read data */
3946                 read_val = g_memdup(&event_data->data[0], event_data->data_len);
3947
3948                 data = g_variant_new_from_data(
3949                                 G_VARIANT_TYPE_BYTESTRING,
3950                                 read_val,
3951                                 event_data->data_len,
3952                                 TRUE, NULL, NULL);
3953                 /* SVC uuid */
3954                 svc_uuid = g_memdup(&event_data->srvc_id.id.uuid.uuid[0], uuid_len);
3955
3956                 data_svc_uuid = g_variant_new_from_data(
3957                                 G_VARIANT_TYPE_BYTESTRING,
3958                                 svc_uuid,
3959                                 uuid_len,
3960                                 TRUE, NULL, NULL);
3961
3962                 /* Char uuid */
3963                 char_uuid = g_memdup(&event_data->char_id.uuid.uuid[0], uuid_len);
3964
3965                 data_char_uuid = g_variant_new_from_data(
3966                                 G_VARIANT_TYPE_BYTESTRING,
3967                                 char_uuid,
3968                                 uuid_len,
3969                                 TRUE, NULL, NULL);
3970
3971                 /* Build Param */
3972                 param = g_variant_new("(isn@ayin@ayin@ay)", result,
3973                                 addr,
3974                                 16,
3975                                 data_svc_uuid,
3976                                 event_data->srvc_id.id.inst_id,
3977                                 16,
3978                                 data_char_uuid,
3979                                 event_data->char_id.inst_id,
3980                                 event_data->data_len,
3981                                 data);
3982
3983                 /* Send Event */
3984                 _bt_send_event(BT_GATT_CLIENT_EVENT,
3985                                 BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
3986                                 param);
3987         } else {
3988                 BT_ERR("No Data!!");
3989         }
3990         /* Free data */
3991         if (read_val)
3992                 g_free(read_val);
3993         if (svc_uuid)
3994                 g_free(svc_uuid);
3995         if (char_uuid)
3996                 g_free(char_uuid);
3997         if (addr)
3998                 g_free(addr);
3999 }
4000
4001 gboolean _bt_is_remote_gatt_device_connected(bluetooth_device_address_t *address)
4002 {
4003         char *addr;
4004         struct gatt_server_info_t *conn_info =  NULL;
4005         gboolean connected = FALSE;
4006         BT_INFO("+");
4007
4008         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
4009         _bt_convert_addr_type_to_string(addr,
4010                         (unsigned char *)&(address->addr));
4011
4012         BT_INFO("Check Connected or not for [%s]", addr);
4013         /* Check if device is already in connected list */
4014         conn_info = __bt_find_remote_gatt_server_info(addr);
4015
4016         if (conn_info) {
4017                 BT_INFO("Remote GATT Server device [%s] is Connected", conn_info->addr);
4018                 connected = TRUE;
4019         } else
4020                 BT_INFO("Remote GATT Server Device [%s] is not Connected", addr);
4021         g_free(addr);
4022         return connected;
4023 }
4024
4025
4026 int _bt_connect_le_device(bluetooth_device_address_t *address,
4027                 int auto_connect, int client_id)
4028 {
4029         struct gatt_server_info_t *conn_info = NULL;
4030         struct gatt_out_conn_info_t *out_conn_info = NULL;
4031
4032         invocation_info_t *req_info = NULL;
4033         int ret = OAL_STATUS_SUCCESS;
4034         char *addr;
4035         char *remote_address = NULL;
4036
4037         BT_CHECK_PARAMETER(address, return);
4038
4039         BT_INFO("+");
4040
4041         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4042         _bt_convert_addr_type_to_string(addr, address->addr);
4043         BT_INFO("GATT Client connect request for address [%s] client instance [%d]",
4044                         addr, client_id);
4045
4046
4047         /* Check if Remote Device is already under connection progress */
4048         req_info = _bt_get_request_info_data_from_function_name(BT_CONNECT_LE);
4049         if (req_info) {
4050                 remote_address = (char*)req_info->user_data;
4051                 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4052                         BT_INFO("Already Connection ongoing for same remote GATT Server address [%s]", remote_address);
4053                         /* Return and wait for events to be sent to all apps */
4054                         g_free(addr);
4055                         return BLUETOOTH_ERROR_IN_PROGRESS;
4056                 }
4057         }
4058
4059         /* Check if remote GATT Server is connected or not */
4060         conn_info = __bt_find_remote_gatt_server_info(addr);
4061         if (conn_info) {
4062                 BT_ERR("GATT Server is already connected..");
4063                 g_free(addr);
4064                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
4065         }
4066
4067         /* TODO Check Requirement of holding Advertisement before initiating LE connect */
4068
4069         /* Check if app sent 0 client id for connection, in such case, use default gatt client ID */
4070         if (client_id == 0) {
4071                 BT_INFO("GATT CLient connect request sent by an app without any client instance [%d]",
4072                                 client_id);
4073                 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4074                 client_id = gatt_default_client;
4075         }
4076
4077         BT_INFO("Connect using CLient ID [%d]", client_id);
4078         ret = gattc_connect(client_id, (bt_address_t*)(address), auto_connect);
4079
4080         if (ret != OAL_STATUS_SUCCESS) {
4081                 BT_ERR("ret: %d", ret);
4082                 g_free(addr);
4083                 return BLUETOOTH_ERROR_INTERNAL;
4084         }
4085
4086         /* Mark this as outgoing connection */
4087         out_conn_info = g_new0(struct gatt_out_conn_info_t, 1);
4088         out_conn_info->addr = g_strdup(addr);
4089         out_conn_info->client_id = client_id;
4090         BT_INFO("Added outgoing connection info addr[%s]", out_conn_info->addr);
4091         outgoing_gatt_conn_list = g_slist_append(outgoing_gatt_conn_list, out_conn_info);
4092
4093         g_free(addr);
4094         return BLUETOOTH_ERROR_NONE;
4095 }
4096
4097 int _bt_gatt_get_primary_services(char *address)
4098 {
4099         BT_CHECK_PARAMETER(address, return);
4100         struct gatt_server_info_t *conn_info = NULL;
4101         invocation_info_t *req_info = NULL;
4102         int ret = OAL_STATUS_SUCCESS;
4103         BT_INFO("+");
4104
4105         /* Check if any app is already browsing primary services on the same remote GATT Server */
4106         req_info = _bt_get_request_info_data(BT_GATT_GET_PRIMARY_SERVICES, address);
4107         if (req_info) {
4108                 BT_INFO("Already Primary Service Browsing ongoing for same rmeote GATT Server");
4109                 /* Return and wait for events to be sent to all apps */
4110                 return BLUETOOTH_ERROR_NONE;
4111         }
4112
4113         /* Check if remote GATT Server is connected or not */
4114         conn_info = __bt_find_remote_gatt_server_info(address);
4115         if (conn_info) {
4116                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4117                                 conn_info->addr, conn_info->connection_id);
4118         } else {
4119                 BT_ERR("GATT Server is not yet connected..");
4120                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4121         }
4122
4123         /* Send Primary Service Browsing request to stack */
4124         ret = gattc_search_service(conn_info->connection_id, NULL);
4125         if (ret != OAL_STATUS_SUCCESS) {
4126                 BT_ERR("ret: %d", ret);
4127                 return BLUETOOTH_ERROR_INTERNAL;
4128         }
4129         return BLUETOOTH_ERROR_NONE;
4130 }
4131
4132 int _bt_gatt_get_all_characteristic(bluetooth_gatt_client_svc_prop_info_t *svc)
4133 {
4134         BT_CHECK_PARAMETER(svc, return);
4135         struct gatt_server_info_t *conn_info = NULL;
4136         invocation_info_t *req_info = NULL;
4137         bluetooth_gatt_client_svc_prop_info_t *prop;
4138         oal_gatt_srvc_id_t srvc_id;
4139         int ret = OAL_STATUS_SUCCESS;
4140         char *addr;
4141         BT_INFO("+");
4142
4143         /* Check if any app is already browsing characteristics of the same service on the same remote GATT Server */
4144         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_SERVICE_PROPERTIES);
4145         if (req_info) {
4146                 prop = (bluetooth_gatt_client_svc_prop_info_t*)req_info->user_data;
4147                 if (prop && !memcmp(svc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t))
4148                                 && memcmp(prop->svc.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN)
4149                                 && prop->svc.instance_id == svc->svc.instance_id) {
4150                         BT_INFO("Already Properties browsing for Primary Service ongoing for same remote GATT Server");
4151                         /* Return and wait for events to be sent to all apps */
4152                         return BLUETOOTH_ERROR_NONE;
4153                 }
4154         }
4155
4156         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4157         _bt_convert_addr_type_to_string(addr, svc->device_address.addr);
4158
4159         /* Check if remote GATT Server is connected or not */
4160         conn_info = __bt_find_remote_gatt_server_info(addr);
4161         if (conn_info) {
4162                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4163                                 conn_info->addr, conn_info->connection_id);
4164         } else {
4165                 BT_ERR("GATT Server is not yet connected..");
4166                 g_free(addr);
4167                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4168         }
4169
4170         srvc_id.is_prmry = TRUE;
4171         srvc_id.id.inst_id = svc->svc.instance_id;
4172         memcpy(srvc_id.id.uuid.uuid, svc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4173
4174         /* Search All Characteristic */
4175         ret = gattc_get_characteristic(conn_info->connection_id, &srvc_id, NULL);
4176         if (ret != OAL_STATUS_SUCCESS) {
4177                 BT_ERR("ret: %d", ret);
4178                 g_free(addr);
4179                 return BLUETOOTH_ERROR_INTERNAL;
4180         }
4181         g_free(addr);
4182         return BLUETOOTH_ERROR_NONE;
4183 }
4184
4185 int _bt_gatt_get_all_characteristic_properties(
4186                 bluetooth_gatt_client_char_prop_info_t *chr)
4187 {
4188         struct gatt_server_info_t *conn_info = NULL;
4189         invocation_info_t *req_info = NULL;
4190         bluetooth_gatt_client_char_prop_info_t *prop;
4191         oal_gatt_srvc_id_t srvc_id;
4192         oal_gatt_id_t char_id;
4193         int ret = OAL_STATUS_SUCCESS;
4194         char *addr;
4195
4196         BT_CHECK_PARAMETER(chr, return);
4197
4198         BT_INFO("+");
4199
4200         /* Check if any app is already browsing descriptors of the same char of
4201            particular service on the same remote GATT Server */
4202         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_GET_CHARACTERISTIC_PROPERTIES);
4203         if (req_info) {
4204                 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4205                 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4206                                 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4207                                 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4208                                 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4209                                 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4210                         BT_INFO("Already Properties browsing for Characteristic ongoing for same remote GATT Server");
4211                         /* Return and wait for events to be sent to all apps */
4212                         return BLUETOOTH_ERROR_NONE;
4213                 }
4214         }
4215
4216         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4217         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4218
4219         /* Check if remote GATT Server is connected or not */
4220         conn_info = __bt_find_remote_gatt_server_info(addr);
4221         if (conn_info) {
4222                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4223                                 conn_info->addr, conn_info->connection_id);
4224         } else {
4225                 BT_ERR("GATT Server is not yet connected..");
4226                 g_free(addr);
4227                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4228         }
4229
4230         srvc_id.is_prmry = TRUE;
4231         srvc_id.id.inst_id = chr->svc.instance_id;
4232         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4233
4234         char_id.inst_id = chr->characteristic.instance_id;
4235         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4236
4237         /* Search All Descriptors */
4238         ret = gattc_get_descriptor(conn_info->connection_id, &srvc_id, &char_id, NULL);
4239         if (ret != OAL_STATUS_SUCCESS) {
4240                 BT_ERR("ret: %d", ret);
4241                 g_free(addr);
4242                 return BLUETOOTH_ERROR_INTERNAL;
4243         }
4244         g_free(addr);
4245         return BLUETOOTH_ERROR_NONE;
4246 }
4247
4248 int _bt_gatt_read_characteristic_value(
4249                 bluetooth_gatt_client_char_prop_info_t *chr)
4250 {
4251         struct gatt_server_info_t *conn_info = NULL;
4252         invocation_info_t *req_info = NULL;
4253         bluetooth_gatt_client_char_prop_info_t *prop;
4254         oal_gatt_srvc_id_t srvc_id;
4255         oal_gatt_id_t char_id;
4256         int ret = OAL_STATUS_SUCCESS;
4257         char *addr;
4258
4259         BT_CHECK_PARAMETER(chr, return);
4260
4261         BT_INFO("+");
4262
4263         /* Check if any app is already Reading characteristic of the same char of
4264            particular service on the same remote GATT Server */
4265         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_CHARACTERISTIC);
4266         if (req_info) {
4267                 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4268                 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4269                                 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4270                                 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4271                                 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4272                                 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4273                         BT_INFO("Already Characteristic value Read operation in progress for same remote GATT Server");
4274                         /* Return and wait for events to be sent to all apps */
4275                         return BLUETOOTH_ERROR_NONE;
4276                 }
4277         }
4278
4279         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4280         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4281
4282         /* Check if remote GATT Server is connected or not */
4283         conn_info = __bt_find_remote_gatt_server_info(addr);
4284         if (conn_info) {
4285                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4286                                 conn_info->addr, conn_info->connection_id);
4287         } else {
4288                 BT_ERR("GATT Server is not yet connected..");
4289                 g_free(addr);
4290                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4291         }
4292
4293         srvc_id.is_prmry = TRUE;
4294         srvc_id.id.inst_id = chr->svc.instance_id;
4295         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4296
4297         char_id.inst_id = chr->characteristic.instance_id;
4298         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4299
4300         /* Search All Descriptors */
4301         ret = gattc_read_characteristic(conn_info->connection_id, &srvc_id, &char_id, OAL_GATT_AUTH_REQ_NONE);
4302         if (ret != OAL_STATUS_SUCCESS) {
4303                 BT_ERR("ret: %d", ret);
4304                 g_free(addr);
4305                 return BLUETOOTH_ERROR_INTERNAL;
4306         }
4307         g_free(addr);
4308         return BLUETOOTH_ERROR_NONE;
4309 }
4310
4311 int _bt_gatt_read_descriptor_value(
4312                 bluetooth_gatt_client_desc_prop_info_t *desc)
4313 {
4314         struct gatt_server_info_t *conn_info = NULL;
4315         invocation_info_t *req_info = NULL;
4316         bluetooth_gatt_client_desc_prop_info_t *prop;
4317         oal_gatt_srvc_id_t srvc_id;
4318         oal_gatt_id_t char_id;
4319         oal_gatt_id_t desc_id;
4320         int ret = OAL_STATUS_SUCCESS;
4321         char *addr;
4322
4323         BT_CHECK_PARAMETER(desc, return);
4324
4325         BT_INFO("+");
4326
4327         /* Check if any app is already Reading descriptors of the same char of
4328            particular service on the same remote GATT Server */
4329         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_READ_DESCRIPTOR_VALUE);
4330         if (req_info) {
4331                 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4332                 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4333                                 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4334                                 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4335                                 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4336                                 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4337                                 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4338                                 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4339                         BT_INFO("Already Descriptor value Read operation in progress for same remote GATT Server");
4340                         /* Return and wait for events to be sent to all apps */
4341                         return BLUETOOTH_ERROR_NONE;
4342                 }
4343         }
4344
4345         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4346         _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4347
4348         /* Check if remote GATT Server is connected or not */
4349         conn_info = __bt_find_remote_gatt_server_info(addr);
4350         if (conn_info) {
4351                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4352                                 conn_info->addr, conn_info->connection_id);
4353         } else {
4354                 BT_ERR("GATT Server is not yet connected..");
4355                 g_free(addr);
4356                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4357         }
4358
4359         srvc_id.is_prmry = TRUE;
4360         srvc_id.id.inst_id = desc->svc.instance_id;
4361         memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4362
4363         char_id.inst_id = desc->characteristic.instance_id;
4364         memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4365
4366         desc_id.inst_id = desc->descriptor.instance_id;
4367         memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4368
4369         /* Search All Descriptors */
4370         ret = gattc_read_descriptor(conn_info->connection_id, &srvc_id, &char_id,
4371                         &desc_id, OAL_GATT_AUTH_REQ_NONE);
4372         if (ret != OAL_STATUS_SUCCESS) {
4373                 BT_ERR("ret: %d", ret);
4374                 g_free(addr);
4375                 return BLUETOOTH_ERROR_INTERNAL;
4376         }
4377         g_free(addr);
4378         return BLUETOOTH_ERROR_NONE;
4379 }
4380
4381 /*acquire Notify*/
4382 int _bt_gatt_acquire_notify(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4383 {
4384         struct gatt_server_info_t *conn_info = NULL;
4385         oal_gatt_srvc_id_t srvc_id;
4386         oal_gatt_id_t char_id;
4387         int ret = OAL_STATUS_SUCCESS;
4388         char *addr;
4389
4390         BT_CHECK_PARAMETER(chr, return);
4391
4392         BT_INFO("+");
4393
4394         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4395         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4396
4397         /* Check if remote GATT Server is connected or not */
4398         conn_info = __bt_find_remote_gatt_server_info(addr);
4399         if (conn_info) {
4400                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4401                                 conn_info->addr, conn_info->connection_id);
4402         } else {
4403                 BT_ERR("GATT Server is not yet connected..");
4404                 g_free(addr);
4405                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4406         }
4407
4408         srvc_id.is_prmry = TRUE;
4409         srvc_id.id.inst_id = chr->svc.instance_id;
4410         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4411
4412         char_id.inst_id = chr->characteristic.instance_id;
4413         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4414
4415                 ret = gattc_acquire_notify(conn_info->connection_id, &srvc_id, &char_id, fd, mtu);
4416
4417         if (ret != OAL_STATUS_SUCCESS) {
4418                 BT_ERR("ret: %d", ret);
4419                 g_free(addr);
4420                 return BLUETOOTH_ERROR_INTERNAL;
4421         }
4422         BT_INFO("GATT characterstics FD [%d]  mtu[%d]", *fd, *mtu);
4423         g_free(addr);
4424         return BLUETOOTH_ERROR_NONE;
4425 }
4426
4427 /*acquire Write*/
4428 int _bt_gatt_acquire_write(bluetooth_gatt_client_char_prop_info_t *chr, int *fd, int *mtu)
4429 {
4430
4431         struct gatt_server_info_t *conn_info = NULL;
4432         oal_gatt_srvc_id_t srvc_id;
4433         oal_gatt_id_t char_id;
4434         int ret = OAL_STATUS_SUCCESS;
4435         char *addr;
4436
4437         BT_CHECK_PARAMETER(chr, return);
4438
4439         BT_INFO("+");
4440
4441         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4442         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4443
4444         /* Check if remote GATT Server is connected or not */
4445         conn_info = __bt_find_remote_gatt_server_info(addr);
4446         if (conn_info) {
4447                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4448                                 conn_info->addr, conn_info->connection_id);
4449         } else {
4450                 BT_ERR("GATT Server is not yet connected..");
4451                 g_free(addr);
4452                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4453         }
4454
4455         srvc_id.is_prmry = TRUE;
4456         srvc_id.id.inst_id = chr->svc.instance_id;
4457         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4458
4459         char_id.inst_id = chr->characteristic.instance_id;
4460         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4461
4462                 ret = gattc_acquire_write(conn_info->connection_id, &srvc_id, &char_id,
4463                                         OAL_GATT_AUTH_REQ_NONE, fd, mtu);
4464         if (ret != OAL_STATUS_SUCCESS) {
4465                 BT_ERR("ret: %d", ret);
4466                 g_free(addr);
4467                 return BLUETOOTH_ERROR_INTERNAL;
4468         }
4469         BT_INFO("GATT characterstics FD [%d]  mtu [%d]", *fd, *mtu);
4470         g_free(addr);
4471         return BLUETOOTH_ERROR_NONE;
4472
4473 }
4474
4475
4476 /* Write Characteristic */
4477 int _bt_gatt_write_characteristic_value_by_type(
4478                 bluetooth_gatt_client_char_prop_info_t *chr,
4479                 bluetooth_gatt_att_data_t *data,
4480                 bluetooth_gatt_write_type_e write_type)
4481 {
4482         struct gatt_server_info_t *conn_info = NULL;
4483         invocation_info_t *req_info = NULL;
4484         bluetooth_gatt_client_char_prop_info_t *prop;
4485         oal_gatt_srvc_id_t srvc_id;
4486         oal_gatt_id_t char_id;
4487         int ret = OAL_STATUS_SUCCESS;
4488         char *addr;
4489         int k;
4490
4491         BT_CHECK_PARAMETER(chr, return);
4492         BT_CHECK_PARAMETER(data, return);
4493
4494         BT_INFO("+");
4495
4496         /* Check if any app is already writing same char of
4497            particular service on the same remote GATT Server */
4498         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE);
4499         if (req_info) {
4500                 prop = (bluetooth_gatt_client_char_prop_info_t*)req_info->user_data;
4501                 if (prop && !memcmp(chr->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4502                                 && memcmp(chr->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4503                                 && chr->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4504                                 && memcmp(chr->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4505                                 && chr->characteristic.instance_id == prop->characteristic.instance_id) { /* Characteristic Instance ID matched */
4506                         BT_INFO("Already Characteristic Write Value operation in progress for same remote GATT Server");
4507                         /* Return and wait for events to be sent to all apps */
4508                         return BLUETOOTH_ERROR_NONE;
4509                 }
4510         }
4511
4512         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4513         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4514
4515         /* Check if remote GATT Server is connected or not */
4516         conn_info = __bt_find_remote_gatt_server_info(addr);
4517         if (conn_info) {
4518                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4519                                 conn_info->addr, conn_info->connection_id);
4520         } else {
4521                 BT_ERR("GATT Server is not yet connected..");
4522                 g_free(addr);
4523                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4524         }
4525
4526         srvc_id.is_prmry = TRUE;
4527         srvc_id.id.inst_id = chr->svc.instance_id;
4528         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4529
4530         char_id.inst_id = chr->characteristic.instance_id;
4531         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4532
4533         /* Write CHar value */
4534         /* DEBUG */
4535         BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4536         for (k = 0; k < data->length; k++)
4537                 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4538
4539         ret = gattc_write_characteristic(conn_info->connection_id,
4540                         &srvc_id, &char_id,
4541                         (oal_gatt_write_type_t)write_type, data->length,
4542                         OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4543         if (ret != OAL_STATUS_SUCCESS) {
4544                 BT_ERR("ret: %d", ret);
4545                 g_free(addr);
4546                 return BLUETOOTH_ERROR_INTERNAL;
4547         }
4548         g_free(addr);
4549         return BLUETOOTH_ERROR_NONE;
4550 }
4551
4552 /* Write Descriptor */
4553 int _bt_gatt_write_descriptor_value_by_type(
4554                 bluetooth_gatt_client_desc_prop_info_t *desc,
4555                 bluetooth_gatt_att_data_t *data,
4556                 bluetooth_gatt_write_type_e write_type)
4557 {
4558         struct gatt_server_info_t *conn_info = NULL;
4559         invocation_info_t *req_info = NULL;
4560         bluetooth_gatt_client_desc_prop_info_t *prop;
4561         oal_gatt_srvc_id_t srvc_id;
4562         oal_gatt_id_t char_id;
4563         oal_gatt_id_t desc_id;
4564         int ret = OAL_STATUS_SUCCESS;
4565         char *addr;
4566         int k;
4567
4568         BT_CHECK_PARAMETER(desc, return);
4569         BT_CHECK_PARAMETER(data, return);
4570
4571         BT_INFO("+");
4572
4573         /* Check if any app is already writing on same Descriptor of the same char of
4574            particular service on the same remote GATT Server */
4575         req_info = _bt_get_request_info_data_from_function_name(BT_GATT_WRITE_DESCRIPTOR_VALUE);
4576         if (req_info) {
4577                 prop = (bluetooth_gatt_client_desc_prop_info_t*)req_info->user_data;
4578                 if (prop && !memcmp(desc->device_address.addr, prop->device_address.addr, sizeof(bluetooth_device_address_t)) /* Address matched */
4579                                 && memcmp(desc->svc.uuid, prop->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Service UUID matched */
4580                                 && desc->svc.instance_id == prop->svc.instance_id /* Service Instance ID matched */
4581                                 && memcmp(desc->characteristic.uuid, prop->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Characteristic UUID matched */
4582                                 && desc->characteristic.instance_id == prop->characteristic.instance_id /* Characteristic Instance ID matched */
4583                                 && memcmp(desc->descriptor.uuid, prop->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN) /* Descriptor UUID matched */
4584                                 && desc->descriptor.instance_id == prop->descriptor.instance_id) { /* Descriptor Instance ID matched */
4585                         BT_INFO("Already Descriptor value Write operation in progress for same remote GATT Server");
4586                         /* Return and wait for events to be sent to all apps */
4587                         return BLUETOOTH_ERROR_NONE;
4588                 }
4589         }
4590
4591         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4592         _bt_convert_addr_type_to_string(addr, desc->device_address.addr);
4593
4594         /* Check if remote GATT Server is connected or not */
4595         conn_info = __bt_find_remote_gatt_server_info(addr);
4596         if (conn_info) {
4597                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4598                                 conn_info->addr, conn_info->connection_id);
4599         } else {
4600                 BT_ERR("GATT Server is not yet connected..");
4601                 g_free(addr);
4602                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4603         }
4604
4605         srvc_id.is_prmry = TRUE;
4606         srvc_id.id.inst_id = desc->svc.instance_id;
4607         memcpy(srvc_id.id.uuid.uuid, desc->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4608
4609         char_id.inst_id = desc->characteristic.instance_id;
4610         memcpy(char_id.uuid.uuid, desc->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4611
4612         desc_id.inst_id = desc->descriptor.instance_id;
4613         memcpy(desc_id.uuid.uuid, desc->descriptor.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4614
4615         /* DEBUG */
4616         BT_INFO("Connection ID [%d] write type [%d] data length [%d]", conn_info->connection_id, write_type, data->length);
4617         for (k = 0; k < data->length; k++)
4618                 BT_INFO("Data[%d] [0x%x]", k, data->data[k]);
4619
4620         ret = gattc_write_descriptor(conn_info->connection_id,
4621                         &srvc_id, &char_id, &desc_id,
4622                         (oal_gatt_write_type_t)write_type, data->length,
4623                         OAL_GATT_AUTH_REQ_NONE, (char *)(&data->data[0]));
4624         if (ret != OAL_STATUS_SUCCESS) {
4625                 BT_ERR("ret: %d", ret);
4626                 g_free(addr);
4627                 return BLUETOOTH_ERROR_INTERNAL;
4628         }
4629         g_free(addr);
4630         return BLUETOOTH_ERROR_NONE;
4631 }
4632
4633 int _bt_gatt_watch_characteristic(
4634                 bluetooth_gatt_client_char_prop_info_t *chr,
4635                 int client_id,
4636                 gboolean is_notify)
4637 {
4638         struct gatt_server_info_t *conn_info = NULL;
4639         oal_gatt_srvc_id_t srvc_id;
4640         oal_gatt_id_t char_id;
4641         int ret = OAL_STATUS_SUCCESS;
4642         char *addr;
4643
4644         BT_CHECK_PARAMETER(chr, return);
4645
4646         BT_INFO("Client ID [%d] Is Notify [%d]", client_id, is_notify);
4647
4648         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4649         _bt_convert_addr_type_to_string(addr, chr->device_address.addr);
4650
4651         /* Check if remote GATT Server is connected or not */
4652         conn_info = __bt_find_remote_gatt_server_info(addr);
4653         if (conn_info) {
4654                 BT_INFO("GATT Server [%s] is connected, conn Id [%d]",
4655                                 conn_info->addr, conn_info->connection_id);
4656         } else {
4657                 BT_ERR("GATT Server is not yet connected..");
4658                 g_free(addr);
4659                 return BLUETOOTH_ERROR_NOT_CONNECTED;
4660         }
4661         srvc_id.is_prmry = TRUE;
4662         srvc_id.id.inst_id = chr->svc.instance_id;
4663         memcpy(srvc_id.id.uuid.uuid, chr->svc.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4664
4665         char_id.inst_id = chr->characteristic.instance_id;
4666         memcpy(char_id.uuid.uuid, chr->characteristic.uuid, BLUETOOTH_UUID_HEX_MAX_LEN);
4667
4668         /* Register or unregister Notification characteristic */
4669         if (is_notify)
4670                 ret = gattc_register_for_notification(conn_info->client_id,
4671                                 (bt_address_t*)&(chr->device_address),
4672                                 &srvc_id, &char_id);
4673         else
4674                 ret = gattc_deregister_for_notification(conn_info->client_id,
4675                                 (bt_address_t*)&(chr->device_address),
4676                                 &srvc_id, &char_id);
4677
4678         BT_INFO("Result[%d]", ret);
4679         if (ret != OAL_STATUS_SUCCESS) {
4680                 BT_ERR("ret: %d", ret);
4681                 g_free(addr);
4682                 return BLUETOOTH_ERROR_INTERNAL;
4683         }
4684         g_free(addr);
4685         return BLUETOOTH_ERROR_NONE;
4686 }
4687
4688
4689 int _bt_disconnect_le_device(bluetooth_device_address_t *address,
4690                 int client_id)
4691 {
4692         struct gatt_server_info_t *conn_info = NULL;
4693         invocation_info_t *req_info = NULL;
4694         int ret = OAL_STATUS_SUCCESS;
4695         char *addr;
4696         char *remote_address = NULL;
4697
4698         BT_CHECK_PARAMETER(address, return);
4699
4700         BT_INFO("+");
4701
4702         addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
4703         _bt_convert_addr_type_to_string(addr, address->addr);
4704         BT_INFO("GATT Client Disconnect request for address [%s]", addr);
4705
4706         /* Check if Remote Device is already under connection progress */
4707         req_info = _bt_get_request_info_data_from_function_name(BT_DISCONNECT_LE);
4708         if (req_info) {
4709                 remote_address = (char*)req_info->user_data;
4710                 if (remote_address && !strcasecmp(remote_address, addr)) {/* Address matched */
4711                         BT_INFO("Already DisConnection ongoing for same remote GATT Server address [%s]", remote_address);
4712                         /* Return success and wait for events to be sent to all apps */
4713                         g_free(addr);
4714                         return BLUETOOTH_ERROR_IN_PROGRESS;
4715                 }
4716         }
4717         /* Check if remote GATT Server is connected or not */
4718         conn_info = __bt_find_remote_gatt_server_info(addr);
4719         if (!conn_info) {
4720                 BT_ERR("GATT Server is not connected..");
4721                 g_free(addr);
4722                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
4723         }
4724
4725         /* Check if app sent 0 client id for Disconnection, in such case, use default gatt client ID */
4726         if (client_id == 0) {
4727                 BT_INFO("GATT CLient Disconnect request sent by an app without any client instance [%d]",
4728                                 client_id);
4729                 BT_INFO("Assign default GATT client id [%d]", gatt_default_client);
4730                 client_id = gatt_default_client;
4731         }
4732
4733         BT_INFO("Disconnect using CLient ID [%d] Connection ID [%d]", client_id, conn_info->connection_id);
4734         ret = gattc_disconnect(client_id, (bt_address_t*)(address),
4735                         conn_info->connection_id);
4736
4737         if (ret != OAL_STATUS_SUCCESS) {
4738                 BT_ERR("ret: %d", ret);
4739                 g_free(addr);
4740                 return BLUETOOTH_ERROR_INTERNAL;
4741         }
4742         g_free(addr);
4743         return BLUETOOTH_ERROR_NONE;
4744 }
4745
4746 int _bt_gatt_watch_service_changed_indication(const char *sender,
4747                 bluetooth_device_address_t *address,
4748                 gboolean is_enabled)
4749 {
4750         int k;
4751         bt_service_app_info_t *info = NULL;
4752
4753         BT_INFO("Enable Servic changed Indication watcher [%d] for app [%s]",
4754                         is_enabled, sender);
4755
4756         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4757                 info = &numapps[k];
4758
4759                 if (g_strcmp0(sender, info->sender) == 0 &&
4760                                 memcmp(info->address.addr, address->addr,
4761                                         sizeof(bluetooth_device_address_t)) == 0) {
4762                         BT_INFO("Found GATT client App.. UUID [%s], sender [%s]", info->uuid, info->sender);
4763                         info->is_watcher_enabled = is_enabled;
4764                 }
4765         }
4766
4767         return BLUETOOTH_ERROR_NONE;
4768 }
4769
4770 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
4771 {
4772         BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
4773                         sender, client_id);
4774         int result = BLUETOOTH_ERROR_NONE;
4775         int k;
4776
4777         /* Unregister CLient instance associated with address X. It is possible that another app still
4778            has client_id valid for same remote address */
4779         bt_service_app_info_t *info = NULL;
4780
4781         for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
4782                 info = &numapps[k];
4783
4784                 /* Exact matching of sender */
4785                 if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) {  /* Check for only valid GATT client Instance */
4786                         BT_INFO("Unregister GATT client instance [%d]", info->client_id);
4787                         result = __bt_do_unregister_gatt_instance(info->client_id);
4788                         if (result != BLUETOOTH_ERROR_NONE)
4789                                 BT_ERR("Error in unregistering GATT Client Interface");
4790
4791                         break;
4792                 }
4793         }
4794         return result;
4795 }
4796
4797 #endif