diff --git a/include/bluetooth/addr.h b/include/bluetooth/addr.h index 1cc41aeae2c..227c790a461 100644 --- a/include/bluetooth/addr.h +++ b/include/bluetooth/addr.h @@ -11,6 +11,7 @@ #define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ #include +#include #include #ifdef __cplusplus @@ -96,6 +97,107 @@ static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr) return BT_ADDR_IS_STATIC(&addr->a); } +/** + * @def BT_ADDR_STR_LEN + * + * @brief Recommended length of user string buffer for Bluetooth address + * + * @details The recommended length guarantee the output of address + * conversion will not lose valuable information about address being + * processed. + */ +#define BT_ADDR_STR_LEN 18 + +/** + * @def BT_ADDR_LE_STR_LEN + * + * @brief Recommended length of user string buffer for Bluetooth LE address + * + * @details The recommended length guarantee the output of address + * conversion will not lose valuable information about address being + * processed. + */ +#define BT_ADDR_LE_STR_LEN 30 + +/** + * @brief Converts binary Bluetooth address to string. + * + * @param addr Address of buffer containing binary Bluetooth address. + * @param str Address of user buffer with enough room to store formatted + * string containing binary address. + * @param len Length of data to be copied to user string buffer. Refer to + * BT_ADDR_STR_LEN about recommended value. + * + * @return Number of successfully formatted bytes from binary address. + */ +static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len) +{ + return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X", + addr->val[5], addr->val[4], addr->val[3], + addr->val[2], addr->val[1], addr->val[0]); +} + +/** + * @brief Converts binary LE Bluetooth address to string. + * + * @param addr Address of buffer containing binary LE Bluetooth address. + * @param str Address of user buffer with enough room to store + * formatted string containing binary LE address. + * @param len Length of data to be copied to user string buffer. Refer to + * BT_ADDR_LE_STR_LEN about recommended value. + * + * @return Number of successfully formatted bytes from binary address. + */ +static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str, + size_t len) +{ + char type[10]; + + switch (addr->type) { + case BT_ADDR_LE_PUBLIC: + strcpy(type, "public"); + break; + case BT_ADDR_LE_RANDOM: + strcpy(type, "random"); + break; + case BT_ADDR_LE_PUBLIC_ID: + strcpy(type, "public-id"); + break; + case BT_ADDR_LE_RANDOM_ID: + strcpy(type, "random-id"); + break; + default: + snprintk(type, sizeof(type), "0x%02x", addr->type); + break; + } + + return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)", + addr->a.val[5], addr->a.val[4], addr->a.val[3], + addr->a.val[2], addr->a.val[1], addr->a.val[0], type); +} + +/** + * @brief Convert Bluetooth address from string to binary. + * + * @param[in] str The string representation of a Bluetooth address. + * @param[out] addr Address of buffer to store the Bluetooth address + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_addr_from_str(const char *str, bt_addr_t *addr); + +/** + * @brief Convert LE Bluetooth address from string to binary. + * + * @param[in] str The string representation of an LE Bluetooth address. + * @param[in] type The string representation of the LE Bluetooth address + * type. + * @param[out] addr Address of buffer to store the LE Bluetooth address + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr); + /** * @} */ diff --git a/include/bluetooth/bluetooth.h b/include/bluetooth/bluetooth.h index 0d3a77a25e9..8f985f08f74 100644 --- a/include/bluetooth/bluetooth.h +++ b/include/bluetooth/bluetooth.h @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -1861,106 +1860,6 @@ struct bt_br_oob { */ int bt_br_oob_get_local(struct bt_br_oob *oob); -/** - * @def BT_ADDR_STR_LEN - * - * @brief Recommended length of user string buffer for Bluetooth address - * - * @details The recommended length guarantee the output of address - * conversion will not lose valuable information about address being - * processed. - */ -#define BT_ADDR_STR_LEN 18 - -/** - * @def BT_ADDR_LE_STR_LEN - * - * @brief Recommended length of user string buffer for Bluetooth LE address - * - * @details The recommended length guarantee the output of address - * conversion will not lose valuable information about address being - * processed. - */ -#define BT_ADDR_LE_STR_LEN 30 - -/** - * @brief Converts binary Bluetooth address to string. - * - * @param addr Address of buffer containing binary Bluetooth address. - * @param str Address of user buffer with enough room to store formatted - * string containing binary address. - * @param len Length of data to be copied to user string buffer. Refer to - * BT_ADDR_STR_LEN about recommended value. - * - * @return Number of successfully formatted bytes from binary address. - */ -static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len) -{ - return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X", - addr->val[5], addr->val[4], addr->val[3], - addr->val[2], addr->val[1], addr->val[0]); -} - -/** - * @brief Converts binary LE Bluetooth address to string. - * - * @param addr Address of buffer containing binary LE Bluetooth address. - * @param str Address of user buffer with enough room to store - * formatted string containing binary LE address. - * @param len Length of data to be copied to user string buffer. Refer to - * BT_ADDR_LE_STR_LEN about recommended value. - * - * @return Number of successfully formatted bytes from binary address. - */ -static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str, - size_t len) -{ - char type[10]; - - switch (addr->type) { - case BT_ADDR_LE_PUBLIC: - strcpy(type, "public"); - break; - case BT_ADDR_LE_RANDOM: - strcpy(type, "random"); - break; - case BT_ADDR_LE_PUBLIC_ID: - strcpy(type, "public-id"); - break; - case BT_ADDR_LE_RANDOM_ID: - strcpy(type, "random-id"); - break; - default: - snprintk(type, sizeof(type), "0x%02x", addr->type); - break; - } - - return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)", - addr->a.val[5], addr->a.val[4], addr->a.val[3], - addr->a.val[2], addr->a.val[1], addr->a.val[0], type); -} - -/** - * @brief Convert Bluetooth address from string to binary. - * - * @param[in] str The string representation of a Bluetooth address. - * @param[out] addr Address of buffer to store the Bluetooth address - * - * @return Zero on success or (negative) error code otherwise. - */ -int bt_addr_from_str(const char *str, bt_addr_t *addr); - -/** - * @brief Convert LE Bluetooth address from string to binary. - * - * @param[in] str The string representation of an LE Bluetooth address. - * @param[in] type The string representation of the LE Bluetooth address - * type. - * @param[out] addr Address of buffer to store the LE Bluetooth address - * - * @return Zero on success or (negative) error code otherwise. - */ -int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr); /** * @brief Enable/disable set controller in discoverable state. diff --git a/subsys/bluetooth/host/CMakeLists.txt b/subsys/bluetooth/host/CMakeLists.txt index ba44c5c4fee..dd9c3a7beac 100644 --- a/subsys/bluetooth/host/CMakeLists.txt +++ b/subsys/bluetooth/host/CMakeLists.txt @@ -30,6 +30,7 @@ zephyr_library_sources_ifdef( if(CONFIG_BT_HCI_HOST) zephyr_library_sources( uuid.c + addr.c hci_core.c hci_common.c ) diff --git a/subsys/bluetooth/host/addr.c b/subsys/bluetooth/host/addr.c new file mode 100644 index 00000000000..1cecc228bd0 --- /dev/null +++ b/subsys/bluetooth/host/addr.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include + +#include +#include + +static inline int create_random_addr(bt_addr_le_t *addr) +{ + addr->type = BT_ADDR_LE_RANDOM; + + return bt_rand(addr->a.val, 6); +} + +int bt_addr_le_create_nrpa(bt_addr_le_t *addr) +{ + int err; + + err = create_random_addr(addr); + if (err) { + return err; + } + + BT_ADDR_SET_NRPA(&addr->a); + + return 0; +} + +int bt_addr_le_create_static(bt_addr_le_t *addr) +{ + int err; + + err = create_random_addr(addr); + if (err) { + return err; + } + + BT_ADDR_SET_STATIC(&addr->a); + + return 0; +} + +int bt_addr_from_str(const char *str, bt_addr_t *addr) +{ + int i, j; + uint8_t tmp; + + if (strlen(str) != 17U) { + return -EINVAL; + } + + for (i = 5, j = 1; *str != '\0'; str++, j++) { + if (!(j % 3) && (*str != ':')) { + return -EINVAL; + } else if (*str == ':') { + i--; + continue; + } + + addr->val[i] = addr->val[i] << 4; + + if (char2hex(*str, &tmp) < 0) { + return -EINVAL; + } + + addr->val[i] |= tmp; + } + + return 0; +} + +int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr) +{ + int err; + + err = bt_addr_from_str(str, &addr->a); + if (err < 0) { + return err; + } + + if (!strcmp(type, "public") || !strcmp(type, "(public)")) { + addr->type = BT_ADDR_LE_PUBLIC; + } else if (!strcmp(type, "random") || !strcmp(type, "(random)")) { + addr->type = BT_ADDR_LE_RANDOM; + } else if (!strcmp(type, "public-id") || !strcmp(type, "(public-id)")) { + addr->type = BT_ADDR_LE_PUBLIC_ID; + } else if (!strcmp(type, "random-id") || !strcmp(type, "(random-id)")) { + addr->type = BT_ADDR_LE_RANDOM_ID; + } else { + return -EINVAL; + } + + return 0; +} diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 5ec41a79855..1539ed3141c 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -658,60 +658,6 @@ static int set_adv_random_address(struct bt_le_ext_adv *adv, adv->random_addr.type = BT_ADDR_LE_RANDOM; return 0; } - -int bt_addr_from_str(const char *str, bt_addr_t *addr) -{ - int i, j; - uint8_t tmp; - - if (strlen(str) != 17U) { - return -EINVAL; - } - - for (i = 5, j = 1; *str != '\0'; str++, j++) { - if (!(j % 3) && (*str != ':')) { - return -EINVAL; - } else if (*str == ':') { - i--; - continue; - } - - addr->val[i] = addr->val[i] << 4; - - if (char2hex(*str, &tmp) < 0) { - return -EINVAL; - } - - addr->val[i] |= tmp; - } - - return 0; -} - -int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr) -{ - int err; - - err = bt_addr_from_str(str, &addr->a); - if (err < 0) { - return err; - } - - if (!strcmp(type, "public") || !strcmp(type, "(public)")) { - addr->type = BT_ADDR_LE_PUBLIC; - } else if (!strcmp(type, "random") || !strcmp(type, "(random)")) { - addr->type = BT_ADDR_LE_RANDOM; - } else if (!strcmp(type, "public-id") || !strcmp(type, "(public-id)")) { - addr->type = BT_ADDR_LE_PUBLIC_ID; - } else if (!strcmp(type, "random-id") || !strcmp(type, "(random-id)")) { - addr->type = BT_ADDR_LE_RANDOM_ID; - } else { - return -EINVAL; - } - - return 0; -} - static void adv_rpa_invalidate(struct bt_le_ext_adv *adv, void *data) { if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) { @@ -824,7 +770,7 @@ static int le_set_private_addr(uint8_t id) return err; } - nrpa.val[5] &= 0x3f; + BT_ADDR_SET_NRPA(&nrpa); return set_random_address(&nrpa); } @@ -839,7 +785,7 @@ static int le_adv_set_private_addr(struct bt_le_ext_adv *adv) return err; } - nrpa.val[5] &= 0x3f; + BT_ADDR_SET_NRPA(&nrpa); return set_adv_random_address(adv, &nrpa); } @@ -6084,41 +6030,6 @@ static int set_event_mask(void) return bt_hci_cmd_send_sync(BT_HCI_OP_SET_EVENT_MASK, buf, NULL); } -static inline int create_random_addr(bt_addr_le_t *addr) -{ - addr->type = BT_ADDR_LE_RANDOM; - - return bt_rand(addr->a.val, 6); -} - -int bt_addr_le_create_nrpa(bt_addr_le_t *addr) -{ - int err; - - err = create_random_addr(addr); - if (err) { - return err; - } - - BT_ADDR_SET_NRPA(&addr->a); - - return 0; -} - -int bt_addr_le_create_static(bt_addr_le_t *addr) -{ - int err; - - err = create_random_addr(addr); - if (err) { - return err; - } - - BT_ADDR_SET_STATIC(&addr->a); - - return 0; -} - static uint8_t bt_read_public_addr(bt_addr_le_t *addr) { struct bt_hci_rp_read_bd_addr *rp;