1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

ieee802154: replace literals for lengths by macros

This commit is contained in:
Oleg Hahm 2016-04-03 23:12:11 +02:00
parent 1b635e1218
commit a59e100d41
4 changed files with 79 additions and 75 deletions

View File

@ -67,9 +67,9 @@ void at86rf2xx_reset(at86rf2xx_t *dev)
#if CPUID_LEN
cpuid_get(cpuid);
#if CPUID_LEN < 8
#if CPUID_LEN < IEEE802154_LONG_ADDRESS_LEN
/* in case CPUID_LEN < 8, fill missing bytes with zeros */
for (int i = CPUID_LEN; i < 8; i++) {
for (int i = CPUID_LEN; i < IEEE802154_LONG_ADDRESS_LEN; i++) {
cpuid[i] = 0;
}
#else
@ -81,7 +81,7 @@ void at86rf2xx_reset(at86rf2xx_t *dev)
cpuid[0] &= ~(0x01);
cpuid[0] |= 0x02;
/* copy and set long address */
memcpy(&addr_long, cpuid, 8);
memcpy(&addr_long, cpuid, IEEE802154_LONG_ADDRESS_LEN);
at86rf2xx_set_addr_long(dev, NTOHLL(addr_long.uint64.u64));
at86rf2xx_set_addr_short(dev, NTOHS(addr_long.uint16[0].u16));
#else

View File

@ -373,17 +373,20 @@ int kw2xrf_set_addr(kw2xrf_t *dev, uint16_t addr)
* 0 for unicast addresses */
dev->addr_short[1] &= 0x7F;
#endif
kw2xrf_write_iregs(MKW2XDMI_MACSHORTADDRS0_LSB, val_ar, 2);
kw2xrf_write_iregs(MKW2XDMI_MACSHORTADDRS0_LSB, val_ar,
IEEE802154_SHORT_ADDRESS_LEN);
return sizeof(uint16_t);
}
int kw2xrf_set_addr_long(kw2xrf_t *dev, uint64_t addr)
{
for (int i = 0; i < 8; i++) {
dev->addr_long[i] = (addr >> ((7 - i) * 8));
for (int i = 0; i < IEEE802154_LONG_ADDRESS_LEN; i++) {
dev->addr_long[i] = (addr >> ((IEEE802154_LONG_ADDRESS_LEN - 1 - i) * 8));
}
kw2xrf_write_iregs(MKW2XDMI_MACLONGADDRS0_0, (dev->addr_long), 8);
kw2xrf_write_iregs(MKW2XDMI_MACLONGADDRS0_0, (dev->addr_long),
IEEE802154_LONG_ADDRESS_LEN);
return sizeof(uint64_t);
}
@ -424,16 +427,16 @@ int kw2xrf_init(kw2xrf_t *dev, spi_t spi, spi_speed_t spi_speed,
#if CPUID_LEN
cpuid_get(cpuid);
#if CPUID_LEN < 8
#if CPUID_LEN < IEEE802154_LONG_ADDRESS_LEN
/* in case CPUID_LEN < 8, fill missing bytes with zeros */
for (int i = CPUID_LEN; i < 8; i++) {
for (int i = CPUID_LEN; i < IEEE802154_LONG_ADDRESS_LEN; i++) {
cpuid[i] = 0;
}
#else
for (int i = 8; i < CPUID_LEN; i++) {
for (int i = IEEE802154_LONG_ADDRESS_LEN; i < CPUID_LEN; i++) {
cpuid[i & 0x07] ^= cpuid[i];
}
@ -442,7 +445,7 @@ int kw2xrf_init(kw2xrf_t *dev, spi_t spi, spi_speed_t spi_speed,
cpuid[0] &= ~(0x01);
cpuid[0] |= 0x02;
/* copy and set long address */
memcpy(&addr_long, cpuid, 8);
memcpy(&addr_long, cpuid, IEEE802154_LONG_ADDRESS_LEN);
kw2xrf_set_addr_long(dev, NTOHLL(addr_long.uint64.u64));
kw2xrf_set_addr(dev, NTOHS(addr_long.uint16[0].u16));
#else
@ -516,8 +519,8 @@ uint64_t kw2xrf_get_addr_long(kw2xrf_t *dev)
uint64_t addr;
uint8_t *ap = (uint8_t *)(&addr);
for (int i = 0; i < 8; i++) {
ap[i] = dev->addr_long[7 - i];
for (int i = 0; i < IEEE802154_LONG_ADDRESS_LEN; i++) {
ap[i] = dev->addr_long[IEEE802154_LONG_ADDRESS_LEN - 1 - i];
}
return addr;
@ -581,10 +584,10 @@ int kw2xrf_get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len)
}
if (dev->option & KW2XRF_OPT_SRC_ADDR_LONG) {
*((uint16_t *)value) = 8;
*((uint16_t *)value) = IEEE802154_LONG_ADDRESS_LEN;
}
else {
*((uint16_t *)value) = 2;
*((uint16_t *)value) = IEEE802154_SHORT_ADDRESS_LEN;
}
return sizeof(uint16_t);
@ -603,11 +606,11 @@ int kw2xrf_get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len)
}
if (dev->option & KW2XRF_OPT_SRC_ADDR_LONG) {
uint64_t addr = kw2xrf_get_addr_long(dev);
ieee802154_get_iid(value, (uint8_t *)&addr, 8);
ieee802154_get_iid(value, (uint8_t *)&addr, IEEE802154_LONG_ADDRESS_LEN);
}
else {
uint16_t addr = kw2xrf_get_addr_short(dev);
ieee802154_get_iid(value, (uint8_t *)&addr, 2);
ieee802154_get_iid(value, (uint8_t *)&addr, IEEE802154_SHORT_ADDRESS_LEN);
}
return sizeof(eui64_t);
@ -783,11 +786,11 @@ int kw2xrf_set(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t value_le
return -EOVERFLOW;
}
if (*((uint16_t *)value) == 2) {
if (*((uint16_t *)value) == IEEE802154_SHORT_ADDRESS_LEN) {
kw2xrf_set_option(dev, KW2XRF_OPT_SRC_ADDR_LONG,
false);
}
else if (*((uint16_t *)value) == 8) {
else if (*((uint16_t *)value) == IEEE802154_LONG_ADDRESS_LEN) {
kw2xrf_set_option(dev, KW2XRF_OPT_SRC_ADDR_LONG,
true);
}
@ -902,10 +905,10 @@ static size_t _get_frame_hdr_len(uint8_t *mhr)
}
if (tmp == IEEE802154_FCF_SRC_ADDR_SHORT) {
return (len + 2);
return (len + IEEE802154_SHORT_ADDRESS_LEN);
}
else if (tmp == IEEE802154_FCF_SRC_ADDR_LONG) {
return (len + 8);
return (len + IEEE802154_LONG_ADDRESS_LEN);
}
}
@ -925,10 +928,10 @@ static gnrc_pktsnip_t *_make_netif_hdr(uint8_t *mhr)
tmp = mhr[1] & IEEE802154_FCF_SRC_ADDR_MASK;
if (tmp == IEEE802154_FCF_SRC_ADDR_SHORT) {
src_len = 2;
src_len = IEEE802154_SHORT_ADDRESS_LEN;
}
else if (tmp == IEEE802154_FCF_SRC_ADDR_LONG) {
src_len = 8;
src_len = IEEE802154_LONG_ADDRESS_LEN;
}
else if (tmp == 0) {
src_len = 0;
@ -940,10 +943,10 @@ static gnrc_pktsnip_t *_make_netif_hdr(uint8_t *mhr)
tmp = mhr[1] & IEEE802154_FCF_DST_ADDR_MASK;
if (tmp == IEEE802154_FCF_DST_ADDR_SHORT) {
dst_len = 2;
dst_len = IEEE802154_SHORT_ADDRESS_LEN;
}
else if (tmp == IEEE802154_FCF_DST_ADDR_LONG) {
dst_len = 8;
dst_len = IEEE802154_LONG_ADDRESS_LEN;
}
else if (tmp == 0) {
dst_len = 0;
@ -1139,7 +1142,7 @@ int _assemble_tx_buf(kw2xrf_t *dev, gnrc_pktsnip_t *pkt)
dev->buf[index++] = 0xff;
dev->buf[index++] = 0xff;
}
else if (hdr->dst_l2addr_len == 2) {
else if (hdr->dst_l2addr_len == IEEE802154_SHORT_ADDRESS_LEN) {
/* set to short addressing mode */
dev->buf[2] |= IEEE802154_FCF_DST_ADDR_SHORT;
/* set destination address, byte order is inverted */
@ -1147,12 +1150,12 @@ int _assemble_tx_buf(kw2xrf_t *dev, gnrc_pktsnip_t *pkt)
dev->buf[index++] = dst_addr[1];
dev->buf[index++] = dst_addr[0];
}
else if (hdr->dst_l2addr_len == 8) {
else if (hdr->dst_l2addr_len == IEEE802154_LONG_ADDRESS_LEN) {
/* default to use long address mode for src and dst */
dev->buf[2] |= IEEE802154_FCF_DST_ADDR_LONG;
/* set destination address located directly after gnrc_ifhrd_t in memory */
uint8_t *dst_addr = gnrc_netif_hdr_get_dst_addr(hdr);
for (int i = 7; i >= 0; i--) {
for (int i = IEEE802154_LONG_ADDRESS_LEN - 1; i >= 0; i--) {
dev->buf[index++] = dst_addr[i];
}
}
@ -1170,15 +1173,15 @@ int _assemble_tx_buf(kw2xrf_t *dev, gnrc_pktsnip_t *pkt)
}
/* insert source address according to length */
if (hdr->src_l2addr_len == 2) {
if (hdr->src_l2addr_len == IEEE802154_SHORT_ADDRESS_LEN) {
dev->buf[2] |= IEEE802154_FCF_SRC_ADDR_SHORT;
dev->buf[index++] = (uint8_t)(dev->addr_short[0]);
dev->buf[index++] = (uint8_t)(dev->addr_short[1]);
}
else {
dev->buf[2] |= IEEE802154_FCF_SRC_ADDR_LONG;
memcpy(&(dev->buf[index]), dev->addr_long, 8);
index += 8;
memcpy(&(dev->buf[index]), dev->addr_long, IEEE802154_LONG_ADDRESS_LEN);
index += IEEE802154_LONG_ADDRESS_LEN;
}
/* set sequence number */
dev->buf[3] = dev->seq_nr++;

View File

@ -216,8 +216,8 @@ static int _get_addr_short(xbee_t *dev, uint8_t *val, size_t len)
cmd[1] = 'Y';
_api_at_cmd(dev, cmd, 2, &resp);
if (resp.status == 0) {
memcpy(val, resp.data, 2);
return 2;
memcpy(val, resp.data, IEEE802154_SHORT_ADDRESS_LEN);
return IEEE802154_SHORT_ADDRESS_LEN;
}
return -ECANCELED;
}
@ -227,7 +227,7 @@ static int _get_addr_long(xbee_t *dev, uint8_t *val, size_t len)
uint8_t cmd[2];
resp_t resp;
if (len < 8) {
if (len < IEEE802154_LONG_ADDRESS_LEN) {
return -EOVERFLOW;
}
@ -246,7 +246,7 @@ static int _get_addr_long(xbee_t *dev, uint8_t *val, size_t len)
_api_at_cmd(dev, cmd, 2, &resp);
if (resp.status == 0) {
memcpy(val + 4, resp.data, 4);
return 8;
return IEEE802154_LONG_ADDRESS_LEN;
}
return -ECANCELED;
}
@ -286,10 +286,10 @@ static int _set_addr_len(xbee_t *dev, uint16_t *val, size_t len)
}
switch (*val) {
case 8:
case IEEE802154_LONG_ADDRESS_LEN:
dev->addr_flags |= XBEE_ADDR_FLAGS_LONG;
break;
case 2:
case IEEE802154_SHORT_ADDRESS_LEN:
dev->addr_flags &= ~XBEE_ADDR_FLAGS_LONG;
break;
default:
@ -460,15 +460,15 @@ int xbee_init(xbee_t *dev, const xbee_params_t *params)
_at_cmd(dev, "ATCN\r");
/* load long address (we can not set it, its read only for Xbee devices) */
if (_get_addr_long(dev, dev->addr_long.uint8, 8) < 0) {
if (_get_addr_long(dev, dev->addr_long.uint8, IEEE802154_LONG_ADDRESS_LEN) < 0) {
DEBUG("xbee: Error getting address\n");
return -EIO;
}
/* set default channel */
if (_set_addr(dev, &((dev->addr_long).uint8[6]), 2) < 0) {
if (_set_addr(dev, &((dev->addr_long).uint8[6]), IEEE802154_SHORT_ADDRESS_LEN) < 0) {
DEBUG("xbee: Error setting short address\n");
return -EIO;
}
/* set default channel */
tmp[1] = 0;
tmp[0] = XBEE_DEFAULT_CHANNEL;
if (_set_channel(dev, tmp, 2) < 0) {
@ -520,7 +520,8 @@ static int _send(gnrc_netdev_t *netdev, gnrc_pktsnip_t *pkt)
}
/* get netif header check address length and flags */
hdr = (gnrc_netif_hdr_t *)pkt->data;
if (!((hdr->dst_l2addr_len == 2) || (hdr->dst_l2addr_len == 8) ||
if (!((hdr->dst_l2addr_len == IEEE802154_SHORT_ADDRESS_LEN) ||
(hdr->dst_l2addr_len == IEEE802154_LONG_ADDRESS_LEN) ||
_is_broadcast(hdr))) {
gnrc_pktbuf_release(pkt);
return -ENOMSG;
@ -539,18 +540,18 @@ static int _send(gnrc_netdev_t *netdev, gnrc_pktsnip_t *pkt)
dev->tx_buf[4] = 0xff;
dev->tx_buf[5] = 0xff;
}
if (hdr->dst_l2addr_len == 2) {
if (hdr->dst_l2addr_len == IEEE802154_SHORT_ADDRESS_LEN) {
dev->tx_buf[1] = (uint8_t)((size + 5) >> 8);
dev->tx_buf[2] = (uint8_t)(size + 5);
dev->tx_buf[3] = API_ID_TX_SHORT_ADDR;
memcpy(dev->tx_buf + 5, gnrc_netif_hdr_get_dst_addr(hdr), 2);
memcpy(dev->tx_buf + 5, gnrc_netif_hdr_get_dst_addr(hdr), IEEE802154_SHORT_ADDRESS_LEN);
pos = 7;
}
else {
dev->tx_buf[1] = (uint8_t)((size + 11) >> 8);
dev->tx_buf[2] = (uint8_t)(size + 11);
dev->tx_buf[3] = API_ID_TX_LONG_ADDR;
memcpy(dev->tx_buf + 5, gnrc_netif_hdr_get_dst_addr(hdr), 8);
memcpy(dev->tx_buf + 5, gnrc_netif_hdr_get_dst_addr(hdr), IEEE802154_LONG_ADDRESS_LEN);
pos = 13;
}
/* set options */
@ -614,10 +615,10 @@ static int _get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len
return -EOVERFLOW;
}
if (dev->addr_flags & XBEE_ADDR_FLAGS_LONG) {
*((uint16_t *)value) = 8;
*((uint16_t *)value) = IEEE802154_LONG_ADDRESS_LEN;
}
else {
*((uint16_t *)value) = 2;
*((uint16_t *)value) = IEEE802154_SHORT_ADDRESS_LEN;
}
return sizeof(uint16_t);
case NETOPT_IPV6_IID:
@ -625,10 +626,10 @@ static int _get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len
return -EOVERFLOW;
}
if (dev->addr_flags & XBEE_ADDR_FLAGS_LONG) {
ieee802154_get_iid(value, (uint8_t *)&dev->addr_long, 8);
ieee802154_get_iid(value, (uint8_t *)&dev->addr_long, IEEE802154_LONG_ADDRESS_LEN);
}
else {
ieee802154_get_iid(value, (uint8_t *)&dev->addr_short, 2);
ieee802154_get_iid(value, (uint8_t *)&dev->addr_short, IEEE802154_SHORT_ADDRESS_LEN);
}
return sizeof(eui64_t);
@ -695,10 +696,10 @@ static void _isr_event(gnrc_netdev_t *netdev, uint32_t event_type)
/* read address length */
if (dev->rx_buf[0] == API_ID_RX_SHORT_ADDR) {
addr_len = 2;
addr_len = IEEE802154_SHORT_ADDRESS_LEN;
}
else {
addr_len = 8;
addr_len = IEEE802154_LONG_ADDRESS_LEN;
}
/* check checksum for correctness */
@ -728,10 +729,10 @@ static void _isr_event(gnrc_netdev_t *netdev, uint32_t event_type)
hdr->lqi = 0;
gnrc_netif_hdr_set_src_addr(hdr, &(dev->rx_buf[1]), addr_len);
if (addr_len == 2) {
gnrc_netif_hdr_set_dst_addr(hdr, dev->addr_short, 2);
gnrc_netif_hdr_set_dst_addr(hdr, dev->addr_short, IEEE802154_SHORT_ADDRESS_LEN);
}
else {
gnrc_netif_hdr_set_dst_addr(hdr, dev->addr_long.uint8, 8);
gnrc_netif_hdr_set_dst_addr(hdr, dev->addr_long.uint8, IEEE802154_LONG_ADDRESS_LEN);
}
pos = 3 + addr_len;
/* allocate and copy payload */

View File

@ -357,10 +357,10 @@ static int _get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len
}
if (dev->flags & GNRC_ZEP_FLAGS_DST_ADDR_LONG) {
_set_uint16_ptr(value, 8);
_set_uint16_ptr(value, IEEE802154_LONG_ADDRESS_LEN);
}
else {
_set_uint16_ptr(value, 2);
_set_uint16_ptr(value, IEEE802154_SHORT_ADDRESS_LEN);
}
return sizeof(uint16_t);
@ -371,10 +371,10 @@ static int _get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len
}
if (dev->flags & GNRC_ZEP_FLAGS_SRC_ADDR_LONG) {
_set_uint16_ptr(value, 8);
_set_uint16_ptr(value, IEEE802154_LONG_ADDRESS_LEN);
}
else {
_set_uint16_ptr(value, 2);
_set_uint16_ptr(value, IEEE802154_SHORT_ADDRESS_LEN);
}
return sizeof(uint16_t);
@ -401,11 +401,11 @@ static int _get(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t max_len
}
if (dev->flags & GNRC_ZEP_FLAGS_SRC_ADDR_LONG) {
uint64_t addr = byteorder_ltobll(dev->eui64).u64;
ieee802154_get_iid(value, (uint8_t *)&addr, 8);
ieee802154_get_iid(value, (uint8_t *)&addr, IEEE802154_LONG_ADDRESS_LEN);
}
else {
uint16_t addr = byteorder_ltobs(dev->addr).u16;
ieee802154_get_iid(value, (uint8_t *)&addr, 2);
ieee802154_get_iid(value, (uint8_t *)&addr, IEEE802154_SHORT_ADDRESS_LEN);
}
return sizeof(eui64_t);
@ -480,11 +480,11 @@ static int _set(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t value_l
}
switch (*_get_uint16_ptr(value)) {
case 2:
case IEEE802154_SHORT_ADDRESS_LEN:
dev->flags &= ~GNRC_ZEP_FLAGS_DST_ADDR_LONG;
break;
case 8:
case IEEE802154_LONG_ADDRESS_LEN:
dev->flags |= GNRC_ZEP_FLAGS_DST_ADDR_LONG;
break;
@ -500,11 +500,11 @@ static int _set(gnrc_netdev_t *netdev, netopt_t opt, void *value, size_t value_l
}
switch (*_get_uint16_ptr(value)) {
case 2:
case IEEE802154_SHORT_ADDRESS_LEN:
dev->flags &= ~GNRC_ZEP_FLAGS_SRC_ADDR_LONG;
break;
case 8:
case IEEE802154_LONG_ADDRESS_LEN:
dev->flags |= GNRC_ZEP_FLAGS_SRC_ADDR_LONG;
break;
@ -889,16 +889,16 @@ static size_t _make_data_frame_hdr(gnrc_zep_t *dev, uint8_t *buf,
buf[pos++] = 0xff;
buf[pos++] = 0xff;
}
else if (hdr->dst_l2addr_len == 2) {
else if (hdr->dst_l2addr_len == IEEE802154_SHORT_ADDRESS_LEN) {
uint8_t *dst_addr = gnrc_netif_hdr_get_dst_addr(hdr);
buf[pos++] = dst_addr[1];
buf[pos++] = dst_addr[0];
}
else if (hdr->dst_l2addr_len == 8) {
else if (hdr->dst_l2addr_len == IEEE802154_LONG_ADDRESS_LEN) {
buf[1] |= 0x04;
uint8_t *dst_addr = gnrc_netif_hdr_get_dst_addr(hdr);
for (int i = 7; i >= 0; i--) {
for (int i = (IEEE802154_LONG_ADDRESS_LEN - 1); i >= 0; i--) {
buf[pos++] = dst_addr[i];
}
}
@ -919,12 +919,12 @@ static size_t _make_data_frame_hdr(gnrc_zep_t *dev, uint8_t *buf,
/* fill in source address */
if (dev->flags & GNRC_ZEP_FLAGS_SRC_ADDR_LONG) {
buf[1] |= 0x40;
memcpy(&(buf[pos]), &dev->eui64, 8);
pos += 8;
memcpy(&(buf[pos]), &dev->eui64, IEEE802154_LONG_ADDRESS_LEN);
pos += IEEE802154_LONG_ADDRESS_LEN;
}
else {
memcpy(&(buf[pos]), &dev->addr, 2);
pos += 2;
memcpy(&(buf[pos]), &dev->addr, IEEE802154_SHORT_ADDRESS_LEN);
pos += IEEE802154_SHORT_ADDRESS_LEN;
}
/* set sequence number */
@ -962,10 +962,10 @@ static size_t _get_frame_hdr_len(uint8_t *mhr)
}
if (tmp == IEEE802154_FCF_SRC_ADDR_SHORT) {
return (len + 2);
return (len + IEEE802154_SHORT_ADDRESS_LEN);
}
else if (tmp == IEEE802154_FCF_SRC_ADDR_LONG) {
return (len + 8);
return (len + IEEE802154_LONG_ADDRESS_LEN);
}
}
@ -984,10 +984,10 @@ gnrc_pktsnip_t *_make_netif_hdr(uint8_t *mhr)
tmp = mhr[1] & IEEE802154_FCF_SRC_ADDR_MASK;
if (tmp == IEEE802154_FCF_SRC_ADDR_SHORT) {
src_len = 2;
src_len = IEEE802154_SHORT_ADDRESS_LEN;
}
else if (tmp == IEEE802154_FCF_SRC_ADDR_LONG) {
src_len = 8;
src_len = IEEE802154_LONG_ADDRESS_LEN;
}
else if (tmp == 0) {
src_len = 0;
@ -999,10 +999,10 @@ gnrc_pktsnip_t *_make_netif_hdr(uint8_t *mhr)
tmp = mhr[1] & IEEE802154_FCF_DST_ADDR_MASK;
if (tmp == IEEE802154_FCF_DST_ADDR_SHORT) {
dst_len = 2;
dst_len = IEEE802154_SHORT_ADDRESS_LEN;
}
else if (tmp == IEEE802154_FCF_DST_ADDR_LONG) {
dst_len = 8;
dst_len = IEEE802154_LONG_ADDRESS_LEN;
}
else if (tmp == 0) {
dst_len = 0;