#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#define DEBUG_LOG_MODULE_NAME "NODE 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
typedef struct
{
uint32_t diag_period_ms;
uint32_t packet_ttl_ms;
} control_app_conf_t;
static const control_app_conf_t DEFAULT_CONF =
{
.diag_period_ms = CONF_DIAG_PERIOD_MS,
.packet_ttl_ms = CONF_PKT_TTL_MS,
};
static control_app_conf_t m_conf;
{
(void) status;
}
{
(void) button_id;
(void) event;
control_app_switch_t pkt = {
.type = CTRL_APP_SWITCH_EVENT,
};
static bool light_state = false;
light_state = !light_state;
pkt.button_pressed = light_state;
{
.
bytes = (
const uint8_t *)&pkt,
.num_bytes = sizeof(control_app_switch_t),
.src_endpoint = SRC_EP_SWITCH,
.dest_endpoint = DEST_EP_SWITCH,
};
LOG(
LVL_INFO,
"Send switch event (%s)", light_state?
"On":
"Off");
{
}
}
static void control_node_ack_cb(uint8_t * bytes, uint8_t len)
{
control_app_ack_t * pkt = (control_app_ack_t *) bytes;
control_app_conf_t conf;
.
ack_cb = control_node_ack_cb,
};
if (len != sizeof(control_app_ack_t))
{
return;
}
conf.diag_period_ms = pkt->diag_period_ms;
conf.packet_ttl_ms = pkt->packet_ttl_ms;
if (memcmp(&conf, &m_conf, sizeof(control_app_conf_t)))
{
LOG(
LVL_DEBUG,
" - diag_period_ms: %u", conf.diag_period_ms);
memcpy(&m_conf, &conf, sizeof(control_app_conf_t));
{
LOG(
LVL_ERROR,
"Error re-initializing control library (ret:%d)",
ctrl_ret);
return;
}
}
else
{
}
}
{
(void) functions;
.
ack_cb = control_node_ack_cb,
};
memcpy(&m_conf, &DEFAULT_CONF, sizeof(control_app_conf_t));
LOG(
LVL_DEBUG,
" - diag_period_ms: %u", m_conf.diag_period_ms);
LOG(
LVL_DEBUG,
" - packet_ttl_ms: %u", m_conf.packet_ttl_ms);
{
return;
}
{
LOG(
LVL_ERROR,
"Error setting node role (res:%d)", app_res);
return;
}
{
LOG(
LVL_ERROR,
"Error initializing control library (ret:%d)",
ctrl_ret);
return;
}
{
LOG(
LVL_WARNING,
"Error registering button event (res:%d)", button_res);
}
lib_state->startStack();
}