Contiki-NG
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 to high (%d)\n", sessionid);
93 }
94}
95/*---------------------------------------------------------------------------*/
96static int
97setup_callback(mac_callback_t sent, void *ptr)
98{
99 struct tx_callback *callback;
100 int tmp = callback_pos;
101 callback = &callbacks[callback_pos];
102 callback->cback = sent;
103 callback->ptr = ptr;
104 packetbuf_attr_copyto(callback->attrs, callback->addrs);
105
106 callback_pos++;
107 if(callback_pos >= MAX_CALLBACKS) {
108 callback_pos = 0;
109 }
110
111 return tmp;
112}
113/*---------------------------------------------------------------------------*/
114static void
115send_packet(mac_callback_t sent, void *ptr)
116{
117 int size;
118 /* 3 bytes per packet attribute is required for serialization */
119 uint8_t buf[PACKETBUF_NUM_ATTRS * 3 + PACKETBUF_SIZE + 3];
120 uint8_t sid;
121
122 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
123
124 /* ack or not ? */
125 packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
126
127 /* Will make it send only DATA packets... for now */
128 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
129
130 /* set destination sequence number */
132
133 LOG_INFO("sending packet (%u bytes)\n", packetbuf_datalen());
134
135 if(NETSTACK_FRAMER.create() < 0) {
136 /* Failed to allocate space for headers */
137 LOG_WARN("send failed, too large header\n");
138 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
139 } else {
140 /* here we send the data over SLIP to the radio-chip */
141 size = 0;
142#if SERIALIZE_ATTRIBUTES
143 size = packetutils_serialize_atts(&buf[3], sizeof(buf) - 3);
144#endif
145 if(size < 0 || size + packetbuf_totlen() + 3 > sizeof(buf)) {
146 LOG_WARN("send failed, too large header\n");
147 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
148 } else {
149 sid = setup_callback(sent, ptr);
150
151 buf[0] = '!';
152 buf[1] = 'S';
153 buf[2] = sid; /* sequence or session number for this packet */
154
155 /* Copy packet data */
156 memcpy(&buf[3 + size], packetbuf_hdrptr(), packetbuf_totlen());
157
158 write_to_slip(buf, packetbuf_totlen() + size + 3);
159 }
160 }
161}
162/*---------------------------------------------------------------------------*/
163static void
164packet_input(void)
165{
166 if(NETSTACK_FRAMER.parse() < 0) {
167 LOG_DBG("failed to parse %u\n", packetbuf_datalen());
168 } else {
169 NETSTACK_NETWORK.input();
170 }
171}
172/*---------------------------------------------------------------------------*/
173static int
174on(void)
175{
176 return 1;
177}
178/*---------------------------------------------------------------------------*/
179static int
180off()
181{
182 return 1;
183}
184/*---------------------------------------------------------------------------*/
185static int
186max_payload()
187{
188 init_sec();
189 return 127 - NETSTACK_FRAMER.length();
190}
191/*---------------------------------------------------------------------------*/
192static void
193init(void)
194{
195 callback_pos = 0;
197}
198/*---------------------------------------------------------------------------*/
199const struct mac_driver border_router_mac_driver = {
200 "br-mac",
201 init,
203 packet_input,
204 on,
205 off,
207};
208/*---------------------------------------------------------------------------*/
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.
Definition: sicslowpan.c:1543
static void packet_sent(void *ptr, int status, int transmissions)
Callback function for the MAC packet sent callback.
Definition: sicslowpan.c:1513
Header file for the logging system.
void mac_sequence_set_dsn(void)
Sets and increments the destination sequence number.
Definition: mac-sequence.c:81
void mac_sequence_init(void)
brief Initializes the destination sequence number to a random value.
Definition: mac-sequence.c:75
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:106
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:62
int(* on)(void)
Turn the MAC layer on.
Definition: mac.h:75
int(* max_payload)(void)
Read out estimated max payload size based on payload in packetbuf.
Definition: mac.h:81
int(* off)(void)
Turn the MAC layer off.
Definition: mac.h:78
void(* init)(void)
Initialize the MAC driver.
Definition: mac.h:66