Adds dynamic data to ble advertisement
I am very tempted to drop all the ble advertisement abstraction and deal directly with the 31-byte raw payload.
This commit is contained in:
parent
20243a26a2
commit
cf2725e028
3 changed files with 41 additions and 42 deletions
|
|
@ -217,7 +217,7 @@ CFLAGS += -DSOFTDEVICE_PRESENT
|
|||
CFLAGS += -DEBUG
|
||||
CFLAGS += -mcpu=cortex-m4
|
||||
CFLAGS += -mthumb -mabi=aapcs
|
||||
# CFLAGS += -Wall -Werror
|
||||
CFLAGS += -Wall -Werror
|
||||
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
|
||||
# keep every function in a separate section, this allows linker to discard unused ones
|
||||
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) {
|
|||
static void log_init(void) {
|
||||
ret_code_t err_code = NRF_LOG_INIT(NULL);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
NRF_LOG_DEFAULT_BACKENDS_INIT();
|
||||
NRF_LOG_INFO("Log inited");
|
||||
}
|
||||
|
|
@ -48,11 +47,6 @@ static void leds_init(void) {
|
|||
NRF_LOG_INFO("Leds inited");
|
||||
}
|
||||
|
||||
static void timers_init(void) {
|
||||
ret_code_t err_code = app_timer_init();
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
#define FPU_EXCEPTION_MASK 0x0000009F
|
||||
|
||||
static void power_management_init(void) {
|
||||
|
|
@ -61,7 +55,6 @@ static void power_management_init(void) {
|
|||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
//
|
||||
static void power_manage(void) {
|
||||
__set_FPSCR(__get_FPSCR() & ~(FPU_EXCEPTION_MASK));
|
||||
(void)__get_FPSCR();
|
||||
|
|
@ -69,10 +62,12 @@ static void power_manage(void) {
|
|||
nrf_pwr_mgmt_run();
|
||||
}
|
||||
|
||||
static uint8_t data;
|
||||
static void rtc_callback() {
|
||||
NRF_LOG_INFO("rtc callback running...\n");
|
||||
NRF_LOG_FLUSH();
|
||||
nrf_gpio_pin_set(LED_PIN);
|
||||
prst_ble_update_adv_data(++data);
|
||||
prst_adv_start();
|
||||
nrf_delay_ms(300);
|
||||
prst_adv_stop();
|
||||
|
|
@ -85,7 +80,6 @@ int main(void) {
|
|||
leds_init();
|
||||
power_management_init();
|
||||
prst_ble_init();
|
||||
prst_ble_update_adv_data(1);
|
||||
|
||||
prst_rtc_set_callback(rtc_callback);
|
||||
prst_rtc_init();
|
||||
|
|
|
|||
|
|
@ -3,35 +3,53 @@
|
|||
#include <ble_advdata.h>
|
||||
#include <ble_gap.h>
|
||||
#include <nordic_common.h>
|
||||
#include <nrf_log.h>
|
||||
#include <nrf_sdh.h>
|
||||
#include <nrf_sdh_ble.h>
|
||||
|
||||
#include "prst_config.h"
|
||||
|
||||
// Environmental sensing.
|
||||
// We need to pick a service UUID for broadcasting our sensor data.
|
||||
// 0x181a is defined as "environmental sensing", which seems appopriate.
|
||||
#define SERVICE_UUID 0x181a
|
||||
|
||||
#define NAME "Prst"
|
||||
|
||||
#define SERVICE_DATA_LEN 8
|
||||
|
||||
// The connection to configure. We only have the one.
|
||||
#define PRST_CONN_CFG_TAG 1
|
||||
|
||||
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS)
|
||||
|
||||
// Sensor data payload that will go into the advertisement message.
|
||||
#define SERVICE_DATA_LEN 8
|
||||
static uint8_t service_data[SERVICE_DATA_LEN];
|
||||
|
||||
// Stores the encoded advertisement data. As per BLE spec, 31 bytes max.
|
||||
static uint8_t encoded_adv_data_[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
|
||||
|
||||
// Structure holding high level advertisement data and contains a pointer to
|
||||
// the actual advertised bytes.
|
||||
static ble_gap_adv_data_t adv_data_ = {
|
||||
// the actual encoded advertised bytes.
|
||||
static ble_gap_adv_data_t gap_adv_data_ = {
|
||||
.adv_data = {.p_data = encoded_adv_data_,
|
||||
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX},
|
||||
.scan_rsp_data = {.p_data = NULL, .len = 0}};
|
||||
|
||||
// We'll put our sensor data inside an advertisement service.
|
||||
static ble_advdata_service_data_t advdata_service_data_ = {
|
||||
.service_uuid = SERVICE_UUID,
|
||||
.data = {
|
||||
.p_data = service_data,
|
||||
.size = SERVICE_DATA_LEN,
|
||||
}};
|
||||
|
||||
// Holds the service data to be broadcasted. The contents of this struct
|
||||
// will be encoded into gap_adv_data.
|
||||
// Warning: do not update this while advertising.
|
||||
static ble_advdata_t adv_data_ = {
|
||||
.name_type = BLE_ADVDATA_FULL_NAME,
|
||||
.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED,
|
||||
.p_service_data_array = &advdata_service_data_,
|
||||
.service_data_count = 1,
|
||||
};
|
||||
|
||||
// NRF supports multiple advertisement sets. This initialization is a request
|
||||
// for configuring a new one.
|
||||
static uint8_t adv_handle_ = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
||||
|
|
@ -40,25 +58,8 @@ static uint8_t adv_handle_ = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
|||
static ble_gap_adv_params_t adv_params_;
|
||||
|
||||
static void init_advertisement_data() {
|
||||
// Build advertisement data.
|
||||
ble_advdata_t advdata;
|
||||
memset(&advdata, 0, sizeof(advdata));
|
||||
// memset(&adv_params_, 0, sizeof(adv_params_));
|
||||
|
||||
uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
|
||||
|
||||
// Service data represents a service UUID (2 bytes), followed by
|
||||
// SERVICE_DATA_LEN bytes of data from our sensors.
|
||||
ble_advdata_service_data_t advdata_service_data;
|
||||
advdata_service_data.service_uuid = SERVICE_UUID;
|
||||
advdata_service_data.data.p_data = service_data;
|
||||
advdata_service_data.data.size = SERVICE_DATA_LEN;
|
||||
|
||||
advdata.name_type = BLE_ADVDATA_FULL_NAME;
|
||||
advdata.flags = flags;
|
||||
|
||||
advdata.p_service_data_array = &advdata_service_data;
|
||||
advdata.service_data_count = 1;
|
||||
UNUSED_VARIABLE(adv_data_);
|
||||
UNUSED_VARIABLE(advdata_service_data_);
|
||||
|
||||
// We'll just broadcast our data, so we disallow connections and scan
|
||||
// requests.
|
||||
|
|
@ -73,11 +74,11 @@ static void init_advertisement_data() {
|
|||
|
||||
ble_gap_conn_sec_mode_t sec_mode;
|
||||
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
|
||||
sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)NAME, strlen(NAME));
|
||||
sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)PRST_BLE_ADV_NAME,
|
||||
strlen(PRST_BLE_ADV_NAME));
|
||||
|
||||
// Encodes the advdata into encoded_adv_data.
|
||||
uint32_t err_code = ble_advdata_encode(&advdata, adv_data_.adv_data.p_data,
|
||||
&adv_data_.adv_data.len);
|
||||
uint32_t err_code =
|
||||
sd_ble_gap_adv_set_configure(&adv_handle_, &gap_adv_data_, &adv_params_);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
|
|
@ -101,8 +102,11 @@ void prst_ble_init() {
|
|||
}
|
||||
|
||||
void prst_ble_update_adv_data(uint8_t n) {
|
||||
uint32_t err_code =
|
||||
sd_ble_gap_adv_set_configure(&adv_handle_, &adv_data_, &adv_params_);
|
||||
service_data[0] = n;
|
||||
|
||||
// Encodes adv_data_ into .gap_adv_data_.
|
||||
uint32_t err_code = ble_advdata_encode(
|
||||
&adv_data_, gap_adv_data_.adv_data.p_data, &gap_adv_data_.adv_data.len);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
|
|
@ -110,11 +114,12 @@ void prst_adv_start() {
|
|||
ret_code_t err_code;
|
||||
err_code = sd_ble_gap_adv_start(adv_handle_, PRST_CONN_CFG_TAG);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
// NRF_LOG_INFO("Advertising started\n.");
|
||||
NRF_LOG_INFO("Advertising started.\n");
|
||||
}
|
||||
|
||||
void prst_adv_stop() {
|
||||
ret_code_t err_code;
|
||||
err_code = sd_ble_gap_adv_stop(adv_handle_);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
NRF_LOG_INFO("Advertising stopped.\n");
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue