2006-08-02 14:41:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2007-01-23 12:33:27 +00:00
|
|
|
* $Id: scat.c,v 1.4 2007/01/23 12:33:27 bg- Exp $
|
2006-08-02 14:41:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-12-27 14:19:22 +00:00
|
|
|
#include <ctype.h>
|
2006-08-02 14:41:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#define SLIP_END 0300
|
|
|
|
#define SLIP_ESC 0333
|
|
|
|
#define SLIP_ESC_END 0334
|
|
|
|
#define SLIP_ESC_ESC 0335
|
|
|
|
|
2007-01-23 12:33:27 +00:00
|
|
|
#ifndef BAUDRATE
|
2006-12-27 14:19:22 +00:00
|
|
|
#define BAUDRATE B115200
|
|
|
|
//#define BAUDRATE B57600
|
2006-08-02 14:41:30 +00:00
|
|
|
//#define BAUDRATE B38400
|
|
|
|
//#define BAUDRATE B19200
|
2007-01-23 12:33:27 +00:00
|
|
|
#endif
|
2006-08-02 14:41:30 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
stty_telos(int fd)
|
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
speed_t speed = BAUDRATE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
|
|
|
|
|
|
|
|
if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr");
|
|
|
|
|
|
|
|
cfmakeraw(&tty);
|
|
|
|
|
2006-12-27 14:19:22 +00:00
|
|
|
/* Blocking read. */
|
2006-08-02 14:41:30 +00:00
|
|
|
tty.c_cc[VTIME] = 0;
|
2006-12-27 14:19:22 +00:00
|
|
|
tty.c_cc[VMIN] = 1;
|
2006-08-02 14:41:30 +00:00
|
|
|
tty.c_cflag &= ~CRTSCTS;
|
|
|
|
tty.c_cflag &= ~HUPCL;
|
|
|
|
tty.c_cflag &= ~CLOCAL;
|
|
|
|
|
|
|
|
cfsetispeed(&tty, speed);
|
|
|
|
cfsetospeed(&tty, speed);
|
|
|
|
|
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
|
|
|
|
|
|
|
|
tty.c_cflag |= CLOCAL;
|
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
|
|
|
|
|
|
|
|
i = TIOCM_DTR;
|
|
|
|
if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl");
|
|
|
|
|
|
|
|
usleep(10*1000); /* Wait for hardware 10ms. */
|
|
|
|
|
|
|
|
/* Flush input and output buffers. */
|
|
|
|
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int slipfd;
|
|
|
|
FILE *inslip;
|
|
|
|
const char *siodev;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
err(1, "usage: scat device-file");
|
|
|
|
siodev = argv[1];
|
|
|
|
|
2006-12-27 14:19:22 +00:00
|
|
|
slipfd = open(siodev, O_RDWR);
|
2006-08-02 14:41:30 +00:00
|
|
|
if (slipfd == -1) err(1, "can't open '%s'", siodev);
|
|
|
|
stty_telos(slipfd);
|
|
|
|
inslip = fdopen(slipfd, "r");
|
|
|
|
if(inslip == NULL) err(1, "main: fdopen");
|
|
|
|
|
2006-12-27 14:19:22 +00:00
|
|
|
while (1) {
|
|
|
|
int c = getc(inslip);
|
|
|
|
while (c == SLIP_END)
|
|
|
|
c = getc(inslip);
|
|
|
|
do {
|
|
|
|
if (c == SLIP_ESC) {
|
|
|
|
c = getc(inslip);
|
|
|
|
if (c == SLIP_ESC_ESC)
|
|
|
|
c = SLIP_ESC;
|
|
|
|
else if (c == SLIP_ESC_END)
|
|
|
|
c = SLIP_END;
|
|
|
|
}
|
2007-01-05 18:05:21 +00:00
|
|
|
switch (c) {
|
|
|
|
case EOF:
|
|
|
|
err(1, "getc(inslip)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\007':
|
|
|
|
case '\b':
|
|
|
|
case '\f':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\v':
|
2006-12-27 14:19:22 +00:00
|
|
|
putchar(c);
|
2007-01-05 18:05:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (isprint(c))
|
|
|
|
putchar(c);
|
|
|
|
else
|
|
|
|
printf("%02x ", c);
|
|
|
|
break;
|
|
|
|
}
|
2006-12-27 14:19:22 +00:00
|
|
|
c = getc(inslip);
|
|
|
|
} while (c != SLIP_END);
|
|
|
|
}
|
2006-08-02 14:41:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|