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