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