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 "kernel_defines.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 
122 #define IEEE802154_RADIO_RSSI_OFFSET (-174)
123 
124 #define IEEE802154_PHY_MR_FSK_PHR_LEN (2)
125 #define IEEE802154_PHY_MR_FSK_2FSK_SFD_LEN (2)
135 #define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_0 (0x6F4E)
136 #define IEEE802154_PHY_MR_FSK_2FSK_CODED_SFD_1 (0x632D)
137 #define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_0 (0x90E4)
138 #define IEEE802154_PHY_MR_FSK_2FSK_UNCODED_SFD_1 (0x7A0E)
147 #define IEEE802154G_ATURNAROUNDTIME_US (1 * US_PER_MS)
148 
153 #define IEEE802154_ATURNAROUNDTIME_IN_SYMBOLS (12)
154 
162 #define IEEE802154_CCA_DURATION_IN_SYMBOLS (8)
163 
167 typedef enum {
176 
180 enum {
184 };
185 
193 #define IEEE802154_ADDR_BCAST { 0xff, 0xff }
194 
198 #define IEEE802154_ADDR_BCAST_LEN (IEEE802154_SHORT_ADDRESS_LEN)
199 
215 #if IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_BPSK)
216 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_BPSK
217 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_ASK)
218 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_ASK
219 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_OQPSK)
220 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
221 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OQPSK)
222 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OQPSK
223 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_OFDM)
224 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_OFDM
225 #elif IS_ACTIVE(CONFIG_IEEE802154_DEFAULT_PHY_MR_FSK)
226 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_MR_FSK
227 #endif
228 
229 #ifndef CONFIG_IEEE802154_DEFAULT_PHY_MODE
230 #define CONFIG_IEEE802154_DEFAULT_PHY_MODE IEEE802154_PHY_OQPSK
231 #endif
236 #ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL
237 #define CONFIG_IEEE802154_DEFAULT_SUBGHZ_CHANNEL (5U)
238 #endif
239 
243 #ifndef CONFIG_IEEE802154_DEFAULT_CHANNEL
244 #define CONFIG_IEEE802154_DEFAULT_CHANNEL (26U)
245 #endif
246 
250 #ifndef CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE
251 #define CONFIG_IEEE802154_DEFAULT_SUBGHZ_PAGE (2U)
252 #endif
253 
257 #ifndef CONFIG_IEEE802154_DEFAULT_PANID
258 #define CONFIG_IEEE802154_DEFAULT_PANID (0x0023U)
259 #endif
260 
264 #ifndef IEEE802154_PANID_BCAST
265 #define IEEE802154_PANID_BCAST { 0xff, 0xff }
266 #endif
267 
271 #ifndef CONFIG_IEEE802154_DEFAULT_TXPOWER
272 #define CONFIG_IEEE802154_DEFAULT_TXPOWER (0)
273 #endif
279 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE
280 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MIN_BE (3U)
281 #endif
282 
286 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES
287 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_RETRIES (4U)
288 #endif
289 
293 #ifndef CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE
294 #define CONFIG_IEEE802154_DEFAULT_CSMA_CA_MAX_BE (5U)
295 #endif
296 
300 #ifndef CONFIG_IEEE802154_CCA_THRESH_DEFAULT
301 #define CONFIG_IEEE802154_CCA_THRESH_DEFAULT (-70)
302 #endif
303 
307 #ifdef DOXYGEN
308 #define CONFIG_IEEE802154_AUTO_ACK_DISABLE 0
309 #endif
310 
354 size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len,
355  const uint8_t *dst, size_t dst_len,
356  le_uint16_t src_pan, le_uint16_t dst_pan,
357  uint8_t flags, uint8_t seq);
358 
369 size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr);
370 
383 int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
384 
397 int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
398 
415 int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
416  network_uint16_t short_addr, const eui64_t *ext_addr);
417 
427 static inline uint8_t ieee802154_get_seq(const uint8_t *mhr)
428 {
429  return mhr[2];
430 }
431 
451 static inline eui64_t *ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr,
452  size_t addr_len)
453 {
454  int i = 0;
455 
456  eui64->uint8[0] = eui64->uint8[1] = 0;
457 
458  switch (addr_len) {
459  case 8:
460  eui64->uint8[0] = addr[i++] ^ 0x02;
461  eui64->uint8[1] = addr[i++];
462  eui64->uint8[2] = addr[i++];
463  eui64->uint8[3] = addr[i++];
464  eui64->uint8[4] = addr[i++];
465  eui64->uint8[5] = addr[i++];
466  eui64->uint8[6] = addr[i++];
467  eui64->uint8[7] = addr[i++];
468  break;
469 
470  case 4:
471  eui64->uint8[0] = addr[i++] ^ 0x02;
472  eui64->uint8[1] = addr[i++];
473 
474  /* Falls through. */
475  case 2:
476  eui64->uint8[2] = 0;
477  eui64->uint8[3] = 0xff;
478  eui64->uint8[4] = 0xfe;
479  eui64->uint8[5] = 0;
480  eui64->uint8[6] = addr[i++];
481  eui64->uint8[7] = addr[i++];
482  break;
483 
484  default:
485  return NULL;
486  }
487 
488  return eui64;
489 }
490 
507 static inline int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
508 {
509  return rssi + IEEE802154_RADIO_RSSI_OFFSET;
510 }
511 
525 static inline uint8_t ieee802154_dbm_to_rssi(int16_t dbm)
526 {
527  const int min = IEEE802154_RADIO_RSSI_OFFSET;
528  const int max = min + (UINT8_MAX - 1);
529 
530  int val = dbm <= min ? min : (dbm >= max ? max : dbm);
531  return val - IEEE802154_RADIO_RSSI_OFFSET;
532 }
533 
534 #ifdef __cplusplus
535 }
536 #endif
537 
538 #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:198
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:525
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:451
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:427
ieee802154_phy_mode_t
802.15.4 PHY modes
Definition: ieee802154.h:167
static int16_t ieee802154_rssi_to_dbm(uint8_t rssi)
Convert from RSSI scale to dBm.
Definition: ieee802154.h:507
#define IEEE802154_RADIO_RSSI_OFFSET
value of measured power when RSSI is zero.
Definition: ieee802154.h:122
@ IEEE802154_PHY_BPSK
Binary Phase Shift Keying.
Definition: ieee802154.h:169
@ IEEE802154_PHY_MR_OFDM
Multi-Rate Orthogonal Frequency-Division Multiplexing.
Definition: ieee802154.h:173
@ IEEE802154_PHY_OQPSK
Offset Quadrature Phase-Shift Keying.
Definition: ieee802154.h:171
@ IEEE802154_PHY_ASK
Amplitude-Shift Keying.
Definition: ieee802154.h:170
@ IEEE802154_PHY_MR_FSK
Multi-Rate Frequency Shift Keying.
Definition: ieee802154.h:174
@ IEEE802154_PHY_DISABLED
PHY disabled, no mode selected.
Definition: ieee802154.h:168
@ IEEE802154_PHY_MR_OQPSK
Multi-Rate Offset Quadrature Phase-Shift Keying.
Definition: ieee802154.h:172
@ IEEE802154_FEC_RSC
recursive and systematic code
Definition: ieee802154.h:183
@ IEEE802154_FEC_NRNSC
non-recursive and non-systematic code
Definition: ieee802154.h:182
@ IEEE802154_FEC_NONE
no forward error correction
Definition: ieee802154.h:181
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