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