Optionally measure ADC2 along with temperature
[contiki-2.x.git] / examples / test-ipv6 / tapdev6.c
blob0b7b1ee2a227227f8c7282912751857a592765f5
1 /*
2 * Copyright (c) 2001, Swedish Institute of Computer Science.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the Institute nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * Author: Adam Dunkels <adam@sics.se>
34 * $Id: tapdev6.c,v 1.2 2006/08/10 19:22:23 bg- Exp $
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/uio.h>
48 #include <sys/socket.h>
51 #ifdef linux
52 #include <sys/ioctl.h>
53 #include <linux/if.h>
54 #include <linux/if_tun.h>
55 #define DEVTAP "/dev/net/tun"
56 #else /* linux */
57 #define DEVTAP "/dev/tap0"
58 #endif /* linux */
60 #include "tapdev.h"
62 #include "contiki-net.h"
64 #define DROP 0
66 #if DROP
67 static int drop = 0;
68 #endif
70 static int fd;
72 static unsigned long lasttime;
74 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
75 #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
77 static void do_send(void);
78 u8_t tapdev_send(void);
81 u16_t
82 tapdev_poll(void)
84 fd_set fdset;
85 struct timeval tv;
86 int ret;
88 tv.tv_sec = 0;
89 tv.tv_usec = 0;
91 FD_ZERO(&fdset);
92 if(fd > 0) {
93 FD_SET(fd, &fdset);
96 ret = select(fd + 1, &fdset, NULL, NULL, &tv);
98 if(ret == 0) {
99 return 0;
101 ret = read(fd, uip_buf, UIP_BUFSIZE);
103 /* printf("tapdev6: read %d bytes\n", ret);*/
105 if(ret == -1) {
106 perror("tapdev_poll: read");
108 return ret;
110 /*---------------------------------------------------------------------------*/
111 void
112 tapdev_init(void)
114 char buf[1024];
116 fd = open(DEVTAP, O_RDWR);
117 if(fd == -1) {
118 perror("tapdev: tapdev_init: open");
119 return;
122 #ifdef linux
124 struct ifreq ifr;
125 memset(&ifr, 0, sizeof(ifr));
126 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
127 if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
128 perror(buf);
129 exit(1);
132 #endif /* Linux */
134 snprintf(buf, sizeof(buf), "ifconfig tap0 inet6 fc00::123");
135 system(buf);
136 printf("%s\n", buf);
137 snprintf(buf, sizeof(buf), "route add -inet6 fc00::0/64 -interface tap0");
138 system(buf);
139 printf("%s\n", buf);
141 lasttime = 0;
143 /* gdk_input_add(fd, GDK_INPUT_READ,
144 read_callback, NULL);*/
147 /*---------------------------------------------------------------------------*/
148 static void
149 do_send(void)
151 int ret;
153 if(fd <= 0) {
154 return;
158 /* printf("tapdev_send: sending %d bytes\n", size);*/
159 /* check_checksum(uip_buf, size);*/
160 #if DROP
161 drop++;
162 if(drop % 8 == 7) {
163 printf("Dropped an output packet!\n");
164 return;
166 #endif /* DROP */
168 ret = write(fd, uip_buf, uip_len);
170 if(ret == -1) {
171 perror("tap_dev: tapdev_send: writev");
172 exit(1);
175 /*---------------------------------------------------------------------------*/
176 #define DEBUG_PRINTF(...) printf(__VA_ARGS__)
177 #define DEBUG_PRINT6ADDR(addr) DEBUG_PRINTF("%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
178 u8_t
179 tapdev_send(void)
181 struct uip_neighbor_addr *addr;
182 /* uip_arp_out();*/
184 addr = uip_neighbor_lookup(&IPBUF->destipaddr);
185 if(addr == NULL) {
186 printf("tapdev6: tapdev_send: no matching neighbor found\n");
187 DEBUG_PRINT6ADDR(&IPBUF->destipaddr);
188 printf("\n");
189 } else {
190 memcpy(&BUF->dest, addr, 6);
191 memcpy(&BUF->src, &uip_ethaddr, 6);
192 uip_len += sizeof(struct uip_eth_hdr);
193 do_send();
195 return 0;
197 /*---------------------------------------------------------------------------*/
198 void
199 tapdev_do_send(void)
201 do_send();
203 /*---------------------------------------------------------------------------*/