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