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