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