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