sys/include/net/ieee802154.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-16 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 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
38 #define IEEE802154_SHORT_ADDRESS_LEN (2U)
39 #define IEEE802154_LONG_ADDRESS_LEN (8U)
48 #define IEEE802154_MAX_HDR_LEN (23U)
49 #define IEEE802154_MIN_FRAME_LEN (IEEE802154_FCF_LEN + sizeof(uint8_t))
50 
51 #define IEEE802154_FCF_LEN (2U)
52 #define IEEE802154_FCS_LEN (2U)
53 
54 #define IEEE802154_FCF_TYPE_MASK (0x07)
55 #define IEEE802154_FCF_TYPE_BEACON (0x00)
56 #define IEEE802154_FCF_TYPE_DATA (0x01)
57 #define IEEE802154_FCF_TYPE_ACK (0x02)
58 #define IEEE802154_FCF_TYPE_MACCMD (0x03)
59 
60 #define IEEE802154_FCF_SECURITY_EN (0x08)
61 #define IEEE802154_FCF_FRAME_PEND (0x10)
62 #define IEEE802154_FCF_ACK_REQ (0x20)
63 #define IEEE802154_FCF_PAN_COMP (0x40)
65 #define IEEE802154_FCF_DST_ADDR_MASK (0x0c)
66 #define IEEE802154_FCF_DST_ADDR_VOID (0x00)
67 #define IEEE802154_FCF_DST_ADDR_RESV (0x04)
68 #define IEEE802154_FCF_DST_ADDR_SHORT (0x08)
69 #define IEEE802154_FCF_DST_ADDR_LONG (0x0c)
71 #define IEEE802154_FCF_VERS_MASK (0x30)
72 #define IEEE802154_FCF_VERS_V0 (0x00)
73 #define IEEE802154_FCF_VERS_V1 (0x10)
74 
75 #define IEEE802154_FCF_SRC_ADDR_MASK (0xc0)
76 #define IEEE802154_FCF_SRC_ADDR_VOID (0x00)
77 #define IEEE802154_FCF_SRC_ADDR_RESV (0x40)
78 #define IEEE802154_FCF_SRC_ADDR_SHORT (0x80)
79 #define IEEE802154_FCF_SRC_ADDR_LONG (0xc0)
86 #define IEEE802154_CHANNEL_MIN_SUBGHZ (0U)
87 #define IEEE802154_CHANNEL_MAX_SUBGHZ (10U)
88 #define IEEE802154_CHANNEL_MIN (11U)
89 #define IEEE802154_CHANNEL_MAX (26U)
92 #define IEEE802154_FRAME_LEN_MAX (127U)
101 #define IEEE802154_ADDR_BCAST { 0xff, 0xff }
102 
106 #define IEEE802154_ADDR_BCAST_LEN (IEEE802154_SHORT_ADDRESS_LEN)
107 
119 #ifndef IEEE802154_DEFAULT_SUBGHZ_CHANNEL
120 #define IEEE802154_DEFAULT_SUBGHZ_CHANNEL (5U)
121 #endif
122 
123 #ifndef IEEE802154_DEFAULT_CHANNEL
124 #define IEEE802154_DEFAULT_CHANNEL (26U)
125 #endif
126 
127 #ifndef IEEE802154_DEFAULT_SUBGHZ_PAGE
128 #define IEEE802154_DEFAULT_SUBGHZ_PAGE (2U)
129 #endif
130 
131 #ifndef IEEE802154_DEFAULT_PANID
132 #define IEEE802154_DEFAULT_PANID (0x0023U)
133 #endif
134 
135 #ifndef IEEE802154_DEFAULT_TXPOWER
136 #define IEEE802154_DEFAULT_TXPOWER (0) /* in dBm */
137 #endif
138 
183 size_t ieee802154_set_frame_hdr(uint8_t *buf, const uint8_t *src, size_t src_len,
184  const uint8_t *dst, size_t dst_len,
185  le_uint16_t src_pan, le_uint16_t dst_pan,
186  uint8_t flags, uint8_t seq);
187 
198 size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr);
199 
212 int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
213 
226 int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
227 
237 static inline uint8_t ieee802154_get_seq(const uint8_t *mhr)
238 {
239  return mhr[2];
240 }
241 
261 static inline eui64_t *ieee802154_get_iid(eui64_t *eui64, const uint8_t *addr,
262  size_t addr_len)
263 {
264  int i = 0;
265 
266  eui64->uint8[0] = eui64->uint8[1] = 0;
267 
268  switch (addr_len) {
269  case 8:
270  eui64->uint8[0] = addr[i++] ^ 0x02;
271  eui64->uint8[1] = addr[i++];
272  eui64->uint8[2] = addr[i++];
273  eui64->uint8[3] = addr[i++];
274  eui64->uint8[4] = addr[i++];
275  eui64->uint8[5] = addr[i++];
276  eui64->uint8[6] = addr[i++];
277  eui64->uint8[7] = addr[i++];
278  break;
279 
280  case 4:
281  eui64->uint8[0] = addr[i++] ^ 0x02;
282  eui64->uint8[1] = addr[i++];
283 
284  /* Falls through. */
285  case 2:
286  eui64->uint8[2] = 0;
287  eui64->uint8[3] = 0xff;
288  eui64->uint8[4] = 0xfe;
289  eui64->uint8[5] = 0;
290  eui64->uint8[6] = addr[i++];
291  eui64->uint8[7] = addr[i++];
292  break;
293 
294  default:
295  return NULL;
296  }
297 
298  return eui64;
299 }
300 
301 #ifdef __cplusplus
302 }
303 #endif
304 
305 #endif /* NET_IEEE802154_H */
306 
EUI-64 data type definition.
static uint8_t ieee802154_get_seq(const uint8_t *mhr)
Gets sequence number from MAC header.
int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan)
Gets destination address from MAC header.
uint8_t uint8[8]
split into 8 8-bit words.
Definition: eui64.h:39
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.
int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan)
Gets source address from MAC header.
A 16 bit integer in little endian.
Definition: byteorder.h:40
const uint8_t ieee802154_addr_bcast[IEEE802154_ADDR_BCAST_LEN]
Broadcast address.
Functions to work with different byte orders.
Data type to represent an EUI-64.
Definition: eui64.h:37
stdlib.h wrapper for MSP430
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.
size_t ieee802154_get_frame_hdr_len(const uint8_t *mhr)
Get length of MAC header.
#define IEEE802154_ADDR_BCAST_LEN
Length in byte of IEEE802154_ADDR_BCAST.