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