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