Skip to content

Commit

Permalink
Bluetooth: debug scan callback with legacy advertising
Browse files Browse the repository at this point in the history
Add debug scan callback in legacy advertisements, meant for
manufacturing or debugging usage. Enabled via proper vendor-specific
command and via debug API, allows saving 5.5 kB RAM that would be taken
when using extended advertisements.

Signed-off-by: Giancarlo Stasi <[email protected]>
  • Loading branch information
giansta committed Jan 10, 2024
1 parent a27ae68 commit 48f3540
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 13 deletions.
16 changes: 16 additions & 0 deletions include/zephyr/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,22 @@ int bt_le_adv_start(const struct bt_le_adv_param *param,
const struct bt_data *ad, size_t ad_len,
const struct bt_data *sd, size_t sd_len);

#if !defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
/**
* @brief Add legacy advertising debug callbacks
*
* Set callback structure to legacy advertisement . This feature is meant for
* manufacturing, for a possible method for updating firmware, and for debugging.
*
* @param cb Callback struct to notify about advertiser activity. Can be NULL.
*
* @return Zero on success or (negative) error code otherwise.
* @return -ENOMEM No free connection objects available for connectable
* advertiser.
*/
int bt_le_adv_debug_cb(const struct bt_le_ext_adv_cb *cb);
#endif /* !defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) */

/**
* @brief Update advertising
*
Expand Down
26 changes: 16 additions & 10 deletions include/zephyr/bluetooth/hci_vs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extern "C" {
#define BT_HCI_VS_FW_VAR_VS_CTLR 0x0002
#define BT_HCI_VS_FW_VAR_FW_LOADER 0x0003
#define BT_HCI_VS_FW_VAR_RESCUE_IMG 0x0004
#define BT_HCI_OP_VS_READ_VERSION_INFO BT_OP(BT_OGF_VS, 0x0001)
#define BT_HCI_OP_VS_READ_VERSION_INFO BT_OP(BT_OGF_VS, 0x0001)
struct bt_hci_rp_vs_read_version_info {
uint8_t status;
uint16_t hw_platform;
Expand All @@ -63,13 +63,13 @@ struct bt_hci_rp_vs_read_version_info {
uint32_t fw_build;
} __packed;

#define BT_HCI_OP_VS_READ_SUPPORTED_COMMANDS BT_OP(BT_OGF_VS, 0x0002)
#define BT_HCI_OP_VS_READ_SUPPORTED_COMMANDS BT_OP(BT_OGF_VS, 0x0002)
struct bt_hci_rp_vs_read_supported_commands {
uint8_t status;
uint8_t commands[64];
} __packed;

#define BT_HCI_OP_VS_READ_SUPPORTED_FEATURES BT_OP(BT_OGF_VS, 0x0003)
#define BT_HCI_OP_VS_READ_SUPPORTED_FEATURES BT_OP(BT_OGF_VS, 0x0003)
struct bt_hci_rp_vs_read_supported_features {
uint8_t status;
uint8_t features[8];
Expand Down Expand Up @@ -155,9 +155,9 @@ struct bt_hci_cp_vs_set_scan_req_reports {
uint8_t enable;
} __packed;

#define BT_HCI_VS_LL_HANDLE_TYPE_ADV 0x00
#define BT_HCI_VS_LL_HANDLE_TYPE_SCAN 0x01
#define BT_HCI_VS_LL_HANDLE_TYPE_CONN 0x02
#define BT_HCI_VS_LL_HANDLE_TYPE_ADV 0x00
#define BT_HCI_VS_LL_HANDLE_TYPE_SCAN 0x01
#define BT_HCI_VS_LL_HANDLE_TYPE_CONN 0x02
#define BT_HCI_VS_LL_TX_POWER_LEVEL_NO_PREF 0x7F
#define BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL BT_OP(BT_OGF_VS, 0x000e)
struct bt_hci_cp_vs_write_tx_power_level {
Expand Down Expand Up @@ -194,23 +194,29 @@ struct bt_hci_rp_vs_read_usb_transport_mode {
uint8_t supported_mode[0];
} __packed;

#define BT_HCI_VS_USB_H2_MODE 0x00
#define BT_HCI_VS_USB_H4_MODE 0x01
#define BT_HCI_VS_USB_H2_MODE 0x00
#define BT_HCI_VS_USB_H4_MODE 0x01

#define BT_HCI_OP_VS_SET_USB_TRANSPORT_MODE BT_OP(BT_OGF_VS, 0x0011)
#define BT_HCI_OP_VS_SET_USB_TRANSPORT_MODE BT_OP(BT_OGF_VS, 0x0011)

struct bt_hci_cp_vs_set_usb_transport_mode {
uint8_t mode;
} __packed;

#define BT_HCI_OP_VS_SET_MIN_NUM_USED_CHANS BT_OP(BT_OGF_VS, 0x0012)
#define BT_HCI_OP_VS_SET_MIN_NUM_USED_CHANS BT_OP(BT_OGF_VS, 0x0012)

struct bt_hci_cp_vs_set_min_num_used_chans {
uint16_t handle;
uint8_t phys;
uint8_t min_used_chans;
} __packed;

#define BT_HCI_OP_VS_SET_SCAN_REQ_RCVD_MASK BT_OP(BT_OGF_VS, 0x0013)

struct bt_hci_cp_vs_set_scan_req_rcvd_mask {
uint8_t enable;
} __packed;

/* Events */

