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