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