Skip to content

Commit fcbe3ad

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 the addr field is more properly documented. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent 834d8f9 commit fcbe3ad

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

include/zephyr/bluetooth/bluetooth.h

Lines changed: 40 additions & 2 deletions
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. */
@@ -1722,8 +1747,19 @@ struct bt_le_ext_adv_info {
17221747
/** Currently selected Transmit Power (dBM). */
17231748
int8_t tx_power;
17241749

1725-
/** Current local advertising address used. */
1750+
/**
1751+
* @brief Current local advertising address used.
1752+
*
1753+
* If @ref bt_le_ext_adv_info.ext_adv_state is not BT_LE_EXT_ADV_STATE_STARTED the value
1754+
* of this address may change when bt_le_ext_adv_start() is called.
1755+
*/
17261756
const bt_addr_le_t *addr;
1757+
1758+
/** Extended advertising state */
1759+
enum bt_le_ext_adv_state ext_adv_state;
1760+
1761+
/** Periodic advertising state */
1762+
enum bt_le_per_adv_state per_adv_state;
17271763
};
17281764

17291765
/**
@@ -1732,7 +1768,9 @@ struct bt_le_ext_adv_info {
17321768
* @param adv Advertising set object
17331769
* @param info Advertising set info object
17341770
*
1735-
* @return Zero on success or (negative) error code on failure.
1771+
* @retval 0 Success
1772+
* @retval -EINVAL @p adv or @p info is NULL
1773+
* @retval -ENXIO @p adv is not a created advertising set
17361774
*/
17371775
int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
17381776
struct bt_le_ext_adv_info *info);

subsys/bluetooth/host/adv.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,10 +1592,45 @@ 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;
15971614
info->addr = &adv->random_addr;
15981615

1616+
if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
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+
}
1633+
15991634
return 0;
16001635
}
16011636

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)