Removed some warnings.
[contiki-2.x/lpc17xxx.git] / tools / stm32w / serialdump.c
blob40c6cf7de73f884443da30ba768c7834edef19f9
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <termios.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <time.h>
10 #define BAUDRATE B57600
11 #define BAUDRATE_S "57600"
12 #ifdef linux
13 #define MODEMDEVICE "/dev/ttyS0"
14 #else
15 #define MODEMDEVICE "/dev/com1"
16 #endif /* linux */
18 #define DEFAULT_DELAY 10000
20 #define SLIP_END 0300
21 #define SLIP_ESC 0333
22 #define SLIP_ESC_END 0334
23 #define SLIP_ESC_ESC 0335
25 #define CSNA_INIT 0x01
27 #define BUFSIZE 40
28 #define HCOLS 20
29 #define ICOLS 18
31 #define MODE_START_DATE 0
32 #define MODE_DATE 1
33 #define MODE_START_TEXT 2
34 #define MODE_TEXT 3
35 #define MODE_INT 4
36 #define MODE_HEX 5
37 #define MODE_SLIP_AUTO 6
38 #define MODE_SLIP 7
39 #define MODE_SLIP_HIDE 8
41 static unsigned char rxbuf[2048];
43 static int
44 usage(int result)
46 printf("Usage: serialdump [-x] [-s[on]] [-i] [-dDELAY] [-bSPEED] [SERIALDEVICE]\n");
47 printf(" -x for hexadecimal output\n");
48 printf(" -i for decimal output\n");
49 printf(" -s for automatic SLIP mode\n");
50 printf(" -so for SLIP only mode (all data is SLIP packets)\n");
51 printf(" -sn to hide SLIP packages\n");
52 printf(" -T[format] to add time for each text line\n");
53 printf(" -dDELAY delay in us between 2 consecutive writes (must be different from 0)\n");
54 printf(" (see man page for strftime() for format description)\n");
55 return result;
58 static void
59 print_hex_line(unsigned char *prefix, unsigned char *outbuf, int index)
61 int i;
63 printf("\r%s", prefix);
64 for(i = 0; i < index; i++) {
65 if((i % 4) == 0) {
66 printf(" ");
68 printf("%02X", outbuf[i] & 0xFF);
70 printf(" ");
71 for(i = index; i < HCOLS; i++) {
72 if((i % 4) == 0) {
73 printf(" ");
75 printf(" ");
77 for(i = 0; i < index; i++) {
78 if(outbuf[i] < 30 || outbuf[i] > 126) {
79 printf(".");
80 } else {
81 printf("%c", outbuf[i]);
86 int main(int argc, char **argv)
88 struct termios options;
89 fd_set mask, smask;
90 int fd;
91 speed_t speed = BAUDRATE;
92 char *speedname = BAUDRATE_S;
93 char *device = MODEMDEVICE;
94 char *timeformat = NULL;
95 unsigned char buf[BUFSIZE], outbuf[HCOLS];
96 unsigned char mode = MODE_START_TEXT;
97 int nfound, flags = 0;
98 unsigned char lastc = '\0';
99 int delay = DEFAULT_DELAY;
101 int index = 1;
102 while (index < argc) {
103 if (argv[index][0] == '-') {
104 switch(argv[index][1]) {
105 case 'b':
106 /* set speed */
107 if (strcmp(&argv[index][2], "38400") == 0) {
108 speed = B38400;
109 speedname = "38400";
110 } else if (strcmp(&argv[index][2], "19200") == 0) {
111 speed = B19200;
112 speedname = "19200";
113 } else if (strcmp(&argv[index][2], "57600") == 0) {
114 speed = B57600;
115 speedname = "57600";
116 } else if (strcmp(&argv[index][2], "115200") == 0) {
117 speed = B115200;
118 speedname = "115200";
119 } else {
120 fprintf(stderr, "unsupported speed: %s\n", &argv[index][2]);
121 return usage(1);
123 break;
124 case 'x':
125 mode = MODE_HEX;
126 break;
127 case 'i':
128 mode = MODE_INT;
129 break;
130 case 's':
131 switch(argv[index][2]) {
132 case 'n':
133 mode = MODE_SLIP_HIDE;
134 break;
135 case 'o':
136 mode = MODE_SLIP;
137 break;
138 default:
139 mode = MODE_SLIP_AUTO;
140 break;
142 break;
143 case 'T':
144 if(strlen(&argv[index][2]) == 0) {
145 timeformat = "%Y-%m-%d %H:%M:%S";
146 } else {
147 timeformat = &argv[index][2];
149 mode = MODE_START_DATE;
150 break;
151 case 'd':
152 delay = atoi(&argv[index][2]);
153 if(delay == 0){
154 return usage(1);
156 break;
157 case 'h':
158 return usage(0);
159 default:
160 fprintf(stderr, "unknown option '%c'\n", argv[index][1]);
161 return usage(1);
163 index++;
164 } else {
165 device = argv[index++];
166 if (index < argc) {
167 fprintf(stderr, "too many arguments\n");
168 return usage(1);
172 fprintf(stderr, "connecting to %s (%s)", device, speedname);
174 fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC );
175 if (fd <0) {
176 fprintf(stderr, "\n");
177 perror(device);
178 exit(-1);
180 fprintf(stderr, " [OK]\n");
182 if (fcntl(fd, F_SETFL, 0) < 0) {
183 perror("could not set fcntl");
184 exit(-1);
187 if (tcgetattr(fd, &options) < 0) {
188 perror("could not get options");
189 exit(-1);
191 /* fprintf(stderr, "serial options set\n"); */
192 cfsetispeed(&options, speed);
193 cfsetospeed(&options, speed);
194 /* Enable the receiver and set local mode */
195 options.c_cflag |= (CLOCAL | CREAD);
196 /* Mask the character size bits and turn off (odd) parity */
197 options.c_cflag &= ~(CSIZE|PARENB|PARODD);
198 /* Select 8 data bits */
199 options.c_cflag |= CS8;
201 /* Raw input */
202 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
203 /* Raw output */
204 options.c_oflag &= ~OPOST;
206 if (tcsetattr(fd, TCSANOW, &options) < 0) {
207 perror("could not set options");
208 exit(-1);
211 /* Make read() return immediately */
212 /* if (fcntl(fd, F_SETFL, FNDELAY) < 0) { */
213 /* perror("\ncould not set fcntl"); */
214 /* exit(-1); */
215 /* } */
217 FD_ZERO(&mask);
218 FD_SET(fd, &mask);
219 FD_SET(fileno(stdin), &mask);
221 index = 0;
222 for (;;) {
223 smask = mask;
224 nfound = select(FD_SETSIZE, &smask, (fd_set *) 0, (fd_set *) 0,
225 (struct timeval *) 0);
226 if(nfound < 0) {
227 if (errno == EINTR) {
228 fprintf(stderr, "interrupted system call\n");
229 continue;
231 /* something is very wrong! */
232 perror("select");
233 exit(1);
236 if(FD_ISSET(fileno(stdin), &smask)) {
237 /* data from standard in */
238 int n = read(fileno(stdin), buf, sizeof(buf));
239 if (n < 0) {
240 perror("could not read");
241 exit(-1);
242 } else if (n > 0) {
243 /* because commands might need parameters, lines needs to be
244 separated which means the terminating LF must be sent */
245 /* while(n > 0 && buf[n - 1] < 32) { */
246 /* n--; */
247 /* } */
248 if(n > 0) {
249 int i;
250 /* fprintf(stderr, "SEND %d bytes\n", n);*/
251 /* write slowly */
252 for (i = 0; i < n; i++) {
253 if (write(fd, &buf[i], 1) <= 0) {
254 perror("write");
255 exit(1);
256 } else {
257 fflush(NULL);
258 usleep(delay);
262 } else {
263 /* End of input, exit. */
264 exit(0);
268 if(FD_ISSET(fd, &smask)) {
269 int i, j, n = read(fd, buf, sizeof(buf));
270 if (n < 0) {
271 perror("could not read");
272 exit(-1);
275 for(i = 0; i < n; i++) {
276 switch(mode) {
277 case MODE_START_TEXT:
278 case MODE_TEXT:
279 printf("%c", buf[i]);
280 break;
281 case MODE_START_DATE: {
282 time_t t;
283 t = time(&t);
284 strftime(outbuf, HCOLS, timeformat, localtime(&t));
285 printf("%s|", outbuf);
286 mode = MODE_DATE;
288 /* continue into the MODE_DATE */
289 case MODE_DATE:
290 printf("%c", buf[i]);
291 if(buf[i] == '\n') {
292 mode = MODE_START_DATE;
294 break;
295 case MODE_INT:
296 printf("%03d ", buf[i]);
297 if(++index >= ICOLS) {
298 index = 0;
299 printf("\n");
301 break;
302 case MODE_HEX:
303 rxbuf[index++] = buf[i];
304 if(index >= HCOLS) {
305 print_hex_line("", rxbuf, index);
306 index = 0;
307 printf("\n");
309 break;
311 case MODE_SLIP_AUTO:
312 case MODE_SLIP_HIDE:
313 if(!flags && (buf[i] != SLIP_END)) {
314 /* Not a SLIP packet? */
315 printf("%c", buf[i]);
316 break;
318 /* continue to slip only mode */
319 case MODE_SLIP:
320 switch(buf[i]) {
321 case SLIP_ESC:
322 lastc = SLIP_ESC;
323 break;
325 case SLIP_END:
326 if(index > 0) {
327 if(flags != 2 && mode != MODE_SLIP_HIDE) {
328 /* not overflowed: show packet */
329 print_hex_line("SLIP: ", rxbuf,
330 index > HCOLS ? HCOLS : index);
331 printf("\n");
333 lastc = '\0';
334 index = 0;
335 flags = 0;
336 } else {
337 flags = !flags;
339 break;
341 default:
342 if(lastc == SLIP_ESC) {
343 lastc = '\0';
345 /* Previous read byte was an escape byte, so this byte will be
346 interpreted differently from others. */
347 switch(buf[i]) {
348 case SLIP_ESC_END:
349 buf[i] = SLIP_END;
350 break;
351 case SLIP_ESC_ESC:
352 buf[i] = SLIP_ESC;
353 break;
357 rxbuf[index++] = buf[i];
358 if(index >= sizeof(rxbuf)) {
359 fprintf(stderr, "**** slip overflow\n");
360 index = 0;
361 flags = 2;
363 break;
365 break;
369 /* after processing for some output modes */
370 if(index > 0) {
371 switch(mode) {
372 case MODE_HEX:
373 print_hex_line("", rxbuf, index);
374 break;
377 fflush(stdout);