Contiki 3.x
contikimac-framer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Creates and parses the ContikiMAC header.
36  * \author
37  * Konrad Krentz <konrad.krentz@gmail.com>
38  */
39 
41 #include "net/packetbuf.h"
42 #include "net/netstack.h"
43 #include <string.h>
44 
45 #define CONTIKIMAC_ID 0x00
46 
47 /* SHORTEST_PACKET_SIZE is the shortest packet that ContikiMAC
48  allows. Packets have to be a certain size to be able to be detected
49  by two consecutive CCA checks, and here is where we define this
50  shortest size.
51  Padded packets will have the wrong ipv6 checksum unless CONTIKIMAC_HEADER
52  is used (on both sides) and the receiver will ignore them.
53  With no header, reduce to transmit a proper multicast RPL DIS. */
54 #ifdef CONTIKIMAC_FRAMER_CONF_SHORTEST_PACKET_SIZE
55 #define SHORTEST_PACKET_SIZE CONTIKIMAC_FRAMER_CONF_SHORTEST_PACKET_SIZE
56 #else /* CONTIKIMAC_FRAMER_CONF_SHORTEST_PACKET_SIZE */
57 #define SHORTEST_PACKET_SIZE 43
58 #endif /* CONTIKIMAC_FRAMER_CONF_SHORTEST_PACKET_SIZE */
59 
60 #ifdef CONTIKIMAC_FRAMER_CONF_DECORATED_FRAMER
61 #define DECORATED_FRAMER CONTIKIMAC_FRAMER_CONF_DECORATED_FRAMER
62 #else /* CONTIKIMAC_FRAMER_CONF_DECORATED_FRAMER */
63 #define DECORATED_FRAMER framer_802154
64 #endif /* CONTIKIMAC_FRAMER_CONF_DECORATED_FRAMER */
65 
66 extern const struct framer DECORATED_FRAMER;
67 
68 #define DEBUG 0
69 #if DEBUG
70 #include <stdio.h>
71 #define PRINTF(...) printf(__VA_ARGS__)
72 #else
73 #define PRINTF(...)
74 #endif
75 
76 static void pad(void);
77 
78 /* 2-byte header for recovering padded packets.
79  Wireshark will not understand such packets at present. */
80 struct hdr {
81  uint8_t id;
82  uint8_t len;
83 };
84 
85 /*---------------------------------------------------------------------------*/
86 static int
87 hdr_length(void)
88 {
89  return DECORATED_FRAMER.length() + sizeof(struct hdr);
90 }
91 /*---------------------------------------------------------------------------*/
92 static int
93 create(void)
94 {
95  struct hdr *chdr;
96  int hdr_len;
97 
98  if(packetbuf_hdralloc(sizeof(struct hdr)) == 0) {
99  PRINTF("contikimac-framer: too large header\n");
100  return FRAMER_FAILED;
101  }
102  chdr = packetbuf_hdrptr();
103  chdr->id = CONTIKIMAC_ID;
104  chdr->len = packetbuf_datalen();
105  pad();
106 
107  hdr_len = DECORATED_FRAMER.create();
108  if(hdr_len < 0) {
109  PRINTF("contikimac-framer: decorated framer failed\n");
110  return FRAMER_FAILED;
111  }
112 
114 
115  return hdr_len + sizeof(struct hdr);
116 }
117 /*---------------------------------------------------------------------------*/
118 static void
119 pad(void)
120 {
121  int transmit_len;
122  uint8_t *ptr;
123  uint8_t zeroes_count;
124 
125  transmit_len = packetbuf_totlen() + hdr_length();
126  if(transmit_len < SHORTEST_PACKET_SIZE) {
127  /* Padding required */
128  zeroes_count = SHORTEST_PACKET_SIZE - transmit_len;
129  ptr = packetbuf_dataptr();
130  memset(ptr + packetbuf_datalen(), 0, zeroes_count);
131  packetbuf_set_datalen(packetbuf_datalen() + zeroes_count);
132  }
133 }
134 /*---------------------------------------------------------------------------*/
135 static int
136 parse(void)
137 {
138  int hdr_len;
139  struct hdr *chdr;
140 
141  hdr_len = DECORATED_FRAMER.parse();
142  if(hdr_len < 0) {
143  return FRAMER_FAILED;
144  }
145 
146  chdr = packetbuf_dataptr();
147  if(chdr->id != CONTIKIMAC_ID) {
148  PRINTF("contikimac-framer: CONTIKIMAC_ID is missing\n");
149  return FRAMER_FAILED;
150  }
151 
152  if(!packetbuf_hdrreduce(sizeof(struct hdr))) {
153  PRINTF("contikimac-framer: packetbuf_hdrreduce failed\n");
154  return FRAMER_FAILED;
155  }
156 
157  packetbuf_set_datalen(chdr->len);
158 
159  return hdr_len + sizeof(struct hdr);
160 }
161 /*---------------------------------------------------------------------------*/
162 const struct framer contikimac_framer = {
163  hdr_length,
164  create,
165  parse
166 };
167 /*---------------------------------------------------------------------------*/
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
Creates and parses the ContikiMAC header.
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:182
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:164
Header file for the Rime buffer (packetbuf) management
void packetbuf_compact(void)
Compact the packetbuf.
Definition: packetbuf.c:97
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:151
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:122
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:139
Include file for the Contiki low-layer network stack (NETSTACK)