struct bt_hci_evt_vs {
Expand Down
20 changes: 20 additions & 0 deletions subsys/bluetooth/controller/hci/hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -4951,6 +4951,20 @@ static void vs_read_tx_power_level(struct net_buf *buf, struct net_buf **evt)
}
#endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */

#if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
static void vs_set_scan_req_rcvd_mask(struct net_buf *buf, struct net_buf **evt)
{
struct bt_hci_cp_vs_set_scan_req_rcvd_mask *cmd = (void *)buf->data;

if (cmd->enable != 0) {
le_event_mask |= BT_EVT_MASK_LE_SCAN_REQ_RECEIVED;
} else {
le_event_mask &= ~BT_EVT_MASK_LE_SCAN_REQ_RECEIVED;
}
*evt = cmd_complete_status(0x00);
}
#endif /* CONFIG_BT_CTLR_SCAN_REQ_NOTIFY */

#if defined(CONFIG_BT_HCI_VS_FATAL_ERROR)
/* A memory pool for vandor specific events for fatal error reporting purposes. */
NET_BUF_POOL_FIXED_DEFINE(vs_err_tx_pool, 1, BT_BUF_EVT_RX_SIZE,
Expand Down Expand Up @@ -5493,6 +5507,12 @@ int hci_vendor_cmd_handle_common(uint16_t ocf, struct net_buf *cmd,
break;
#endif /* CONFIG_BT_HCI_MESH_EXT */

#if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
case BT_OCF(BT_HCI_OP_VS_SET_SCAN_REQ_RCVD_MASK):
vs_set_scan_req_rcvd_mask(cmd, evt);
break;
#endif /* CONFIG_BT_CTLR_SCAN_REQ_NOTIFY */

default:
return -EINVAL;
}
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/controller/ll_sw/ull_adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ uint8_t ll_adv_params_set(uint16_t interval, uint8_t adv_type,
adv->lll.chan_map = chan_map;
adv->lll.filter_policy = filter_policy;

#if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
#if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) && defined(CONFIG_BT_CTLR_ADV_EXT)
adv->lll.scan_req_notify = sreq;
#endif

Expand Down
25 changes: 24 additions & 1 deletion subsys/bluetooth/host/adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,21 @@ int bt_le_adv_start(const struct bt_le_adv_param *param,
return err;
}

#if !defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
int bt_le_adv_debug_cb(const struct bt_le_ext_adv_cb *cb)
{
struct bt_le_ext_adv *adv = adv_get_legacy();

if (!adv) {
return -ENOMEM;
}

adv->cb = cb;

return 0;
}
#endif /* !defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) */

int bt_le_adv_stop(void)
{
struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
Expand Down Expand Up @@ -2250,14 +2265,22 @@ void bt_hci_le_adv_set_terminated(struct net_buf *buf)
}
}
}
#endif /* defined(CONFIG_BT_BROADCASTER) */
#endif /* defined(CONFIG_BT_EXT_ADV) */

#if defined(CONFIG_BT_EXT_ADV) || defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
#if defined(CONFIG_BT_BROADCASTER)
void bt_hci_le_scan_req_received(struct net_buf *buf)
{
struct bt_hci_evt_le_scan_req_received *evt;
struct bt_le_ext_adv *adv;

evt = (void *)buf->data;
#if defined(CONFIG_BT_EXT_ADV)
adv = bt_adv_lookup_handle(evt->handle);
#else
adv = adv_get_legacy();
#endif

LOG_DBG("handle %u peer %s", evt->handle, bt_addr_le_str(&evt->addr));

Expand All @@ -2282,4 +2305,4 @@ void bt_hci_le_scan_req_received(struct net_buf *buf)
}
}
#endif /* defined(CONFIG_BT_BROADCASTER) */
#endif /* defined(CONFIG_BT_EXT_ADV) */
#endif /* defined(CONFIG_BT_EXT_ADV) || defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) */
6 changes: 6 additions & 0 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,12 @@ static const struct event_handler meta_events[] = {
#endif /* CONFIG_BT_CONN */
#endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
#endif /* defined(CONFIG_BT_EXT_ADV) */
#if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
#if defined(CONFIG_BT_BROADCASTER)
EVENT_HANDLER(BT_HCI_EVT_LE_SCAN_REQ_RECEIVED, bt_hci_le_scan_req_received,
sizeof(struct bt_hci_evt_le_scan_req_received)),
#endif
#endif /* defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) */
#if defined(CONFIG_BT_ISO_UNICAST)
EVENT_HANDLER(BT_HCI_EVT_LE_CIS_ESTABLISHED, hci_le_cis_established,
sizeof(struct bt_hci_evt_le_cis_established)),
Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/host/hci_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct bt_le_ext_adv {

ATOMIC_DEFINE(flags, BT_ADV_NUM_FLAGS);

#if defined(CONFIG_BT_EXT_ADV)
#if defined(CONFIG_BT_EXT_ADV) || defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
const struct bt_le_ext_adv_cb *cb;

/* TX Power in use by the controller */
Expand Down

0 comments on commit 48f3540

Please sign in to comment.