From ea5ea73fd1875b463f3ff697f55dc588225d9c38 Mon Sep 17 00:00:00 2001 From: Ayush Garg Date: Thu, 29 Sep 2022 12:23:42 +0530 Subject: [PATCH] fhub: add bt_adapter_get_hci_address API Change-Id: I4b5d35b48e53e031a2f6fc0bfd4e7b4794bb8c97 Signed-off-by: shss-choi Signed-off-by: Ayush Garg --- include/bluetooth_internal.h | 29 +++++++++++++++++++++++++++++ src/bluetooth-adapter.c | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/test/bt_unit_test.c | 11 +++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/bluetooth_internal.h b/include/bluetooth_internal.h index cd3e823..de47431 100644 --- a/include/bluetooth_internal.h +++ b/include/bluetooth_internal.h @@ -5447,6 +5447,35 @@ int bt_mesh_model_get_publication(bt_mesh_model_h model, */ +/** + * @addtogroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE + * @{ + */ + +/** + * @ingroup CAPI_NETWORK_BLUETOOTH_ADAPTER_MODULE + * @brief Gets the address from hci when adapter is disabled + * @since_tizen 7.0 + * @privlevel platform + * @privilege %http://tizen.org/privilege/bluetooth.admin + * @remarks The @a address must be released with free() by you. + * + * @param[out] address The device address of local Bluetooth adapter + * + * @return 0 on success, otherwise a negative error value. + * @retval #BT_ERROR_NONE Successful + * @retval #BT_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #BT_ERROR_OUT_OF_MEMORY Out of memory + * @retval #BT_ERROR_OPERATION_FAILED Operation failed + * @retval #BT_ERROR_NOT_SUPPORTED Not supported + */ +int bt_adapter_get_hci_address(char **address); + +/** + * @} + */ + + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/bluetooth-adapter.c b/src/bluetooth-adapter.c index b076e28..82f6188 100644 --- a/src/bluetooth-adapter.c +++ b/src/bluetooth-adapter.c @@ -41,6 +41,9 @@ BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_LE_5_0); \ } +#define BT_MAC_ADD_LENGTH 17 +#define BT_MAC_ADD_PATH "/sys/class/bluetooth/hci0/address" + static bt_scan_filter_h pxp_linkloss_alert_filter; static bt_scan_filter_h pxp_immediate_alert_filter; @@ -230,6 +233,46 @@ int bt_adapter_get_address(char **address) return BT_ERROR_NONE; } +int bt_adapter_get_hci_address(char **address) +{ + BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); + BT_CHECK_INPUT_PARAMETER(address); + + if (!TIZEN_FEATURE_FHUB_REFERENCE) { + BT_INFO("Not a FHUB device"); + return BT_ERROR_NOT_SUPPORTED; + } + + FILE *fp = NULL; + char buf[BT_MAC_ADD_LENGTH +1]; + if (0 == access(BT_MAC_ADD_PATH, F_OK)) + fp = fopen(BT_MAC_ADD_PATH, "r"); + + if (fp == NULL) { + BT_ERR("Failed to open file %s\n", BT_MAC_ADD_PATH); + return BT_ERROR_OPERATION_FAILED; + } + + if (fgets(buf, sizeof(buf), fp) == NULL) { + BT_ERR("Failed to get MAC info from %s\n", BT_MAC_ADD_PATH); + fclose(fp); + return BT_ERROR_OPERATION_FAILED; + } + + BT_DBG("%s : %s", BT_MAC_ADD_PATH, buf); + *address = (char *)g_try_malloc0(BT_MAC_ADD_LENGTH + 1); + if (*address == NULL) { + BT_ERR("malloc() failed"); + fclose(fp); + return BT_ERROR_OUT_OF_MEMORY; + } + + g_strlcpy(*address, buf, BT_MAC_ADD_LENGTH + 1); + BT_DBG("%s", *address); + fclose(fp); + return BT_ERROR_NONE; +} + /* LCOV_EXCL_START */ int bt_adapter_get_version(char **version) { diff --git a/tests/test/bt_unit_test.c b/tests/test/bt_unit_test.c index 27dbe2a..96ad6ab 100644 --- a/tests/test/bt_unit_test.c +++ b/tests/test/bt_unit_test.c @@ -4713,6 +4713,7 @@ int test_input_callback(void *data) char *firmware = NULL; char *stack_version = NULL; char *profiles = NULL; + char *local_bt_mac = NULL; ret = bt_adapter_get_local_info(&chipset, &firmware, &stack_version, &profiles); @@ -4726,6 +4727,16 @@ int test_input_callback(void *data) g_free(stack_version); g_free(profiles); } + + if (TIZEN_FEATURE_FHUB_REFERENCE) { + ret = bt_adapter_get_hci_address(&local_bt_mac); + if (ret < BT_ERROR_NONE) + TC_PRT("get_hci_address : returns %s\n", __bt_get_error_message(ret)); + else { + TC_PRT("Local BT mac: [%s]", local_bt_mac); + g_free(local_bt_mac); + } + } break; } -- 2.7.4