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,82 +257,64 @@ 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;
struct rfm12_msg *rx_msg = (struct rfm12_msg *)rx_pkt->data;
if ((rx_msg->command & MSG_TYPE_MASK) == MSG_TYPE_REQUEST)
{
struct rfm12_packet *rsp_pkt = rfm12_get_txpkt();
if (rsp_pkt == NULL) if (rsp_pkt == NULL)
{ {
rfm12_clear_rx(); rfm12_clear_rx();
continue; continue;
} }
struct rfm12_msg *req_msg = (struct rfm12_msg *)req_pkt->data;
struct rfm12_msg *rsp_msg = (struct rfm12_msg *)rsp_pkt->data; struct rfm12_msg *rsp_msg = (struct rfm12_msg *)rsp_pkt->data;
rsp_pkt->dest_address = rx_pkt->source_address;
#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)
)
{
/* RX packet no longer needed */
rfm12_clear_rx();
/* transmit response */
if (rfm12_start_tx())
{
led[LED_TX] = 5;
}
continue;
}
#endif
rsp_pkt->dest_address = req_pkt->source_address;
rsp_pkt->data_length = 3; rsp_pkt->data_length = 3;
rsp_msg->command = req_msg->command | MSG_TYPE_RESPONSE; rsp_msg->command = rx_msg->command | MSG_TYPE_RESPONSE;
rsp_msg->seqnum = req_msg->seqnum; rsp_msg->seqnum = rx_msg->seqnum;
rsp_msg->cause = CAUSE_SUCCESS; rsp_msg->cause = CAUSE_SUCCESS;
uint8_t i; uint8_t i;
switch (req_msg->command) switch (rx_msg->command)
{ {
case MSG_CMD_SWITCHAPP_REQUEST: case MSG_CMD_SWITCHAPP_REQUEST:
if (req_msg->p.switchapp.app == BOOTMODE_BOOTLOADER) if (rx_msg->p.switchapp.app == BOOTMODE_BOOTLOADER)
{ {
wdt_enable(WDTO_15MS); wdt_enable(WDTO_15MS);
} }
else if (req_msg->p.switchapp.app != BOOTMODE_APPLICATION) else if (rx_msg->p.switchapp.app != BOOTMODE_APPLICATION)
{ {
rsp_msg->cause = CAUSE_INVALID_PARAMETER; rsp_msg->cause = CAUSE_INVALID_PARAMETER;
} }
break; break;
case MSG_CMD_DESCRIPTION_REQUEST: case MSG_CMD_DESCRIPTION_REQUEST:
if ((req_msg->p.desc_req.type == DESCRIPTION_TYPE_CONTROL) && if ((rx_msg->p.desc_req.type == DESCRIPTION_TYPE_CONTROL) &&
(req_msg->p.desc_req.num < ARRAY_SIZE(control_desc)) (rx_msg->p.desc_req.num < ARRAY_SIZE(control_desc))
) )
{ {
memcpy_P(&rsp_msg->p.desc_rsp, memcpy_P(&rsp_msg->p.desc_rsp,
&control_desc[req_msg->p.desc_req.num], &control_desc[rx_msg->p.desc_req.num],
sizeof(struct _description_response)); sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = req_msg->p.desc_req.num; 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_msg->p.desc_rsp.max_num = ARRAY_SIZE(control_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response); rsp_pkt->data_length += sizeof(struct _description_response);
} }
else if ((req_msg->p.desc_req.type == DESCRIPTION_TYPE_EVENT) && else if ((rx_msg->p.desc_req.type == DESCRIPTION_TYPE_EVENT) &&
(req_msg->p.desc_req.num < ARRAY_SIZE(event_desc)) (rx_msg->p.desc_req.num < ARRAY_SIZE(event_desc))
) )
{ {
memcpy_P(&rsp_msg->p.desc_rsp, memcpy_P(&rsp_msg->p.desc_rsp,
&event_desc[req_msg->p.desc_req.num], &event_desc[rx_msg->p.desc_req.num],
sizeof(struct _description_response)); sizeof(struct _description_response));
rsp_msg->p.desc_rsp.num = req_msg->p.desc_req.num; 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_msg->p.desc_rsp.max_num = ARRAY_SIZE(event_desc) -1;
rsp_pkt->data_length += sizeof(struct _description_response); rsp_pkt->data_length += sizeof(struct _description_response);
} }
@ -347,17 +329,17 @@ int main(void)
for (i = 0; i < ARRAY_SIZE(control_desc); i++) 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) && if ((pgm_read_byte(&control_desc[i].data_type) == rx_msg->p.ctrl_req.data_type) &&
(pgm_read_byte(&control_desc[i].data_num) == req_msg->p.ctrl_req.data_num) (pgm_read_byte(&control_desc[i].data_num) == rx_msg->p.ctrl_req.data_num)
) )
{ {
event_queue(req_msg->p.ctrl_req.data_type, event_queue(rx_msg->p.ctrl_req.data_type,
req_msg->p.ctrl_req.data_num, rx_msg->p.ctrl_req.data_num,
req_msg->p.ctrl_req.data_value); rx_msg->p.ctrl_req.data_value);
rsp_msg->cause = CAUSE_SUCCESS; rsp_msg->cause = CAUSE_SUCCESS;
rsp_msg->p.ctrl_rsp.data_type = req_msg->p.ctrl_req.data_type; rsp_msg->p.ctrl_rsp.data_type = rx_msg->p.ctrl_req.data_type;
rsp_msg->p.ctrl_rsp.data_num = req_msg->p.ctrl_req.data_num; rsp_msg->p.ctrl_rsp.data_num = rx_msg->p.ctrl_req.data_num;
rsp_pkt->data_length += sizeof(struct _control_response); rsp_pkt->data_length += sizeof(struct _control_response);
break; break;
} }
@ -367,16 +349,16 @@ int main(void)
case MSG_CMD_REGISTER_REQUEST: case MSG_CMD_REGISTER_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER; rsp_msg->cause = CAUSE_INVALID_PARAMETER;
for (i = 0; i < ARRAY_SIZE(control_desc); i++) for (i = 0; i < ARRAY_SIZE(event_desc); i++)
{ {
if ((pgm_read_byte(&event_desc[i].data_type) == req_msg->p.reg_req.data_type) && if ((pgm_read_byte(&event_desc[i].data_type) == rx_msg->p.reg_req.data_type) &&
(pgm_read_byte(&event_desc[i].data_num) == req_msg->p.reg_req.data_num) (pgm_read_byte(&event_desc[i].data_num) == rx_msg->p.reg_req.data_num)
) )
{ {
if (req_msg->p.reg_req.enable) if (rx_msg->p.reg_req.enable)
{ {
event_data[i].state |= EVT_DATA_STATE_REGISTERED; event_data[i].state |= EVT_DATA_STATE_REGISTERED;
event_data[i].dest_address = req_pkt->source_address; event_data[i].dest_address = rx_pkt->source_address;
} }
else else
{ {
@ -384,8 +366,8 @@ int main(void)
} }
rsp_msg->cause = CAUSE_SUCCESS; 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_type = rx_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_num = rx_msg->p.reg_req.data_num;
rsp_msg->p.reg_rsp.data_value = event_data[i].data_value; rsp_msg->p.reg_rsp.data_value = event_data[i].data_value;
rsp_pkt->data_length += sizeof(struct _register_response); rsp_pkt->data_length += sizeof(struct _register_response);
break; break;
@ -407,6 +389,40 @@ int main(void)
led[LED_TX] = 5; led[LED_TX] = 5;
} }
} }
else if ((rx_msg->command & MSG_TYPE_MASK) == MSG_TYPE_CONFIRMATION)
{
uint8_t i;
switch (rx_msg->command)
{
case MSG_CMD_EVENT_CONFIRMATION:
for (i = 0; i < ARRAY_SIZE(event_desc); i++)
{
if ((pgm_read_byte(&event_desc[i].data_type) == rx_msg->p.evt_cnf.data_type) &&
(pgm_read_byte(&event_desc[i].data_num) == rx_msg->p.evt_cnf.data_num)
)
{
if (event_data[i].state & EVT_DATA_STATE_WAIT4CONF)
{
event_data[i].state &= ~(EVT_DATA_STATE_WAIT4CONF);
event_idx++;
if (event_idx == ARRAY_SIZE(event_desc))
{
event_idx = 0;
}
}
}
}
break;
default:
break;
}
/* RX packet no longer needed */
rfm12_clear_rx();
}
}
} }
return 0; return 0;