Blame view

RIOT/sys/include/seq.h 13.4 KB
fb11e647   vrobic   reseau statique a...
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  /*
   * Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
   *
   * 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    sys_seq     Serial Number Arithmetic
   * @ingroup     sys
   * @brief       Serial Number Arithmetic (RFC 1982)
   * @{
   *
   * @file
   * @brief       Serial Number Arithmetic (RFC 1982)
   * @see         <a href="https://tools.ietf.org/html/rfc1982">
   *                  RFC 1982 - Serial Number Arithmetic
   *              </a>
   *
   * @author      Cenk Gündoğan <cnkgndgn@gmail.com>
   */
  
  #ifndef SEQ_H
  #define SEQ_H
  
  #include <stdint.h>
  #include <errno.h>
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief       Maximum for the addition of a positive integer. X denotes the size of the space
   */
  #define SEQ_LIMIT(X)            (X >> 1)
  
  /**
   * @brief       A 8 bit sequence number
   */
  typedef uint8_t seq8_t;
  
  /**
   * @brief       A 16 bit sequence number
   */
  typedef uint16_t seq16_t;
  
  /**
   * @brief       A 32 bit sequence number
   */
  typedef uint32_t seq32_t;
  
  /**
   * @brief       A 64 bit sequence number
   */
  typedef uint64_t seq64_t;
  
  /**
   * @brief           Addition of a 8 bit sequence number @p s and a positive integer @p n
   *                  in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. ((@p space / 2) - 1)]
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  seq8_t seq8_adds(seq8_t s, uint8_t n, uint8_t space);
  
  /**
   * @brief           Addition of a 8 bit sequence number @p s and a positive integer @p n
   *                  in the serial number space UINT8_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. 127]
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  static inline seq8_t seq8_add(seq8_t s, uint8_t n)
  {
      return seq8_adds(s, n, UINT8_MAX);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number @p space
   * @param[in]       s       sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + 1
   */
  static inline seq8_t seq8_incs(seq8_t s, uint8_t space)
  {
      return seq8_adds(s, 1, space);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number space UINT8_MAX
   * @param[in]       s       sequence number
   * @return          s + 1
   */
  static inline seq8_t seq8_inc(seq8_t s)
  {
      return seq8_adds(s, 1, UINT8_MAX);
  }
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  int seq8_compares(seq8_t s1, seq8_t s2, uint8_t space);
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number space UINT8_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  static inline int seq8_compare(seq8_t s1, seq8_t s2)
  {
      return seq8_compares(s1, s2, UINT8_MAX);
  }
  
  /**
   * @brief           Addition of a 16 bit sequence number @p s and a positive integer @p n
   *                  in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. ((@p space / 2) - 1)]
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  seq16_t seq16_adds(seq16_t s, uint16_t n, uint16_t space);
  
  /**
   * @brief           Addition of a 16 bit sequence number @p s and a positive integer @p n
   *                  in the serial number space UINT16_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. 127]
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  static inline seq16_t seq16_add(seq16_t s, uint16_t n)
  {
      return seq16_adds(s, n, UINT16_MAX);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number @p space
   * @param[in]       s       sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + 1
   */
  static inline seq16_t seq16_incs(seq16_t s, uint16_t space)
  {
      return seq16_adds(s, 1, space);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number space UINT16_MAX
   * @param[in]       s       sequence number
   * @return          s + 1
   */
  static inline seq16_t seq16_inc(seq16_t s)
  {
      return seq16_adds(s, 1, UINT16_MAX);
  }
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  int seq16_compares(seq16_t s1, seq16_t s2, uint16_t space);
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number space UINT16_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  static inline int seq16_compare(seq16_t s1, seq16_t s2)
  {
      return seq16_compares(s1, s2, UINT16_MAX);
  }
  
  /**
   * @brief           Addition of a 32 bit sequence number @p s and a positive integer @p n
   *                  in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. ((@p space / 2) - 1)]
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  seq32_t seq32_adds(seq32_t s, uint32_t n, uint32_t space);
  
  /**
   * @brief           Addition of a 32 bit sequence number @p s and a positive integer @p n
   *                  in the serial number space UINT32_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. 127]
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  static inline seq32_t seq32_add(seq32_t s, uint32_t n)
  {
      return seq32_adds(s, n, UINT32_MAX);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number @p space
   * @param[in]       s       sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + 1
   */
  static inline seq32_t seq32_incs(seq32_t s, uint32_t space)
  {
      return seq32_adds(s, 1, space);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number space UINT32_MAX
   * @param[in]       s       sequence number
   * @return          s + 1
   */
  static inline seq32_t seq32_inc(seq32_t s)
  {
      return seq32_adds(s, 1, UINT32_MAX);
  }
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  int seq32_compares(seq32_t s1, seq32_t s2, uint32_t space);
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number space UINT32_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  static inline int seq32_compare(seq32_t s1, seq32_t s2)
  {
      return seq32_compares(s1, s2, UINT32_MAX);
  }
  
  /**
   * @brief           Addition of a 64 bit sequence number @p s and a positive integer @p n
   *                  in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. ((@p space / 2) - 1)]
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  seq64_t seq64_adds(seq64_t s, uint64_t n, uint64_t space);
  
  /**
   * @brief           Addition of a 64 bit sequence number @p s and a positive integer @p n
   *                  in the serial number space UINT64_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.1">
   *                      3.1. Addition
   *                  </a>
   * @param[in]       s       sequence number
   * @param[in]       n       positive integer in the range of [0 .. 127]
   * @return          s + n, if valid
   * @return          s, if n is out of range
   */
  static inline seq64_t seq64_add(seq64_t s, uint64_t n)
  {
      return seq64_adds(s, n, UINT64_MAX);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number @p space
   * @param[in]       s       sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          s + 1
   */
  static inline seq64_t seq64_incs(seq64_t s, uint64_t space)
  {
      return seq64_adds(s, 1, space);
  }
  
  /**
   * @brief           Increment a sequence number @p s by 1 in the serial number space UINT64_MAX
   * @param[in]       s       sequence number
   * @return          s + 1
   */
  static inline seq64_t seq64_inc(seq64_t s)
  {
      return seq64_adds(s, 1, UINT64_MAX);
  }
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number @p space
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @param[in]       space   serial number space must be a power of 2 minus 1
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  int seq64_compares(seq64_t s1, seq64_t s2, uint64_t space);
  
  /**
   * @brief           Compare sequence numbers @p s1, @p s2 in the serial number space UINT64_MAX
   * @see             <a href="https://tools.ietf.org/html/rfc1982#section-3.2">
   *                      3.2. Comparison
   *                  </a>
   * @param[in]       s1      first sequence number
   * @param[in]       s2      second sequence number
   * @return          -1, if s1 < s2
   * @return          0, if s1 == s2
   * @return          1, if s1 > s2
   * @return          -EINVAL, if comparison of the pair (s1,s2) is undefined
   */
  static inline int seq64_compare(seq64_t s1, seq64_t s2)
  {
      return seq64_compares(s1, s2, UINT64_MAX);
  }
  
  #ifdef __cplusplus
  }
  #endif
  
  /** @} */
  #endif /* SEQ_H */