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