Browse Source

implement event confirmation

master
Olaf Rempel 2 years ago
parent
commit
b838b26996
1 changed files with 143 additions and 127 deletions
  1. +143
    -127
      funkstuff.c

+ 143
- 127
funkstuff.c View File

@@ -257,154 +257,170 @@ int main(void)
}

/* get RX data */
struct rfm12_packet *req_pkt = rfm12_get_rxpkt();
struct rfm12_packet *rsp_pkt = rfm12_get_txpkt();
if (req_pkt != NULL)
struct rfm12_packet *rx_pkt = rfm12_get_rxpkt();
if (rx_pkt != NULL)
{
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 *rsp_msg = (struct rfm12_msg *)rsp_pkt->data;

#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_msg *rx_msg = (struct rfm12_msg *)rx_pkt->data;
if ((rx_msg->command & MSG_TYPE_MASK) == MSG_TYPE_REQUEST)
{
/* RX packet no longer needed */
rfm12_clear_rx();

/* transmit response */
if (rfm12_start_tx())
struct rfm12_packet *rsp_pkt = rfm12_get_txpkt();
if (rsp_pkt == NULL)
{
led[LED_TX] = 5;
rfm12_clear_rx();
continue;
}

continue;
}
#endif
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;

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 (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;

uint8_t i;
switch (req_msg->command)
{
case MSG_CMD_SWITCHAPP_REQUEST:
if (req_msg->p.switchapp.app == BOOTMODE_BOOTLOADER)
{
wdt_enable(WDTO_15MS);
}
else if (req_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));

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 = 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 = 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 = 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;

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;

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;

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)
)
case MSG_CMD_REGISTER_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER;

for (i = 0; i < ARRAY_SIZE(event_desc); i++)
{
event_queue(req_msg->p.ctrl_req.data_type,
req_msg->p.ctrl_req.data_num,
req_msg->p.ctrl_req.data_value);

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_num = req_msg->p.ctrl_req.data_num;
rsp_pkt->data_length += sizeof(struct _control_response);
break;
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;
break;

case MSG_CMD_REGISTER_REQUEST:
rsp_msg->cause = CAUSE_INVALID_PARAMETER;

for (i = 0; i < ARRAY_SIZE(control_desc); i++)
{
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;
}

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;
}
}
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;

/* RX packet no longer needed */
rfm12_clear_rx();
default:
break;
}

/* transmit response */
if (rfm12_start_tx())
{
led[LED_TX] = 5;
/* RX packet no longer needed */
rfm12_clear_rx();
}
}
}


Loading…
Cancel
Save