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