diff --git a/cpu/native/net/linuxradio-drv.c b/cpu/native/net/linuxradio-drv.c new file mode 100644 index 000000000..f07861f86 --- /dev/null +++ b/cpu/native/net/linuxradio-drv.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2013, Google + * 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. + * + * Author: Vladimir Pouzanov + * + */ + +#include "contiki.h" +#include "contiki-conf.h" + +#if defined(linux) && UIP_CONF_IPV6 + +#include "linuxradio-drv.h" + +#include "net/packetbuf.h" +#include "net/netstack.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEBUG 0 +#if DEBUG +#include +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif + +static int sockfd = -1; +static char *sockbuf; +static int buflen; + +#define MAX_PACKET_SIZE 256 + +static int +init(void) +{ + sockbuf = malloc(MAX_PACKET_SIZE); + if(sockbuf == 0) { + return 1; + } + return 0; +} +static int +prepare(const void *payload, unsigned short payload_len) +{ + if(payload_len > MAX_PACKET_SIZE) { + return 0; + } + memcpy(sockbuf, payload, payload_len); + buflen = payload_len; + + return 0; +} +static int +transmit(unsigned short transmit_len) +{ + int sent = 0; + sent = send(sockfd, sockbuf, buflen, 0); + if(sent < 0) { + perror("linuxradio send()"); + return RADIO_TX_ERR; + } + buflen = 0; + return RADIO_TX_OK; +} +static int +my_send(const void *payload, unsigned short payload_len) +{ + int ret = -1; + + if(prepare(payload, payload_len)) { + return ret; + } + + ret = transmit(payload_len); + + return ret; +} +static int +my_read(void *buf, unsigned short buf_len) +{ + return 0; +} +static int +channel_clear(void) +{ + return 1; +} +static int +receiving_packet(void) +{ + return 0; +} +static int +pending_packet(void) +{ + return 0; +} +static int +set_fd(fd_set *rset, fd_set *wset) +{ + FD_SET(sockfd, rset); + return 1; +} +static void +handle_fd(fd_set *rset, fd_set *wset) +{ + if(FD_ISSET(sockfd, rset)) { + int bytes = read(sockfd, sockbuf, MAX_PACKET_SIZE); + buflen = bytes; + memcpy(packetbuf_dataptr(), sockbuf, bytes); + packetbuf_set_datalen(bytes); + NETSTACK_RDC.input(); + } +} + +static const struct select_callback linuxradio_sock_callback = { set_fd, handle_fd }; + +static int +on(void) +{ + struct ifreq ifr; + int err; + struct sockaddr_ll sll; + + sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IEEE802154)); + if(sockfd < 0) { + perror("linuxradio socket()"); + return 0; + } else { + strncpy((char *)ifr.ifr_name, NETSTACK_CONF_LINUXRADIO_DEV, IFNAMSIZ); + err = ioctl(sockfd, SIOCGIFINDEX, &ifr); + if(err == -1) { + perror("linuxradio ioctl()"); + return 0; + } + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifr.ifr_ifindex; + sll.sll_protocol = htons(ETH_P_IEEE802154); + + if(bind(sockfd, (struct sockaddr *)&sll, sizeof(sll)) < 0) { + perror("linuxradio bind()"); + return 0; + } + + select_set_callback(sockfd, &linuxradio_sock_callback); + return 1; + } +} +static int +off(void) +{ + close(sockfd); + sockfd = -1; + return 1; +} +const struct radio_driver linuxradio_driver = +{ + init, + prepare, + transmit, + my_send, + my_read, + channel_clear, + receiving_packet, + pending_packet, + on, + off, +}; + +#endif diff --git a/cpu/native/net/linuxradio-drv.h b/cpu/native/net/linuxradio-drv.h new file mode 100644 index 000000000..fddf38d01 --- /dev/null +++ b/cpu/native/net/linuxradio-drv.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013, Google + * 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. + * + * Author: Vladimir Pouzanov + * + */ + +#ifndef __LINUXRADIO_DRV_H__ +#define __LINUXRADIO_DRV_H__ + +#include "dev/radio.h" + +extern const struct radio_driver linuxradio_driver; + +#endif diff --git a/platform/minimal-net/Makefile.minimal-net b/platform/minimal-net/Makefile.minimal-net index dd7632d92..2c2399617 100644 --- a/platform/minimal-net/Makefile.minimal-net +++ b/platform/minimal-net/Makefile.minimal-net @@ -14,7 +14,7 @@ CONTIKI_TARGET_SOURCEFILES = contiki-main.c clock.c leds.c leds-arch.c cfs-posix ifeq ($(HOST_OS),Windows) CONTIKI_TARGET_SOURCEFILES += wpcap-drv.c wpcap.c else -CONTIKI_TARGET_SOURCEFILES += tapdev-drv.c tapdev.c tapdev6.c +CONTIKI_TARGET_SOURCEFILES += tapdev-drv.c tapdev.c tapdev6.c linuxradio-drv.c endif CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) diff --git a/platform/minimal-net/contiki-conf.h b/platform/minimal-net/contiki-conf.h index e736b363d..bbc44642b 100644 --- a/platform/minimal-net/contiki-conf.h +++ b/platform/minimal-net/contiki-conf.h @@ -35,6 +35,15 @@ #include #include +#ifndef WIN32_LEAN_AND_MEAN +#include +#endif + +struct select_callback { + int (* set_fd)(fd_set *fdr, fd_set *fdw); + void (* handle_fd)(fd_set *fdr, fd_set *fdw); +}; +int select_set_callback(int fd, const struct select_callback *callback); #define CC_CONF_REGISTER_ARGS 1 #define CC_CONF_FUNCTION_POINTER_ARGS 1 @@ -88,9 +97,9 @@ typedef unsigned short uip_stats_t; */ #define WEBSERVER_CONF_STATUSPAGE 1 -/* RPL currently works only on Windows. *nix would require converting the tun interface to two pcap tees. */ +/* RPL currently works only on Windows. *nix would require converting the tun interface to two pcap tees. */ //#define RPL_BORDER_ROUTER 0 -#endif +#endif #if UIP_CONF_IPV6_RPL /* RPL motes use the uip.c link layer address or optionally the harded coded address (but without the prefix!) @@ -121,7 +130,7 @@ typedef unsigned short uip_stats_t; * e.g. the jackdaw RNDIS <-> repeater. Then RPL will configure on the radio network and the RF motes will * be reached through bbbb::. * Possibly minimal-net RPL motes could also be added to this interface? - * + * */ #undef UIP_CONF_ROUTER #define UIP_CONF_ROUTER 1 @@ -151,6 +160,8 @@ typedef unsigned short uip_stats_t; /* Not used but avoids compile errors while sicslowpan.c is being developed */ #define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06 +#define NETSTACK_CONF_LINUXRADIO_DEV "wpan0" + #define UIP_CONF_UDP 1 #define UIP_CONF_TCP 1 diff --git a/platform/native/Makefile.native b/platform/native/Makefile.native index 198dbabee..504c91a3a 100644 --- a/platform/native/Makefile.native +++ b/platform/native/Makefile.native @@ -17,7 +17,7 @@ ifeq ($(HOST_OS),Windows) CONTIKI_TARGET_SOURCEFILES += wpcap-drv.c wpcap.c TARGET_LIBFILES = /lib/w32api/libws2_32.a /lib/w32api/libiphlpapi.a else -CONTIKI_TARGET_SOURCEFILES += tapdev-drv.c +CONTIKI_TARGET_SOURCEFILES += tapdev-drv.c linuxradio-drv.c #math ifneq ($(CONTIKI_WITH_IPV6),1) CONTIKI_TARGET_SOURCEFILES += tapdev.c diff --git a/platform/native/contiki-conf.h b/platform/native/contiki-conf.h index 5ce72c09e..b3c15d48c 100644 --- a/platform/native/contiki-conf.h +++ b/platform/native/contiki-conf.h @@ -101,6 +101,8 @@ typedef unsigned short uip_stats_t; #define NETSTACK_CONF_NETWORK sicslowpan_driver +#define NETSTACK_CONF_LINUXRADIO_DEV "wpan0" + #define UIP_CONF_ROUTER 1 #define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_COMPRESSION_HC06