10 #define BAUDRATE B57600
11 #define BAUDRATE_S "57600"
13 #define MODEMDEVICE "/dev/ttyS0"
15 #define MODEMDEVICE "/dev/com1"
18 #define DEFAULT_DELAY 10000
22 #define SLIP_ESC_END 0334
23 #define SLIP_ESC_ESC 0335
25 #define CSNA_INIT 0x01
31 #define MODE_START_DATE 0
33 #define MODE_START_TEXT 2
37 #define MODE_SLIP_AUTO 6
39 #define MODE_SLIP_HIDE 8
41 static unsigned char rxbuf
[2048];
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");
59 print_hex_line(unsigned char *prefix
, unsigned char *outbuf
, int index
)
63 printf("\r%s", prefix
);
64 for(i
= 0; i
< index
; i
++) {
68 printf("%02X", outbuf
[i
] & 0xFF);
71 for(i
= index
; i
< HCOLS
; i
++) {
77 for(i
= 0; i
< index
; i
++) {
78 if(outbuf
[i
] < 30 || outbuf
[i
] > 126) {
81 printf("%c", outbuf
[i
]);
86 int main(int argc
, char **argv
)
88 struct termios options
;
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
;
102 while (index
< argc
) {
103 if (argv
[index
][0] == '-') {
104 switch(argv
[index
][1]) {
107 if (strcmp(&argv
[index
][2], "38400") == 0) {
110 } else if (strcmp(&argv
[index
][2], "19200") == 0) {
113 } else if (strcmp(&argv
[index
][2], "57600") == 0) {
116 } else if (strcmp(&argv
[index
][2], "115200") == 0) {
118 speedname
= "115200";
120 fprintf(stderr
, "unsupported speed: %s\n", &argv
[index
][2]);
131 switch(argv
[index
][2]) {
133 mode
= MODE_SLIP_HIDE
;
139 mode
= MODE_SLIP_AUTO
;
144 if(strlen(&argv
[index
][2]) == 0) {
145 timeformat
= "%Y-%m-%d %H:%M:%S";
147 timeformat
= &argv
[index
][2];
149 mode
= MODE_START_DATE
;
152 delay
= atoi(&argv
[index
][2]);
160 fprintf(stderr
, "unknown option '%c'\n", argv
[index
][1]);
165 device
= argv
[index
++];
167 fprintf(stderr
, "too many arguments\n");
172 fprintf(stderr
, "connecting to %s (%s)", device
, speedname
);
174 fd
= open(device
, O_RDWR
| O_NOCTTY
| O_NDELAY
| O_SYNC
);
176 fprintf(stderr
, "\n");
180 fprintf(stderr
, " [OK]\n");
182 if (fcntl(fd
, F_SETFL
, 0) < 0) {
183 perror("could not set fcntl");
187 if (tcgetattr(fd
, &options
) < 0) {
188 perror("could not get options");
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
;
202 options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
);
204 options
.c_oflag
&= ~OPOST
;
206 if (tcsetattr(fd
, TCSANOW
, &options
) < 0) {
207 perror("could not set options");
211 /* Make read() return immediately */
212 /* if (fcntl(fd, F_SETFL, FNDELAY) < 0) { */
213 /* perror("\ncould not set fcntl"); */
219 FD_SET(fileno(stdin
), &mask
);
224 nfound
= select(FD_SETSIZE
, &smask
, (fd_set
*) 0, (fd_set
*) 0,
225 (struct timeval
*) 0);
227 if (errno
== EINTR
) {
228 fprintf(stderr
, "interrupted system call\n");
231 /* something is very wrong! */
236 if(FD_ISSET(fileno(stdin
), &smask
)) {
237 /* data from standard in */
238 int n
= read(fileno(stdin
), buf
, sizeof(buf
));
240 perror("could not read");
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) { */
250 /* fprintf(stderr, "SEND %d bytes\n", n);*/
252 for (i
= 0; i
< n
; i
++) {
253 if (write(fd
, &buf
[i
], 1) <= 0) {
263 /* End of input, exit. */
268 if(FD_ISSET(fd
, &smask
)) {
269 int i
, j
, n
= read(fd
, buf
, sizeof(buf
));
271 perror("could not read");
275 for(i
= 0; i
< n
; i
++) {
277 case MODE_START_TEXT
:
279 printf("%c", buf
[i
]);
281 case MODE_START_DATE
: {
284 strftime(outbuf
, HCOLS
, timeformat
, localtime(&t
));
285 printf("%s|", outbuf
);
288 /* continue into the MODE_DATE */
290 printf("%c", buf
[i
]);
292 mode
= MODE_START_DATE
;
296 printf("%03d ", buf
[i
]);
297 if(++index
>= ICOLS
) {
303 rxbuf
[index
++] = buf
[i
];
305 print_hex_line("", rxbuf
, index
);
313 if(!flags
&& (buf
[i
] != SLIP_END
)) {
314 /* Not a SLIP packet? */
315 printf("%c", buf
[i
]);
318 /* continue to slip only mode */
327 if(flags
!= 2 && mode
!= MODE_SLIP_HIDE
) {
328 /* not overflowed: show packet */
329 print_hex_line("SLIP: ", rxbuf
,
330 index
> HCOLS
? HCOLS
: index
);
342 if(lastc
== SLIP_ESC
) {
345 /* Previous read byte was an escape byte, so this byte will be
346 interpreted differently from others. */
357 rxbuf
[index
++] = buf
[i
];
358 if(index
>= sizeof(rxbuf
)) {
359 fprintf(stderr
, "**** slip overflow\n");
369 /* after processing for some output modes */
373 print_hex_line("", rxbuf
, index
);