Contiki-NG
Loading...
Searching...
No Matches
slip-config.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \file
32 * Slip configuration
33 * \author
34 * Niclas Finne <nfi@sics.se>
35 * Joakim Eriksson <joakime@sics.se>
36 */
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdarg.h>
40#include <string.h>
41#include <unistd.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <signal.h>
45#include <termios.h>
46#include <sys/ioctl.h>
47#include <err.h>
48#include "contiki.h"
49
50int slip_config_verbose = 0;
51const char *slip_config_ipaddr;
52int slip_config_flowcontrol = 0;
53int slip_config_timestamp = 0;
54const char *slip_config_siodev = NULL;
55const char *slip_config_host = NULL;
56const char *slip_config_port = NULL;
57char slip_config_tundev[32] = { "" };
58uint16_t slip_config_basedelay = 0;
59
60#ifndef BAUDRATE
61#define BAUDRATE B115200
62#endif
63speed_t slip_config_b_rate = BAUDRATE;
64
65/*---------------------------------------------------------------------------*/
66int
67slip_config_handle_arguments(int argc, char **argv)
68{
69 const char *prog;
70 int c;
71 int baudrate = 115200;
72
73 slip_config_verbose = 0;
74
75 prog = argv[0];
76 while((c = getopt(argc, argv, "B:H:D:Lhs:t:v::d::a:p:T")) != -1) {
77 switch(c) {
78 case 'B':
79 baudrate = atoi(optarg);
80 break;
81
82 case 'H':
83 slip_config_flowcontrol = 1;
84 break;
85
86 case 'L':
87 slip_config_timestamp = 1;
88 break;
89
90 case 's':
91 if(strncmp("/dev/", optarg, 5) == 0) {
92 slip_config_siodev = optarg + 5;
93 } else {
94 slip_config_siodev = optarg;
95 }
96 break;
97
98 case 't':
99 if(strncmp("/dev/", optarg, 5) == 0) {
100 strncpy(slip_config_tundev, optarg + 5, sizeof(slip_config_tundev) - 1);
101 } else {
102 strncpy(slip_config_tundev, optarg, sizeof(slip_config_tundev) - 1);
103 }
104 slip_config_tundev[sizeof(slip_config_tundev) - 1] = '\0';
105 break;
106
107 case 'a':
108 slip_config_host = optarg;
109 break;
110
111 case 'p':
112 slip_config_port = optarg;
113 break;
114
115 case 'd':
116 slip_config_basedelay = 10;
117 if(optarg) {
118 slip_config_basedelay = atoi(optarg);
119 }
120 break;
121
122 case 'v':
123 slip_config_verbose = 2;
124 if(optarg) {
125 slip_config_verbose = atoi(optarg);
126 }
127 break;
128
129 case '?':
130 case 'h':
131 default:
132 fprintf(stderr, "usage: %s [options] ipaddress\n", prog);
133 fprintf(stderr, "example: border-router.native -L -v2 -s ttyUSB1 fd00::1/64\n");
134 fprintf(stderr, "Options are:\n");
135#ifdef linux
136 fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200,921600 (default 115200)\n");
137#else
138 fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200 (default 115200)\n");
139#endif
140 fprintf(stderr, " -H Hardware CTS/RTS flow control (default disabled)\n");
141 fprintf(stderr, " -L Log output format (adds time stamps)\n");
142 fprintf(stderr, " -s siodev Serial device (default /dev/ttyUSB0)\n");
143 fprintf(stderr, " -a host Connect via TCP to server at <host>\n");
144 fprintf(stderr, " -p port Connect via TCP to server at <host>:<port>\n");
145 fprintf(stderr, " -t tundev Name of interface (default tun0)\n");
146#ifdef __APPLE__
147 fprintf(stderr, " -v level Verbosity level\n");
148#else
149 fprintf(stderr, " -v[level] Verbosity level\n");
150#endif
151 fprintf(stderr, " -v0 No messages\n");
152 fprintf(stderr, " -v1 Encapsulated SLIP debug messages (default)\n");
153 fprintf(stderr, " -v2 Printable strings after they are received\n");
154 fprintf(stderr, " -v3 Printable strings and SLIP packet notifications\n");
155 fprintf(stderr, " -v4 All printable characters as they are received\n");
156 fprintf(stderr, " -v5 All SLIP packets in hex\n");
157#ifndef __APPLE__
158 fprintf(stderr, " -v Equivalent to -v3\n");
159#endif
160 fprintf(stderr, " -d[basedelay] Minimum delay between outgoing SLIP packets.\n");
161 fprintf(stderr, " Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n");
162 fprintf(stderr, " -d is equivalent to -d10.\n");
163 exit(1);
164 break;
165 }
166 }
167 argc -= optind - 1;
168 argv += optind - 1;
169
170 if(argc != 2 && argc != 3) {
171 err(1, "usage: %s [-B baudrate] [-H] [-L] [-s siodev] [-t tundev] [-T] [-v verbosity] [-d delay] [-a serveraddress] [-p serverport] ipaddress", prog);
172 }
173 slip_config_ipaddr = argv[1];
174
175 switch(baudrate) {
176 case -2:
177 break; /* Use default. */
178 case 9600:
179 slip_config_b_rate = B9600;
180 break;
181 case 19200:
182 slip_config_b_rate = B19200;
183 break;
184 case 38400:
185 slip_config_b_rate = B38400;
186 break;
187 case 57600:
188 slip_config_b_rate = B57600;
189 break;
190 case 115200:
191 slip_config_b_rate = B115200;
192 break;
193#ifdef linux
194 case 921600:
195 slip_config_b_rate = B921600;
196 break;
197#endif
198 default:
199 err(1, "unknown baudrate %d", baudrate);
200 break;
201 }
202
203 if(*slip_config_tundev == '\0') {
204 /* Use default. */
205 strcpy(slip_config_tundev, "tun0");
206 }
207 return 1;
208}
209/*---------------------------------------------------------------------------*/