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