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

ble/nimble: add scanner submodule

This commit is contained in:
Hauke Petersen 2019-03-26 13:46:25 +01:00
parent f70bba0958
commit 42c6702433
5 changed files with 189 additions and 0 deletions

View File

@ -71,4 +71,7 @@ nimble_addr:
nimble_scanlist:
"$(MAKE)" -C $(TDIR)/scanlist
nimble_scanner:
"$(MAKE)" -C $(TDIR)/scanner
include $(RIOTBASE)/pkg/pkg.mk

View File

@ -76,3 +76,6 @@ endif
ifneq (,$(filter nimble_scanlist,$(USEMODULE)))
INCLUDES += -I$(RIOTPKG)/nimble/scanlist/include
endif
ifneq (,$(filter nimble_scanner,$(USEMODULE)))
INCLUDES += -I$(RIOTPKG)/nimble/scanner/include
endif

View File

@ -0,0 +1,3 @@
MODULE = nimble_scanner
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2019 Freie Universität Berlin
*
* 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.
*/
/**
* @defgroup ble_nimble_scanner NimBLE Scanner Helper
* @ingroup ble_nimble
* @brief Helper module to simplify the usage of NimBLE in scanning mode
* @{
*
* @file
* @brief Scanner abstraction for NimBLE
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/
#ifndef NIMBLE_SCANNER_H
#define NIMBLE_SCANNER_H
#include <stdint.h>
#include "host/ble_hs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Return values used by this submodule
*/
enum {
NIMBLE_SCANNER_OK = 0,
NIMBLE_SCANNER_SCANNING = 1,
NIMBLE_SCANNER_STOPPED = 2,
NIMBLE_SCANNER_ERR = -1,
};
/**
* @brief Callback signature triggered by this module for each discovered
* advertising packet
*/
typedef void(*nimble_scanner_cb)(const ble_addr_t *addr, int8_t rssi,
const uint8_t *ad, size_t ad_len);
/**
* @brief Initialize the scanner module
*
* @param[in] params scan parameters to use, pass NULL to use NimBLE's
* default parameters
* @param[in] disc_cb callback triggered of each received advertising packet
*
* @return NIMBLE_SCANNER_OK on success
* @return NIMBLE_SCANNER_ERR if putting NimBLE into discovery mode failed
*/
int nimble_scanner_init(const struct ble_gap_disc_params *params,
nimble_scanner_cb disc_cb);
/**
* @brief Start scanning using timing parameters configured on initialization
*/
int nimble_scanner_start(void);
/**
* @brief Stop scanning
*/
void nimble_scanner_stop(void);
/**
* @brief Get the current scanning status
*
* @return NIMBLE_SCANNER_SCANNING if currently scanning
* @return NIMBLE_SCANNER_STOPPED if the scanner is stopped
*/
int nimble_scanner_status(void);
#ifdef __cplusplus
}
#endif
#endif /* NIMBLE_SCANNER_H */
/** @} */

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2019 Freie Universität Berlin
*
* 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 ble_nimble_scanner
* @{
*
* @file
* @brief Implementation of a scanner abstraction for NimBLE
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <string.h>
#include "nimble_riot.h"
#include "nimble_scanner.h"
#include "host/ble_gap.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
static nimble_scanner_cb _disc_cb = NULL;
static struct ble_gap_disc_params _scan_params = { 0 };
static int _on_scan_evt(struct ble_gap_event *event, void *arg)
{
/* only interested in the DISC event */
if (event->type == BLE_GAP_EVENT_DISC) {
_disc_cb(&event->disc.addr, event->disc.rssi,
event->disc.data, (size_t)event->disc.length_data);
}
else {
/* this should never happen */
DEBUG("[scanner] unknown event triggered (%i)\n", (int)event->type);
assert(0);
}
return 0;
}
int nimble_scanner_start(void)
{
if (ble_gap_disc_active() == 0) {
int res = ble_gap_disc(nimble_riot_own_addr_type, BLE_HS_FOREVER,
&_scan_params, _on_scan_evt, NULL);
if (res != 0) {
DEBUG("[scanner] err: start failed (%i)\n", res);
return NIMBLE_SCANNER_ERR;
}
}
return NIMBLE_SCANNER_OK;
}
void nimble_scanner_stop(void)
{
if (ble_gap_disc_active() == 1) {
int res = ble_gap_disc_cancel();
/* the above should always succeed */
assert(res == 0);
(void)res;
}
}
int nimble_scanner_status(void)
{
return (ble_gap_disc_active())
? NIMBLE_SCANNER_SCANNING
: NIMBLE_SCANNER_STOPPED;
}
int nimble_scanner_init(const struct ble_gap_disc_params *params,
nimble_scanner_cb disc_cb)
{
assert(disc_cb);
if (params) {
memcpy(&_scan_params, params, sizeof(_scan_params));
}
else {
memset(&_scan_params, 0, sizeof(_scan_params));
}
_disc_cb = disc_cb;
return NIMBLE_SCANNER_OK;
}