TSCH: remove duplicate code from tsch_packet_create_eack()

tsch_packet_create_eack() prepares a frame802154_t data by itself, which
is similar to what framer_802154_setup_params() does. In order to avoid
duplicate code, tsch_packet_create_eack() is going to use
framer_802154_setup_params() to have a ready-to-use frame802154_t data.
This commit is contained in:
Yasuyuki Tanaka 2017-01-06 22:23:58 +01:00 committed by Yasuyuki Tanaka
parent a35c3cad08
commit 6e8c5906bc
2 changed files with 73 additions and 35 deletions

View File

@ -56,64 +56,101 @@
#define LOG_MODULE "TSCH Pkt" #define LOG_MODULE "TSCH Pkt"
#define LOG_LEVEL LOG_LEVEL_MAC #define LOG_LEVEL LOG_LEVEL_MAC
/*
* We use a local packetbuf_attr array to collect necessary frame settings to
* create an EACK because EACK is generated in the interrupt context where
* packetbuf and packetbuf_attrs[] may be in use for another purpose.
*
* We have accessors of eackbuf_attrs: tsch_packet_eackbuf_set_attr() and
* tsch_packet_eackbuf_attr(). For some platform, they might need to be
* implemented as inline functions. However, for now, we don't provide the
* inline option. Such an optimization is left to the compiler for a target
* platform.
*/
static struct packetbuf_attr eackbuf_attrs[PACKETBUF_NUM_ATTRS];
/*---------------------------------------------------------------------------*/
static int
tsch_packet_eackbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
{
eackbuf_attrs[type].val = val;
return 1;
}
/*---------------------------------------------------------------------------*/
/* Return the value of a specified attribute */
packetbuf_attr_t
tsch_packet_eackbuf_attr(uint8_t type)
{
return eackbuf_attrs[type].val;
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Construct enhanced ACK packet and return ACK length */ /* Construct enhanced ACK packet and return ACK length */
int int
tsch_packet_create_eack(uint8_t *buf, int buf_size, tsch_packet_create_eack(uint8_t *buf, uint16_t buf_len,
const linkaddr_t *dest_addr, uint8_t seqno, int16_t drift, int nack) const linkaddr_t *dest_addr, uint8_t seqno,
int16_t drift, int nack)
{ {
int ret; frame802154_t params;
uint8_t curr_len = 0;
frame802154_t p;
struct ieee802154_ies ies; struct ieee802154_ies ies;
int hdr_len;
int ack_len;
memset(&p, 0, sizeof(p)); if(buf == NULL) {
p.fcf.frame_type = FRAME802154_ACKFRAME; return -1;
p.fcf.frame_version = FRAME802154_IEEE802154_2015; }
p.fcf.ie_list_present = 1;
/* Compression unset. According to IEEE802.15.4e-2012: memset(eackbuf_attrs, 0, sizeof(eackbuf_attrs));
* - if no address is present: elide PAN ID
* - if at least one address is present: include exactly one PAN ID (dest by default) */ tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_ACKFRAME);
p.fcf.panid_compression = 0; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_METADATA, 1);
p.dest_pid = IEEE802154_PANID; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, seqno);
p.seq = seqno;
tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_NO_DEST_ADDR, 1);
#if TSCH_PACKET_EACK_WITH_DEST_ADDR #if TSCH_PACKET_EACK_WITH_DEST_ADDR
if(dest_addr != NULL) { if(dest_addr != NULL) {
p.fcf.dest_addr_mode = LINKADDR_SIZE > 2 ? FRAME802154_LONGADDRMODE : FRAME802154_SHORTADDRMODE;; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_NO_DEST_ADDR, 0);
linkaddr_copy((linkaddr_t *)&p.dest_addr, dest_addr); linkaddr_copy((linkaddr_t *)&params.dest_addr, dest_addr);
} }
#endif #endif
tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_NO_SRC_ADDR, 1);
#if TSCH_PACKET_EACK_WITH_SRC_ADDR #if TSCH_PACKET_EACK_WITH_SRC_ADDR
p.fcf.src_addr_mode = LINKADDR_SIZE > 2 ? FRAME802154_LONGADDRMODE : FRAME802154_SHORTADDRMODE;; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_MAC_NO_SRC_ADDR, 0);
p.src_pid = IEEE802154_PANID; linkaddr_copy((linkaddr_t *)&params.src_addr, &linkaddr_node_addr);
linkaddr_copy((linkaddr_t *)&p.src_addr, &linkaddr_node_addr);
#endif #endif
#if LLSEC802154_ENABLED #if LLSEC802154_ENABLED
if(tsch_is_pan_secured) { if(tsch_is_pan_secured) {
p.fcf.security_enabled = 1; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL,
p.aux_hdr.security_control.security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK; TSCH_SECURITY_KEY_SEC_LEVEL_ACK);
p.aux_hdr.security_control.key_id_mode = FRAME802154_1_BYTE_KEY_ID_MODE; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE,
p.aux_hdr.security_control.frame_counter_suppression = 1; FRAME802154_1_BYTE_KEY_ID_MODE);
p.aux_hdr.security_control.frame_counter_size = 1; tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX,
p.aux_hdr.key_index = TSCH_SECURITY_KEY_INDEX_ACK; TSCH_SECURITY_KEY_INDEX_ACK);
} }
#endif /* LLSEC802154_ENABLED */ #endif /* LLSEC802154_ENABLED */
if((curr_len = frame802154_create(&p, buf)) == 0) { framer_802154_setup_params(tsch_packet_eackbuf_attr, 0, &params);
return 0; hdr_len = frame802154_hdrlen(&params);
}
/* Append IE timesync */ memset(buf, 0, buf_len);
/* Setup IE timesync */
memset(&ies, 0, sizeof(ies)); memset(&ies, 0, sizeof(ies));
ies.ie_time_correction = drift; ies.ie_time_correction = drift;
ies.ie_is_nack = nack; ies.ie_is_nack = nack;
if((ret = frame80215e_create_ie_header_ack_nack_time_correction(buf+curr_len, buf_size-curr_len, &ies)) == -1) { ack_len =
frame80215e_create_ie_header_ack_nack_time_correction(buf + hdr_len,
buf_len - hdr_len, &ies);
if(ack_len < 0) {
return -1; return -1;
} }
curr_len += ret; ack_len += hdr_len;
return curr_len; frame802154_create(&params, buf);
return ack_len;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Parse enhanced ACK packet, extract drift and nack */ /* Parse enhanced ACK packet, extract drift and nack */

View File

@ -87,8 +87,9 @@ by default, useful in case of duplicate seqno */
/********** Functions *********/ /********** Functions *********/
/* Construct enhanced ACK packet and return ACK length */ /* Construct enhanced ACK packet and return ACK length */
int tsch_packet_create_eack(uint8_t *buf, int buf_size, int tsch_packet_create_eack(uint8_t *buf, uint16_t buf_size,
const linkaddr_t *dest_addr, uint8_t seqno, int16_t drift, int nack); const linkaddr_t *dest_addr, uint8_t seqno,
int16_t drift, int nack);
/* Parse enhanced ACK packet, extract drift and nack */ /* Parse enhanced ACK packet, extract drift and nack */
int tsch_packet_parse_eack(const uint8_t *buf, int buf_size, int tsch_packet_parse_eack(const uint8_t *buf, int buf_size,
uint8_t seqno, frame802154_t *frame, struct ieee802154_ies *ies, uint8_t *hdr_len); uint8_t seqno, frame802154_t *frame, struct ieee802154_ies *ies, uint8_t *hdr_len);