Contiki-NG
Loading...
Searching...
No Matches
border-router-mac.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, 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 * \file
32 * A null RDC implementation that uses framer for headers and sends
33 * the packets over slip instead of radio.
34 * \author
35 * Adam Dunkels <adam@sics.se>
36 * Joakim Eriksson <joakime@sics.se>
37 * Niclas Finne <nfi@sics.se>
38 */
39
40#include "net/packetbuf.h"
41#include "net/queuebuf.h"
42#include "net/netstack.h"
44#include "packetutils.h"
45#include "border-router.h"
46#include <string.h>
47
48/*---------------------------------------------------------------------------*/
49/* Log configuration */
50#include "sys/log.h"
51#define LOG_MODULE "BR-MAC"
52#define LOG_LEVEL LOG_LEVEL_NONE
53
54#define MAX_CALLBACKS 16
55static int callback_pos;
56
57/* a structure for calling back when packet data is coming back
58 from radio... */
59struct tx_callback {
60 mac_callback_t cback;
61 void *ptr;
62 struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
63 struct packetbuf_addr addrs[PACKETBUF_NUM_ADDRS];
64};
65/*---------------------------------------------------------------------------*/
66static struct tx_callback callbacks[MAX_CALLBACKS];
67/*---------------------------------------------------------------------------*/
68void
69init_sec(void)
70{
71 /* use the CSMA LLSEC config parameter */
72#if LLSEC802154_USES_AUX_HEADER
73 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) ==
74 PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT) {
75 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL,
76 CSMA_LLSEC_SECURITY_LEVEL);
77 }
78#endif
79}
80/*---------------------------------------------------------------------------*/
81
82void
83packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx)
84{
85 if(sessionid < MAX_CALLBACKS) {
86 struct tx_callback *callback;
87 callback = &callbacks[sessionid];
89 packetbuf_attr_copyfrom(callback->attrs, callback->addrs);
90 mac_call_sent_callback(callback->cback, callback->ptr, status, tx);
91 } else {
92 LOG_ERR("Session id (%d) >= MAX_CALLBACKS (%d)\n", sessionid,
93 MAX_CALLBACKS);
94 }
95}
96/*---------------------------------------------------------------------------*/
97static int
98setup_callback(mac_callback_t sent, void *ptr)
99{
100 struct tx_callback *callback;
101 int tmp = callback_pos;
102 callback = &callbacks[callback_pos];
103 callback->cback = sent;
104 callback->ptr = ptr;
105 packetbuf_attr_copyto(callback->attrs, callback->addrs);
106
107 callback_pos++;
108 if(callback_pos >= MAX_CALLBACKS) {
109 callback_pos = 0;
110 }
111
112 return tmp;
113}
114/*---------------------------------------------------------------------------*/
115static void
116send_packet(mac_callback_t sent, void *ptr)
117{
118 int size;
119 /* 3 bytes per packet attribute is required for serialization */
120 uint8_t buf[PACKETBUF_NUM_ATTRS * 3 + PACKETBUF_SIZE + 3];
121 uint8_t sid;
122
123 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
124
125 /* ack or not ? */
126 packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
127
128 /* Will make it send only DATA packets... for now */
129 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
130
131 /* set destination sequence number */
133
134 LOG_INFO("sending packet (%u bytes)\n", packetbuf_datalen());
135
136 if(NETSTACK_FRAMER.create() < 0) {
137 /* Failed to allocate space for headers */
138 LOG_WARN("send failed, too large header\n");
139 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
140 } else {
141 /* here we send the data over SLIP to the radio-chip */
142#if SERIALIZE_ATTRIBUTES
143 size = packetutils_serialize_atts(&buf[3], sizeof(buf) - 3);
144#else
145 size = 0;
146#endif
147 if(size < 0 || size + packetbuf_totlen() + 3 > sizeof(buf)) {
148 LOG_WARN("send failed, too large header\n");
149 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
150 } else {
151 sid = setup_callback(sent, ptr);
152
153 buf[0] = '!';
154 buf[1] = 'S';
155 buf[2] = sid; /* sequence or session number for this packet */
156
157 /* Copy packet data */
158 memcpy(&buf[3 + size], packetbuf_hdrptr(), packetbuf_totlen());
159
160 write_to_slip(buf, packetbuf_totlen() + size + 3);
161 }
162 }
163}
164/*---------------------------------------------------------------------------*/
165static void
166packet_input(void)
167{
168 if(NETSTACK_FRAMER.parse() < 0) {
169 LOG_DBG("failed to parse %u\n", packetbuf_datalen());
170 } else {
171 NETSTACK_NETWORK.input();
172 }
173}
174/*---------------------------------------------------------------------------*/
175static int
176on(void)
177{
178 return 1;
179}
180/*---------------------------------------------------------------------------*/
181static int
182off()
183{
184 return 1;
185}
186/*---------------------------------------------------------------------------*/
187static int
188max_payload()
189{
190 init_sec();
191 return 127 - NETSTACK_FRAMER.length();
192}
193/*---------------------------------------------------------------------------*/
194static void
195init(void)
196{
197 callback_pos = 0;
199}
200/*---------------------------------------------------------------------------*/
201const struct mac_driver border_router_mac_driver = {
202 "br-mac",
203 init,
205 packet_input,
206 on,
207 off,
209};
210/*---------------------------------------------------------------------------*/
Border router header file.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition packetbuf.c:167
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition packetbuf.c:155
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition packetbuf.c:149
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition packetbuf.h:67
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition packetbuf.c:75
static void send_packet(void)
This function is called by the 6lowpan code to send out a packet.
Header file for the logging system.
void mac_sequence_set_dsn(void)
Sets and increments the destination sequence number.
void mac_sequence_init(void)
brief Initializes the destination sequence number to a random value.
Header file for MAC sequence numbers management.
@ MAC_TX_ERR_FATAL
The MAC layer transmission could not be performed because of insufficient queue space,...
Definition mac.h:112
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
Header file for the Packet queue buffer management.
The structure of a MAC protocol driver in Contiki.
Definition mac.h:68
int(* on)(void)
Turn the MAC layer on.
Definition mac.h:81
int(* max_payload)(void)
Read out estimated max payload size based on payload in packetbuf.
Definition mac.h:87
int(* off)(void)
Turn the MAC layer off.
Definition mac.h:84
void(* init)(void)
Initialize the MAC driver.
Definition mac.h:72