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