Wirepas SDK
shared_data/app.c
/* Copyright 2019 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
#include <stdio.h>
#include "api.h"
#include "shared_data.h"
#include "app_scheduler.h"
#define DEBUG_LOG_MODULE_NAME "DATA APP"
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#include "debug_log.h"
static app_lib_data_receive_res_e shared_data_cb(
const shared_data_item_t * item,
const app_lib_data_received_t * data);
static app_lib_data_receive_res_e shared_data_cb_filter4(
const shared_data_item_t * item,
const app_lib_data_received_t * data);
static bool filter_multicast_cb(app_addr_t group_addr);
static shared_data_item_t packet_1 =
{
.cb = shared_data_cb,
.filter = {
.src_endpoint = 10,
.dest_endpoint = 10,
.multicast_cb = NULL
}
};
static shared_data_item_t packet_2 =
{
.cb = shared_data_cb,
.filter = {
.src_endpoint = 20,
.dest_endpoint = 20,
.multicast_cb = NULL
}
};
static shared_data_item_t packet_3 =
{
.cb = shared_data_cb,
.filter = {
.src_endpoint = SHARED_DATA_UNUSED_ENDPOINT,
.dest_endpoint = SHARED_DATA_UNUSED_ENDPOINT,
.multicast_cb = filter_multicast_cb
}
};
static shared_data_item_t packet_4 =
{
.cb = shared_data_cb_filter4,
.filter = {
.src_endpoint = 11,
.dest_endpoint = 11,
.multicast_cb = NULL
}
};
static void sent_cb(const app_lib_data_sent_status_t * status)
{
LOG(LVL_INFO, "Packet sent (src ep: %u, dest ep: %u, tracking_id: %d, "
"success: %d).",
status->src_endpoint,
status->dest_endpoint,
status->tracking_id,
(uint8_t)status->success);
}
static uint32_t enable_reception()
{
LOG(LVL_INFO, "Enable reception again");
}
static app_lib_data_receive_res_e shared_data_cb_filter4(
const shared_data_item_t * item,
{
/* Messages will be offered two times the message
* - First refuse it no space
* - Second, handle it
*/
static bool blocked = false;
if (blocked)
{
blocked = false;
}
else
{
blocked = true;
LOG(LVL_INFO, "Block reception");
/*
* Any received packet on this EP will block reception for 30s
*/
App_Scheduler_addTask_execTime(enable_reception, 30 * 1000, 100);
}
}
static app_lib_data_receive_res_e shared_data_cb(
const shared_data_item_t * item,
{
LOG(LVL_INFO, "packet received with filter (src ep: %u, dest ep: %u, mode: %u).",
item->filter.src_endpoint,
item->filter.dest_endpoint,
(uint8_t)item->filter.mode);
/* This code here demonstrate that item can be modified directly from
* packet callback.
*/
if(item == &packet_2)
{
packet_2.cb = shared_data_cb;
packet_2.filter.mode = SHARED_DATA_NET_MODE_ALL;
if(data->dest_endpoint == 20)
{
packet_2.filter.dest_endpoint = 30;
packet_2.filter.src_endpoint = 30;
}
else
{
packet_2.filter.dest_endpoint = 20;
packet_2.filter.src_endpoint = 20;
}
}
/* Send back the received packet. */
app_lib_data_to_send_t data_to_send;
data_to_send.bytes = data->bytes;
data_to_send.num_bytes = data->num_bytes;
data_to_send.dest_address = data->src_address;
/* Add a callback to be notified when the packet is sent. */
data_to_send.qos = APP_LIB_DATA_QOS_HIGH;
data_to_send.src_endpoint = data->src_endpoint;
data_to_send.dest_endpoint = data->dest_endpoint;
Shared_Data_sendData(&data_to_send, sent_cb);
LOG(LVL_INFO, "Sent packet (tracking_id: %d).", data_to_send.tracking_id);
}
static bool filter_multicast_cb(app_addr_t group_addr)
{
LOG(LVL_INFO, "Multicast filter (group_addr: %u).", group_addr);
/* Accept packet on Multicast group #2. */
if (group_addr == 0x80000002)
{
LOG(LVL_INFO, "Accept.");
return true;
}
else
{
LOG(LVL_INFO, "Reject.");
return false;
}
}
void App_init(const app_global_functions_t * functions)
{
// Initialize uart for application debug prints
LOG(LVL_INFO, "Shared Data example started.");
// 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;
}
// Start the stack
lib_state->startStack();
}
app_lib_data_sent_status_t
Struct to tracking callback function.
Definition: wms_data.h:370
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
APP_SCHEDULER_STOP_TASK
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
Definition: app_scheduler.h:37
SHARED_DATA_UNUSED_ENDPOINT
#define SHARED_DATA_UNUSED_ENDPOINT
Definition: shared_data.h:33
app_lib_data_sent_status_t::tracking_id
app_lib_data_tracking_id_t tracking_id
Definition: wms_data.h:381
app_lib_data_sent_status_t::src_endpoint
uint8_t src_endpoint
Definition: wms_data.h:383
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_received_t::src_address
app_addr_t src_address
Definition: wms_data.h:292
app_lib_data_sent_status_t::success
bool success
Definition: wms_data.h:387
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_sent_status_t::dest_endpoint
uint8_t dest_endpoint
Definition: wms_data.h:385
APP_LIB_DATA_RECEIVE_RES_NO_SPACE
@ APP_LIB_DATA_RECEIVE_RES_NO_SPACE
Definition: wms_data.h:200
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
SHARED_DATA_NET_MODE_MULTICAST
@ SHARED_DATA_NET_MODE_MULTICAST
Definition: shared_data.h:46
APP_LIB_DATA_QOS_HIGH
@ APP_LIB_DATA_QOS_HIGH
Definition: wms_data.h:91
Shared_Data_readyToReceive
app_res_e Shared_Data_readyToReceive(shared_data_item_t *item)
Enable back the reception for an item. Reception is automatically paused when an item returns APP_LIB...
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_addr_t
uint32_t app_addr_t
Definition: wms_app.h:228
app_lib_data_to_send_t::flags
uint8_t flags
Definition: wms_data.h:348
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
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.
SHARED_DATA_NET_MODE_UNICAST
@ SHARED_DATA_NET_MODE_UNICAST
Definition: shared_data.h:42
app_lib_data_to_send_t::bytes
const uint8_t * bytes
Definition: wms_data.h:336
api.h