implement event confirmation

This commit is contained in:
Olaf Rempel 2017-11-28 21:13:46 +01:00
parent 1c766cbc27
commit b838b26996

View File

@ -257,27 +257,129 @@ int main(void)
} }
/* get RX data */ /* get RX data */
struct rfm12_packet *req_pkt = rfm12_get_rxpkt(); struct rfm12_packet *rx_pkt = rfm12_get_rxpkt();
struct rfm12_packet *rsp_pkt = rfm12_get_txpkt(); if (rx_pkt != NULL)
if (req_pkt != NULL)
{ {
led[LED_RX] = 5; led[LED_RX] = 5;
if (rsp_pkt == NULL)
{
rfm12_clear_rx();
continue;
}
struct rfm12_msg *req_msg = (struct rfm12_msg *)req_pkt->data; struct rfm12_msg *rx_msg = (struct rfm12_msg *)rx_pkt->data;
struct rfm12_msg *rsp_msg = (struct rfm12_msg *)rsp_pkt->data; if ((rx_msg->command & MSG_TYPE_MASK) == MSG_TYPE_REQUEST)
#if 0
/* retransmitted request -> retransmit response */
if ((req_pkt->source_address == rsp_pkt->dest_address) &&
((req_msg->command & MSG_CMD_MASK) == (rsp_msg->command & MSG_CMD_MASK)) &&
(req_msg->seqnum == rsp_msg->seqnum)
)
{ {
struct rfm12_packet *rsp_pkt = rfm12_get_txpkt();
if (rsp_pkt == NULL)
{
rfm12_clear_rx();
continue;
}
struct rfm12_msg *rsp_msg = (struct rfm12_msg *)rsp_pkt->data;
rsp_pkt->dest_address = rx_pkt->source_address;
rsp_pkt->data_length = 3;
rsp_msg->command = rx_msg->command | MSG_TYPE_RESPONSE;
rsp_msg->seqnum = rx_msg->seqnum;
rsp_msg->cause = CAUSE_SUCCESS;
uint8_t i;
switch (rx_msg->command)
{
case MSG_CMD_SWITCHAPP_REQUEST:
if (rx_msg->p.switchapp.app == BOOTMODE_BOOTLOADER)
{
wdt_enable(WDTO_15MS);
}
else if (rx_msg->p.switchapp.app != BOOTMODE_APPLICATION)
{
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
}
break;
case MSG_CMD_DESCRIPTION_REQUEST:
if ((rx_msg->p.desc_req.type == DESCRIPTION_TYPE_CONTROL) &&
(rx_msg->p.desc_req.num < ARRAY_SIZE(control_desc))
)
{
memcpy_P(&rsp_msg->p.desc_rsp,
&control_desc[rx_msg->p.desc_req.num],
sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = rx_msg->p.desc_req.num;
rsp_msg->p.desc_rsp.max_num = ARRAY_SIZE(control_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response);
}
else if ((rx_msg->p.desc_req.type == DESCRIPTION_TYPE_EVENT) &&
(rx_msg->p.desc_req.num < ARRAY_SIZE(event_desc))
)
{
memcpy_P(&rsp_msg->p.desc_rsp,
&event_desc[rx_msg->p.desc_req.num],
sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = rx_msg->p.desc_req.num;
rsp_msg->p.desc_rsp.max_num = ARRAY_SIZE(event_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response);
}
else
{
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
}
break;
case MSG_CMD_CONTROL_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
for (i = 0; i < ARRAY_SIZE(control_desc); i++)
{
if ((pgm_read_byte(&control_desc[i].data_type) == rx_msg->p.ctrl_req.data_type) &&
(pgm_read_byte(&control_desc[i].data_num) == rx_msg->p.ctrl_req.data_num)
)
{
event_queue(rx_msg->p.ctrl_req.data_type,
rx_msg->p.ctrl_req.data_num,
rx_msg->p.ctrl_req.data_value);
rsp_msg->cause = CAUSE_SUCCESS;
rsp_msg->p.ctrl_rsp.data_type = rx_msg->p.ctrl_req.data_type;
rsp_msg->p.ctrl_rsp.data_num = rx_msg->p.ctrl_req.data_num;
rsp_pkt->data_length += sizeof(struct _control_response);
break;
}
}
break;
case MSG_CMD_REGISTER_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
for (i = 0; i < ARRAY_SIZE(event_desc); i++)
{
if ((pgm_read_byte(&event_desc[i].data_type) == rx_msg->p.reg_req.data_type) &&
(pgm_read_byte(&event_desc[i].data_num) == rx_msg->p.reg_req.data_num)
)
{
if (rx_msg->p.reg_req.enable)
{
event_data[i].state |= EVT_DATA_STATE_REGISTERED;
event_data[i].dest_address = rx_pkt->source_address;
}
else
{
event_data[i].state |= ~(EVT_DATA_STATE_REGISTERED | EVT_DATA_STATE_UPDATE);
}
rsp_msg->cause = CAUSE_SUCCESS;
rsp_msg->p.reg_rsp.data_type = rx_msg->p.reg_req.data_type;
rsp_msg->p.reg_rsp.data_num = rx_msg->p.reg_req.data_num;
rsp_msg->p.reg_rsp.data_value = event_data[i].data_value;
rsp_pkt->data_length += sizeof(struct _register_response);
break;
}
}
break;
default:
rsp_msg->cause = CAUSE_NOT_SUPPORTED;
break;
}
/* RX packet no longer needed */ /* RX packet no longer needed */
rfm12_clear_rx(); rfm12_clear_rx();
@ -286,125 +388,39 @@ int main(void)
{ {
led[LED_TX] = 5; led[LED_TX] = 5;
} }
continue;
} }
#endif else if ((rx_msg->command & MSG_TYPE_MASK) == MSG_TYPE_CONFIRMATION)
rsp_pkt->dest_address = req_pkt->source_address;
rsp_pkt->data_length = 3;
rsp_msg->command = req_msg->command | MSG_TYPE_RESPONSE;
rsp_msg->seqnum = req_msg->seqnum;
rsp_msg->cause = CAUSE_SUCCESS;
uint8_t i;
switch (req_msg->command)
{ {
case MSG_CMD_SWITCHAPP_REQUEST: uint8_t i;
if (req_msg->p.switchapp.app == BOOTMODE_BOOTLOADER) switch (rx_msg->command)
{ {
wdt_enable(WDTO_15MS); case MSG_CMD_EVENT_CONFIRMATION:
} for (i = 0; i < ARRAY_SIZE(event_desc); i++)
else if (req_msg->p.switchapp.app != BOOTMODE_APPLICATION)
{
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
}
break;
case MSG_CMD_DESCRIPTION_REQUEST:
if ((req_msg->p.desc_req.type == DESCRIPTION_TYPE_CONTROL) &&
(req_msg->p.desc_req.num < ARRAY_SIZE(control_desc))
)
{
memcpy_P(&rsp_msg->p.desc_rsp,
&control_desc[req_msg->p.desc_req.num],
sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = req_msg->p.desc_req.num;
rsp_msg->p.desc_rsp.max_num = ARRAY_SIZE(control_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response);
}
else if ((req_msg->p.desc_req.type == DESCRIPTION_TYPE_EVENT) &&
(req_msg->p.desc_req.num < ARRAY_SIZE(event_desc))
)
{
memcpy_P(&rsp_msg->p.desc_rsp,
&event_desc[req_msg->p.desc_req.num],
sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = req_msg->p.desc_req.num;
rsp_msg->p.desc_rsp.max_num = ARRAY_SIZE(event_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response);
}
else
{
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
}
break;
case MSG_CMD_CONTROL_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER;
for (i = 0; i < ARRAY_SIZE(control_desc); i++)
{
if ((pgm_read_byte(&control_desc[i].data_type) == req_msg->p.ctrl_req.data_type) &&
(pgm_read_byte(&control_desc[i].data_num) == req_msg->p.ctrl_req.data_num)
)
{ {
event_queue(req_msg->p.ctrl_req.data_type, if ((pgm_read_byte(&event_desc[i].data_type) == rx_msg->p.evt_cnf.data_type) &&
req_msg->p.ctrl_req.data_num, (pgm_read_byte(&event_desc[i].data_num) == rx_msg->p.evt_cnf.data_num)
req_msg->p.ctrl_req.data_value); )
{
if (event_data[i].state & EVT_DATA_STATE_WAIT4CONF)
{
event_data[i].state &= ~(EVT_DATA_STATE_WAIT4CONF);
rsp_msg->cause = CAUSE_SUCCESS; event_idx++;
rsp_msg->p.ctrl_rsp.data_type = req_msg->p.ctrl_req.data_type; if (event_idx == ARRAY_SIZE(event_desc))
rsp_msg->p.ctrl_rsp.data_num = req_msg->p.ctrl_req.data_num; {
rsp_pkt->data_length += sizeof(struct _control_response); event_idx = 0;
break; }
}
}
} }
} break;
break;
case MSG_CMD_REGISTER_REQUEST: default:
rsp_msg->cause = CAUSE_INVALID_PARAMETER; break;
}
for (i = 0; i < ARRAY_SIZE(control_desc); i++) /* RX packet no longer needed */
{ rfm12_clear_rx();
if ((pgm_read_byte(&event_desc[i].data_type) == req_msg->p.reg_req.data_type) &&
(pgm_read_byte(&event_desc[i].data_num) == req_msg->p.reg_req.data_num)
)
{
if (req_msg->p.reg_req.enable)
{
event_data[i].state |= EVT_DATA_STATE_REGISTERED;
event_data[i].dest_address = req_pkt->source_address;
}
else
{
event_data[i].state |= ~(EVT_DATA_STATE_REGISTERED | EVT_DATA_STATE_UPDATE);
}
rsp_msg->cause = CAUSE_SUCCESS;
rsp_msg->p.reg_rsp.data_type = req_msg->p.reg_req.data_type;
rsp_msg->p.reg_rsp.data_num = req_msg->p.reg_req.data_num;
rsp_msg->p.reg_rsp.data_value = event_data[i].data_value;
rsp_pkt->data_length += sizeof(struct _register_response);
break;
}
}
break;
default:
rsp_msg->cause = CAUSE_NOT_SUPPORTED;
break;
}
/* RX packet no longer needed */
rfm12_clear_rx();
/* transmit response */
if (rfm12_start_tx())
{
led[LED_TX] = 5;
} }
} }
} }