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