2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004, Swedish Institute of Computer Science.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
2010-06-14 19:19:16 +00:00
|
|
|
* $Id: ethernode.c,v 1.18 2010/06/14 19:19:17 adamdunkels Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* uIP virtual network interface using UDP tunnels.
|
|
|
|
* \author Adam Dunkels
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/ethernode.h"
|
|
|
|
#include "net/uip_arch.h"
|
|
|
|
#include "net/uip-fw.h"
|
|
|
|
#include "ether.h"
|
|
|
|
|
2007-05-22 21:09:19 +00:00
|
|
|
#include "dev/radio.h"
|
2010-02-23 18:44:08 +00:00
|
|
|
#include "net/netstack.h"
|
2010-06-14 19:19:16 +00:00
|
|
|
#include "net/packetbuf.h"
|
2007-05-22 21:09:19 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "node.h"
|
|
|
|
|
|
|
|
#include "lib/random.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2007-11-28 12:54:41 +00:00
|
|
|
#include <sys/select.h>
|
2006-06-17 22:41:10 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-02-23 18:44:08 +00:00
|
|
|
#include "net/ethernode.h"
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#define BUF ((uip_tcpip_hdr *)&uip_buf[HDR_LEN])
|
|
|
|
|
2007-03-29 22:25:25 +00:00
|
|
|
#define PRINTF(...)
|
|
|
|
/*#define PRINTF(x) printf x; fflush(NULL)*/
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
u8_t id;
|
|
|
|
int timer;
|
|
|
|
u8_t seqno;
|
|
|
|
} state;
|
|
|
|
|
|
|
|
#define TYPE_DATA 1
|
|
|
|
#define TYPE_ACK 2
|
|
|
|
|
|
|
|
struct hdr {
|
|
|
|
u8_t type;
|
|
|
|
u8_t src;
|
|
|
|
u8_t dest;
|
|
|
|
u8_t seqno;
|
|
|
|
};
|
|
|
|
|
2008-05-14 19:22:57 +00:00
|
|
|
static int radio_is_on = 1;
|
|
|
|
|
2010-02-23 18:44:08 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-05-14 19:22:57 +00:00
|
|
|
static int
|
|
|
|
ethernode_on(void)
|
|
|
|
{
|
|
|
|
radio_is_on = 1;
|
|
|
|
ether_set_radio_status(radio_is_on);
|
2010-02-23 18:44:08 +00:00
|
|
|
|
|
|
|
return 1;
|
2008-05-14 19:22:57 +00:00
|
|
|
}
|
2010-02-23 18:44:08 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-05-14 19:22:57 +00:00
|
|
|
static int
|
|
|
|
ethernode_safe_off(void)
|
|
|
|
{
|
|
|
|
radio_is_on = 0;
|
|
|
|
ether_set_radio_status(radio_is_on);
|
2010-02-23 18:44:08 +00:00
|
|
|
return 1;
|
2008-05-14 19:22:57 +00:00
|
|
|
}
|
2010-02-23 18:44:08 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
channel_clear(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
receiving(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
pending(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-22 21:09:19 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#define HDR_LEN UIP_LLH_LEN
|
|
|
|
|
|
|
|
#define ID_BROADCAST 0x80
|
2007-05-22 21:09:19 +00:00
|
|
|
|
|
|
|
PROCESS(ethernode_process, "Ethernode");
|
2006-06-17 22:41:10 +00:00
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
static u8_t
|
|
|
|
do_send(u8_t type, u8_t dest, struct hdr *hdr, int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
hdr->type = type;
|
|
|
|
hdr->src = state.id;
|
|
|
|
|
|
|
|
hdr->dest = dest;
|
|
|
|
|
|
|
|
hdr->seqno = state.seqno;
|
|
|
|
|
|
|
|
++state.seqno;
|
|
|
|
|
2007-03-29 22:25:25 +00:00
|
|
|
PRINTF("ether_send len %d\n", len);
|
2006-06-17 22:41:10 +00:00
|
|
|
return ether_send((char *)hdr, len);
|
|
|
|
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Initialize the virtual UDP tunnel network interface.
|
|
|
|
*
|
|
|
|
* \param sid The ID number of this node.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
ethernode_init(int port)
|
|
|
|
{
|
|
|
|
ether_client_init(port);
|
|
|
|
|
|
|
|
do {
|
|
|
|
state.id = random_rand() & 0x7f;
|
|
|
|
} while(state.id == ID_BROADCAST);
|
|
|
|
|
|
|
|
state.seqno = 0;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Poll the network device to see if a packet has arrived.
|
|
|
|
*
|
|
|
|
* \return The length of the incoming packet, or zero if no packet was
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-05-22 21:09:19 +00:00
|
|
|
ethernode_poll(void)
|
|
|
|
{
|
|
|
|
return ether_client_poll();
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2007-11-17 18:01:00 +00:00
|
|
|
int
|
|
|
|
ethernode_read(void *buf, unsigned short bufsize)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
int len;
|
2007-03-29 22:25:25 +00:00
|
|
|
u8_t tmpbuf[2048];
|
2007-03-13 13:07:47 +00:00
|
|
|
struct hdr *hdr = (struct hdr *)tmpbuf;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-03-29 22:25:25 +00:00
|
|
|
len = ether_client_read(tmpbuf, sizeof(tmpbuf));
|
2006-06-17 22:41:10 +00:00
|
|
|
if(len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-14 19:22:57 +00:00
|
|
|
|
|
|
|
if(radio_is_on == 0) {
|
|
|
|
/* Drop the incoming packet if the simulated radio is switched off. */
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-13 13:07:47 +00:00
|
|
|
/* printf("ethernode_poll: received data packet with len %d type %d\n", len, hdr->type);*/
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
switch(hdr->type) {
|
|
|
|
case TYPE_DATA:
|
|
|
|
if(hdr->dest == state.id ||
|
|
|
|
hdr->dest == ID_BROADCAST) {
|
2007-03-13 13:07:47 +00:00
|
|
|
memcpy(buf, tmpbuf + HDR_LEN, bufsize);
|
2006-06-17 22:41:10 +00:00
|
|
|
return len - HDR_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TYPE_ACK:
|
|
|
|
printf("<%d>: Received ack packet from %d\n", state.id, hdr->src);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("<%d>: Received unknown packet type %d from %d\n", state.id, hdr->type, hdr->src);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Send a packet from the uip_buf buffer over the UDP tunnel.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \retval UIP_FW_TOOLARGE A transmission of packet that was too large was attempted.
|
|
|
|
*
|
|
|
|
* \retval UIP_FW_DROPPED The packet is known to be dropped.
|
|
|
|
*
|
|
|
|
* \retval UIP_FW_OK The packet was transmitted.
|
|
|
|
*/
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
u8_t
|
|
|
|
ethernode_send(void)
|
|
|
|
{
|
|
|
|
int len;
|
2007-03-29 22:25:25 +00:00
|
|
|
static char tmpbuf[2048];
|
2006-06-17 22:41:10 +00:00
|
|
|
struct hdr *hdr = (struct hdr *)tmpbuf;
|
|
|
|
u8_t dest;
|
2007-11-28 12:54:41 +00:00
|
|
|
struct timeval tv;
|
2007-11-26 23:28:33 +00:00
|
|
|
|
2007-03-29 22:25:25 +00:00
|
|
|
if(uip_len > sizeof(tmpbuf)) {
|
2006-06-17 22:41:10 +00:00
|
|
|
PRINTF(("Ethernode_send: too large uip_len %d\n", uip_len));
|
|
|
|
return UIP_FW_TOOLARGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&tmpbuf[HDR_LEN], &uip_buf[HDR_LEN], uip_len);
|
|
|
|
len = uip_len + HDR_LEN;
|
|
|
|
|
|
|
|
dest = ID_BROADCAST;
|
2007-11-28 12:54:41 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = (random_rand() % 1000);
|
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
do_send(TYPE_DATA, dest, hdr, len);
|
|
|
|
|
|
|
|
return UIP_FW_OK;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2010-02-23 18:44:08 +00:00
|
|
|
static char tmpbuf[2048];
|
|
|
|
static struct hdr *hdr = (struct hdr *)tmpbuf;
|
|
|
|
static u8_t dest;
|
2007-03-13 13:07:47 +00:00
|
|
|
|
2010-02-23 18:44:08 +00:00
|
|
|
static int
|
|
|
|
prepare(const void *buf, unsigned short len)
|
|
|
|
{
|
2007-03-13 13:07:47 +00:00
|
|
|
memcpy(&tmpbuf[HDR_LEN], buf, len);
|
|
|
|
len = len + HDR_LEN;
|
|
|
|
|
|
|
|
dest = ID_BROADCAST;
|
2007-05-22 21:09:19 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2010-02-23 18:44:08 +00:00
|
|
|
static int
|
|
|
|
transmit(unsigned short len)
|
|
|
|
{
|
|
|
|
do_send(TYPE_DATA, dest, hdr, len + HDR_LEN);
|
|
|
|
return RADIO_TX_OK;
|
|
|
|
}
|
2007-05-22 21:09:19 +00:00
|
|
|
/*-------------------------------------------------------------------------------*/
|
2010-02-23 18:44:08 +00:00
|
|
|
static int
|
|
|
|
send(const void *payload, unsigned short payload_len)
|
2007-05-22 21:09:19 +00:00
|
|
|
{
|
2010-02-23 18:44:08 +00:00
|
|
|
prepare(payload, payload_len);
|
|
|
|
return transmit(payload_len);
|
2007-05-22 21:09:19 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(ethernode_process, ev, data)
|
|
|
|
{
|
2010-02-23 18:44:08 +00:00
|
|
|
int len;
|
2007-05-22 21:09:19 +00:00
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
2008-08-15 19:54:58 +00:00
|
|
|
process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL);
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
|
2010-02-23 18:44:08 +00:00
|
|
|
|
|
|
|
len = ethernode_poll();
|
|
|
|
if(len > 0) {
|
|
|
|
|
|
|
|
packetbuf_clear();
|
|
|
|
len = ethernode_read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
|
|
|
if(len > 0) {
|
|
|
|
packetbuf_set_datalen(len);
|
|
|
|
NETSTACK_RDC.input();
|
2007-05-22 21:09:19 +00:00
|
|
|
}
|
2010-02-23 18:44:08 +00:00
|
|
|
|
|
|
|
/* if(receiver_callback) {
|
|
|
|
receiver_callback(ðernode_driver);
|
|
|
|
}*/
|
2007-05-22 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PROCESS_END();
|
2007-03-13 13:07:47 +00:00
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2010-02-23 18:44:08 +00:00
|
|
|
const struct radio_driver ethernode_driver =
|
|
|
|
{
|
|
|
|
(int (*)(void))ethernode_init,
|
|
|
|
prepare,
|
|
|
|
transmit,
|
|
|
|
send,
|
|
|
|
ethernode_read,
|
|
|
|
channel_clear,
|
|
|
|
receiving,
|
|
|
|
pending,
|
|
|
|
ethernode_on,
|
|
|
|
ethernode_safe_off,
|
|
|
|
};
|
|
|
|
|
|
|
|
|