2013-12-06 11:25:23 +00:00
|
|
|
/*
|
|
|
|
* 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 <farcaller@gmail.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-12-05 18:21:06 +00:00
|
|
|
#include "contiki.h"
|
2014-03-01 10:37:10 +00:00
|
|
|
#include "contiki-conf.h"
|
2013-12-05 18:21:06 +00:00
|
|
|
|
2015-09-29 11:29:08 +00:00
|
|
|
#if defined(linux) && NETSTACK_CONF_WITH_IPV6
|
2014-02-19 17:04:00 +00:00
|
|
|
|
2013-12-05 18:21:06 +00:00
|
|
|
#include "linuxradio-drv.h"
|
|
|
|
|
|
|
|
#include "net/packetbuf.h"
|
|
|
|
#include "net/netstack.h"
|
|
|
|
|
2015-10-04 20:33:27 +00:00
|
|
|
#include <stdlib.h>
|
2015-10-01 10:00:02 +00:00
|
|
|
#include <stdio.h>
|
2014-02-19 17:04:00 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <string.h>
|
2013-12-05 18:21:06 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int sockfd = -1;
|
|
|
|
static char *sockbuf;
|
|
|
|
static int buflen;
|
|
|
|
|
2013-12-06 11:25:23 +00:00
|
|
|
#define MAX_PACKET_SIZE 256
|
2013-12-05 18:21:06 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
init(void)
|
|
|
|
{
|
2013-12-06 11:25:23 +00:00
|
|
|
sockbuf = malloc(MAX_PACKET_SIZE);
|
2014-01-25 17:16:54 +00:00
|
|
|
if(sockbuf == 0) {
|
2014-01-25 17:16:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2013-12-05 18:21:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
prepare(const void *payload, unsigned short payload_len)
|
|
|
|
{
|
2014-01-25 17:16:54 +00:00
|
|
|
if(payload_len > MAX_PACKET_SIZE) {
|
2013-12-05 18:21:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(sockbuf, payload, payload_len);
|
|
|
|
buflen = payload_len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
transmit(unsigned short transmit_len)
|
|
|
|
{
|
2014-01-25 17:16:54 +00:00
|
|
|
int sent = 0;
|
2013-12-05 18:21:06 +00:00
|
|
|
sent = send(sockfd, sockbuf, buflen, 0);
|
2014-01-25 17:16:54 +00:00
|
|
|
if(sent < 0) {
|
2013-12-06 11:25:23 +00:00
|
|
|
perror("linuxradio send()");
|
2014-01-25 17:13:15 +00:00
|
|
|
return RADIO_TX_ERR;
|
2013-12-05 18:21:06 +00:00
|
|
|
}
|
|
|
|
buflen = 0;
|
|
|
|
return RADIO_TX_OK;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
my_send(const void *payload, unsigned short payload_len)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
2014-01-25 17:16:54 +00:00
|
|
|
if(prepare(payload, payload_len)) {
|
2013-12-05 18:21:06 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-01-25 17:16:54 +00:00
|
|
|
if(FD_ISSET(sockfd, rset)) {
|
2014-01-25 17:13:49 +00:00
|
|
|
int bytes = read(sockfd, sockbuf, MAX_PACKET_SIZE);
|
2013-12-05 18:21:06 +00:00
|
|
|
buflen = bytes;
|
|
|
|
memcpy(packetbuf_dataptr(), sockbuf, bytes);
|
|
|
|
packetbuf_set_datalen(bytes);
|
2017-05-17 13:09:19 +00:00
|
|
|
NETSTACK_MAC.input();
|
2013-12-05 18:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 17:04:00 +00:00
|
|
|
|
2013-12-05 18:21:06 +00:00
|
|
|
static const struct select_callback linuxradio_sock_callback = { set_fd, handle_fd };
|
|
|
|
|
|
|
|
static int
|
|
|
|
on(void)
|
|
|
|
{
|
2014-01-25 17:16:54 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
int err;
|
|
|
|
struct sockaddr_ll sll;
|
|
|
|
|
2013-12-05 18:21:06 +00:00
|
|
|
sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IEEE802154));
|
2014-01-25 17:16:54 +00:00
|
|
|
if(sockfd < 0) {
|
|
|
|
perror("linuxradio socket()");
|
2013-12-05 18:21:06 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2014-03-01 10:30:41 +00:00
|
|
|
strncpy((char *)ifr.ifr_name, NETSTACK_CONF_LINUXRADIO_DEV, IFNAMSIZ);
|
2014-01-25 17:16:54 +00:00
|
|
|
err = ioctl(sockfd, SIOCGIFINDEX, &ifr);
|
|
|
|
if(err == -1) {
|
|
|
|
perror("linuxradio ioctl()");
|
2014-01-25 17:16:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-05 18:21:06 +00:00
|
|
|
sll.sll_family = AF_PACKET;
|
|
|
|
sll.sll_ifindex = ifr.ifr_ifindex;
|
|
|
|
sll.sll_protocol = htons(ETH_P_IEEE802154);
|
|
|
|
|
2014-01-25 17:16:54 +00:00
|
|
|
if(bind(sockfd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
|
2013-12-05 18:21:06 +00:00
|
|
|
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 =
|
2014-01-25 17:16:54 +00:00
|
|
|
{
|
|
|
|
init,
|
|
|
|
prepare,
|
|
|
|
transmit,
|
|
|
|
my_send,
|
|
|
|
my_read,
|
|
|
|
channel_clear,
|
|
|
|
receiving_packet,
|
|
|
|
pending_packet,
|
|
|
|
on,
|
|
|
|
off,
|
|
|
|
};
|
2014-02-19 17:04:00 +00:00
|
|
|
|
|
|
|
#endif
|