Contiki 3.x
framer-nullmac.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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  */
30 
31 /**
32  * \file
33  * MAC framer for nullmac
34  * \author
35  * Niclas Finne <nfi@sics.se>
36  * Joakim Eriksson <joakime@sics.se>
37  */
38 
39 #include "net/mac/framer-nullmac.h"
40 #include "net/packetbuf.h"
41 
42 #define DEBUG 0
43 
44 #if DEBUG
45 #include <stdio.h>
46 #define PRINTF(...) printf(__VA_ARGS__)
47 #define PRINTADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7])
48 #else
49 #define PRINTF(...)
50 #define PRINTADDR(addr)
51 #endif
52 
53 struct nullmac_hdr {
54  linkaddr_t receiver;
55  linkaddr_t sender;
56 };
57 
58 /*---------------------------------------------------------------------------*/
59 static int
60 hdr_length(void)
61 {
62  return sizeof(struct nullmac_hdr);
63 }
64 /*---------------------------------------------------------------------------*/
65 static int
66 create(void)
67 {
68  struct nullmac_hdr *hdr;
69 
70  if(packetbuf_hdralloc(sizeof(struct nullmac_hdr))) {
71  hdr = packetbuf_hdrptr();
72  linkaddr_copy(&(hdr->sender), &linkaddr_node_addr);
73  linkaddr_copy(&(hdr->receiver), packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
74  return sizeof(struct nullmac_hdr);
75  }
76  PRINTF("PNULLMAC-UT: too large header: %u\n", sizeof(struct nullmac_hdr));
77  return FRAMER_FAILED;
78 }
79 /*---------------------------------------------------------------------------*/
80 static int
81 parse(void)
82 {
83  struct nullmac_hdr *hdr;
84  hdr = packetbuf_dataptr();
85  if(packetbuf_hdrreduce(sizeof(struct nullmac_hdr))) {
86  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &(hdr->sender));
87  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &(hdr->receiver));
88 
89  PRINTF("PNULLMAC-IN: ");
90  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
91  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
92  PRINTF("%u (%u)\n", packetbuf_datalen(), sizeof(struct nullmac_hdr));
93 
94  return sizeof(struct nullmac_hdr);
95  }
96  return FRAMER_FAILED;
97 }
98 /*---------------------------------------------------------------------------*/
99 const struct framer framer_nullmac = {
100  hdr_length,
101  create,
102  framer_canonical_create_and_secure,
103  parse
104 };
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Header file for the Rime buffer (packetbuf) management
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
MAC framer for nullmac
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188