Skip to content

Commit

Permalink
Merge pull request #16860 from haukepetersen/add_nimble_netifextadv
Browse files Browse the repository at this point in the history
nimble/netif: add support for BT5 PHY modes
  • Loading branch information
fjmolinas authored Feb 4, 2022
2 parents 14f22c1 + f57e813 commit dbae3ed
Show file tree
Hide file tree
Showing 33 changed files with 1,214 additions and 379 deletions.
3 changes: 3 additions & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ PSEUDOMODULES += netstats_rpl
PSEUDOMODULES += nimble
PSEUDOMODULES += nimble_adv_ext
PSEUDOMODULES += nimble_autoconn_%
PSEUDOMODULES += nimble_netif_ext
PSEUDOMODULES += nimble_phy_coded
PSEUDOMODULES += nimble_phy_2mbit
PSEUDOMODULES += nimble_rpble_ext
PSEUDOMODULES += nimble_statconn_ext
PSEUDOMODULES += newlib
PSEUDOMODULES += newlib_gnu_source
PSEUDOMODULES += newlib_nano
Expand Down
2 changes: 1 addition & 1 deletion pkg/nimble/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ else
CFLAGS += -Wno-unused-but-set-variable
endif

IGNORE := nimble_autoconn_% nimble_phy_% nimble_adv_ext
IGNORE := nimble_autoconn_% nimble_phy_% nimble_%_ext
SUBMODS := $(filter-out $(IGNORE),$(filter nimble_%,$(USEMODULE)))

.PHONY: all
Expand Down
32 changes: 28 additions & 4 deletions pkg/nimble/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ USEMODULE += nimble_riot_contrib
# RIOT port
USEMODULE += nimble_porting_nimble

# Pull in dependencies based on used features
ifneq (,$(filter ble_phy_coded,$(FEATURES_USED)))
USEMODULE += nimble_phy_coded
endif

ifneq (,$(filter ble_phy_2mbit,$(FEATURES_USED)))
USEMODULE += nimble_phy_2mbit
endif

# NOTE: this dependency depends on inclusion order, for it to work properly
# mynewt-core should be selected as nimble backend as early as possible,
# i.e. at the application level.
Expand Down Expand Up @@ -56,6 +65,10 @@ ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
USEMODULE += bluetil_ad
endif

ifneq (,$(filter nimble_autoconn_ext,$(USEMODULE)))
USEMODULE += nimble_netif_ext
endif

ifneq (,$(filter nimble_autoconn_%,$(USEMODULE)))
USEMODULE += nimble_autoconn
endif
Expand All @@ -67,10 +80,6 @@ ifneq (,$(filter nimble_autoconn,$(USEMODULE)))
USEMODULE += bluetil_ad
endif

ifneq (,$(filter nimble_phy_%,$(USEMODULE)))
USEMODULE += nimble_adv_ext
endif

ifneq (,$(filter nimble_phy_2mbit,$(USEMODULE)))
FEATURES_REQUIRED += ble_phy_2mbit
endif
Expand All @@ -79,6 +88,11 @@ ifneq (,$(filter nimble_phy_coded,$(USEMODULE)))
FEATURES_REQUIRED += ble_phy_coded
endif

ifneq (,$(filter nimble_rpble_ext,$(USEMODULE)))
USEMODULE += nimble_rpble
USEMODULE += nimble_netif_ext
endif

ifneq (,$(filter nimble_rpble,$(USEMODULE)))
USEMODULE += gnrc_rpl
USEMODULE += nimble_netif
Expand All @@ -92,12 +106,22 @@ ifneq (,$(filter nimble_scanlist,$(USEMODULE)))
USEMODULE += ztimer_usec
endif

ifneq (,$(filter nimble_statconn_ext,$(USEMODULE)))
USEMODULE += nimble_statconn
USEMODULE += nimble_netif_ext
endif

ifneq (,$(filter nimble_statconn,$(USEMODULE)))
USEMODULE += random
USEMODULE += nimble_netif
USEMODULE += nimble_addr
endif

ifneq (,$(filter nimble_netif_ext,$(USEMODULE)))
USEMODULE += nimble_netif
USEMODULE += nimble_adv_ext
endif

ifneq (,$(filter nimble_netif,$(USEMODULE)))
FEATURES_REQUIRED += ble_nimble_netif
USEMODULE += random
Expand Down
20 changes: 8 additions & 12 deletions pkg/nimble/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ ifneq (,$(filter nimble_controller,$(USEMODULE)))
ifneq (,$(filter nimble_drivers_nrf5x,$(USEMODULE)))
INCLUDES += $(NIMIBASE)/nimble/drivers/$(CPU_FAM)/include
endif

