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"
#include "shared_data.h"
#include "app_scheduler.h"
#define DEBUG_LOG_MODULE_NAME "CUSTOM_APP"
#define DEBUG_LOG_MAX_LEVEL LVL_NOLOG
#include "debug_log.h"
#define DEFAULT_PERIOD_S 10
#define DEFAULT_PERIOD_MS (DEFAULT_PERIOD_S*1000)
#define EXECUTION_TIME_US 500
#define SET_PERIOD_EP 10
#define DATA_EP 1
static uint32_t period_ms;
typedef struct __attribute__((packed))
{
uint32_t period_ms;
} payload_periodic_t;
static uint32_t send_data_task(void)
{
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;
// Send the data packet
Shared_Data_sendData(&data_to_send, NULL);
// Increment value to send
id++;
// Inform the stack that this function should be called again in
// period_ms microseconds. By returning APP_SCHEDULER_STOP_TASK,
// the stack won't call this function again.
return period_ms;
}
static app_lib_data_receive_res_e dataReceivedCb(
const shared_data_item_t * item,
{
LOG(LVL_INFO, "dataReceivedCb");
if ((data->num_bytes < 1) || (data->num_bytes!= sizeof(payload_periodic_t)))
{
// Data was not for this application
LOG(LVL_INFO, "dataReceiveCb - data was not for this application");
}
LOG(LVL_INFO, "dataReceivedCb");
payload_periodic_t payload = *((payload_periodic_t *)data->bytes);
/* Change the period for a new one */
period_ms = payload.period_ms;
// Data handled successfully
}
/*Unicast messages filter*/
static shared_data_item_t alltype_packets_filter =
{
.cb = dataReceivedCb,
.filter = {
/* Filtering by source endpoint. */
.src_endpoint = SET_PERIOD_EP,
/* Filtering by destination endpoint. */
.dest_endpoint = SET_PERIOD_EP,
.multicast_cb = NULL
}
};
void App_init(const app_global_functions_t * functions)
{
LOG(LVL_INFO, "App_init");
// 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_MS
period_ms = DEFAULT_PERIOD_MS;
EXECUTION_TIME_US);
// Set callback for received unicast messages
Shared_Data_addDataReceivedCb(&alltype_packets_filter);
// Start the stack
lib_state->startStack();
}
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
app_lib_data_to_send_t::tracking_id
app_lib_data_tracking_id_t tracking_id
Definition: wms_data.h:344
app_lib_data_to_send_t::src_endpoint
uint8_t src_endpoint
Definition: wms_data.h:350
APP_LIB_DATA_SEND_FLAG_NONE
@ APP_LIB_DATA_SEND_FLAG_NONE
Definition: wms_data.h:100
app_lib_data_to_send_t::dest_address
app_addr_t dest_address
Definition: wms_data.h:340
Shared_Data_addDataReceivedCb
app_res_e Shared_Data_addDataReceivedCb(shared_data_item_t *item)
Add a new packet received item to the list. If the item is already in the list it is only updated.
app_lib_data_to_send_t::num_bytes
size_t num_bytes
Definition: wms_data.h:338
node_configuration.h
APP_LIB_DATA_RECEIVE_RES_HANDLED
@ APP_LIB_DATA_RECEIVE_RES_HANDLED
Definition: wms_data.h:192
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.
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: wms_data.h:60
APP_RES_OK
@ APP_RES_OK
Definition: wms_app.h:204
SHARED_DATA_NET_MODE_ALL
@ SHARED_DATA_NET_MODE_ALL
Definition: shared_data.h:48
app_lib_data_receive_res_e
app_lib_data_receive_res_e
Return value of data reception callback.
Definition: wms_data.h:188
app_lib_data_to_send_t::dest_endpoint
uint8_t dest_endpoint
Definition: wms_data.h:352
app_scheduler.h
app_lib_data_to_send_t
A struct for lib_data->sendData().
Definition: wms_data.h:333
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: wms_app.h:157
debug_log.h
APP_LIB_DATA_QOS_HIGH
@ APP_LIB_DATA_QOS_HIGH
Definition: wms_data.h:91
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:224
app_lib_data_received_t
Struct passed to data reception callback functions.
Definition: wms_data.h:285
app_lib_data_to_send_t::flags
uint8_t flags
Definition: wms_data.h:348
APP_ADDR_ANYSINK
@ APP_ADDR_ANYSINK
Definition: wms_app.h:236
app_lib_data_to_send_t::qos
app_lib_data_qos_e qos
Definition: wms_data.h:346
LOG_INIT
#define LOG_INIT()
Definition: debug_log.h:66
shared_data.h
APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
@ APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
Definition: wms_data.h:195
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:336
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
api.h