Wirepas SDK
custom_app/app.c
/* Copyright 2017 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief This file is a template for writing a custom application
*/
#include <stdlib.h>
#include "api.h"
#define DEFAULT_PERIOD_S 10
#define DEFAULT_PERIOD_US (DEFAULT_PERIOD_S*1000*1000)
#define EXECUTION_TIME_US 500
#define SET_PERIOD_EP 10
#define DATA_EP 1
static uint32_t period_us;
static uint32_t get_value_from_string(const uint8_t * bytes,
size_t num_bytes)
{
uint32_t value = 0;
while ((num_bytes--) > 0)
{
char c = (char)(*bytes++);
if ((c < '0') || (c > '9'))
{
// Not a digit
break;
}
value *= 10;
value += c - '0';
}
return value;
}
static uint32_t send_data(void)
{
// This function will be called every period_us microseconds by the stack.
// You can do anything you want for EXECUTION_TIME_US. In this example, a
// monotonically increasing 32-bit value is sent to the sink.
static uint32_t id = 0; // Value to send
// Create a data packet to send
app_lib_data_to_send_t data_to_send;
data_to_send.bytes = (const uint8_t *) &id;
data_to_send.num_bytes = sizeof(id);
data_to_send.src_endpoint = DATA_EP;
data_to_send.dest_endpoint = DATA_EP;
data_to_send.qos = APP_LIB_DATA_QOS_HIGH;
data_to_send.delay = 0;
// Send the data packet
lib_data->sendData(&data_to_send);
// Increment value to send
id++;
// Inform the stack that this function should be called again in
// period_us microseconds. By returning APP_LIB_SYSTEM_STOP_PERIODIC,
// the stack won't call this function again.
return period_us;
}
static app_lib_data_receive_res_e dataReceivedCb(
{
if ((data->num_bytes < 1) ||
(data->dest_endpoint != SET_PERIOD_EP))
{
// Data was not for this application
}
// Parse decimal digits to a period value
uint32_t new_period = get_value_from_string(data->bytes, data->num_bytes);
period_us = (new_period > 0) ? new_period * 1000 * 1000 :
// Update the period now. The current period will be longer
// (already elapsed time from last period + new full period).
lib_system->setPeriodicCb(send_data,
period_us,
EXECUTION_TIME_US);
// Data handled successfully
}
void App_init(const app_global_functions_t * functions)
{
// Basic configuration of the node with a unique node address
{
// Could not configure the node
// It should not happen except if one of the config value is invalid
return;
}
// Set a periodic callback to be called after DEFAULT_PERIOD_US
period_us = DEFAULT_PERIOD_US;
lib_system->setPeriodicCb(send_data,
period_us,
EXECUTION_TIME_US);
// Set callback for received unicast messages
lib_data->setDataReceivedCb(dataReceivedCb);
// Set callback for received broadcast messages
lib_data->setBcastDataReceivedCb(dataReceivedCb);
// Start the stack
lib_state->startStack();
}
app_lib_data_to_send_t::num_bytes
size_t num_bytes
Definition: data.h:293
app_lib_data_to_send_t
A struct for lib_data->sendData().
Definition: data.h:288
node_configuration.h
app_lib_data_to_send_t::dest_address
app_addr_t dest_address
Definition: data.h:295
APP_RES_OK
@ APP_RES_OK
Definition: app.h:204
app_lib_data_to_send_t::tracking_id
app_lib_data_tracking_id_t tracking_id
Definition: data.h:303
app_lib_data_to_send_t::qos
app_lib_data_qos_e qos
Definition: data.h:305
APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
@ APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
Definition: data.h:191
APP_LIB_DATA_RECEIVE_RES_HANDLED
@ APP_LIB_DATA_RECEIVE_RES_HANDLED
Definition: data.h:188
app_lib_data_to_send_t::delay
uint32_t delay
Definition: data.h:299
configureNodeFromBuildParameters
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
Definition: node_configuration.h:233
app_lib_data_to_send_t::flags
uint8_t flags
Definition: data.h:307
app_lib_data_receive_res_e
app_lib_data_receive_res_e
Return value of data reception callback.
Definition: data.h:184
app_lib_data_to_send_t::src_endpoint
uint8_t src_endpoint
Definition: data.h:309
app_lib_data_to_send_t::dest_endpoint
uint8_t dest_endpoint
Definition: data.h:311
app_lib_data_to_send_t::bytes
const uint8_t * bytes
Definition: data.h:291
APP_LIB_SYSTEM_STOP_PERIODIC
#define APP_LIB_SYSTEM_STOP_PERIODIC
Constant to stop periodic callback.
Definition: system.h:46
APP_ADDR_ANYSINK
@ APP_ADDR_ANYSINK
Definition: app.h:236
APP_LIB_DATA_NO_TRACKING_ID
#define APP_LIB_DATA_NO_TRACKING_ID
When sending data and no tracking of packet is requested, this ID may be used.
Definition: data.h:60
app_lib_data_received_t
Struct passed to data reception callback functions.
Definition: data.h:244
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: app.h:157
APP_LIB_DATA_QOS_HIGH
@ APP_LIB_DATA_QOS_HIGH
Definition: data.h:93
APP_LIB_DATA_SEND_FLAG_NONE
@ APP_LIB_DATA_SEND_FLAG_NONE
Definition: data.h:102
api.h