1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

Merge pull request #13506 from HendrikVE/pr/autoadv_transition

examples/nimble_*: use nimble_autoadv module
This commit is contained in:
Francisco 2021-07-21 15:21:01 +02:00 committed by GitHub
commit 7d829f1274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 79 deletions

View File

@ -12,8 +12,10 @@ USEPKG += nimble
USEMODULE += nimble_svc_gap
USEMODULE += nimble_svc_gatt
# We also use the AD part of the BLE helper module
USEMODULE += bluetil_ad
# Use automated advertising
USEMODULE += nimble_autoadv
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"NimBLE GATT Example"'
CFLAGS += -DNIMBLE_AUTOADV_START_MANUALLY=1
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the

View File

@ -37,7 +37,7 @@
#include <string.h>
#include "nimble_riot.h"
#include "net/bluetil/ad.h"
#include "nimble_autoadv.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
@ -66,8 +66,6 @@ static const ble_uuid128_t gatt_svr_chr_rw_demo_readonly_uuid
= BLE_UUID128_INIT(0xaa, 0xf4, 0x82, 0xdd, 0x28, 0xa7, 0xac, 0x86, 0x68,
0x4d, 0xd5, 0x40, 0x3f, 0x11, 0xdd, 0xcc);
static const char *device_name = "NimBLE on RIOT";
static char rm_demo_write_data[64] = "This characteristic is read- and writeable!";
static int gatt_svr_chr_access_device_info_manufacturer(
@ -82,8 +80,6 @@ static int gatt_svr_chr_access_rw_demo(
uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
static void start_advertise(void);
static char str_answer[STR_ANSWER_BUFFER_SIZE];
/* define several bluetooth services for our device */
@ -133,39 +129,6 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
},
};
static int gap_event_cb(struct ble_gap_event *event, void *arg)
{
(void)arg;
switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
if (event->connect.status) {
start_advertise();
}
break;
case BLE_GAP_EVENT_DISCONNECT:
start_advertise();
break;
}
return 0;
}
static void start_advertise(void)
{
struct ble_gap_adv_params advp;
int rc;
memset(&advp, 0, sizeof advp);
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
rc = ble_gap_adv_start(nimble_riot_own_addr_type, NULL, BLE_HS_FOREVER,
&advp, gap_event_cb, NULL);
assert(rc == 0);
(void)rc;
}
static int gatt_svr_chr_access_device_info_manufacturer(
uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg)
@ -320,19 +283,12 @@ int main(void)
assert(rc == 0);
/* set the device name */
ble_svc_gap_device_name_set(device_name);
ble_svc_gap_device_name_set(NIMBLE_AUTOADV_DEVICE_NAME);
/* reload the GATT server to link our added services */
ble_gatts_start();
/* configure and set the advertising data */
uint8_t buf[BLE_HS_ADV_MAX_SZ];
bluetil_ad_t ad;
bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
bluetil_ad_add_name(&ad, device_name);
ble_gap_adv_set_data(ad.buf, ad.pos);
/* start to advertise this node */
start_advertise();
nimble_autoadv_start();
return 0;
}

View File

@ -15,8 +15,10 @@ USEPKG += nimble
USEMODULE += nimble_svc_gap
USEMODULE += nimble_svc_gatt
# We also use the AD part of the BLE helper module
USEMODULE += bluetil_ad
# Use automated advertising
USEMODULE += nimble_autoadv
CFLAGS += -DNIMBLE_AUTOADV_DEVICE_NAME='"RIOT Heart Rate Sensor"'
CFLAGS += -DNIMBLE_AUTOADV_START_MANUALLY=1
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the

View File

@ -14,6 +14,7 @@
* @brief (Mock-up) BLE heart rate sensor example
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
*
* @}
*/
@ -24,6 +25,7 @@
#include "assert.h"
#include "event/timeout.h"
#include "nimble_riot.h"
#include "nimble_autoadv.h"
#include "net/bluetil/ad.h"
#include "timex.h"
@ -40,7 +42,6 @@
#define BPM_STEP (2)
#define BAT_LEVEL (42U)
static const char *_device_name = "RIOT Heart Rate Sensor";
static const char *_manufacturer_name = "Unfit Byte Inc.";
static const char *_model_number = "2A";
static const char *_serial_number = "a8b302c7f3-29183-x8";
@ -70,7 +71,6 @@ static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle,
static int _bas_handler(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
static void _start_advertising(void);
static void _start_updating(void);
static void _stop_updating(void);
@ -215,7 +215,7 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_CONNECT:
if (event->connect.status) {
_stop_updating();
_start_advertising();
nimble_autoadv_start();
return 0;
}
_conn_handle = event->connect.conn_handle;
@ -223,7 +223,7 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_DISCONNECT:
_stop_updating();
_start_advertising();
nimble_autoadv_start();
break;
case BLE_GAP_EVENT_SUBSCRIBE:
@ -241,22 +241,6 @@ static int gap_event_cb(struct ble_gap_event *event, void *arg)
return 0;
}
static void _start_advertising(void)
{
struct ble_gap_adv_params advp;
int res;
memset(&advp, 0, sizeof advp);
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
advp.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
advp.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MAX;
res = ble_gap_adv_start(nimble_riot_own_addr_type, NULL, BLE_HS_FOREVER,
&advp, gap_event_cb, NULL);
assert(res == 0);
(void)res;
}
static void _start_updating(void)
{
event_timeout_set(&_update_timeout_evt, UPDATE_INTERVAL);
@ -312,21 +296,29 @@ int main(void)
assert(res == 0);
/* set the device name */
ble_svc_gap_device_name_set(_device_name);
ble_svc_gap_device_name_set(NIMBLE_AUTOADV_DEVICE_NAME);
/* reload the GATT server to link our added services */
ble_gatts_start();
struct ble_gap_adv_params advp;
memset(&advp, 0, sizeof(advp));
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
advp.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
advp.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MAX;
/* set advertise params */
nimble_autoadv_set_ble_gap_adv_params(&advp);
/* configure and set the advertising data */
uint8_t buf[BLE_HS_ADV_MAX_SZ];
bluetil_ad_t ad;
bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
uint16_t hrs_uuid = BLE_GATT_SVC_HRS;
bluetil_ad_add(&ad, BLE_GAP_AD_UUID16_INCOMP, &hrs_uuid, sizeof(hrs_uuid));
bluetil_ad_add_name(&ad, _device_name);
ble_gap_adv_set_data(ad.buf, ad.pos);
nimble_autoadv_add_field(BLE_GAP_AD_UUID16_INCOMP, &hrs_uuid, sizeof(hrs_uuid));
nimble_auto_adv_set_gap_cb(&gap_event_cb, NULL);
/* start to advertise this node */
_start_advertising();
nimble_autoadv_start();
/* run an event loop for handling the heart rate update events */
event_loop(&_eq);