From 20243a26a2e272599543b2386589ceceeba6a6e8 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sun, 14 Mar 2021 11:06:45 +0100 Subject: [PATCH] Extracted BLE to src/prst/ble.{c,h} --- code/b-parasite/Makefile | 3 +- code/b-parasite/config/prst_config.h | 4 + code/b-parasite/src/main.c | 127 +++------------------------ code/b-parasite/src/prst/ble.c | 120 +++++++++++++++++++++++++ code/b-parasite/src/prst/ble.h | 16 ++++ code/b-parasite/src/{ => prst}/rtc.c | 2 +- code/b-parasite/src/{ => prst}/rtc.h | 0 7 files changed, 153 insertions(+), 119 deletions(-) create mode 100644 code/b-parasite/src/prst/ble.c create mode 100644 code/b-parasite/src/prst/ble.h rename code/b-parasite/src/{ => prst}/rtc.c (98%) rename code/b-parasite/src/{ => prst}/rtc.h (100%) diff --git a/code/b-parasite/Makefile b/code/b-parasite/Makefile index 75cfcfe..a76db5b 100644 --- a/code/b-parasite/Makefile +++ b/code/b-parasite/Makefile @@ -61,7 +61,8 @@ SRC_FILES += \ $(SDK_ROOT)/components/softdevice/common/nrf_sdh.c \ $(SDK_ROOT)/components/softdevice/common/nrf_sdh_ble.c \ $(SDK_ROOT)/components/softdevice/common/nrf_sdh_soc.c \ - $(PROJ_DIR)/rtc.c \ + $(PROJ_DIR)/prst/ble.c \ + $(PROJ_DIR)/prst/rtc.c \ # Include folders common to all targets INC_FOLDERS += \ diff --git a/code/b-parasite/config/prst_config.h b/code/b-parasite/config/prst_config.h index 74d640c..d1c4936 100644 --- a/code/b-parasite/config/prst_config.h +++ b/code/b-parasite/config/prst_config.h @@ -1,6 +1,10 @@ #ifndef _PRST_CONFIG_H_ #define _PRST_CONFIG_H_ +// Deep sleep. #define PRST_DEEP_SLEEP_IN_SECONDS 2 +// BLE. +#define PRST_BLE_ADV_NAME "Prst" + #endif // _PRST_CONFIG_H_ \ No newline at end of file diff --git a/code/b-parasite/src/main.c b/code/b-parasite/src/main.c index 3e11ad2..546c0cc 100644 --- a/code/b-parasite/src/main.c +++ b/code/b-parasite/src/main.c @@ -15,7 +15,8 @@ #include "nrf_sdh.h" #include "nrf_sdh_ble.h" #include "nrf_soc.h" -#include "rtc.h" +#include "prst/ble.h" +#include "prst/rtc.h" // P0.03 // #define LED_PIN 3 @@ -26,113 +27,10 @@ #define DEAD_BEEF 0xDEADBEEF -#define NAME "Parasite" -#define NAME_LEN 8 - -#define SERVICE_DATA_LEN 8 -static uint8_t service_data[SERVICE_DATA_LEN]; - -#define APP_BLE_CONN_CFG_TAG 1 - -// Seconds between RTC 2 events. -#define COMPARE_COUNTERTIME (1UL) - -// RTC0 is used by softdevice, so we need to pick another instance. -const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); - -static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the - stack when starting advertising. */ -static uint8_t m_adv_handle = - BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an - advertising set. */ - -#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) - -static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; - -static ble_gap_adv_data_t m_adv_data = { - .adv_data = {.p_data = m_enc_advdata, .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX}, - .scan_rsp_data = {.p_data = NULL, .len = 0 - - }}; - void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { app_error_handler(DEAD_BEEF, line_num, p_file_name); } -static void advertising_init(void) { - uint32_t err_code; - ble_advdata_t advdata; - // Build and set advertising data. - memset(&advdata, 0, sizeof(advdata)); - - uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED; - - 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; - - // Initialize advertising parameters (used when starting advertising). - memset(&m_adv_params, 0, sizeof(m_adv_params)); - - m_adv_params.properties.type = - BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED; - m_adv_params.p_peer_addr = NULL; // Undirected advertisement. - m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY; - m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL; - m_adv_params.duration = 0; // Never time out. - - 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, NAME_LEN); - - err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, - &m_adv_data.adv_data.len); - APP_ERROR_CHECK(err_code); - - err_code = - sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params); - APP_ERROR_CHECK(err_code); -} - -static void advertising_start(void) { - ret_code_t err_code; - - err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG); - APP_ERROR_CHECK(err_code); - NRF_LOG_INFO("Advertising started\n.") -} - -static void advertising_stop(void) { - ret_code_t err_code; - err_code = sd_ble_gap_adv_stop(m_adv_handle); - APP_ERROR_CHECK(err_code); -} - -static void ble_stack_init(void) { - ret_code_t err_code; - - err_code = nrf_sdh_enable_request(); - APP_ERROR_CHECK(err_code); - - // Configure the BLE stack using the default settings. - // Fetch the start address of the application RAM. - uint32_t ram_start = 0; - err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); - APP_ERROR_CHECK(err_code); - - // Enable BLE stack. - err_code = nrf_sdh_ble_enable(&ram_start); - APP_ERROR_CHECK(err_code); -} - static void log_init(void) { ret_code_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); @@ -155,20 +53,15 @@ static void timers_init(void) { APP_ERROR_CHECK(err_code); } +#define FPU_EXCEPTION_MASK 0x0000009F + static void power_management_init(void) { ret_code_t err_code; err_code = nrf_pwr_mgmt_init(); APP_ERROR_CHECK(err_code); } -static void idle_state_handle(void) { - if (NRF_LOG_PROCESS() == false) { - nrf_pwr_mgmt_run(); - } -} - -#define FPU_EXCEPTION_MASK 0x0000009F - +// static void power_manage(void) { __set_FPSCR(__get_FPSCR() & ~(FPU_EXCEPTION_MASK)); (void)__get_FPSCR(); @@ -180,9 +73,9 @@ static void rtc_callback() { NRF_LOG_INFO("rtc callback running...\n"); NRF_LOG_FLUSH(); nrf_gpio_pin_set(LED_PIN); - advertising_start(); - nrf_delay_ms(500); - advertising_stop(); + prst_adv_start(); + nrf_delay_ms(300); + prst_adv_stop(); nrf_gpio_pin_clear(LED_PIN); } @@ -191,8 +84,8 @@ int main(void) { log_init(); leds_init(); power_management_init(); - ble_stack_init(); - advertising_init(); + prst_ble_init(); + prst_ble_update_adv_data(1); prst_rtc_set_callback(rtc_callback); prst_rtc_init(); diff --git a/code/b-parasite/src/prst/ble.c b/code/b-parasite/src/prst/ble.c new file mode 100644 index 0000000..ec1dacd --- /dev/null +++ b/code/b-parasite/src/prst/ble.c @@ -0,0 +1,120 @@ +#include "prst/ble.h" + +#include +#include +#include +#include +#include + +#include "prst_config.h" + +// Environmental sensing. +#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) + +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_ = { + .adv_data = {.p_data = encoded_adv_data_, + .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX}, + .scan_rsp_data = {.p_data = NULL, .len = 0}}; + +// 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; + +// Advertisement parameters. +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; + + // We'll just broadcast our data, so we disallow connections and scan + // requests. + adv_params_.properties.type = + BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED; + + // No particular peer - undirected advertisement. + adv_params_.p_peer_addr = NULL; + adv_params_.filter_policy = BLE_GAP_ADV_FP_ANY; + adv_params_.interval = NON_CONNECTABLE_ADV_INTERVAL; + adv_params_.duration = 0; // Never time out. + + 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)); + + // 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); + APP_ERROR_CHECK(err_code); +} + +void prst_ble_init() { + uint32_t err_code; + + // Enable SoftDevice request. + err_code = nrf_sdh_enable_request(); + APP_ERROR_CHECK(err_code); + + // Set the default config and get the starting RAM address. + uint32_t ram_start = 0; + err_code = nrf_sdh_ble_default_cfg_set(PRST_CONN_CFG_TAG, &ram_start); + APP_ERROR_CHECK(err_code); + + // Enable SoftDevice. + err_code = nrf_sdh_ble_enable(&ram_start); + APP_ERROR_CHECK(err_code); + + init_advertisement_data(); +} + +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_); + APP_ERROR_CHECK(err_code); +} + +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."); +} + +void prst_adv_stop() { + ret_code_t err_code; + err_code = sd_ble_gap_adv_stop(adv_handle_); + APP_ERROR_CHECK(err_code); +} \ No newline at end of file diff --git a/code/b-parasite/src/prst/ble.h b/code/b-parasite/src/prst/ble.h new file mode 100644 index 0000000..bb6e1f9 --- /dev/null +++ b/code/b-parasite/src/prst/ble.h @@ -0,0 +1,16 @@ +#ifndef _PRST_BLE_H_ +#define _PRST_BLE_H_ + +#include +#include + +// Initializes SoftDevice. +void prst_ble_init(); + +void prst_adv_start(); + +void prst_adv_stop(); + +void prst_ble_update_adv_data(uint8_t n); + +#endif // _PRST_BLE_H_ \ No newline at end of file diff --git a/code/b-parasite/src/rtc.c b/code/b-parasite/src/prst/rtc.c similarity index 98% rename from code/b-parasite/src/rtc.c rename to code/b-parasite/src/prst/rtc.c index 2e0272b..2f0db11 100644 --- a/code/b-parasite/src/rtc.c +++ b/code/b-parasite/src/prst/rtc.c @@ -1,4 +1,4 @@ -#include "rtc.h" +#include "prst/rtc.h" #include #include diff --git a/code/b-parasite/src/rtc.h b/code/b-parasite/src/prst/rtc.h similarity index 100% rename from code/b-parasite/src/rtc.h rename to code/b-parasite/src/prst/rtc.h