Skip to content

Commit

Permalink
pkg/nimble/autoadv: add shell
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed Apr 27, 2022
1 parent 1de00ba commit d474b8f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions 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_%_ext
IGNORE := nimble_autoconn_% nimble_phy_% nimble_%_ext nimble_autoadv%
SUBMODS := $(filter-out $(IGNORE),$(filter nimble_%,$(USEMODULE)))

.PHONY: all
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions pkg/nimble/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions pkg/nimble/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
4 changes: 4 additions & 0 deletions pkg/nimble/autoadv/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
MODULE = nimble_autoadv

SUBMODULES = 1

SRC += nimble_autoadv.c

include $(RIOTBASE)/Makefile.base
2 changes: 1 addition & 1 deletion pkg/nimble/autoadv/nimble_autoadv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
100 changes: 100 additions & 0 deletions pkg/nimble/autoadv/shell.c
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit d474b8f

Please sign in to comment.