Wirepas SDK
provisioning_joining_node/app.c
/* Copyright 2026 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief Wirepas Mesh stack provisioning example app
* \note To configure initial node parameters:
* \code{.sh}
* tools/app_setup.py \
* --hexfile build/<target_board>/<app_name>/final_image_<app_name>.hex \
* --device <device, e.g., nRF52840> \
* --nodeAddress 12345 \
* --networkAddress 0xABCDE \
* --networkChannel 5 \
* --nodeRole LL_SUBNODE \
* --networkAuthKey <Network auth key (dummy), 32 hex chars> \
* --networkEncKey <Network encrypt key (dummy), 32 hex chars> \
* --provisionMethod EUID \
* --provisionAuthKey <Provisioning auth key, 32 hex chars> \
* --provisionEncKey <Provisioning encrypt key, 32 hex chars> \
* --provisionRetries 5 \
* --provisionTimeout 30 \
* --authenticatorUidType 1 \
* --authenticatorUid <Authenticator UUID (ver 4), 32 hex chars> \
* --nodeUidType 1 \
* --nodeUid <Node UUID (ver 4), 32 hex chars>
* \endcode
*/
#include "api.h"
#include "button.h" // For Button_register_for_event()
#include "usart.h" // For Usart_enableReceiver(), Usart_setEnabled(),
// Usart_receiverOn()
#include "app_setup.h" // For App_Setup()
#include "provisioning.h" // For Provisioning_start(),
// Provisioning_init_from_storage(),
// provisioning_conf_t
#include "app_scheduler.h" // For App_Scheduler_addTask_execTime(),
// APP_SCHEDULER_STOP_TASK
#define DEBUG_LOG_MODULE_NAME "PROV APP"
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#include "debug_log.h" // For LOG_INIT(), LOG(), LOG_BUFFER(), LOG_LVL_*
static uint32_t start_provisioning(void)
{
LOG(LVL_INFO, "Provisioning starting");
}
const app_lib_joining_received_beacon_t * select_joining_beacon(
const app_lib_joining_received_beacon_t * beacons)
{
const app_lib_joining_received_beacon_t * strongest_beacon = NULL;
int16_t strongest_rssi = INT16_MIN; // RSSI is int8_t, so > INT16_MIN
while (beacons != NULL)
{
if (beacons->rssi > strongest_rssi)
{
strongest_beacon = beacons;
strongest_rssi = beacons->rssi;
}
beacons = beacons->next;
}
return strongest_beacon;
}
void user_data_cb(uint32_t id, CborType type, uint8_t * data, uint8_t len)
{
LOG(LVL_INFO, "User Data, id:%d, type:%d, data:", id, type);
LOG_BUFFER(LVL_DEBUG, data, len);
}
bool end_cb(provisioning_res_e res)
{
LOG(LVL_INFO, "Provisioning ended with result: %d", res);
return true;
}
void button_0_cb(uint8_t button_id, button_event_e event)
{
(void) button_id;
(void) event;
// This function is run in interrupt context, so
// schedule a separate task to start provisioning
App_Scheduler_addTask_execTime(start_provisioning,
2000);
}
void usart_rx_cb(uint8_t * ch, size_t n)
{
if ((n == 1) && (*ch == ' ')) // Space
{
button_0_cb(0, 0);
}
}
void App_init(const app_global_functions_t * functions)
{
(void) functions;
LOG(LVL_INFO, "Starting");
/* Set up initial node configuration */
/* Print node configuration */
app_addr_t node_addr;
lib_settings->getNodeAddress(&node_addr);
lib_settings->getNetworkAddress(&net_addr);
lib_settings->getNetworkChannel(&net_channel);
lib_settings->getNodeRole(&node_role);
LOG(LVL_INFO, "Node configuration:");
LOG(LVL_INFO, " - node addr: %d", node_addr);
LOG(LVL_INFO, " - net addr: 0x%06X", net_addr);
LOG(LVL_INFO, " - net ch: %d", net_channel);
LOG(LVL_INFO, " - node role: 0x%02X", node_role);
= { .end_cb = end_cb,
.user_data_cb = user_data_cb,
.beacon_joining_cb = select_joining_beacon };
/* Load provisioning parameters from secure storage */
/* Start provisioning on button press */
/* Start provisioning on received USART space character */
Usart_enableReceiver(usart_rx_cb);
/* Start the stack */
lib_state->startStack();
}
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb, uint32_t delay_ms, uint32_t exec_time_us)
Add a task.
#define APP_SCHEDULER_SCHEDULE_ASAP
Value to return from task or as initial time to be executed ASAP.
app_setup_res_e App_Setup(void)
Setup configuration from application persistent memory.
Board-independent button functions.
button_event_e
Different events for a button.
Definition button.h:21
@ BUTTON_PRESSED
Definition button.h:22
button_res_e Button_register_for_event(uint8_t button_id, button_event_e event, on_button_event_cb cb)
Register for a button event.
CborType
Definition cbor.h:82
#define LOG_INIT()
Definition debug_log.h:91
#define LOG_BUFFER(level, buffer, size)
Print a buffer if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition debug_log.h:239
#define LVL_DEBUG
Macros to define several log levels: Debug, Info, Warning, Error.
Definition debug_log.h:109
#define LVL_INFO
Definition debug_log.h:110
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition debug_log.h:206
provisioning_res_e
Provisioning result.
provisioning_ret_e Provisioning_start(void)
Start the provisioning process.
provisioning_end_cb_f end_cb
provisioning_ret_e Provisioning_init_from_storage(provisioning_conf_t *conf)
Initialize the provisioning process with provisioning settings from the secure storage.
This structure contains all provisioning related parameters.
void Usart_setEnabled(bool enabled)
Enable or disable USART Enables or disables USART clock and configures GPIO pins.
void Usart_receiverOn(void)
Enable USART Receiver.
void Usart_enableReceiver(serial_rx_callback_f callback)
Enable UART receiver Set callback handler for character RX event Enables interrupt source and event,...
uint32_t app_addr_t
Definition wms_app.h:228
List of global functions, passed to App_entrypoint()
Definition wms_app.h:158
uint32_t app_lib_settings_net_addr_t
Network address type definition.
uint8_t app_lib_settings_role_t
Node role type.
uint8_t app_lib_settings_net_channel_t
Channel type definition.