diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index cc64d28a9a..e281451309 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -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 diff --git a/pkg/nimble/Makefile b/pkg/nimble/Makefile index 832f06411c..d27cecc397 100644 --- a/pkg/nimble/Makefile +++ b/pkg/nimble/Makefile @@ -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 diff --git a/pkg/nimble/Makefile.dep b/pkg/nimble/Makefile.dep index cdeef5b6e8..b199681339 100644 --- a/pkg/nimble/Makefile.dep +++ b/pkg/nimble/Makefile.dep @@ -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))) diff --git a/pkg/nimble/Makefile.include b/pkg/nimble/Makefile.include index 75da027ad9..c192b432e0 100644 --- a/pkg/nimble/Makefile.include +++ b/pkg/nimble/Makefile.include @@ -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))) diff --git a/pkg/nimble/autoadv/Makefile b/pkg/nimble/autoadv/Makefile index 24d0f78ede..0e4218641b 100644 --- a/pkg/nimble/autoadv/Makefile +++ b/pkg/nimble/autoadv/Makefile @@ -1,3 +1,7 @@ MODULE = nimble_autoadv +SUBMODULES = 1 + +SRC += nimble_autoadv.c + include $(RIOTBASE)/Makefile.base diff --git a/pkg/nimble/autoadv/nimble_autoadv.c b/pkg/nimble/autoadv/nimble_autoadv.c index 083659c34e..f22e400029 100644 --- a/pkg/nimble/autoadv/nimble_autoadv.c +++ b/pkg/nimble/autoadv/nimble_autoadv.c @@ -236,7 +236,7 @@ void nimble_autoadv_start(ble_addr_t *addr) #endif } -int static _ble_gap_stop(void) +static int _ble_gap_stop(void) { #if MYNEWT_VAL_BLE_EXT_ADV if (ble_gap_ext_adv_active(CONFIG_NIMBLE_AUTOADV_INSTANCE)) { diff --git a/pkg/nimble/autoadv/shell.c b/pkg/nimble/autoadv/shell.c new file mode 100644 index 0000000000..8ef844263b --- /dev/null +++ b/pkg/nimble/autoadv/shell.c @@ -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 + * + * @} + */ +#include +#include +#include + +#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);