#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#define DEBUG_LOG_MODULE_NAME "LOWLATENCY_APP"
#define DEBUG_LOG_MAX_LEVEL LVL_NOLOG
#define MAX_LED_PER_BOARD 4u
#define SWITCH_ON_COMMAND_PATTERN 0b00000001
#define DATA_EP (25u)
#define TASK_EXEC_TIME_US_SEND_BUTTON_PRESSED_MSG (250u)
#define LED_STATE_OFF 0
#define LED_STATE_ON 1
typedef enum
{
MSG_ID_SWITCH_COMMAND = 1,
MSG_ID_DOWNLINK_SET_LED_STATE = 2,
MSG_ID_MULTICAST_GROUPING = 3
} message_id_e;
static uint8_t m_button_pressed_id;
static uint32_t m_mcast_address;
typedef struct __attribute__((packed))
{
uint8_t button_id;
uint8_t action;
uint16_t counter;
} payload_switch_command_t;
typedef struct __attribute__((packed))
{
uint8_t led_id;
uint8_t brightness;
} payload_on_off_lighting_t;
typedef struct __attribute__((packed))
{
uint32_t multicast_address;
} payload_grouping_command_t;
typedef struct __attribute__((packed))
{
uint8_t id;
union
{
payload_switch_command_t switch_status;
payload_on_off_lighting_t set_brightness;
payload_grouping_command_t set_multicast_address;
} payload;
} msg_t;
static void set_led_state(uint8_t led_id, uint8_t led_state)
{
"Set led_id %d to state %d", led_id, led_state);
{
}
}
{
msg_t msg;
size_t msg_byte_size = sizeof (msg.id);
msg.id = (uint8_t)message_id;
memcpy(&msg.payload.switch_status,
(payload_switch_command_t *)payload,
sizeof(payload_switch_command_t));
msg_byte_size += sizeof(payload_switch_command_t);
data_to_send.
bytes = (
const uint8_t *) &msg;
}
static bool filter_multicast_cb(
app_addr_t received_multicast_addr)
{
LOG(
LVL_INFO,
"Multicast address received : 0x%x", received_multicast_addr);
return m_mcast_address == received_multicast_addr;
}
static void set_led_for_button(uint8_t button_id, uint8_t led_id)
{
if (button_id == 0)
{
set_led_state(led_id, LED_STATE_ON);
}
if (button_id == 1)
{
set_led_state(led_id, LED_STATE_OFF);
}
}
const shared_data_item_t * item,
{
(void) item;
msg_t *msg = (( msg_t *) data->
bytes);
"Message received id %d",
msg->id);
if (msg_size < sizeof (msg->id))
{
}
msg_size -= sizeof(msg->id);
switch (msg->id)
{
case MSG_ID_SWITCH_COMMAND :
if (msg_size == sizeof(payload_switch_command_t))
{
payload_switch_command_t *payload = & msg->payload.switch_status;
LOG (
LVL_INFO,
"Switch command received : button_id = %d, action = %d", payload->button_id, payload->action);
if (payload->action == 1)
{
set_led_for_button(payload->button_id, payload->action);
}
else if (payload->action == 0)
{
}
}
else
{
LOG(
LVL_INFO,
"Message received doesn't match the size of the expected message.");
}
break;
case MSG_ID_DOWNLINK_SET_LED_STATE:
if (msg_size == sizeof(payload_on_off_lighting_t))
{
if (msg->payload.set_brightness.brightness == 1)
{
"Downlink brightness action %d, set Led ON",
msg->payload.set_brightness.brightness);
set_led_state(msg->payload.set_brightness.led_id, LED_STATE_ON);
}
if (msg->payload.set_brightness.brightness == 0)
{
"Downlink brightness action %d, set Led ON",
msg->payload.set_brightness.brightness);
set_led_state(msg->payload.set_brightness.led_id, LED_STATE_OFF);
}
}
else
{
LOG(
LVL_ERROR,
"Message recieved size doesn't correspond to the payload size");
}
break;
case MSG_ID_MULTICAST_GROUPING:
if(msg_size == sizeof(payload_grouping_command_t))
{
"Set new Mcast address : 0x%x",
msg->payload.set_multicast_address.multicast_address);
m_mcast_address=msg->payload.set_multicast_address.multicast_address;
{
}
}
break;
default:
break;
}
}
static shared_data_item_t alltype_packets_filter =
{
.cb = data_received_cb,
.filter = {
.src_endpoint = DATA_EP,
.dest_endpoint = DATA_EP,
.multicast_cb = filter_multicast_cb
}
};
static uint32_t task_send_button_pressed_msg(void)
{
payload_switch_command_t payload;
static uint32_t counter_value = 0;
payload.counter = counter_value;
uint8_t led_id = 1;
payload.button_id = m_button_pressed_id;
payload.action = 1;
{
LOG(
LVL_ERROR,
"Message was not accepted for sending. ERROR CODE : %d", res);
}
counter_value ++;
LOG(
LVL_INFO,
"Button pressed id : %d", m_button_pressed_id);
set_led_for_button(m_button_pressed_id, led_id);
}
static void button_pressed_handler(uint8_t button_id,
button_event_e event)
{
(void) event;
m_button_pressed_id = button_id;
LOG(
LVL_INFO,
"Button %d pressed", m_button_pressed_id);
TASK_EXEC_TIME_US_SEND_BUTTON_PRESSED_MSG);
}
{
(void) functions;
uint8_t num_buttons;
{
return;
}
{
LOG(
LVL_INFO,
"App persistant first initialization\n");
m_mcast_address = 0x80000001;
}
{
LOG(
LVL_ERROR,
"Persistent area is not initialized as it should (no area defined?)\n");
}
for (uint8_t button_id = 0; button_id < num_buttons; button_id++)
{
button_pressed_handler);
}
lib_state->startStack();
}
app_persistent_res_e App_Persistent_read(uint8_t *data, size_t len)
Read from persistent.
app_persistent_res_e
List of return code.
@ APP_PERSISTENT_RES_UNINITIALIZED
@ APP_PERSISTENT_RES_INVALID_CONTENT
app_persistent_res_e App_Persistent_write(uint8_t *data, size_t len)
Write to persistent.
#define APP_SCHEDULER_STOP_TASK
Value to return from task to remove it.
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(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Board-independent LED functions.
led_res_e Led_set(uint8_t led_id, bool state)
Turn the given LED on or off.
__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
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_BROADCAST
Send packet as broadcast to all nodes.
List of global functions, passed to App_entrypoint()
@ APP_LIB_DATA_SEND_FLAG_NONE
#define APP_LIB_DATA_NO_TRACKING_ID
When sending data and no tracking of packet is requested, this ID may be used.
app_lib_data_tracking_id_t tracking_id
app_lib_data_send_res_e
A result code returned from lib_data->sendData().
@ APP_LIB_DATA_SEND_RES_SUCCESS
app_lib_data_receive_res_e
Return value of data reception callback.
@ APP_LIB_DATA_RECEIVE_RES_HANDLED
@ APP_LIB_DATA_RECEIVE_RES_NOT_FOR_APP
A struct for lib_data->sendData().
Struct passed to data reception callback functions.
@ APP_LIB_SETTINGS_ROLE_HEADNODE_LL