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