ieee802154.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015-2019 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #include "byteorder.h"
24 #include "modules.h"
25 #include "net/eui64.h"
26 #include "time_units.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 #define IEEE802154_SFD (0xa7)
36 
41 #define IEEE802154_SHORT_ADDRESS_LEN (2U)
42 #define IEEE802154_LONG_ADDRESS_LEN (8U)
51 #define IEEE802154_MAX_HDR_LEN (23U)
52 #define IEEE802154_MIN_FRAME_LEN (IEEE802154_FCF_LEN + sizeof(uint8_t))
53 
54 #define IEEE802154_FCF_LEN (2U)
55 #define IEEE802154_FCS_LEN (2U)
56 
57 #define IEEE802154_FCF_TYPE_MASK (0x07)
58 #define IEEE802154_FCF_TYPE_BEACON (0x00)
59 #define IEEE802154_FCF_TYPE_DATA (0x01)
60 #define IEEE802154_FCF_TYPE_ACK (0x02)
61 #define IEEE802154_FCF_TYPE_MACCMD (0x03)
62 
63 #define IEEE802154_FCF_SECURITY_EN (0x08)
64 #define IEEE802154_FCF_FRAME_PEND (0x10)
65 #define IEEE802154_FCF_ACK_REQ (0x20)
66 #define IEEE802154_FCF_PAN_COMP (0x40)
68 #define IEEE802154_FCF_DST_ADDR_MASK (0x0c)
69 #define IEEE802154_FCF_DST_ADDR_VOID (0x00)
70 #define IEEE802154_FCF_DST_ADDR_RESV (0x04)
71 #define IEEE802154_FCF_DST_ADDR_SHORT (0x08)
72 #define IEEE802154_FCF_DST_ADDR_LONG (0x0c)
74 #define IEEE802154_FCF_VERS_MASK (0x30)
75 #define IEEE802154_FCF_VERS_V0 (0x00)
76 #define IEEE802154_FCF_VERS_V1 (0x10)
77 
78 #define IEEE802154_FCF_SRC_ADDR_MASK (0xc0)
79 #define IEEE802154_FCF_SRC_ADDR_VOID (0x00)
80 #define IEEE802154_FCF_SRC_ADDR_RESV (0x40)
81 #define IEEE802154_FCF_SRC_ADDR_SHORT (0x80)
82 #define IEEE802154_FCF_SRC_ADDR_LONG (0xc0)
89 #define IEEE802154_CHANNEL_MIN_SUBGHZ (0U)
90 #define IEEE802154_CHANNEL_MAX_SUBGHZ (10U)
91 #define IEEE802154_CHANNEL_MIN (11U)
92 #define IEEE802154_CHANNEL_MAX (26U)
95 #define IEEE802154_FRAME_LEN_MAX (127U)
96 #define IEEE802154G_FRAME_LEN_MAX (2047U)
97 #define IEEE802154_ACK_FRAME_LEN (5U)
102 #define IEEE802154_LIFS_SYMS (40U)
103 
107 #define IEEE802154_SIFS_SYMS (12U)
108 
112 #define IEEE802154_SIFS_MAX_FRAME_SIZE (18U)
113 
117 #define IEEE802154_ACK_TIMEOUT_SYMS (54)
118 
122 #define IEEE802154_MR_OFDM_SYMBOL_TIME_US (120)
123 
129 #define IEEE802154_MR_FSK_SYMBOL_TIME_US (20)
130 
136 #define IEEE802154_RADIO_RSSI_OFFSET (-174)
137 
138 #define IEEE802154_PHY_MR_FSK_PHR_LEN (2)
139 #define IEEE802154_PHY_MR_FSK_2FSK_SFD_LEN (2)
149 #define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_0 (0x6F4E)
150 #define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_1 (0x632D)
151 #define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_0 (0x90E4)
152 #define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_1 (0x7A0E)
161 #define IEEE802154G_ATURNAROUNDTIME_US (1 * US_PER_MS)
162 
167 #define IEEE802154_ATURNAROUNDTIME_IN_SYMBOLS (12)
168 
178 #define IEEE802154_SYMBOL_TIME_US (16)
179 
187 #define IEEE802154_CCA_DURATION_IN_SYMBOLS (8)
188 
192 typedef enum {
203 
207 typedef enum {
212 
216 typedef enum {
224 
228 typedef enum {
234 
244 {
245  switch (srate) {
247  return 2;
249  return 3;
253  return 8;
255  return 10;
256  }
257 
258  return 0;
259 }
260 
268 #define IEEE802154_ADDR_BCAST { 0xff, 0xff }
269 
273 #define IEEE802154_ADDR_BCAST_LEN (IEEE802154_SHORT_ADDRESS_LEN)
274 
290 #if IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_BPSK)
291 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_BPSK
292 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_ASK)
293 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_ASK
294 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_OQPSK)
295 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
296 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OQPSK)
297 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OQPSK
298 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OFDM)
299 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OFDM
300 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_FSK)
301 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_FSK
302 #endif
303 
304 #ifndef CONFIG_IEEE802154_DEFAULT_PHY_MODE
305 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
306 #endif
311 #ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL
312 #define CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL (5U)
313 #endif
314 
318 #ifndef CONFIG_IEEE802154_DEFAULT_CHANNEL
319 #define CONFIG_IEEE802154_DEFAULT_CHANNEL (26U)
320 #endif
321 
325 #ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE
326 #define CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE (2U)
327 #endif
328 
332 #ifndef CONFIG_IEEE802154_MR_OQPSK_DEFAULT_CHIPS
333 #define CONFIG_IEEE802154_MR_OQPSK_DEFAULT_CHIPS IEEE802154_MR_OQPSK_CHIPS_1000
334 #endif
335 
339 #ifndef CONFIG_IEEE802154_MR_OQPSK_DEFAULT_RATE
340 #define CONFIG_IEEE802154_MR_OQPSK_DEFAULT_RATE (2U)
341 #endif
342 
346 #ifndef CONFIG_IEEE802154_MR_OFDM_DEFAULT_OPTION
347 #define CONFIG_IEEE802154_MR_OFDM_DEFAULT_OPTION (2U)
348 #endif
349 
353 #ifndef CONFIG_IEEE802154_MR_OFDM_DEFAULT_SCHEME
354 #define CONFIG_IEEE802154_MR_OFDM_DEFAULT_SCHEME (2U)
355 #endif
356 
360 #ifndef CONFIG_IEEE802154_MR_FSK_DEFAULT_SRATE
361 #define CONFIG_IEEE802154_MR_FSK_DEFAULT_SRATE IEEE802154_MR_FSK_SRATE_200K
362 #endif
363 
367 #ifndef CONFIG_IEEE802154_MR_FSK_DEFAULT_MOD_IDX
368 #define CONFIG_IEEE802154_MR_FSK_DEFAULT_MOD_IDX (64U)
369 #endif
370 
374 #ifndef CONFIG_IEEE802154_MR_FSK_DEFAULT_MOD_ORD
375 #define CONFIG_IEEE802154_MR_FSK_DEFAULT_MOD_ORD (2U)
376 #endif
377 
381 #ifndef CONFIG_IEEE802154_MR_FSK_DEFAULT_FEC
382 #define CONFIG_IEEE802154_MR_FSK_DEFAULT_FEC IEEE802154_FEC_NONE
383 #endif
384 
388 #ifndef CONFIG_IEEE802154_DEFAULT_PANID
389 #define CONFIG_IEEE802154_DEFAULT_PANID (0x0023U)
390 #endif
391 
395 #ifndef IEEE802154_PANID_BCAST
396 #define IEEE802154_PANID_BCAST { 0xff, 0xff }
397 #endif
398 
402 #ifndef CONFIG_IEEE802154_DEFAULT_TXPOWER
403 #define CONFIG_IEEE802154_DEFAULT_TXPOWER (0)
404 #endif
410 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE
411 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE (3U)
412 #endif
413 
417 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES
418 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES (4U)
419 #endif
420 
424 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE
425 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE (5U)
426 #endif
427 
431 #ifndef CONFIG_IEEE802154_CCA_THRESH_DEFAULT
432 #define CONFIG_IEEE802154_CCA_THRESH_DEFAULT (-70)
433 #endif
434 
438 #ifndef CONFIG_IEEE802154_DEFAULT_MAX_FRAME_RETRANS
439 #define CONFIG_IEEE802154_DEFAULT_MAX_FRAME_RETRANS (4U)
440 #endif
441 
445 #ifdef DOXYGEN
446 #define CONFIG_IEEE802154_AUTO_ACK_DISABLE 0
447 #endif
448 
452 #ifndef CONFIG_IEEE802154_DEFAULT_ACK_REQ
453 #define CONFIG_IEEE802154_DEFAULT_ACK_REQ 1
454 #endif
455 
461 #ifndef CONFIG_IEEE802154_DSME_CAP_REDUCTION
462 #define CONFIG_IEEE802154_DSME_CAP_REDUCTION 0
463 #endif
464 
474 #ifndef CONFIG_IEEE802154_DSME_MAC_RESPONSE_WAIT_TIME
475 #define CONFIG_IEEE802154_DSME_MAC_RESPONSE_WAIT_TIME (244U)
476 #endif
477 
486 #ifndef CONFIG_IEEE802154_DSME_SCAN_DURATION
487 #define CONFIG_IEEE802154_DSME_SCAN_DURATION (4U)
488 #endif
489 
501 #ifndef CONFIG_IEEE802154_DSME_SUPERFRAME_ORDER
502 #define CONFIG_IEEE802154_DSME_SUPERFRAME_ORDER (3U)
503 #endif
504 
514 #ifndef CONFIG_IEEE802154_DSME_MULTISUPERFRAME_ORDER
515 #define CONFIG_IEEE802154_DSME_MULTISUPERFRAME_ORDER (3U)
516 #endif
517 
527 #ifndef CONFIG_IEEE802154_DSME_BEACON_ORDER
528 #define CONFIG_IEEE802154_DSME_BEACON_ORDER (3U)
529 #endif
530 
541 #ifdef DOXYGEN
542 #define CONFIG_IEEE802154_DSME_STATIC_GTS 0
543 #endif
544 
553 #ifndef CONFIG_IEEE802154_DSME_GTS_EXPIRATION
554 #define CONFIG_IEEE802154_DSME_GTS_EXPIRATION (16U)
555 #endif
556 
562 #ifndef CONFIG_IEEE802154_DSME_MIN_COORD_LQI
563 #define CONFIG_IEEE802154_DSME_MIN_COORD_LQI (100U)
564 #endif
565 
609 size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len,
610  const uint8_t *dst, size_t dst_len,
611  le_uint16_t src_pan, le_uint16_t dst_pan,
612  uint8_t flags, uint8_t seq);
613 
624 size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr);
625 
638 int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
639 
652 int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
653 
670 int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
671  network_uint16_t short_addr, const eui64_t *ext_addr);
672 
682 static inline uint8_t ieee802154_get_seq(const uint8_t *mhr)
683 {
684  return mhr[2];
685 }
686 
706 static inline eui64_t *ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr,
707  size_t addr_len)
708 {
709  int i = 0;
710 
711  eui64->uint8[0] = eui64->uint8[1] = 0;
712 
713  switch (addr_len) {
714  case 8:
715  eui64->uint8[0] = addr[i++] ^ 0x02;
716  eui64->uint8[1] = addr[i++];
717  eui64->uint8[2] = addr[i++];
718  eui64->uint8[3] = addr[i++];
719  eui64->uint8[4] = addr[i++];
720  eui64->uint8[5] = addr[i++];
721  eui64->uint8[6] = addr[i++];
722  eui64->uint8[7] = addr[i++];
723  break;
724 
725  case 4:
726  eui64->uint8[0] = addr[i++] ^ 0x02;
727  eui64->uint8[1] = addr[i++];
728 
729  /* Falls through. */
730  case 2:
731  eui64->uint8[2] = 0;
732  eui64->uint8[3] = 0xff;
733  eui64->uint8[4] = 0xfe;
734  eui64->uint8[5] = 0;
735  eui64->uint8[6] = addr[i++];
736  eui64->uint8[7] = addr[i++];
737  break;
738 
739  default:
740  return NULL;
741  }
742 
743  return eui64;
744 }
745 
762 static inline int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
763 {
764  return rssi + IEEE802154_RADIO_RSSI_OFFSET;
765 }
766 
780 static inline uint8_t ieee802154_dbm_to_rssi(int16_t dbm)
781 {
782  const int min = IEEE802154_RADIO_RSSI_OFFSET;
783  const int max = min + (UINT8_MAX - 1);
784 
785  int val = dbm <= min ? min : (dbm >= max ? max : dbm);
786  return val - IEEE802154_RADIO_RSSI_OFFSET;
787 }
788 
789 #ifdef __cplusplus
790 }
791 #endif
792 
Functions to work with different byte orders.
EUI-64 data type definition.
ieee802154_mr_oqpsk_chips_t
802.15.4 MR-OQPSK chip rates
Definition: ieee802154.h:228
#define IEEE802154_ADDR_BCAST_LEN
Length in byte of IEEE802154_ADDR_BCAST.
Definition: ieee802154.h:273
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.
static uint8_t ieee802154_mr_fsk_plen(ieee802154_mr_fsk_srate_t srate)
Get the minimum preamble length for a given symbol rate.
Definition: ieee802154.h:243
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.
ieee802154_mr_fsk_srate_t
802.15.4 MR-FSK symbol rates
Definition: ieee802154.h:216
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:780
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:706
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:682
ieee802154_phy_mode_t
802.15.4 PHY modes
Definition: ieee802154.h:192
static int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
Convert from RSSI scale to dBm.
Definition: ieee802154.h:762
#define IEEE802154_RADIO_RSSI_OFFSET
value of measured power when RSSI is zero.
Definition: ieee802154.h:136
ieee802154_mr_fsk_fec_t
802.15.4 forward error correction schemes
Definition: ieee802154.h:207
@ IEEE802154_MR_OQPSK_CHIPS_100
100 kChip/s
Definition: ieee802154.h:229
@ IEEE802154_MR_OQPSK_CHIPS_200
200 kChip/s
Definition: ieee802154.h:230
@ IEEE802154_MR_OQPSK_CHIPS_2000
2000 kChip/s
Definition: ieee802154.h:232
@ IEEE802154_MR_OQPSK_CHIPS_1000
1000 kChip/s
Definition: ieee802154.h:231
@ IEEE802154_MR_FSK_SRATE_50K
50k Symbols/s
Definition: ieee802154.h:217
@ IEEE802154_MR_FSK_SRATE_100K
100k Symbols/s
Definition: ieee802154.h:218
@ IEEE802154_MR_FSK_SRATE_400K
400k Symbols/s
Definition: ieee802154.h:222
@ IEEE802154_MR_FSK_SRATE_150K
150k Symbols/s
Definition: ieee802154.h:219
@ IEEE802154_MR_FSK_SRATE_200K
200k Symbols/s
Definition: ieee802154.h:220
@ IEEE802154_MR_FSK_SRATE_300K
300k Symbols/s
Definition: ieee802154.h:221
@ IEEE802154_PHY_BPSK
Binary Phase Shift Keying.
Definition: ieee802154.h:194
@ IEEE802154_PHY_MR_OFDM
Multi-Rate Orthogonal Frequency-Division Multiplexing.
Definition: ieee802154.h:198
@ IEEE802154_PHY_OQPSK
Offset Quadrature Phase-Shift Keying.
Definition: ieee802154.h:196
@ IEEE802154_PHY_ASK
Amplitude-Shift Keying.
Definition: ieee802154.h:195
@ IEEE802154_PHY_MR_FSK
Multi-Rate Frequency Shift Keying.
Definition: ieee802154.h:199
@ IEEE802154_PHY_DISABLED
PHY disabled, no mode selected.
Definition: ieee802154.h:193
@ IEEE802154_PHY_MR_OQPSK
Multi-Rate Offset Quadrature Phase-Shift Keying.
Definition: ieee802154.h:197
@ IEEE802154_PHY_NO_OP
don't change PHY configuration
Definition: ieee802154.h:201
@ IEEE802154_FEC_RSC
recursive and systematic code
Definition: ieee802154.h:210
@ IEEE802154_FEC_NRNSC
non-recursive and non-systematic code
Definition: ieee802154.h:209
@ IEEE802154_FEC_NONE
no forward error correction
Definition: ieee802154.h:208
Common macros and compiler attributes/pragmas configuration.
Utility header providing time unit defines.
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:70
Data type to represent an EUI-64.
Definition: eui64.h:52
uint8_t uint8[8]
split into 8 8-bit words.
Definition: eui64.h:54
A 16 bit integer in little endian.
Definition: byteorder.h:34