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,
{
(void) 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)
{
(void) 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();
}
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.
#define LOG_INIT()
Definition debug_log.h:66
#define LVL_INFO
Definition debug_log.h:83
#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
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
@ SHARED_DATA_NET_MODE_ALL
Definition shared_data.h:53
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_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_ADDR_ANYSINK
Definition wms_app.h:236
@ APP_RES_OK
Definition wms_app.h:204
List of global functions, passed to App_entrypoint()
Definition wms_app.h:158
@ APP_LIB_DATA_SEND_FLAG_NONE
Definition wms_data.h:100
#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_lib_data_tracking_id_t tracking_id
Definition wms_data.h:340
app_lib_data_qos_e qos
Definition wms_data.h:342
@ APP_LIB_DATA_QOS_HIGH
Definition wms_data.h:91
app_addr_t dest_address
Definition wms_data.h:336
const uint8_t * bytes
Definition wms_data.h:332
app_lib_data_receive_res_e
Return value of data reception callback.
Definition wms_data.h:189
@ APP_LIB_DATA_RECEIVE_RES_HANDLED
Definition wms_data.h:192
@ APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
Definition wms_data.h:195
A struct for lib_data->sendData().
Definition wms_data.h:330
Struct passed to data reception callback functions.
Definition wms_data.h:286