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