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