Skip to content

Commit a91135e

Browse files
committed
Bluetooth: Host: Add advertising state to bt_le_ext_adv_info
The bt_le_ext_adv_info struct has been extended to also contain the advertising and periodic advertising states. Additionally, the function verifies the input to avoid NULL pointer access, and more importantly does not return 0 with `addr` set to a valid that is not used for advertising. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent b78d1a1 commit a91135e

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

include/zephyr/bluetooth/bluetooth.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,31 @@ int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv);
17141714
*/
17151715
uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv);
17161716

1717+
enum bt_le_ext_adv_state {
1718+
/** No state */
1719+
BT_LE_EXT_ADV_STATE_NONE,
1720+
1721+
/** The advertising set has been created but not started */
1722+
BT_LE_EXT_ADV_STATE_CREATED,
1723+
1724+
/** The advertising set is started */
1725+
BT_LE_EXT_ADV_STATE_STARTED,
1726+
1727+
/** Ther advertising set is temporarily paused */
1728+
BT_LE_EXT_ADV_STATE_PAUSED,
1729+
};
1730+
1731+
enum bt_le_per_adv_state {
1732+
/** No state */
1733+
BT_LE_PER_ADV_STATE_NONE,
1734+
1735+
/** The advertising set has been configured for periodic advertising */
1736+
BT_LE_PER_ADV_STATE_CONFIGURED,
1737+
1738+
/** Periodic advertising is started */
1739+
BT_LE_PER_ADV_STATE_STARTED,
1740+
};
1741+
17171742
/** @brief Advertising set info structure. */
17181743
struct bt_le_ext_adv_info {
17191744
/** Local identity handle. */
@@ -1724,6 +1749,12 @@ struct bt_le_ext_adv_info {
17241749

17251750
/** Current local advertising address used. */
17261751
const bt_addr_le_t *addr;
1752+
1753+
/** Extended advertising state */
1754+
enum bt_le_ext_adv_state ext_adv_state;
1755+
1756+
/** Periodic advertising state */
1757+
enum bt_le_per_adv_state per_adv_state;
17271758
};
17281759

17291760
/**
@@ -1732,7 +1763,9 @@ struct bt_le_ext_adv_info {
17321763
* @param adv Advertising set object
17331764
* @param info Advertising set info object
17341765
*
1735-
* @return Zero on success or (negative) error code on failure.
1766+
* @retval 0 Success
1767+
* @retval -EINVAL @p adv or @p info is NULL
1768+
* @retval -ENXIO @p adv is not a created advertising set
17361769
*/
17371770
int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
17381771
struct bt_le_ext_adv_info *info);

subsys/bluetooth/host/adv.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,9 +1592,44 @@ void bt_le_adv_resume(void)
15921592
int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
15931593
struct bt_le_ext_adv_info *info)
15941594
{
1595+
if (adv == NULL) {
1596+
LOG_DBG("adv is NULL");
1597+
return -EINVAL;
1598+
}
1599+
1600+
if (info == NULL) {
1601+
LOG_DBG("info is NULL");
1602+
return -EINVAL;
1603+
}
1604+
1605+
if (!atomic_test_bit(adv->flags, BT_ADV_CREATED)) {
1606+
LOG_DBG("Advertising set %p is not created", adv);
1607+
return -ENXIO;
1608+
}
1609+
1610+
(void)memset(info, 0, sizeof(*info));
1611+
15951612
info->id = adv->id;
15961613
info->tx_power = adv->tx_power;
1597-
info->addr = &adv->random_addr;
1614+
1615+
if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1616+
info->addr = &adv->random_addr;
1617+
info->ext_adv_state = BT_LE_EXT_ADV_STATE_STARTED;
1618+
} else if (atomic_test_bit(adv->flags, BT_ADV_PAUSED)) {
1619+
info->ext_adv_state = BT_LE_EXT_ADV_STATE_PAUSED;
1620+
} else {
1621+
info->ext_adv_state = BT_LE_EXT_ADV_STATE_CREATED;
1622+
}
1623+
1624+
if (IS_ENABLED(CONFIG_BT_PER_ADV)) {
1625+
if (atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1626+
info->per_adv_state = BT_LE_PER_ADV_STATE_CONFIGURED;
1627+
} else if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
1628+
info->per_adv_state = BT_LE_PER_ADV_STATE_STARTED;
1629+
} else {
1630+
info->per_adv_state = BT_LE_PER_ADV_STATE_NONE;
1631+
}
1632+
}
15981633

15991634
return 0;
16001635
}

subsys/bluetooth/host/shell/bt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2566,7 +2566,11 @@ static int cmd_adv_info(const struct shell *sh, size_t argc, char *argv[])
25662566

25672567
shell_print(sh, "Advertiser[%d] %p", selected_adv, adv);
25682568
shell_print(sh, "Id: %d, TX power: %d dBm", info.id, info.tx_power);
2569-
print_le_addr("Address", info.addr);
2569+
shell_print(sh, "Adv state: %d", info.ext_adv_state);
2570+
if (info.ext_adv_state == BT_LE_EXT_ADV_STATE_STARTED) {
2571+
print_le_addr("Address", info.addr);
2572+
}
2573+
shell_print(sh, "Per Adv state: %d", info.per_adv_state);
25702574

25712575
return 0;
25722576
}

0 commit comments

Comments
 (0)