From 4d01b3815f5a85e6ae905c3cb953c63af56f5197 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Tue, 3 Aug 2021 11:53:53 +0200 Subject: [PATCH 1/2] pkg/nimble/scanner: add function to set scan duration Co-authored-by: Roudy Dagher --- pkg/nimble/scanner/include/nimble_scanner.h | 12 ++++++++++++ pkg/nimble/scanner/nimble_scanner.c | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/nimble/scanner/include/nimble_scanner.h b/pkg/nimble/scanner/include/nimble_scanner.h index e896c00fe74a..fca8aa4b5c26 100644 --- a/pkg/nimble/scanner/include/nimble_scanner.h +++ b/pkg/nimble/scanner/include/nimble_scanner.h @@ -68,6 +68,9 @@ int nimble_scanner_init(const struct ble_gap_disc_params *params, /** * @brief Start scanning using timing parameters configured on initialization + * + * @note Scanning will run for ever unless stopped or unless a different + * scan duration is set with @ref nimble_scanner_set_scan_duration */ int nimble_scanner_start(void); @@ -84,6 +87,15 @@ void nimble_scanner_stop(void); */ int nimble_scanner_status(void); +/** + * @brief Set the duration for the scanning procedure. + * + * If there is an active scanning process, it will be restarted. + * + * @param[in] duration_ms duration of scanning procedure in ms + */ +void nimble_scanner_set_scan_duration(int32_t duration_ms); + #ifdef __cplusplus } #endif diff --git a/pkg/nimble/scanner/nimble_scanner.c b/pkg/nimble/scanner/nimble_scanner.c index c81e7b75e6b8..b1e6ca8e37b8 100644 --- a/pkg/nimble/scanner/nimble_scanner.c +++ b/pkg/nimble/scanner/nimble_scanner.c @@ -32,6 +32,9 @@ static nimble_scanner_cb _disc_cb = NULL; static struct ble_gap_disc_params _scan_params = { 0 }; +/* duration of the scanning procedure */ +static int32_t _scan_duration = BLE_HS_FOREVER; + static int _on_scan_evt(struct ble_gap_event *event, void *arg) { /* only interested in the DISC event */ @@ -39,6 +42,9 @@ static int _on_scan_evt(struct ble_gap_event *event, void *arg) _disc_cb(event->disc.event_type, &event->disc.addr, event->disc.rssi, event->disc.data, (size_t)event->disc.length_data); } + else if (event->type == BLE_GAP_EVENT_DISC_COMPLETE) { + DEBUG("[scanner] scan cycle completed\n"); + } else { /* this should never happen */ DEBUG("[scanner] unknown event triggered (%i)\n", (int)event->type); @@ -51,7 +57,7 @@ static int _on_scan_evt(struct ble_gap_event *event, void *arg) int nimble_scanner_start(void) { if (ble_gap_disc_active() == 0) { - int res = ble_gap_disc(nimble_riot_own_addr_type, BLE_HS_FOREVER, + int res = ble_gap_disc(nimble_riot_own_addr_type, _scan_duration, &_scan_params, _on_scan_evt, NULL); if (res != 0) { DEBUG("[scanner] err: start failed (%i)\n", res); @@ -79,6 +85,15 @@ int nimble_scanner_status(void) : NIMBLE_SCANNER_STOPPED; } +void nimble_scanner_set_scan_duration(int32_t duration_ms) +{ + _scan_duration = duration_ms; + if (ble_gap_disc_active()) { + nimble_scanner_stop(); + nimble_scanner_start(); + } +} + int nimble_scanner_init(const struct ble_gap_disc_params *params, nimble_scanner_cb disc_cb) { From 070b61ff489127ed821f34c62c0db0f274c79714 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Tue, 3 Aug 2021 11:54:25 +0200 Subject: [PATCH 2/2] examples/nimble_scanner: use nimble_scanner_ser_scan_duration Replace xtimer by ztimer Co-authored-by: Roudy Dagher --- examples/nimble_scanner/Makefile | 1 - examples/nimble_scanner/main.c | 15 ++++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/nimble_scanner/Makefile b/examples/nimble_scanner/Makefile index b43bcb756922..3bda95e19b41 100644 --- a/examples/nimble_scanner/Makefile +++ b/examples/nimble_scanner/Makefile @@ -8,7 +8,6 @@ BOARD ?= nrf52dk RIOTBASE ?= $(CURDIR)/../.. # We use the xtimer and the shell in this example -USEMODULE += xtimer USEMODULE += shell # configure and use Nimble diff --git a/examples/nimble_scanner/main.c b/examples/nimble_scanner/main.c index 82c2314db965..f7ca6db35cfc 100644 --- a/examples/nimble_scanner/main.c +++ b/examples/nimble_scanner/main.c @@ -22,7 +22,8 @@ #include #include -#include "xtimer.h" +#include "timex.h" +#include "ztimer.h" #include "shell.h" #include "shell_commands.h" @@ -30,25 +31,25 @@ #include "nimble_scanlist.h" /* default scan duration (1s) */ -#define DEFAULT_DURATION (1000000U) +#define DEFAULT_DURATION_MS (1 * MS_PER_SEC) int _cmd_scan(int argc, char **argv) { - uint32_t timeout = DEFAULT_DURATION; + uint32_t timeout = DEFAULT_DURATION_MS; if ((argc == 2) && (memcmp(argv[1], "help", 4) == 0)) { printf("usage: %s [timeout in ms]\n", argv[0]); return 0; } if (argc >= 2) { - timeout = (uint32_t)(atoi(argv[1]) * 1000); + timeout = atoi(argv[1]); } nimble_scanlist_clear(); - printf("Scanning for %ums now ...", (unsigned)(timeout / 1000)); + printf("Scanning for %"PRIu32" ms now ...", timeout); + nimble_scanner_set_scan_duration(timeout); nimble_scanner_start(); - xtimer_usleep(timeout); - nimble_scanner_stop(); + ztimer_sleep(ZTIMER_MSEC, timeout); puts(" done\n\nResults:"); nimble_scanlist_print(); puts("");