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