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"
45#include "packetutils.h"
46#include "border-router.h"
47#include <string.h>
48
49/*---------------------------------------------------------------------------*/
50/* Log configuration */
51#include "sys/log.h"
52#define LOG_MODULE "BR-MAC"
53#define LOG_LEVEL LOG_LEVEL_NONE
54
55#define MAX_CALLBACKS 16
56static int callback_pos;
57
58/* a structure for calling back when packet data is coming back
59 from radio... */
60struct tx_callback {
61 mac_callback_t cback;
62 void *ptr;
63 struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
64 struct packetbuf_addr addrs[PACKETBUF_NUM_ADDRS];
65};
66/*---------------------------------------------------------------------------*/
67static struct tx_callback callbacks[MAX_CALLBACKS];
68/*---------------------------------------------------------------------------*/
69void
70init_sec(void)
71{
72 /* use the CSMA LLSEC config parameter */
73#if LLSEC802154_USES_AUX_HEADER
74 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) ==
75 PACKETBUF_ATTR_SECURITY_LEVEL_DEFAULT) {
76 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL,
77 CSMA_LLSEC_SECURITY_LEVEL);
78 }
79#endif
80}
81/*---------------------------------------------------------------------------*/
82
83void
84packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx)
85{
86 if(sessionid < MAX_CALLBACKS) {
87 struct tx_callback *callback;
88 callback = &callbacks[sessionid];
90 packetbuf_attr_copyfrom(callback->attrs, callback->addrs);
91 mac_call_sent_callback(callback->cback, callback->ptr, status, tx);
92 } else {
93 LOG_ERR("Session id (%d) >= MAX_CALLBACKS (%d)\n", sessionid,
94 MAX_CALLBACKS);
95 }
96}
97/*---------------------------------------------------------------------------*/
98static int
99setup_callback(mac_callback_t sent, void *ptr)
100{
101 struct tx_callback *callback;
102 int tmp = callback_pos;
103 callback = &callbacks[callback_pos];
104 callback->cback = sent;
105 callback->ptr = ptr;
106 packetbuf_attr_copyto(callback->attrs, callback->addrs);
107
108 callback_pos++;
109 if(callback_pos >= MAX_CALLBACKS) {
110 callback_pos = 0;
111 }
112
113 return tmp;
114}
115/*---------------------------------------------------------------------------*/
116static void
117send_packet(mac_callback_t sent, void *ptr)
118{
119 int size;
120 /* 3 bytes per packet attribute is required for serialization */
121 uint8_t buf[PACKETBUF_NUM_ATTRS * 3 + PACKETBUF_SIZE + 3];
122 uint8_t sid;
123
124 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
125
126 /* ack or not ? */
127 packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
128
129 /* Will make it send only DATA packets... for now */
130 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
131
132 /* set destination sequence number */
134
135 LOG_INFO("sending packet (%u bytes)\n", packetbuf_datalen());
136
137 if(NETSTACK_FRAMER.create() < 0) {
138 /* Failed to allocate space for headers */
139 LOG_WARN("send failed, too large header\n");
140 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
141 } else {
142 /* here we send the data over SLIP to the radio-chip */
143#if SERIALIZE_ATTRIBUTES
144 size = packetutils_serialize_atts(&buf[3], sizeof(buf) - 3);
145#else
146 size = 0;
147#endif
148 if(size < 0 || size + packetbuf_totlen() + 3 > sizeof(buf)) {
149 LOG_WARN("send failed, too large header\n");
150 mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
151 } else {
152 sid = setup_callback(sent, ptr);
153
154 buf[0] = '!';
155 buf[1] = 'S';
156 buf[2] = sid; /* sequence or session number for this packet */
157
158 /* Copy packet data */
159 memcpy(&buf[3 + size], packetbuf_hdrptr(), packetbuf_totlen());
160
161 write_to_slip(buf, packetbuf_totlen() + size + 3);
162 }
163 }
164}
165/*---------------------------------------------------------------------------*/
166static void
167packet_input(void)
168{
169 if(NETSTACK_FRAMER.parse() < 0) {
170 LOG_DBG("failed to parse %u\n", packetbuf_datalen());
171 } else {
172 NETSTACK_NETWORK.input();
173 }
174}
175/*---------------------------------------------------------------------------*/
176static int
177on(void)
178{
179 return 1;
180}
181/*---------------------------------------------------------------------------*/
182static int
183off()
184{
185 return 1;
186}
187/*---------------------------------------------------------------------------*/
188static int
189max_payload()
190{
191 init_sec();
192 return 127 - NETSTACK_FRAMER.length();
193}
194/*---------------------------------------------------------------------------*/
195static void
196init(void)
197{
198 callback_pos = 0;
200}
201/*---------------------------------------------------------------------------*/
202const struct mac_driver border_router_mac_driver = {
203 "br-mac",
204 init,
207 on,
208 off,
210};
211/*---------------------------------------------------------------------------*/
Border router header file.
802.15.4 frame creation and parsing functions
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)
static void packet_input(void)
Called by the radio driver process when a frame has been received.
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