#include <string.h>
#include "../control_node/common.h"
#define DEBUG_LOG_MODULE_NAME "ROUT APP"
#ifdef DEBUG_LOG_MAX_LEVEL_APP
#define DEBUG_LOG_MAX_LEVEL DEBUG_LOG_MAX_LEVEL_APP
#else
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#endif
#define APP_CONFIG_CTRL_ROUTER_TYPE 0xC0
#define LIGHT_TASK_EXEC_TIME_US 30
#define MAX_LIGHT_TURN_ON_DELAY_MS 250
#define COARSE_TO_MS(delay) ((1000u * (delay)) >> 7)
#define MS_TO_COARSE(delay) (((delay) * 128) / 1000u)
static bool m_received_config;
static control_app_ack_t m_ack_config;
static uint16_t m_filter_id;
static bool m_light_state;
const shared_data_item_t * item,
void received_appconfig (uint16_t type, uint8_t length, uint8_t * value_p);
static shared_data_item_t m_switch_received_item = {
.cb = received_switch_cb,
.filter = {
.src_endpoint = SRC_EP_SWITCH,
.dest_endpoint = DEST_EP_SWITCH,
.multicast_cb = NULL
}
};
.
type = APP_CONFIG_CTRL_ROUTER_TYPE,
.cb = received_appconfig,
.call_cb_always = false
};
static uint32_t light_task(void)
{
m_light_state?"On":"Off");
}
const shared_data_item_t * item,
{
(void) item;
if (data->
num_bytes ==
sizeof(control_app_switch_t))
{
control_app_switch_t * swtch = (control_app_switch_t *) &(data->
bytes[0]);
control_app_switch_fwd_t swtch_fwd;
{
.
bytes = (
const uint8_t *)&swtch_fwd,
.num_bytes = sizeof(control_app_switch_fwd_t),
.src_endpoint = SRC_EP_SWITCH,
.dest_endpoint = DEST_EP_SWITCH,
};
swtch->button_pressed?"On":"Off",
COARSE_TO_MS(data->
delay));
m_light_state = swtch->button_pressed;
memcpy(&swtch_fwd.pkt, swtch, sizeof(control_app_switch_t));
{
}
}
else if (data->
num_bytes ==
sizeof(control_app_switch_fwd_t))
{
control_app_switch_fwd_t * swtch_fwd =
(control_app_switch_fwd_t *) &(data->
bytes[0]);
LOG(
LVL_INFO,
"Received a forwarded Switch %s Event (delay:%d).",
swtch_fwd->pkt.button_pressed?"On":"Off",
COARSE_TO_MS(data->
delay));
m_light_state = swtch_fwd->pkt.button_pressed;
}
else
{
}
if (COARSE_TO_MS(data->
delay) >= MAX_LIGHT_TURN_ON_DELAY_MS)
{
m_light_state?"On":"Off");
}
else
{
light_task,
MAX_LIGHT_TURN_ON_DELAY_MS - COARSE_TO_MS(data->
delay),
LIGHT_TASK_EXEC_TIME_US);
{
}
}
}
{
(void) in;
if (m_received_config)
{
out->
data = (
void *) &m_ack_config;
out->
length =
sizeof(control_app_ack_t);
return true;
}
return false;
}
void received_appconfig(uint16_t type, uint8_t length, uint8_t * value_p)
{
(void) type;
if (length != sizeof(control_app_appconfig_t))
{
return;
}
control_app_appconfig_t * pkt = (control_app_appconfig_t *) value_p;
m_received_config = true;
m_ack_config.diag_period_ms = pkt->ack.diag_period_ms;
m_ack_config.packet_ttl_ms = pkt->ack.packet_ttl_ms;
LOG(
LVL_DEBUG,
" - diag_period_ms: %u", m_ack_config.diag_period_ms);
LOG(
LVL_DEBUG,
" - packet_ttl_ms: %u", m_ack_config.packet_ttl_ms);
}
{
(void) functions;
};
{
return;
}
{
LOG(
LVL_ERROR,
"Error setting node role (res:%d)", app_res);
return;
}
{
LOG(
LVL_ERROR,
"Error initializing control library (ret:%d)",
ctrl_ret);
}
{
LOG(
LVL_ERROR,
"Error adding Data callback (res:%d)", app_res);
}
&m_filter_id);
{
LOG(
LVL_ERROR,
"Error adding AppConfig filter (res:%d)", app_cfg_res);
}
m_light_state = false;
m_received_config = false;
lib_state->startStack();
}