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
32 extern "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 
172 typedef enum {
181 
185 enum {
189 };
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 
373 size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len,
374  const uint8_t *dst, size_t dst_len,
375  le_uint16_t src_pan, le_uint16_t dst_pan,
376  uint8_t flags, uint8_t seq);
377 
388 size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr);
389 
402 int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
403 
416 int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
417 
434 int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
435  network_uint16_t short_addr, const eui64_t *ext_addr);
436 
446 static inline uint8_t ieee802154_get_seq(const uint8_t *mhr)
447 {
448  return mhr[2];
449 }
450 
470 static inline eui64_t *ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr,
471  size_t addr_len)
472 {
473  int i = 0;
474 
475  eui64->uint8[0] = eui64->uint8[1] = 0;
476 
477  switch (addr_len) {
478  case 8:
479  eui64->uint8[0] = addr[i++] ^ 0x02;
480  eui64->uint8[1] = addr[i++];
481  eui64->uint8[2] = addr[i++];
482  eui64->uint8[3] = addr[i++];
483  eui64->uint8[4] = addr[i++];
484  eui64->uint8[5] = addr[i++];
485  eui64->uint8[6] = addr[i++];
486  eui64->uint8[7] = addr[i++];
487  break;
488 
489  case 4:
490  eui64->uint8[0] = addr[i++] ^ 0x02;
491  eui64->uint8[1] = addr[i++];
492 
493  /* Falls through. */
494  case 2:
495  eui64->uint8[2] = 0;
496  eui64->uint8[3] = 0xff;
497  eui64->uint8[4] = 0xfe;
498  eui64->uint8[5] = 0;
499  eui64->uint8[6] = addr[i++];
500  eui64->uint8[7] = addr[i++];
501  break;
502 
503  default:
504  return NULL;
505  }
506 
507  return eui64;
508 }
509 
526 static inline int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
527 {
528  return rssi + IEEE802154_RADIO_RSSI_OFFSET;
529 }
530 
544 static inline uint8_t ieee802154_dbm_to_rssi(int16_t dbm)
545 {
546  const int min = IEEE802154_RADIO_RSSI_OFFSET;
547  const int max = min + (UINT8_MAX - 1);
548 
549  int val = dbm <= min ? min : (dbm >= max ? max : dbm);
550  return val - IEEE802154_RADIO_RSSI_OFFSET;
551 }
552 
553 #ifdef __cplusplus
554 }
555 #endif
556 
557 #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:544
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:470
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:446
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:526
#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:77
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:41