Contiki-NG
sixp-pkt.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Yasuyuki Tanaka.
3  * Copyright (c) 2016, Centre for Development of Advanced Computing (C-DAC).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup sixtop
33  * @{
34  */
35 /**
36  * \file
37  * 6top Protocol (6P) Packet Manipulation
38  * \author
39  * Shalu R <shalur@cdac.in>
40  * Lijo Thomas <lijo@cdac.in>
41  * Yasuyuki Tanaka <yasuyuki.tanaka@inf.ethz.ch>
42  */
43 #include "contiki.h"
44 #include "contiki-lib.h"
45 #include "lib/assert.h"
46 #include "net/packetbuf.h"
47 #include "net/mac/tsch/tsch.h"
48 
49 #include "sixp.h"
50 #include "sixp-pkt.h"
51 
52 /* Log configuration */
53 #include "sys/log.h"
54 #define LOG_MODULE "6top"
55 #define LOG_LEVEL LOG_LEVEL_6TOP
56 
57 static int32_t get_metadata_offset(sixp_pkt_type_t type, sixp_pkt_code_t code);
58 static int32_t get_cell_options_offset(sixp_pkt_type_t type,
59  sixp_pkt_code_t code);
60 static int32_t get_num_cells_offset(sixp_pkt_type_t type, sixp_pkt_code_t code);
61 static int32_t get_reserved_offset(sixp_pkt_type_t type, sixp_pkt_code_t code);
62 static int32_t get_offset_offset(sixp_pkt_type_t type, sixp_pkt_code_t code);
63 static int32_t get_max_num_cells_offset(sixp_pkt_type_t type,
64  sixp_pkt_code_t code);
65 static int32_t get_cell_list_offset(sixp_pkt_type_t type, sixp_pkt_code_t code);
66 static int32_t get_rel_cell_list_offset(sixp_pkt_type_t type,
67  sixp_pkt_code_t code);
68 static int32_t get_total_num_cells_offset(sixp_pkt_type_t type,
69  sixp_pkt_code_t code);
70 static int32_t get_payload_offset(sixp_pkt_type_t type,
71  sixp_pkt_code_t code);
72 
73 /*---------------------------------------------------------------------------*/
74 static int32_t
75 get_metadata_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
76 {
77  if(type == SIXP_PKT_TYPE_REQUEST) {
78  return 0; /* offset */
79  }
80  return -1;
81 }
82 /*---------------------------------------------------------------------------*/
83 static int32_t
84 get_cell_options_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
85 {
86  if(type == SIXP_PKT_TYPE_REQUEST &&
87  (code.cmd == SIXP_PKT_CMD_ADD ||
88  code.cmd == SIXP_PKT_CMD_DELETE ||
89  code.cmd == SIXP_PKT_CMD_RELOCATE ||
90  code.cmd == SIXP_PKT_CMD_COUNT ||
91  code.cmd == SIXP_PKT_CMD_LIST)) {
92  return sizeof(sixp_pkt_metadata_t);
93  }
94  return -1;
95 }
96 /*---------------------------------------------------------------------------*/
97 static int32_t
98 get_num_cells_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
99 {
100  if(type == SIXP_PKT_TYPE_REQUEST &&
101  (code.value == SIXP_PKT_CMD_ADD ||
102  code.value == SIXP_PKT_CMD_DELETE ||
103  code.value == SIXP_PKT_CMD_RELOCATE)) {
104  return sizeof(sixp_pkt_metadata_t) + sizeof(sixp_pkt_cell_options_t);
105  }
106 
107  return -1;
108 }
109 /*---------------------------------------------------------------------------*/
110 static int32_t
111 get_reserved_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
112 {
113  if(type == SIXP_PKT_TYPE_REQUEST &&
114  code.value == SIXP_PKT_CMD_LIST) {
115  return sizeof(sixp_pkt_metadata_t) + sizeof(sixp_pkt_cell_options_t);
116  }
117  return -1;
118 }
119 /*---------------------------------------------------------------------------*/
120 static int32_t
121 get_offset_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
122 {
123  if(type == SIXP_PKT_TYPE_REQUEST &&
124  code.value == SIXP_PKT_CMD_LIST) {
125  return (sizeof(sixp_pkt_metadata_t) +
126  sizeof(sixp_pkt_cell_options_t) +
127  sizeof(sixp_pkt_reserved_t));
128  }
129  return -1;
130 }
131 /*---------------------------------------------------------------------------*/
132 static int32_t
133 get_max_num_cells_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
134 {
135  if(type == SIXP_PKT_TYPE_REQUEST &&
136  code.value == SIXP_PKT_CMD_LIST) {
137  return (sizeof(sixp_pkt_metadata_t) +
138  sizeof(sixp_pkt_cell_options_t) +
139  sizeof(sixp_pkt_reserved_t) +
140  sizeof(sixp_pkt_offset_t));
141  }
142  return -1;
143 }
144 /*---------------------------------------------------------------------------*/
145 static int32_t
146 get_cell_list_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
147 {
148  if(type == SIXP_PKT_TYPE_REQUEST && (code.value == SIXP_PKT_CMD_ADD ||
149  code.value == SIXP_PKT_CMD_DELETE)) {
150  return (sizeof(sixp_pkt_metadata_t) +
151  sizeof(sixp_pkt_cell_options_t) +
152  sizeof(sixp_pkt_num_cells_t));
153  } else if((type == SIXP_PKT_TYPE_RESPONSE ||
154  type == SIXP_PKT_TYPE_CONFIRMATION) &&
155  (code.value == SIXP_PKT_RC_SUCCESS ||
156  code.value == SIXP_PKT_RC_EOL)) {
157  return 0;
158  }
159  return -1;
160 }
161 /*---------------------------------------------------------------------------*/
162 static int32_t
163 get_rel_cell_list_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
164 {
165  if(type == SIXP_PKT_TYPE_REQUEST && code.value == SIXP_PKT_CMD_RELOCATE) {
166  return (sizeof(sixp_pkt_metadata_t) +
167  sizeof(sixp_pkt_cell_options_t) +
168  sizeof(sixp_pkt_num_cells_t));
169  }
170  return -1;
171 }
172 /*---------------------------------------------------------------------------*/
173 static int32_t
174 get_total_num_cells_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
175 {
176  if(type == SIXP_PKT_TYPE_RESPONSE && code.value == SIXP_PKT_RC_SUCCESS) {
177  return 0;
178  }
179  return -1;
180 }
181 /*---------------------------------------------------------------------------*/
182 static int32_t
183 get_payload_offset(sixp_pkt_type_t type, sixp_pkt_code_t code)
184 {
185  if(type == SIXP_PKT_TYPE_REQUEST && code.value == SIXP_PKT_CMD_SIGNAL) {
186  return sizeof(sixp_pkt_metadata_t);
187  } else if((type == SIXP_PKT_TYPE_RESPONSE ||
188  type == SIXP_PKT_TYPE_CONFIRMATION) &&
189  code.value == SIXP_PKT_RC_SUCCESS) {
190  return 0;
191  }
192  return -1;
193 }
194 /*---------------------------------------------------------------------------*/
195 int
197  sixp_pkt_metadata_t metadata,
198  uint8_t *body, uint16_t body_len)
199 {
200  int32_t offset;
201 
202  if(body == NULL) {
203  LOG_ERR("6P-pkt: cannot set metadata; body is null\n");
204  return -1;
205  }
206 
207  if((offset = get_metadata_offset(type, code)) < 0) {
208  LOG_ERR("6P-pkt: cannot set metadata [type=%u, code=%u], invalid type\n",
209  type, code.value);
210  return -1;
211  }
212 
213  if(body_len < (offset + sizeof(metadata))) {
214  LOG_ERR("6P-pkt: cannot set metadata, body is too short [body_len=%u]\n",
215  body_len);
216  return -1;
217  }
218 
219  /*
220  * Copy the content into the Metadata field as it is since 6P has no idea
221  * about the internal structure of the field.
222  */
223  memcpy(body + offset, &metadata, sizeof(metadata));
224 
225  return 0;
226 }
227 /*---------------------------------------------------------------------------*/
228 int
230  sixp_pkt_metadata_t *metadata,
231  const uint8_t *body, uint16_t body_len)
232 {
233  int32_t offset;
234 
235  if(metadata == NULL || body == NULL) {
236  LOG_ERR("6P-pkt: cannot get metadata; invalid argument\n");
237  return -1;
238  }
239 
240  if((offset = get_metadata_offset(type, code)) < 0) {
241  LOG_ERR("6P-pkt: cannot get metadata [type=%u, code=%u], invalid type\n",
242  type, code.value);
243  return -1;
244  }
245 
246  if(body_len < offset + sizeof(*metadata)) {
247  LOG_ERR("6P-pkt: cannot get metadata [type=%u, code=%u], ",
248  type, code.value);
249  LOG_ERR_("body is too short\n");
250  return -1;
251  }
252 
253  /*
254  * Copy the content in the Metadata field as it is since 6P has no idea about
255  * the internal structure of the field.
256  */
257  memcpy(metadata, body + offset, sizeof(*metadata));
258 
259  return 0;
260 }
261 /*---------------------------------------------------------------------------*/
262 int
264  sixp_pkt_cell_options_t cell_options,
265  uint8_t *body, uint16_t body_len)
266 {
267  int32_t offset;
268 
269  if(body == NULL) {
270  LOG_ERR("6P-pkt: cannot set cell_options; body is null\n");
271  return -1;
272  }
273 
274  if((offset = get_cell_options_offset(type, code)) < 0) {
275  LOG_ERR("6P-pkt: cannot set cell_options [type=%u, code=%u], ",
276  type, code.value);
277  LOG_ERR_("invalid type\n");
278  return -1;
279  }
280 
281  if(body_len < (offset + sizeof(cell_options))) {
282  LOG_ERR("6P-pkt: cannot set cell_options, ");
283  LOG_ERR_("body is too short [body_len=%u]\n", body_len);
284  return -1;
285  }
286 
287  /* The CellOption field is an 8-bit bitfield */
288  memcpy(body + offset, &cell_options, sizeof(uint8_t));
289 
290  return 0;
291 }
292 /*---------------------------------------------------------------------------*/
293 int
295  sixp_pkt_cell_options_t *cell_options,
296  const uint8_t *body, uint16_t body_len)
297 {
298  int32_t offset;
299 
300  if(cell_options == NULL || body == NULL) {
301  LOG_ERR("6P-pkt: cannot get cell_options; invalid argument\n");
302  return -1;
303  }
304 
305  if((offset = get_cell_options_offset(type, code)) < 0) {
306  LOG_ERR("6P-pkt: cannot get cell_options [type=%u, code=%u]",
307  type, code.value);
308  LOG_ERR_("invalid type\n");
309  return -1;
310  }
311 
312  if(body_len < (offset + sizeof(*cell_options))) {
313  LOG_ERR("6P-pkt: cannot get cell_options, ");
314  LOG_ERR_("body is too short [body_len=%u]\n", body_len);
315  return -1;
316  }
317 
318  /* The CellOption field is an 8-bit bitfield */
319  memcpy(cell_options, body + offset, sizeof(uint8_t));
320 
321  return 0;
322 }
323 /*---------------------------------------------------------------------------*/
324 int
326  sixp_pkt_num_cells_t num_cells,
327  uint8_t *body, uint16_t body_len)
328 {
329  int32_t offset;
330 
331  if(body == NULL) {
332  LOG_ERR("6P-pkt: cannot set num_cells; body is null\n");
333  return -1;
334  }
335 
336  if((offset = get_num_cells_offset(type, code)) < 0) {
337  LOG_ERR("6P-pkt: cannot set num_cells; ");
338  LOG_ERR_("packet [type=%u, code=%u] won't have NumCells\n",
339  type, code.value);
340  return -1;
341  }
342 
343  memcpy(body + offset, &num_cells, sizeof(uint8_t));
344 
345  return 0;
346 }
347 /*---------------------------------------------------------------------------*/
348 int
350  sixp_pkt_num_cells_t *num_cells,
351  const uint8_t *body, uint16_t body_len)
352 {
353  int32_t offset;
354 
355  if(num_cells == NULL || body == NULL) {
356  LOG_ERR("6P-pkt: cannot get num_cells; invalid argument\n");
357  return -1;
358  }
359 
360  if((offset = get_num_cells_offset(type, code)) < 0) {
361  LOG_ERR("6P-pkt: cannot get num_cells; ");
362  LOG_ERR_("packet [type=%u, code=%u] won't have NumCells\n",
363  type, code.value);
364  return -1;
365  }
366 
367  if(body_len < (offset + sizeof(*num_cells))) {
368  LOG_ERR("6P-pkt: cannot get num_cells; body is too short\n");
369  return -1;
370  }
371 
372  /* NumCells is an 8-bit unsigned integer */
373  memcpy(num_cells, body + offset, sizeof(uint8_t));
374 
375  return 0;
376 }
377 /*---------------------------------------------------------------------------*/
378 int
380  sixp_pkt_reserved_t reserved,
381  uint8_t *body, uint16_t body_len)
382 {
383  int32_t offset;
384 
385  if(body == NULL) {
386  LOG_ERR("6P-pkt: cannot set reserved; body is null\n");
387  return -1;
388  }
389 
390  if((offset = get_reserved_offset(type, code)) < 0) {
391  LOG_ERR("6P-pkt: cannot set reserved; ");
392  LOG_ERR_("packet [type=%u, code=%u] won't have Reserved\n",
393  type, code.value);
394  return -1;
395  }
396 
397  if(body_len < (offset + sizeof(reserved))) {
398  LOG_ERR("6P-pkt: cannot set reserved; body is too short\n");
399  return -1;
400  }
401 
402  /* The Reserved field is an 8-bit field */
403  memcpy(body + offset, &reserved, sizeof(uint8_t));
404 
405  return 0;
406 }
407 /*---------------------------------------------------------------------------*/
408 int
410  sixp_pkt_reserved_t *reserved,
411  const uint8_t *body, uint16_t body_len)
412 {
413  int32_t offset;
414 
415  if(reserved == NULL || body == NULL) {
416  LOG_ERR("6P-pkt: cannot get reserved; invalid argument\n");
417  return -1;
418  }
419 
420  if((offset = get_reserved_offset(type, code)) < 0) {
421  LOG_ERR("6P-pkt: cannot get reserved; ");
422  LOG_ERR_("packet [type=%u, code=%u] won't have Reserved\n",
423  type, code.value);
424  return -1;
425  }
426 
427  /* The Reserved field is an 8-bit field */
428  memcpy(reserved, body + offset, sizeof(uint8_t));
429 
430  return 0;
431 }
432 /*---------------------------------------------------------------------------*/
433 int
435  sixp_pkt_offset_t cell_offset,
436  uint8_t *body, uint16_t body_len)
437 {
438  int32_t offset;
439 
440  if(body == NULL) {
441  LOG_ERR("6P-pkt: cannot set offset; invalid argument\n");
442  return -1;
443  }
444 
445  if((offset = get_offset_offset(type, code)) < 0) {
446  LOG_ERR("6P-pkt: cannot set offset; ");
447  LOG_ERR_("packet [type=%u, code=%u] won't have Offset\n",
448  type, code.value);
449  return -1;
450  }
451 
452  if(body_len < (offset + sizeof(cell_offset))) {
453  LOG_ERR("6P-pkt: cannot set offset; body is too short\n");
454  return -1;
455  }
456 
457  /*
458  * The (Cell)Offset field is 16-bit long; treat it as a little-endian value of
459  * unsigned integer following IEEE 802.15.4-2015.
460  */
461  (body + offset)[0] = *((uint16_t *)&cell_offset) & 0xff;
462  (body + offset)[1] = (*((uint16_t *)&cell_offset) >> 8) & 0xff;
463 
464  return 0;
465 }
466 /*---------------------------------------------------------------------------*/
467 int
469  sixp_pkt_offset_t *cell_offset,
470  const uint8_t *body, uint16_t body_len)
471 {
472  int32_t offset;
473  const uint8_t *p;
474 
475  if(cell_offset == NULL || body == NULL) {
476  LOG_ERR("6P-pkt: cannot get offset; invalid argument\n");
477  return -1;
478  }
479 
480  if((offset = get_offset_offset(type, code)) < 0) {
481  LOG_ERR("6P-pkt: cannot get offset; ");
482  LOG_ERR_("packet [type=%u, code=%u] won't have Offset\n",
483  type, code.value);
484  return -1;
485  }
486 
487  if(body_len < (offset + sizeof(*cell_offset))) {
488  LOG_ERR("6P-pkt: cannot get offset; body is too short\n");
489  return -1;
490  }
491 
492  /*
493  * The (Cell)Offset field is 16-bit long; treat it as a little-endian value of
494  * unsigned integer following IEEE 802.15.4-2015.
495  */
496  p = body + offset;
497  *((uint16_t *)cell_offset) = p[0] + (p[1] << 8);
498 
499  return 0;
500 }
501 /*---------------------------------------------------------------------------*/
502 int
504  sixp_pkt_max_num_cells_t max_num_cells,
505  uint8_t *body, uint16_t body_len)
506 {
507  int32_t offset;
508 
509  if(body == NULL) {
510  LOG_ERR("6P-pkt: cannot set max_num_cells; invalid argument\n");
511  return -1;
512  }
513 
514  if((offset = get_max_num_cells_offset(type, code)) < 0) {
515  LOG_ERR("6P-pkt: cannot set max_num_cells; ");
516  LOG_ERR_("packet [type=%u, code=%u] won't have MaxNumCells\n",
517  type, code.value);
518  return -1;
519  }
520 
521  if(body_len < (offset + sizeof(max_num_cells))) {
522  LOG_ERR("6P-pkt: cannot set max_num_cells; body is too short\n");
523  return -1;
524  }
525 
526  /*
527  * The MaxNumCells field is 16-bit long; treat it as a little-endian value of
528  * unsigned integer following IEEE 802.15.4-2015.
529  */
530  (body + offset)[0] = *((uint16_t *)&max_num_cells) & 0xff;
531  (body + offset)[1] = (*((uint16_t *)&max_num_cells) >> 8) & 0xff;
532 
533  return 0;
534 }
535 /*---------------------------------------------------------------------------*/
536 int
538  sixp_pkt_max_num_cells_t *max_num_cells,
539  const uint8_t *body, uint16_t body_len)
540 {
541  int32_t offset;
542  const uint8_t *p;
543 
544  if(max_num_cells == NULL || body == NULL) {
545  LOG_ERR("6P-pkt: cannot get max_num_cells; invalid argument\n");
546  return -1;
547  }
548 
549  if((offset = get_max_num_cells_offset(type, code)) < 0) {
550  LOG_ERR("6P-pkt: cannot get max_num_cells; ");
551  LOG_ERR_("packet [type=%u, code=%u] won't have MaxNumCells\n",
552  type, code.value);
553  return -1;
554  }
555 
556  if(body_len < (offset + sizeof(*max_num_cells))) {
557  LOG_ERR("6P-pkt: cannot get max_num_cells; body is too short\n");
558  return -1;
559  }
560 
561  /*
562  * The MaxNumCells field is 16-bit long; treat it as a little-endian value of
563  * unsigned integer following IEEE 802.15.4-2015.
564  */
565  p = body + offset;
566  *((uint16_t *)max_num_cells) = p[0] + (p[1] << 8);
567 
568  return 0;
569 }
570 /*---------------------------------------------------------------------------*/
571 int
573  const uint8_t *cell_list,
574  uint16_t cell_list_len,
575  uint16_t cell_offset,
576  uint8_t *body, uint16_t body_len)
577 {
578  int32_t offset;
579 
580  if(cell_list == NULL || body == NULL) {
581  LOG_ERR("6P-pkt: cannot set cell_list; invalid argument\n");
582  return -1;
583  }
584 
585  if((offset = get_cell_list_offset(type, code)) < 0) {
586  LOG_ERR("6P-pkt: cannot set cell_list; ");
587  LOG_ERR_("packet [type=%u, code=%u] won't have CellList\n",
588  type, code.value);
589  return -1;
590  }
591 
592  offset += cell_offset;
593 
594  if(body_len < (offset + cell_list_len)) {
595  LOG_ERR("6P-pkt: cannot set cell_list; body is too short\n");
596  return -1;
597  } else if((cell_list_len % sizeof(sixp_pkt_cell_t)) != 0) {
598  LOG_ERR("6P-pkt: cannot set cell_list; invalid {body, cell_list}_len\n");
599  return -1;
600  }
601 
602  memcpy(body + offset, cell_list, cell_list_len);
603 
604  return 0;
605 }
606 /*---------------------------------------------------------------------------*/
607 int
609  const uint8_t **cell_list,
610  sixp_pkt_offset_t *cell_list_len,
611  const uint8_t *body, uint16_t body_len)
612 {
613  int32_t offset;
614 
615  if(cell_list_len == NULL || body == NULL) {
616  LOG_ERR("6P-pkt: cannot get cell_list\n");
617  return -1;
618  }
619 
620  if((offset = get_cell_list_offset(type, code)) < 0) {
621  LOG_ERR("6P-pkt: cannot get cell_list; ");
622  LOG_ERR_("packet [type=%u, code=%u] won't have CellList\n",
623  type, code.value);
624  return -1;
625  }
626 
627  if(body_len < offset) {
628  LOG_ERR("6P-pkt: cannot set cell_list; body is too short\n");
629  return -1;
630  } else if(((body_len - offset) % sizeof(sixp_pkt_cell_t)) != 0) {
631  LOG_ERR("6P-pkt: cannot set cell_list; invalid {body, cell_list}_len\n");
632  return -1;
633  }
634 
635  if(cell_list != NULL) {
636  *cell_list = body + offset;
637  }
638 
639  *cell_list_len = body_len - offset;
640 
641  return 0;
642 }
643 /*---------------------------------------------------------------------------*/
644 int
646  const uint8_t *rel_cell_list,
647  uint16_t rel_cell_list_len,
648  uint16_t cell_offset,
649  uint8_t *body, uint16_t body_len)
650 {
651  int32_t offset;
652  sixp_pkt_num_cells_t num_cells;
653 
654  if(rel_cell_list == NULL || body == NULL) {
655  LOG_ERR("6P-pkt: cannot set rel_cell_list; invalid argument\n");
656  return -1;
657  }
658 
659  if(sixp_pkt_get_num_cells(type, code, &num_cells, body, body_len) < 0) {
660  LOG_ERR("6P-pkt: cannot set rel_cell_list; no NumCells field\n");
661  return -1;
662  }
663 
664  if((offset = get_rel_cell_list_offset(type, code)) < 0) {
665  LOG_ERR("6P-pkt: cannot set rel_cell_list; ");
666  LOG_ERR_("packet [type=%u, code=%u] won't have RelCellList\n",
667  type, code.value);
668  return -1;
669  }
670 
671  offset += cell_offset;
672 
673  if(body_len < (offset + rel_cell_list_len)) {
674  LOG_ERR("6P-pkt: cannot set rel_cell_list; body is too short\n");
675  return -1;
676  } else if((offset + rel_cell_list_len) >
677  (offset + num_cells * sizeof(sixp_pkt_cell_t))) {
678  LOG_ERR("6P-pkt: cannot set rel_cell_list; RelCellList is too long\n");
679  return -1;
680  } else if((rel_cell_list_len % sizeof(sixp_pkt_cell_t)) != 0) {
681  LOG_ERR("6P-pkt: cannot set rel_cell_list; invalid body_len\n");
682  return -1;
683  }
684 
685  memcpy(body + offset, rel_cell_list, rel_cell_list_len);
686 
687  return 0;
688 }
689 /*---------------------------------------------------------------------------*/
690 int
692  const uint8_t **rel_cell_list,
693  sixp_pkt_offset_t *rel_cell_list_len,
694  const uint8_t *body, uint16_t body_len)
695 {
696  int32_t offset;
697  sixp_pkt_num_cells_t num_cells;
698 
699  if(rel_cell_list_len == NULL || body == NULL) {
700  LOG_ERR("6P-pkt: cannot get rel_cell_list; invalid argument\n");
701  return -1;
702  }
703 
704  if(sixp_pkt_get_num_cells(type, code, &num_cells, body, body_len) < 0) {
705  LOG_ERR("6P-pkt: cannot get rel_cell_list; no NumCells field\n");
706  return -1;
707  }
708 
709  if((offset = get_rel_cell_list_offset(type, code)) < 0) {
710  LOG_ERR("6P-pkt: cannot get rel_cell_list; ");
711  LOG_ERR_("packet [type=%u, code=%u] won't have RelCellList\n",
712  type, code.value);
713  return -1;
714  }
715 
716  if(body_len < (offset + (num_cells * sizeof(sixp_pkt_cell_t)))) {
717  LOG_ERR("6P-pkt: cannot set rel_cell_list; body is too short\n");
718  return -1;
719  } else if(((body_len - offset) % sizeof(sixp_pkt_cell_t)) != 0) {
720  LOG_ERR("6P-pkt: cannot set rel_cell_list; invalid body_len\n");
721  return -1;
722  }
723 
724  if(rel_cell_list != NULL) {
725  *rel_cell_list = body + offset;
726  }
727 
728  *rel_cell_list_len = num_cells * sizeof(sixp_pkt_cell_t);
729 
730  return 0;
731 }
732 /*---------------------------------------------------------------------------*/
733 int
735  const uint8_t *cand_cell_list,
736  uint16_t cand_cell_list_len,
737  uint16_t cell_offset,
738  uint8_t *body, uint16_t body_len)
739 {
740  int32_t offset;
741  sixp_pkt_num_cells_t num_cells;
742 
743  if(cand_cell_list == NULL || body == NULL) {
744  LOG_ERR("6P-pkt: cannot set cand_cell_list; invalid argument\n");
745  return -1;
746  }
747 
748  if(sixp_pkt_get_num_cells(type, code, &num_cells, body, body_len) < 0) {
749  LOG_ERR("6P-pkt: cannot set cand_cell_list; no NumCells field\n");
750  return -1;
751  }
752 
753  if((offset = get_rel_cell_list_offset(type, code)) < 0) {
754  LOG_ERR("6P-pkt: cannot set cand_cell_list; ");
755  LOG_ERR_("packet [type=%u, code=%u] won't have RelCellList\n",
756  type, code.value);
757  return -1;
758  }
759 
760  offset += cell_offset + num_cells * sizeof(sixp_pkt_cell_t);
761 
762  if(body_len < (offset + cand_cell_list_len)) {
763  LOG_ERR("6P-pkt: cannot set cand_cell_list; body is too short\n");
764  return -1;
765  } else if((cand_cell_list_len % sizeof(sixp_pkt_cell_t)) != 0) {
766  LOG_ERR("6P-pkt: cannot set cand_cell_list; invalid body_len\n");
767  return -1;
768  }
769 
770  memcpy(body + offset, cand_cell_list, cand_cell_list_len);
771 
772  return 0;
773 }
774 /*---------------------------------------------------------------------------*/
775 int
777  const uint8_t **cand_cell_list,
778  sixp_pkt_offset_t *cand_cell_list_len,
779  const uint8_t *body, uint16_t body_len)
780 {
781  int32_t offset;
782  sixp_pkt_num_cells_t num_cells;
783 
784  if(cand_cell_list_len == NULL || body == NULL) {
785  LOG_ERR("6P-pkt: cannot get cand_cell_list; invalid argument\n");
786  return -1;
787  }
788 
789  if(sixp_pkt_get_num_cells(type, code, &num_cells, body, body_len) < 0) {
790  LOG_ERR("6P-pkt: cannot get cand_cell_list; no NumCells field\n");
791  return -1;
792  }
793 
794  if((offset = get_rel_cell_list_offset(type, code)) < 0) {
795  LOG_ERR("6P-pkt: cannot get cand_cell_list; ");
796  LOG_ERR_("packet [type=%u, code=%u] won't have RelCellList\n",
797  type, code.value);
798  return -1;
799  }
800 
801  offset += num_cells * sizeof(sixp_pkt_cell_t);
802 
803  if(body_len < offset) {
804  LOG_ERR("6P-pkt: cannot set cand_cell_list; body is too short\n");
805  return -1;
806  } else if(((body_len - offset) % sizeof(sixp_pkt_cell_t)) != 0) {
807  LOG_ERR("6P-pkt: cannot set cand_cell_list; invalid body_len\n");
808  return -1;
809  }
810 
811  if(cand_cell_list != NULL) {
812  *cand_cell_list = body + offset;
813  }
814 
815  *cand_cell_list_len = body_len - offset;
816 
817  return 0;
818 }
819 /*---------------------------------------------------------------------------*/
820 int
822  sixp_pkt_total_num_cells_t total_num_cells,
823  uint8_t *body, uint16_t body_len)
824 {
825  int32_t offset;
826 
827  if(body == NULL) {
828  LOG_ERR("6P-pkt: cannot set num_cells; body is null\n");
829  return -1;
830  }
831 
832  if((offset = get_total_num_cells_offset(type, code)) < 0) {
833  LOG_ERR("6P-pkt: cannot set total_num_cells; ");
834  LOG_ERR_("packet [type=%u, code=%u] won't have TotalNumCells\n",
835  type, code.value);
836  return -1;
837  }
838 
839  /*
840  * TotalNumCells for 6P Response is a 16-bit unsigned integer, little-endian.
841  */
842  body[offset] = (uint8_t)(total_num_cells & 0xff);
843  body[offset + 1] = (uint8_t)(total_num_cells >> 8);
844 
845  return 0;
846 }
847 /*---------------------------------------------------------------------------*/
848 int
850  sixp_pkt_total_num_cells_t *total_num_cells,
851  const uint8_t *body, uint16_t body_len)
852 {
853  int32_t offset;
854 
855  if(total_num_cells == NULL || body == NULL) {
856  LOG_ERR("6P-pkt: cannot get num_cells; invalid argument\n");
857  return -1;
858  }
859 
860  if((offset = get_total_num_cells_offset(type, code)) < 0) {
861  LOG_ERR("6P-pkt: cannot get num_cells; ");
862  LOG_ERR_("packet [type=%u, code=%u] won't have TotalNumCells\n",
863  type, code.value);
864  return -1;
865  }
866 
867  if(body_len < (offset + sizeof(sixp_pkt_total_num_cells_t))) {
868  LOG_ERR("6P-pkt: cannot get num_cells; body is too short\n");
869  return -1;
870  }
871 
872  /* TotalNumCells is a 16-bit unsigned integer, little-endian. */
873  *total_num_cells = body[0];
874  *total_num_cells += ((uint16_t)body[1]) << 8;
875 
876  return 0;
877 }
878 /*---------------------------------------------------------------------------*/
879 int
881  const uint8_t *payload, uint16_t payload_len,
882  uint8_t *body, uint16_t body_len)
883 {
884  int32_t offset;
885 
886  if(body == NULL) {
887  LOG_ERR("6P-pkt: cannot set metadata; body is null\n");
888  return -1;
889  }
890 
891  if((offset = get_payload_offset(type, code)) < 0) {
892  LOG_ERR("6P-pkt: cannot set payload [type=%u, code=%u], invalid type\n",
893  type, code.value);
894  return -1;
895  }
896 
897  if(body_len < (offset + payload_len)) {
898  LOG_ERR("6P-pkt: cannot set payload, body is too short [body_len=%u]\n",
899  body_len);
900  return -1;
901  }
902 
903  /*
904  * Copy the content into the Payload field as it is since 6P has no idea
905  * about the internal structure of the field.
906  */
907  memcpy(body + offset, payload, payload_len);
908 
909  return 0;
910 }
911 /*---------------------------------------------------------------------------*/
912 int
914  uint8_t *buf, uint16_t buf_len,
915  const uint8_t *body, uint16_t body_len)
916 {
917  int32_t offset;
918 
919  if(buf == NULL || body == NULL) {
920  LOG_ERR("6P-pkt: cannot get payload; invalid argument\n");
921  return -1;
922  }
923 
924  if((offset = get_payload_offset(type, code)) < 0) {
925  LOG_ERR("6P-pkt: cannot get payload [type=%u, code=%u], invalid type\n",
926  type, code.value);
927  return -1;
928  }
929 
930  if((body_len - offset) > buf_len) {
931  LOG_ERR("6P-pkt: cannot get payload [type=%u, code=%u], ",
932  type, code.value);
933  LOG_ERR_("buf_len is too short\n");
934  return -1;
935  }
936 
937  /*
938  * Copy the content in the Payload field as it is since 6P has no idea about
939  * the internal structure of the field.
940  */
941  memcpy(buf, body + offset, buf_len);
942 
943  return 0;
944 }
945 /*---------------------------------------------------------------------------*/
946 int
947 sixp_pkt_parse(const uint8_t *buf, uint16_t len,
948  sixp_pkt_t *pkt)
949 {
950  assert(buf != NULL && pkt != NULL);
951  if(buf == NULL || pkt == NULL) {
952  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid argument\n");
953  return -1;
954  }
955 
956  memset(pkt, 0, sizeof(sixp_pkt_t));
957 
958  /* read the first 4 octets */
959  if(len < 4) {
960  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because it's a too short packet\n");
961  return -1;
962  }
963 
964  /* parse the header as it's version 0 6P packet */
965  pkt->version = buf[0] & 0x0f;
966  pkt->type = (buf[0] & 0x30) >> 4;
967  pkt->code.value = buf[1];
968  pkt->sfid = buf[2];
969  pkt->seqno = buf[3];
970 
971  if(pkt->version != SIXP_PKT_VERSION) {
972  /* invalid version; stop parsing */
973  return -1;
974  }
975 
976  buf += 4;
977  len -= 4;
978 
979  LOG_INFO("6P-pkt: sixp_pkt_parse() is processing [type:%u, code:%u, len:%u]\n",
980  pkt->type, pkt->code.value, len);
981 
982  /* the rest is message body called "Other Fields" */
983  if(pkt->type == SIXP_PKT_TYPE_REQUEST) {
984  switch(pkt->code.cmd) {
985  case SIXP_PKT_CMD_ADD:
986  case SIXP_PKT_CMD_DELETE:
987  /* Add and Delete has the same request format */
988  if(len < (sizeof(sixp_pkt_metadata_t) +
989  sizeof(sixp_pkt_cell_options_t) +
990  sizeof(sixp_pkt_num_cells_t)) ||
991  (len % sizeof(uint32_t)) != 0) {
992  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
993  return -1;
994  }
995  break;
997  if(len < (sizeof(sixp_pkt_metadata_t) +
998  sizeof(sixp_pkt_cell_options_t) +
999  sizeof(sixp_pkt_num_cells_t)) ||
1000  (len % sizeof(uint32_t)) != 0) {
1001  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1002  return -1;
1003  }
1004  break;
1005  case SIXP_PKT_CMD_COUNT:
1006  if(len != (sizeof(sixp_pkt_metadata_t) +
1007  sizeof(sixp_pkt_cell_options_t))) {
1008  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1009  return -1;
1010  }
1011  break;
1012  case SIXP_PKT_CMD_LIST:
1013  if(len != (sizeof(sixp_pkt_metadata_t) +
1014  sizeof(sixp_pkt_cell_options_t) +
1015  sizeof(sixp_pkt_reserved_t) +
1016  sizeof(sixp_pkt_offset_t) +
1017  sizeof(sixp_pkt_max_num_cells_t))) {
1018  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1019  return -1;
1020  }
1021  break;
1022  case SIXP_PKT_CMD_SIGNAL:
1023  if(len < sizeof(sixp_pkt_metadata_t)) {
1024  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1025  return -1;
1026  }
1027  break;
1028  case SIXP_PKT_CMD_CLEAR:
1029  if(len != sizeof(sixp_pkt_metadata_t)) {
1030  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1031  return -1;
1032  }
1033  break;
1034  default:
1035  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of unsupported cmd\n");
1036  return -1;
1037  }
1038  } else if(pkt->type == SIXP_PKT_TYPE_RESPONSE ||
1039  pkt->type == SIXP_PKT_TYPE_CONFIRMATION) {
1040  switch(pkt->code.rc) {
1041  case SIXP_PKT_RC_SUCCESS:
1042  /*
1043  * The "Other Field" contains
1044  * - Res to CLEAR: Empty (length 0)
1045  * - Res to STATUS: "Num. Cells" (total_num_cells)
1046  * - Res to ADD, DELETE, LIST: 0, 1, or multiple 6P cells
1047  * - Res to SIGNAL: Payload (arbitrary length)
1048  */
1049  /* we accept any length because of SIGNAL */
1050  break;
1051  case SIXP_PKT_RC_EOL:
1052  if((len % sizeof(uint32_t)) != 0) {
1053  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1054  return -1;
1055  }
1056  break;
1057  case SIXP_PKT_RC_ERR:
1058  case SIXP_PKT_RC_RESET:
1060  case SIXP_PKT_RC_ERR_SFID:
1063  case SIXP_PKT_RC_ERR_BUSY:
1065  if(len != 0) {
1066  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of invalid length\n");
1067  return -1;
1068  }
1069  break;
1070  default:
1071  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of unsupported code\n");
1072  return -1;
1073  }
1074  } else {
1075  LOG_ERR("6P-pkt: sixp_pkt_parse() fails because of unsupported type\n");
1076  return -1;
1077  }
1078 
1079  pkt->body = buf;
1080  pkt->body_len = len;
1081 
1082  return 0;
1083 }
1084 /*---------------------------------------------------------------------------*/
1085 int
1087  uint8_t sfid, uint8_t seqno,
1088  const uint8_t *body, uint16_t body_len, sixp_pkt_t *pkt)
1089 {
1090  uint8_t *hdr;
1091 
1092  assert((body == NULL && body_len == 0) || (body != NULL && body_len > 0));
1093  if((body == NULL && body_len > 0) || (body != NULL && body_len == 0)) {
1094  LOG_ERR("6P-pkt: sixp_pkt_create() fails because of invalid argument\n");
1095  return -1;
1096  }
1097 
1098  packetbuf_clear();
1099 
1100  /*
1101  * We're going to create a packet having 6top IE header (4 octets) and body
1102  * (body_len octets).
1103  */
1104  if(PACKETBUF_SIZE < (packetbuf_totlen() + body_len)) {
1105  LOG_ERR("6P-pkt: sixp_pkt_create() fails because body is too long\n");
1106  return -1;
1107  }
1108 
1109  if(packetbuf_hdralloc(4) != 1) {
1110  LOG_ERR("6P-pkt: sixp_pkt_create fails to allocate header space\n");
1111  return -1;
1112  }
1113  hdr = packetbuf_hdrptr();
1114  /* header: write the 6top IE header, 4 octets */
1115  hdr[0] = (type << 4) | SIXP_PKT_VERSION;
1116  hdr[1] = code.value;
1117  hdr[2] = sfid;
1118  hdr[3] = seqno;
1119 
1120  /* data: write body */
1121  if(body_len > 0 && body != NULL) {
1122  memcpy(packetbuf_dataptr(), body, body_len);
1123  packetbuf_set_datalen(body_len);
1124  }
1125 
1126  /* copy information of a sending packet into pkt if necessary */
1127  if(pkt != NULL) {
1128  pkt->type = type;
1129  pkt->code = code;
1130  pkt->sfid = sfid;
1131  pkt->seqno = seqno;
1132  pkt->body = body;
1133  pkt->body_len = body_len;
1134  }
1135 
1136  /* packetbuf is ready to be sent */
1137  return 0;
1138 }
1139 /*---------------------------------------------------------------------------*/
1140 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
int sixp_pkt_get_metadata(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_metadata_t *metadata, const uint8_t *body, uint16_t body_len)
Read Metadata stored in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:229
sixp_pkt_type_t
6P Message Types
Definition: sixp-pkt.h:62
int sixp_pkt_get_reserved(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_reserved_t *reserved, const uint8_t *body, uint16_t body_len)
Read Reserved in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:409
sixp_pkt_code_t code
Code.
Definition: sixp-pkt.h:124
int sixp_pkt_get_cand_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t **cand_cell_list, sixp_pkt_offset_t *cand_cell_list_len, const uint8_t *body, uint16_t body_len)
Read CandCellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:776
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:75
int sixp_pkt_set_cell_options(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_cell_options_t cell_options, uint8_t *body, uint16_t body_len)
Write CellOptions in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:263
CMD_CLEAR.
Definition: sixp-pkt.h:79
int sixp_pkt_get_payload(sixp_pkt_type_t type, sixp_pkt_code_t code, uint8_t *buf, uint16_t buf_len, const uint8_t *body, uint16_t body_len)
Read Payload in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:913
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:107
RC_ERR_SFID.
Definition: sixp-pkt.h:92
int sixp_pkt_get_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_num_cells_t *num_cells, const uint8_t *body, uint16_t body_len)
Read NumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:349
uint16_t body_len
The length of Other Fields.
Definition: sixp-pkt.h:128
sixp_pkt_rc_t rc
6P Return Code
Definition: sixp-pkt.h:105
int sixp_pkt_get_cell_options(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_cell_options_t *cell_options, const uint8_t *body, uint16_t body_len)
Read CellOptions in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:294
uint8_t seqno
SeqNum.
Definition: sixp-pkt.h:126
RC_EOL.
Definition: sixp-pkt.h:88
6top Protocol (6P) APIs
int sixp_pkt_set_total_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_total_num_cells_t total_num_cells, uint8_t *body, uint16_t body_len)
Write TotalNumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:821
int sixp_pkt_get_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t **cell_list, sixp_pkt_offset_t *cell_list_len, const uint8_t *body, uint16_t body_len)
Read CellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:608
int sixp_pkt_set_cand_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t *cand_cell_list, uint16_t cand_cell_list_len, uint16_t cell_offset, uint8_t *body, uint16_t body_len)
Write CandCellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:734
uint8_t value
8-bit unsigned integer value
Definition: sixp-pkt.h:106
int sixp_pkt_set_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_num_cells_t num_cells, uint8_t *body, uint16_t body_len)
Write NumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:325
6P Request
Definition: sixp-pkt.h:63
RC_ERR_BUSY.
Definition: sixp-pkt.h:95
uint8_t sfid
SFID.
Definition: sixp-pkt.h:125
int sixp_pkt_get_offset(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_offset_t *cell_offset, const uint8_t *body, uint16_t body_len)
Read Offset in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:468
int sixp_pkt_get_rel_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t **rel_cell_list, sixp_pkt_offset_t *rel_cell_list_len, const uint8_t *body, uint16_t body_len)
Read RelCellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:691
6P Codes integrating Command IDs and Return Codes
Definition: sixp-pkt.h:103
int sixp_pkt_set_payload(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t *payload, uint16_t payload_len, uint8_t *body, uint16_t body_len)
Write Payload in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:880
6P Response
Definition: sixp-pkt.h:64
int sixp_pkt_get_max_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_max_num_cells_t *max_num_cells, const uint8_t *body, uint16_t body_len)
Read MaxNumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:537
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:167
RC_SUCCESS.
Definition: sixp-pkt.h:87
CMD_SIGNAL.
Definition: sixp-pkt.h:78
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:67
int sixp_pkt_set_max_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_max_num_cells_t max_num_cells, uint8_t *body, uint16_t body_len)
Write MaxNumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:503
CMD_STATUS.
Definition: sixp-pkt.h:75
int sixp_pkt_set_offset(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_offset_t cell_offset, uint8_t *body, uint16_t body_len)
Write Offset in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:434
const uint8_t * body
Other Fields...
Definition: sixp-pkt.h:127
Main API declarations for TSCH.
int sixp_pkt_set_rel_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t *rel_cell_list, uint16_t rel_cell_list_len, uint16_t cell_offset, uint8_t *body, uint16_t body_len)
Write RelCellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:645
sixp_pkt_type_t type
Type.
Definition: sixp-pkt.h:123
RC_ERR_VERSION.
Definition: sixp-pkt.h:91
CMD_DELETE.
Definition: sixp-pkt.h:74
RC_ERR_LOCKED.
Definition: sixp-pkt.h:96
int sixp_pkt_set_metadata(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_metadata_t metadata, uint8_t *body, uint16_t body_len)
Write Metadata into "Other Fields" of 6P packet.
Definition: sixp-pkt.c:196
6P Confirmation
Definition: sixp-pkt.h:65
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:149
6top IE Structure
Definition: sixp-pkt.h:121
6top Protocol (6P) Packet Manipulation APIs
int sixp_pkt_parse(const uint8_t *buf, uint16_t len, sixp_pkt_t *pkt)
Parse a 6P packet.
Definition: sixp-pkt.c:947
int sixp_pkt_get_total_num_cells(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_total_num_cells_t *total_num_cells, const uint8_t *body, uint16_t body_len)
Read TotalNumCells in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:849
int sixp_pkt_set_cell_list(sixp_pkt_type_t type, sixp_pkt_code_t code, const uint8_t *cell_list, uint16_t cell_list_len, uint16_t cell_offset, uint8_t *body, uint16_t body_len)
Write CellList in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:572
sixp_pkt_cmd_t cmd
6P Command Identifier
Definition: sixp-pkt.h:104
int sixp_pkt_create(sixp_pkt_type_t type, sixp_pkt_code_t code, uint8_t sfid, uint8_t seqno, const uint8_t *body, uint16_t body_len, sixp_pkt_t *pkt)
Create a 6P packet.
Definition: sixp-pkt.c:1086
Header file for the Packet buffer (packetbuf) management
CMD_STATUS.
Definition: sixp-pkt.h:76
RC_ERR_CELLLIST.
Definition: sixp-pkt.h:94
RC_ERR.
Definition: sixp-pkt.h:89
CMD_LIST.
Definition: sixp-pkt.h:77
RC_ERR_SEQNUM.
Definition: sixp-pkt.h:93
Header file for the logging system
CMD_ADD.
Definition: sixp-pkt.h:73
int sixp_pkt_set_reserved(sixp_pkt_type_t type, sixp_pkt_code_t code, sixp_pkt_reserved_t reserved, uint8_t *body, uint16_t body_len)
Write Reserved in "Other Fields" of 6P packet.
Definition: sixp-pkt.c:379
sixp_pkt_version_t version
Version.
Definition: sixp-pkt.h:122
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:136
RC_RESET.
Definition: sixp-pkt.h:90