mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #17774 from fjmolinas/pr_nimble_auto_adv_extended
pkg/nimble/autoadv: add support for ext_adv
This commit is contained in:
commit
b167d6931c
@ -172,6 +172,8 @@ CLEAN = $(filter clean, $(MAKECMDGOALS))
|
||||
include $(RIOTMAKE)/utils/variables.mk
|
||||
include $(RIOTMAKE)/utils/strings.mk
|
||||
|
||||
# include nimble makefile tools
|
||||
include $(RIOTMAKE)/pkg/nimble.adv.mk
|
||||
|
||||
# UNAME is always needed so use simple variable expansion so only evaluated once
|
||||
UNAME := $(shell uname -m -s)
|
||||
|
9
dist/tools/doccheck/exclude_patterns
vendored
9
dist/tools/doccheck/exclude_patterns
vendored
@ -14947,3 +14947,12 @@ boards/nucleo\-f439zi/include/periph_conf\.h:[0-9]+: warning: Member ETH_DMA_ISR
|
||||
boards/nucleo\-f439zi/include/periph_conf\.h:[0-9]+: warning: Member eth_config \(variable\) of file periph_conf\.h is not documented\.
|
||||
boards/arduino\-mega2560/include/board\.h:[0-9]+: warning: Member CONFIG_ZTIMER_USEC_ADJUST_SET \(macro definition\) of file board\.h is not documented\.
|
||||
boards/arduino\-mega2560/include/board\.h:[0-9]+: warning: Member CONFIG_ZTIMER_USEC_ADJUST_SLEEP \(macro definition\) of file board\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_ADV_ITVL_MS \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_ADV_DURATION_MS \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_FLAGS \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_PHY \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_TX_POWER \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_CHANNEL_MAP \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_OWN_ADDR_TYPE \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_FILTER_POLICY \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
pkg/nimble/autoadv/include/nimble_autoadv_params\.h:[0-9]+: warning: Member NIMBLE_AUTOADV_PARAMS \(macro definition\) of file nimble_autoadv_params\.h is not documented\.
|
||||
|
@ -14,8 +14,8 @@ USEMODULE += nimble_svc_gatt
|
||||
|
||||
# Use automated advertising
|
||||
USEMODULE += nimble_autoadv
|
||||
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"NimBLE GATT Example"'
|
||||
CFLAGS += -DNIMBLE_AUTOADV_START_MANUALLY=1
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_DEVICE_NAME='"NimBLE GATT Example"'
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_START_MANUALLY=1
|
||||
|
||||
# Comment this out to disable code in RIOT that does safety checking
|
||||
# which is not needed in a production environment but helps in the
|
||||
|
@ -283,12 +283,12 @@ int main(void)
|
||||
assert(rc == 0);
|
||||
|
||||
/* set the device name */
|
||||
ble_svc_gap_device_name_set(NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
ble_svc_gap_device_name_set(CONFIG_NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
/* reload the GATT server to link our added services */
|
||||
ble_gatts_start();
|
||||
|
||||
/* start to advertise this node */
|
||||
nimble_autoadv_start();
|
||||
nimble_autoadv_start(NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -17,8 +17,8 @@ USEMODULE += nimble_svc_gatt
|
||||
|
||||
# Use automated advertising
|
||||
USEMODULE += nimble_autoadv
|
||||
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"RIOT Heart Rate Sensor"'
|
||||
CFLAGS += -DNIMBLE_AUTOADV_START_MANUALLY=1
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_DEVICE_NAME='"RIOT Heart Rate Sensor"'
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_START_MANUALLY=1
|
||||
|
||||
# Comment this out to disable code in RIOT that does safety checking
|
||||
# which is not needed in a production environment but helps in the
|
||||
|
@ -215,7 +215,7 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg)
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
if (event->connect.status) {
|
||||
_stop_updating();
|
||||
nimble_autoadv_start();
|
||||
nimble_autoadv_start(NULL);
|
||||
return 0;
|
||||
}
|
||||
_conn_handle = event->connect.conn_handle;
|
||||
@ -223,7 +223,7 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg)
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
_stop_updating();
|
||||
nimble_autoadv_start();
|
||||
nimble_autoadv_start(NULL);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
@ -296,29 +296,32 @@ int main(void)
|
||||
assert(res == 0);
|
||||
|
||||
/* set the device name */
|
||||
ble_svc_gap_device_name_set(NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
ble_svc_gap_device_name_set(CONFIG_NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
/* reload the GATT server to link our added services */
|
||||
ble_gatts_start();
|
||||
|
||||
struct ble_gap_adv_params advp;
|
||||
memset(&advp, 0, sizeof(advp));
|
||||
|
||||
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
advp.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
||||
advp.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MAX;
|
||||
|
||||
nimble_autoadv_cfg_t cfg = {
|
||||
.adv_duration_ms = BLE_HS_FOREVER,
|
||||
.adv_itvl_ms = BLE_GAP_ADV_ITVL_MS(100),
|
||||
.flags = NIMBLE_AUTOADV_FLAG_CONNECTABLE | NIMBLE_AUTOADV_FLAG_LEGACY | \
|
||||
NIMBLE_AUTOADV_FLAG_SCANNABLE,
|
||||
.channel_map = 0,
|
||||
.filter_policy = 0,
|
||||
.own_addr_type = nimble_riot_own_addr_type,
|
||||
.phy = NIMBLE_PHY_1M,
|
||||
.tx_power = 0,
|
||||
};
|
||||
/* set advertise params */
|
||||
nimble_autoadv_set_ble_gap_adv_params(&advp);
|
||||
nimble_autoadv_cfg_update(&cfg);
|
||||
|
||||
/* configure and set the advertising data */
|
||||
uint16_t hrs_uuid = BLE_GATT_SVC_HRS;
|
||||
nimble_autoadv_add_field(BLE_GAP_AD_UUID16_INCOMP, &hrs_uuid, sizeof(hrs_uuid));
|
||||
|
||||
nimble_auto_adv_set_gap_cb(&gap_event_cb, NULL);
|
||||
nimble_autoadv_set_gap_cb(&gap_event_cb, NULL);
|
||||
|
||||
/* start to advertise this node */
|
||||
nimble_autoadv_start();
|
||||
nimble_autoadv_start(NULL);
|
||||
|
||||
/* run an event loop for handling the heart rate update events */
|
||||
event_loop(&_eq);
|
||||
|
15
makefiles/pkg/nimble.adv.mk
Normal file
15
makefiles/pkg/nimble.adv.mk
Normal file
@ -0,0 +1,15 @@
|
||||
# Adds an external Advertisement Instance
|
||||
# Parameter 1: The value for the advertisement instance without the CONFIG_ prefix
|
||||
# Result:
|
||||
# - Increases BLE_MULTI_ADV_INSTANCES count by 1, note that the actual amount
|
||||
# of advertisement instances is MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES + 1, which
|
||||
# is why BLE_MULTI_ADV_INSTANCES starts at -1
|
||||
# - Sets the adv instance for CONFIG_$1 index to $(BLE_MULTI_ADV_INSTANCES)
|
||||
BLE_MULTI_ADV_INSTANCES ?= -1
|
||||
define _add_ext_adv_instance
|
||||
# Increase the count in one
|
||||
BLE_MULTI_ADV_INSTANCES := $$(shell echo $$$$(($(BLE_MULTI_ADV_INSTANCES) + 1)))
|
||||
# Export the definition in CFLAGS
|
||||
$(1) := $$(BLE_MULTI_ADV_INSTANCES)
|
||||
CFLAGS += -DCONFIG_$(1)=$$($(1))
|
||||
endef
|
@ -165,6 +165,7 @@ PSEUDOMODULES += nimble_phy_coded
|
||||
PSEUDOMODULES += nimble_phy_2mbit
|
||||
PSEUDOMODULES += nimble_rpble_ext
|
||||
PSEUDOMODULES += nimble_statconn_ext
|
||||
PSEUDOMODULES += nimble_autoadv_shell
|
||||
PSEUDOMODULES += newlib
|
||||
PSEUDOMODULES += newlib_gnu_source
|
||||
PSEUDOMODULES += newlib_nano
|
||||
|
@ -23,7 +23,7 @@ else
|
||||
CFLAGS += -Wno-unused-but-set-variable
|
||||
endif
|
||||
|
||||
IGNORE := nimble_autoconn_% nimble_phy_% nimble_%_ext
|
||||
IGNORE := nimble_autoconn_% nimble_phy_% nimble_%_ext nimble_autoadv%
|
||||
SUBMODS := $(filter-out $(IGNORE),$(filter nimble_%,$(USEMODULE)))
|
||||
|
||||
.PHONY: all
|
||||
@ -74,9 +74,6 @@ nimble_drivers_nrf5x:
|
||||
nimble_addr:
|
||||
$(QQ)"$(MAKE)" -C $(TDIR)/addr/
|
||||
|
||||
nimble_autoadv:
|
||||
$(QQ)"$(MAKE)" -C $(TDIR)/autoadv
|
||||
|
||||
nimble_autoconn:
|
||||
$(QQ)"$(MAKE)" -C $(TDIR)/autoconn
|
||||
|
||||
|
@ -63,6 +63,10 @@ endif
|
||||
|
||||
ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
|
||||
USEMODULE += bluetil_ad
|
||||
USEMODULE += bluetil_addr
|
||||
ifneq (,$(filter shell_commands,$(USEMODULE)))
|
||||
DEFAULT_MODULE += nimble_autoadv_shell
|
||||
endif
|
||||
endif
|
||||
|
||||
ifneq (,$(filter nimble_autoconn_ext,$(USEMODULE)))
|
||||
@ -122,6 +126,15 @@ ifneq (,$(filter nimble_netif_ext,$(USEMODULE)))
|
||||
USEMODULE += nimble_adv_ext
|
||||
endif
|
||||
|
||||
ifneq (,$(filter nimble_netif,$(USEMODULE)))
|
||||
# nimble_netif and nimble_autoadv will both setup gat advertisement, which will
|
||||
# conflict with each other. Using the extended advertisement module allows
|
||||
# setting this up on independent state machines allowing them to run concurently
|
||||
ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
|
||||
USEMODULE += nimble_adv_ext
|
||||
endif
|
||||
endif
|
||||
|
||||
ifneq (,$(filter nimble_netif,$(USEMODULE)))
|
||||
FEATURES_REQUIRED += ble_nimble_netif
|
||||
USEMODULE += random
|
||||
|
@ -88,6 +88,7 @@ endif
|
||||
|
||||
ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
|
||||
INCLUDES += -I$(RIOTPKG)/nimble/autoadv/include
|
||||
DIRS += $(RIOTPKG)/nimble/autoadv
|
||||
endif
|
||||
|
||||
ifneq (,$(filter nimble_autoconn,$(USEMODULE)))
|
||||
@ -97,9 +98,20 @@ endif
|
||||
ifneq (,$(filter nimble_adv_ext,$(USEMODULE)))
|
||||
CFLAGS += -DMYNEWT_VAL_BLE_EXT_ADV=1
|
||||
CFLAGS += -DMYNEWT_VAL_BLE_LL_EXT_ADV_AUX_PTR_CNT=2
|
||||
CFLAGS += -DMYNEWT_VAL_BLE_LL_SCAN_AUX_SEGMENT_CNT=1
|
||||
ifneq (,$(filter nimble_controller,$(USEMODULE)))
|
||||
CFLAGS += -DMYNEWT_VAL_BLE_LL_CFG_FEAT_LL_EXT_ADV=1
|
||||
endif
|
||||
ifneq (,$(filter nimble_netif,$(USEMODULE)))
|
||||
$(eval $(call _add_ext_adv_instance,NIMBLE_NETIF_ADV_INSTANCE))
|
||||
endif
|
||||
ifneq (,$(filter nimble_autoadv,$(USEMODULE)))
|
||||
$(eval $(call _add_ext_adv_instance,NIMBLE_AUTOADV_INSTANCE))
|
||||
endif
|
||||
# check that an advertisement instances was configured
|
||||
ifneq (-1,$(BLE_MULTI_ADV_INSTANCES))
|
||||
CFLAGS += -DMYNEWT_VAL_BLE_MULTI_ADV_INSTANCES=$(BLE_MULTI_ADV_INSTANCES)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifneq (,$(filter nimble_netif,$(USEMODULE)))
|
||||
|
@ -1,3 +1,7 @@
|
||||
MODULE = nimble_autoadv
|
||||
|
||||
SUBMODULES = 1
|
||||
|
||||
SRC += nimble_autoadv.c
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
||||
|
@ -14,17 +14,17 @@ USEMODULE += nimble_autoadv
|
||||
to your makefile.
|
||||
|
||||
If your application is calling functions from nimble, e.g.
|
||||
ble_svc_gap_device_name_set(), NIMBLE_AUTOADV_START_MANUALLY should be set to 1
|
||||
ble_svc_gap_device_name_set(), CONFIG_NIMBLE_AUTOADV_START_MANUALLY should be set to 1
|
||||
with the following line in your Makefile:
|
||||
```
|
||||
CFLAGS += -DNIMBLE_AUTOADV_START_MANUALLY=1
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_START_MANUALLY=1
|
||||
```
|
||||
Then the application should call nimble_autoadv_adv_start() after all of its
|
||||
nimble calls to prevent errors like BLE_HS_EBUSY.
|
||||
|
||||
To specify a device name add the following line to your Makefile:
|
||||
```
|
||||
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"Riot OS device"'
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_DEVICE_NAME='"Riot OS device"'
|
||||
```
|
||||
|
||||
By the default, in the advertised packet, the module includes the advertising
|
||||
@ -36,8 +36,8 @@ omitted.
|
||||
|
||||
If your application is not connectable (eg. a temperature sensor advertising
|
||||
its current value), you might want omit this flag by clearing the
|
||||
`NIMBLE_AUTOADV_FLAG_FIELD` when including this module:
|
||||
`CONFIG_NIMBLE_AUTOADV_FLAG_FIELD` when including this module:
|
||||
```
|
||||
CFLAGS += -DNIMBLE_AUTOADV_FLAG_FIELD=0
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_FLAG_FIELD=0
|
||||
```
|
||||
This will grant three extra bytes in the advertisement packet.
|
||||
|
@ -35,25 +35,25 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Name of the device for the advertising procedure. If this is not
|
||||
* @brief Name of the device for the advertising procedure. If this is not
|
||||
* defined, it will be defined as NULL, resulting in not configuring
|
||||
* a name at all.
|
||||
*/
|
||||
#ifndef NIMBLE_AUTOADV_DEVICE_NAME
|
||||
#define NIMBLE_AUTOADV_DEVICE_NAME NULL
|
||||
#ifndef CONFIG_NIMBLE_AUTOADV_DEVICE_NAME
|
||||
#define CONFIG_NIMBLE_AUTOADV_DEVICE_NAME "NimBLE on RIOT"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief If an application is calling functions from nimble, e.g.
|
||||
* ble_svc_gap_device_name_set(), NIMBLE_AUTOADV_START_MANUALLY should
|
||||
* ble_svc_gap_device_name_set(), CONFIG_NIMBLE_AUTOADV_START_MANUALLY should
|
||||
* be set to 1 and then the application should call
|
||||
* nimble_autoadv_start() after all of its nimble calls to prevent
|
||||
* nimble_autoadv_start(NULL) after all of its nimble calls to prevent
|
||||
* errors like BLE_HS_EBUSY.
|
||||
*
|
||||
* Defined as 0 by default.
|
||||
*/
|
||||
#ifndef NIMBLE_AUTOADV_START_MANUALLY
|
||||
#define NIMBLE_AUTOADV_START_MANUALLY 0
|
||||
#ifndef CONFIG_NIMBLE_AUTOADV_START_MANUALLY
|
||||
#define CONFIG_NIMBLE_AUTOADV_START_MANUALLY 0
|
||||
#endif
|
||||
|
||||
/**
|
||||
@ -63,14 +63,49 @@ extern "C" {
|
||||
* are non-zero and the advertising packet is connectable, otherwise
|
||||
* the Flags data type may be omitted.
|
||||
*/
|
||||
#ifndef NIMBLE_AUTOADV_FLAG_FIELD
|
||||
#define NIMBLE_AUTOADV_FLAG_FIELD 1
|
||||
#ifndef CONFIG_NIMBLE_AUTOADV_FLAG_FIELD
|
||||
#define CONFIG_NIMBLE_AUTOADV_FLAG_FIELD 1
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize autoadv module.
|
||||
*/
|
||||
void nimble_autoadv_init(void);
|
||||
* @name NimBLE Auto Advertisement Configuration Flags
|
||||
*
|
||||
* Flags for enabling legacy advertisement and high-duty cycle mode when
|
||||
* accepting incoming connections
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
#define NIMBLE_AUTOADV_FLAG_LEGACY (1 << 0) /**< use legacy advertising mode */
|
||||
#define NIMBLE_AUTOADV_FLAG_HD_MODE (1 << 1) /**< use high duty cycle mode, only
|
||||
ignored if no direct advertising */
|
||||
#define NIMBLE_AUTOADV_FLAG_CONNECTABLE (1 << 2) /**< if connectable advertisement */
|
||||
#define NIMBLE_AUTOADV_FLAG_SCANNABLE (1 << 3) /**< if scannable advertisement */
|
||||
#define NIMBLE_AUTOADV_FLAG_ANONYMOUS (1 << 4) /**< if anonymous advertisement
|
||||
ignore if no 'nimble_adv_ex' */
|
||||
#define NIMBLE_AUTOADV_FLAG_SCAN_REQ_NOTIF (1 << 5) /**< enable scan-request notification
|
||||
ignore if no 'nimble_adv_ex' */
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Parameter set used to configure accepting connections (advertising)
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t adv_itvl_ms; /**< advertising interval [ms] */
|
||||
int32_t adv_duration_ms; /**< advertising interval [ms] */
|
||||
uint8_t flags; /**< advertising flags */
|
||||
uint8_t phy; /**< PHY mode */
|
||||
int8_t tx_power; /**< specify TX power to be used */
|
||||
uint8_t channel_map; /**< specify custom channel map */
|
||||
uint8_t own_addr_type; /**< specify our own address type to use */
|
||||
uint8_t filter_policy; /**< Advertising Filter policy */
|
||||
} nimble_autoadv_cfg_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize autoadv module.
|
||||
*
|
||||
* @param[in] cfg struct to copy current configuration into
|
||||
*/
|
||||
void nimble_autoadv_init(const nimble_autoadv_cfg_t *cfg);
|
||||
|
||||
/**
|
||||
* @brief Set struct for additional arguments specifying the particulars of
|
||||
@ -78,9 +113,17 @@ void nimble_autoadv_init(void);
|
||||
*
|
||||
* If there is an active advertising process, it will be restarted.
|
||||
*
|
||||
* @param[in] params struct with customized additional arguments
|
||||
* @param[in] cfg struct with customized additional arguments
|
||||
*/
|
||||
void nimble_autoadv_set_ble_gap_adv_params(struct ble_gap_adv_params *params);
|
||||
void nimble_autoadv_cfg_update(nimble_autoadv_cfg_t *cfg);
|
||||
|
||||
/**
|
||||
* @brief Gets struct for additional arguments specifying the particulars of
|
||||
* the advertising procedure. Uses memcpy internally.
|
||||
*
|
||||
* @param[in] cfg struct to copy current configuration into
|
||||
*/
|
||||
void nimble_autoadv_get_cfg(nimble_autoadv_cfg_t *cfg);
|
||||
|
||||
/**
|
||||
* @brief Add a new field to the given advertising data.
|
||||
@ -96,15 +139,6 @@ void nimble_autoadv_set_ble_gap_adv_params(struct ble_gap_adv_params *params);
|
||||
*/
|
||||
int nimble_autoadv_add_field(uint8_t type, const void *data, size_t data_len);
|
||||
|
||||
/**
|
||||
* @brief Set the duration for the advertising procedure.
|
||||
*
|
||||
* If there is an active advertising process, it will be restarted.
|
||||
*
|
||||
* @param[in] duration_ms duration of advertising procedure in ms
|
||||
*/
|
||||
void nimble_auto_adv_set_adv_duration(int32_t duration_ms);
|
||||
|
||||
/**
|
||||
* @brief Set the callback for gap events. Callback is used for the logic when
|
||||
* to start the advertising procedure.
|
||||
@ -119,32 +153,39 @@ void nimble_auto_adv_set_adv_duration(int32_t duration_ms);
|
||||
*
|
||||
* @param[in] cb_arg The optional argument to pass to the callback function.
|
||||
*/
|
||||
void nimble_auto_adv_set_gap_cb(ble_gap_event_fn *cb, void *cb_arg);
|
||||
void nimble_autoadv_set_gap_cb(ble_gap_event_fn *cb, void *cb_arg);
|
||||
|
||||
/**
|
||||
* @brief Start the automated advertising procedure.
|
||||
*
|
||||
* Needs to be called manually when NIMBLE_AUTOADV_START_MANUALLY was
|
||||
* Needs to be called manually when CONFIG_NIMBLE_AUTOADV_START_MANUALLY was
|
||||
* set to true and after every call of nimble_autoadv_stop() to start
|
||||
* advertising again.
|
||||
*
|
||||
* @param[in] addr addr for directed advertisement, can be NULL
|
||||
*/
|
||||
void nimble_autoadv_start(void);
|
||||
void nimble_autoadv_start(ble_addr_t *addr);
|
||||
|
||||
/**
|
||||
* @brief Stop the automated advertising procedure. After calling this, you
|
||||
* have to call nimble_autoadv_start() manually to restart the process.
|
||||
* have to call nimble_autoadv_start(NULL) manually to restart the process.
|
||||
*/
|
||||
void nimble_autoadv_stop(void);
|
||||
|
||||
/**
|
||||
* @brief Reset all data regarding the advertising process.
|
||||
* Following characteristics will be applied:
|
||||
* - General discoverable mode (BLE_GAP_DISC_MODE_GEN)
|
||||
* - Undirected connectable mode (BLE_GAP_CONN_MODE_UND)
|
||||
* - No expiration (BLE_HS_FOREVER)
|
||||
* - No name
|
||||
* @brief Reset all data regarding the advertising process
|
||||
*
|
||||
* @param[in] cfg struct to copy current configuration into
|
||||
*/
|
||||
void nimble_autoadv_reset(void);
|
||||
void nimble_autoadv_reset(nimble_autoadv_cfg_t *cfg);
|
||||
|
||||
/**
|
||||
* @brief Return the advertisement instance
|
||||
* @note The advertisement instance is set automatically by the build-system
|
||||
*
|
||||
* @return The advertisement instance
|
||||
*/
|
||||
int nimble_autoadv_get_adv_instance(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
84
pkg/nimble/autoadv/include/nimble_autoadv_params.h
Normal file
84
pkg/nimble/autoadv/include/nimble_autoadv_params.h
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Inria
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup pkg_nimble_autoadv
|
||||
*
|
||||
* @{
|
||||
* @file
|
||||
* @brief Default configuration for the nimble_autoadv module
|
||||
*
|
||||
* @author Francisco Molina <francois-xavier.molina@inria.fr>
|
||||
*/
|
||||
|
||||
#ifndef NIMBLE_AUTOADV_PARAMS_H
|
||||
#define NIMBLE_AUTOADV_PARAMS_H
|
||||
|
||||
#include "nimble_autoadv.h"
|
||||
#include "nimble_riot.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @name Default parameters used for the nimble_autoadv module
|
||||
* @{
|
||||
*/
|
||||
#ifndef NIMBLE_AUTOADV_ADV_ITVL_MS
|
||||
#define NIMBLE_AUTOADV_ADV_ITVL_MS (100U) /* 100ms */
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_ADV_DURATION_MS
|
||||
#define NIMBLE_AUTOADV_ADV_DURATION_MS (BLE_HS_FOREVER) /* forever */
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_FLAGS
|
||||
#define NIMBLE_AUTOADV_FLAGS (NIMBLE_AUTOADV_FLAG_LEGACY | \
|
||||
NIMBLE_AUTOADV_FLAG_CONNECTABLE | \
|
||||
NIMBLE_AUTOADV_FLAG_SCANNABLE)
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_PHY
|
||||
#define NIMBLE_AUTOADV_PHY NIMBLE_PHY_1M
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_TX_POWER
|
||||
#define NIMBLE_AUTOADV_TX_POWER 0 /* 0dBm */
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_CHANNEL_MAP
|
||||
#define NIMBLE_AUTOADV_CHANNEL_MAP 0
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_OWN_ADDR_TYPE
|
||||
#define NIMBLE_AUTOADV_OWN_ADDR_TYPE 0xFF /* sets to nimble_riot_own_addr */
|
||||
#endif
|
||||
#ifndef NIMBLE_AUTOADV_FILTER_POLICY
|
||||
#define NIMBLE_AUTOADV_FILTER_POLICY 0
|
||||
#endif
|
||||
|
||||
#ifndef NIMBLE_AUTOADV_PARAMS
|
||||
#define NIMBLE_AUTOADV_PARAMS \
|
||||
{ .adv_itvl_ms = NIMBLE_AUTOADV_ADV_ITVL_MS, \
|
||||
.adv_duration_ms = NIMBLE_AUTOADV_ADV_DURATION_MS, \
|
||||
.flags = NIMBLE_AUTOADV_FLAGS, \
|
||||
.phy = NIMBLE_AUTOADV_PHY, \
|
||||
.tx_power = NIMBLE_AUTOADV_TX_POWER, \
|
||||
.channel_map = NIMBLE_AUTOADV_CHANNEL_MAP, \
|
||||
.own_addr_type = NIMBLE_AUTOADV_OWN_ADDR_TYPE, \
|
||||
.filter_policy = NIMBLE_AUTOADV_FILTER_POLICY }
|
||||
#endif
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
* @brief nimble_autoadv configuration
|
||||
*/
|
||||
static const nimble_autoadv_cfg_t nimble_autoadv_cfg =
|
||||
NIMBLE_AUTOADV_PARAMS;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NIMBLE_AUTOADV_PARAMS_H */
|
||||
/** @} */
|
@ -29,56 +29,62 @@
|
||||
|
||||
#include "nimble_autoadv.h"
|
||||
|
||||
/* settings for advertising procedure */
|
||||
static struct ble_gap_adv_params _advp;
|
||||
#ifndef CONFIG_NIMBLE_AUTOADV_INSTANCE
|
||||
#define CONFIG_NIMBLE_AUTOADV_INSTANCE 0
|
||||
#endif
|
||||
|
||||
/* duration of the advertisement procedure */
|
||||
static int32_t _adv_duration;
|
||||
/* settings for advertising procedure */
|
||||
static nimble_autoadv_cfg_t _cfg;
|
||||
|
||||
/* buffer for _ad */
|
||||
static uint8_t buf[BLE_HS_ADV_MAX_SZ];
|
||||
|
||||
/* advertising data struct */
|
||||
static bluetil_ad_t _ad;
|
||||
|
||||
/* GAP callback function */
|
||||
static ble_gap_event_fn *_gap_cb;
|
||||
|
||||
/* arguments for GAP callback function */
|
||||
static void *_gap_cb_arg;
|
||||
|
||||
void nimble_autoadv_start(void);
|
||||
|
||||
static int _gap_event_cb(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
(void) arg;
|
||||
(void)arg;
|
||||
|
||||
switch (event->type) {
|
||||
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
if (event->connect.status != 0) {
|
||||
// failed, ensure advertising is restarted
|
||||
nimble_autoadv_start();
|
||||
}
|
||||
break;
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
if (event->connect.status != 0) {
|
||||
/* failed, ensure advertising is restarted */
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
nimble_autoadv_start();
|
||||
break;
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
nimble_autoadv_start(NULL);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void nimble_autoadv_init(void)
|
||||
void nimble_autoadv_init(const nimble_autoadv_cfg_t *cfg)
|
||||
{
|
||||
nimble_autoadv_reset();
|
||||
nimble_autoadv_reset((nimble_autoadv_cfg_t *)cfg);
|
||||
|
||||
if (!NIMBLE_AUTOADV_START_MANUALLY) {
|
||||
nimble_autoadv_start();
|
||||
if (!CONFIG_NIMBLE_AUTOADV_START_MANUALLY) {
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static int _ble_gap_adv_active(void)
|
||||
{
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
return ble_gap_ext_adv_active(CONFIG_NIMBLE_AUTOADV_INSTANCE);
|
||||
#else
|
||||
return ble_gap_adv_active();
|
||||
#endif
|
||||
}
|
||||
|
||||
int nimble_autoadv_add_field(uint8_t type, const void *data, size_t data_len)
|
||||
{
|
||||
int rc = bluetil_ad_add(&_ad, type, data, data_len);
|
||||
@ -87,94 +93,180 @@ int nimble_autoadv_add_field(uint8_t type, const void *data, size_t data_len)
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (ble_gap_adv_active()) {
|
||||
nimble_autoadv_start();
|
||||
if (_ble_gap_adv_active()) {
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void nimble_autoadv_set_ble_gap_adv_params(struct ble_gap_adv_params *params)
|
||||
void nimble_autoadv_cfg_update(nimble_autoadv_cfg_t *cfg)
|
||||
{
|
||||
memcpy(&_advp, params, sizeof(struct ble_gap_adv_params));
|
||||
memcpy(&_cfg, cfg, sizeof(nimble_autoadv_cfg_t));
|
||||
|
||||
if (ble_gap_adv_active()) {
|
||||
nimble_autoadv_start();
|
||||
if (cfg->own_addr_type == 0xff) {
|
||||
_cfg.own_addr_type = nimble_riot_own_addr_type;
|
||||
}
|
||||
|
||||
if (_ble_gap_adv_active()) {
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void nimble_auto_adv_set_adv_duration(int32_t duration_ms)
|
||||
void nimble_autoadv_get_cfg(nimble_autoadv_cfg_t *cfg)
|
||||
{
|
||||
_adv_duration = duration_ms;
|
||||
|
||||
if (ble_gap_adv_active()) {
|
||||
nimble_autoadv_start();
|
||||
}
|
||||
memcpy(cfg, &_cfg, sizeof(nimble_autoadv_cfg_t));
|
||||
}
|
||||
|
||||
void nimble_auto_adv_set_gap_cb(ble_gap_event_fn *cb, void *cb_arg)
|
||||
void nimble_autoadv_set_gap_cb(ble_gap_event_fn *cb, void *cb_arg)
|
||||
{
|
||||
_gap_cb = cb;
|
||||
_gap_cb_arg = cb_arg;
|
||||
|
||||
if (ble_gap_adv_active()) {
|
||||
nimble_autoadv_start();
|
||||
if (_ble_gap_adv_active()) {
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void nimble_autoadv_start(void)
|
||||
void nimble_autoadv_start(ble_addr_t *addr)
|
||||
{
|
||||
int rc;
|
||||
(void) rc;
|
||||
|
||||
rc = ble_gap_adv_stop();
|
||||
assert(rc == BLE_HS_EALREADY || rc == 0);
|
||||
(void)rc;
|
||||
|
||||
nimble_autoadv_stop();
|
||||
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
struct ble_gap_ext_adv_params advp = {
|
||||
.connectable = (_cfg.flags & NIMBLE_AUTOADV_FLAG_CONNECTABLE) ? 1 : 0,
|
||||
.scannable = (_cfg.flags & NIMBLE_AUTOADV_FLAG_SCANNABLE) ? 1 : 0,
|
||||
.directed = addr ? 1 : 0,
|
||||
.high_duty_directed = (_cfg.flags & NIMBLE_AUTOADV_FLAG_HD_MODE) ? 1 : 0,
|
||||
.legacy_pdu = (_cfg.flags & NIMBLE_AUTOADV_FLAG_LEGACY) ? 1 : 0,
|
||||
.anonymous = (_cfg.flags & NIMBLE_AUTOADV_FLAG_ANONYMOUS) ? 1 : 0,
|
||||
.include_tx_power = 0,
|
||||
.scan_req_notif = (_cfg.flags & NIMBLE_AUTOADV_FLAG_SCAN_REQ_NOTIF) ? 1 : 0,
|
||||
.itvl_min = BLE_GAP_ADV_ITVL_MS(_cfg.adv_itvl_ms),
|
||||
.itvl_max = BLE_GAP_ADV_ITVL_MS(_cfg.adv_itvl_ms),
|
||||
.channel_map = _cfg.channel_map,
|
||||
.own_addr_type = _cfg.own_addr_type,
|
||||
.peer = *addr,
|
||||
.filter_policy = _cfg.filter_policy,
|
||||
.primary_phy = nimble_riot_get_phy_hci(_cfg.phy),
|
||||
.secondary_phy = nimble_riot_get_phy_hci(_cfg.phy),
|
||||
.tx_power = _cfg.tx_power,
|
||||
.sid = CONFIG_NIMBLE_AUTOADV_INSTANCE,
|
||||
};
|
||||
|
||||
/* remove before configure */
|
||||
if (ble_gap_ext_adv_active(CONFIG_NIMBLE_AUTOADV_INSTANCE)) {
|
||||
rc = ble_gap_ext_adv_remove(CONFIG_NIMBLE_AUTOADV_INSTANCE);
|
||||
assert(rc == 0);
|
||||
}
|
||||
/* configure */
|
||||
rc = ble_gap_ext_adv_configure(CONFIG_NIMBLE_AUTOADV_INSTANCE, &advp,
|
||||
NULL, _gap_event_cb, _gap_cb_arg);
|
||||
assert(rc == 0);
|
||||
|
||||
/* get mbuf for adv data, this is freed from the `ble_gap_ext_adv_set_data` */
|
||||
struct os_mbuf *data;
|
||||
|
||||
data = os_msys_get_pkthdr(BLE_HS_ADV_MAX_SZ, 0);
|
||||
assert(data);
|
||||
|
||||
/* fill mbuf with adv data */
|
||||
rc = os_mbuf_append(data, _ad.buf, _ad.pos);
|
||||
assert(rc == 0);
|
||||
|
||||
/* set adv data */
|
||||
rc = ble_gap_ext_adv_set_data(CONFIG_NIMBLE_AUTOADV_INSTANCE, data);
|
||||
assert(rc == 0);
|
||||
|
||||
/* set a single advertisement event, ble_gap_ext_adv units are in 10s of ms */
|
||||
int32_t dur = (_cfg.adv_duration_ms == BLE_HS_FOREVER) ? 0
|
||||
: _cfg.adv_duration_ms / 10;
|
||||
rc = ble_gap_ext_adv_start(CONFIG_NIMBLE_AUTOADV_INSTANCE, dur, 0);
|
||||
assert(rc == 0);
|
||||
#else
|
||||
uint8_t mode = BLE_GAP_CONN_MODE_NON;
|
||||
if (addr != NULL) {
|
||||
mode = BLE_GAP_CONN_MODE_DIR;
|
||||
}
|
||||
else if (_cfg.flags && NIMBLE_AUTOADV_FLAG_CONNECTABLE) {
|
||||
mode = BLE_GAP_CONN_MODE_UND;
|
||||
}
|
||||
uint8_t disc = (_cfg.flags && NIMBLE_AUTOADV_FLAG_SCANNABLE) ? BLE_GAP_CONN_MODE_DIR
|
||||
: BLE_GAP_CONN_MODE_UND;
|
||||
struct ble_gap_adv_params advp = {
|
||||
.conn_mode = mode,
|
||||
.disc_mode = disc,
|
||||
.itvl_min = BLE_GAP_ADV_ITVL_MS(_cfg.adv_itvl_ms),
|
||||
.itvl_max = BLE_GAP_ADV_ITVL_MS(_cfg.adv_itvl_ms),
|
||||
.channel_map = _cfg.channel_map,
|
||||
.filter_policy = _cfg.filter_policy,
|
||||
.high_duty_cycle = (_cfg.flags & NIMBLE_AUTOADV_FLAG_HD_MODE) ? 1 : 0,
|
||||
};
|
||||
|
||||
rc = ble_gap_adv_set_data(_ad.buf, _ad.pos);
|
||||
assert(rc == 0);
|
||||
|
||||
rc = ble_gap_adv_start(nimble_riot_own_addr_type, NULL, _adv_duration, &_advp, _gap_cb, _gap_cb_arg);
|
||||
rc = ble_gap_adv_start(_cfg.own_addr_type, addr, _cfg.adv_duration_ms,
|
||||
&advp, _gap_cb, _gap_cb_arg);
|
||||
assert(rc == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int _ble_gap_stop(void)
|
||||
{
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
if (ble_gap_ext_adv_active(CONFIG_NIMBLE_AUTOADV_INSTANCE)) {
|
||||
return ble_gap_ext_adv_stop(CONFIG_NIMBLE_AUTOADV_INSTANCE);
|
||||
}
|
||||
#else
|
||||
if (ble_gap_adv_active()) {
|
||||
return ble_gap_adv_stop();
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
void nimble_autoadv_stop(void)
|
||||
{
|
||||
int rc;
|
||||
(void) rc;
|
||||
int rc = _ble_gap_stop();
|
||||
|
||||
rc = ble_gap_adv_stop();
|
||||
(void)rc;
|
||||
assert(rc == BLE_HS_EALREADY || rc == 0);
|
||||
}
|
||||
|
||||
void nimble_autoadv_reset(void)
|
||||
void nimble_autoadv_reset(nimble_autoadv_cfg_t *cfg)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
(void)rc;
|
||||
_gap_cb = &_gap_event_cb;
|
||||
_gap_cb_arg = NULL;
|
||||
|
||||
_adv_duration = BLE_HS_FOREVER;
|
||||
nimble_autoadv_cfg_update(cfg);
|
||||
|
||||
memset(&_advp, 0, sizeof _advp);
|
||||
_advp.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
_advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
|
||||
int rc = 0;
|
||||
(void) rc;
|
||||
|
||||
if (IS_ACTIVE(NIMBLE_AUTOADV_FLAG_FIELD) && BLUETIL_AD_FLAGS_DEFAULT != 0) {
|
||||
rc = bluetil_ad_init_with_flags(&_ad, buf, sizeof(buf),
|
||||
BLUETIL_AD_FLAGS_DEFAULT);
|
||||
if (IS_ACTIVE(CONFIG_NIMBLE_AUTOADV_FLAG_FIELD) && BLUETIL_AD_FLAGS_DEFAULT != 0) {
|
||||
rc = bluetil_ad_init_with_flags(&_ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
|
||||
assert(rc == BLUETIL_AD_OK);
|
||||
}
|
||||
else {
|
||||
bluetil_ad_init(&_ad, buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
if (NIMBLE_AUTOADV_DEVICE_NAME != NULL) {
|
||||
rc = bluetil_ad_add_name(&_ad, NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
if (CONFIG_NIMBLE_AUTOADV_DEVICE_NAME != NULL) {
|
||||
rc = bluetil_ad_add_name(&_ad, CONFIG_NIMBLE_AUTOADV_DEVICE_NAME);
|
||||
assert(rc == BLUETIL_AD_OK);
|
||||
}
|
||||
|
||||
if (ble_gap_adv_active()) {
|
||||
nimble_autoadv_start();
|
||||
if (_ble_gap_adv_active()) {
|
||||
nimble_autoadv_start(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int nimble_autoadv_get_adv_instance(void)
|
||||
{
|
||||
return CONFIG_NIMBLE_AUTOADV_INSTANCE;
|
||||
}
|
||||
|
100
pkg/nimble/autoadv/shell.c
Normal file
100
pkg/nimble/autoadv/shell.c
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Inria
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup pkg_nimble_autoadv
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Auto advertisement module shell commands
|
||||
*
|
||||
* @author Francisco Molina <francois-xavier.molina@inria.fr>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "shell.h"
|
||||
#include "shell_commands.h"
|
||||
#include "xfa.h"
|
||||
|
||||
#include "nimble_riot.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/ble_gap.h"
|
||||
#include "net/bluetil/ad.h"
|
||||
#include "net/bluetil/addr.h"
|
||||
#include "nimble_autoadv.h"
|
||||
|
||||
static int _ble_gap_adv_active(void)
|
||||
{
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
return ble_gap_ext_adv_active(nimble_autoadv_get_adv_instance());
|
||||
#else
|
||||
return ble_gap_adv_active();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void _print_usage(void)
|
||||
{
|
||||
puts("Usage:");
|
||||
puts("\tautoadv start [addr]: start NimBLE auto advertisement");
|
||||
puts("\tautoadv stop: stop NimBLE auto advertisement");
|
||||
puts("\tautoadv status: print NimBLE auto advertisement status");
|
||||
}
|
||||
|
||||
static int _autoadv_handler(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
_print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "start")) {
|
||||
ble_addr_t *addr_ptr = NULL;
|
||||
ble_addr_t addr = { .type = nimble_riot_own_addr_type };
|
||||
if (argc == 3) {
|
||||
uint8_t addrn[BLE_ADDR_LEN];
|
||||
if (bluetil_addr_from_str(addrn, argv[2]) != NULL) {
|
||||
/* NimBLE expects address in little endian, so swap */
|
||||
bluetil_addr_swapped_cp(addrn, addr.val);
|
||||
addr_ptr = &addr;
|
||||
puts("Found BLE address: sending directed advertisements");
|
||||
}
|
||||
|
||||
}
|
||||
nimble_autoadv_start(addr_ptr);
|
||||
printf("[autoadv] shell: start advertising on inst=(%d)\n",
|
||||
nimble_autoadv_get_adv_instance());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "stop")) {
|
||||
nimble_autoadv_stop();
|
||||
printf("[autoadv] shell: stop advertising on inst=(%d)\n",
|
||||
nimble_autoadv_get_adv_instance());
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "status")) {
|
||||
if (_ble_gap_adv_active()) {
|
||||
printf("[autoadv] shell: active, inst=(%d)\n",
|
||||
nimble_autoadv_get_adv_instance());
|
||||
}
|
||||
else {
|
||||
puts("[autoadv] shell: inactive\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
_print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
SHELL_COMMAND(autoadv, "NimBLE autoadv", _autoadv_handler);
|
@ -93,6 +93,15 @@ extern uint8_t nimble_riot_own_addr_type;
|
||||
*/
|
||||
void nimble_riot_init(void);
|
||||
|
||||
/**
|
||||
* @brief Converts BLE PHY mode to BLE HCI PHY
|
||||
*
|
||||
* @param[in] mode ble phy mode to convert
|
||||
*/
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
int nimble_riot_get_phy_hci(uint8_t mode);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -41,6 +41,11 @@
|
||||
#include "nimble_statconn.h"
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_NIMBLE_AUTOADV
|
||||
#include "nimble_autoadv_params.h"
|
||||
#include "nimble_autoadv.h"
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_NIMBLE_AUTOCONN) && !defined(MODULE_NIMBLE_AUTOCONN_NOAUTOINIT)
|
||||
#include "nimble_autoconn.h"
|
||||
#include "nimble_autoconn_params.h"
|
||||
@ -184,8 +189,7 @@ void nimble_riot_init(void)
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_NIMBLE_AUTOADV
|
||||
extern void nimble_autoadv_init(void);
|
||||
nimble_autoadv_init();
|
||||
nimble_autoadv_init(&nimble_autoadv_cfg);
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_NIMBLE_RPBLE
|
||||
@ -193,3 +197,23 @@ void nimble_riot_init(void)
|
||||
assert(res == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
int nimble_riot_get_phy_hci(uint8_t mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case NIMBLE_PHY_1M:
|
||||
return BLE_HCI_LE_PHY_1M;
|
||||
#if IS_USED(MODULE_NIMBLE_PHY_2MBIT)
|
||||
case NIMBLE_PHY_2M:
|
||||
return BLE_HCI_LE_PHY_2M;
|
||||
#endif
|
||||
#if IS_USED(MODULE_NIMBLE_PHY_CODED)
|
||||
case NIMBLE_PHY_CODED:
|
||||
return BLE_HCI_LE_PHY_CODED;
|
||||
#endif
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -55,7 +55,9 @@
|
||||
#define NIMBLE_NETIF_PRIO GNRC_NETIF_PRIO
|
||||
#endif
|
||||
|
||||
#define EXT_ADV_INST 0
|
||||
#ifndef CONFIG_NIMBLE_NETIF_ADV_INSTANCE
|
||||
#define CONFIG_NIMBLE_NETIF_ADV_INSTANCE 0
|
||||
#endif
|
||||
|
||||
/* thread flag used for signaling transmit readiness */
|
||||
#define FLAG_TX_UNSTALLED (1u << 13)
|
||||
@ -682,26 +684,6 @@ int nimble_netif_close(int handle)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
static int _get_phy_hci(uint8_t mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case NIMBLE_PHY_1M:
|
||||
return BLE_HCI_LE_PHY_1M;
|
||||
#if IS_USED(MODULE_NIMBLE_PHY_2MBIT)
|
||||
case NIMBLE_PHY_2M:
|
||||
return BLE_HCI_LE_PHY_2M;
|
||||
#endif
|
||||
#if IS_USED(MODULE_NIMBLE_PHY_CODED)
|
||||
case NIMBLE_PHY_CODED:
|
||||
return BLE_HCI_LE_PHY_CODED;
|
||||
#endif
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int _accept(const uint8_t *ad, size_t ad_len, const ble_addr_t *addr,
|
||||
const nimble_netif_accept_cfg_t *params)
|
||||
{
|
||||
@ -728,8 +710,8 @@ static int _accept(const uint8_t *ad, size_t ad_len, const ble_addr_t *addr,
|
||||
memset(&p, 0, sizeof(p));
|
||||
|
||||
/* figure out PHY modes */
|
||||
int phy_pri = _get_phy_hci(params->primary_phy);
|
||||
int phy_sec = _get_phy_hci(params->secondary_phy);
|
||||
int phy_pri = nimble_riot_get_phy_hci(params->primary_phy);
|
||||
int phy_sec = nimble_riot_get_phy_hci(params->secondary_phy);
|
||||
if ((phy_pri < 0) || (phy_sec < 0)) {
|
||||
nimble_netif_conn_free(handle, NULL);
|
||||
return -EINVAL;
|
||||
@ -763,7 +745,7 @@ static int _accept(const uint8_t *ad, size_t ad_len, const ble_addr_t *addr,
|
||||
p.secondary_phy = (uint8_t)phy_sec;
|
||||
p.tx_power = params->tx_power;
|
||||
|
||||
res = ble_gap_ext_adv_configure(EXT_ADV_INST, &p, NULL,
|
||||
res = ble_gap_ext_adv_configure(CONFIG_NIMBLE_NETIF_ADV_INSTANCE, &p, NULL,
|
||||
_on_gap_slave_evt, (void *)handle);
|
||||
if (res != 0) {
|
||||
nimble_netif_conn_free(handle, NULL);
|
||||
@ -782,10 +764,10 @@ static int _accept(const uint8_t *ad, size_t ad_len, const ble_addr_t *addr,
|
||||
nimble_netif_conn_free(handle, NULL);
|
||||
return -ENOMEM;
|
||||
}
|
||||
res = ble_gap_ext_adv_set_data(EXT_ADV_INST, data);
|
||||
res = ble_gap_ext_adv_set_data(CONFIG_NIMBLE_NETIF_ADV_INSTANCE, data);
|
||||
assert(res == 0);
|
||||
}
|
||||
res = ble_gap_ext_adv_start(EXT_ADV_INST, params->timeout_ms / 10, 0);
|
||||
res = ble_gap_ext_adv_start(CONFIG_NIMBLE_NETIF_ADV_INSTANCE, params->timeout_ms / 10, 0);
|
||||
#else
|
||||
uint8_t mode = (addr != NULL) ? BLE_GAP_CONN_MODE_DIR
|
||||
: BLE_GAP_CONN_MODE_UND;
|
||||
@ -847,7 +829,7 @@ int nimble_netif_accept_stop(void)
|
||||
|
||||
int res;
|
||||
#if MYNEWT_VAL_BLE_EXT_ADV
|
||||
res = ble_gap_ext_adv_stop(EXT_ADV_INST);
|
||||
res = ble_gap_ext_adv_stop(CONFIG_NIMBLE_NETIF_ADV_INSTANCE);
|
||||
#else
|
||||
res = ble_gap_adv_stop();
|
||||
#endif
|
||||
|
@ -35,7 +35,7 @@ to your makefile.
|
||||
|
||||
The advertised device name can then optionally be configured with
|
||||
```
|
||||
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"Riot OS device"'
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_DEVICE_NAME='"Riot OS device"'
|
||||
```
|
||||
Otherwise the device will appear as "*RIOT OS device*".
|
||||
|
||||
@ -107,4 +107,4 @@ ps
|
||||
2 | bl anyfl _ | 5
|
||||
3 | bl anyfl _ | 0
|
||||
>
|
||||
```
|
||||
```
|
||||
|
@ -237,39 +237,44 @@ static int _gap_event_cb(struct ble_gap_event *event, void *arg)
|
||||
switch (event->type) {
|
||||
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
_debug_printf("BLE_GAP_EVENT_CONNECT\n");
|
||||
if (event->connect.status == 0) {
|
||||
_debug_printf("BLE_GAP_EVENT_CONNECT handle: %d\n", event->connect.conn_handle);
|
||||
if (event->connect.status == 0 && _conn_handle == 0) {
|
||||
_status = STDIO_NIMBLE_CONNECTED;
|
||||
if (CONFIG_STDIO_NIMBLE_CLEAR_BUFFER_ON_CONNECT) {
|
||||
_purge_buffer();
|
||||
}
|
||||
_conn_handle = event->connect.conn_handle;
|
||||
}
|
||||
else {
|
||||
else if (event->connect.conn_handle == _conn_handle) {
|
||||
_conn_handle = 0;
|
||||
_status = STDIO_NIMBLE_DISCONNECTED;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
_debug_printf("BLE_GAP_EVENT_DISCONNECT\n");
|
||||
_status = STDIO_NIMBLE_DISCONNECTED;
|
||||
_debug_printf("BLE_GAP_EVENT_DISCONNECT %d\n", event->disconnect.conn.conn_handle);
|
||||
if (event->disconnect.conn.conn_handle == _conn_handle) {
|
||||
_status = STDIO_NIMBLE_DISCONNECTED;
|
||||
_conn_handle = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
_debug_printf("BLE_GAP_EVENT_SUBSCRIBE\n");
|
||||
_debug_printf("BLE_GAP_EVENT_SUBSCRIBE %d\n", event->subscribe.conn_handle);
|
||||
if (event->subscribe.attr_handle == _val_handle_stdout) {
|
||||
if (event->subscribe.cur_indicate == 1) {
|
||||
_status = STDIO_NIMBLE_SUBSCRIBED;
|
||||
_conn_handle = event->subscribe.conn_handle;
|
||||
}
|
||||
else {
|
||||
_status = STDIO_NIMBLE_CONNECTED;
|
||||
_conn_handle = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_TX:
|
||||
_debug_printf("BLE_GAP_EVENT_NOTIFY_TX\n");
|
||||
if (event->notify_tx.indication == 1) {
|
||||
_debug_printf("BLE_GAP_EVENT_NOTIFY_TX %d\n", event->notify_tx.conn_handle);
|
||||
if (event->notify_tx.indication == 1 && (event->notify_tx.conn_handle == _conn_handle)) {
|
||||
if (event->notify_tx.status == BLE_HS_EDONE) {
|
||||
_status = STDIO_NIMBLE_SUBSCRIBED;
|
||||
}
|
||||
@ -356,9 +361,11 @@ ssize_t stdio_write(const void *buffer, size_t len)
|
||||
|
||||
unsigned int consumed = tsrb_add(&_tsrb_stdout, buffer, len);
|
||||
|
||||
if (!ble_npl_callout_is_active(&_send_stdout_callout)) {
|
||||
/* bootstrap callout */
|
||||
ble_npl_callout_reset(&_send_stdout_callout, CALLOUT_TICKS_MS);
|
||||
if (_status == STDIO_NIMBLE_SUBSCRIBED || _status == STDIO_NIMBLE_SENDING) {
|
||||
if (!ble_npl_callout_is_active(&_send_stdout_callout)) {
|
||||
/* bootstrap callout */
|
||||
ble_npl_callout_reset(&_send_stdout_callout, CALLOUT_TICKS_MS);
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
|
@ -9,7 +9,7 @@ USEMODULE += ps
|
||||
|
||||
USEMODULE += stdio_nimble stdio_nimble_debug
|
||||
USEMODULE += nimble_autoadv
|
||||
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"tests/shell_ble"'
|
||||
CFLAGS += -DCONFIG_NIMBLE_AUTOADV_DEVICE_NAME='"tests/shell_ble"'
|
||||
|
||||
TESTRUNNER_SHELL_SKIP_REBOOT = 1
|
||||
TESTRUNNER_RESET_BOARD_ON_STARTUP = 0
|
||||
|
Loading…
Reference in New Issue
Block a user