Bluetooth: host: Refactor out address handling to new file
Refactor implementation of functions defined in addr.h to its own source file addr.c. Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
parent
e54d2a7740
commit
4978c19c05
5 changed files with 208 additions and 192 deletions
|
@ -11,6 +11,7 @@
|
||||||
#define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
|
#define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/printk.h>
|
||||||
#include <zephyr/types.h>
|
#include <zephyr/types.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#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);
|
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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/printk.h>
|
|
||||||
#include <sys/util.h>
|
#include <sys/util.h>
|
||||||
#include <net/buf.h>
|
#include <net/buf.h>
|
||||||
#include <bluetooth/gap.h>
|
#include <bluetooth/gap.h>
|
||||||
|
@ -1861,106 +1860,6 @@ struct bt_br_oob {
|
||||||
*/
|
*/
|
||||||
int bt_br_oob_get_local(struct bt_br_oob *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.
|
* @brief Enable/disable set controller in discoverable state.
|
||||||
|
|
|
@ -30,6 +30,7 @@ zephyr_library_sources_ifdef(
|
||||||
if(CONFIG_BT_HCI_HOST)
|
if(CONFIG_BT_HCI_HOST)
|
||||||
zephyr_library_sources(
|
zephyr_library_sources(
|
||||||
uuid.c
|
uuid.c
|
||||||
|
addr.c
|
||||||
hci_core.c
|
hci_core.c
|
||||||
hci_common.c
|
hci_common.c
|
||||||
)
|
)
|
||||||
|
|
103
subsys/bluetooth/host/addr.c
Normal file
103
subsys/bluetooth/host/addr.c
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2017 Nordic Semiconductor ASA
|
||||||
|
* Copyright (c) 2015 Intel Corporation
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <sys/util.h>
|
||||||
|
|
||||||
|
#include <bluetooth/addr.h>
|
||||||
|
#include <bluetooth/crypto.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -658,60 +658,6 @@ static int set_adv_random_address(struct bt_le_ext_adv *adv,
|
||||||
adv->random_addr.type = BT_ADDR_LE_RANDOM;
|
adv->random_addr.type = BT_ADDR_LE_RANDOM;
|
||||||
return 0;
|
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)
|
static void adv_rpa_invalidate(struct bt_le_ext_adv *adv, void *data)
|
||||||
{
|
{
|
||||||
if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
|
if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
|
||||||
|
@ -824,7 +770,7 @@ static int le_set_private_addr(uint8_t id)
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
nrpa.val[5] &= 0x3f;
|
BT_ADDR_SET_NRPA(&nrpa);
|
||||||
|
|
||||||
return set_random_address(&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;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
nrpa.val[5] &= 0x3f;
|
BT_ADDR_SET_NRPA(&nrpa);
|
||||||
|
|
||||||
return set_adv_random_address(adv, &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);
|
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)
|
static uint8_t bt_read_public_addr(bt_addr_le_t *addr)
|
||||||
{
|
{
|
||||||
struct bt_hci_rp_read_bd_addr *rp;
|
struct bt_hci_rp_read_bd_addr *rp;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue