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