2012-01-01 02:00:21 +00:00
|
|
|
/*
|
2011-12-19 12:50:50 +00:00
|
|
|
* Copyright (c) 2011, Swedish Institute of Computer Science.
|
2012-01-01 02:00:21 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2011-12-19 12:50:50 +00:00
|
|
|
* A null RDC implementation that uses framer for headers and sends
|
|
|
|
* the packets over slip instead of radio.
|
2012-01-01 02:00:21 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
2011-12-19 12:50:50 +00:00
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
2012-01-01 02:00:21 +00:00
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/packetbuf.h"
|
|
|
|
#include "net/queuebuf.h"
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "packetutils.h"
|
2011-12-19 12:50:50 +00:00
|
|
|
#include "border-router.h"
|
|
|
|
#include <string.h>
|
2012-01-01 02:00:21 +00:00
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
#define DEBUG 0
|
2012-01-01 02:00:21 +00:00
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_CALLBACKS 16
|
|
|
|
static int callback_pos;
|
|
|
|
|
|
|
|
/* a structure for calling back when packet data is coming back
|
|
|
|
from radio... */
|
|
|
|
struct tx_callback {
|
|
|
|
mac_callback_t cback;
|
|
|
|
void *ptr;
|
2011-12-19 12:50:50 +00:00
|
|
|
struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
|
|
|
|
struct packetbuf_addr addrs[PACKETBUF_NUM_ADDRS];
|
2012-01-01 02:00:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tx_callback callbacks[MAX_CALLBACKS];
|
2011-12-19 12:50:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-02 00:07:54 +00:00
|
|
|
void
|
|
|
|
packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx)
|
2012-01-01 02:00:21 +00:00
|
|
|
{
|
|
|
|
if(sessionid < MAX_CALLBACKS) {
|
|
|
|
struct tx_callback *callback;
|
|
|
|
callback = &callbacks[sessionid];
|
2011-12-19 12:50:50 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
packetbuf_attr_copyfrom(callback->attrs, callback->addrs);
|
|
|
|
mac_call_sent_callback(callback->cback, callback->ptr, status, tx);
|
2012-01-01 02:00:21 +00:00
|
|
|
} else {
|
2012-01-08 18:33:40 +00:00
|
|
|
PRINTF("*** ERROR: too high session id %d\n", sessionid);
|
2012-01-01 02:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-19 12:50:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-01 02:00:21 +00:00
|
|
|
static int
|
2011-12-19 12:50:50 +00:00
|
|
|
setup_callback(mac_callback_t sent, void *ptr)
|
2012-01-01 02:00:21 +00:00
|
|
|
{
|
|
|
|
struct tx_callback *callback;
|
2011-12-19 12:50:50 +00:00
|
|
|
int tmp = callback_pos;
|
2012-01-01 02:00:21 +00:00
|
|
|
callback = &callbacks[callback_pos];
|
|
|
|
callback->cback = sent;
|
|
|
|
callback->ptr = ptr;
|
2011-12-19 12:50:50 +00:00
|
|
|
packetbuf_attr_copyto(callback->attrs, callback->addrs);
|
|
|
|
|
2012-01-01 02:00:21 +00:00
|
|
|
callback_pos++;
|
2011-12-19 12:50:50 +00:00
|
|
|
if(callback_pos >= MAX_CALLBACKS) {
|
2012-01-01 02:00:21 +00:00
|
|
|
callback_pos = 0;
|
2011-12-19 12:50:50 +00:00
|
|
|
}
|
|
|
|
|
2012-01-01 02:00:21 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
send_packet(mac_callback_t sent, void *ptr)
|
|
|
|
{
|
2011-12-19 12:50:50 +00:00
|
|
|
int size;
|
|
|
|
/* 3 bytes per packet attribute is required for serialization */
|
|
|
|
uint8_t buf[PACKETBUF_NUM_ATTRS * 3 + PACKETBUF_SIZE + 3];
|
|
|
|
uint8_t sid;
|
|
|
|
|
2013-12-12 22:58:52 +00:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_SENDER, &linkaddr_node_addr);
|
2012-01-01 02:00:21 +00:00
|
|
|
|
|
|
|
/* ack or not ? */
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAC_ACK, 1);
|
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
if(NETSTACK_FRAMER.create() < 0) {
|
2012-01-01 02:00:21 +00:00
|
|
|
/* Failed to allocate space for headers */
|
|
|
|
PRINTF("br-rdc: send failed, too large header\n");
|
2011-12-19 12:50:50 +00:00
|
|
|
mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
|
|
|
|
} else {
|
|
|
|
/* here we send the data over SLIP to the radio-chip */
|
2012-03-16 10:08:47 +00:00
|
|
|
size = 0;
|
|
|
|
#if SERIALIZE_ATTRIBUTES
|
2011-12-19 12:50:50 +00:00
|
|
|
size = packetutils_serialize_atts(&buf[3], sizeof(buf) - 3);
|
2012-03-16 10:08:47 +00:00
|
|
|
#endif
|
2011-12-19 12:50:50 +00:00
|
|
|
if(size < 0 || size + packetbuf_totlen() + 3 > sizeof(buf)) {
|
|
|
|
PRINTF("br-rdc: send failed, too large header\n");
|
|
|
|
mac_call_sent_callback(sent, ptr, MAC_TX_ERR_FATAL, 1);
|
|
|
|
} else {
|
|
|
|
sid = setup_callback(sent, ptr);
|
2012-01-01 02:00:21 +00:00
|
|
|
|
|
|
|
buf[0] = '!';
|
|
|
|
buf[1] = 'S';
|
|
|
|
buf[2] = sid; /* sequence or session number for this packet */
|
2011-12-19 12:50:50 +00:00
|
|
|
|
|
|
|
/* Copy packet data */
|
|
|
|
memcpy(&buf[3 + size], packetbuf_hdrptr(), packetbuf_totlen());
|
|
|
|
|
|
|
|
write_to_slip(buf, packetbuf_totlen() + size + 3);
|
2012-01-01 02:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
packet_input(void)
|
|
|
|
{
|
2011-12-19 12:50:50 +00:00
|
|
|
if(NETSTACK_FRAMER.parse() < 0) {
|
2012-01-01 02:00:21 +00:00
|
|
|
PRINTF("br-rdc: failed to parse %u\n", packetbuf_datalen());
|
|
|
|
} else {
|
2017-05-17 13:09:19 +00:00
|
|
|
NETSTACK_NETWORK.input();
|
2012-01-01 02:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
on(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2017-05-17 13:09:19 +00:00
|
|
|
off()
|
2012-01-01 02:00:21 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
callback_pos = 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-05-17 13:09:19 +00:00
|
|
|
const struct mac_driver border_router_mac_driver = {
|
|
|
|
"br-mac",
|
2012-01-01 02:00:21 +00:00
|
|
|
init,
|
|
|
|
send_packet,
|
|
|
|
packet_input,
|
|
|
|
on,
|
2017-05-17 12:34:29 +00:00
|
|
|
off
|
2012-01-01 02:00:21 +00:00
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|