Contiki-NG
Loading...
Searching...
No Matches
slip-dev.c
1/*
2 * Copyright (c) 2001, Adam Dunkels.
3 * Copyright (c) 2009, 2010 Joakim Eriksson, Niclas Finne, Dogan Yazar.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32 /* Below define allows importing saved output into Wireshark as "Raw IP" packet type */
33#define WIRESHARK_IMPORT_FORMAT 1
34#include "contiki.h"
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <string.h>
40#include <time.h>
41#include <sys/types.h>
42#include <unistd.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <termios.h>
46#include <signal.h>
47#include <sys/ioctl.h>
48#include <sys/socket.h>
49#include <netinet/in.h>
50#include <arpa/inet.h>
51#include <netdb.h>
52#include <err.h>
53
54#include "net/netstack.h"
55#include "net/packetbuf.h"
56#include "cmd.h"
57#include "border-router.h"
58#include "border-router-cmds.h"
59#include "border-router-cbor.h"
60
61extern int slip_config_verbose;
62extern int slip_config_flowcontrol;
63extern const char *slip_config_siodev;
64extern const char *slip_config_host;
65extern const char *slip_config_port;
66extern uint16_t slip_config_basedelay;
67extern speed_t slip_config_b_rate;
68
69#ifdef SLIP_DEV_CONF_SEND_DELAY
70#define SEND_DELAY SLIP_DEV_CONF_SEND_DELAY
71#else
72#define SEND_DELAY 0
73#endif
74
75static FILE *inslip;
76
77/* for statistics */
78long slip_sent = 0;
79long slip_received = 0;
80
81int slipfd = 0;
82
83#define PROGRESS(s) do { } while(0)
84
85#define SLIP_END 0300
86#define SLIP_ESC 0333
87#define SLIP_ESC_END 0334
88#define SLIP_ESC_ESC 0335
89
90/*---------------------------------------------------------------------------*/
91static void *
92get_in_addr(struct sockaddr *sa)
93{
94 if(sa->sa_family == AF_INET) {
95 return &(((struct sockaddr_in *)sa)->sin_addr);
96 }
97 return &(((struct sockaddr_in6 *)sa)->sin6_addr);
98}
99/*---------------------------------------------------------------------------*/
100static int
101connect_to_server(const char *host, const char *port)
102{
103 /* Setup TCP connection */
104 struct addrinfo hints, *servinfo, *p;
105 char s[INET6_ADDRSTRLEN];
106 int rv, fd;
107
108 memset(&hints, 0, sizeof hints);
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_socktype = SOCK_STREAM;
111
112 if((rv = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
113 err(EXIT_FAILURE, "getaddrinfo: %s", gai_strerror(rv));
114 return -1;
115 }
116
117 /* loop through all the results and connect to the first we can */
118 for(p = servinfo; p != NULL; p = p->ai_next) {
119 if((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
120 perror("client: socket");
121 continue;
122 }
123
124 if(connect(fd, p->ai_addr, p->ai_addrlen) == -1) {
125 close(fd);
126 perror("client: connect");
127 continue;
128 }
129 break;
130 }
131
132 if(p == NULL) {
133 err(EXIT_FAILURE, "can't connect to ``%s:%s''", host, port);
134 return -1;
135 }
136
137 fcntl(fd, F_SETFL, O_NONBLOCK);
138
139 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
140 s, sizeof(s));
141
142 /* all done with this structure */
143 freeaddrinfo(servinfo);
144 return fd;
145}
146/*---------------------------------------------------------------------------*/
147int
148is_sensible_string(const unsigned char *s, int len)
149{
150 int i;
151 for(i = 1; i < len; i++) {
152 if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
153 continue;
154 } else if(s[i] < ' ' || '~' < s[i]) {
155 return 0;
156 }
157 }
158 return 1;
159}
160/*---------------------------------------------------------------------------*/
161void
162slip_packet_input(unsigned char *data, int len)
163{
164 packetbuf_copyfrom(data, len);
165 if(slip_config_verbose > 0) {
166 printf("Packet input over SLIP: %d\n", len);
167 }
168 NETSTACK_MAC.input();
169}
170/*---------------------------------------------------------------------------*/
171/*
172 * Read from serial, when we have a packet call slip_packet_input. No output
173 * buffering, input buffered by stdio.
174 */
175void
176serial_input(FILE *inslip)
177{
178 static unsigned char inbuf[2048];
179 static int inbufptr = 0;
180 int ret, i;
181 unsigned char c;
182
183#ifdef linux
184 ret = fread(&c, 1, 1, inslip);
185 if(ret == -1 || ret == 0) {
186 err(EXIT_FAILURE, "serial_input: read");
187 }
188 goto after_fread;
189#endif
190
191read_more:
192 if(inbufptr >= sizeof(inbuf)) {
193 fprintf(stderr, "*** dropping large %d byte packet\n", inbufptr);
194 inbufptr = 0;
195 }
196 ret = fread(&c, 1, 1, inslip);
197#ifdef linux
198after_fread:
199#endif
200 if(ret == -1) {
201 err(EXIT_FAILURE, "serial_input: read");
202 }
203 if(ret == 0) {
204 clearerr(inslip);
205 return;
206 }
207 slip_received++;
208 switch(c) {
209 case SLIP_END:
210 if(inbufptr > 0) {
211 if(inbuf[0] == '!') {
212 command_context = CMD_CONTEXT_RADIO;
213 cmd_input(inbuf, inbufptr);
214 } else if(inbuf[0] == '?') {
215#define DEBUG_LINE_MARKER '\r'
216 } else if(inbuf[0] == DEBUG_LINE_MARKER) {
217 fwrite(inbuf + 1, inbufptr - 1, 1, stdout);
218 } else if(is_sensible_string(inbuf, inbufptr)) {
219 if(slip_config_verbose == 1) { /* strings already echoed below for verbose>1 */
220 fwrite(inbuf, inbufptr, 1, stdout);
221 }
222 } else {
223 if(slip_config_verbose > 2) {
224 printf("Packet from SLIP of length %d - write TUN\n", inbufptr);
225 if(slip_config_verbose > 4) {
226#if WIRESHARK_IMPORT_FORMAT
227 printf("0000");
228 for(i = 0; i < inbufptr; i++) {
229 printf(" %02x", inbuf[i]);
230 }
231#else
232 printf(" ");
233 for(i = 0; i < inbufptr; i++) {
234 printf("%02x", inbuf[i]);
235 if((i & 3) == 3) {
236 printf(" ");
237 }
238 if((i & 15) == 15) {
239 printf("\n ");
240 }
241 }
242#endif
243 printf("\n");
244 }
245 }
246#if BORDER_ROUTER_SERIAL_RADIO
247 /* serialradio: a binary SLIP frame is a CBOR message with a trailing
248 CRC16. Verify and dispatch it (RX frames, TX responses, address
249 reports, ...). */
250 border_router_cbor_input(inbuf, inbufptr);
251#else
252 slip_packet_input(inbuf, inbufptr);
253#endif
254 }
255 inbufptr = 0;
256 }
257 break;
258
259 case SLIP_ESC:
260 if(fread(&c, 1, 1, inslip) != 1) {
261 clearerr(inslip);
262 /* Put ESC back and give up! */
263 ungetc(SLIP_ESC, inslip);
264 return;
265 }
266
267 switch(c) {
268 case SLIP_ESC_END:
269 c = SLIP_END;
270 break;
271 case SLIP_ESC_ESC:
272 c = SLIP_ESC;
273 break;
274 }
275 /* FALLTHROUGH */
276 default:
277 inbuf[inbufptr++] = c;
278
279 /* Echo lines as they are received for verbose=2,3,5+ */
280 /* Echo all printable characters for verbose==4 */
281 if(slip_config_verbose == 4) {
282 if(c == 0 || c == '\r' || c == '\n' || c == '\t' || (c >= ' ' && c <= '~')) {
283 fwrite(&c, 1, 1, stdout);
284 }
285 } else if(slip_config_verbose >= 2) {
286 if(c == '\n' && is_sensible_string(inbuf, inbufptr)) {
287 fwrite(inbuf, inbufptr, 1, stdout);
288 inbufptr = 0;
289 }
290 }
291 break;
292 }
293
294 goto read_more;
295}
296unsigned char slip_buf[2048];
297int slip_end, slip_begin, slip_packet_end, slip_packet_count;
298static struct timer send_delay_timer;
299/* delay between slip packets */
300static clock_time_t send_delay = SEND_DELAY;
301/*---------------------------------------------------------------------------*/
302static void
303slip_send(int fd, unsigned char c)
304{
305 if(slip_end >= sizeof(slip_buf)) {
306 err(EXIT_FAILURE, "slip_send overflow");
307 }
308 slip_buf[slip_end] = c;
309 slip_end++;
310 slip_sent++;
311 if(c == SLIP_END) {
312 /* Full packet received. */
313 slip_packet_count++;
314 if(slip_packet_end == 0) {
315 slip_packet_end = slip_end;
316 }
317 }
318}
319/*---------------------------------------------------------------------------*/
320int
321slip_empty()
322{
323 return slip_packet_end == 0;
324}
325/*---------------------------------------------------------------------------*/
326void
327slip_flushbuf(int fd)
328{
329 int n;
330
331 if(slip_empty()) {
332 return;
333 }
334
335 n = write(fd, slip_buf + slip_begin, slip_packet_end - slip_begin);
336
337 if(n == -1 && errno != EAGAIN) {
338 err(EXIT_FAILURE, "slip_flushbuf write failed");
339 } else if(n == -1) {
340 PROGRESS("Q"); /* Outqueue is full! */
341 } else {
342 slip_begin += n;
343 if(slip_begin == slip_packet_end) {
344 slip_packet_count--;
345 if(slip_end > slip_packet_end) {
346 memmove(slip_buf, slip_buf + slip_packet_end,
347 slip_end - slip_packet_end);
348 }
349 slip_end -= slip_packet_end;
350 slip_begin = slip_packet_end = 0;
351 if(slip_end > 0) {
352 /* Find end of next slip packet */
353 for(n = 1; n < slip_end; n++) {
354 if(slip_buf[n] == SLIP_END) {
355 slip_packet_end = n + 1;
356 break;
357 }
358 }
359 /* a delay between slip packets to avoid losing data */
360 if(send_delay > 0) {
361 timer_set(&send_delay_timer, send_delay);
362 }
363 }
364 }
365 }
366}
367/*---------------------------------------------------------------------------*/
368static void
369write_to_serial(int outfd, const uint8_t *inbuf, int len)
370{
371 const uint8_t *p = inbuf;
372 int i;
373
374 if(slip_config_verbose > 2) {
375 printf("Packet from TUN of length %d - write SLIP\n", len);
376 if(slip_config_verbose > 4) {
377#if WIRESHARK_IMPORT_FORMAT
378 printf("0000");
379 for(i = 0; i < len; i++) {
380 printf(" %02x", p[i]);
381 }
382#else
383 printf(" ");
384 for(i = 0; i < len; i++) {
385 printf("%02x", p[i]);
386 if((i & 3) == 3) {
387 printf(" ");
388 }
389 if((i & 15) == 15) {
390 printf("\n ");
391 }
392 }
393#endif
394 printf("\n");
395 }
396 }
397
398 /* It would be ``nice'' to send a SLIP_END here but it's not
399 * really necessary.
400 */
401 /* slip_send(outfd, SLIP_END); */
402
403 for(i = 0; i < len; i++) {
404 switch(p[i]) {
405 case SLIP_END:
406 slip_send(outfd, SLIP_ESC);
407 slip_send(outfd, SLIP_ESC_END);
408 break;
409 case SLIP_ESC:
410 slip_send(outfd, SLIP_ESC);
411 slip_send(outfd, SLIP_ESC_ESC);
412 break;
413 default:
414 slip_send(outfd, p[i]);
415 break;
416 }
417 }
418 slip_send(outfd, SLIP_END);
419 PROGRESS("t");
420}
421/*---------------------------------------------------------------------------*/
422/* writes an 802.15.4 packet to slip-radio */
423void
424write_to_slip(const uint8_t *buf, int len)
425{
426 if(slipfd > 0) {
427 write_to_serial(slipfd, buf, len);
428 }
429}
430/*---------------------------------------------------------------------------*/
431static void
432stty_telos(int fd)
433{
434 struct termios tty;
435 speed_t speed = slip_config_b_rate;
436 int i;
437
438 if(tcflush(fd, TCIOFLUSH) == -1) {
439 err(EXIT_FAILURE, "tcflush");
440 }
441
442 if(tcgetattr(fd, &tty) == -1) {
443 err(1, "tcgetattr");
444 }
445
446 cfmakeraw(&tty);
447
448 /* Nonblocking read. */
449 tty.c_cc[VTIME] = 0;
450 tty.c_cc[VMIN] = 0;
451 if(slip_config_flowcontrol) {
452 tty.c_cflag |= CRTSCTS;
453 } else {
454 tty.c_cflag &= ~CRTSCTS;
455 }
456 tty.c_cflag &= ~HUPCL;
457 tty.c_cflag &= ~CLOCAL;
458
459 cfsetispeed(&tty, speed);
460 cfsetospeed(&tty, speed);
461
462 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
463 err(EXIT_FAILURE, "tcsetattr");
464 }
465
466#if 1
467 /* Nonblocking read and write. */
468 /* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(EXIT_FAILURE, "fcntl"); */
469
470 tty.c_cflag |= CLOCAL;
471 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) {
472 err(EXIT_FAILURE, "tcsetattr");
473 }
474
475 i = TIOCM_DTR;
476 if(ioctl(fd, TIOCMBIS, &i) == -1) {
477 err(EXIT_FAILURE, "ioctl");
478 }
479#endif
480
481 usleep(10 * 1000); /* Wait for hardware 10ms. */
482
483 /* Flush input and output buffers. */
484 if(tcflush(fd, TCIOFLUSH) == -1) {
485 err(EXIT_FAILURE, "tcflush");
486 }
487}
488/*---------------------------------------------------------------------------*/
489static int
490set_fd(fd_set *rset, fd_set *wset)
491{
492 /* Anything to flush? */
493 if(!slip_empty() && (send_delay == 0 || timer_expired(&send_delay_timer))) {
494 FD_SET(slipfd, wset);
495 }
496
497 FD_SET(slipfd, rset); /* Read from slip ASAP! */
498 return 1;
499}
500/*---------------------------------------------------------------------------*/
501static void
502handle_fd(fd_set *rset, fd_set *wset)
503{
504 if(FD_ISSET(slipfd, rset)) {
505 serial_input(inslip);
506 }
507
508 if(FD_ISSET(slipfd, wset)) {
509 slip_flushbuf(slipfd);
510 }
511}
512/*---------------------------------------------------------------------------*/
513static const struct select_callback slip_callback = { set_fd, handle_fd };
514/*---------------------------------------------------------------------------*/
515void
516slip_init(void)
517{
518 setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
519
520 if(slip_config_host != NULL) {
521 if(slip_config_port == NULL) {
522 slip_config_port = "60001";
523 }
524 slipfd = connect_to_server(slip_config_host, slip_config_port);
525 if(slipfd == -1) {
526 err(EXIT_FAILURE, "can't connect to ``%s:%s''", slip_config_host,
527 slip_config_port);
528 }
529 } else if(slip_config_siodev != NULL) {
530 if(strcmp(slip_config_siodev, "null") == 0) {
531 /* Disable slip */
532 return;
533 }
534 slipfd = open(slip_config_siodev, O_RDWR | O_NONBLOCK);
535 if(slipfd == -1) {
536 err(EXIT_FAILURE, "can't open siodev ``%s''", slip_config_siodev);
537 }
538 } else {
539 static const char *siodevs[] = {
540 "/dev/ttyUSB0", "/dev/cuaU0", "/dev/ucom0" /* linux, fbsd6, fbsd5 */
541 };
542 for(int i = 0; i < 3; i++) {
543 slip_config_siodev = siodevs[i];
544 slipfd = open(slip_config_siodev, O_RDWR | O_NONBLOCK);
545 if(slipfd != -1) {
546 break;
547 }
548 }
549 if(slipfd == -1) {
550 err(EXIT_FAILURE, "can't open siodev");
551 }
552 }
553
554 select_set_callback(slipfd, &slip_callback);
555
556 if(slip_config_host != NULL) {
557 fprintf(stderr, "********SLIP opened to ``%s:%s''\n", slip_config_host,
558 slip_config_port);
559 } else {
560 fprintf(stderr, "********SLIP started on ``/dev/%s''\n", slip_config_siodev);
561 stty_telos(slipfd);
562 }
563
564 timer_set(&send_delay_timer, 0);
565 slip_send(slipfd, SLIP_END);
566 inslip = fdopen(slipfd, "r");
567 if(inslip == NULL) {
568 err(EXIT_FAILURE, "slip_init: fdopen");
569 }
570}
571/*---------------------------------------------------------------------------*/
CBOR-over-SLIP protocol for talking to the serialradio firmware (examples/serialradio) from the nativ...
void border_router_cbor_input(const uint8_t *data, int len)
Parse and dispatch a complete CBOR frame (including trailing CRC16) received from the serial radio.
Sets up some commands for the border router.
Border router header file.
Simple command handler.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition packetbuf.c:84
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition timer.c:64
bool timer_expired(struct timer *t)
Check if a timer has expired.
Definition timer.c:123
Include file for the Contiki low-layer network stack (NETSTACK).
Header file for the Packet buffer (packetbuf) management.
A timer.
Definition timer.h:84