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 <net/if.h>
47#include <sys/ioctl.h>
48#include <err.h>
49#include "contiki.h"
50#include "sys/platform.h"
51#include "tun6-net.h"
52
53int slip_config_verbose = 0;
54int slip_config_flowcontrol = 0;
55int slip_config_timestamp = 0;
56const char *slip_config_siodev = NULL;
57const char *slip_config_host = NULL;
58const char *slip_config_port = NULL;
59uint16_t slip_config_basedelay = 0;
60
61#ifndef BAUDRATE
62#define BAUDRATE B115200
63#endif
64speed_t slip_config_b_rate = BAUDRATE;
65
66#define BAUDRATE_PRIO CONTIKI_VERBOSE_PRIO + 20
67
68CONTIKI_USAGE(300, " [ipaddress]\n"
69 "example parameters: -L -v=2 -s /dev/ttyUSB1 fd00::1/64\n\n");
71 "\nVerbosity level:\n"
72 " 0 No messages\n"
73 " 1 Encapsulated SLIP debug messages (default)\n"
74 " 2 Printable strings after they are received\n"
75 " 3 Printable strings and SLIP packet notifications\n"
76 " 4 All printable characters as they are received\n"
77 " 5 All SLIP packets in hex\n");
78/*---------------------------------------------------------------------------*/
79static int
80baudrate_callback(const char *optarg)
81{
82 int baudrate = atoi(optarg);
83 switch(baudrate) {
84 case -2:
85 break; /* Use default. */
86 case 9600:
87 slip_config_b_rate = B9600;
88 break;
89 case 19200:
90 slip_config_b_rate = B19200;
91 break;
92 case 38400:
93 slip_config_b_rate = B38400;
94 break;
95 case 57600:
96 slip_config_b_rate = B57600;
97 break;
98 case 115200:
99 slip_config_b_rate = B115200;
100 break;
101#ifdef linux
102 case 921600:
103 slip_config_b_rate = B921600;
104 break;
105#endif
106 default:
107 fprintf(stderr, "unknown baudrate %s", optarg);
108 return 1;
109 }
110 return 0;
111}
112CONTIKI_OPTION(BAUDRATE_PRIO, { "B", required_argument, NULL, 0 },
113 baudrate_callback,
114#ifdef linux
115 "baudrate (9600,19200,38400,57600,115200,921600)"
116#else
117 "baudrate (9600,19200,38400,57600,115200)"
118#endif
119 " (default 115200)\n");
120CONTIKI_OPTION(BAUDRATE_PRIO + 1,
121 { "H", no_argument, &slip_config_flowcontrol, 1 }, NULL,
122 "hardware CTS/RTS flow control (default disabled)\n");
123CONTIKI_OPTION(BAUDRATE_PRIO + 2,
124 { "L", no_argument, &slip_config_timestamp, 1 }, NULL,
125 "log output format (adds time stamps)\n");
126static int
127device_callback(const char *optarg)
128{
129 slip_config_siodev = optarg;
130 return 0;
131}
132CONTIKI_OPTION(BAUDRATE_PRIO + 3, { "s", required_argument, NULL, 0 },
133 device_callback, "serial device\n");
134static int
135host_callback(const char *optarg)
136{
137 slip_config_host = optarg;
138 return 0;
139}
140CONTIKI_OPTION(BAUDRATE_PRIO + 4, { "a", required_argument, NULL, 0 },
141 host_callback, "connect via TCP to server at <value>\n");
142static int
143port_callback(const char *optarg)
144{
145 slip_config_port = optarg;
146 return 0;
147}
148CONTIKI_OPTION(BAUDRATE_PRIO + 5, { "p", required_argument, NULL, 0 },
149 port_callback, "connect via TCP to server on port <value>\n");
150static int
151delay_callback(const char *optarg)
152{
153 slip_config_basedelay = optarg ? atoi(optarg) : 10;
154 if(slip_config_basedelay < 0 ||
155 (slip_config_basedelay == 0 && optarg && optarg[0] != '0')) {
156 fprintf(stderr, "Delay '%s' could not be parsed as a number\n", optarg);
157 return 1;
158 }
159
160 return 0;
161}
162CONTIKI_OPTION(BAUDRATE_PRIO + 6, { "d", optional_argument, NULL, 0 },
163 delay_callback,
164 "minimum delay between outgoing SLIP packets (default 10)\n"
165 "\t\tActual delay is basedelay * (#6LowPAN fragments)"
166 " milliseconds.\n");
167/*---------------------------------------------------------------------------*/
168int
169slip_config_handle_arguments(int argc, char **argv)
170{
171 /* For backward compatiblity: assume subnet prefix if exactly one argument
172 been specified. */
173 if(argc == 2) {
174 tun6_net_set_prefix(argv[1]);
175 }
176 return 1;
177}
178/*---------------------------------------------------------------------------*/
179static int
180verbose_callback(const char *optarg)
181{
182 slip_config_verbose = optarg ? atoi(optarg) : 3;
183 if(slip_config_verbose < 0 || slip_config_verbose > 5 ||
184 (slip_config_verbose == 0 && optarg && optarg[0] != '0')) {
185 fprintf(stderr, "Verbose level '%s' not between 0 and 5\n", optarg);
186 return 1;
187 }
188 return 0;
189}
190CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO, { "v", optional_argument, NULL, 0 },
191 verbose_callback, "verbosity level (0-5)\n");
192/*---------------------------------------------------------------------------*/
193/* Hidden compatibility options with legacy parameter names. */
194CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 1,
195 { "v0", no_argument, &slip_config_verbose, 0 }, NULL, NULL);
196CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 2,
197 { "v1", no_argument, &slip_config_verbose, 1 }, NULL, NULL);
198CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 3,
199 { "v2", no_argument, &slip_config_verbose, 2 }, NULL, NULL);
200CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 4,
201 { "v3", no_argument, &slip_config_verbose, 3 }, NULL, NULL);
202CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 5,
203 { "v4", no_argument, &slip_config_verbose, 4 }, NULL, NULL);
204CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 6,
205 { "v5", no_argument, &slip_config_verbose, 5 }, NULL, NULL);
#define CONTIKI_EXTRA_HELP(prio, msg)
Set extra usage help shown at the end of –help.
Definition platform.h:189
#define CONTIKI_OPTION(prio,...)
Add a command line option when the compilation unit is present.
Definition platform.h:153
#define CONTIKI_USAGE(prio, msg)
Set the usage string shown for –help.
Definition platform.h:171
Header file for the Contiki-NG main routine.