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