gatt client adaptation feature changes on HAL
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-client.c
1 /*
2  * BLUETOOOTH HAL
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dlog.h>
29 #include <gio/gio.h>
30 #include <glib.h>
31
32
33 #include "bt-hal-log.h"
34 #include "bt-hal-msg.h"
35 #include "bt-hal-event-receiver.h"
36 #include "bt-hal-adapter-le.h"
37
38 #include "bt-hal-gatt-client.h"
39 #include "bt-hal-dbus-common-utils.h"
40 #include "bt-hal-utils.h"
41
42 /************************************************************************************
43  **  Static variables
44  ************************************************************************************/
45 extern const btgatt_callbacks_t *bt_gatt_callbacks;
46 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
47 {\
48         ERR("%s: BTGATT not initialized", __FUNCTION__);\
49         return BT_STATUS_NOT_READY;\
50 } else {\
51         DBG("%s", __FUNCTION__);\
52 }
53
54 #ifdef TIZEN_BT_HAL
55 int le_scan_type = BT_GATTC_LE_SCAN_TYPE_PASSIVE;
56 #endif
57
58 static handle_stack_msg event_cb = NULL;
59
60 #define GATT_SERV_INTERFACE             "org.bluez.GattService1"
61 #define MAX_HAL_OBJECT_PATH_LEN         100
62
63 typedef enum {
64         HAL_GATT_CHARACTERISTIC_PROPERTY_BROADCAST = 0x01,
65         HAL_GATT_CHARACTERISTIC_PROPERTY_READ = 0x02,
66         HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE = 0x04,
67         HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE = 0x08,
68         HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY = 0x10,
69         HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE = 0x20,
70         HAL_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE = 0x40,
71         HAL_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE = 0x80,
72         HAL_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES = 0x100,
73         HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ = 0x200,
74         HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE = 0x400,
75         HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ = 0x800,
76         HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE = 0x1000,
77         HAL_GATT_CHARACTERISTIC_PROPERTY_EXTENDED_PROPS = 0xffff,
78 } bt_gatt_characteristic_property_t;
79
80
81 typedef enum {
82         HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE = 1,
83         HAL_GATT_WRITE_TYPE_WRITE ,
84 } hal_gatt_write_type_t;
85
86
87 typedef enum {
88         HAL_GATT_PROPERTY_BROADCAST = 0x01,  /**< Broadcast property */
89         HAL_GATT_PROPERTY_READ = 0x02,  /**< Read property */
90         HAL_GATT_PROPERTY_WRITE_WITHOUT_RESPONSE = 0x04,  /**< Write without response property */
91         HAL_GATT_PROPERTY_WRITE = 0x08,  /**< Write property */
92         HAL_GATT_PROPERTY_NOTIFY = 0x10,  /**< Notify property */
93         HAL_GATT_PROPERTY_INDICATE = 0x20,  /**< Indicate property */
94         HAL_GATT_PROPERTY_AUTHENTICATED_SIGNED_WRITES = 0x40,  /**< Authenticated signed writes property */
95         HAL_GATT_PROPERTY_EXTENDED_PROPERTIES = 0x80,  /**< Extended properties */
96 } hal_gatt_property_e;
97
98 typedef struct {
99         int client_if;
100         bt_bdaddr_t bd_addr;
101         gboolean auto_connect;
102 } bt_pending_le_conn_info_s;
103
104 typedef struct {
105         char *desc_path;
106         bt_uuid_t desc_uuid;
107 } hal_gattc_desc_t;
108
109 typedef struct {
110         char *chr_path;
111         bt_uuid_t chr_uuid;
112         unsigned int permission;
113         GSList *gatt_list_descs;
114 } hal_gattc_char_t;
115
116 typedef struct {
117         gchar *svc_path;
118         bt_uuid_t svc_uuid;
119         GSList *gatt_list_chars;
120 } hal_gattc_service_t;
121
122 typedef struct {
123         bt_bdaddr_t bd_addr;    /*remote server address*/
124         int inst_id;            /*server instance id*/
125         GSList *gatt_list_services;
126 } hal_gattc_server_info_t;
127
128 /* Linked List of connected GATT server */
129 static GSList *hal_gattc_server_info_list = NULL;
130
131 typedef struct {
132         int client_if;
133         bt_bdaddr_t bd_addr;    /*remote server address*/
134         int conn_id;
135         int inst_id;
136 } hal_gattc_client_info_t;
137
138 /* Linked list of connected GATT client connection */
139 static GSList * hal_gattc_client_info_list = NULL;
140
141
142 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
143 static guint pending_le_conn_timer_id = 0;
144 static int bt_conn_id = 0;
145 static int bt_inst_id = 0;
146
147 #define BT_GATTC_CL_MAX 32
148
149 typedef struct {
150         int conn_id;
151         btgatt_srvc_id_t srvc_id;
152         btgatt_gatt_id_t char_id;
153         btgatt_gatt_id_t desc_id;
154 } hal_gatt_resp_data_t;
155
156
157 typedef struct {
158         int client_if;
159         bt_uuid_t app_uuid;
160 } hal_gatt_client_app;
161
162 static GSList * hal_gattc_client_app_list = NULL;
163
164 static int bt_client_if = 0;
165
166
167 static bt_status_t __bt_connect_le_device_internal(int client_if, const bt_bdaddr_t *bd_addr,
168                 gboolean auto_connect);
169 static bt_status_t _bt_hold_current_advertising();
170 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data);
171 static gboolean __bt_connect_le_timer_cb(gpointer user_data);
172 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
173                 gpointer user_data);
174 static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int  conn_id);
175 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
176                                                 const char* uuid_str, int inst_id);
177 static void _bt_hal_send_search_service_complete_event(int conn_id, int status);
178
179
180 /* To send stack event to hal-av handler */
181 void _bt_hal_register_gatt_client_handler_cb(handle_stack_msg cb)
182 {
183         event_cb = cb;
184 }
185
186 void _bt_hal_unregister_gatt_client_handler_cb(void)
187 {
188         event_cb = NULL;
189 }
190
191 #ifdef TIZEN_BT_HAL
192 int _bt_hal_gatt_client_get_le_scan_type(void)
193 {
194         return le_scan_type;
195 }
196 #endif
197
198 static gboolean __bt_hal_register_client_cb(gpointer user_data)
199 {
200         struct hal_ev_gatt_client_registered ev;
201         hal_gatt_client_app *client_info = user_data;
202
203         DBG("+");
204         /* Prepare to send AV connecting event */
205         memset(&ev, 0, sizeof(ev));
206         ev.status = BT_STATUS_SUCCESS;
207         ev.client_if = client_info->client_if;
208         memcpy(ev.app_uuid, client_info->app_uuid.uu, sizeof(ev.app_uuid));
209
210         if (!event_cb)
211                 ERR("GATT Callback not registered");
212         else {
213                 DBG("GATT client registered, client_if: [%d]", client_info->client_if);
214                 event_cb(HAL_EV_GATT_CLIENT_REGISTERED, (void *)&ev, sizeof(ev));
215         }
216
217         g_free(user_data);
218         return FALSE;
219 }
220
221 static int __hal_generate_client_id()
222 {
223         return ++bt_client_if;
224 }
225
226 static hal_gatt_client_app *__hal_gattc_add_client_app(bt_uuid_t *app_uuid)
227 {
228         GSList *l;
229         hal_gatt_client_app *info = NULL;
230         hal_gatt_client_app *gattc_app = NULL;
231
232         //check if client app is already registered
233         for (l = hal_gattc_client_app_list; l != NULL; l = g_slist_next(l)) {
234                 info = (hal_gatt_client_app*)l->data;
235                 if (info == NULL)
236                         continue;
237
238                 if (memcmp(&info->app_uuid, app_uuid, sizeof(bt_uuid_t)) == 0) {
239                         DBG("gatt client app already registered");
240                         return info;
241                 }
242         }
243
244         DBG("adding the gatt client app");
245
246         //add client app
247         gattc_app = g_malloc0(sizeof(hal_gatt_client_app));
248         if (gattc_app == NULL) {
249                 DBG("Failed to allocate memory");
250                 return NULL;
251         }
252
253         gattc_app->client_if = __hal_generate_client_id();
254         memcpy(&gattc_app->app_uuid, app_uuid, sizeof(bt_uuid_t));
255
256         hal_gattc_client_app_list = g_slist_append(hal_gattc_client_app_list, gattc_app);
257
258         return gattc_app;
259 }
260
261 static bt_status_t __hal_gattc_register_client_app(bt_uuid_t *app_uuid)
262 {
263         hal_gatt_client_app *gattc_app = NULL;
264         hal_gatt_client_app *client_app_info = NULL;
265
266         DBG("+");
267         /* add gatt client in list */
268         gattc_app = __hal_gattc_add_client_app(app_uuid);
269         if (gattc_app == NULL) {
270                 ERR("Failed to register gatt client app");
271                 return BT_STATUS_FAIL;
272         }
273
274         /*send event */
275         client_app_info = g_malloc0(sizeof(hal_gatt_client_app));
276         if (NULL == client_app_info) {
277                 ERR("Failed to allocate memory");
278                 return BT_STATUS_FAIL;
279         }
280
281         client_app_info->client_if = gattc_app->client_if;
282         memcpy(&client_app_info->app_uuid, app_uuid, sizeof(bt_uuid_t));
283         g_idle_add(__bt_hal_register_client_cb, (gpointer)client_app_info);
284
285         DBG("registered client client_if [%d]", client_app_info->client_if);
286
287         return BT_STATUS_SUCCESS;
288 }
289
290 /** Registers a GATT client application with the stack */
291 bt_status_t btif_gattc_register_client(bt_uuid_t *uuid)
292 {
293         CHECK_BTGATT_INIT();
294         DBG("+");
295
296         return __hal_gattc_register_client_app(uuid);
297 }
298
299 bt_status_t __hal_gattc_unregister_client(int client_if)
300 {
301         GSList *l;
302         hal_gatt_client_app *info = NULL;
303
304         DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
305
306         /* remove the gatt client app */
307         for (l = hal_gattc_client_app_list; l != NULL; ) {
308                 info = (hal_gatt_client_app*)l->data;
309                 l = g_slist_next(l);
310
311                 if (info == NULL)
312                         continue;
313
314                 if (info->client_if == client_if) {
315                         DBG("gatt client app found");
316                         hal_gattc_client_app_list = g_slist_remove(hal_gattc_client_app_list, info);
317                         g_free(info);
318                 }
319         }
320
321         DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
322         return BT_STATUS_SUCCESS;
323 }
324
325 /** Unregister a client application from the stack */
326 bt_status_t btif_gattc_unregister_client(int client_if)
327 {
328         CHECK_BTGATT_INIT();
329         DBG("+");
330
331         return __hal_gattc_unregister_client(client_if);
332 }
333
334 /** Start or stop LE device scanning */
335 bt_status_t scan(int client_if, bool start)
336 {
337         int ret;
338
339         CHECK_BTGATT_INIT();
340
341         if (start)
342                 ret = _bt_hal_adapter_le_start_scan();
343         else
344                 ret = _bt_hal_adapter_le_stop_scan();
345
346         return ret;
347 }
348
349 /** Create a connection to a remote LE or dual-mode device */
350 bt_status_t btif_gattc_client_connect(int client_if, const bt_bdaddr_t *bd_addr,
351                 bool is_direct)
352 {
353         int ret = BT_STATUS_SUCCESS;
354
355         CHECK_BTGATT_INIT();
356         DBG("+");
357
358         if (NULL == bd_addr)
359                 return BT_STATUS_PARM_INVALID;
360
361         ret = _bt_hold_current_advertising();
362         if (ret == BT_STATUS_SUCCESS) {
363                 DBG("Current advertising is held");
364                 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
365                 pending_le_conn_info->client_if = client_if;
366                 memcpy(pending_le_conn_info->bd_addr.address, bd_addr->address,
367                                 BT_HAL_ADDRESS_LENGTH_MAX);
368                 pending_le_conn_info->auto_connect = is_direct;
369
370                 pending_le_conn_timer_id =
371                         g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
372
373                 return BT_STATUS_SUCCESS;
374         } else {
375                 ERR("advertising is not stopped");
376         }
377
378         return __bt_connect_le_device_internal(client_if, bd_addr, is_direct);
379 }
380
381
382 static void __le_disconnection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
383                 gpointer user_data)
384 {
385         GError *g_error = NULL;
386         GVariant *reply = NULL;
387         hal_gattc_client_info_t *gatt_conn_info  = user_data;
388         int result = BT_STATUS_SUCCESS;
389         struct hal_ev_gatt_client_connected ev;
390
391         DBG("+");
392
393         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
394         g_object_unref(proxy);
395         if (reply == NULL) {
396                 ERR("Connect LE Dbus Call Error");
397                 if (g_error) {
398                         ERR("Error: %s\n", g_error->message);
399                         g_clear_error(&g_error);
400                 }
401                 result = BT_STATUS_FAIL;
402         }
403         g_variant_unref(reply);
404
405         if (NULL == gatt_conn_info) {
406                 ERR("server_data is NULL");
407                 return;
408         }
409
410         /*send fail event*/
411         if (result == BT_STATUS_FAIL) {
412                 memset(&ev, 0, sizeof(ev));
413                 ev.conn_id = gatt_conn_info->conn_id;
414                 ev.status = result;
415                 ev.client_if = gatt_conn_info->client_if;
416                 memcpy(ev.bdaddr, gatt_conn_info->bd_addr.address,
417                                                 BT_HAL_ADDRESS_LENGTH_MAX);
418
419                 if (!event_cb) {
420                         ERR("gatt client callback not registered");
421                 } else {
422                         DBG("sending gatt client disconnected event");
423                         event_cb(HAL_EV_GATT_CLIENT_DISCONNECTED, (void *)&ev, sizeof(ev));
424                 }
425         }
426
427         /*remove conn_info*/
428         if (gatt_conn_info)
429                 g_free(gatt_conn_info);
430
431         DBG("-");
432 }
433
434 bt_status_t _hal_gattc_disconnect(int client_if, const bt_bdaddr_t *bd_addr,
435                         int conn_id)
436 {
437         char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
438         gchar *device_path;
439         GDBusProxy *device_proxy;
440         GDBusConnection *conn;
441         int ret = BT_STATUS_SUCCESS;
442         hal_gattc_client_info_t *gattc_data;
443
444         if (NULL == bd_addr) {
445                 ERR("bd_addr is NULL");
446                 return BT_STATUS_PARM_INVALID;
447         }
448
449         conn = _bt_hal_get_system_gconn();
450         if (NULL == conn) {
451                 ERR("_bt_gdbus_get_system_gconn returned NULL");
452                 return BT_STATUS_FAIL;
453         }
454
455         _bt_hal_convert_addr_type_to_string(device_address,
456                         (unsigned char *)bd_addr->address);
457         device_path = _bt_hal_get_device_object_path(device_address);
458         if (device_path == NULL) {
459                 DBG("device_path NULL");
460                 ret =  BT_STATUS_FAIL;
461                 return ret;
462         }
463
464         ERR("device_path:%s", device_path);
465
466         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
467                         NULL, BT_HAL_BLUEZ_NAME,
468                         device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
469         g_free(device_path);
470         if (NULL == device_proxy) {
471                 ERR("device_proxy returned NULL");
472                 return BT_STATUS_FAIL;
473         }
474
475         gattc_data = g_malloc0(sizeof(hal_gattc_client_info_t));
476         if (NULL == gattc_data) {
477                 ERR("Unable to allocate memory");
478                 ret = BT_STATUS_FAIL;
479                 goto fail;
480         }
481
482         memcpy(gattc_data->bd_addr.address, bd_addr->address,
483                         BT_HAL_ADDRESS_LENGTH_MAX);
484         gattc_data->client_if = client_if;
485         gattc_data->conn_id = conn_id;
486
487         DBG("DisconnectLE [%s]", device_address);
488
489         g_dbus_proxy_call(device_proxy, "DisconnectLE",
490                         NULL,
491                         G_DBUS_CALL_FLAGS_NONE,
492                         BT_HAL_MAX_DBUS_TIMEOUT,
493                         NULL,
494                         (GAsyncReadyCallback)__le_disconnection_req_cb, gattc_data);
495         return ret;
496 fail:
497         if (device_proxy)
498                 g_object_unref(device_proxy);
499
500         g_free(gattc_data);
501
502         return ret;
503 }
504
505 /** Disconnect a remote device or cancel a pending connection */
506 bt_status_t btif_gattc_client_disconnect(int client_if, const bt_bdaddr_t *bd_addr,
507                 int conn_id)
508 {
509         CHECK_BTGATT_INIT();
510
511         DBG("+");
512
513         return _hal_gattc_disconnect(client_if, bd_addr, conn_id);
514 }
515
516 /** Clear the attribute cache for a given device */
517 bt_status_t refresh(int client_if, const bt_bdaddr_t *bd_addr)
518 {
519         CHECK_BTGATT_INIT();
520         return BT_STATUS_UNSUPPORTED;
521 }
522
523 static hal_gattc_service_t* _gattc_find_service_from_uuid(hal_gattc_server_info_t *conn_info, bt_uuid_t *svc_uuid)
524 {
525         DBG("+");
526
527         GSList *l;
528         hal_gattc_service_t *info = NULL;
529
530         for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
531                 info = (hal_gattc_service_t*)l->data;
532                 if (info == NULL)
533                         continue;
534
535                 if (!memcmp(&info->svc_uuid, svc_uuid, sizeof(bt_uuid_t))) {
536                         INFO("Found GATT service uuid");
537                         return info;
538                 }
539         }
540         return NULL;
541 }
542
543
544 static hal_gattc_char_t* _gattc_find_char_from_uuid(hal_gattc_service_t *gattc_svc, bt_uuid_t *char_uuid)
545 {
546         DBG("+");
547
548         GSList *l;
549         hal_gattc_char_t *info = NULL;
550
551         for (l = gattc_svc->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
552                 info = (hal_gattc_char_t*)l->data;
553                 if (info == NULL)
554                         continue;
555
556                 if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t))) {
557                         INFO("Found GATT char uuid");
558                         return info;
559                 }
560         }
561         return NULL;
562 }
563
564 static hal_gattc_desc_t* _gattc_find_desc_from_uuid(hal_gattc_char_t *gattc_char, bt_uuid_t *desc_uuid)
565 {
566         DBG("+");
567
568         GSList *l;
569         hal_gattc_desc_t *info = NULL;
570
571         for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
572                 info = (hal_gattc_desc_t*)l->data;
573                 if (info == NULL)
574                         continue;
575
576                 if (!memcmp(&info->desc_uuid, desc_uuid, sizeof(bt_uuid_t))) {
577                         INFO("Found GATT descriptor uuid");
578                         return info;
579                 }
580         }
581         return NULL;
582 }
583
584
585 static hal_gattc_service_t* _hal_gatt_client_add_service(hal_gattc_server_info_t *conn_info,
586                 const char *uuid_str, char *object_path)
587 {
588         DBG("+");
589         hal_gattc_service_t *gattc_service = NULL;
590
591         gattc_service = g_malloc0(sizeof(hal_gattc_service_t));
592         gattc_service->svc_path = g_strdup(object_path);
593         _bt_hal_convert_uuid_string_to_type(gattc_service->svc_uuid.uu, uuid_str);
594
595         DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
596
597         conn_info->gatt_list_services = g_slist_append(conn_info->gatt_list_services, gattc_service);
598
599         DBG("svc path {%s] svc uuid [%s]", object_path, uuid_str);
600
601         return gattc_service;
602 }
603
604 static void _hal_gattc_add_characteristic(hal_gattc_service_t *gatt_svc, char *char_handle)
605 {
606         DBG("+");
607         hal_gattc_char_t *gattc_char = NULL;
608
609         gattc_char = g_malloc0(sizeof(hal_gattc_char_t));
610         gattc_char->chr_path = g_strdup(char_handle);
611
612         DBG("svc path: [%s]", gatt_svc->svc_path);
613         DBG("char path: [%s]", gattc_char->chr_path);
614
615         gatt_svc->gatt_list_chars = g_slist_append(gatt_svc->gatt_list_chars, gattc_char);
616 }
617
618 static void _gattc_create_new_service(hal_gattc_server_info_t *conn_info, gboolean is_primary,
619                 const char* uuid_str, char *object_path, GPtrArray *gp_char_array)
620 {
621         hal_gattc_service_t* gatt_svc = NULL;
622         int i;
623         gchar *gp_char_path = NULL;
624
625         DBG("+");
626
627         /* add the service */
628         gatt_svc = _hal_gatt_client_add_service(conn_info, uuid_str, object_path);
629         if (gatt_svc == NULL) {
630                 ERR("Failed to add service");
631                 return;
632         }
633
634         /* add the characteristic */
635         for (i = 0; i < gp_char_array->len; i++) {
636                 gp_char_path = g_ptr_array_index(gp_char_array, i);
637                 _hal_gattc_add_characteristic(gatt_svc, gp_char_path);
638         }
639
640         g_ptr_array_free(gp_char_array, TRUE);
641 }
642
643 static void _hal_gattc_add_descriptor(hal_gattc_char_t *gattc_char, char *desc_path)
644 {
645         hal_gattc_desc_t *gattc_desc = NULL;
646
647         gattc_desc = g_malloc0(sizeof(hal_gattc_desc_t));
648         gattc_desc->desc_path = g_strdup(desc_path);
649
650         gattc_char->gatt_list_descs = g_slist_append(gattc_char->gatt_list_descs, gattc_desc);
651
652         DBG("char path: [%s]", gattc_char->chr_path);
653         DBG("desc path: [%s]", gattc_desc->desc_path);
654 }
655
656 static void _hal_gattc_update_char_property(hal_gattc_char_t *gattc_char, const char* char_uuid_str,
657                 GPtrArray *gp_desc_array, unsigned int char_permission)
658 {
659         gchar *gp_desc_path = NULL;
660         int i;
661
662         DBG("+");
663
664         if (char_uuid_str == NULL) {
665                 DBG("char_uuid_str is NULL");
666                 return;
667         }
668
669         //update the char uuid
670         DBG("char UUID: [%s] ", char_uuid_str);
671         DBG("char path: [%s]", gattc_char->chr_path);
672
673         _bt_hal_convert_uuid_string_to_type(gattc_char->chr_uuid.uu, char_uuid_str);
674
675         //update char permission
676         gattc_char->permission = char_permission;
677
678         //add the descriptor
679         for (i = 0; i < gp_desc_array->len; i++) {
680                 gp_desc_path = g_ptr_array_index(gp_desc_array, i);
681                 _hal_gattc_add_descriptor(gattc_char, gp_desc_path);
682         }
683 }
684
685 static void _hal_gattc_update_desc_property(hal_gattc_desc_t *gattc_desc, const char* desc_uuid_str)
686 {
687         DBG("+");
688
689         if (desc_uuid_str == NULL) {
690                 DBG("char_uuid_str is NULL");
691                 return;
692         }
693
694         //update the descriptor uuid
695         DBG("desc UUID: [%s] ", desc_uuid_str);
696         DBG("desc path: [%s]", gattc_desc->desc_path);
697
698         _bt_hal_convert_uuid_string_to_type(gattc_desc->desc_uuid.uu, desc_uuid_str);
699 }
700
701 static void browse_service_char(int conn_id)
702 {
703         hal_gattc_server_info_t *conn_info = NULL;
704         GSList *l;
705         GSList *k;
706         GSList *m;
707         hal_gattc_service_t *svc_info = NULL;
708         hal_gattc_char_t *char_info = NULL;
709         hal_gattc_desc_t *desc_info = NULL;
710
711         DBG("+");
712
713         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
714         if (conn_info == NULL) {
715                 DBG("conn_info is NULL");
716                 return;
717         }
718
719         DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
720
721         for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
722                 svc_info = (hal_gattc_service_t*)l->data;
723                 if (svc_info == NULL)
724                         continue;
725
726                 DBG("svc path [%s]", svc_info->svc_path);
727
728                 /* find characteristic object path */
729                 for (k = svc_info->gatt_list_chars; k != NULL; k = g_slist_next(k)) {
730                         char_info = (hal_gattc_char_t *)k->data;
731                         if (char_info == NULL)
732                                 continue;
733
734                         DBG("char path[%s]", char_info->chr_path);
735
736                         /* descriptor */
737                         for (m = char_info->gatt_list_descs; m != NULL; m = g_slist_next(m)) {
738                                 desc_info = (hal_gattc_desc_t *)m->data;
739                                 if (desc_info == NULL)
740                                         continue;
741
742                                 DBG("desc path[%s]", desc_info->desc_path);
743                         }
744                 }
745         }
746 }
747
748
749
750 /**
751  * Enumerate all GATT services on a connected device.
752  * Optionally, the results can be filtered for a given UUID.
753  */
754 static bt_status_t _gattc_client_search_service(int conn_id)
755 {
756         CHECK_BTGATT_INIT();
757
758         GVariant *result = NULL;
759         GVariantIter *iter;
760         GVariantIter *svc_iter;
761         GVariantIter *interface_iter;
762         char *object_path = NULL;
763         char *interface_str = NULL;
764         const gchar *key = NULL;
765         GVariant *value = NULL;
766         GPtrArray *gp_array  = NULL;
767         char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
768         char temp_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
769         int ret = BT_STATUS_FAIL;
770         int idx = 0;
771         const gchar *uuid_str = NULL;
772         gsize len = 0;
773         hal_gattc_server_info_t *conn_info = NULL;
774         gboolean is_primary = FALSE;
775         int svc_count = 0;
776
777         char *char_handle = NULL;
778         GVariantIter *char_iter = NULL;
779         GPtrArray *gp_char_array  = NULL;
780
781         DBG("+");
782
783         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
784         if (NULL == conn_info) {
785                 DBG("Failed to get the conn_info");
786                 return BT_STATUS_FAIL;
787         }
788
789         _bt_hal_convert_addr_type_to_string(device_address,
790                         (unsigned char *)conn_info->bd_addr.address);
791
792         result = _bt_hal_get_managed_objects();
793         if (result == NULL)
794                 return ret;
795
796         gp_array = g_ptr_array_new();
797         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
798
799         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
800                                 &interface_iter)) {
801                 if (object_path == NULL)
802                         continue;
803
804                 _bt_hal_convert_device_path_to_address(object_path, temp_address);
805
806                 if (g_strcmp0(temp_address, device_address) != 0)
807                         continue;
808
809                 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
810                                         &interface_str, &svc_iter)) {
811                         if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
812                                 continue;
813
814                         DBG("[%d] Object Path : %s", idx++, object_path);
815                         /* for characteristic */
816                         gp_char_array = g_ptr_array_new();
817                         while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
818                                 if (g_strcmp0(key, "Primary") == 0) {
819                                         is_primary = g_variant_get_boolean(value);
820                                         DBG("primary");
821                                         if (is_primary) {
822                                                 g_ptr_array_add(gp_array, (gpointer)object_path);
823                                                 svc_count++;
824                                         }
825                                 } else if (g_strcmp0(key, "UUID") == 0) {
826                                         uuid_str = g_variant_get_string(value, &len);
827                                         DBG(" UUID: [%s]", uuid_str);
828                                 } else if (g_strcmp0(key, "Characteristics") == 0) {
829                                         g_variant_get(value, "ao", &char_iter);
830                                         if (char_iter != NULL) {
831                                                 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
832                                                         DBG("char handle : %s", char_handle);
833                                                         g_ptr_array_add(gp_char_array, (gpointer)char_handle);
834                                                 }
835                                         }
836                                 }
837                         }
838
839                         DBG("send search service result event");
840                         _bt_hal_send_search_service_result_event(conn_id, is_primary,
841                                         uuid_str, conn_info->inst_id);
842
843                         _gattc_create_new_service(conn_info, is_primary, uuid_str, object_path, gp_char_array);
844                 }
845         }
846
847         if (gp_array->len == 0 || svc_count == 0) {
848                 ERR("gp_array is NULL");
849                 ret = BT_STATUS_FAIL;
850         } else {
851                 ret = BT_STATUS_SUCCESS;
852         }
853
854         browse_service_char(conn_id);
855         /* send search service complete event */
856         _bt_hal_send_search_service_complete_event(conn_id, ret);
857
858         g_ptr_array_free(gp_array, TRUE);
859         g_variant_iter_free(iter);
860         g_variant_unref(result);
861         DBG("-");
862         return ret;
863 }
864
865 bt_status_t btif_gattc_client_search_service(int conn_id, bt_uuid_t *filter_uuid)
866 {
867         if (NULL == filter_uuid) {
868                 DBG("Browse all the services");
869                 return _gattc_client_search_service(conn_id);
870         } else {
871                 DBG("TODO implement it");
872                 return BT_STATUS_UNSUPPORTED;
873         }
874 }
875 /**
876  * Enumerate included services for a given service.
877  * Set start_incl_srvc_id to NULL to get the first included service.
878  */
879 bt_status_t get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
880                 btgatt_srvc_id_t *start_incl_srvc_id)
881 {
882         CHECK_BTGATT_INIT();
883         return BT_STATUS_UNSUPPORTED;
884 }
885
886 static void _bt_hal_send_client_char_search_result_event(int conn_id, int status,
887                 btgatt_srvc_id_t *svc_id, bt_uuid_t *char_uuid, int char_prop)
888 {
889         struct hal_ev_gatt_client_char_search_result  ev;
890
891          if (!event_cb) {
892                 ERR("gatt client callback not registered");
893                 return;
894         }
895
896         DBG("sending gatt client search char result event conn_id[%d] status[%d]", conn_id, status);
897
898         memset(&ev, 0, sizeof(ev));
899         ev.conn_id = conn_id;
900         ev.inst_id = svc_id->id.inst_id;
901         ev.is_primary = svc_id->is_primary;
902         ev.status = status;
903         memcpy(ev.svc_uuid, svc_id->id.uuid.uu, sizeof(ev.svc_uuid));
904
905         if (status == BT_STATUS_SUCCESS) {
906                 DBG("building char uuid");
907                 memcpy(ev.char_uuid, char_uuid->uu, sizeof(ev.char_uuid));
908                 ev.char_prop = char_prop;
909         }
910
911         DBG("sending the char search event");
912
913         event_cb(HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
914 }
915
916 static int _hal_get_permission_flag(char *permission)
917 {
918         int ret = 0;
919
920         if (NULL == permission) {
921                 ERR("gatt permission is NULL");
922                 return ret;
923         }
924
925         if (!g_strcmp0(permission, "broadcast"))
926                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_BROADCAST;
927         else if (!g_strcmp0(permission, "read"))
928                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_READ;
929         else if (!g_strcmp0(permission, "write-without-response"))
930                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
931         else if (!g_strcmp0(permission, "write"))
932                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
933         else if (!g_strcmp0(permission, "notify"))
934                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
935         else if (!g_strcmp0(permission, "indicate"))
936                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
937         else if (!g_strcmp0(permission, "authenticated-signed-writes"))
938                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE;
939         else if (!g_strcmp0(permission, "reliable-write"))
940                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE;
941         else if (!g_strcmp0(permission, "writable-auxiliaries"))
942                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES;
943         else if (!g_strcmp0(permission, "encrypt-read"))
944                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
945         else if (!g_strcmp0(permission, "encrypt-write"))
946                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
947         else if (!g_strcmp0(permission, "encrypt-authenticated-read"))
948                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
949         else if (!g_strcmp0(permission, "encrypt-authenticated-write"))
950                 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
951
952         return ret;
953 }
954
955
956 static bt_status_t _hal_gattc_get_characteristic_info(hal_gattc_char_t *gattc_char)
957 {
958         GDBusProxy *properties_proxy = NULL;
959         GError *error = NULL;
960         GVariant *value = NULL;
961         GVariant *result = NULL;
962         GDBusConnection *g_conn;
963         const gchar *key;
964         char *char_desc_handle = NULL;
965         gsize len;
966         GVariantIter *property_iter;
967         GVariantIter *char_desc_iter;
968         char* char_handle = NULL;
969         const gchar *char_uuid_str = NULL;
970         GPtrArray *gp_desc_array  = NULL;
971         GVariantIter *char_perm_iter;
972         gchar* permission;
973         unsigned int char_permission = 0 ;
974
975         DBG("+");
976
977         if (gattc_char->chr_path == NULL) {
978                 DBG("char path is NULL");
979                  return BT_STATUS_FAIL;
980         }
981         char_handle = gattc_char->chr_path;
982
983         DBG("char path:[%s]", gattc_char->chr_path);
984
985         g_conn = _bt_hal_get_system_gconn();
986         if (NULL == g_conn) {
987                  ERR("_bt_gdbus_get_system_gconn returned NULL");
988                 return BT_STATUS_FAIL;
989         }
990
991         properties_proxy = g_dbus_proxy_new_sync(g_conn,
992                         G_DBUS_PROXY_FLAGS_NONE, NULL,
993                         BT_HAL_BLUEZ_NAME,
994                         char_handle,
995                         BT_HAL_PROPERTIES_INTERFACE,
996                         NULL, &error);
997
998         if (properties_proxy == NULL) {
999                 ERR("properties_proxy returned NULL");
1000                 return BT_STATUS_FAIL;
1001         }
1002
1003         result = g_dbus_proxy_call_sync(properties_proxy,
1004                         "GetAll",
1005                         g_variant_new("(s)", BT_HAL_GATT_CHAR_INTERFACE),
1006                         G_DBUS_CALL_FLAGS_NONE,
1007                         -1,
1008                         NULL,
1009                         &error);
1010         if (!result) {
1011                 if (error != NULL) {
1012                         ERR("Fail to get properties (Error: %s)", error->message);
1013                         g_clear_error(&error);
1014                 } else
1015                         ERR("Fail to get properties");
1016                 g_object_unref(properties_proxy);
1017                 return BT_STATUS_FAIL;
1018         }
1019
1020         gp_desc_array = g_ptr_array_new();
1021
1022         g_variant_get(result, "(a{sv})", &property_iter);
1023
1024         while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1025                 if (!g_strcmp0(key, "UUID")) {
1026                         char_uuid_str = g_variant_dup_string(value, &len);
1027                         DBG("char UUID [%s]", char_uuid_str);
1028                 } else if (!g_strcmp0(key, "Flags")) {
1029                         g_variant_get(value, "as", &char_perm_iter);
1030                                 char_permission = 0x00;
1031
1032                         while (g_variant_iter_loop(char_perm_iter, "s", &permission)) {
1033                                 DBG("char permission: [%s]", permission);
1034                                 char_permission |= _hal_get_permission_flag(permission);
1035                         }
1036                 } else if (!g_strcmp0(key, "Descriptors")) {
1037                         g_variant_get(value, "ao", &char_desc_iter);
1038                         while (g_variant_iter_loop(char_desc_iter, "&o", &char_desc_handle)) {
1039                                 DBG("char descriptor handle : %s", char_desc_handle);
1040
1041                                 g_ptr_array_add(gp_desc_array, (gpointer)char_desc_handle);
1042                         }
1043                 }
1044         }
1045
1046         _hal_gattc_update_char_property(gattc_char, char_uuid_str, gp_desc_array, char_permission);
1047
1048         g_variant_iter_free(property_iter);
1049         g_variant_unref(result);
1050         g_object_unref(properties_proxy);
1051         g_ptr_array_free(gp_desc_array, TRUE);
1052
1053         return BT_STATUS_SUCCESS;
1054 }
1055
1056 static bt_status_t _gattc_get_all_characteristic(int conn_id,
1057                 btgatt_srvc_id_t *srvc_id)
1058 {
1059         hal_gattc_server_info_t * conn_info = NULL;
1060         hal_gattc_service_t *gattc_service = NULL;
1061         GSList *l;
1062         hal_gattc_char_t *gattc_char = NULL;
1063         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1064         int status = BT_STATUS_FAIL;
1065
1066         DBG("+");
1067         DBG("conn_id[%d]", conn_id);
1068
1069         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1070         if (NULL == conn_info) {
1071                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1072                 return BT_STATUS_FAIL;
1073         }
1074
1075         /* find service */
1076         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1077         if (NULL == gattc_service) {
1078                 DBG("Failed to get the gatt service");
1079                 return BT_STATUS_FAIL;
1080         }
1081
1082         DBG("service path [%s]", gattc_service->svc_path);
1083
1084         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1085         DBG("service uuid [%s]", svc_uuid_str);
1086
1087         /* find characteristic object path */
1088         for (l = gattc_service->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
1089                 gattc_char = (hal_gattc_char_t *)l->data;
1090                 status = _hal_gattc_get_characteristic_info(gattc_char);
1091
1092                 /* send event */
1093                 if (BT_STATUS_SUCCESS == status) {
1094                         DBG("Sending the success charateristics event");
1095                         _bt_hal_send_client_char_search_result_event(conn_id, status, srvc_id,
1096                                         &gattc_char->chr_uuid, gattc_char->permission);
1097                 }
1098         }
1099
1100         DBG("sending final event");
1101
1102         status = BT_STATUS_FAIL;
1103         _bt_hal_send_client_char_search_result_event(conn_id, status, srvc_id, NULL, 0);
1104
1105         browse_service_char(conn_id);
1106         /* retrive uuid for characteristic and object path for descriptor */
1107
1108         return BT_STATUS_SUCCESS;
1109 }
1110
1111 /**
1112  * Enumerate characteristics for a given service.
1113  * Set start_char_id to NULL to get the first characteristic.
1114  */
1115 bt_status_t btif_gattc_get_characteristic(int conn_id,
1116                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
1117 {
1118         DBG("+");
1119         CHECK_BTGATT_INIT();
1120
1121         if (start_char_id == NULL) {
1122                 DBG("Get all the characteristics");
1123                 return _gattc_get_all_characteristic(conn_id, srvc_id);
1124
1125         } else {
1126                 DBG("TBD Get specific characteristics");
1127                 return BT_STATUS_UNSUPPORTED;
1128         }
1129 }
1130
1131 static bt_status_t _hal_gattc_get_descriptor_info(hal_gattc_desc_t *gattc_desc)
1132 {
1133         GDBusProxy *properties_proxy = NULL;
1134         GError *error = NULL;
1135         GVariant *value = NULL;
1136         GVariant *result = NULL;
1137         GDBusConnection *g_conn;
1138         const gchar *key;
1139         gsize len;
1140         GVariantIter *property_iter;
1141         char* desc_handle = NULL;
1142         const gchar *desc_uuid_str = NULL;
1143
1144         DBG("+");
1145
1146         if (gattc_desc->desc_path == NULL) {
1147                 DBG("desc path is NULL");
1148                 return BT_STATUS_FAIL;
1149         }
1150         desc_handle = gattc_desc->desc_path;
1151
1152         DBG("desc path:[%s]", gattc_desc->desc_path);
1153
1154         g_conn = _bt_hal_get_system_gconn();
1155         if (NULL == g_conn) {
1156                 ERR("_bt_gdbus_get_system_gconn returned NULL");
1157                 return BT_STATUS_FAIL;
1158         }
1159
1160         properties_proxy = g_dbus_proxy_new_sync(g_conn,
1161                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1162                         BT_HAL_BLUEZ_NAME,
1163                         desc_handle,
1164                         BT_HAL_PROPERTIES_INTERFACE,
1165                         NULL, &error);
1166
1167         if (properties_proxy == NULL) {
1168                 ERR("properties_proxy returned NULL");
1169                 return BT_STATUS_FAIL;
1170         }
1171
1172         result = g_dbus_proxy_call_sync(properties_proxy,
1173                         "GetAll",
1174                         g_variant_new("(s)", BT_HAL_GATT_DESC_INTERFACE),
1175                         G_DBUS_CALL_FLAGS_NONE,
1176                         -1,
1177                         NULL,
1178                         &error);
1179         if (!result) {
1180                 if (error != NULL) {
1181                         ERR("Fail to get properties (Error: %s)", error->message);
1182                         g_clear_error(&error);
1183                 } else
1184                         ERR("Fail to get properties");
1185                 g_object_unref(properties_proxy);
1186                 return BT_STATUS_FAIL;
1187         }
1188
1189         g_variant_get(result, "(a{sv})", &property_iter);
1190
1191         while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1192                 if (!g_strcmp0(key, "UUID")) {
1193                         desc_uuid_str = g_variant_dup_string(value, &len);
1194                         DBG("desc UUID [%s]", desc_uuid_str);
1195                 }
1196         }
1197
1198         _hal_gattc_update_desc_property(gattc_desc, desc_uuid_str);
1199
1200         g_variant_iter_free(property_iter);
1201         g_variant_unref(result);
1202         g_object_unref(properties_proxy);
1203
1204         return BT_STATUS_SUCCESS;
1205 }
1206
1207 static void _bt_hal_send_client_desc_search_result_event(int conn_id, int status,
1208                 btgatt_srvc_id_t *svc_id, btgatt_gatt_id_t *char_id, bt_uuid_t *desc_uuid)
1209 {
1210         struct hal_ev_gatt_client_desc_search_result  ev;
1211
1212         if (!event_cb) {
1213                 ERR("gatt client callback not registered");
1214                 return;
1215         }
1216
1217         DBG("sending gatt client search desc result event conn_id[%d] status[%d]", conn_id, status);
1218
1219         memset(&ev, 0, sizeof(ev));
1220         ev.conn_id = conn_id;
1221         ev.inst_id = svc_id->id.inst_id;
1222         ev.is_primary = svc_id->is_primary;
1223         ev.status = status;
1224
1225         memcpy(ev.svc_uuid, svc_id->id.uuid.uu, sizeof(ev.svc_uuid));
1226         memcpy(ev.char_uuid, char_id->uuid.uu, sizeof(ev.char_uuid));
1227
1228         if (status == BT_STATUS_SUCCESS) {
1229                 DBG("building desc uuid");
1230                 memcpy(ev.desc_uuid, desc_uuid->uu, sizeof(ev.desc_uuid));
1231         }
1232
1233         DBG("sending the desc search event");
1234
1235         event_cb(HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
1236 }
1237
1238 static bt_status_t _hal_gattc_get_all_descriptor(int conn_id,
1239                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1240 {
1241         hal_gattc_server_info_t * conn_info = NULL;
1242         hal_gattc_service_t *gattc_service = NULL;
1243         GSList *l;
1244         hal_gattc_char_t *gattc_char = NULL;
1245         hal_gattc_desc_t *gattc_desc = NULL;
1246         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1247         char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1248         int status = BT_STATUS_FAIL;
1249
1250         DBG("+");
1251
1252         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1253         if (NULL == conn_info) {
1254                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1255                 return BT_STATUS_FAIL;
1256         }
1257
1258         /* find service */
1259         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1260         if (NULL == gattc_service) {
1261                 DBG("Failed to get the gatt service");
1262                 return BT_STATUS_FAIL;
1263         }
1264
1265         DBG("service path [%s]", gattc_service->svc_path);
1266         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1267         DBG("service uuid [%s]", svc_uuid_str);
1268
1269         /* find characteristics */
1270         gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1271         if (NULL == gattc_char) {
1272                 DBG("Failed to get the gatt char");
1273                 return BT_STATUS_FAIL;
1274         }
1275
1276         DBG("char path [%s]", gattc_char->chr_path);
1277         _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1278         DBG("char uuid [%s]", char_uuid_str);
1279
1280         /* get descriptor uuid */
1281         for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
1282                 gattc_desc = (hal_gattc_desc_t *)l->data;
1283                 status = _hal_gattc_get_descriptor_info(gattc_desc);
1284
1285                 /* send event */
1286                 if (BT_STATUS_SUCCESS == status) {
1287                         DBG("Sending the success descriptor event");
1288                         _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id,
1289                                         char_id, &gattc_desc->desc_uuid);
1290                 }
1291         }
1292
1293         DBG("sending final event");
1294
1295         status = BT_STATUS_FAIL;
1296         _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id, char_id, NULL);
1297
1298         browse_service_char(conn_id);
1299         /* retrive uuid for characteristic and object path for descriptor */
1300
1301         return BT_STATUS_SUCCESS;
1302 }
1303
1304 /**
1305  * Enumerate descriptors for a given characteristic.
1306  * Set start_descr_id to NULL to get the first descriptor.
1307  */
1308 bt_status_t btif_gattc_get_descriptor(int conn_id,
1309                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1310                 btgatt_gatt_id_t *start_descr_id)
1311 {
1312         DBG("+");
1313         CHECK_BTGATT_INIT();
1314
1315         if (start_descr_id == NULL) {
1316                 DBG("Get all the descriptors");
1317                 return _hal_gattc_get_all_descriptor(conn_id, srvc_id, char_id);
1318         } else {
1319                 DBG("TBD Get specific descriptor");
1320                 return BT_STATUS_UNSUPPORTED;
1321         }
1322 }
1323
1324 static void __hal_send_char_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1325 {
1326         struct hal_ev_gatt_client_read_data  ev;
1327
1328         if (!event_cb) {
1329                 ERR("gatt client callback not registered");
1330                 return;
1331         }
1332
1333         DBG("sending gatt client charac read conn_id[%d] status[%d]", resp_data->conn_id, result);
1334
1335         memset(&ev, 0, sizeof(ev));
1336         ev.conn_id = resp_data->conn_id;
1337         ev.inst_id = resp_data->srvc_id.id.inst_id;
1338         ev.is_primary = resp_data->srvc_id.is_primary;
1339         ev.status = result;
1340
1341         memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1342         memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1343
1344         ev.len = len;
1345         if (len > 0) {
1346                 DBG("building the char read value [%d]", len);
1347                 memcpy(ev.value, value, len);
1348         }
1349
1350         DBG("sending the gatt client read charac event");
1351
1352         event_cb(HAL_EV_GATT_CLIENT_READ_CHARAC, (void *)&ev, sizeof(ev));
1353 }
1354
1355 static void __hal_internal_read_char_cb(GObject *source_object,
1356                 GAsyncResult *res, gpointer user_data)
1357 {
1358         GError *error = NULL;
1359         GDBusConnection *system_gconn = NULL;
1360         GVariant *value;
1361         GVariantIter *iter;
1362         GByteArray *gp_byte_array = NULL;
1363         guint8 g_byte;
1364         hal_gatt_resp_data_t *resp_data = user_data;
1365         int result = BT_STATUS_SUCCESS;
1366         int i;
1367
1368         DBG("+");
1369
1370         system_gconn = _bt_hal_get_system_gconn();
1371         value = g_dbus_connection_call_finish(system_gconn, res, &error);
1372
1373         if (error) {
1374                 ERR("Read Characteristic dbus failed Error:", error->message);
1375
1376                 //send failed event
1377                 result  = BT_STATUS_FAIL;
1378                 __hal_send_char_read_event(resp_data, result, NULL, 0);
1379                 g_clear_error(&error);
1380                 g_free(resp_data);
1381                 return;
1382         }
1383
1384         gp_byte_array = g_byte_array_new();
1385         g_variant_get(value, "(ay)", &iter);
1386
1387         while (g_variant_iter_loop(iter, "y", &g_byte))
1388                 g_byte_array_append(gp_byte_array, &g_byte, 1);
1389
1390         //print the value
1391         DBG("value is");
1392         for (i = 0; i < gp_byte_array->len; i++)
1393                 DBG("%02x", gp_byte_array->data[i]);
1394
1395         //send value  event
1396         __hal_send_char_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1397
1398         g_free(resp_data);
1399
1400         g_byte_array_free(gp_byte_array, TRUE);
1401         g_variant_iter_free(iter);
1402         g_variant_unref(value);
1403
1404         DBG("-");
1405 }
1406
1407
1408 static bt_status_t _hal_read_characteristic_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1409                 btgatt_gatt_id_t *char_id, int auth_req)
1410 {
1411         GDBusConnection *g_conn;
1412         hal_gatt_resp_data_t *resp_data;
1413         hal_gattc_service_t *gattc_service = NULL;
1414         GVariantBuilder *builder = NULL;
1415         guint16 offset = 0;
1416         hal_gattc_server_info_t * conn_info = NULL;
1417         hal_gattc_char_t *gattc_char = NULL;
1418         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1419         char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1420         char* char_handle = NULL;
1421
1422         DBG("+");
1423
1424         /* get the connection info */
1425         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1426         if (NULL == conn_info) {
1427                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1428                 return BT_STATUS_FAIL;
1429         }
1430
1431         /* find service */
1432         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1433         if (NULL == gattc_service) {
1434                 DBG("Failed to get the gatt service");
1435                 return BT_STATUS_FAIL;
1436         }
1437
1438         DBG("service path [%s]", gattc_service->svc_path);
1439         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1440         DBG("service uuid [%s]", svc_uuid_str);
1441
1442
1443         /* find characteristic */
1444         gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1445         if (NULL == gattc_char) {
1446                 DBG("Failed to get the gatt char");
1447                 return BT_STATUS_FAIL;
1448         }
1449
1450         DBG("char path [%s]", gattc_char->chr_path);
1451         _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1452         DBG("char uuid [%s]", char_uuid_str);
1453
1454         g_conn = _bt_hal_get_system_gconn();
1455         if (NULL == g_conn) {
1456                 ERR("_bt_gdbus_get_system_gconn returned NULL");
1457                 return BT_STATUS_FAIL;
1458         }
1459
1460         resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1461         if (NULL == resp_data) {
1462                 ERR("failed to get the memory");
1463                 return BT_STATUS_FAIL;
1464         }
1465
1466         resp_data->conn_id = conn_id;
1467         memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1468         memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1469
1470         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1471
1472         /*offset*/
1473         g_variant_builder_add(builder, "{sv}", "offset",
1474                         g_variant_new("q", offset));
1475
1476         char_handle = gattc_char->chr_path;
1477
1478         DBG("calling char read value");
1479
1480         g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, char_handle, BT_HAL_GATT_CHAR_INTERFACE,
1481                         "ReadValue", g_variant_new("(a{sv})", builder),
1482                         G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1483                         (GAsyncReadyCallback)__hal_internal_read_char_cb,
1484                         (gpointer)resp_data);
1485         g_variant_builder_unref(builder);
1486
1487         return BT_STATUS_SUCCESS;
1488 }
1489
1490 /** Read a characteristic on a remote device */
1491 bt_status_t btif_read_characteristic(int conn_id,
1492                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1493                 int auth_req)
1494 {
1495         CHECK_BTGATT_INIT();
1496         DBG("+");
1497
1498         return _hal_read_characteristic_value(conn_id, srvc_id, char_id, auth_req);
1499 }
1500
1501 static void __hal_send_char_write_event(hal_gatt_resp_data_t *resp_data, int result)
1502 {
1503         struct hal_ev_gatt_client_write_result  ev;
1504
1505          if (!event_cb) {
1506                 ERR("gatt client callback not registered");
1507                 return;
1508         }
1509
1510         DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
1511
1512         memset(&ev, 0, sizeof(ev));
1513         ev.conn_id = resp_data->conn_id;
1514         ev.inst_id = resp_data->srvc_id.id.inst_id;
1515         ev.is_primary = resp_data->srvc_id.is_primary;
1516         ev.status = result;
1517
1518         memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1519         memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1520
1521         DBG("sending the gatt client write charac event");
1522
1523         event_cb(HAL_EV_GATT_CLIENT_WRITE_CHARAC, (void *)&ev, sizeof(ev));
1524 }
1525
1526 static void __hal_bluetooth_internal_write_cb(GObject *source_object,
1527                 GAsyncResult *res, gpointer user_data)
1528 {
1529         GError *error = NULL;
1530         GDBusConnection *system_gconn = NULL;
1531         GVariant *value;
1532         hal_gatt_resp_data_t *resp_data = user_data;
1533         int result = BT_STATUS_SUCCESS;
1534
1535         DBG("+");
1536
1537         system_gconn = _bt_hal_get_system_gconn();
1538         value = g_dbus_connection_call_finish(system_gconn, res, &error);
1539
1540         if (error) {
1541                 ERR("write Characteristic dbus failed Error:", error->message);
1542
1543                 result  = BT_STATUS_FAIL;
1544                 //send failed event
1545                 __hal_send_char_write_event(resp_data, result);
1546                 g_clear_error(&error);
1547                 g_free(resp_data);
1548                 return;
1549         }
1550
1551         //send write value  event
1552         __hal_send_char_write_event(resp_data, result);
1553
1554         g_free(resp_data);
1555         g_variant_unref(value);
1556
1557         DBG("-");
1558 }
1559
1560 static bt_status_t __hal_get_write_prop(hal_gatt_write_type_t type, hal_gatt_property_e *prop)
1561 {
1562         switch (type) {
1563         case HAL_GATT_WRITE_TYPE_WRITE:
1564                 *prop = HAL_GATT_PROPERTY_WRITE;
1565                 break;
1566         case HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE:
1567                 *prop = HAL_GATT_PROPERTY_WRITE_WITHOUT_RESPONSE;
1568                 break;
1569         default:
1570                 ERR("Unknow write type : %d", type);
1571                 return BT_STATUS_FAIL;
1572         }
1573
1574         return BT_STATUS_SUCCESS;
1575 }
1576
1577 static bt_status_t _hal_write_characteristic_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1578                 btgatt_gatt_id_t *char_id, int write_type, int length, int auth_req, char* value)
1579 {
1580         GVariant *val, *options;
1581         GVariantBuilder *builder1;
1582         GVariantBuilder *builder2;
1583         GDBusConnection *g_conn;
1584         guint16 offset = 0;
1585         int i = 0;
1586         hal_gatt_resp_data_t  *resp_data;
1587         hal_gattc_service_t *gattc_service = NULL;
1588         hal_gattc_server_info_t * conn_info = NULL;
1589         hal_gattc_char_t *gattc_char = NULL;
1590         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1591         char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1592         char* char_handle = NULL;
1593         hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
1594         int ret = BT_STATUS_SUCCESS;
1595
1596         DBG("+");
1597
1598         ret = __hal_get_write_prop(write_type, &write_prop);
1599         if (BT_STATUS_FAIL == ret) {
1600                 DBG("received invalid  write type:[%d] ", write_type);
1601                 return BT_STATUS_FAIL;
1602         }
1603
1604         /* get the connection info */
1605         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1606         if (NULL == conn_info) {
1607                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1608                 return BT_STATUS_FAIL;
1609         }
1610
1611         /* find service */
1612         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1613         if (NULL == gattc_service) {
1614                 DBG("Failed to get the gatt service");
1615                 return BT_STATUS_FAIL;
1616         }
1617
1618         DBG("service path [%s]", gattc_service->svc_path);
1619         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1620         DBG("service uuid [%s]", svc_uuid_str);
1621
1622         /* find characteristic */
1623         gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1624         if (NULL == gattc_char) {
1625                 DBG("Failed to get the gatt char");
1626                 return BT_STATUS_FAIL;
1627         }
1628
1629         DBG("char path [%s]", gattc_char->chr_path);
1630         _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1631         DBG("char uuid [%s]", char_uuid_str);
1632
1633         g_conn = _bt_hal_get_system_gconn();
1634         if (NULL == g_conn) {
1635                  ERR("_bt_gdbus_get_system_gconn returned NULL");
1636                 return BT_STATUS_FAIL;
1637         }
1638
1639         resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1640         if (NULL == resp_data) {
1641                 ERR("failed to get the memory");
1642                 return BT_STATUS_FAIL;
1643         }
1644
1645         resp_data->conn_id = conn_id;
1646         memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1647         memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1648
1649         char_handle = gattc_char->chr_path;
1650
1651         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1652
1653         for (i = 0; i < length; i++)
1654                 g_variant_builder_add(builder1, "y", value[i]);
1655
1656         val = g_variant_new("ay", builder1);
1657
1658         builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1659         /*offset*/
1660         g_variant_builder_add(builder2, "{sv}", "offset",
1661                         g_variant_new_uint16(offset));
1662
1663         options = g_variant_new("a{sv}", builder2);
1664
1665         g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, char_handle, BT_HAL_GATT_CHAR_INTERFACE,
1666                         "WriteValuebyType",
1667                         g_variant_new("(y@ay@a{sv})", write_prop, val, options),
1668                         NULL,
1669                         G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1670                         (GAsyncReadyCallback)__hal_bluetooth_internal_write_cb,
1671                         (gpointer)resp_data);
1672
1673         g_variant_builder_unref(builder1);
1674         g_variant_builder_unref(builder2);
1675
1676         return BT_STATUS_SUCCESS;
1677 }
1678
1679 /** Write a remote characteristic */
1680 bt_status_t btif_write_characteristic(int conn_id,
1681                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1682                 int write_type, int len, int auth_req,
1683                 char* p_value)
1684 {
1685         CHECK_BTGATT_INIT();
1686
1687         DBG("+");
1688
1689         DBG("svc isntance id:[%d]", srvc_id->id.inst_id);
1690         return _hal_write_characteristic_value(conn_id, srvc_id, char_id, write_type,
1691                                 len, auth_req, p_value);
1692 }
1693
1694 static void __hal_send_desc_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1695 {
1696         struct hal_ev_gatt_client_read_data  ev;
1697
1698          if (!event_cb) {
1699                 ERR("gatt client callback not registered");
1700                 return;
1701         }
1702
1703         DBG("sending gatt client desc read conn_id[%d] status[%d]", resp_data->conn_id, result);
1704
1705         memset(&ev, 0, sizeof(ev));
1706         ev.conn_id = resp_data->conn_id;
1707         ev.inst_id = resp_data->srvc_id.id.inst_id;
1708         ev.is_primary = resp_data->srvc_id.is_primary;
1709         ev.status = result;
1710
1711         memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1712         memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1713         memcpy(ev.desc_uuid, resp_data->desc_id.uuid.uu, sizeof(ev.char_uuid));
1714
1715         ev.len = len;
1716         if (len > 0) {
1717                 DBG("building the desc read value [%d]", len);
1718                 memcpy(ev.value, value, len);
1719         }
1720
1721         DBG("sending the gatt client read descriptor event");
1722
1723         event_cb(HAL_EV_GATT_CLIENT_READ_DESC, (void *)&ev, sizeof(ev));
1724 }
1725
1726 static void __hal_internal_read_desc_cb(GObject *source_object,
1727                 GAsyncResult *res, gpointer user_data)
1728 {
1729         GError *error = NULL;
1730         GDBusConnection *system_gconn = NULL;
1731         GVariant *value;
1732         GVariantIter *iter;
1733         GByteArray *gp_byte_array = NULL;
1734         guint8 g_byte;
1735         hal_gatt_resp_data_t *resp_data = user_data;
1736         int result = BT_STATUS_SUCCESS;
1737         int i;
1738
1739         DBG("+");
1740
1741         system_gconn = _bt_hal_get_system_gconn();
1742         value = g_dbus_connection_call_finish(system_gconn, res, &error);
1743
1744         if (error) {
1745                 ERR("Read descriptor dbus failed Error:", error->message);
1746
1747                 //send failed event
1748                 result  = BT_STATUS_FAIL;
1749                 __hal_send_desc_read_event(resp_data, result, NULL, 0);
1750                 g_clear_error(&error);
1751                 g_free(resp_data);
1752                 return;
1753         }
1754
1755         gp_byte_array = g_byte_array_new();
1756         g_variant_get(value, "(ay)", &iter);
1757
1758         while (g_variant_iter_loop(iter, "y", &g_byte))
1759                 g_byte_array_append(gp_byte_array, &g_byte, 1);
1760
1761         //print the value
1762         DBG("value is");
1763         for (i = 0; i < gp_byte_array->len; i++)
1764                 DBG("%02x", gp_byte_array->data[i]);
1765
1766         //send value  event
1767         __hal_send_desc_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1768
1769         g_free(resp_data);
1770
1771         g_byte_array_free(gp_byte_array, TRUE);
1772         g_variant_iter_free(iter);
1773         g_variant_unref(value);
1774
1775         DBG("-");
1776 }
1777
1778 static bt_status_t _hal_read_descriptor_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1779                 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *desc_id, int auth_req)
1780 {
1781         GDBusConnection *g_conn;
1782         hal_gatt_resp_data_t *resp_data;
1783         hal_gattc_service_t *gattc_service = NULL;
1784         GVariantBuilder *builder = NULL;
1785         guint16 offset = 0;
1786         hal_gattc_server_info_t * conn_info = NULL;
1787         hal_gattc_char_t *gattc_char = NULL;
1788         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1789         char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1790         char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
1791         char* desc_handle = NULL;
1792
1793         hal_gattc_desc_t *gattc_desc = NULL;
1794
1795         DBG("+");
1796
1797         /* get the connection info */
1798         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1799         if (NULL == conn_info) {
1800                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1801                 return BT_STATUS_FAIL;
1802         }
1803
1804         /* find service */
1805         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1806         if (NULL == gattc_service) {
1807                 DBG("Failed to get the gatt service");
1808                 return BT_STATUS_FAIL;
1809         }
1810
1811         DBG("service path [%s]", gattc_service->svc_path);
1812         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1813         DBG("service uuid [%s]", svc_uuid_str);
1814
1815         /* find characteristic */
1816         gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1817         if (NULL == gattc_char) {
1818                 DBG("Failed to get the gatt char");
1819                 return BT_STATUS_FAIL;
1820         }
1821
1822         DBG("char path [%s]", gattc_char->chr_path);
1823         _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1824         DBG("char uuid [%s]", char_uuid_str);
1825
1826         /* find descriptor */
1827         gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &desc_id->uuid);
1828         if (NULL == gattc_desc) {
1829                 DBG("Failed to get the gatt desc");
1830                 return BT_STATUS_FAIL;
1831         }
1832
1833         DBG("desc path [%s]", gattc_desc->desc_path);
1834         _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
1835         DBG("desc uuid [%s]", desc_uuid_str);
1836
1837         g_conn = _bt_hal_get_system_gconn();
1838         if (NULL == g_conn) {
1839                  ERR("_bt_gdbus_get_system_gconn returned NULL");
1840                 return BT_STATUS_FAIL;
1841         }
1842
1843         resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1844         if (NULL == resp_data) {
1845                 ERR("failed to get the memory");
1846                 return BT_STATUS_FAIL;
1847         }
1848
1849         resp_data->conn_id = conn_id;
1850         memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1851         memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1852         memcpy(&resp_data->desc_id, desc_id, sizeof(btgatt_gatt_id_t));
1853
1854         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1855
1856         /*offset*/
1857         g_variant_builder_add(builder, "{sv}", "offset",
1858                         g_variant_new("q", offset));
1859
1860         desc_handle = gattc_desc->desc_path;
1861
1862         DBG("calling desc read value");
1863
1864         g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, desc_handle, BT_HAL_GATT_DESC_INTERFACE,
1865                         "ReadValue", g_variant_new("(a{sv})", builder),
1866                         G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1867                         (GAsyncReadyCallback)__hal_internal_read_desc_cb,
1868                         (gpointer)resp_data);
1869         g_variant_builder_unref(builder);
1870
1871         return BT_STATUS_SUCCESS;
1872 }
1873
1874 /** Read the descriptor for a given characteristic */
1875 bt_status_t btif_read_descriptor(int conn_id,
1876                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1877                 btgatt_gatt_id_t *descr_id, int auth_req)
1878 {
1879         CHECK_BTGATT_INIT();
1880
1881         return _hal_read_descriptor_value(conn_id, srvc_id, char_id, descr_id, auth_req);
1882 }
1883
1884 static void __hal_send_desc_write_event(hal_gatt_resp_data_t *resp_data, int result)
1885 {
1886         struct hal_ev_gatt_client_write_result  ev;
1887
1888          if (!event_cb) {
1889                 ERR("gatt client callback not registered");
1890                 return;
1891         }
1892
1893         DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
1894
1895         memset(&ev, 0, sizeof(ev));
1896         ev.conn_id = resp_data->conn_id;
1897         ev.inst_id = resp_data->srvc_id.id.inst_id;
1898         ev.is_primary = resp_data->srvc_id.is_primary;
1899         ev.status = result;
1900
1901         memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1902         memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1903         memcpy(ev.desc_uuid, resp_data->desc_id.uuid.uu, sizeof(ev.desc_uuid));
1904
1905         DBG("sending the gatt client write charac event");
1906
1907         event_cb(HAL_EV_GATT_CLIENT_WRITE_DESC, (void *)&ev, sizeof(ev));
1908 }
1909
1910 static void __hal_bluetooth_internal_desc_write_cb(GObject *source_object,
1911                 GAsyncResult *res, gpointer user_data)
1912 {
1913         GError *error = NULL;
1914         GDBusConnection *system_gconn = NULL;
1915         GVariant *value;
1916         hal_gatt_resp_data_t *resp_data = user_data;
1917         int result = BT_STATUS_SUCCESS;
1918
1919         DBG("+");
1920
1921         system_gconn = _bt_hal_get_system_gconn();
1922         value = g_dbus_connection_call_finish(system_gconn, res, &error);
1923
1924         if (error) {
1925                 ERR("write descriptor dbus failed Error:", error->message);
1926
1927                 //send failed event
1928                 result  = BT_STATUS_FAIL;
1929                 __hal_send_desc_write_event(resp_data, result);
1930                 g_clear_error(&error);
1931                 g_free(resp_data);
1932                 return;
1933         }
1934
1935         //send write value  event
1936         __hal_send_desc_write_event(resp_data, result);
1937
1938         g_free(resp_data);
1939         g_variant_unref(value);
1940
1941         DBG("-");
1942 }
1943
1944 static bt_status_t _hal_write_descriptor_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1945                                 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id,
1946                                 int write_type, int length, int auth_req, char* value)
1947 {
1948         GVariant *val, *options;
1949         GVariantBuilder *builder1;
1950         GVariantBuilder *builder2;
1951         GDBusConnection *g_conn;
1952         guint16 offset = 0;
1953         int i = 0;
1954         hal_gatt_resp_data_t  *resp_data;
1955         hal_gattc_service_t *gattc_service = NULL;
1956         hal_gattc_server_info_t * conn_info = NULL;
1957         hal_gattc_char_t *gattc_char = NULL;
1958         hal_gattc_desc_t *gattc_desc = NULL;
1959         char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1960         char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1961         char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
1962         char* desc_handle = NULL;
1963         hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
1964         int ret = BT_STATUS_SUCCESS;
1965
1966         DBG("+");
1967
1968         ret = __hal_get_write_prop(write_type, &write_prop);
1969         if (BT_STATUS_FAIL == ret) {
1970                 DBG("received invalid  write type:[%d] ", write_type);
1971                 return BT_STATUS_FAIL;
1972         }
1973
1974         /* get the connection info */
1975         conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1976         if (NULL == conn_info) {
1977                 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1978                 return BT_STATUS_FAIL;
1979         }
1980
1981         /* find service */
1982         gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1983         if (NULL == gattc_service) {
1984                 DBG("Failed to get the gatt service");
1985                 return BT_STATUS_FAIL;
1986         }
1987
1988         DBG("service path [%s]", gattc_service->svc_path);
1989         _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1990         DBG("service uuid [%s]", svc_uuid_str);
1991
1992         /* find characteristic */
1993         gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1994         if (NULL == gattc_char) {
1995                 DBG("Failed to get the gatt char");
1996                 return BT_STATUS_FAIL;
1997         }
1998
1999         DBG("char path [%s]", gattc_char->chr_path);
2000         _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
2001         DBG("char uuid [%s]", char_uuid_str);
2002
2003         /* find descriptor */
2004         gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &descr_id->uuid);
2005         if (NULL == gattc_desc) {
2006                 DBG("Failed to get the gatt char");
2007                 return BT_STATUS_FAIL;
2008         }
2009
2010         DBG("desc path [%s]", gattc_desc->desc_path);
2011         _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
2012         DBG("char uuid [%s]", desc_uuid_str);
2013
2014         g_conn = _bt_hal_get_system_gconn();
2015         if (NULL == g_conn) {
2016                  ERR("_bt_gdbus_get_system_gconn returned NULL");
2017                 return BT_STATUS_FAIL;
2018         }
2019
2020         resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
2021         if (NULL == resp_data) {
2022                 ERR("failed to get the memory");
2023                 return BT_STATUS_FAIL;
2024         }
2025
2026         resp_data->conn_id = conn_id;
2027         memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
2028         memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
2029         memcpy(&resp_data->desc_id, descr_id, sizeof(btgatt_gatt_id_t));
2030
2031         desc_handle = gattc_desc->desc_path;
2032
2033         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2034
2035         for (i = 0; i < length; i++)
2036                 g_variant_builder_add(builder1, "y", value[i]);
2037
2038         val = g_variant_new("ay", builder1);
2039
2040         builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2041         /*offset*/
2042         g_variant_builder_add(builder2, "{sv}", "offset",
2043                         g_variant_new_uint16(offset));
2044
2045         options = g_variant_new("a{sv}", builder2);
2046
2047         g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, desc_handle, BT_HAL_GATT_DESC_INTERFACE,
2048                         "WriteValue",
2049                         g_variant_new("(@ay@a{sv})", val, options),
2050                         NULL,
2051                         G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2052                         (GAsyncReadyCallback)__hal_bluetooth_internal_desc_write_cb,
2053                         (gpointer)resp_data);
2054
2055         g_variant_builder_unref(builder1);
2056         g_variant_builder_unref(builder2);
2057
2058         return BT_STATUS_SUCCESS;
2059 }
2060
2061 /** Write a remote descriptor for a given characteristic */
2062 bt_status_t btif_write_descriptor(int conn_id,
2063                 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
2064                 btgatt_gatt_id_t *descr_id, int write_type, int len,
2065                 int auth_req, char* p_value)
2066 {
2067         CHECK_BTGATT_INIT();
2068
2069         return _hal_write_descriptor_value(conn_id, srvc_id, char_id, descr_id, write_type,
2070                         len, auth_req, p_value);
2071 }
2072
2073 /** Execute a prepared write operation */
2074 bt_status_t execute_write(int conn_id, int execute)
2075 {
2076         CHECK_BTGATT_INIT();
2077         return BT_STATUS_UNSUPPORTED;
2078 }
2079
2080 /**
2081  * Register to receive notifications or indications for a given
2082  * characteristic
2083  */
2084 bt_status_t register_for_notification(int client_if,
2085                 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
2086                 btgatt_gatt_id_t *char_id)
2087 {
2088         CHECK_BTGATT_INIT();
2089         return BT_STATUS_UNSUPPORTED;
2090 }
2091
2092 /** Deregister a previous request for notifications/indications */
2093 bt_status_t deregister_for_notification(int client_if,
2094                 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
2095                 btgatt_gatt_id_t *char_id)
2096 {
2097         CHECK_BTGATT_INIT();
2098         return BT_STATUS_UNSUPPORTED;
2099 }
2100
2101 /** Request RSSI for a given remote device */
2102 bt_status_t read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
2103 {
2104         CHECK_BTGATT_INIT();
2105         return BT_STATUS_UNSUPPORTED;
2106 }
2107
2108 /** OTA firmware download */
2109 bt_status_t ota_fw_update(int client_if, int conn_id, const bt_bdaddr_t *bd_addr, char* path)
2110 {
2111         CHECK_BTGATT_INIT();
2112         return BT_STATUS_UNSUPPORTED;
2113 }
2114
2115 /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
2116 int get_device_type(const bt_bdaddr_t *bd_addr)
2117 {
2118         CHECK_BTGATT_INIT();
2119         return BT_STATUS_UNSUPPORTED;
2120 }
2121
2122 static bt_status_t __hal_update_conn_parameter(bt_bdaddr_t *bd_addr,
2123                         int min_int, int max_int, int latency, int timeout)
2124 {
2125         gchar *device_path = NULL;
2126         GError *error = NULL;
2127         GDBusProxy *device_proxy = NULL;
2128         GDBusConnection *conn;
2129         GVariant *reply;
2130         int ret = BT_STATUS_SUCCESS;
2131         char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
2132
2133         INFO("Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2134                         min_int, max_int, latency, timeout);
2135
2136         conn = _bt_hal_get_system_gconn();
2137         if (conn == NULL) {
2138                 ERR("conn NULL");
2139                 return BT_STATUS_FAIL;
2140         }
2141
2142         _bt_hal_convert_addr_type_to_string(device_address,
2143                                 (unsigned char *)bd_addr->address);
2144         device_path = _bt_hal_get_device_object_path(device_address);
2145
2146         if (device_path == NULL) {
2147                 ERR("device_path NULL : [%s]", device_address);
2148                 return BT_STATUS_FAIL;
2149         }
2150
2151         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2152                         NULL, BT_HAL_BLUEZ_NAME,
2153                         device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
2154
2155         g_free(device_path);
2156         if (NULL == device_proxy) {
2157                 ERR("device_proxy returned NULL");
2158                 return BT_STATUS_FAIL;
2159         }
2160
2161         INFO("### LeConnUpdate");
2162         reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2163                         g_variant_new("(uuuu)", min_int, max_int, latency, timeout),
2164                         G_DBUS_CALL_FLAGS_NONE,
2165                         -1,
2166                         NULL,
2167                         &error);
2168
2169         g_object_unref(device_proxy);
2170         if (reply == NULL) {
2171                 if (error) {
2172                         ERR("Error %s[%s]", error->message, device_address);
2173                         if (g_strrstr(error->message, "In Progress"))
2174                                 ret = BT_STATUS_SUCCESS;
2175                         else
2176                                 ret = BT_STATUS_FAIL;
2177                         g_error_free(error);
2178                         return ret;
2179                 }
2180         }
2181         g_variant_unref(reply);
2182
2183         INFO("LE Connection parameter Updated");
2184         return ret;
2185 }
2186
2187 /** Request a connection parameter update */
2188 bt_status_t btif_gattc_conn_parameter_update(bt_bdaddr_t *bd, int min_int, int max_int, int latency, int timeout)
2189 {
2190         CHECK_BTGATT_INIT();
2191
2192         DBG("+");
2193
2194         return __hal_update_conn_parameter(bd, min_int, max_int, latency, timeout);
2195 }
2196
2197 /** Test mode interface */
2198 bt_status_t test_command(int command, btgatt_test_params_t* params)
2199 {
2200         CHECK_BTGATT_INIT();
2201         return BT_STATUS_UNSUPPORTED;
2202 }
2203
2204 /** MTU Exchange request from client */
2205 bt_status_t configure_mtu(int conn_id, int mtu)
2206 {
2207         CHECK_BTGATT_INIT();
2208         return BT_STATUS_UNSUPPORTED;
2209 }
2210
2211 /** Setup scan filter params */
2212 bt_status_t scan_filter_param_setup(int client_if, int action, int filt_index, int feat_seln,
2213                 int list_logic_type, int filt_logic_type, int rssi_high_thres,
2214                 int rssi_low_thres, int dely_mode, int found_timeout,
2215                 int lost_timeout, int found_timeout_cnt)
2216 {
2217         CHECK_BTGATT_INIT();
2218         return BT_STATUS_UNSUPPORTED;
2219 }
2220
2221 /** Configure a scan filter condition */
2222 bt_status_t scan_filter_add_remove(int client_if, int action, int filt_type,
2223                 int filt_index, int company_id,
2224                 int company_id_mask, const bt_uuid_t *p_uuid,
2225                 const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
2226                 char addr_type, int data_len, char* p_data, int mask_len,
2227                 char* p_mask)
2228 {
2229         CHECK_BTGATT_INIT();
2230         return BT_STATUS_UNSUPPORTED;
2231 }
2232
2233 /** Clear all scan filter conditions for specific filter index*/
2234 bt_status_t scan_filter_clear(int client_if, int filt_index)
2235 {
2236         CHECK_BTGATT_INIT();
2237         return BT_STATUS_UNSUPPORTED;
2238 }
2239
2240 /** Enable / disable scan filter feature*/
2241 bt_status_t scan_filter_enable(int client_if, bool enable)
2242 {
2243         CHECK_BTGATT_INIT();
2244         return BT_STATUS_UNSUPPORTED;
2245 }
2246
2247 /** Sets the LE scan interval and window in units of N*0.625 msec */
2248 #ifdef TIZEN_BT_HAL
2249 bt_status_t set_scan_parameters(int scan_type, int scan_interval, int scan_window)
2250 {
2251         int ret;
2252
2253         CHECK_BTGATT_INIT();
2254
2255         le_scan_type = scan_type;
2256         ret = _bt_hal_adapter_le_set_scan_parameters(
2257                         scan_type, scan_interval, scan_window);
2258         return ret;
2259 }
2260 #else
2261 bt_status_t set_scan_parameters(int scan_interval, int scan_window)
2262 {
2263         CHECK_BTGATT_INIT();
2264         return BT_STATUS_UNSUPPORTED;
2265 }
2266 #endif
2267
2268 /* Configure the batchscan storage */
2269 bt_status_t batchscan_cfg_storage(int client_if, int batch_scan_full_max,
2270                 int batch_scan_trunc_max, int batch_scan_notify_threshold)
2271 {
2272         CHECK_BTGATT_INIT();
2273         return BT_STATUS_UNSUPPORTED;
2274 }
2275
2276 /* Enable batchscan */
2277 bt_status_t batchscan_enb_batch_scan(int client_if, int scan_mode,
2278                 int scan_interval, int scan_window, int addr_type, int discard_rule)
2279 {
2280         CHECK_BTGATT_INIT();
2281         return BT_STATUS_UNSUPPORTED;
2282 }
2283
2284 /* Disable batchscan */
2285 bt_status_t batchscan_dis_batch_scan(int client_if)
2286 {
2287         CHECK_BTGATT_INIT();
2288         return BT_STATUS_UNSUPPORTED;
2289 }
2290
2291 /* Read out batchscan reports */
2292 bt_status_t batchscan_read_reports(int client_if, int scan_mode)
2293 {
2294         CHECK_BTGATT_INIT();
2295         return BT_STATUS_UNSUPPORTED;
2296 }
2297
2298 const btgatt_client_interface_t btgatt_client_interface = {
2299         btif_gattc_register_client,
2300         btif_gattc_unregister_client,
2301         scan,
2302         btif_gattc_client_connect,
2303         btif_gattc_client_disconnect,
2304         refresh,
2305         btif_gattc_client_search_service,
2306         get_included_service,
2307         btif_gattc_get_characteristic,
2308         btif_gattc_get_descriptor,
2309         btif_read_characteristic,
2310         btif_write_characteristic,
2311         btif_read_descriptor,
2312         btif_write_descriptor,
2313         execute_write,
2314         register_for_notification,
2315         deregister_for_notification,
2316         read_remote_rssi,
2317         ota_fw_update,
2318         get_device_type,
2319         btif_gattc_conn_parameter_update,
2320         test_command,
2321         configure_mtu,
2322         scan_filter_param_setup,
2323         scan_filter_add_remove,
2324         scan_filter_clear,
2325         scan_filter_enable,
2326         set_scan_parameters,
2327         batchscan_cfg_storage,
2328         batchscan_enb_batch_scan,
2329         batchscan_dis_batch_scan,
2330         batchscan_read_reports
2331 };
2332
2333 static hal_gattc_server_info_t *__bt_find_gatt_conn_info(bt_bdaddr_t *serv_addr)
2334 {
2335         DBG("+");
2336
2337         GSList *l;
2338         hal_gattc_server_info_t *info = NULL;
2339
2340         for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2341                 info = (hal_gattc_server_info_t*)l->data;
2342                 if (info == NULL)
2343                         continue;
2344
2345                 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2346                         INFO("GATT connection found addr");
2347                         return info;
2348                 }
2349         }
2350         return NULL;
2351 }
2352
2353 static hal_gattc_client_info_t *__bt_find_gatt_client_info(bt_bdaddr_t *serv_addr)
2354 {
2355         DBG("+");
2356
2357         GSList *l;
2358         hal_gattc_client_info_t *info = NULL;
2359
2360         for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2361                 info = (hal_gattc_client_info_t*)l->data;
2362                 if (info == NULL)
2363                         continue;
2364
2365                 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2366                         INFO("GATT client info found addr");
2367                         return info;
2368                 }
2369         }
2370         return NULL;
2371 }
2372
2373 static hal_gattc_client_info_t *__bt_find_gatt_client_info_from_conn_id(int conn_id)
2374 {
2375         DBG("+");
2376
2377         GSList *l;
2378         hal_gattc_client_info_t *info = NULL;
2379
2380         for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2381                 info = (hal_gattc_client_info_t*)l->data;
2382                 if (info == NULL)
2383                         continue;
2384
2385                 if (info->conn_id == conn_id) {
2386                         INFO("GATT client info found for conn_id [%d]", conn_id);
2387                         return info;
2388                 }
2389         }
2390         return NULL;
2391 }
2392
2393 static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int  conn_id)
2394 {
2395         DBG("+");
2396
2397         GSList *l;
2398         hal_gattc_server_info_t *info = NULL;
2399         hal_gattc_client_info_t *gattc_client = NULL;
2400
2401         gattc_client = __bt_find_gatt_client_info_from_conn_id(conn_id);
2402         if (gattc_client == NULL) {
2403                 INFO("GATT client conn info not found");
2404                 return NULL;
2405         }
2406
2407         for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2408                 info = (hal_gattc_server_info_t*)l->data;
2409                 if (info == NULL)
2410                         continue;
2411
2412                 if ((info->inst_id == gattc_client->inst_id) &&
2413                         !memcmp(&info->bd_addr, &gattc_client->bd_addr, sizeof(bt_bdaddr_t))) {
2414                         INFO("GATT connection found for conn_id [%d]", conn_id);
2415                         return info;
2416                 }
2417         }
2418         return NULL;
2419 }
2420
2421 static bt_status_t __bt_connect_le_device_internal(int client_if, const bt_bdaddr_t *bd_addr,
2422                 gboolean auto_connect)
2423 {
2424         char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
2425         gchar *device_path = NULL;
2426         GDBusProxy *device_proxy = NULL;
2427         GDBusConnection *conn;
2428         int ret = BT_STATUS_SUCCESS;
2429         hal_gattc_client_info_t *gattc_data;
2430
2431         DBG("+");
2432
2433         if (NULL == bd_addr) {
2434                 ERR("bd_addr is NULL");
2435                 return BT_STATUS_PARM_INVALID;
2436         }
2437
2438         conn = _bt_hal_get_system_gconn();
2439         if (NULL == conn) {
2440                 ERR("_bt_gdbus_get_system_gconn returned NULL");
2441                 return BT_STATUS_FAIL;
2442         }
2443
2444         _bt_hal_convert_addr_type_to_string(device_address,
2445                         (unsigned char *)bd_addr->address);
2446         device_path = _bt_hal_get_device_object_path(device_address);
2447         if (device_path == NULL) {
2448                 ERR("device_path NULL : [%s]", device_address);
2449                 ret = BT_STATUS_FAIL;
2450                 return ret;
2451         }
2452         ERR("device_path:%s", device_path);
2453
2454         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2455                         NULL, BT_HAL_BLUEZ_NAME,
2456                         device_path, BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
2457         g_free(device_path);
2458         if (NULL == device_proxy) {
2459                 ERR("device_proxy returned NULL");
2460                 return BT_STATUS_FAIL;
2461         }
2462
2463         gattc_data = g_malloc0(sizeof(hal_gattc_client_info_t));
2464         if (gattc_data == NULL) {
2465                 ERR("Unable to allocate memory");
2466                 ret = BT_STATUS_NOMEM;
2467                 goto fail;
2468         }
2469         memcpy(gattc_data->bd_addr.address, bd_addr->address,
2470                                         BT_HAL_ADDRESS_LENGTH_MAX);
2471
2472         DBG("Connect LE [%s]", device_address);
2473
2474         gattc_data->client_if = client_if;
2475
2476         g_dbus_proxy_call(device_proxy, "ConnectLE",
2477                         g_variant_new("(b)", auto_connect),
2478                         G_DBUS_CALL_FLAGS_NONE,
2479                         BT_HAL_MAX_DBUS_TIMEOUT,
2480                         NULL,
2481                         (GAsyncReadyCallback)__le_connection_req_cb, gattc_data);
2482
2483         return ret;
2484
2485 fail:
2486         if (device_proxy)
2487                 g_object_unref(device_proxy);
2488
2489         g_free(gattc_data);
2490
2491         return ret;
2492 }
2493
2494 static bt_status_t _bt_hold_current_advertising()
2495 {
2496         int ret = BT_STATUS_FAIL;
2497         gboolean is_advertising = FALSE;
2498         DBG("+");
2499
2500         is_advertising = _bt_hal_is_advertising_in_slot(0);
2501         if (is_advertising) {
2502                 DBG("+ Stop current advertising");
2503
2504                 ret = _bt_hal_enable_advertising(0, FALSE, FALSE);
2505                 g_timeout_add(2000, __bt_hold_current_advertising_timeout_cb, NULL);
2506         }
2507
2508         return ret;
2509 }
2510
2511 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data)
2512 {
2513         DBG("+ start current advertising");
2514
2515         _bt_hal_enable_advertising(0, TRUE, FALSE);
2516
2517         return FALSE;
2518 }
2519
2520 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
2521 {
2522         DBG("Try to initiate pending LE connection");
2523
2524         pending_le_conn_timer_id = 0;
2525
2526         __bt_connect_le_device_internal(pending_le_conn_info->client_if,
2527                         &pending_le_conn_info->bd_addr,
2528                         pending_le_conn_info->auto_connect);
2529
2530         g_free(pending_le_conn_info);
2531         pending_le_conn_info = NULL;
2532
2533         return FALSE;
2534 }
2535
2536 static int __hal_generate_conn_id()
2537 {
2538          return ++bt_conn_id;
2539 }
2540
2541 static int __hal_generate_server_instance_id()
2542 {
2543         return ++bt_inst_id;
2544 }
2545
2546 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
2547                 gpointer user_data)
2548 {
2549         GError *g_error = NULL;
2550         GVariant *reply = NULL;
2551         hal_gattc_client_info_t *gattc_data  = user_data;
2552         int result = BT_STATUS_SUCCESS;
2553         struct hal_ev_gatt_client_connected ev;
2554         hal_gattc_server_info_t *gatt_conn_info = NULL;
2555
2556         DBG("+");
2557
2558         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
2559         g_object_unref(proxy);
2560         if (reply == NULL) {
2561                 ERR("Connect LE Dbus Call Error");
2562                 if (g_error) {
2563                         ERR("Error: %s\n", g_error->message);
2564                         g_clear_error(&g_error);
2565                 }
2566                 result = BT_STATUS_FAIL;
2567         }
2568         g_variant_unref(reply);
2569
2570         if (NULL == gattc_data) {
2571                 ERR("server_data is NULL");
2572                 return;
2573         }
2574
2575         /*send fail event*/
2576         if (result == BT_STATUS_FAIL) {
2577                 memset(&ev, 0, sizeof(ev));
2578                 ev.conn_id = -1;
2579                 ev.status = result;
2580                 ev.client_if = gattc_data->client_if;
2581                 memcpy(ev.bdaddr, gattc_data->bd_addr.address,
2582                                                 BT_HAL_ADDRESS_LENGTH_MAX);
2583
2584                 if (!event_cb) {
2585                         ERR("gatt client callback not registered");
2586                 } else {
2587                         DBG("sending gatt client connected event");
2588                         event_cb(HAL_EV_GATT_CLIENT_CONNECTED, (void *)&ev, sizeof(ev));
2589                 }
2590
2591                 goto fail;
2592         }
2593
2594         DBG("adding the server conn info in list");
2595         gattc_data->conn_id = __hal_generate_conn_id() ;
2596         gattc_data->inst_id = __hal_generate_server_instance_id();
2597
2598         hal_gattc_client_info_list = g_slist_append(hal_gattc_client_info_list, gattc_data);
2599
2600         /*add gatt server connection info*/
2601         gatt_conn_info = g_malloc0(sizeof(hal_gattc_server_info_t));
2602         if (gatt_conn_info == NULL) {
2603                 ERR("Failed to allocate memory");
2604                 goto fail;
2605         }
2606
2607         memcpy(gatt_conn_info->bd_addr.address, gattc_data->bd_addr.address, BT_HAL_ADDRESS_LENGTH_MAX);
2608         gatt_conn_info->inst_id = gattc_data->inst_id;
2609         hal_gattc_server_info_list = g_slist_append(hal_gattc_server_info_list, gatt_conn_info);
2610
2611         DBG("-");
2612         return;
2613
2614 fail:
2615         /*remove conn_info*/
2616         if (gattc_data)
2617                 g_free(gattc_data);
2618 }
2619
2620 void __hal_gattc_free_svc_info(hal_gattc_service_t *svc_info)
2621 {
2622         g_free(svc_info->svc_path);
2623         g_free(svc_info);
2624 }
2625
2626 void __hal_gattc_free_char_info(hal_gattc_char_t *char_info)
2627 {
2628         g_free(char_info->chr_path);
2629         g_free(char_info);
2630 }
2631
2632 void __hal_gattc_free_desc_info(hal_gattc_desc_t *desc_info)
2633 {
2634         g_free(desc_info->desc_path);
2635         g_free(desc_info);
2636 }
2637
2638 void __hal_clean_gattc_server_info(hal_gattc_server_info_t *conn_info)
2639 {
2640         GSList *l;
2641         GSList *m;
2642         GSList *k;
2643         hal_gattc_service_t *svc_info = NULL;
2644         hal_gattc_char_t *char_info = NULL;
2645         hal_gattc_desc_t *desc_info = NULL;
2646
2647         DBG("+");
2648
2649         for (l = conn_info->gatt_list_services; l != NULL;) {
2650                 svc_info = (hal_gattc_service_t*)l->data;
2651                 if (svc_info == NULL)
2652                         continue;
2653                 l = g_slist_next(l);
2654
2655                 for (m = svc_info->gatt_list_chars; m != NULL; ) {
2656                         char_info = (hal_gattc_char_t*)m->data;
2657                         if (char_info == NULL)
2658                                 continue;
2659                         m = g_slist_next(m);
2660
2661                         for (k = char_info->gatt_list_descs; k != NULL; ) {
2662                                 desc_info = (hal_gattc_desc_t*)k->data;
2663                                 if (desc_info == NULL)
2664                                         continue;
2665                                 k = g_slist_next(k);
2666
2667                                 /*remove desc element*/
2668                                 char_info->gatt_list_descs = g_slist_remove(char_info->gatt_list_descs, desc_info);
2669                                 __hal_gattc_free_desc_info(desc_info);
2670                         }
2671
2672                         /*remove desc list*/
2673                         g_slist_free(char_info->gatt_list_descs);
2674                         char_info->gatt_list_descs = NULL;
2675
2676                         /*remove char element*/
2677                         svc_info->gatt_list_chars = g_slist_remove(svc_info->gatt_list_chars, char_info);
2678                         __hal_gattc_free_char_info(char_info);
2679                 }
2680
2681                 /*remove char list*/
2682                 g_slist_free(svc_info->gatt_list_chars);
2683                 svc_info->gatt_list_chars = NULL;
2684
2685                 /*remove svc element*/
2686                 conn_info->gatt_list_services = g_slist_remove(conn_info->gatt_list_services, svc_info);
2687                 __hal_gattc_free_svc_info(svc_info);
2688         }
2689
2690         /*remove svc list */
2691         g_slist_free(conn_info->gatt_list_services);
2692         conn_info->gatt_list_services = NULL;
2693
2694         /*remove conn info*/
2695         g_free(conn_info);
2696 }
2697
2698 void _bt_hal_handle_gattc_connected_event(char* address, gboolean gatt_connected)
2699 {
2700         int result = BT_STATUS_SUCCESS;
2701         struct hal_ev_gatt_client_connected ev;
2702         hal_gattc_server_info_t *conn_info = NULL;
2703         bt_bdaddr_t bd_addr;
2704         int event;
2705         hal_gattc_client_info_t *gattc_client = NULL;
2706         int inst_id = -1;
2707
2708
2709         DBG("+ connected device address [%s]", address);
2710
2711         event = gatt_connected ? HAL_EV_GATT_CLIENT_CONNECTED :
2712                                 HAL_EV_GATT_CLIENT_DISCONNECTED;
2713
2714         _bt_hal_convert_addr_string_to_type(bd_addr.address, address);
2715         /* find the gatt client info */
2716         gattc_client = __bt_find_gatt_client_info(&bd_addr);
2717         if (NULL == gattc_client) {
2718                 ERR("Fail to get gatt client info");
2719                 return;
2720         }
2721
2722         //send event
2723         memset(&ev, 0, sizeof(ev));
2724         ev.conn_id = gattc_client->conn_id;
2725         ev.status = result;
2726         ev.client_if = gattc_client->client_if;
2727         memcpy(ev.bdaddr, gattc_client->bd_addr.address,
2728                                 BT_HAL_ADDRESS_LENGTH_MAX);
2729
2730         if (!event_cb) {
2731                 ERR("gatt client callback not registered");
2732         } else {
2733                 DBG("sending gatt client connected status  event");
2734                 event_cb(event, (void *)&ev, sizeof(ev));
2735         }
2736
2737         if (!gatt_connected) {
2738                 inst_id = gattc_client->inst_id;
2739
2740                 /* remove the gatt client info from the client list also*/
2741                 hal_gattc_client_info_list = g_slist_remove(hal_gattc_client_info_list, gattc_client);
2742                 g_free(gattc_client);
2743
2744                 //find the connected server info
2745                 conn_info = __bt_find_gatt_conn_info(&bd_addr);
2746                 if (NULL == conn_info) {
2747                         ERR("Fail to get gatt server info");
2748                         return;
2749                 }
2750
2751                 if (inst_id != conn_info->inst_id) {
2752                         ERR("server instance is different");
2753                         return;
2754                 }
2755
2756                 //remove gatt conn info from the server list
2757                 DBG("remove the server conn_info from list after gatt disconnection");
2758                 hal_gattc_server_info_list = g_slist_remove(hal_gattc_server_info_list, conn_info);
2759                 __hal_clean_gattc_server_info(conn_info);
2760         }
2761
2762         DBG("-");
2763 }
2764
2765 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
2766                                                 const char* uuid_str, int inst_id)
2767 {
2768         struct hal_ev_gatt_client_search_result ev;
2769
2770          if (!event_cb) {
2771                 ERR("gatt client callback not registered");
2772                 return;
2773         }
2774
2775         DBG("sending gatt client search service result event conn_id[%d]", conn_id);
2776
2777         memset(&ev, 0, sizeof(ev));
2778         ev.conn_id = conn_id;
2779         ev.inst_id = inst_id;
2780         ev.is_primary = is_primary;
2781         _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid_str);
2782
2783         event_cb(HAL_EV_GATT_CLIENT_SEARCH_RESULT, (void *)&ev, sizeof(ev));
2784 }
2785
2786 static void _bt_hal_send_search_service_complete_event(int conn_id, int status)
2787 {
2788         struct hal_ev_gatt_client_search_complete ev;
2789
2790          if (!event_cb) {
2791                 ERR("gatt client callback not registered");
2792                 return;
2793         }
2794
2795         DBG("sending gatt client search service complete event conn_id[%d]", conn_id);
2796
2797
2798         memset(&ev, 0, sizeof(ev));
2799         ev.conn_id = conn_id;
2800         ev.status = status;
2801
2802         event_cb(HAL_EV_GATT_CLIENT_SEARCH_COMPLETE, (void *)&ev, sizeof(ev));
2803 }