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