Blame view

RIOT/sys/include/net/sixlowpan.h 7.23 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  /*
   * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    net_sixlowpan   6LoWPAN
   * @ingroup     net
   * @brief       Provides 6LoWPAN dispatch types and helper functions
   * @{
   *
   * @file
   * @brief   6LoWPAN dispatch type and helper function definitons.
   *
   * @author  Martine Lenders <mlenders@inf.fu-berlin.de>
   */
  #ifndef NET_SIXLOWPAN_H
  #define NET_SIXLOWPAN_H
  
  #include <stdbool.h>
  #include <stddef.h>
  #include <stdint.h>
  
  #include "byteorder.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @name    6LoWPAN dispatch definition
   * @see     <a href="https://tools.ietf.org/html/rfc4944#section-5.1">
   *              RFC 4944, section 5.1
   *          </a>
   * @{
   */
  #define SIXLOWPAN_UNCOMP            (0x41)      /**< uncompressed 6LoWPAN frame dispatch. */
  #define SIXLOWPAN_FRAG_DISP_MASK    (0xf8)      /**< mask for fragmentation
                                                   *   dispatch */
  #define SIXLOWPAN_FRAG_1_DISP       (0xc0)      /**< dispatch for 1st fragment */
  #define SIXLOWPAN_FRAG_N_DISP       (0xe0)      /**< dispatch for subsequent
                                                   *   fragments */
  #define SIXLOWPAN_FRAG_MAX_LEN      (2047)      /**< Maximum datagram size @f$ (2^{11} - 1) @f$ */
  
  /**
   * @brief   Dispatch mask for LOWPAN_IPHC.
   * @see     <a href="https://tools.ietf.org/html/rfc6282#section-3.1">
   *              RFC 6282, section 3.1
   *          </a>
   */
  #define SIXLOWPAN_IPHC1_DISP_MASK   (0xe0)
  
  /**
   * @brief   Dispatch for LOWPAN_IPHC.
   * @see     <a href="https://tools.ietf.org/html/rfc6282#section-3.1">
   *              RFC 6282, section 3.1
   *          </a>
   */
  #define SIXLOWPAN_IPHC1_DISP        (0x60)
  
  /**
   * @brief   Checks if dispatch indicates that frame is not a 6LoWPAN (NALP) frame.
   *
   * @param[in] disp  The first byte of a frame.
   *
   * @return  true, if frame is a NALP.
   * @return  false, if frame is not a NALP.
   */
  static inline bool sixlowpan_nalp(uint8_t disp)
  {
      return ((disp & 0xc0) == 0);
  }
  /** @} */
  
  /**
   * @name    6LoWPAN fragmentation header definitions
   * @{
   */
  #define SIXLOWPAN_FRAG_SIZE_MASK    (0x07ff)    /**< mask for datagram size */
  
  /**
   * @brief   General and 1st 6LoWPAN fragmentation header
   *
   * @note    The general 6LoWPAN fragmentation header refers to the first 4
   *          bytes of a \c FRAG0 or \c FRAGN fragmentation header, which are
   *          identical.
   *
   * @see <a href="https://tools.ietf.org/html/rfc4944#section-5.3">
   *          RFC 4944, section 5.3
   *      </a>
   */
  typedef struct __attribute__((packed)) {
      /**
       * @brief   Dispatch and datagram size.
       *
       * @details The 5 most significant bits are the dispatch, the remaining
       *          bits are the size.
       */
      network_uint16_t disp_size;
      network_uint16_t tag;       /**< datagram tag */
  } sixlowpan_frag_t;
  
  /**
   * @brief   Subsequent 6LoWPAN fragmentation header
   *
   * @see <a href="https://tools.ietf.org/html/rfc4944#section-5.3">
   *          RFC 4944, section 5.3
   *      </a>
   *
   * @extends sixlowpan_frag_t
   */
  typedef struct __attribute__((packed)) {
      /**
       * @brief   Dispatch and datagram size.
       *
       * @details The 5 most significant bits are the dispatch, the remaining
       *          bits are the size.
       */
      network_uint16_t disp_size;
      network_uint16_t tag;       /**< datagram tag */
      uint8_t offset;             /**< offset */
  } sixlowpan_frag_n_t;
  
  /**
   * @brief   Checks if a given fragment is a 6LoWPAN fragment.
   *
   * @param[in] hdr   A 6LoWPAN fragmentation header.
   *
   * @return  true, if given fragment is a 6LoWPAN fragment.
   * @return  false, if given fragment is not a 6LoWPAN fragment.
   */
  static inline bool sixlowpan_frag_is(sixlowpan_frag_t *hdr)
  {
      return ((hdr->disp_size.u8[0] & SIXLOWPAN_FRAG_DISP_MASK) ==
              SIXLOWPAN_FRAG_1_DISP) ||
             ((hdr->disp_size.u8[0] & SIXLOWPAN_FRAG_DISP_MASK) ==
              SIXLOWPAN_FRAG_N_DISP);
  }
  /** @} */
  
  /**
   * @name    6LoWPAN IPHC dispatch definitions
   * @{
   */
  /**
   * @brief   Flag for Traffic Class & Flow Label elision (part of first byte of
   *          LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC1_TF          (0x18)
  
  /**
   * @brief   Flag for Next Header Compression (part of first byte of
   *          LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC1_NH          (0x04)
  
  /**
   * @brief   Flag for Hop Limit elision (part of first byte of LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC1_HL          (0x03)
  
  /**
   * @brief   Flag for Context Identifier Extention (part of second byte
   *          of LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_CID_EXT     (0x80)
  
  /**
   * @brief   Flag for Source Address Compression (part of second byte
   *          of LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_SAC         (0x40)
  
  /**
   * @brief   Bits for Source Address Mode (part of second byte of
   *          LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_SAM         (0x30)
  
  /**
   * @brief   Flag for Destination Address Compression (part of second
   *          byte of LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_DAC         (0x04)
  
  /**
   * @brief   Bits for Destination Address Mode (part of second byte of
   *          LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_DAM         (0x03)
  
  /**
   * @brief   Flag for Multicast Compression (part of second byte of
   *          LOWPAN_IPHC).
   * @see <a href="http://tools.ietf.org/html/rfc6282#section-3.1.1">
   *          RFC 6282, section 3.1.1
   *      </a>
   */
  #define SIXLOWPAN_IPHC2_M           (0x08)
  
  /**
   * @brief   6LoWPAN IPHC header length
   */
  #define SIXLOWPAN_IPHC_HDR_LEN      (2)
  
  /**
   * @brief   6LoWPAN context idendifier extension header length
   */
  #define SIXLOWPAN_IPHC_CID_EXT_LEN  (1)
  
  /**
   * @brief   Checks if datagram is an IPHC datagram.
   *
   * @param[in] data  Data of a datagram, may not be NULL.
   *
   * @return  true, if datagram is an IPHC datagram.
   * @return  false, if datagram is not an IPHC datagram.
   */
  static inline bool sixlowpan_iphc_is(uint8_t *data)
  {
      return ((*data & SIXLOWPAN_IPHC1_DISP_MASK) == SIXLOWPAN_IPHC1_DISP);
  }
  /** @} */
  
  /**
   * @brief   Prints 6LoWPAN dispatch to stdout.
   *
   * @param[in] data  A 6LoWPAN frame.
   * @param[in] size  Size of @p data.
   */
  void sixlowpan_print(uint8_t *data, size_t size);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* NET_SIXLOWPAN_H */
  /** @} */