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