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