Wirepas SDK
local_provisioning/app.c
/* Copyright 2021 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief This file is an application example to use the local_provisioning
* library.
*/
#include "api.h"
#include "button.h"
#include "led.h"
#include "app_scheduler.h"
#include "shared_data.h"
#include "provisioning.h"
#include "stack_state.h"
#define DEBUG_LOG_MODULE_NAME "LOC PROV APP"
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#include "debug_log.h"
// Pre-shared keys, shared with the local_provisioning_tool.py script
static local_provisioning_psk_t m_psk = {
// EXAMPLES KEY ID AND PRESHARED KEY TO BE REPLACED
// In production applications, store these in secure storage instead of
// hardcoding here.
// See wms_secure_storage.h. Hardcoding results in the data being stored as
// plaintext on the flash.
// 4 bytes id
.id = 0x12345678,
// 32 bytes psk
.psk = {0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
0xd1, 0xd2, 0xd3, 0xd4, 0xd5}
};
// Led id depends if node is joining or proxy
static uint8_t m_led_id = 0;
static uint32_t send_data(void)
{
static uint32_t id = 0; // Value to send
// Create a data packet to send
app_lib_data_to_send_t data_to_send = {
.bytes = (const uint8_t *) &id,
.num_bytes = sizeof(id),
.dest_address = APP_ADDR_ANYSINK,
.src_endpoint = 26, // As an example
.dest_endpoint = 26,
};
// Send the data packet
Shared_Data_sendData(&data_to_send, NULL);
// Increment value to send
id++;
return 10*1000;
}
static uint32_t blink_led(void)
{
Led_toggle(m_led_id);
return 500;
}
static void on_prov_proxy_enabled_cb(bool enabled)
{
if (enabled)
{
}
else
{
Led_set(m_led_id, true);
}
}
static bool on_joining_end_cb(bool success)
{
(void) success;
Led_set(m_led_id, true);
// We don't want to reboot at the end
return false;
}
static void button_start_joining_cb(uint8_t button_id, button_event_e event)
{
(void) button_id;
(void) event;
{
// Blink Led 0 to see that node is trying to join.
// It will end automatically as it will finish with a reboot
}
}
static void button_reset_cb(uint8_t button_id, button_event_e event)
{
(void) button_id;
(void) event;
LOG(LVL_INFO, "Perform reset...");
}
void App_init(const app_global_functions_t * functions)
{
(void) functions;
LOG(LVL_INFO, "Starting");
Local_provisioning_init(&m_psk, on_prov_proxy_enabled_cb);
{
m_led_id = 1;
// Node is provisioned, enable proxy mode
// Register button 1 to reset node config
// Once provisioned, this app send periodically data as a demo
}
else
{
m_led_id = 0;
Button_register_for_event(0, BUTTON_PRESSED, button_start_joining_cb);
}
Led_set(m_led_id, true);
}
local_provisioning.h
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
Led_set
led_res_e Led_set(uint8_t led_id, bool state)
Turn the given LED on or off.
Local_provisioning_is_provisioned
bool Local_provisioning_is_provisioned()
Is the node provisioned A node is considered provisioned if it has a valid config with network securi...
button.h
Board-independent button functions.
Local_provisioning_reset_node
void Local_provisioning_reset_node()
Reset all node settings.
stack_state.h
App_Scheduler_addTask_execTime
app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb, uint32_t delay_ms, uint32_t exec_time_us)
Add a task.
Stack_State_startStack
app_res_e Stack_State_startStack()
Start the stack.
Led_toggle
led_res_e Led_toggle(uint8_t led_id)
Toggle the given LED.
local_provisioning_psk_t
Pre shared key structure.
Definition: local_provisioning.h:31
app_scheduler.h
app_lib_data_to_send_t
A struct for lib_data->sendData().
Definition: wms_data.h:323
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: wms_app.h:157
debug_log.h
Local_provisioning_init
Local_provisioning_res_e Local_provisioning_init(local_provisioning_psk_t *psk, local_provisioning_proxy_enabled_cb on_proxy_enabled_cb)
Initialize the Local provisioning library.
Local_provisioning_start_joining
Local_provisioning_res_e Local_provisioning_start_joining(local_provisioning_joining_beacon_selection_f cb_beacons, local_provisioning_joining_done_cb cb_end)
Start a joining session Node will start a joining request.
APP_LIB_DATA_QOS_HIGH
@ APP_LIB_DATA_QOS_HIGH
Definition: wms_data.h:87
Button_register_for_event
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.
led.h
Board-independent LED functions.
App_Scheduler_cancelTask
app_scheduler_res_e App_Scheduler_cancelTask(task_cb_f cb)
Cancel a task.
APP_ADDR_ANYSINK
@ APP_ADDR_ANYSINK
Definition: wms_app.h:236
LOG_INIT
#define LOG_INIT()
Definition: debug_log.h:66
shared_data.h
LOG
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition: debug_log.h:173
Shared_Data_sendData
app_lib_data_send_res_e Shared_Data_sendData(app_lib_data_to_send_t *data, app_lib_data_data_sent_cb_f sent_cb)
Send data. The packet to send is represented as a app_lib_data_to_send_t struct.
app_lib_data_to_send_t::bytes
const uint8_t * bytes
Definition: wms_data.h:326
BUTTON_PRESSED
@ BUTTON_PRESSED
Definition: button.h:22
APP_SCHEDULER_SCHEDULE_ASAP
#define APP_SCHEDULER_SCHEDULE_ASAP
Value to return from task or as initial time to be executed ASAP.
Definition: app_scheduler.h:42
local_provisioning_psk_t::id
uint32_t id
Definition: local_provisioning.h:32
provisioning.h
LOCAL_PROVISIONING_RES_SUCCESS
@ LOCAL_PROVISIONING_RES_SUCCESS
Definition: local_provisioning.h:20
api.h
button_event_e
button_event_e
Different events for a button.
Definition: button.h:21