# Enable additional PHY modes if requested by the build
ifneq (,$(filter ble_phy_2mbit,$(FEATURES_USED)))
CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_LE_2M_PHY=1
endif
ifneq (,$(filter ble_phy_coded,$(FEATURES_USED)))
CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CODED_PHY=1
endif
endif

# include nimble host headers
Expand Down Expand Up @@ -94,18 +102,6 @@ ifneq (,$(filter nimble_adv_ext,$(USEMODULE)))
endif
endif

ifneq (,$(filter nimble_phy_2mbit,$(USEMODULE)))
ifneq (,$(filter nimble_controller,$(USEMODULE)))
CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_LE_2M_PHY=1
endif
endif

ifneq (,$(filter nimble_phy_coded,$(USEMODULE)))
ifneq (,$(filter nimble_controller,$(USEMODULE)))
CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CODED_PHY=1
endif
endif

ifneq (,$(filter nimble_netif,$(USEMODULE)))
INCLUDES += -I$(RIOTPKG)/nimble/netif/include

Expand Down
24 changes: 13 additions & 11 deletions pkg/nimble/autoconn/include/nimble_autoconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,31 @@ enum {
*/
typedef struct {
/** amount of time spend in scanning mode [in ms] */
uint32_t period_scan;
uint32_t period_scan_ms;
/** amount of time spend in advertising mode [in ms] */
uint32_t period_adv;
uint32_t period_adv_ms;
/** a random value from 0 to this value is added to the duration of each
* scanning and advertising period [in ms] */
uint32_t period_jitter;
uint32_t period_jitter_ms;
/** advertising interval used when in advertising mode [in ms] */
uint32_t adv_itvl;
uint32_t adv_itvl_ms;
/** scan interval applied while in scanning state [in ms] */
uint32_t scan_itvl;
uint32_t scan_itvl_ms;
/** scan window applied while in scanning state [in ms] */
uint32_t scan_win;
uint32_t scan_win_ms;
/** opening a new connection is aborted after this time [in ms] */
uint32_t conn_timeout;
uint32_t conn_timeout_ms;
/** connection interval used when opening a new connection, lower bound.
* [in ms] */
uint32_t conn_itvl_min;
uint32_t conn_itvl_min_ms;
/** connection interval, upper bound [in ms] */
uint32_t conn_itvl_max;
/** slave latency used for new connections [in ms] */
uint32_t conn_itvl_max_ms;
/** slave latency used for new connections */
uint16_t conn_latency;
/** supervision timeout used for new connections [in ms] */
uint32_t conn_super_to;
uint32_t conn_super_to_ms;
/** BLE PHY mode to use */
nimble_phy_t phy_mode;
/** node ID included in the advertising data, may be NULL */
const char *node_id;
} nimble_autoconn_params_t;
Expand Down
32 changes: 20 additions & 12 deletions pkg/nimble/autoconn/include/nimble_autoconn_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,32 @@ extern "C" {
#define NIMBLE_AUTOCONN_CONN_SVTO_MS (2500U) /* 2.5s */
#endif

/**
* @brief Default BLE PHY mode used by autoconn
*/
#ifndef NIMBLE_AUTOCONN_PHY_MODE
#define NIMBLE_AUTOCONN_PHY_MODE NIMBLE_PHY_1M
#endif

#ifndef NIMBLE_AUTOCONN_NODE_ID
#define NIMBLE_AUTOCONN_NODE_ID "RIOT-autoconn"
#endif

#ifndef NIMBLE_AUTOCONN_PARAMS
#define NIMBLE_AUTOCONN_PARAMS \
{ .period_scan = NIMBLE_AUTOCONN_PERIOD_SCAN_MS, \
.period_adv = NIMBLE_AUTOCONN_PERIOD_ADV_MS, \
.period_jitter = NIMBLE_AUTOCONN_PERIOD_JITTER_MS, \
.adv_itvl = NIMBLE_AUTOCONN_ADV_ITVL_MS, \
.scan_itvl = NIMBLE_AUTOCONN_SCAN_ITVL_MS, \
.scan_win = NIMBLE_AUTOCONN_SCAN_WIN_MS, \
.conn_timeout = NIMBLE_AUTOCONN_CONN_TIMEOUT_MS, \
.conn_itvl_min = NIMBLE_AUTOCONN_CONN_ITVL_MIN_MS, \
.conn_itvl_max = NIMBLE_AUTOCONN_CONN_ITVL_MAX_MS, \
.conn_latency = NIMBLE_AUTOCONN_CONN_LATENCY, \
.conn_super_to = NIMBLE_AUTOCONN_CONN_SVTO_MS, \
.node_id = NIMBLE_AUTOCONN_NODE_ID, }
{ .period_scan_ms = NIMBLE_AUTOCONN_PERIOD_SCAN_MS, \
.period_adv_ms = NIMBLE_AUTOCONN_PERIOD_ADV_MS, \
.period_jitter_ms = NIMBLE_AUTOCONN_PERIOD_JITTER_MS, \
.adv_itvl_ms = NIMBLE_AUTOCONN_ADV_ITVL_MS, \
.scan_itvl_ms = NIMBLE_AUTOCONN_SCAN_ITVL_MS, \
.scan_win_ms = NIMBLE_AUTOCONN_SCAN_WIN_MS, \
.conn_timeout_ms = NIMBLE_AUTOCONN_CONN_TIMEOUT_MS, \
.conn_itvl_min_ms = NIMBLE_AUTOCONN_CONN_ITVL_MIN_MS, \
.conn_itvl_max_ms = NIMBLE_AUTOCONN_CONN_ITVL_MAX_MS, \
.conn_latency = NIMBLE_AUTOCONN_CONN_LATENCY, \
.conn_super_to_ms = NIMBLE_AUTOCONN_CONN_SVTO_MS, \
.phy_mode = NIMBLE_AUTOCONN_PHY_MODE, \
.node_id = NIMBLE_AUTOCONN_NODE_ID, }
#endif
/**@}*/

Expand Down
93 changes: 59 additions & 34 deletions pkg/nimble/autoconn/nimble_autoconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ static volatile uint8_t _enabled = 0;
static bluetil_ad_t _ad;
static uint8_t _ad_buf[BLE_HS_ADV_MAX_SZ];

static struct ble_gap_adv_params _adv_params;
static struct ble_gap_conn_params _conn_params;
static uint32_t _conn_timeout;
static nimble_netif_accept_cfg_t _accept_params;
static nimble_netif_connect_cfg_t _conn_params;

static struct ble_npl_callout _state_evt;
static ble_npl_time_t _timeout_adv_period;
Expand All @@ -77,7 +76,7 @@ static void _on_state_change(struct ble_npl_event *ev)
/* stop scanning */
nimble_scanner_stop();
/* start advertising/accepting */
int res = nimble_netif_accept(_ad.buf, _ad.pos, &_adv_params);
int res = nimble_netif_accept(_ad.buf, _ad.pos, &_accept_params);
assert((res == 0) || (res == -ENOMEM));
(void)res;

Expand Down Expand Up @@ -137,11 +136,18 @@ static void _on_scan_evt(uint8_t type, const ble_addr_t *addr,
{
(void)info;

#if IS_USED(MODULE_NIMBLE_AUTOCONN_EXT)
if ((type != (NIMBLE_SCANNER_EXT_ADV | BLE_HCI_ADV_CONN_MASK)) ||
(info->status != BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE)) {
return;
}
#else
/* we are only interested in ADV_IND packets, the rest can be dropped right
* away */
if (type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND) {
return;
}
#endif

bluetil_ad_t ad = {
.buf = (uint8_t *)ad_buf,
Expand All @@ -157,7 +163,7 @@ static void _on_scan_evt(uint8_t type, const ble_addr_t *addr,
nimble_scanner_stop();
DEBUG("[autoconn] SCAN success, initiating connection\n");
_state = STATE_CONN;
int res = nimble_netif_connect(addr, &_conn_params, _conn_timeout);
int res = nimble_netif_connect(addr, &_conn_params);
assert(res >= 0);
(void)res;
}
Expand Down Expand Up @@ -271,47 +277,66 @@ int nimble_autoconn_update(const nimble_autoconn_params_t *params,
}

/* scan and advertising period configuration */
ble_npl_time_ms_to_ticks(params->period_adv, &_timeout_adv_period);
ble_npl_time_ms_to_ticks(params->period_scan, &_timeout_scan_period);
ble_npl_time_ms_to_ticks(params->period_jitter, &_period_jitter);
ble_npl_time_ms_to_ticks(params->period_adv_ms, &_timeout_adv_period);
ble_npl_time_ms_to_ticks(params->period_scan_ms, &_timeout_scan_period);
ble_npl_time_ms_to_ticks(params->period_jitter_ms, &_period_jitter);

/* populate the connection parameters */
_conn_params.scan_itvl = BLE_GAP_SCAN_ITVL_MS(params->scan_win);
_conn_params.scan_window = _conn_params.scan_itvl;
_conn_params.itvl_min = BLE_GAP_CONN_ITVL_MS(params->conn_itvl_min);
_conn_params.itvl_max = BLE_GAP_CONN_ITVL_MS(params->conn_itvl_max);
_conn_params.latency = 0;
_conn_params.supervision_timeout = BLE_GAP_SUPERVISION_TIMEOUT_MS(
params->conn_super_to);
_conn_params.min_ce_len = 0;
_conn_params.max_ce_len = 0;
_conn_timeout = params->conn_timeout;
memset(&_conn_params, 0, sizeof(_conn_params));
_conn_params.scan_itvl_ms = params->scan_itvl_ms;
_conn_params.scan_window_ms = params->scan_win_ms;
_conn_params.conn_itvl_min_ms = params->conn_itvl_min_ms;
_conn_params.conn_itvl_max_ms = params->conn_itvl_max_ms;
_conn_params.conn_supervision_timeout_ms = params->conn_super_to_ms;
_conn_params.conn_slave_latency = params->conn_latency;
_conn_params.timeout_ms = params->conn_timeout_ms;
#if IS_USED(MODULE_NIMBLE_AUTOCONN_EXT)
_conn_params.phy_mode = params->phy_mode;
#else
_conn_params.phy_mode = NIMBLE_PHY_1M;
#endif
_conn_params.own_addr_type = nimble_riot_own_addr_type;

/* we use the same values to updated existing connections */
struct ble_gap_upd_params conn_update_params;
conn_update_params.itvl_min = _conn_params.itvl_min;
conn_update_params.itvl_max = _conn_params.itvl_max;
conn_update_params.latency = _conn_params.latency;
conn_update_params.supervision_timeout = _conn_params.supervision_timeout;
conn_update_params.itvl_min = BLE_GAP_CONN_ITVL_MS(params->conn_itvl_min_ms);
conn_update_params.itvl_max = BLE_GAP_CONN_ITVL_MS(params->conn_itvl_max_ms);
conn_update_params.latency = params->conn_latency;
conn_update_params.supervision_timeout =
BLE_GAP_SUPERVISION_TIMEOUT_MS(params->conn_super_to_ms);
conn_update_params.min_ce_len = 0;
conn_update_params.max_ce_len = 0;

/* calculate the used scan parameters */
nimble_scanner_cfg_t scan_params;
scan_params.itvl_ms = params->scan_itvl;
scan_params.win_ms = params->scan_win;
scan_params.flags = NIMBLE_SCANNER_PASSIVE
| NIMBLE_SCANNER_FILTER_DUPS
| NIMBLE_SCANNER_PHY_1M;
scan_params.itvl_ms = params->scan_itvl_ms;
scan_params.win_ms = params->scan_win_ms;
scan_params.flags = (NIMBLE_SCANNER_PASSIVE | NIMBLE_SCANNER_FILTER_DUPS);
#if IS_USED(MODULE_NIMBLE_AUTOCONN_EXT) && IS_USED(MODULE_NIMBLE_PHY_CODED)
if (params->phy_mode == NIMBLE_PHY_CODED) {
scan_params.flags |= NIMBLE_SCANNER_PHY_CODED;
}
else {
scan_params.flags |= NIMBLE_SCANNER_PHY_1M;
}
#else
scan_params.flags |= NIMBLE_SCANNER_PHY_1M;
#endif

/* set the advertising parameters used */
_adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
_adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
_adv_params.itvl_min = BLE_GAP_ADV_ITVL_MS(params->adv_itvl);
_adv_params.itvl_max = _adv_params.itvl_min;
_adv_params.channel_map = 0;
_adv_params.filter_policy = 0;
_adv_params.high_duty_cycle = 0;
memset(&_accept_params, 0, sizeof(_accept_params));
#if IS_USED(MODULE_NIMBLE_AUTOCONN_EXT)
_accept_params.flags = 0;
_accept_params.primary_phy = params->phy_mode;
_accept_params.secondary_phy = params->phy_mode;
#else
_accept_params.flags = NIMBLE_NETIF_FLAG_LEGACY;
_accept_params.primary_phy = NIMBLE_PHY_1M;
_accept_params.secondary_phy = NIMBLE_PHY_1M;
#endif
_accept_params.adv_itvl_ms = params->adv_itvl_ms;
_accept_params.timeout_ms = BLE_HS_FOREVER;
_accept_params.own_addr_type = nimble_riot_own_addr_type;

/* initialize the advertising data that will be used */
if (adlen > 0) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/nimble/contrib/include/nimble_riot.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ extern "C" {
#define NIMBLE_HOST_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
#endif

/**
* @brief BLE PHY modes
*/
typedef enum {
NIMBLE_PHY_INVALID, /**< PHY mode invalid */
NIMBLE_PHY_1M, /**< legacy 1Mbit PHY mode (always supported) */
#if IS_USED(MODULE_NIMBLE_PHY_2MBIT)
NIMBLE_PHY_2M, /**< 2Mbit PHY mode */
#endif
#if IS_USED(MODULE_NIMBLE_PHY_CODED)
NIMBLE_PHY_CODED, /**< Coded (long range) PHY mode */
#endif
} nimble_phy_t;

/**
* @brief Export our own address type for later usage
*/
Expand Down
Loading

0 comments on commit dbae3ed

Please sign in to comment.