2006-06-18 00:41:10 +02: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>
|
|
|
|
*
|
2007-11-28 13:54:41 +01:00
|
|
|
* $Id: ethernode.c,v 1.13 2007/11/28 12:54:42 adamdunkels Exp $
|
2006-06-18 00:41:10 +02: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 23:09:19 +02:00
|
|
|
#include "dev/radio.h"
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "node.h"
|
|
|
|
|
|
|
|
#include "lib/random.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2007-11-28 13:54:41 +01:00
|
|
|
#include <sys/select.h>
|
2006-06-18 00:41:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define BUF ((uip_tcpip_hdr *)&uip_buf[HDR_LEN])
|
|
|
|
|
2007-03-30 00:25:25 +02:00
|
|
|
#define PRINTF(...)
|
|
|
|
/*#define PRINTF(x) printf x; fflush(NULL)*/
|
2006-06-18 00:41:10 +02: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;
|
|
|
|
};
|
|
|
|
|
2007-05-22 23:09:19 +02:00
|
|
|
static int ethernode_on(void) {return 0;}
|
|
|
|
static int ethernode_safe_off(void) {return 0;}
|
|
|
|
|
|
|
|
#include "net/ethernode.h"
|
|
|
|
|
|
|
|
const struct radio_driver ethernode_driver =
|
|
|
|
{
|
|
|
|
ethernode_send_buf,
|
|
|
|
ethernode_read,
|
|
|
|
ethernode_set_receiver,
|
|
|
|
ethernode_on,
|
|
|
|
ethernode_safe_off,
|
|
|
|
};
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#define HDR_LEN UIP_LLH_LEN
|
|
|
|
|
|
|
|
#define ID_BROADCAST 0x80
|
2007-05-22 23:09:19 +02:00
|
|
|
|
|
|
|
PROCESS(ethernode_process, "Ethernode");
|
2006-06-18 00:41:10 +02: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-30 00:25:25 +02:00
|
|
|
PRINTF("ether_send len %d\n", len);
|
2006-06-18 00:41:10 +02: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 23:09:19 +02:00
|
|
|
ethernode_poll(void)
|
|
|
|
{
|
|
|
|
return ether_client_poll();
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2007-11-17 19:01:00 +01:00
|
|
|
int
|
|
|
|
ethernode_read(void *buf, unsigned short bufsize)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
int len;
|
2007-03-30 00:25:25 +02:00
|
|
|
u8_t tmpbuf[2048];
|
2007-03-13 14:07:47 +01:00
|
|
|
struct hdr *hdr = (struct hdr *)tmpbuf;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2007-03-30 00:25:25 +02:00
|
|
|
len = ether_client_read(tmpbuf, sizeof(tmpbuf));
|
2006-06-18 00:41:10 +02:00
|
|
|
if(len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-13 14:07:47 +01:00
|
|
|
/* printf("ethernode_poll: received data packet with len %d type %d\n", len, hdr->type);*/
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
switch(hdr->type) {
|
|
|
|
case TYPE_DATA:
|
|
|
|
if(hdr->dest == state.id ||
|
|
|
|
hdr->dest == ID_BROADCAST) {
|
2007-03-13 14:07:47 +01:00
|
|
|
memcpy(buf, tmpbuf + HDR_LEN, bufsize);
|
2006-06-18 00:41:10 +02: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-30 00:25:25 +02:00
|
|
|
static char tmpbuf[2048];
|
2006-06-18 00:41:10 +02:00
|
|
|
struct hdr *hdr = (struct hdr *)tmpbuf;
|
|
|
|
u8_t dest;
|
2007-11-28 13:54:41 +01:00
|
|
|
struct timeval tv;
|
2007-11-27 00:28:33 +01:00
|
|
|
|
2007-03-30 00:25:25 +02:00
|
|
|
if(uip_len > sizeof(tmpbuf)) {
|
2006-06-18 00:41:10 +02: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 13:54:41 +01:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = (random_rand() % 1000);
|
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
do_send(TYPE_DATA, dest, hdr, len);
|
|
|
|
|
|
|
|
return UIP_FW_OK;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
2007-05-22 23:09:19 +02:00
|
|
|
int
|
2007-11-17 19:01:00 +01:00
|
|
|
ethernode_send_buf(const void *buf, unsigned short len)
|
2007-03-13 14:07:47 +01:00
|
|
|
{
|
2007-03-30 00:25:25 +02:00
|
|
|
char tmpbuf[2048];
|
2007-03-13 14:07:47 +01:00
|
|
|
struct hdr *hdr = (struct hdr *)tmpbuf;
|
|
|
|
u8_t dest;
|
|
|
|
|
|
|
|
memcpy(&tmpbuf[HDR_LEN], buf, len);
|
|
|
|
len = len + HDR_LEN;
|
|
|
|
|
|
|
|
dest = ID_BROADCAST;
|
|
|
|
do_send(TYPE_DATA, dest, hdr, len);
|
2007-05-22 23:09:19 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
static void (* receiver_callback)(const struct radio_driver *);
|
|
|
|
/*-------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
ethernode_set_receiver(void (* recv)(const struct radio_driver *))
|
|
|
|
{
|
|
|
|
process_start(ðernode_process, NULL);
|
|
|
|
receiver_callback = recv;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(ethernode_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
process_poll(ðernode_process);
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
|
|
|
|
if(ethernode_poll()) {
|
|
|
|
if(receiver_callback) {
|
|
|
|
receiver_callback(ðernode_driver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PROCESS_END();
|
2007-03-13 14:07:47 +01:00
|
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------------*/
|