4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <hardware/bluetooth.h>
23 #include <hardware/bt_gatt.h>
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"
38 #include "bt-hal-gatt-client.h"
39 #include "bt-hal-dbus-common-utils.h"
40 #include "bt-hal-utils.h"
42 /************************************************************************************
44 ************************************************************************************/
45 extern const btgatt_callbacks_t *bt_gatt_callbacks;
46 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
48 ERR("%s: BTGATT not initialized", __FUNCTION__);\
49 return BT_STATUS_NOT_READY;\
51 DBG("%s", __FUNCTION__);\
55 int le_scan_type = BT_GATTC_LE_SCAN_TYPE_PASSIVE;
58 static handle_stack_msg event_cb = NULL;
60 #define GATT_SERV_INTERFACE "org.bluez.GattService1"
61 #define MAX_HAL_OBJECT_PATH_LEN 100
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;
82 HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE = 1,
83 HAL_GATT_WRITE_TYPE_WRITE ,
84 } hal_gatt_write_type_t;
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;
101 gboolean auto_connect;
102 } bt_pending_le_conn_info_s;
112 unsigned int permission;
113 GSList *gatt_list_descs;
119 GSList *gatt_list_chars;
120 } hal_gattc_service_t;
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;
128 /* Linked List of connected GATT server */
129 static GSList *hal_gattc_server_info_list = NULL;
133 bt_bdaddr_t bd_addr; /*remote server address*/
136 } hal_gattc_client_info_t;
138 /* Linked list of connected GATT client connection */
139 static GSList * hal_gattc_client_info_list = NULL;
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;
146 #define BT_GATTC_CL_MAX 32
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;
159 } hal_gatt_client_app;
161 static GSList * hal_gattc_client_app_list = NULL;
163 static int bt_client_if = 0;
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,
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);
181 /* To send stack event to hal-av handler */
182 void _bt_hal_register_gatt_client_handler_cb(handle_stack_msg cb)
187 void _bt_hal_unregister_gatt_client_handler_cb(void)
193 int _bt_hal_gatt_client_get_le_scan_type(void)
199 static gboolean __bt_hal_register_client_cb(gpointer user_data)
201 struct hal_ev_gatt_client_registered ev;
202 hal_gatt_client_app *client_info = user_data;
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));
212 ERR("GATT Callback not registered");
214 DBG("GATT client registered, client_if: [%d]", client_info->client_if);
215 event_cb(HAL_EV_GATT_CLIENT_REGISTERED, (void *)&ev, sizeof(ev));
222 static int __hal_generate_client_id()
224 return ++bt_client_if;
227 static hal_gatt_client_app *__hal_gattc_add_client_app(bt_uuid_t *app_uuid)
230 hal_gatt_client_app *info = NULL;
231 hal_gatt_client_app *gattc_app = NULL;
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;
239 if (memcmp(&info->app_uuid, app_uuid, sizeof(bt_uuid_t)) == 0) {
240 DBG("gatt client app already registered");
245 DBG("adding the gatt client app");
248 gattc_app = g_malloc0(sizeof(hal_gatt_client_app));
249 if (gattc_app == NULL) {
250 DBG("Failed to allocate memory");
254 gattc_app->client_if = __hal_generate_client_id();
255 memcpy(&gattc_app->app_uuid, app_uuid, sizeof(bt_uuid_t));
257 hal_gattc_client_app_list = g_slist_append(hal_gattc_client_app_list, gattc_app);
262 static bt_status_t __hal_gattc_register_client_app(bt_uuid_t *app_uuid)
264 hal_gatt_client_app *gattc_app = NULL;
265 hal_gatt_client_app *client_app_info = NULL;
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;
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;
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);
286 DBG("registered client client_if [%d]", client_app_info->client_if);
288 return BT_STATUS_SUCCESS;
291 /** Registers a GATT client application with the stack */
292 bt_status_t btif_gattc_register_client(bt_uuid_t *uuid)
297 return __hal_gattc_register_client_app(uuid);
300 bt_status_t __hal_gattc_unregister_client(int client_if)
303 hal_gatt_client_app *info = NULL;
305 DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
307 /* remove the gatt client app */
308 for (l = hal_gattc_client_app_list; l != NULL; ) {
309 info = (hal_gatt_client_app*)l->data;
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);
322 DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
323 return BT_STATUS_SUCCESS;
326 /** Unregister a client application from the stack */
327 bt_status_t btif_gattc_unregister_client(int client_if)
332 return __hal_gattc_unregister_client(client_if);
335 /** Start or stop LE device scanning */
336 bt_status_t scan(int client_if, bool start)
343 ret = _bt_hal_adapter_le_start_scan();
345 ret = _bt_hal_adapter_le_stop_scan();
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,
354 int ret = BT_STATUS_SUCCESS;
360 return BT_STATUS_PARM_INVALID;
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;
371 pending_le_conn_timer_id =
372 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
374 return BT_STATUS_SUCCESS;
376 ERR("advertising is not stopped");
379 return __bt_connect_le_device_internal(client_if, bd_addr, is_direct);
383 static void __le_disconnection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
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;
394 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
395 g_object_unref(proxy);
397 ERR("Connect LE Dbus Call Error");
399 ERR("Error: %s\n", g_error->message);
400 g_clear_error(&g_error);
402 result = BT_STATUS_FAIL;
404 g_variant_unref(reply);
406 if (NULL == gatt_conn_info) {
407 ERR("server_data is NULL");
412 if (result == BT_STATUS_FAIL) {
413 memset(&ev, 0, sizeof(ev));
414 ev.conn_id = gatt_conn_info->conn_id;
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);
421 ERR("gatt client callback not registered");
423 DBG("sending gatt client disconnected event");
424 event_cb(HAL_EV_GATT_CLIENT_DISCONNECTED, (void *)&ev, sizeof(ev));
430 g_free(gatt_conn_info);
435 bt_status_t _hal_gattc_disconnect(int client_if, const bt_bdaddr_t *bd_addr,
438 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
440 GDBusProxy *device_proxy;
441 GDBusConnection *conn;
442 int ret = BT_STATUS_SUCCESS;
443 hal_gattc_client_info_t *gattc_data;
445 if (NULL == bd_addr) {
446 ERR("bd_addr is NULL");
447 return BT_STATUS_PARM_INVALID;
450 conn = _bt_hal_get_system_gconn();
452 ERR("_bt_gdbus_get_system_gconn returned NULL");
453 return BT_STATUS_FAIL;
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;
465 ERR("device_path:%s", device_path);
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);
471 if (NULL == device_proxy) {
472 ERR("device_proxy returned NULL");
473 return BT_STATUS_FAIL;
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;
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;
488 DBG("DisconnectLE [%s]", device_address);
490 g_dbus_proxy_call(device_proxy, "DisconnectLE",
492 G_DBUS_CALL_FLAGS_NONE,
493 BT_HAL_MAX_DBUS_TIMEOUT,
495 (GAsyncReadyCallback)__le_disconnection_req_cb, gattc_data);
499 g_object_unref(device_proxy);
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,
514 return _hal_gattc_disconnect(client_if, bd_addr, conn_id);
517 /** Clear the attribute cache for a given device */
518 bt_status_t refresh(int client_if, const bt_bdaddr_t *bd_addr)
521 return BT_STATUS_UNSUPPORTED;
524 static hal_gattc_service_t* _gattc_find_service_from_uuid(hal_gattc_server_info_t *conn_info, bt_uuid_t *svc_uuid)
529 hal_gattc_service_t *info = NULL;
531 for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
532 info = (hal_gattc_service_t*)l->data;
536 if (!memcmp(&info->svc_uuid, svc_uuid, sizeof(bt_uuid_t))) {
537 INFO("Found GATT service uuid");
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)
551 hal_gattc_char_t *info = NULL;
553 for (l = gattc_svc->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
554 info = (hal_gattc_char_t*)l->data;
558 if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t)) &&
559 (info->permission & prop)) {
560 INFO("Found GATT char uuid");
567 static hal_gattc_desc_t* _gattc_find_desc_from_uuid(hal_gattc_char_t *gattc_char, bt_uuid_t *desc_uuid)
572 hal_gattc_desc_t *info = NULL;
574 for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
575 info = (hal_gattc_desc_t*)l->data;
579 if (!memcmp(&info->desc_uuid, desc_uuid, sizeof(bt_uuid_t))) {
580 INFO("Found GATT descriptor uuid");
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)
592 hal_gattc_service_t *gattc_service = NULL;
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);
598 DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
600 conn_info->gatt_list_services = g_slist_append(conn_info->gatt_list_services, gattc_service);
602 DBG("svc path {%s] svc uuid [%s]", object_path, uuid_str);
604 return gattc_service;
607 static void _hal_gattc_add_characteristic(hal_gattc_service_t *gatt_svc, char *char_handle)
610 hal_gattc_char_t *gattc_char = NULL;
612 gattc_char = g_malloc0(sizeof(hal_gattc_char_t));
613 gattc_char->chr_path = g_strdup(char_handle);
615 DBG("svc path: [%s]", gatt_svc->svc_path);
616 DBG("char path: [%s]", gattc_char->chr_path);
618 gatt_svc->gatt_list_chars = g_slist_append(gatt_svc->gatt_list_chars, gattc_char);
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)
624 hal_gattc_service_t* gatt_svc = NULL;
626 gchar *gp_char_path = NULL;
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");
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);
643 g_ptr_array_free(gp_char_array, TRUE);
646 static void _hal_gattc_add_descriptor(hal_gattc_char_t *gattc_char, char *desc_path)
648 hal_gattc_desc_t *gattc_desc = NULL;
650 gattc_desc = g_malloc0(sizeof(hal_gattc_desc_t));
651 gattc_desc->desc_path = g_strdup(desc_path);
653 gattc_char->gatt_list_descs = g_slist_append(gattc_char->gatt_list_descs, gattc_desc);
655 DBG("char path: [%s]", gattc_char->chr_path);
656 DBG("desc path: [%s]", gattc_desc->desc_path);
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)
662 gchar *gp_desc_path = NULL;
667 if (char_uuid_str == NULL) {
668 DBG("char_uuid_str is NULL");
672 //update the char uuid
673 DBG("char UUID: [%s] ", char_uuid_str);
674 DBG("char path: [%s]", gattc_char->chr_path);
676 _bt_hal_convert_uuid_string_to_type(gattc_char->chr_uuid.uu, char_uuid_str);
678 //update char permission
679 gattc_char->permission = char_permission;
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);
688 static void _hal_gattc_update_desc_property(hal_gattc_desc_t *gattc_desc, const char* desc_uuid_str)
692 if (desc_uuid_str == NULL) {
693 DBG("char_uuid_str is NULL");
697 //update the descriptor uuid
698 DBG("desc UUID: [%s] ", desc_uuid_str);
699 DBG("desc path: [%s]", gattc_desc->desc_path);
701 _bt_hal_convert_uuid_string_to_type(gattc_desc->desc_uuid.uu, desc_uuid_str);
704 static void browse_service_char(int conn_id)
706 hal_gattc_server_info_t *conn_info = NULL;
710 hal_gattc_service_t *svc_info = NULL;
711 hal_gattc_char_t *char_info = NULL;
712 hal_gattc_desc_t *desc_info = NULL;
716 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
717 if (conn_info == NULL) {
718 DBG("conn_info is NULL");
722 DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
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)
729 DBG("svc path [%s]", svc_info->svc_path);
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)
737 DBG("char path[%s]", char_info->chr_path);
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)
745 DBG("desc path[%s]", desc_info->desc_path);
754 * Enumerate all GATT services on a connected device.
755 * Optionally, the results can be filtered for a given UUID.
757 static bt_status_t _gattc_client_search_service(int conn_id)
761 GVariant *result = NULL;
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;
774 const gchar *uuid_str = NULL;
776 hal_gattc_server_info_t *conn_info = NULL;
777 gboolean is_primary = FALSE;
780 char *char_handle = NULL;
781 GVariantIter *char_iter = NULL;
782 GPtrArray *gp_char_array = NULL;
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;
792 _bt_hal_convert_addr_type_to_string(device_address,
793 (unsigned char *)conn_info->bd_addr.address);
795 result = _bt_hal_get_managed_objects();
799 gp_array = g_ptr_array_new();
800 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
802 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
804 if (object_path == NULL)
807 _bt_hal_convert_device_path_to_address(object_path, temp_address);
809 if (g_strcmp0(temp_address, device_address) != 0)
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)
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);
825 g_ptr_array_add(gp_array, (gpointer)object_path);
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);
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);
847 _gattc_create_new_service(conn_info, is_primary, uuid_str, object_path, gp_char_array);
852 if (gp_array->len == 0 || svc_count == 0) {
853 ERR("gp_array is NULL");
854 ret = BT_STATUS_FAIL;
856 ret = BT_STATUS_SUCCESS;
859 browse_service_char(conn_id);
860 /* send search service complete event */
861 _bt_hal_send_search_service_complete_event(conn_id, ret);
863 g_ptr_array_free(gp_array, TRUE);
864 g_variant_iter_free(iter);
865 g_variant_unref(result);
870 bt_status_t btif_gattc_client_search_service(int conn_id, bt_uuid_t *filter_uuid)
872 if (NULL == filter_uuid) {
873 DBG("Browse all the services");
874 return _gattc_client_search_service(conn_id);
876 DBG("TODO implement it");
877 return BT_STATUS_UNSUPPORTED;
881 * Enumerate included services for a given service.
882 * Set start_incl_srvc_id to NULL to get the first included service.
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)
888 return BT_STATUS_UNSUPPORTED;
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)
894 struct hal_ev_gatt_client_char_search_result ev;
897 ERR("gatt client callback not registered");
901 DBG("sending gatt client search char result event conn_id[%d] status[%d]", conn_id, status);
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;
908 memcpy(ev.svc_uuid, svc_id->id.uuid.uu, sizeof(ev.svc_uuid));
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;
916 DBG("sending the char search event");
918 event_cb(HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
921 static int _hal_get_permission_flag(char *permission)
925 if (NULL == permission) {
926 ERR("gatt permission is NULL");
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;
961 static bt_status_t _hal_gattc_get_characteristic_info(hal_gattc_char_t *gattc_char)
963 GDBusProxy *properties_proxy = NULL;
964 GError *error = NULL;
965 GVariant *value = NULL;
966 GVariant *result = NULL;
967 GDBusConnection *g_conn;
969 char *char_desc_handle = NULL;
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;
978 unsigned int char_permission = 0 ;
982 if (gattc_char->chr_path == NULL) {
983 DBG("char path is NULL");
984 return BT_STATUS_FAIL;
986 char_handle = gattc_char->chr_path;
988 DBG("char path:[%s]", gattc_char->chr_path);
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;
996 properties_proxy = g_dbus_proxy_new_sync(g_conn,
997 G_DBUS_PROXY_FLAGS_NONE, NULL,
1000 BT_HAL_PROPERTIES_INTERFACE,
1003 if (properties_proxy == NULL) {
1004 ERR("properties_proxy returned NULL");
1005 return BT_STATUS_FAIL;
1008 result = g_dbus_proxy_call_sync(properties_proxy,
1010 g_variant_new("(s)", BT_HAL_GATT_CHAR_INTERFACE),
1011 G_DBUS_CALL_FLAGS_NONE,
1016 if (error != NULL) {
1017 ERR("Fail to get properties (Error: %s)", error->message);
1018 g_clear_error(&error);
1020 ERR("Fail to get properties");
1021 g_object_unref(properties_proxy);
1022 return BT_STATUS_FAIL;
1025 gp_desc_array = g_ptr_array_new();
1027 g_variant_get(result, "(a{sv})", &property_iter);
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;
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);
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);
1046 g_ptr_array_add(gp_desc_array, (gpointer)char_desc_handle);
1051 _hal_gattc_update_char_property(gattc_char, char_uuid_str, gp_desc_array, char_permission);
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);
1058 return BT_STATUS_SUCCESS;
1061 static bt_status_t _gattc_get_all_characteristic(int conn_id,
1062 btgatt_srvc_id_t *srvc_id)
1064 hal_gattc_server_info_t * conn_info = NULL;
1065 hal_gattc_service_t *gattc_service = NULL;
1067 hal_gattc_char_t *gattc_char = NULL;
1068 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1069 int status = BT_STATUS_FAIL;
1072 DBG("conn_id[%d]", conn_id);
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;
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;
1087 DBG("service path [%s]", gattc_service->svc_path);
1089 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1090 DBG("service uuid [%s]", svc_uuid_str);
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);
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);
1105 DBG("sending final event");
1107 status = BT_STATUS_FAIL;
1108 _bt_hal_send_client_char_search_result_event(conn_id, status, srvc_id, NULL, 0);
1110 browse_service_char(conn_id);
1111 /* retrive uuid for characteristic and object path for descriptor */
1113 return BT_STATUS_SUCCESS;
1117 * Enumerate characteristics for a given service.
1118 * Set start_char_id to NULL to get the first characteristic.
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)
1124 CHECK_BTGATT_INIT();
1126 if (start_char_id == NULL) {
1127 DBG("Get all the characteristics");
1128 return _gattc_get_all_characteristic(conn_id, srvc_id);
1131 DBG("TBD Get specific characteristics");
1132 return BT_STATUS_UNSUPPORTED;
1136 static bt_status_t _hal_gattc_get_descriptor_info(hal_gattc_desc_t *gattc_desc)
1138 GDBusProxy *properties_proxy = NULL;
1139 GError *error = NULL;
1140 GVariant *value = NULL;
1141 GVariant *result = NULL;
1142 GDBusConnection *g_conn;
1145 GVariantIter *property_iter;
1146 char* desc_handle = NULL;
1147 const gchar *desc_uuid_str = NULL;
1151 if (gattc_desc->desc_path == NULL) {
1152 DBG("desc path is NULL");
1153 return BT_STATUS_FAIL;
1155 desc_handle = gattc_desc->desc_path;
1157 DBG("desc path:[%s]", gattc_desc->desc_path);
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;
1165 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1166 G_DBUS_PROXY_FLAGS_NONE, NULL,
1169 BT_HAL_PROPERTIES_INTERFACE,
1172 if (properties_proxy == NULL) {
1173 ERR("properties_proxy returned NULL");
1174 return BT_STATUS_FAIL;
1177 result = g_dbus_proxy_call_sync(properties_proxy,
1179 g_variant_new("(s)", BT_HAL_GATT_DESC_INTERFACE),
1180 G_DBUS_CALL_FLAGS_NONE,
1185 if (error != NULL) {
1186 ERR("Fail to get properties (Error: %s)", error->message);
1187 g_clear_error(&error);
1189 ERR("Fail to get properties");
1190 g_object_unref(properties_proxy);
1191 return BT_STATUS_FAIL;
1194 g_variant_get(result, "(a{sv})", &property_iter);
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);
1203 _hal_gattc_update_desc_property(gattc_desc, desc_uuid_str);
1205 g_variant_iter_free(property_iter);
1206 g_variant_unref(result);
1207 g_object_unref(properties_proxy);
1209 return BT_STATUS_SUCCESS;
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)
1215 struct hal_ev_gatt_client_desc_search_result ev;
1218 ERR("gatt client callback not registered");
1222 DBG("sending gatt client search desc result event conn_id[%d] status[%d]", conn_id, status);
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;
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));
1233 if (status == BT_STATUS_SUCCESS) {
1234 DBG("building desc uuid");
1235 memcpy(ev.desc_uuid, desc_uuid->uu, sizeof(ev.desc_uuid));
1238 DBG("sending the desc search event");
1240 event_cb(HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
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)
1246 hal_gattc_server_info_t * conn_info = NULL;
1247 hal_gattc_service_t *gattc_service = NULL;
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;
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;
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;
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);
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)
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);
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)
1294 status = _hal_gattc_get_descriptor_info(gattc_desc);
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);
1306 DBG("sending final event");
1308 status = BT_STATUS_FAIL;
1309 _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id, char_id, NULL);
1311 browse_service_char(conn_id);
1312 /* retrive uuid for characteristic and object path for descriptor */
1314 return BT_STATUS_SUCCESS;
1318 * Enumerate descriptors for a given characteristic.
1319 * Set start_descr_id to NULL to get the first descriptor.
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)
1326 CHECK_BTGATT_INIT();
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);
1332 DBG("TBD Get specific descriptor");
1333 return BT_STATUS_UNSUPPORTED;
1337 static void __hal_send_char_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1339 struct hal_ev_gatt_client_read_data ev;
1342 ERR("gatt client callback not registered");
1346 DBG("sending gatt client charac read conn_id[%d] status[%d]", resp_data->conn_id, result);
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;
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));
1359 DBG("building the char read value [%d]", len);
1360 memcpy(ev.value, value, len);
1363 DBG("sending the gatt client read charac event");
1365 event_cb(HAL_EV_GATT_CLIENT_READ_CHARAC, (void *)&ev, sizeof(ev));
1368 static void __hal_internal_read_char_cb(GObject *source_object,
1369 GAsyncResult *res, gpointer user_data)
1371 GError *error = NULL;
1372 GDBusConnection *system_gconn = NULL;
1375 GByteArray *gp_byte_array = NULL;
1377 hal_gatt_resp_data_t *resp_data = user_data;
1378 int result = BT_STATUS_SUCCESS;
1383 system_gconn = _bt_hal_get_system_gconn();
1384 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1387 ERR("Read Characteristic dbus failed Error:[%s]", error->message);
1390 result = BT_STATUS_FAIL;
1391 __hal_send_char_read_event(resp_data, result, NULL, 0);
1392 g_clear_error(&error);
1397 gp_byte_array = g_byte_array_new();
1398 g_variant_get(value, "(ay)", &iter);
1400 while (g_variant_iter_loop(iter, "y", &g_byte))
1401 g_byte_array_append(gp_byte_array, &g_byte, 1);
1405 for (i = 0; i < gp_byte_array->len; i++)
1406 DBG("%02x", gp_byte_array->data[i]);
1409 __hal_send_char_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1413 g_byte_array_free(gp_byte_array, TRUE);
1414 g_variant_iter_free(iter);
1415 g_variant_unref(value);
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)
1424 GDBusConnection *g_conn;
1425 hal_gatt_resp_data_t *resp_data;
1426 hal_gattc_service_t *gattc_service = NULL;
1427 GVariantBuilder *builder = NULL;
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;
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;
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;
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);
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;
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);
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;
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;
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));
1484 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1487 g_variant_builder_add(builder, "{sv}", "offset",
1488 g_variant_new("q", offset));
1490 char_handle = gattc_char->chr_path;
1492 DBG("calling char read value");
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);
1501 return BT_STATUS_SUCCESS;
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,
1509 CHECK_BTGATT_INIT();
1512 return _hal_read_characteristic_value(conn_id, srvc_id, char_id, auth_req);
1515 static void __hal_send_char_write_event(hal_gatt_resp_data_t *resp_data, int result)
1517 struct hal_ev_gatt_client_write_result ev;
1520 ERR("gatt client callback not registered");
1524 DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
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;
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));
1535 DBG("sending the gatt client write charac event");
1537 event_cb(HAL_EV_GATT_CLIENT_WRITE_CHARAC, (void *)&ev, sizeof(ev));
1540 static void __hal_bluetooth_internal_write_cb(GObject *source_object,
1541 GAsyncResult *res, gpointer user_data)
1543 GError *error = NULL;
1544 GDBusConnection *system_gconn = NULL;
1546 hal_gatt_resp_data_t *resp_data = user_data;
1547 int result = BT_STATUS_SUCCESS;
1551 system_gconn = _bt_hal_get_system_gconn();
1552 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1555 ERR("write Characteristic dbus failed Error:[%s]", error->message);
1557 result = BT_STATUS_FAIL;
1559 __hal_send_char_write_event(resp_data, result);
1560 g_clear_error(&error);
1565 //send write value event
1566 __hal_send_char_write_event(resp_data, result);
1569 g_variant_unref(value);
1574 static bt_status_t __hal_get_write_prop(hal_gatt_write_type_t type, bt_gatt_characteristic_property_t *prop)
1577 case HAL_GATT_WRITE_TYPE_WRITE:
1578 *prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
1580 case HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE:
1581 *prop = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
1584 ERR("Unknow write type : %d", type);
1585 return BT_STATUS_FAIL;
1588 return BT_STATUS_SUCCESS;
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)
1594 GVariant *val, *options;
1595 GVariantBuilder *builder1;
1596 GVariantBuilder *builder2;
1597 GDBusConnection *g_conn;
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;
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;
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;
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;
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);
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;
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);
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;
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;
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));
1663 char_handle = gattc_char->chr_path;
1665 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1667 for (i = 0; i < length; i++)
1668 g_variant_builder_add(builder1, "y", value[i]);
1670 val = g_variant_new("ay", builder1);
1672 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1674 g_variant_builder_add(builder2, "{sv}", "offset",
1675 g_variant_new_uint16(offset));
1677 options = g_variant_new("a{sv}", builder2);
1679 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, char_handle, BT_HAL_GATT_CHAR_INTERFACE,
1681 g_variant_new("(y@ay@a{sv})", write_prop, val, options),
1683 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1684 (GAsyncReadyCallback)__hal_bluetooth_internal_write_cb,
1685 (gpointer)resp_data);
1687 g_variant_builder_unref(builder1);
1688 g_variant_builder_unref(builder2);
1690 return BT_STATUS_SUCCESS;
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,
1699 CHECK_BTGATT_INIT();
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);
1708 static void __hal_send_desc_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1710 struct hal_ev_gatt_client_read_data ev;
1713 ERR("gatt client callback not registered");
1717 DBG("sending gatt client desc read conn_id[%d] status[%d]", resp_data->conn_id, result);
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;
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));
1731 DBG("building the desc read value [%d]", len);
1732 memcpy(ev.value, value, len);
1735 DBG("sending the gatt client read descriptor event");
1737 event_cb(HAL_EV_GATT_CLIENT_READ_DESC, (void *)&ev, sizeof(ev));
1740 static void __hal_internal_read_desc_cb(GObject *source_object,
1741 GAsyncResult *res, gpointer user_data)
1743 GError *error = NULL;
1744 GDBusConnection *system_gconn = NULL;
1747 GByteArray *gp_byte_array = NULL;
1749 hal_gatt_resp_data_t *resp_data = user_data;
1750 int result = BT_STATUS_SUCCESS;
1755 system_gconn = _bt_hal_get_system_gconn();
1756 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1759 ERR("Read descriptor dbus failed Error:[%s]", error->message);
1762 result = BT_STATUS_FAIL;
1763 __hal_send_desc_read_event(resp_data, result, NULL, 0);
1764 g_clear_error(&error);
1769 gp_byte_array = g_byte_array_new();
1770 g_variant_get(value, "(ay)", &iter);
1772 while (g_variant_iter_loop(iter, "y", &g_byte))
1773 g_byte_array_append(gp_byte_array, &g_byte, 1);
1777 for (i = 0; i < gp_byte_array->len; i++)
1778 DBG("%02x", gp_byte_array->data[i]);
1781 __hal_send_desc_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1785 g_byte_array_free(gp_byte_array, TRUE);
1786 g_variant_iter_free(iter);
1787 g_variant_unref(value);
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)
1795 GDBusConnection *g_conn;
1796 hal_gatt_resp_data_t *resp_data;
1797 hal_gattc_service_t *gattc_service = NULL;
1798 GVariantBuilder *builder = NULL;
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;
1808 hal_gattc_desc_t *gattc_desc = NULL;
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;
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;
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);
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)
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);
1843 /* find descriptor */
1844 gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &desc_id->uuid);
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);
1854 if (NULL == gattc_desc) {
1855 DBG("Failed to get the gatt desc");
1856 return BT_STATUS_FAIL;
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;
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;
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));
1876 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1879 g_variant_builder_add(builder, "{sv}", "offset",
1880 g_variant_new("q", offset));
1882 desc_handle = gattc_desc->desc_path;
1884 DBG("calling desc read value");
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);
1893 return BT_STATUS_SUCCESS;
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)
1901 CHECK_BTGATT_INIT();
1903 return _hal_read_descriptor_value(conn_id, srvc_id, char_id, descr_id, auth_req);
1906 static void __hal_send_desc_write_event(hal_gatt_resp_data_t *resp_data, int result)
1908 struct hal_ev_gatt_client_write_result ev;
1911 ERR("gatt client callback not registered");
1915 DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
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;
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));
1927 DBG("sending the gatt client write charac event");
1929 event_cb(HAL_EV_GATT_CLIENT_WRITE_DESC, (void *)&ev, sizeof(ev));
1932 static void __hal_bluetooth_internal_desc_write_cb(GObject *source_object,
1933 GAsyncResult *res, gpointer user_data)
1935 GError *error = NULL;
1936 GDBusConnection *system_gconn = NULL;
1938 hal_gatt_resp_data_t *resp_data = user_data;
1939 int result = BT_STATUS_SUCCESS;
1943 system_gconn = _bt_hal_get_system_gconn();
1944 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1947 ERR("write descriptor dbus failed Error: [%s]", error->message);
1950 result = BT_STATUS_FAIL;
1951 __hal_send_desc_write_event(resp_data, result);
1952 g_clear_error(&error);
1957 //send write value event
1958 __hal_send_desc_write_event(resp_data, result);
1961 g_variant_unref(value);
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)
1970 GVariant *val, *options;
1971 GVariantBuilder *builder1;
1972 GVariantBuilder *builder2;
1973 GDBusConnection *g_conn;
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;
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;
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;
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;
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);
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)
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);
2028 /* find descriptor */
2029 gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &descr_id->uuid);
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);
2039 if (NULL == gattc_desc) {
2040 DBG("Failed to get the gatt desc");
2041 return BT_STATUS_FAIL;
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;
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;
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));
2061 desc_handle = gattc_desc->desc_path;
2063 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2065 for (i = 0; i < length; i++)
2066 g_variant_builder_add(builder1, "y", value[i]);
2068 val = g_variant_new("ay", builder1);
2070 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2072 g_variant_builder_add(builder2, "{sv}", "offset",
2073 g_variant_new_uint16(offset));
2075 options = g_variant_new("a{sv}", builder2);
2077 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, desc_handle, BT_HAL_GATT_DESC_INTERFACE,
2079 g_variant_new("(@ay@a{sv})", val, options),
2081 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2082 (GAsyncReadyCallback)__hal_bluetooth_internal_desc_write_cb,
2083 (gpointer)resp_data);
2085 g_variant_builder_unref(builder1);
2086 g_variant_builder_unref(builder2);
2088 return BT_STATUS_SUCCESS;
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)
2097 CHECK_BTGATT_INIT();
2099 return _hal_write_descriptor_value(conn_id, srvc_id, char_id, descr_id, write_type,
2100 len, auth_req, p_value);
2103 /** Execute a prepared write operation */
2104 bt_status_t execute_write(int conn_id, int execute)
2106 CHECK_BTGATT_INIT();
2107 return BT_STATUS_UNSUPPORTED;
2110 static gboolean _hal_watch_register_notifi_cb(gpointer user_data)
2112 struct hal_ev_gatt_client_watch_notification ev;
2113 hal_gatt_resp_data_t *resp_data = user_data;
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 */
2120 ev.status = resp_data->result;
2122 ev.is_primary = resp_data->srvc_id.is_primary;
2123 ev.inst_id = resp_data->srvc_id.id.inst_id;
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));
2129 ERR("GATT Callback not registered");
2131 event_cb(HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION, (void *)&ev, sizeof(ev));
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)
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;
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;
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;
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;
2174 if (conn_info->inst_id != gattc_client->inst_id) {
2175 ERR("could not fild the conn_info");
2176 return BT_STATUS_FAIL;
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;
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);
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;
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);
2203 char_handle = gattc_char->chr_path;
2205 g_conn = _bt_hal_get_system_gconn();
2206 if (g_conn == NULL) {
2208 return BT_STATUS_FAIL;
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;
2217 DBG("#StartNotify");
2218 g_dbus_connection_call_sync(g_conn,
2221 BT_HAL_GATT_CHAR_INTERFACE,
2225 G_DBUS_CALL_FLAGS_NONE,
2226 BT_HAL_MAX_DBUS_TIMEOUT, NULL, &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;
2246 result = BT_STATUS_FAIL;
2248 g_clear_error(&error);
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));
2256 g_idle_add(_hal_watch_register_notifi_cb, (gpointer)resp_data);
2260 return BT_STATUS_SUCCESS;
2264 * Register to receive notifications or indications for a given
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)
2271 CHECK_BTGATT_INIT();
2273 return _hal_register_for_notification(client_if, (bt_bdaddr_t *)bd_addr, srvc_id, char_id);
2276 static gboolean _hal_watch_deregister_notifi_cb(gpointer user_data)
2278 struct hal_ev_gatt_client_watch_notification ev;
2279 hal_gatt_resp_data_t *resp_data = user_data;
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 */
2286 ev.status = resp_data->result;
2288 ev.is_primary = resp_data->srvc_id.is_primary;
2289 ev.inst_id = resp_data->srvc_id.id.inst_id;
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));
2295 ERR("GATT Callback not registered");
2297 event_cb(HAL_EV_GATT_CLIENT_WATCH_NOTIFICATION, (void *)&ev, sizeof(ev));
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)
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;
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;
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;
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;
2340 if (conn_info->inst_id != gattc_client->inst_id) {
2341 ERR("could not fild the conn_info");
2342 return BT_STATUS_FAIL;
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;
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);
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;
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);
2369 char_handle = gattc_char->chr_path;
2371 g_conn = _bt_hal_get_system_gconn();
2372 if (g_conn == NULL) {
2374 return BT_STATUS_FAIL;
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;
2383 DBG("#StartNotify");
2384 g_dbus_connection_call_sync(g_conn,
2387 BT_HAL_GATT_CHAR_INTERFACE,
2391 G_DBUS_CALL_FLAGS_NONE,
2392 BT_HAL_MAX_DBUS_TIMEOUT, NULL, &error);
2395 ERR("### Watch Failed: %s", error->message);
2396 g_clear_error(&error);
2397 result = BT_STATUS_FAIL;
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));
2405 g_idle_add(_hal_watch_deregister_notifi_cb, (gpointer)resp_data);
2409 return BT_STATUS_SUCCESS;
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)
2416 CHECK_BTGATT_INIT();
2417 return _hal_deregister_for_notification(client_if, (bt_bdaddr_t *)bd_addr, srvc_id, char_id);
2420 /** Request RSSI for a given remote device */
2421 bt_status_t read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
2423 CHECK_BTGATT_INIT();
2424 return BT_STATUS_UNSUPPORTED;
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)
2430 CHECK_BTGATT_INIT();
2431 return BT_STATUS_UNSUPPORTED;
2434 /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
2435 int get_device_type(const bt_bdaddr_t *bd_addr)
2437 CHECK_BTGATT_INIT();
2438 return BT_STATUS_UNSUPPORTED;
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)
2444 gchar *device_path = NULL;
2445 GError *error = NULL;
2446 GDBusProxy *device_proxy = NULL;
2447 GDBusConnection *conn;
2449 int ret = BT_STATUS_SUCCESS;
2450 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
2452 INFO("Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2453 min_int, max_int, latency, timeout);
2455 conn = _bt_hal_get_system_gconn();
2458 return BT_STATUS_FAIL;
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);
2465 if (device_path == NULL) {
2466 ERR("device_path NULL : [%s]", device_address);
2467 return BT_STATUS_FAIL;
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);
2474 g_free(device_path);
2475 if (NULL == device_proxy) {
2476 ERR("device_proxy returned NULL");
2477 return BT_STATUS_FAIL;
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,
2488 g_object_unref(device_proxy);
2489 if (reply == NULL) {
2491 ERR("Error %s[%s]", error->message, device_address);
2492 if (g_strrstr(error->message, "In Progress"))
2493 ret = BT_STATUS_SUCCESS;
2495 ret = BT_STATUS_FAIL;
2496 g_error_free(error);
2500 g_variant_unref(reply);
2502 INFO("LE Connection parameter Updated");
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)
2509 CHECK_BTGATT_INIT();
2513 return __hal_update_conn_parameter(bd, min_int, max_int, latency, timeout);
2516 /** Test mode interface */
2517 bt_status_t test_command(int command, btgatt_test_params_t* params)
2519 CHECK_BTGATT_INIT();
2520 return BT_STATUS_UNSUPPORTED;
2523 /** MTU Exchange request from client */
2524 bt_status_t configure_mtu(int conn_id, int mtu)
2526 CHECK_BTGATT_INIT();
2527 return BT_STATUS_UNSUPPORTED;
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)
2536 CHECK_BTGATT_INIT();
2537 return BT_STATUS_UNSUPPORTED;
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,
2548 CHECK_BTGATT_INIT();
2549 return BT_STATUS_UNSUPPORTED;
2552 /** Clear all scan filter conditions for specific filter index*/
2553 bt_status_t scan_filter_clear(int client_if, int filt_index)
2555 CHECK_BTGATT_INIT();
2556 return BT_STATUS_UNSUPPORTED;
2559 /** Enable / disable scan filter feature*/
2560 bt_status_t scan_filter_enable(int client_if, bool enable)
2562 CHECK_BTGATT_INIT();
2563 return BT_STATUS_UNSUPPORTED;
2566 /** Sets the LE scan interval and window in units of N*0.625 msec */
2568 bt_status_t set_scan_parameters(int scan_type, int scan_interval, int scan_window)
2572 CHECK_BTGATT_INIT();
2574 le_scan_type = scan_type;
2575 ret = _bt_hal_adapter_le_set_scan_parameters(
2576 scan_type, scan_interval, scan_window);
2580 bt_status_t set_scan_parameters(int scan_interval, int scan_window)
2582 CHECK_BTGATT_INIT();
2583 return BT_STATUS_UNSUPPORTED;
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)
2591 CHECK_BTGATT_INIT();
2592 return BT_STATUS_UNSUPPORTED;
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)
2599 CHECK_BTGATT_INIT();
2600 return BT_STATUS_UNSUPPORTED;
2603 /* Disable batchscan */
2604 bt_status_t batchscan_dis_batch_scan(int client_if)
2606 CHECK_BTGATT_INIT();
2607 return BT_STATUS_UNSUPPORTED;
2610 /* Read out batchscan reports */
2611 bt_status_t batchscan_read_reports(int client_if, int scan_mode)
2613 CHECK_BTGATT_INIT();
2614 return BT_STATUS_UNSUPPORTED;
2617 const btgatt_client_interface_t btgatt_client_interface = {
2618 btif_gattc_register_client,
2619 btif_gattc_unregister_client,
2621 btif_gattc_client_connect,
2622 btif_gattc_client_disconnect,
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,
2633 btif_register_for_notification,
2634 btif_deregister_for_notification,
2638 btif_gattc_conn_parameter_update,
2641 scan_filter_param_setup,
2642 scan_filter_add_remove,
2645 set_scan_parameters,
2646 batchscan_cfg_storage,
2647 batchscan_enb_batch_scan,
2648 batchscan_dis_batch_scan,
2649 batchscan_read_reports
2652 static hal_gattc_server_info_t *__bt_find_gatt_conn_info(bt_bdaddr_t *serv_addr)
2657 hal_gattc_server_info_t *info = NULL;
2659 for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2660 info = (hal_gattc_server_info_t*)l->data;
2664 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2665 INFO("GATT connection found addr");
2672 static hal_gattc_client_info_t *__bt_find_gatt_client_info(bt_bdaddr_t *serv_addr)
2677 hal_gattc_client_info_t *info = NULL;
2679 for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2680 info = (hal_gattc_client_info_t*)l->data;
2684 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2685 INFO("GATT client info found addr");
2692 static hal_gattc_client_info_t *__bt_find_gatt_client_info_from_conn_id(int conn_id)
2697 hal_gattc_client_info_t *info = NULL;
2699 for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2700 info = (hal_gattc_client_info_t*)l->data;
2704 if (info->conn_id == conn_id) {
2705 INFO("GATT client info found for conn_id [%d]", conn_id);
2712 static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int conn_id)
2717 hal_gattc_server_info_t *info = NULL;
2718 hal_gattc_client_info_t *gattc_client = NULL;
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");
2726 for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2727 info = (hal_gattc_server_info_t*)l->data;
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);
2740 static bt_status_t __bt_connect_le_device_internal(int client_if, const bt_bdaddr_t *bd_addr,
2741 gboolean auto_connect)
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;
2752 if (NULL == bd_addr) {
2753 ERR("bd_addr is NULL");
2754 return BT_STATUS_PARM_INVALID;
2757 conn = _bt_hal_get_system_gconn();
2759 ERR("_bt_gdbus_get_system_gconn returned NULL");
2760 return BT_STATUS_FAIL;
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;
2771 ERR("device_path:%s", device_path);
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;
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;
2788 memcpy(gattc_data->bd_addr.address, bd_addr->address,
2789 BT_HAL_ADDRESS_LENGTH_MAX);
2791 DBG("Connect LE [%s]", device_address);
2793 gattc_data->client_if = client_if;
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,
2800 (GAsyncReadyCallback)__le_connection_req_cb, gattc_data);
2806 g_object_unref(device_proxy);
2813 static bt_status_t _bt_hold_current_advertising()
2815 int ret = BT_STATUS_FAIL;
2816 gboolean is_advertising = FALSE;
2819 is_advertising = _bt_hal_is_advertising_in_slot(0);
2820 if (is_advertising) {
2821 DBG("+ Stop current advertising");
2823 ret = _bt_hal_enable_advertising(0, FALSE, FALSE);
2824 g_timeout_add(2000, __bt_hold_current_advertising_timeout_cb, NULL);
2830 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data)
2832 DBG("+ start current advertising");
2834 _bt_hal_enable_advertising(0, TRUE, FALSE);
2839 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
2841 DBG("Try to initiate pending LE connection");
2843 pending_le_conn_timer_id = 0;
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);
2849 g_free(pending_le_conn_info);
2850 pending_le_conn_info = NULL;
2855 static int __hal_generate_conn_id()
2857 return ++bt_conn_id;
2860 static int __hal_generate_server_instance_id()
2862 return ++bt_inst_id;
2865 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
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;
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");
2882 ERR("Error: %s\n", g_error->message);
2883 g_clear_error(&g_error);
2885 result = BT_STATUS_FAIL;
2887 g_variant_unref(reply);
2889 if (NULL == gattc_data) {
2890 ERR("server_data is NULL");
2895 if (result == BT_STATUS_FAIL) {
2896 memset(&ev, 0, sizeof(ev));
2899 ev.client_if = gattc_data->client_if;
2900 memcpy(ev.bdaddr, gattc_data->bd_addr.address,
2901 BT_HAL_ADDRESS_LENGTH_MAX);
2904 ERR("gatt client callback not registered");
2906 DBG("sending gatt client connected event");
2907 event_cb(HAL_EV_GATT_CLIENT_CONNECTED, (void *)&ev, sizeof(ev));
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();
2917 hal_gattc_client_info_list = g_slist_append(hal_gattc_client_info_list, gattc_data);
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");
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);
2934 /*remove conn_info*/
2939 void __hal_gattc_free_svc_info(hal_gattc_service_t *svc_info)
2941 g_free(svc_info->svc_path);
2945 void __hal_gattc_free_char_info(hal_gattc_char_t *char_info)
2947 g_free(char_info->chr_path);
2951 void __hal_gattc_free_desc_info(hal_gattc_desc_t *desc_info)
2953 g_free(desc_info->desc_path);
2957 void __hal_clean_gattc_server_info(hal_gattc_server_info_t *conn_info)
2962 hal_gattc_service_t *svc_info = NULL;
2963 hal_gattc_char_t *char_info = NULL;
2964 hal_gattc_desc_t *desc_info = NULL;
2968 for (l = conn_info->gatt_list_services; l != NULL;) {
2969 svc_info = (hal_gattc_service_t*)l->data;
2970 if (svc_info == NULL)
2972 l = g_slist_next(l);
2974 for (m = svc_info->gatt_list_chars; m != NULL; ) {
2975 char_info = (hal_gattc_char_t*)m->data;
2976 if (char_info == NULL)
2978 m = g_slist_next(m);
2980 for (k = char_info->gatt_list_descs; k != NULL; ) {
2981 desc_info = (hal_gattc_desc_t*)k->data;
2982 if (desc_info == NULL)
2984 k = g_slist_next(k);
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);
2991 /*remove desc list*/
2992 g_slist_free(char_info->gatt_list_descs);
2993 char_info->gatt_list_descs = NULL;
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);
3000 /*remove char list*/
3001 g_slist_free(svc_info->gatt_list_chars);
3002 svc_info->gatt_list_chars = NULL;
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);
3009 /*remove svc list */
3010 g_slist_free(conn_info->gatt_list_services);
3011 conn_info->gatt_list_services = NULL;
3013 /*remove conn info*/
3017 void _bt_hal_handle_gattc_connected_event(char* address, gboolean gatt_connected)
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;
3024 hal_gattc_client_info_t *gattc_client = NULL;
3028 DBG("+ connected device address [%s]", address);
3030 event = gatt_connected ? HAL_EV_GATT_CLIENT_CONNECTED :
3031 HAL_EV_GATT_CLIENT_DISCONNECTED;
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");
3042 memset(&ev, 0, sizeof(ev));
3043 ev.conn_id = gattc_client->conn_id;
3045 ev.client_if = gattc_client->client_if;
3046 memcpy(ev.bdaddr, gattc_client->bd_addr.address,
3047 BT_HAL_ADDRESS_LENGTH_MAX);
3050 ERR("gatt client callback not registered");
3052 DBG("sending gatt client connected status event");
3053 event_cb(event, (void *)&ev, sizeof(ev));
3056 if (!gatt_connected) {
3057 inst_id = gattc_client->inst_id;
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);
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");
3070 if (inst_id != conn_info->inst_id) {
3071 ERR("server instance is different");
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);
3084 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
3085 const char* uuid_str, int inst_id)
3087 struct hal_ev_gatt_client_search_result ev;
3090 ERR("gatt client callback not registered");
3094 DBG("sending gatt client search service result event conn_id[%d]", conn_id);
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);
3102 event_cb(HAL_EV_GATT_CLIENT_SEARCH_RESULT, (void *)&ev, sizeof(ev));
3105 static void _bt_hal_send_search_service_complete_event(int conn_id, int status)
3107 struct hal_ev_gatt_client_search_complete ev;
3110 ERR("gatt client callback not registered");
3114 DBG("sending gatt client search service complete event conn_id[%d]", conn_id);
3117 memset(&ev, 0, sizeof(ev));
3118 ev.conn_id = conn_id;
3121 event_cb(HAL_EV_GATT_CLIENT_SEARCH_COMPLETE, (void *)&ev, sizeof(ev));