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