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