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