Loading...
Searching...
No Matches
ieee802154.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015-2019 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
21#ifndef NET_IEEE802154_H
22#define NET_IEEE802154_H
23
24#include <stdint.h>
25#include <stdlib.h>
26
27#include "byteorder.h"
28#include "net/eui64.h"
29#include "modules.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
38#define IEEE802154_SFD (0xa7)
39
44#define IEEE802154_SHORT_ADDRESS_LEN (2U)
45#define IEEE802154_LONG_ADDRESS_LEN (8U)
54#define IEEE802154_MAX_HDR_LEN (23U)
55#define IEEE802154_MIN_FRAME_LEN (IEEE802154_FCF_LEN + sizeof(uint8_t))
56
57#define IEEE802154_FCF_LEN (2U)
58#define IEEE802154_FCS_LEN (2U)
59
60#define IEEE802154_FCF_TYPE_MASK (0x07)
61#define IEEE802154_FCF_TYPE_BEACON (0x00)
62#define IEEE802154_FCF_TYPE_DATA (0x01)
63#define IEEE802154_FCF_TYPE_ACK (0x02)
64#define IEEE802154_FCF_TYPE_MACCMD (0x03)
65
66#define IEEE802154_FCF_SECURITY_EN (0x08)
67#define IEEE802154_FCF_FRAME_PEND (0x10)
68#define IEEE802154_FCF_ACK_REQ (0x20)
69#define IEEE802154_FCF_PAN_COMP (0x40)
71#define IEEE802154_FCF_DST_ADDR_MASK (0x0c)
72#define IEEE802154_FCF_DST_ADDR_VOID (0x00)
73#define IEEE802154_FCF_DST_ADDR_RESV (0x04)
74#define IEEE802154_FCF_DST_ADDR_SHORT (0x08)
75#define IEEE802154_FCF_DST_ADDR_LONG (0x0c)
77#define IEEE802154_FCF_VERS_MASK (0x30)
78#define IEEE802154_FCF_VERS_V0 (0x00)
79#define IEEE802154_FCF_VERS_V1 (0x10)
80
81#define IEEE802154_FCF_SRC_ADDR_MASK (0xc0)
82#define IEEE802154_FCF_SRC_ADDR_VOID (0x00)
83#define IEEE802154_FCF_SRC_ADDR_RESV (0x40)
84#define IEEE802154_FCF_SRC_ADDR_SHORT (0x80)
85#define IEEE802154_FCF_SRC_ADDR_LONG (0xc0)
92#define IEEE802154_CHANNEL_MIN_SUBGHZ (0U)
93#define IEEE802154_CHANNEL_MAX_SUBGHZ (10U)
94#define IEEE802154_CHANNEL_MIN (11U)
95#define IEEE802154_CHANNEL_MAX (26U)
98#define IEEE802154_FRAME_LEN_MAX (127U)
99#define IEEE802154G_FRAME_LEN_MAX (2047U)
100#define IEEE802154_ACK_FRAME_LEN (5U)
105#define IEEE802154_LIFS_SYMS (40U)
106
110#define IEEE802154_SIFS_SYMS (12U)
111
115#define IEEE802154_SIFS_MAX_FRAME_SIZE (18U)
116
120#define IEEE802154_ACK_TIMEOUT_SYMS (54)
121
127#define IEEE802154_RADIO_RSSI_OFFSET (-174)
128
129#define IEEE802154_PHY_MR_FSK_PHR_LEN (2)
130#define IEEE802154_PHY_MR_FSK_2FSK_SFD_LEN (2)
140#define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_0 (0x6F4E)
141#define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_1 (0x632D)
142#define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_0 (0x90E4)
143#define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_1 (0x7A0E)
152#define IEEE802154G_ATURNAROUNDTIME_US (1 * US_PER_MS)
153
158#define IEEE802154_ATURNAROUNDTIME_IN_SYMBOLS (12)
159
167#define IEEE802154_CCA_DURATION_IN_SYMBOLS (8)
168
181
185enum {
190
198#define IEEE802154_ADDR_BCAST { 0xff, 0xff }
199
203#define IEEE802154_ADDR_BCAST_LEN (IEEE802154_SHORT_ADDRESS_LEN)
204
220#if IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_BPSK)
221#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_BPSK
222#elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_ASK)
223#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_ASK
224#elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_OQPSK)
225#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
226#elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OQPSK)
227#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OQPSK
228#elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OFDM)
229#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OFDM
230#elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_FSK)
231#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_FSK
232#endif
233
234#ifndef CONFIG_IEEE802154_DEFAULT_PHY_MODE
235#define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
236#endif
241#ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL
242#define CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL (5U)
243#endif
244
248#ifndef CONFIG_IEEE802154_DEFAULT_CHANNEL
249#define CONFIG_IEEE802154_DEFAULT_CHANNEL (26U)
250#endif
251
255#ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE
256#define CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE (2U)
257#endif
258
262#ifndef CONFIG_IEEE802154_DEFAULT_PANID
263#define CONFIG_IEEE802154_DEFAULT_PANID (0x0023U)
264#endif
265
269#ifndef IEEE802154_PANID_BCAST
270#define IEEE802154_PANID_BCAST { 0xff, 0xff }
271#endif
272
276#ifndef CONFIG_IEEE802154_DEFAULT_TXPOWER
277#define CONFIG_IEEE802154_DEFAULT_TXPOWER (0)
278#endif
284#ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE
285#define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE (3U)
286#endif
287
291#ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES
292#define CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES (4U)
293#endif
294
298#ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE
299#define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE (5U)
300#endif
301
305#ifndef CONFIG_IEEE802154_CCA_THRESH_DEFAULT
306#define CONFIG_IEEE802154_CCA_THRESH_DEFAULT (-70)
307#endif
308
312#ifndef CONFIG_IEEE802154_DEFAULT_MAX_FRAME_RETRANS
313#define CONFIG_IEEE802154_DEFAULT_MAX_FRAME_RETRANS (4U)
314#endif
315
319#ifdef DOXYGEN
320#define CONFIG_IEEE802154_AUTO_ACK_DISABLE 0
321#endif
322
326#ifndef CONFIG_IEEE802154_DEFAULT_ACK_REQ
327#define CONFIG_IEEE802154_DEFAULT_ACK_REQ 1
328#endif
329
335#ifndef CONFIG_IEEE802154_DSME_CAP_REDUCTION
336#define CONFIG_IEEE802154_DSME_CAP_REDUCTION 0
337#endif
338
348#ifndef CONFIG_IEEE802154_DSME_MAC_RESPONSE_WAIT_TIME
349#define CONFIG_IEEE802154_DSME_MAC_RESPONSE_WAIT_TIME (244U)
350#endif
351
360#ifndef CONFIG_IEEE802154_DSME_SCAN_DURATION
361#define CONFIG_IEEE802154_DSME_SCAN_DURATION (4U)
362#endif
363
375#ifndef CONFIG_IEEE802154_DSME_SUPERFRAME_ORDER
376#define CONFIG_IEEE802154_DSME_SUPERFRAME_ORDER (3U)
377#endif
378
388#ifndef CONFIG_IEEE802154_DSME_MULTISUPERFRAME_ORDER
389#define CONFIG_IEEE802154_DSME_MULTISUPERFRAME_ORDER (3U)
390#endif
391
401#ifndef CONFIG_IEEE802154_DSME_BEACON_ORDER
402#define CONFIG_IEEE802154_DSME_BEACON_ORDER (3U)
403#endif
404
415#ifdef DOXYGEN
416#define CONFIG_IEEE802154_DSME_STATIC_GTS 0
417#endif
418
427#ifndef CONFIG_IEEE802154_DSME_GTS_EXPIRATION
428#define CONFIG_IEEE802154_DSME_GTS_EXPIRATION (16U)
429#endif
430
436#ifndef CONFIG_IEEE802154_DSME_MIN_COORD_LQI
437#define CONFIG_IEEE802154_DSME_MIN_COORD_LQI (100U)
438#endif
439
483size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len,
484 const uint8_t *dst, size_t dst_len,
485 le_uint16_t src_pan, le_uint16_t dst_pan,
486 uint8_t flags, uint8_t seq);
487
498size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr);
499
512int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
513
526int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
527
544int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
545 network_uint16_t short_addr, const eui64_t *ext_addr);
546
556static inline uint8_t ieee802154_get_seq(const uint8_t *mhr)
557{
558 return mhr[2];
559}
560
580static inline eui64_t *ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr,
581 size_t addr_len)
582{
583 int i = 0;
584
585 eui64->uint8[0] = eui64->uint8[1] = 0;
586
587 switch (addr_len) {
588 case 8:
589 eui64->uint8[0] = addr[i++] ^ 0x02;
590 eui64->uint8[1] = addr[i++];
591 eui64->uint8[2] = addr[i++];
592 eui64->uint8[3] = addr[i++];
593 eui64->uint8[4] = addr[i++];
594 eui64->uint8[5] = addr[i++];
595 eui64->uint8[6] = addr[i++];
596 eui64->uint8[7] = addr[i++];
597 break;
598
599 case 4:
600 eui64->uint8[0] = addr[i++] ^ 0x02;
601 eui64->uint8[1] = addr[i++];
602
603 /* Falls through. */
604 case 2:
605 eui64->uint8[2] = 0;
606 eui64->uint8[3] = 0xff;
607 eui64->uint8[4] = 0xfe;
608 eui64->uint8[5] = 0;
609 eui64->uint8[6] = addr[i++];
610 eui64->uint8[7] = addr[i++];
611 break;
612
613 default:
614 return NULL;
615 }
616
617 return eui64;
618}
619
636static inline int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
637{
638 return rssi + IEEE802154_RADIO_RSSI_OFFSET;
639}
640
654static inline uint8_t ieee802154_dbm_to_rssi(int16_t dbm)
655{
656 const int min = IEEE802154_RADIO_RSSI_OFFSET;
657 const int max = min + (UINT8_MAX - 1);
658
659 int val = dbm <= min ? min : (dbm >= max ? max : dbm);
660 return val - IEEE802154_RADIO_RSSI_OFFSET;
661}
662
663#ifdef __cplusplus
664}
665#endif
666
667#endif /* NET_IEEE802154_H */
Functions to work with different byte orders.
EUI-64 data type definition.
#define IEEE802154_ADDR_BCAST_LEN
Length in byte of IEEE802154_ADDR_BCAST.
Definition ieee802154.h:203
int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan)
Gets source address from MAC header.
size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr)
Get length of MAC header.
int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan)
Gets destination address from MAC header.
int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan, network_uint16_t short_addr, const eui64_t *ext_addr)
Check whether a frame pass the IEEE 802.15.4 frame filter.
size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len, const uint8_t *dst, size_t dst_len, le_uint16_t src_pan, le_uint16_t dst_pan, uint8_t flags, uint8_t seq)
Initializes an IEEE 802.15.4 MAC frame header in buf.
static uint8_t ieee802154_dbm_to_rssi(int16_t dbm)
Convert from dBm scale to RSSI.
Definition ieee802154.h:654
const uint8_t ieee802154_addr_bcast[IEEE802154_ADDR_BCAST_LEN]
Broadcast address.
static uint8_t ieee802154_get_seq(const uint8_t *mhr)
Gets sequence number from MAC header.
Definition ieee802154.h:556
static eui64_t * ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr, size_t addr_len)
Generates an IPv6 interface identifier from an IEEE 802.15.4 address.
Definition ieee802154.h:580
ieee802154_phy_mode_t
802.15.4 PHY modes
Definition ieee802154.h:172
static int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
Convert from RSSI scale to dBm.
Definition ieee802154.h:636
#define IEEE802154_RADIO_RSSI_OFFSET
value of measured power when RSSI is zero.
Definition ieee802154.h:127
@ IEEE802154_FEC_RSC
recursive and systematic code
Definition ieee802154.h:188
@ IEEE802154_FEC_NRNSC
non-recursive and non-systematic code
Definition ieee802154.h:187
@ IEEE802154_FEC_NONE
no forward error correction
Definition ieee802154.h:186
@ IEEE802154_PHY_BPSK
Binary Phase Shift Keying.
Definition ieee802154.h:174
@ IEEE802154_PHY_MR_OFDM
Multi-Rate Orthogonal Frequency-Division Multiplexing.
Definition ieee802154.h:178
@ IEEE802154_PHY_OQPSK
Offset Quadrature Phase-Shift Keying.
Definition ieee802154.h:176
@ IEEE802154_PHY_ASK
Amplitude-Shift Keying.
Definition ieee802154.h:175
@ IEEE802154_PHY_MR_FSK
Multi-Rate Frequency Shift Keying.
Definition ieee802154.h:179
@ IEEE802154_PHY_DISABLED
PHY disabled, no mode selected.
Definition ieee802154.h:173
@ IEEE802154_PHY_MR_OQPSK
Multi-Rate Offset Quadrature Phase-Shift Keying.
Definition ieee802154.h:177
Common macros and compiler attributes/pragmas configuration.
A 16 bit integer in big endian aka network byte order.
Definition byteorder.h:74
Data type to represent an EUI-64.
Definition eui64.h:55
uint8_t uint8[8]
split into 8 8-bit words.
Definition eui64.h:57
A 16 bit integer in little endian.
Definition byteorder.h:38