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