1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #16701 from fjmolinas/pr_nimble_scanner_set_duration

pkg/nimble/scanner: add function to set scan duration
This commit is contained in:
Francisco 2021-08-23 10:07:31 +02:00 committed by GitHub
commit 8ca1520342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 9 deletions

View File

@ -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

View File

@ -22,7 +22,8 @@
#include <stdlib.h>
#include <string.h>
#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("");

View File

@ -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

View File

@ -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)
{