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