Contiki-NG
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 
50 int slip_config_verbose = 0;
51 const char *slip_config_ipaddr;
52 int slip_config_flowcontrol = 0;
53 int slip_config_timestamp = 0;
54 const char *slip_config_siodev = NULL;
55 const char *slip_config_host = NULL;
56 const char *slip_config_port = NULL;
57 char slip_config_tundev[32] = { "" };
58 uint16_t slip_config_basedelay = 0;
59 
60 #ifndef BAUDRATE
61 #define BAUDRATE B115200
62 #endif
63 speed_t slip_config_b_rate = BAUDRATE;
64 
65 /*---------------------------------------------------------------------------*/
66 int
67 slip_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));
101  } else {
102  strncpy(slip_config_tundev, optarg, sizeof(slip_config_tundev));
103  }
104  break;
105 
106  case 'a':
107  slip_config_host = optarg;
108  break;
109 
110  case 'p':
111  slip_config_port = optarg;
112  break;
113 
114  case 'd':
115  slip_config_basedelay = 10;
116  if(optarg) {
117  slip_config_basedelay = atoi(optarg);
118  }
119  break;
120 
121  case 'v':
122  slip_config_verbose = 2;
123  if(optarg) {
124  slip_config_verbose = atoi(optarg);
125  }
126  break;
127 
128  case '?':
129  case 'h':
130  default:
131  fprintf(stderr, "usage: %s [options] ipaddress\n", prog);
132  fprintf(stderr, "example: border-router.native -L -v2 -s ttyUSB1 fd00::1/64\n");
133  fprintf(stderr, "Options are:\n");
134 #ifdef linux
135  fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200,921600 (default 115200)\n");
136 #else
137  fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200 (default 115200)\n");
138 #endif
139  fprintf(stderr, " -H Hardware CTS/RTS flow control (default disabled)\n");
140  fprintf(stderr, " -L Log output format (adds time stamps)\n");
141  fprintf(stderr, " -s siodev Serial device (default /dev/ttyUSB0)\n");
142  fprintf(stderr, " -a host Connect via TCP to server at <host>\n");
143  fprintf(stderr, " -p port Connect via TCP to server at <host>:<port>\n");
144  fprintf(stderr, " -t tundev Name of interface (default tun0)\n");
145 #ifdef __APPLE__
146  fprintf(stderr, " -v level Verbosity level\n");
147 #else
148  fprintf(stderr, " -v[level] Verbosity level\n");
149 #endif
150  fprintf(stderr, " -v0 No messages\n");
151  fprintf(stderr, " -v1 Encapsulated SLIP debug messages (default)\n");
152  fprintf(stderr, " -v2 Printable strings after they are received\n");
153  fprintf(stderr, " -v3 Printable strings and SLIP packet notifications\n");
154  fprintf(stderr, " -v4 All printable characters as they are received\n");
155  fprintf(stderr, " -v5 All SLIP packets in hex\n");
156 #ifndef __APPLE__
157  fprintf(stderr, " -v Equivalent to -v3\n");
158 #endif
159  fprintf(stderr, " -d[basedelay] Minimum delay between outgoing SLIP packets.\n");
160  fprintf(stderr, " Actual delay is basedelay*(#6LowPAN fragments) milliseconds.\n");
161  fprintf(stderr, " -d is equivalent to -d10.\n");
162  exit(1);
163  break;
164  }
165  }
166  argc -= optind - 1;
167  argv += optind - 1;
168 
169  if(argc != 2 && argc != 3) {
170  err(1, "usage: %s [-B baudrate] [-H] [-L] [-s siodev] [-t tundev] [-T] [-v verbosity] [-d delay] [-a serveraddress] [-p serverport] ipaddress", prog);
171  }
172  slip_config_ipaddr = argv[1];
173 
174  switch(baudrate) {
175  case -2:
176  break; /* Use default. */
177  case 9600:
178  slip_config_b_rate = B9600;
179  break;
180  case 19200:
181  slip_config_b_rate = B19200;
182  break;
183  case 38400:
184  slip_config_b_rate = B38400;
185  break;
186  case 57600:
187  slip_config_b_rate = B57600;
188  break;
189  case 115200:
190  slip_config_b_rate = B115200;
191  break;
192 #ifdef linux
193  case 921600:
194  slip_config_b_rate = B921600;
195  break;
196 #endif
197  default:
198  err(1, "unknown baudrate %d", baudrate);
199  break;
200  }
201 
202  if(*slip_config_tundev == '\0') {
203  /* Use default. */
204  strcpy(slip_config_tundev, "tun0");
205  }
206  return 1;
207 }
208 /*---------------------------------------------------------------------------*/