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;
142 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
143 static guint pending_le_conn_timer_id = 0;
144 static int bt_conn_id = 0;
145 static int bt_inst_id = 0;
147 #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;
160 } hal_gatt_client_app;
162 static GSList * hal_gattc_client_app_list = NULL;
164 static int bt_client_if = 0;
167 static bt_status_t __bt_connect_le_device_internal(int client_if, const bt_bdaddr_t *bd_addr,
168 gboolean auto_connect);
169 static bt_status_t _bt_hold_current_advertising();
170 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data);
171 static gboolean __bt_connect_le_timer_cb(gpointer user_data);
172 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
174 static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int conn_id);
175 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
176 const char* uuid_str, int inst_id);
177 static void _bt_hal_send_search_service_complete_event(int conn_id, int status);
180 /* To send stack event to hal-av handler */
181 void _bt_hal_register_gatt_client_handler_cb(handle_stack_msg cb)
186 void _bt_hal_unregister_gatt_client_handler_cb(void)
192 int _bt_hal_gatt_client_get_le_scan_type(void)
198 static gboolean __bt_hal_register_client_cb(gpointer user_data)
200 struct hal_ev_gatt_client_registered ev;
201 hal_gatt_client_app *client_info = user_data;
204 /* Prepare to send AV connecting event */
205 memset(&ev, 0, sizeof(ev));
206 ev.status = BT_STATUS_SUCCESS;
207 ev.client_if = client_info->client_if;
208 memcpy(ev.app_uuid, client_info->app_uuid.uu, sizeof(ev.app_uuid));
211 ERR("GATT Callback not registered");
213 DBG("GATT client registered, client_if: [%d]", client_info->client_if);
214 event_cb(HAL_EV_GATT_CLIENT_REGISTERED, (void *)&ev, sizeof(ev));
221 static int __hal_generate_client_id()
223 return ++bt_client_if;
226 static hal_gatt_client_app *__hal_gattc_add_client_app(bt_uuid_t *app_uuid)
229 hal_gatt_client_app *info = NULL;
230 hal_gatt_client_app *gattc_app = NULL;
232 //check if client app is already registered
233 for (l = hal_gattc_client_app_list; l != NULL; l = g_slist_next(l)) {
234 info = (hal_gatt_client_app*)l->data;
238 if (memcmp(&info->app_uuid, app_uuid, sizeof(bt_uuid_t)) == 0) {
239 DBG("gatt client app already registered");
244 DBG("adding the gatt client app");
247 gattc_app = g_malloc0(sizeof(hal_gatt_client_app));
248 if (gattc_app == NULL) {
249 DBG("Failed to allocate memory");
253 gattc_app->client_if = __hal_generate_client_id();
254 memcpy(&gattc_app->app_uuid, app_uuid, sizeof(bt_uuid_t));
256 hal_gattc_client_app_list = g_slist_append(hal_gattc_client_app_list, gattc_app);
261 static bt_status_t __hal_gattc_register_client_app(bt_uuid_t *app_uuid)
263 hal_gatt_client_app *gattc_app = NULL;
264 hal_gatt_client_app *client_app_info = NULL;
267 /* add gatt client in list */
268 gattc_app = __hal_gattc_add_client_app(app_uuid);
269 if (gattc_app == NULL) {
270 ERR("Failed to register gatt client app");
271 return BT_STATUS_FAIL;
275 client_app_info = g_malloc0(sizeof(hal_gatt_client_app));
276 if (NULL == client_app_info) {
277 ERR("Failed to allocate memory");
278 return BT_STATUS_FAIL;
281 client_app_info->client_if = gattc_app->client_if;
282 memcpy(&client_app_info->app_uuid, app_uuid, sizeof(bt_uuid_t));
283 g_idle_add(__bt_hal_register_client_cb, (gpointer)client_app_info);
285 DBG("registered client client_if [%d]", client_app_info->client_if);
287 return BT_STATUS_SUCCESS;
290 /** Registers a GATT client application with the stack */
291 bt_status_t btif_gattc_register_client(bt_uuid_t *uuid)
296 return __hal_gattc_register_client_app(uuid);
299 bt_status_t __hal_gattc_unregister_client(int client_if)
302 hal_gatt_client_app *info = NULL;
304 DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
306 /* remove the gatt client app */
307 for (l = hal_gattc_client_app_list; l != NULL; ) {
308 info = (hal_gatt_client_app*)l->data;
314 if (info->client_if == client_if) {
315 DBG("gatt client app found");
316 hal_gattc_client_app_list = g_slist_remove(hal_gattc_client_app_list, info);
321 DBG("registered client count: [%d]", g_slist_length(hal_gattc_client_app_list));
322 return BT_STATUS_SUCCESS;
325 /** Unregister a client application from the stack */
326 bt_status_t btif_gattc_unregister_client(int client_if)
331 return __hal_gattc_unregister_client(client_if);
334 /** Start or stop LE device scanning */
335 bt_status_t scan(int client_if, bool start)
342 ret = _bt_hal_adapter_le_start_scan();
344 ret = _bt_hal_adapter_le_stop_scan();
349 /** Create a connection to a remote LE or dual-mode device */
350 bt_status_t btif_gattc_client_connect(int client_if, const bt_bdaddr_t *bd_addr,
353 int ret = BT_STATUS_SUCCESS;
359 return BT_STATUS_PARM_INVALID;
361 ret = _bt_hold_current_advertising();
362 if (ret == BT_STATUS_SUCCESS) {
363 DBG("Current advertising is held");
364 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
365 pending_le_conn_info->client_if = client_if;
366 memcpy(pending_le_conn_info->bd_addr.address, bd_addr->address,
367 BT_HAL_ADDRESS_LENGTH_MAX);
368 pending_le_conn_info->auto_connect = is_direct;
370 pending_le_conn_timer_id =
371 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
373 return BT_STATUS_SUCCESS;
375 ERR("advertising is not stopped");
378 return __bt_connect_le_device_internal(client_if, bd_addr, is_direct);
382 static void __le_disconnection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
385 GError *g_error = NULL;
386 GVariant *reply = NULL;
387 hal_gattc_client_info_t *gatt_conn_info = user_data;
388 int result = BT_STATUS_SUCCESS;
389 struct hal_ev_gatt_client_connected ev;
393 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
394 g_object_unref(proxy);
396 ERR("Connect LE Dbus Call Error");
398 ERR("Error: %s\n", g_error->message);
399 g_clear_error(&g_error);
401 result = BT_STATUS_FAIL;
403 g_variant_unref(reply);
405 if (NULL == gatt_conn_info) {
406 ERR("server_data is NULL");
411 if (result == BT_STATUS_FAIL) {
412 memset(&ev, 0, sizeof(ev));
413 ev.conn_id = gatt_conn_info->conn_id;
415 ev.client_if = gatt_conn_info->client_if;
416 memcpy(ev.bdaddr, gatt_conn_info->bd_addr.address,
417 BT_HAL_ADDRESS_LENGTH_MAX);
420 ERR("gatt client callback not registered");
422 DBG("sending gatt client disconnected event");
423 event_cb(HAL_EV_GATT_CLIENT_DISCONNECTED, (void *)&ev, sizeof(ev));
429 g_free(gatt_conn_info);
434 bt_status_t _hal_gattc_disconnect(int client_if, const bt_bdaddr_t *bd_addr,
437 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
439 GDBusProxy *device_proxy;
440 GDBusConnection *conn;
441 int ret = BT_STATUS_SUCCESS;
442 hal_gattc_client_info_t *gattc_data;
444 if (NULL == bd_addr) {
445 ERR("bd_addr is NULL");
446 return BT_STATUS_PARM_INVALID;
449 conn = _bt_hal_get_system_gconn();
451 ERR("_bt_gdbus_get_system_gconn returned NULL");
452 return BT_STATUS_FAIL;
455 _bt_hal_convert_addr_type_to_string(device_address,
456 (unsigned char *)bd_addr->address);
457 device_path = _bt_hal_get_device_object_path(device_address);
458 if (device_path == NULL) {
459 DBG("device_path NULL");
460 ret = BT_STATUS_FAIL;
464 ERR("device_path:%s", device_path);
466 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
467 NULL, BT_HAL_BLUEZ_NAME,
468 device_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
470 if (NULL == device_proxy) {
471 ERR("device_proxy returned NULL");
472 return BT_STATUS_FAIL;
475 gattc_data = g_malloc0(sizeof(hal_gattc_client_info_t));
476 if (NULL == gattc_data) {
477 ERR("Unable to allocate memory");
478 ret = BT_STATUS_FAIL;
482 memcpy(gattc_data->bd_addr.address, bd_addr->address,
483 BT_HAL_ADDRESS_LENGTH_MAX);
484 gattc_data->client_if = client_if;
485 gattc_data->conn_id = conn_id;
487 DBG("DisconnectLE [%s]", device_address);
489 g_dbus_proxy_call(device_proxy, "DisconnectLE",
491 G_DBUS_CALL_FLAGS_NONE,
492 BT_HAL_MAX_DBUS_TIMEOUT,
494 (GAsyncReadyCallback)__le_disconnection_req_cb, gattc_data);
498 g_object_unref(device_proxy);
505 /** Disconnect a remote device or cancel a pending connection */
506 bt_status_t btif_gattc_client_disconnect(int client_if, const bt_bdaddr_t *bd_addr,
513 return _hal_gattc_disconnect(client_if, bd_addr, conn_id);
516 /** Clear the attribute cache for a given device */
517 bt_status_t refresh(int client_if, const bt_bdaddr_t *bd_addr)
520 return BT_STATUS_UNSUPPORTED;
523 static hal_gattc_service_t* _gattc_find_service_from_uuid(hal_gattc_server_info_t *conn_info, bt_uuid_t *svc_uuid)
528 hal_gattc_service_t *info = NULL;
530 for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
531 info = (hal_gattc_service_t*)l->data;
535 if (!memcmp(&info->svc_uuid, svc_uuid, sizeof(bt_uuid_t))) {
536 INFO("Found GATT service uuid");
544 static hal_gattc_char_t* _gattc_find_char_from_uuid(hal_gattc_service_t *gattc_svc, bt_uuid_t *char_uuid)
549 hal_gattc_char_t *info = NULL;
551 for (l = gattc_svc->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
552 info = (hal_gattc_char_t*)l->data;
556 if (!memcmp(&info->chr_uuid, char_uuid, sizeof(bt_uuid_t))) {
557 INFO("Found GATT char uuid");
564 static hal_gattc_desc_t* _gattc_find_desc_from_uuid(hal_gattc_char_t *gattc_char, bt_uuid_t *desc_uuid)
569 hal_gattc_desc_t *info = NULL;
571 for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
572 info = (hal_gattc_desc_t*)l->data;
576 if (!memcmp(&info->desc_uuid, desc_uuid, sizeof(bt_uuid_t))) {
577 INFO("Found GATT descriptor uuid");
585 static hal_gattc_service_t* _hal_gatt_client_add_service(hal_gattc_server_info_t *conn_info,
586 const char *uuid_str, char *object_path)
589 hal_gattc_service_t *gattc_service = NULL;
591 gattc_service = g_malloc0(sizeof(hal_gattc_service_t));
592 gattc_service->svc_path = g_strdup(object_path);
593 _bt_hal_convert_uuid_string_to_type(gattc_service->svc_uuid.uu, uuid_str);
595 DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
597 conn_info->gatt_list_services = g_slist_append(conn_info->gatt_list_services, gattc_service);
599 DBG("svc path {%s] svc uuid [%s]", object_path, uuid_str);
601 return gattc_service;
604 static void _hal_gattc_add_characteristic(hal_gattc_service_t *gatt_svc, char *char_handle)
607 hal_gattc_char_t *gattc_char = NULL;
609 gattc_char = g_malloc0(sizeof(hal_gattc_char_t));
610 gattc_char->chr_path = g_strdup(char_handle);
612 DBG("svc path: [%s]", gatt_svc->svc_path);
613 DBG("char path: [%s]", gattc_char->chr_path);
615 gatt_svc->gatt_list_chars = g_slist_append(gatt_svc->gatt_list_chars, gattc_char);
618 static void _gattc_create_new_service(hal_gattc_server_info_t *conn_info, gboolean is_primary,
619 const char* uuid_str, char *object_path, GPtrArray *gp_char_array)
621 hal_gattc_service_t* gatt_svc = NULL;
623 gchar *gp_char_path = NULL;
627 /* add the service */
628 gatt_svc = _hal_gatt_client_add_service(conn_info, uuid_str, object_path);
629 if (gatt_svc == NULL) {
630 ERR("Failed to add service");
634 /* add the characteristic */
635 for (i = 0; i < gp_char_array->len; i++) {
636 gp_char_path = g_ptr_array_index(gp_char_array, i);
637 _hal_gattc_add_characteristic(gatt_svc, gp_char_path);
640 g_ptr_array_free(gp_char_array, TRUE);
643 static void _hal_gattc_add_descriptor(hal_gattc_char_t *gattc_char, char *desc_path)
645 hal_gattc_desc_t *gattc_desc = NULL;
647 gattc_desc = g_malloc0(sizeof(hal_gattc_desc_t));
648 gattc_desc->desc_path = g_strdup(desc_path);
650 gattc_char->gatt_list_descs = g_slist_append(gattc_char->gatt_list_descs, gattc_desc);
652 DBG("char path: [%s]", gattc_char->chr_path);
653 DBG("desc path: [%s]", gattc_desc->desc_path);
656 static void _hal_gattc_update_char_property(hal_gattc_char_t *gattc_char, const char* char_uuid_str,
657 GPtrArray *gp_desc_array, unsigned int char_permission)
659 gchar *gp_desc_path = NULL;
664 if (char_uuid_str == NULL) {
665 DBG("char_uuid_str is NULL");
669 //update the char uuid
670 DBG("char UUID: [%s] ", char_uuid_str);
671 DBG("char path: [%s]", gattc_char->chr_path);
673 _bt_hal_convert_uuid_string_to_type(gattc_char->chr_uuid.uu, char_uuid_str);
675 //update char permission
676 gattc_char->permission = char_permission;
679 for (i = 0; i < gp_desc_array->len; i++) {
680 gp_desc_path = g_ptr_array_index(gp_desc_array, i);
681 _hal_gattc_add_descriptor(gattc_char, gp_desc_path);
685 static void _hal_gattc_update_desc_property(hal_gattc_desc_t *gattc_desc, const char* desc_uuid_str)
689 if (desc_uuid_str == NULL) {
690 DBG("char_uuid_str is NULL");
694 //update the descriptor uuid
695 DBG("desc UUID: [%s] ", desc_uuid_str);
696 DBG("desc path: [%s]", gattc_desc->desc_path);
698 _bt_hal_convert_uuid_string_to_type(gattc_desc->desc_uuid.uu, desc_uuid_str);
701 static void browse_service_char(int conn_id)
703 hal_gattc_server_info_t *conn_info = NULL;
707 hal_gattc_service_t *svc_info = NULL;
708 hal_gattc_char_t *char_info = NULL;
709 hal_gattc_desc_t *desc_info = NULL;
713 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
714 if (conn_info == NULL) {
715 DBG("conn_info is NULL");
719 DBG("service count[%d]", g_slist_length(conn_info->gatt_list_services));
721 for (l = conn_info->gatt_list_services; l != NULL; l = g_slist_next(l)) {
722 svc_info = (hal_gattc_service_t*)l->data;
723 if (svc_info == NULL)
726 DBG("svc path [%s]", svc_info->svc_path);
728 /* find characteristic object path */
729 for (k = svc_info->gatt_list_chars; k != NULL; k = g_slist_next(k)) {
730 char_info = (hal_gattc_char_t *)k->data;
731 if (char_info == NULL)
734 DBG("char path[%s]", char_info->chr_path);
737 for (m = char_info->gatt_list_descs; m != NULL; m = g_slist_next(m)) {
738 desc_info = (hal_gattc_desc_t *)m->data;
739 if (desc_info == NULL)
742 DBG("desc path[%s]", desc_info->desc_path);
751 * Enumerate all GATT services on a connected device.
752 * Optionally, the results can be filtered for a given UUID.
754 static bt_status_t _gattc_client_search_service(int conn_id)
758 GVariant *result = NULL;
760 GVariantIter *svc_iter;
761 GVariantIter *interface_iter;
762 char *object_path = NULL;
763 char *interface_str = NULL;
764 const gchar *key = NULL;
765 GVariant *value = NULL;
766 GPtrArray *gp_array = NULL;
767 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
768 char temp_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
769 int ret = BT_STATUS_FAIL;
771 const gchar *uuid_str = NULL;
773 hal_gattc_server_info_t *conn_info = NULL;
774 gboolean is_primary = FALSE;
777 char *char_handle = NULL;
778 GVariantIter *char_iter = NULL;
779 GPtrArray *gp_char_array = NULL;
783 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
784 if (NULL == conn_info) {
785 DBG("Failed to get the conn_info");
786 return BT_STATUS_FAIL;
789 _bt_hal_convert_addr_type_to_string(device_address,
790 (unsigned char *)conn_info->bd_addr.address);
792 result = _bt_hal_get_managed_objects();
796 gp_array = g_ptr_array_new();
797 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
799 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
801 if (object_path == NULL)
804 _bt_hal_convert_device_path_to_address(object_path, temp_address);
806 if (g_strcmp0(temp_address, device_address) != 0)
809 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
810 &interface_str, &svc_iter)) {
811 if (g_strcmp0(interface_str, GATT_SERV_INTERFACE) != 0)
814 DBG("[%d] Object Path : %s", idx++, object_path);
815 /* for characteristic */
816 gp_char_array = g_ptr_array_new();
817 while (g_variant_iter_loop(svc_iter, "{sv}", &key, &value)) {
818 if (g_strcmp0(key, "Primary") == 0) {
819 is_primary = g_variant_get_boolean(value);
822 g_ptr_array_add(gp_array, (gpointer)object_path);
825 } else if (g_strcmp0(key, "UUID") == 0) {
826 uuid_str = g_variant_get_string(value, &len);
827 DBG(" UUID: [%s]", uuid_str);
828 } else if (g_strcmp0(key, "Characteristics") == 0) {
829 g_variant_get(value, "ao", &char_iter);
830 if (char_iter != NULL) {
831 while (g_variant_iter_loop(char_iter, "&o", &char_handle)) {
832 DBG("char handle : %s", char_handle);
833 g_ptr_array_add(gp_char_array, (gpointer)char_handle);
839 DBG("send search service result event");
840 _bt_hal_send_search_service_result_event(conn_id, is_primary,
841 uuid_str, conn_info->inst_id);
843 _gattc_create_new_service(conn_info, is_primary, uuid_str, object_path, gp_char_array);
847 if (gp_array->len == 0 || svc_count == 0) {
848 ERR("gp_array is NULL");
849 ret = BT_STATUS_FAIL;
851 ret = BT_STATUS_SUCCESS;
854 browse_service_char(conn_id);
855 /* send search service complete event */
856 _bt_hal_send_search_service_complete_event(conn_id, ret);
858 g_ptr_array_free(gp_array, TRUE);
859 g_variant_iter_free(iter);
860 g_variant_unref(result);
865 bt_status_t btif_gattc_client_search_service(int conn_id, bt_uuid_t *filter_uuid)
867 if (NULL == filter_uuid) {
868 DBG("Browse all the services");
869 return _gattc_client_search_service(conn_id);
871 DBG("TODO implement it");
872 return BT_STATUS_UNSUPPORTED;
876 * Enumerate included services for a given service.
877 * Set start_incl_srvc_id to NULL to get the first included service.
879 bt_status_t get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
880 btgatt_srvc_id_t *start_incl_srvc_id)
883 return BT_STATUS_UNSUPPORTED;
886 static void _bt_hal_send_client_char_search_result_event(int conn_id, int status,
887 btgatt_srvc_id_t *svc_id, bt_uuid_t *char_uuid, int char_prop)
889 struct hal_ev_gatt_client_char_search_result ev;
892 ERR("gatt client callback not registered");
896 DBG("sending gatt client search char result event conn_id[%d] status[%d]", conn_id, status);
898 memset(&ev, 0, sizeof(ev));
899 ev.conn_id = conn_id;
900 ev.inst_id = svc_id->id.inst_id;
901 ev.is_primary = svc_id->is_primary;
903 memcpy(ev.svc_uuid, svc_id->id.uuid.uu, sizeof(ev.svc_uuid));
905 if (status == BT_STATUS_SUCCESS) {
906 DBG("building char uuid");
907 memcpy(ev.char_uuid, char_uuid->uu, sizeof(ev.char_uuid));
908 ev.char_prop = char_prop;
911 DBG("sending the char search event");
913 event_cb(HAL_EV_GATT_CLIENT_CHARAC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
916 static int _hal_get_permission_flag(char *permission)
920 if (NULL == permission) {
921 ERR("gatt permission is NULL");
925 if (!g_strcmp0(permission, "broadcast"))
926 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_BROADCAST;
927 else if (!g_strcmp0(permission, "read"))
928 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_READ;
929 else if (!g_strcmp0(permission, "write-without-response"))
930 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE_NO_RESPONSE;
931 else if (!g_strcmp0(permission, "write"))
932 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITE;
933 else if (!g_strcmp0(permission, "notify"))
934 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_NOTIFY;
935 else if (!g_strcmp0(permission, "indicate"))
936 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_INDICATE;
937 else if (!g_strcmp0(permission, "authenticated-signed-writes"))
938 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_SIGNED_WRITE;
939 else if (!g_strcmp0(permission, "reliable-write"))
940 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_RELIABLE_WRITE;
941 else if (!g_strcmp0(permission, "writable-auxiliaries"))
942 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_WRITABLE_AUXILIARIES;
943 else if (!g_strcmp0(permission, "encrypt-read"))
944 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_READ;
945 else if (!g_strcmp0(permission, "encrypt-write"))
946 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_WRITE;
947 else if (!g_strcmp0(permission, "encrypt-authenticated-read"))
948 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_READ;
949 else if (!g_strcmp0(permission, "encrypt-authenticated-write"))
950 ret = HAL_GATT_CHARACTERISTIC_PROPERTY_ENCRYPT_AUTHENTICATED_WRITE;
956 static bt_status_t _hal_gattc_get_characteristic_info(hal_gattc_char_t *gattc_char)
958 GDBusProxy *properties_proxy = NULL;
959 GError *error = NULL;
960 GVariant *value = NULL;
961 GVariant *result = NULL;
962 GDBusConnection *g_conn;
964 char *char_desc_handle = NULL;
966 GVariantIter *property_iter;
967 GVariantIter *char_desc_iter;
968 char* char_handle = NULL;
969 const gchar *char_uuid_str = NULL;
970 GPtrArray *gp_desc_array = NULL;
971 GVariantIter *char_perm_iter;
973 unsigned int char_permission = 0 ;
977 if (gattc_char->chr_path == NULL) {
978 DBG("char path is NULL");
979 return BT_STATUS_FAIL;
981 char_handle = gattc_char->chr_path;
983 DBG("char path:[%s]", gattc_char->chr_path);
985 g_conn = _bt_hal_get_system_gconn();
986 if (NULL == g_conn) {
987 ERR("_bt_gdbus_get_system_gconn returned NULL");
988 return BT_STATUS_FAIL;
991 properties_proxy = g_dbus_proxy_new_sync(g_conn,
992 G_DBUS_PROXY_FLAGS_NONE, NULL,
995 BT_HAL_PROPERTIES_INTERFACE,
998 if (properties_proxy == NULL) {
999 ERR("properties_proxy returned NULL");
1000 return BT_STATUS_FAIL;
1003 result = g_dbus_proxy_call_sync(properties_proxy,
1005 g_variant_new("(s)", BT_HAL_GATT_CHAR_INTERFACE),
1006 G_DBUS_CALL_FLAGS_NONE,
1011 if (error != NULL) {
1012 ERR("Fail to get properties (Error: %s)", error->message);
1013 g_clear_error(&error);
1015 ERR("Fail to get properties");
1016 g_object_unref(properties_proxy);
1017 return BT_STATUS_FAIL;
1020 gp_desc_array = g_ptr_array_new();
1022 g_variant_get(result, "(a{sv})", &property_iter);
1024 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1025 if (!g_strcmp0(key, "UUID")) {
1026 char_uuid_str = g_variant_dup_string(value, &len);
1027 DBG("char UUID [%s]", char_uuid_str);
1028 } else if (!g_strcmp0(key, "Flags")) {
1029 g_variant_get(value, "as", &char_perm_iter);
1030 char_permission = 0x00;
1032 while (g_variant_iter_loop(char_perm_iter, "s", &permission)) {
1033 DBG("char permission: [%s]", permission);
1034 char_permission |= _hal_get_permission_flag(permission);
1036 } else if (!g_strcmp0(key, "Descriptors")) {
1037 g_variant_get(value, "ao", &char_desc_iter);
1038 while (g_variant_iter_loop(char_desc_iter, "&o", &char_desc_handle)) {
1039 DBG("char descriptor handle : %s", char_desc_handle);
1041 g_ptr_array_add(gp_desc_array, (gpointer)char_desc_handle);
1046 _hal_gattc_update_char_property(gattc_char, char_uuid_str, gp_desc_array, char_permission);
1048 g_variant_iter_free(property_iter);
1049 g_variant_unref(result);
1050 g_object_unref(properties_proxy);
1051 g_ptr_array_free(gp_desc_array, TRUE);
1053 return BT_STATUS_SUCCESS;
1056 static bt_status_t _gattc_get_all_characteristic(int conn_id,
1057 btgatt_srvc_id_t *srvc_id)
1059 hal_gattc_server_info_t * conn_info = NULL;
1060 hal_gattc_service_t *gattc_service = NULL;
1062 hal_gattc_char_t *gattc_char = NULL;
1063 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1064 int status = BT_STATUS_FAIL;
1067 DBG("conn_id[%d]", conn_id);
1069 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1070 if (NULL == conn_info) {
1071 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1072 return BT_STATUS_FAIL;
1076 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1077 if (NULL == gattc_service) {
1078 DBG("Failed to get the gatt service");
1079 return BT_STATUS_FAIL;
1082 DBG("service path [%s]", gattc_service->svc_path);
1084 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1085 DBG("service uuid [%s]", svc_uuid_str);
1087 /* find characteristic object path */
1088 for (l = gattc_service->gatt_list_chars; l != NULL; l = g_slist_next(l)) {
1089 gattc_char = (hal_gattc_char_t *)l->data;
1090 status = _hal_gattc_get_characteristic_info(gattc_char);
1093 if (BT_STATUS_SUCCESS == status) {
1094 DBG("Sending the success charateristics event");
1095 _bt_hal_send_client_char_search_result_event(conn_id, status, srvc_id,
1096 &gattc_char->chr_uuid, gattc_char->permission);
1100 DBG("sending final event");
1102 status = BT_STATUS_FAIL;
1103 _bt_hal_send_client_char_search_result_event(conn_id, status, srvc_id, NULL, 0);
1105 browse_service_char(conn_id);
1106 /* retrive uuid for characteristic and object path for descriptor */
1108 return BT_STATUS_SUCCESS;
1112 * Enumerate characteristics for a given service.
1113 * Set start_char_id to NULL to get the first characteristic.
1115 bt_status_t btif_gattc_get_characteristic(int conn_id,
1116 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
1119 CHECK_BTGATT_INIT();
1121 if (start_char_id == NULL) {
1122 DBG("Get all the characteristics");
1123 return _gattc_get_all_characteristic(conn_id, srvc_id);
1126 DBG("TBD Get specific characteristics");
1127 return BT_STATUS_UNSUPPORTED;
1131 static bt_status_t _hal_gattc_get_descriptor_info(hal_gattc_desc_t *gattc_desc)
1133 GDBusProxy *properties_proxy = NULL;
1134 GError *error = NULL;
1135 GVariant *value = NULL;
1136 GVariant *result = NULL;
1137 GDBusConnection *g_conn;
1140 GVariantIter *property_iter;
1141 char* desc_handle = NULL;
1142 const gchar *desc_uuid_str = NULL;
1146 if (gattc_desc->desc_path == NULL) {
1147 DBG("desc path is NULL");
1148 return BT_STATUS_FAIL;
1150 desc_handle = gattc_desc->desc_path;
1152 DBG("desc path:[%s]", gattc_desc->desc_path);
1154 g_conn = _bt_hal_get_system_gconn();
1155 if (NULL == g_conn) {
1156 ERR("_bt_gdbus_get_system_gconn returned NULL");
1157 return BT_STATUS_FAIL;
1160 properties_proxy = g_dbus_proxy_new_sync(g_conn,
1161 G_DBUS_PROXY_FLAGS_NONE, NULL,
1164 BT_HAL_PROPERTIES_INTERFACE,
1167 if (properties_proxy == NULL) {
1168 ERR("properties_proxy returned NULL");
1169 return BT_STATUS_FAIL;
1172 result = g_dbus_proxy_call_sync(properties_proxy,
1174 g_variant_new("(s)", BT_HAL_GATT_DESC_INTERFACE),
1175 G_DBUS_CALL_FLAGS_NONE,
1180 if (error != NULL) {
1181 ERR("Fail to get properties (Error: %s)", error->message);
1182 g_clear_error(&error);
1184 ERR("Fail to get properties");
1185 g_object_unref(properties_proxy);
1186 return BT_STATUS_FAIL;
1189 g_variant_get(result, "(a{sv})", &property_iter);
1191 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
1192 if (!g_strcmp0(key, "UUID")) {
1193 desc_uuid_str = g_variant_dup_string(value, &len);
1194 DBG("desc UUID [%s]", desc_uuid_str);
1198 _hal_gattc_update_desc_property(gattc_desc, desc_uuid_str);
1200 g_variant_iter_free(property_iter);
1201 g_variant_unref(result);
1202 g_object_unref(properties_proxy);
1204 return BT_STATUS_SUCCESS;
1207 static void _bt_hal_send_client_desc_search_result_event(int conn_id, int status,
1208 btgatt_srvc_id_t *svc_id, btgatt_gatt_id_t *char_id, bt_uuid_t *desc_uuid)
1210 struct hal_ev_gatt_client_desc_search_result ev;
1213 ERR("gatt client callback not registered");
1217 DBG("sending gatt client search desc result event conn_id[%d] status[%d]", conn_id, status);
1219 memset(&ev, 0, sizeof(ev));
1220 ev.conn_id = conn_id;
1221 ev.inst_id = svc_id->id.inst_id;
1222 ev.is_primary = svc_id->is_primary;
1225 memcpy(ev.svc_uuid, svc_id->id.uuid.uu, sizeof(ev.svc_uuid));
1226 memcpy(ev.char_uuid, char_id->uuid.uu, sizeof(ev.char_uuid));
1228 if (status == BT_STATUS_SUCCESS) {
1229 DBG("building desc uuid");
1230 memcpy(ev.desc_uuid, desc_uuid->uu, sizeof(ev.desc_uuid));
1233 DBG("sending the desc search event");
1235 event_cb(HAL_EV_GATT_CLIENT_DESC_SEARCH_RESULT, (void *)&ev, sizeof(ev));
1238 static bt_status_t _hal_gattc_get_all_descriptor(int conn_id,
1239 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id)
1241 hal_gattc_server_info_t * conn_info = NULL;
1242 hal_gattc_service_t *gattc_service = NULL;
1244 hal_gattc_char_t *gattc_char = NULL;
1245 hal_gattc_desc_t *gattc_desc = NULL;
1246 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1247 char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1248 int status = BT_STATUS_FAIL;
1252 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1253 if (NULL == conn_info) {
1254 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1255 return BT_STATUS_FAIL;
1259 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1260 if (NULL == gattc_service) {
1261 DBG("Failed to get the gatt service");
1262 return BT_STATUS_FAIL;
1265 DBG("service path [%s]", gattc_service->svc_path);
1266 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1267 DBG("service uuid [%s]", svc_uuid_str);
1269 /* find characteristics */
1270 gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1271 if (NULL == gattc_char) {
1272 DBG("Failed to get the gatt char");
1273 return BT_STATUS_FAIL;
1276 DBG("char path [%s]", gattc_char->chr_path);
1277 _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1278 DBG("char uuid [%s]", char_uuid_str);
1280 /* get descriptor uuid */
1281 for (l = gattc_char->gatt_list_descs; l != NULL; l = g_slist_next(l)) {
1282 gattc_desc = (hal_gattc_desc_t *)l->data;
1283 status = _hal_gattc_get_descriptor_info(gattc_desc);
1286 if (BT_STATUS_SUCCESS == status) {
1287 DBG("Sending the success descriptor event");
1288 _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id,
1289 char_id, &gattc_desc->desc_uuid);
1293 DBG("sending final event");
1295 status = BT_STATUS_FAIL;
1296 _bt_hal_send_client_desc_search_result_event(conn_id, status, srvc_id, char_id, NULL);
1298 browse_service_char(conn_id);
1299 /* retrive uuid for characteristic and object path for descriptor */
1301 return BT_STATUS_SUCCESS;
1305 * Enumerate descriptors for a given characteristic.
1306 * Set start_descr_id to NULL to get the first descriptor.
1308 bt_status_t btif_gattc_get_descriptor(int conn_id,
1309 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1310 btgatt_gatt_id_t *start_descr_id)
1313 CHECK_BTGATT_INIT();
1315 if (start_descr_id == NULL) {
1316 DBG("Get all the descriptors");
1317 return _hal_gattc_get_all_descriptor(conn_id, srvc_id, char_id);
1319 DBG("TBD Get specific descriptor");
1320 return BT_STATUS_UNSUPPORTED;
1324 static void __hal_send_char_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1326 struct hal_ev_gatt_client_read_data ev;
1329 ERR("gatt client callback not registered");
1333 DBG("sending gatt client charac read conn_id[%d] status[%d]", resp_data->conn_id, result);
1335 memset(&ev, 0, sizeof(ev));
1336 ev.conn_id = resp_data->conn_id;
1337 ev.inst_id = resp_data->srvc_id.id.inst_id;
1338 ev.is_primary = resp_data->srvc_id.is_primary;
1341 memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1342 memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1346 DBG("building the char read value [%d]", len);
1347 memcpy(ev.value, value, len);
1350 DBG("sending the gatt client read charac event");
1352 event_cb(HAL_EV_GATT_CLIENT_READ_CHARAC, (void *)&ev, sizeof(ev));
1355 static void __hal_internal_read_char_cb(GObject *source_object,
1356 GAsyncResult *res, gpointer user_data)
1358 GError *error = NULL;
1359 GDBusConnection *system_gconn = NULL;
1362 GByteArray *gp_byte_array = NULL;
1364 hal_gatt_resp_data_t *resp_data = user_data;
1365 int result = BT_STATUS_SUCCESS;
1370 system_gconn = _bt_hal_get_system_gconn();
1371 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1374 ERR("Read Characteristic dbus failed Error:", error->message);
1377 result = BT_STATUS_FAIL;
1378 __hal_send_char_read_event(resp_data, result, NULL, 0);
1379 g_clear_error(&error);
1384 gp_byte_array = g_byte_array_new();
1385 g_variant_get(value, "(ay)", &iter);
1387 while (g_variant_iter_loop(iter, "y", &g_byte))
1388 g_byte_array_append(gp_byte_array, &g_byte, 1);
1392 for (i = 0; i < gp_byte_array->len; i++)
1393 DBG("%02x", gp_byte_array->data[i]);
1396 __hal_send_char_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1400 g_byte_array_free(gp_byte_array, TRUE);
1401 g_variant_iter_free(iter);
1402 g_variant_unref(value);
1408 static bt_status_t _hal_read_characteristic_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1409 btgatt_gatt_id_t *char_id, int auth_req)
1411 GDBusConnection *g_conn;
1412 hal_gatt_resp_data_t *resp_data;
1413 hal_gattc_service_t *gattc_service = NULL;
1414 GVariantBuilder *builder = NULL;
1416 hal_gattc_server_info_t * conn_info = NULL;
1417 hal_gattc_char_t *gattc_char = NULL;
1418 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1419 char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1420 char* char_handle = NULL;
1424 /* get the connection info */
1425 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1426 if (NULL == conn_info) {
1427 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1428 return BT_STATUS_FAIL;
1432 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1433 if (NULL == gattc_service) {
1434 DBG("Failed to get the gatt service");
1435 return BT_STATUS_FAIL;
1438 DBG("service path [%s]", gattc_service->svc_path);
1439 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1440 DBG("service uuid [%s]", svc_uuid_str);
1443 /* find characteristic */
1444 gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1445 if (NULL == gattc_char) {
1446 DBG("Failed to get the gatt char");
1447 return BT_STATUS_FAIL;
1450 DBG("char path [%s]", gattc_char->chr_path);
1451 _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1452 DBG("char uuid [%s]", char_uuid_str);
1454 g_conn = _bt_hal_get_system_gconn();
1455 if (NULL == g_conn) {
1456 ERR("_bt_gdbus_get_system_gconn returned NULL");
1457 return BT_STATUS_FAIL;
1460 resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1461 if (NULL == resp_data) {
1462 ERR("failed to get the memory");
1463 return BT_STATUS_FAIL;
1466 resp_data->conn_id = conn_id;
1467 memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1468 memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1470 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1473 g_variant_builder_add(builder, "{sv}", "offset",
1474 g_variant_new("q", offset));
1476 char_handle = gattc_char->chr_path;
1478 DBG("calling char read value");
1480 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, char_handle, BT_HAL_GATT_CHAR_INTERFACE,
1481 "ReadValue", g_variant_new("(a{sv})", builder),
1482 G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1483 (GAsyncReadyCallback)__hal_internal_read_char_cb,
1484 (gpointer)resp_data);
1485 g_variant_builder_unref(builder);
1487 return BT_STATUS_SUCCESS;
1490 /** Read a characteristic on a remote device */
1491 bt_status_t btif_read_characteristic(int conn_id,
1492 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1495 CHECK_BTGATT_INIT();
1498 return _hal_read_characteristic_value(conn_id, srvc_id, char_id, auth_req);
1501 static void __hal_send_char_write_event(hal_gatt_resp_data_t *resp_data, int result)
1503 struct hal_ev_gatt_client_write_result ev;
1506 ERR("gatt client callback not registered");
1510 DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
1512 memset(&ev, 0, sizeof(ev));
1513 ev.conn_id = resp_data->conn_id;
1514 ev.inst_id = resp_data->srvc_id.id.inst_id;
1515 ev.is_primary = resp_data->srvc_id.is_primary;
1518 memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1519 memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1521 DBG("sending the gatt client write charac event");
1523 event_cb(HAL_EV_GATT_CLIENT_WRITE_CHARAC, (void *)&ev, sizeof(ev));
1526 static void __hal_bluetooth_internal_write_cb(GObject *source_object,
1527 GAsyncResult *res, gpointer user_data)
1529 GError *error = NULL;
1530 GDBusConnection *system_gconn = NULL;
1532 hal_gatt_resp_data_t *resp_data = user_data;
1533 int result = BT_STATUS_SUCCESS;
1537 system_gconn = _bt_hal_get_system_gconn();
1538 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1541 ERR("write Characteristic dbus failed Error:", error->message);
1543 result = BT_STATUS_FAIL;
1545 __hal_send_char_write_event(resp_data, result);
1546 g_clear_error(&error);
1551 //send write value event
1552 __hal_send_char_write_event(resp_data, result);
1555 g_variant_unref(value);
1560 static bt_status_t __hal_get_write_prop(hal_gatt_write_type_t type, hal_gatt_property_e *prop)
1563 case HAL_GATT_WRITE_TYPE_WRITE:
1564 *prop = HAL_GATT_PROPERTY_WRITE;
1566 case HAL_GATT_WRITE_TYPE_WRITE_NO_RESPONSE:
1567 *prop = HAL_GATT_PROPERTY_WRITE_WITHOUT_RESPONSE;
1570 ERR("Unknow write type : %d", type);
1571 return BT_STATUS_FAIL;
1574 return BT_STATUS_SUCCESS;
1577 static bt_status_t _hal_write_characteristic_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1578 btgatt_gatt_id_t *char_id, int write_type, int length, int auth_req, char* value)
1580 GVariant *val, *options;
1581 GVariantBuilder *builder1;
1582 GVariantBuilder *builder2;
1583 GDBusConnection *g_conn;
1586 hal_gatt_resp_data_t *resp_data;
1587 hal_gattc_service_t *gattc_service = NULL;
1588 hal_gattc_server_info_t * conn_info = NULL;
1589 hal_gattc_char_t *gattc_char = NULL;
1590 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1591 char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1592 char* char_handle = NULL;
1593 hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
1594 int ret = BT_STATUS_SUCCESS;
1598 ret = __hal_get_write_prop(write_type, &write_prop);
1599 if (BT_STATUS_FAIL == ret) {
1600 DBG("received invalid write type:[%d] ", write_type);
1601 return BT_STATUS_FAIL;
1604 /* get the connection info */
1605 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1606 if (NULL == conn_info) {
1607 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1608 return BT_STATUS_FAIL;
1612 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1613 if (NULL == gattc_service) {
1614 DBG("Failed to get the gatt service");
1615 return BT_STATUS_FAIL;
1618 DBG("service path [%s]", gattc_service->svc_path);
1619 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1620 DBG("service uuid [%s]", svc_uuid_str);
1622 /* find characteristic */
1623 gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1624 if (NULL == gattc_char) {
1625 DBG("Failed to get the gatt char");
1626 return BT_STATUS_FAIL;
1629 DBG("char path [%s]", gattc_char->chr_path);
1630 _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1631 DBG("char uuid [%s]", char_uuid_str);
1633 g_conn = _bt_hal_get_system_gconn();
1634 if (NULL == g_conn) {
1635 ERR("_bt_gdbus_get_system_gconn returned NULL");
1636 return BT_STATUS_FAIL;
1639 resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1640 if (NULL == resp_data) {
1641 ERR("failed to get the memory");
1642 return BT_STATUS_FAIL;
1645 resp_data->conn_id = conn_id;
1646 memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1647 memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1649 char_handle = gattc_char->chr_path;
1651 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1653 for (i = 0; i < length; i++)
1654 g_variant_builder_add(builder1, "y", value[i]);
1656 val = g_variant_new("ay", builder1);
1658 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1660 g_variant_builder_add(builder2, "{sv}", "offset",
1661 g_variant_new_uint16(offset));
1663 options = g_variant_new("a{sv}", builder2);
1665 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, char_handle, BT_HAL_GATT_CHAR_INTERFACE,
1667 g_variant_new("(y@ay@a{sv})", write_prop, val, options),
1669 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1670 (GAsyncReadyCallback)__hal_bluetooth_internal_write_cb,
1671 (gpointer)resp_data);
1673 g_variant_builder_unref(builder1);
1674 g_variant_builder_unref(builder2);
1676 return BT_STATUS_SUCCESS;
1679 /** Write a remote characteristic */
1680 bt_status_t btif_write_characteristic(int conn_id,
1681 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1682 int write_type, int len, int auth_req,
1685 CHECK_BTGATT_INIT();
1689 DBG("svc isntance id:[%d]", srvc_id->id.inst_id);
1690 return _hal_write_characteristic_value(conn_id, srvc_id, char_id, write_type,
1691 len, auth_req, p_value);
1694 static void __hal_send_desc_read_event(hal_gatt_resp_data_t *resp_data, int result, uint8_t *value, int len)
1696 struct hal_ev_gatt_client_read_data ev;
1699 ERR("gatt client callback not registered");
1703 DBG("sending gatt client desc read conn_id[%d] status[%d]", resp_data->conn_id, result);
1705 memset(&ev, 0, sizeof(ev));
1706 ev.conn_id = resp_data->conn_id;
1707 ev.inst_id = resp_data->srvc_id.id.inst_id;
1708 ev.is_primary = resp_data->srvc_id.is_primary;
1711 memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1712 memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1713 memcpy(ev.desc_uuid, resp_data->desc_id.uuid.uu, sizeof(ev.char_uuid));
1717 DBG("building the desc read value [%d]", len);
1718 memcpy(ev.value, value, len);
1721 DBG("sending the gatt client read descriptor event");
1723 event_cb(HAL_EV_GATT_CLIENT_READ_DESC, (void *)&ev, sizeof(ev));
1726 static void __hal_internal_read_desc_cb(GObject *source_object,
1727 GAsyncResult *res, gpointer user_data)
1729 GError *error = NULL;
1730 GDBusConnection *system_gconn = NULL;
1733 GByteArray *gp_byte_array = NULL;
1735 hal_gatt_resp_data_t *resp_data = user_data;
1736 int result = BT_STATUS_SUCCESS;
1741 system_gconn = _bt_hal_get_system_gconn();
1742 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1745 ERR("Read descriptor dbus failed Error:", error->message);
1748 result = BT_STATUS_FAIL;
1749 __hal_send_desc_read_event(resp_data, result, NULL, 0);
1750 g_clear_error(&error);
1755 gp_byte_array = g_byte_array_new();
1756 g_variant_get(value, "(ay)", &iter);
1758 while (g_variant_iter_loop(iter, "y", &g_byte))
1759 g_byte_array_append(gp_byte_array, &g_byte, 1);
1763 for (i = 0; i < gp_byte_array->len; i++)
1764 DBG("%02x", gp_byte_array->data[i]);
1767 __hal_send_desc_read_event(resp_data, result, gp_byte_array->data, gp_byte_array->len);
1771 g_byte_array_free(gp_byte_array, TRUE);
1772 g_variant_iter_free(iter);
1773 g_variant_unref(value);
1778 static bt_status_t _hal_read_descriptor_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1779 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *desc_id, int auth_req)
1781 GDBusConnection *g_conn;
1782 hal_gatt_resp_data_t *resp_data;
1783 hal_gattc_service_t *gattc_service = NULL;
1784 GVariantBuilder *builder = NULL;
1786 hal_gattc_server_info_t * conn_info = NULL;
1787 hal_gattc_char_t *gattc_char = NULL;
1788 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1789 char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1790 char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
1791 char* desc_handle = NULL;
1793 hal_gattc_desc_t *gattc_desc = NULL;
1797 /* get the connection info */
1798 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1799 if (NULL == conn_info) {
1800 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1801 return BT_STATUS_FAIL;
1805 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1806 if (NULL == gattc_service) {
1807 DBG("Failed to get the gatt service");
1808 return BT_STATUS_FAIL;
1811 DBG("service path [%s]", gattc_service->svc_path);
1812 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1813 DBG("service uuid [%s]", svc_uuid_str);
1815 /* find characteristic */
1816 gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1817 if (NULL == gattc_char) {
1818 DBG("Failed to get the gatt char");
1819 return BT_STATUS_FAIL;
1822 DBG("char path [%s]", gattc_char->chr_path);
1823 _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
1824 DBG("char uuid [%s]", char_uuid_str);
1826 /* find descriptor */
1827 gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &desc_id->uuid);
1828 if (NULL == gattc_desc) {
1829 DBG("Failed to get the gatt desc");
1830 return BT_STATUS_FAIL;
1833 DBG("desc path [%s]", gattc_desc->desc_path);
1834 _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
1835 DBG("desc uuid [%s]", desc_uuid_str);
1837 g_conn = _bt_hal_get_system_gconn();
1838 if (NULL == g_conn) {
1839 ERR("_bt_gdbus_get_system_gconn returned NULL");
1840 return BT_STATUS_FAIL;
1843 resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
1844 if (NULL == resp_data) {
1845 ERR("failed to get the memory");
1846 return BT_STATUS_FAIL;
1849 resp_data->conn_id = conn_id;
1850 memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
1851 memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
1852 memcpy(&resp_data->desc_id, desc_id, sizeof(btgatt_gatt_id_t));
1854 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1857 g_variant_builder_add(builder, "{sv}", "offset",
1858 g_variant_new("q", offset));
1860 desc_handle = gattc_desc->desc_path;
1862 DBG("calling desc read value");
1864 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, desc_handle, BT_HAL_GATT_DESC_INTERFACE,
1865 "ReadValue", g_variant_new("(a{sv})", builder),
1866 G_VARIANT_TYPE("(ay)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1867 (GAsyncReadyCallback)__hal_internal_read_desc_cb,
1868 (gpointer)resp_data);
1869 g_variant_builder_unref(builder);
1871 return BT_STATUS_SUCCESS;
1874 /** Read the descriptor for a given characteristic */
1875 bt_status_t btif_read_descriptor(int conn_id,
1876 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
1877 btgatt_gatt_id_t *descr_id, int auth_req)
1879 CHECK_BTGATT_INIT();
1881 return _hal_read_descriptor_value(conn_id, srvc_id, char_id, descr_id, auth_req);
1884 static void __hal_send_desc_write_event(hal_gatt_resp_data_t *resp_data, int result)
1886 struct hal_ev_gatt_client_write_result ev;
1889 ERR("gatt client callback not registered");
1893 DBG("sending gatt client charac write conn_id[%d] status[%d]", resp_data->conn_id, result);
1895 memset(&ev, 0, sizeof(ev));
1896 ev.conn_id = resp_data->conn_id;
1897 ev.inst_id = resp_data->srvc_id.id.inst_id;
1898 ev.is_primary = resp_data->srvc_id.is_primary;
1901 memcpy(ev.svc_uuid, resp_data->srvc_id.id.uuid.uu, sizeof(ev.svc_uuid));
1902 memcpy(ev.char_uuid, resp_data->char_id.uuid.uu, sizeof(ev.char_uuid));
1903 memcpy(ev.desc_uuid, resp_data->desc_id.uuid.uu, sizeof(ev.desc_uuid));
1905 DBG("sending the gatt client write charac event");
1907 event_cb(HAL_EV_GATT_CLIENT_WRITE_DESC, (void *)&ev, sizeof(ev));
1910 static void __hal_bluetooth_internal_desc_write_cb(GObject *source_object,
1911 GAsyncResult *res, gpointer user_data)
1913 GError *error = NULL;
1914 GDBusConnection *system_gconn = NULL;
1916 hal_gatt_resp_data_t *resp_data = user_data;
1917 int result = BT_STATUS_SUCCESS;
1921 system_gconn = _bt_hal_get_system_gconn();
1922 value = g_dbus_connection_call_finish(system_gconn, res, &error);
1925 ERR("write descriptor dbus failed Error:", error->message);
1928 result = BT_STATUS_FAIL;
1929 __hal_send_desc_write_event(resp_data, result);
1930 g_clear_error(&error);
1935 //send write value event
1936 __hal_send_desc_write_event(resp_data, result);
1939 g_variant_unref(value);
1944 static bt_status_t _hal_write_descriptor_value(int conn_id, btgatt_srvc_id_t *srvc_id,
1945 btgatt_gatt_id_t *char_id, btgatt_gatt_id_t *descr_id,
1946 int write_type, int length, int auth_req, char* value)
1948 GVariant *val, *options;
1949 GVariantBuilder *builder1;
1950 GVariantBuilder *builder2;
1951 GDBusConnection *g_conn;
1954 hal_gatt_resp_data_t *resp_data;
1955 hal_gattc_service_t *gattc_service = NULL;
1956 hal_gattc_server_info_t * conn_info = NULL;
1957 hal_gattc_char_t *gattc_char = NULL;
1958 hal_gattc_desc_t *gattc_desc = NULL;
1959 char svc_uuid_str[BT_HAL_UUID_STRING_LEN];
1960 char char_uuid_str[BT_HAL_UUID_STRING_LEN];
1961 char desc_uuid_str[BT_HAL_UUID_STRING_LEN];
1962 char* desc_handle = NULL;
1963 hal_gatt_property_e write_prop = HAL_GATT_PROPERTY_WRITE;
1964 int ret = BT_STATUS_SUCCESS;
1968 ret = __hal_get_write_prop(write_type, &write_prop);
1969 if (BT_STATUS_FAIL == ret) {
1970 DBG("received invalid write type:[%d] ", write_type);
1971 return BT_STATUS_FAIL;
1974 /* get the connection info */
1975 conn_info = __bt_find_gatt_conn_info_from_conn_id(conn_id);
1976 if (NULL == conn_info) {
1977 DBG("Failed to get the conn_info for conn_id[%d]", conn_id);
1978 return BT_STATUS_FAIL;
1982 gattc_service = _gattc_find_service_from_uuid(conn_info, &srvc_id->id.uuid);
1983 if (NULL == gattc_service) {
1984 DBG("Failed to get the gatt service");
1985 return BT_STATUS_FAIL;
1988 DBG("service path [%s]", gattc_service->svc_path);
1989 _bt_hal_convert_uuid_type_to_string(svc_uuid_str, gattc_service->svc_uuid.uu);
1990 DBG("service uuid [%s]", svc_uuid_str);
1992 /* find characteristic */
1993 gattc_char = _gattc_find_char_from_uuid(gattc_service, &char_id->uuid);
1994 if (NULL == gattc_char) {
1995 DBG("Failed to get the gatt char");
1996 return BT_STATUS_FAIL;
1999 DBG("char path [%s]", gattc_char->chr_path);
2000 _bt_hal_convert_uuid_type_to_string(char_uuid_str, gattc_char->chr_uuid.uu);
2001 DBG("char uuid [%s]", char_uuid_str);
2003 /* find descriptor */
2004 gattc_desc = _gattc_find_desc_from_uuid(gattc_char, &descr_id->uuid);
2005 if (NULL == gattc_desc) {
2006 DBG("Failed to get the gatt char");
2007 return BT_STATUS_FAIL;
2010 DBG("desc path [%s]", gattc_desc->desc_path);
2011 _bt_hal_convert_uuid_type_to_string(desc_uuid_str, gattc_desc->desc_uuid.uu);
2012 DBG("char uuid [%s]", desc_uuid_str);
2014 g_conn = _bt_hal_get_system_gconn();
2015 if (NULL == g_conn) {
2016 ERR("_bt_gdbus_get_system_gconn returned NULL");
2017 return BT_STATUS_FAIL;
2020 resp_data = g_malloc0(sizeof(hal_gatt_resp_data_t));
2021 if (NULL == resp_data) {
2022 ERR("failed to get the memory");
2023 return BT_STATUS_FAIL;
2026 resp_data->conn_id = conn_id;
2027 memcpy(&resp_data->srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
2028 memcpy(&resp_data->char_id, char_id, sizeof(btgatt_gatt_id_t));
2029 memcpy(&resp_data->desc_id, descr_id, sizeof(btgatt_gatt_id_t));
2031 desc_handle = gattc_desc->desc_path;
2033 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2035 for (i = 0; i < length; i++)
2036 g_variant_builder_add(builder1, "y", value[i]);
2038 val = g_variant_new("ay", builder1);
2040 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2042 g_variant_builder_add(builder2, "{sv}", "offset",
2043 g_variant_new_uint16(offset));
2045 options = g_variant_new("a{sv}", builder2);
2047 g_dbus_connection_call(g_conn, BT_HAL_BLUEZ_NAME, desc_handle, BT_HAL_GATT_DESC_INTERFACE,
2049 g_variant_new("(@ay@a{sv})", val, options),
2051 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2052 (GAsyncReadyCallback)__hal_bluetooth_internal_desc_write_cb,
2053 (gpointer)resp_data);
2055 g_variant_builder_unref(builder1);
2056 g_variant_builder_unref(builder2);
2058 return BT_STATUS_SUCCESS;
2061 /** Write a remote descriptor for a given characteristic */
2062 bt_status_t btif_write_descriptor(int conn_id,
2063 btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
2064 btgatt_gatt_id_t *descr_id, int write_type, int len,
2065 int auth_req, char* p_value)
2067 CHECK_BTGATT_INIT();
2069 return _hal_write_descriptor_value(conn_id, srvc_id, char_id, descr_id, write_type,
2070 len, auth_req, p_value);
2073 /** Execute a prepared write operation */
2074 bt_status_t execute_write(int conn_id, int execute)
2076 CHECK_BTGATT_INIT();
2077 return BT_STATUS_UNSUPPORTED;
2081 * Register to receive notifications or indications for a given
2084 bt_status_t register_for_notification(int client_if,
2085 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
2086 btgatt_gatt_id_t *char_id)
2088 CHECK_BTGATT_INIT();
2089 return BT_STATUS_UNSUPPORTED;
2092 /** Deregister a previous request for notifications/indications */
2093 bt_status_t deregister_for_notification(int client_if,
2094 const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
2095 btgatt_gatt_id_t *char_id)
2097 CHECK_BTGATT_INIT();
2098 return BT_STATUS_UNSUPPORTED;
2101 /** Request RSSI for a given remote device */
2102 bt_status_t read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
2104 CHECK_BTGATT_INIT();
2105 return BT_STATUS_UNSUPPORTED;
2108 /** OTA firmware download */
2109 bt_status_t ota_fw_update(int client_if, int conn_id, const bt_bdaddr_t *bd_addr, char* path)
2111 CHECK_BTGATT_INIT();
2112 return BT_STATUS_UNSUPPORTED;
2115 /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
2116 int get_device_type(const bt_bdaddr_t *bd_addr)
2118 CHECK_BTGATT_INIT();
2119 return BT_STATUS_UNSUPPORTED;
2122 static bt_status_t __hal_update_conn_parameter(bt_bdaddr_t *bd_addr,
2123 int min_int, int max_int, int latency, int timeout)
2125 gchar *device_path = NULL;
2126 GError *error = NULL;
2127 GDBusProxy *device_proxy = NULL;
2128 GDBusConnection *conn;
2130 int ret = BT_STATUS_SUCCESS;
2131 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
2133 INFO("Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2134 min_int, max_int, latency, timeout);
2136 conn = _bt_hal_get_system_gconn();
2139 return BT_STATUS_FAIL;
2142 _bt_hal_convert_addr_type_to_string(device_address,
2143 (unsigned char *)bd_addr->address);
2144 device_path = _bt_hal_get_device_object_path(device_address);
2146 if (device_path == NULL) {
2147 ERR("device_path NULL : [%s]", device_address);
2148 return BT_STATUS_FAIL;
2151 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2152 NULL, BT_HAL_BLUEZ_NAME,
2153 device_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
2155 g_free(device_path);
2156 if (NULL == device_proxy) {
2157 ERR("device_proxy returned NULL");
2158 return BT_STATUS_FAIL;
2161 INFO("### LeConnUpdate");
2162 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2163 g_variant_new("(uuuu)", min_int, max_int, latency, timeout),
2164 G_DBUS_CALL_FLAGS_NONE,
2169 g_object_unref(device_proxy);
2170 if (reply == NULL) {
2172 ERR("Error %s[%s]", error->message, device_address);
2173 if (g_strrstr(error->message, "In Progress"))
2174 ret = BT_STATUS_SUCCESS;
2176 ret = BT_STATUS_FAIL;
2177 g_error_free(error);
2181 g_variant_unref(reply);
2183 INFO("LE Connection parameter Updated");
2187 /** Request a connection parameter update */
2188 bt_status_t btif_gattc_conn_parameter_update(bt_bdaddr_t *bd, int min_int, int max_int, int latency, int timeout)
2190 CHECK_BTGATT_INIT();
2194 return __hal_update_conn_parameter(bd, min_int, max_int, latency, timeout);
2197 /** Test mode interface */
2198 bt_status_t test_command(int command, btgatt_test_params_t* params)
2200 CHECK_BTGATT_INIT();
2201 return BT_STATUS_UNSUPPORTED;
2204 /** MTU Exchange request from client */
2205 bt_status_t configure_mtu(int conn_id, int mtu)
2207 CHECK_BTGATT_INIT();
2208 return BT_STATUS_UNSUPPORTED;
2211 /** Setup scan filter params */
2212 bt_status_t scan_filter_param_setup(int client_if, int action, int filt_index, int feat_seln,
2213 int list_logic_type, int filt_logic_type, int rssi_high_thres,
2214 int rssi_low_thres, int dely_mode, int found_timeout,
2215 int lost_timeout, int found_timeout_cnt)
2217 CHECK_BTGATT_INIT();
2218 return BT_STATUS_UNSUPPORTED;
2221 /** Configure a scan filter condition */
2222 bt_status_t scan_filter_add_remove(int client_if, int action, int filt_type,
2223 int filt_index, int company_id,
2224 int company_id_mask, const bt_uuid_t *p_uuid,
2225 const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
2226 char addr_type, int data_len, char* p_data, int mask_len,
2229 CHECK_BTGATT_INIT();
2230 return BT_STATUS_UNSUPPORTED;
2233 /** Clear all scan filter conditions for specific filter index*/
2234 bt_status_t scan_filter_clear(int client_if, int filt_index)
2236 CHECK_BTGATT_INIT();
2237 return BT_STATUS_UNSUPPORTED;
2240 /** Enable / disable scan filter feature*/
2241 bt_status_t scan_filter_enable(int client_if, bool enable)
2243 CHECK_BTGATT_INIT();
2244 return BT_STATUS_UNSUPPORTED;
2247 /** Sets the LE scan interval and window in units of N*0.625 msec */
2249 bt_status_t set_scan_parameters(int scan_type, int scan_interval, int scan_window)
2253 CHECK_BTGATT_INIT();
2255 le_scan_type = scan_type;
2256 ret = _bt_hal_adapter_le_set_scan_parameters(
2257 scan_type, scan_interval, scan_window);
2261 bt_status_t set_scan_parameters(int scan_interval, int scan_window)
2263 CHECK_BTGATT_INIT();
2264 return BT_STATUS_UNSUPPORTED;
2268 /* Configure the batchscan storage */
2269 bt_status_t batchscan_cfg_storage(int client_if, int batch_scan_full_max,
2270 int batch_scan_trunc_max, int batch_scan_notify_threshold)
2272 CHECK_BTGATT_INIT();
2273 return BT_STATUS_UNSUPPORTED;
2276 /* Enable batchscan */
2277 bt_status_t batchscan_enb_batch_scan(int client_if, int scan_mode,
2278 int scan_interval, int scan_window, int addr_type, int discard_rule)
2280 CHECK_BTGATT_INIT();
2281 return BT_STATUS_UNSUPPORTED;
2284 /* Disable batchscan */
2285 bt_status_t batchscan_dis_batch_scan(int client_if)
2287 CHECK_BTGATT_INIT();
2288 return BT_STATUS_UNSUPPORTED;
2291 /* Read out batchscan reports */
2292 bt_status_t batchscan_read_reports(int client_if, int scan_mode)
2294 CHECK_BTGATT_INIT();
2295 return BT_STATUS_UNSUPPORTED;
2298 const btgatt_client_interface_t btgatt_client_interface = {
2299 btif_gattc_register_client,
2300 btif_gattc_unregister_client,
2302 btif_gattc_client_connect,
2303 btif_gattc_client_disconnect,
2305 btif_gattc_client_search_service,
2306 get_included_service,
2307 btif_gattc_get_characteristic,
2308 btif_gattc_get_descriptor,
2309 btif_read_characteristic,
2310 btif_write_characteristic,
2311 btif_read_descriptor,
2312 btif_write_descriptor,
2314 register_for_notification,
2315 deregister_for_notification,
2319 btif_gattc_conn_parameter_update,
2322 scan_filter_param_setup,
2323 scan_filter_add_remove,
2326 set_scan_parameters,
2327 batchscan_cfg_storage,
2328 batchscan_enb_batch_scan,
2329 batchscan_dis_batch_scan,
2330 batchscan_read_reports
2333 static hal_gattc_server_info_t *__bt_find_gatt_conn_info(bt_bdaddr_t *serv_addr)
2338 hal_gattc_server_info_t *info = NULL;
2340 for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2341 info = (hal_gattc_server_info_t*)l->data;
2345 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2346 INFO("GATT connection found addr");
2353 static hal_gattc_client_info_t *__bt_find_gatt_client_info(bt_bdaddr_t *serv_addr)
2358 hal_gattc_client_info_t *info = NULL;
2360 for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2361 info = (hal_gattc_client_info_t*)l->data;
2365 if (!memcmp(&info->bd_addr, serv_addr, sizeof(bt_bdaddr_t))) {
2366 INFO("GATT client info found addr");
2373 static hal_gattc_client_info_t *__bt_find_gatt_client_info_from_conn_id(int conn_id)
2378 hal_gattc_client_info_t *info = NULL;
2380 for (l = hal_gattc_client_info_list; l != NULL; l = g_slist_next(l)) {
2381 info = (hal_gattc_client_info_t*)l->data;
2385 if (info->conn_id == conn_id) {
2386 INFO("GATT client info found for conn_id [%d]", conn_id);
2393 static hal_gattc_server_info_t *__bt_find_gatt_conn_info_from_conn_id(int conn_id)
2398 hal_gattc_server_info_t *info = NULL;
2399 hal_gattc_client_info_t *gattc_client = NULL;
2401 gattc_client = __bt_find_gatt_client_info_from_conn_id(conn_id);
2402 if (gattc_client == NULL) {
2403 INFO("GATT client conn info not found");
2407 for (l = hal_gattc_server_info_list; l != NULL; l = g_slist_next(l)) {
2408 info = (hal_gattc_server_info_t*)l->data;
2412 if ((info->inst_id == gattc_client->inst_id) &&
2413 !memcmp(&info->bd_addr, &gattc_client->bd_addr, sizeof(bt_bdaddr_t))) {
2414 INFO("GATT connection found for conn_id [%d]", conn_id);
2421 static bt_status_t __bt_connect_le_device_internal(int client_if, const bt_bdaddr_t *bd_addr,
2422 gboolean auto_connect)
2424 char device_address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
2425 gchar *device_path = NULL;
2426 GDBusProxy *device_proxy = NULL;
2427 GDBusConnection *conn;
2428 int ret = BT_STATUS_SUCCESS;
2429 hal_gattc_client_info_t *gattc_data;
2433 if (NULL == bd_addr) {
2434 ERR("bd_addr is NULL");
2435 return BT_STATUS_PARM_INVALID;
2438 conn = _bt_hal_get_system_gconn();
2440 ERR("_bt_gdbus_get_system_gconn returned NULL");
2441 return BT_STATUS_FAIL;
2444 _bt_hal_convert_addr_type_to_string(device_address,
2445 (unsigned char *)bd_addr->address);
2446 device_path = _bt_hal_get_device_object_path(device_address);
2447 if (device_path == NULL) {
2448 ERR("device_path NULL : [%s]", device_address);
2449 ret = BT_STATUS_FAIL;
2452 ERR("device_path:%s", device_path);
2454 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2455 NULL, BT_HAL_BLUEZ_NAME,
2456 device_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
2457 g_free(device_path);
2458 if (NULL == device_proxy) {
2459 ERR("device_proxy returned NULL");
2460 return BT_STATUS_FAIL;
2463 gattc_data = g_malloc0(sizeof(hal_gattc_client_info_t));
2464 if (gattc_data == NULL) {
2465 ERR("Unable to allocate memory");
2466 ret = BT_STATUS_NOMEM;
2469 memcpy(gattc_data->bd_addr.address, bd_addr->address,
2470 BT_HAL_ADDRESS_LENGTH_MAX);
2472 DBG("Connect LE [%s]", device_address);
2474 gattc_data->client_if = client_if;
2476 g_dbus_proxy_call(device_proxy, "ConnectLE",
2477 g_variant_new("(b)", auto_connect),
2478 G_DBUS_CALL_FLAGS_NONE,
2479 BT_HAL_MAX_DBUS_TIMEOUT,
2481 (GAsyncReadyCallback)__le_connection_req_cb, gattc_data);
2487 g_object_unref(device_proxy);
2494 static bt_status_t _bt_hold_current_advertising()
2496 int ret = BT_STATUS_FAIL;
2497 gboolean is_advertising = FALSE;
2500 is_advertising = _bt_hal_is_advertising_in_slot(0);
2501 if (is_advertising) {
2502 DBG("+ Stop current advertising");
2504 ret = _bt_hal_enable_advertising(0, FALSE, FALSE);
2505 g_timeout_add(2000, __bt_hold_current_advertising_timeout_cb, NULL);
2511 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data)
2513 DBG("+ start current advertising");
2515 _bt_hal_enable_advertising(0, TRUE, FALSE);
2520 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
2522 DBG("Try to initiate pending LE connection");
2524 pending_le_conn_timer_id = 0;
2526 __bt_connect_le_device_internal(pending_le_conn_info->client_if,
2527 &pending_le_conn_info->bd_addr,
2528 pending_le_conn_info->auto_connect);
2530 g_free(pending_le_conn_info);
2531 pending_le_conn_info = NULL;
2536 static int __hal_generate_conn_id()
2538 return ++bt_conn_id;
2541 static int __hal_generate_server_instance_id()
2543 return ++bt_inst_id;
2546 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
2549 GError *g_error = NULL;
2550 GVariant *reply = NULL;
2551 hal_gattc_client_info_t *gattc_data = user_data;
2552 int result = BT_STATUS_SUCCESS;
2553 struct hal_ev_gatt_client_connected ev;
2554 hal_gattc_server_info_t *gatt_conn_info = NULL;
2558 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
2559 g_object_unref(proxy);
2560 if (reply == NULL) {
2561 ERR("Connect LE Dbus Call Error");
2563 ERR("Error: %s\n", g_error->message);
2564 g_clear_error(&g_error);
2566 result = BT_STATUS_FAIL;
2568 g_variant_unref(reply);
2570 if (NULL == gattc_data) {
2571 ERR("server_data is NULL");
2576 if (result == BT_STATUS_FAIL) {
2577 memset(&ev, 0, sizeof(ev));
2580 ev.client_if = gattc_data->client_if;
2581 memcpy(ev.bdaddr, gattc_data->bd_addr.address,
2582 BT_HAL_ADDRESS_LENGTH_MAX);
2585 ERR("gatt client callback not registered");
2587 DBG("sending gatt client connected event");
2588 event_cb(HAL_EV_GATT_CLIENT_CONNECTED, (void *)&ev, sizeof(ev));
2594 DBG("adding the server conn info in list");
2595 gattc_data->conn_id = __hal_generate_conn_id() ;
2596 gattc_data->inst_id = __hal_generate_server_instance_id();
2598 hal_gattc_client_info_list = g_slist_append(hal_gattc_client_info_list, gattc_data);
2600 /*add gatt server connection info*/
2601 gatt_conn_info = g_malloc0(sizeof(hal_gattc_server_info_t));
2602 if (gatt_conn_info == NULL) {
2603 ERR("Failed to allocate memory");
2607 memcpy(gatt_conn_info->bd_addr.address, gattc_data->bd_addr.address, BT_HAL_ADDRESS_LENGTH_MAX);
2608 gatt_conn_info->inst_id = gattc_data->inst_id;
2609 hal_gattc_server_info_list = g_slist_append(hal_gattc_server_info_list, gatt_conn_info);
2615 /*remove conn_info*/
2620 void __hal_gattc_free_svc_info(hal_gattc_service_t *svc_info)
2622 g_free(svc_info->svc_path);
2626 void __hal_gattc_free_char_info(hal_gattc_char_t *char_info)
2628 g_free(char_info->chr_path);
2632 void __hal_gattc_free_desc_info(hal_gattc_desc_t *desc_info)
2634 g_free(desc_info->desc_path);
2638 void __hal_clean_gattc_server_info(hal_gattc_server_info_t *conn_info)
2643 hal_gattc_service_t *svc_info = NULL;
2644 hal_gattc_char_t *char_info = NULL;
2645 hal_gattc_desc_t *desc_info = NULL;
2649 for (l = conn_info->gatt_list_services; l != NULL;) {
2650 svc_info = (hal_gattc_service_t*)l->data;
2651 if (svc_info == NULL)
2653 l = g_slist_next(l);
2655 for (m = svc_info->gatt_list_chars; m != NULL; ) {
2656 char_info = (hal_gattc_char_t*)m->data;
2657 if (char_info == NULL)
2659 m = g_slist_next(m);
2661 for (k = char_info->gatt_list_descs; k != NULL; ) {
2662 desc_info = (hal_gattc_desc_t*)k->data;
2663 if (desc_info == NULL)
2665 k = g_slist_next(k);
2667 /*remove desc element*/
2668 char_info->gatt_list_descs = g_slist_remove(char_info->gatt_list_descs, desc_info);
2669 __hal_gattc_free_desc_info(desc_info);
2672 /*remove desc list*/
2673 g_slist_free(char_info->gatt_list_descs);
2674 char_info->gatt_list_descs = NULL;
2676 /*remove char element*/
2677 svc_info->gatt_list_chars = g_slist_remove(svc_info->gatt_list_chars, char_info);
2678 __hal_gattc_free_char_info(char_info);
2681 /*remove char list*/
2682 g_slist_free(svc_info->gatt_list_chars);
2683 svc_info->gatt_list_chars = NULL;
2685 /*remove svc element*/
2686 conn_info->gatt_list_services = g_slist_remove(conn_info->gatt_list_services, svc_info);
2687 __hal_gattc_free_svc_info(svc_info);
2690 /*remove svc list */
2691 g_slist_free(conn_info->gatt_list_services);
2692 conn_info->gatt_list_services = NULL;
2694 /*remove conn info*/
2698 void _bt_hal_handle_gattc_connected_event(char* address, gboolean gatt_connected)
2700 int result = BT_STATUS_SUCCESS;
2701 struct hal_ev_gatt_client_connected ev;
2702 hal_gattc_server_info_t *conn_info = NULL;
2703 bt_bdaddr_t bd_addr;
2705 hal_gattc_client_info_t *gattc_client = NULL;
2709 DBG("+ connected device address [%s]", address);
2711 event = gatt_connected ? HAL_EV_GATT_CLIENT_CONNECTED :
2712 HAL_EV_GATT_CLIENT_DISCONNECTED;
2714 _bt_hal_convert_addr_string_to_type(bd_addr.address, address);
2715 /* find the gatt client info */
2716 gattc_client = __bt_find_gatt_client_info(&bd_addr);
2717 if (NULL == gattc_client) {
2718 ERR("Fail to get gatt client info");
2723 memset(&ev, 0, sizeof(ev));
2724 ev.conn_id = gattc_client->conn_id;
2726 ev.client_if = gattc_client->client_if;
2727 memcpy(ev.bdaddr, gattc_client->bd_addr.address,
2728 BT_HAL_ADDRESS_LENGTH_MAX);
2731 ERR("gatt client callback not registered");
2733 DBG("sending gatt client connected status event");
2734 event_cb(event, (void *)&ev, sizeof(ev));
2737 if (!gatt_connected) {
2738 inst_id = gattc_client->inst_id;
2740 /* remove the gatt client info from the client list also*/
2741 hal_gattc_client_info_list = g_slist_remove(hal_gattc_client_info_list, gattc_client);
2742 g_free(gattc_client);
2744 //find the connected server info
2745 conn_info = __bt_find_gatt_conn_info(&bd_addr);
2746 if (NULL == conn_info) {
2747 ERR("Fail to get gatt server info");
2751 if (inst_id != conn_info->inst_id) {
2752 ERR("server instance is different");
2756 //remove gatt conn info from the server list
2757 DBG("remove the server conn_info from list after gatt disconnection");
2758 hal_gattc_server_info_list = g_slist_remove(hal_gattc_server_info_list, conn_info);
2759 __hal_clean_gattc_server_info(conn_info);
2765 static void _bt_hal_send_search_service_result_event(int conn_id, int is_primary,
2766 const char* uuid_str, int inst_id)
2768 struct hal_ev_gatt_client_search_result ev;
2771 ERR("gatt client callback not registered");
2775 DBG("sending gatt client search service result event conn_id[%d]", conn_id);
2777 memset(&ev, 0, sizeof(ev));
2778 ev.conn_id = conn_id;
2779 ev.inst_id = inst_id;
2780 ev.is_primary = is_primary;
2781 _bt_hal_convert_uuid_string_to_type(ev.uuid, uuid_str);
2783 event_cb(HAL_EV_GATT_CLIENT_SEARCH_RESULT, (void *)&ev, sizeof(ev));
2786 static void _bt_hal_send_search_service_complete_event(int conn_id, int status)
2788 struct hal_ev_gatt_client_search_complete ev;
2791 ERR("gatt client callback not registered");
2795 DBG("sending gatt client search service complete event conn_id[%d]", conn_id);
2798 memset(&ev, 0, sizeof(ev));
2799 ev.conn_id = conn_id;
2802 event_cb(HAL_EV_GATT_CLIENT_SEARCH_COMPLETE, (void *)&ev, sizeof(ev));