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