Contiki-Inga 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 create(void)
61 {
62  struct nullmac_hdr *hdr;
63 
64  if(packetbuf_hdralloc(sizeof(struct nullmac_hdr))) {
65  hdr = packetbuf_hdrptr();
66  linkaddr_copy(&(hdr->sender), &linkaddr_node_addr);
67  linkaddr_copy(&(hdr->receiver), packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
68  return sizeof(struct nullmac_hdr);
69  }
70  PRINTF("PNULLMAC-UT: too large header: %u\n", sizeof(struct nullmac_hdr));
71  return FRAMER_FAILED;
72 }
73 /*---------------------------------------------------------------------------*/
74 static int
75 parse(void)
76 {
77  struct nullmac_hdr *hdr;
78  hdr = packetbuf_dataptr();
79  if(packetbuf_hdrreduce(sizeof(struct nullmac_hdr))) {
80  packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &(hdr->sender));
81  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, &(hdr->receiver));
82 
83  PRINTF("PNULLMAC-IN: ");
84  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
85  PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
86  PRINTF("%u (%u)\n", packetbuf_datalen(), sizeof(struct nullmac_hdr));
87 
88  return sizeof(struct nullmac_hdr);
89  }
90  return FRAMER_FAILED;
91 }
92 /*---------------------------------------------------------------------------*/
93 const struct framer framer_nullmac = {
94  create, parse
95 };