#if !defined(_WIN32) && !defined(_WIN64)
#define _XOPEN_SOURCE 600
#define _GNU_SOURCE
#if defined(__APPLE__)
#define _DARWIN_C_SOURCE
#include <Availability.h>
#include <AvailabilityMacros.h>
#include <TargetConditionals.h>
#endif
#include "netif/sio.h"
#include "netif/fifo.h"
#include "lwip/debug.h"
#include "lwip/def.h"
#include "lwip/sys.h"
#include "lwip/arch.h"
#include "lwip/sio.h"
#include "netif/ppp/ppp_opts.h"
#undef htonl
#undef ntohl
#undef htons
#undef ntohs
#undef HTONL
#undef NTOHL
#undef HTONS
#undef NTOHS
#include <stdlib.h>
#include <stdio.h>
#if defined(LWIP_UNIX_OPENBSD)
#include <util.h>
#endif
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#ifndef B57600
#define B57600 57600
#endif
#ifndef B115200
#define B115200 115200
#endif
#ifndef CRTS_IFLOW
#define CRTS_IFLOW 0x00020000
#endif
#ifndef CCTS_OFLOW
#define CCTS_OFLOW 0x00010000
#endif
#ifndef CRTSCTS
#define CRTSCTS (CCTS_OFLOW | CRTS_IFLOW)
#endif
#ifndef LWIP_HAVE_SLIPIF
#define LWIP_HAVE_SLIPIF 1
#endif
#ifndef SLIP_MAX_SIZE
#define SLIP_MAX_SIZE 1500
#endif
#if (PPP_SUPPORT || LWIP_HAVE_SLIPIF) && defined(LWIP_UNIX_LINUX)
#include <pty.h>
#endif
#define BAUDRATE B115200
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef SIO_DEBUG
#define SIO_DEBUG 0
#endif
static sio_status_t statusar[4];
static int run_command(const char *command) {
int res = -1;
#ifndef __APPLE__
res = system(command);
#endif
return res;
}
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
static void signal_handler_IO_0( int status )
{
LWIP_UNUSED_ARG(status);
LWIP_DEBUGF(SIO_DEBUG, ("SigHand: rxSignal channel 0\n"));
fifoPut( &statusar[0].myfifo, statusar[0].fd );
}
static void signal_handler_IO_1( int status )
{
LWIP_UNUSED_ARG(status);
LWIP_DEBUGF(SIO_DEBUG, ("SigHand: rxSignal channel 1\n"));
fifoPut( &statusar[1].myfifo, statusar[1].fd );
}
#endif
static int sio_init( char * device, int devnum, sio_status_t * siostat )
{
struct termios oldtio,newtio;
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
struct sigaction saio;
#endif
int fd;
LWIP_UNUSED_ARG(siostat);
LWIP_UNUSED_ARG(devnum);
fd = open( device, O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( fd < 0 )
{
perror( device );
exit( -1 );
}
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
switch ( devnum )
{
case 0:
LWIP_DEBUGF( SIO_DEBUG, ("sioinit, signal_handler_IO_0\n") );
saio.sa_handler = signal_handler_IO_0;
break;
case 1:
LWIP_DEBUGF( SIO_DEBUG, ("sioinit, signal_handler_IO_1\n") );
saio.sa_handler = signal_handler_IO_1;
break;
default:
LWIP_DEBUGF( SIO_DEBUG,("sioinit, devnum not allowed\n") );
break;
}
saio.sa_flags = 0;
#if defined(LWIP_UNIX_LINUX)
saio.sa_restorer = NULL;
#endif
sigaction( SIGIO,&saio,NULL );
if ( fcntl( fd, F_SETOWN, getpid( ) ) != 0)
{
perror( device );
exit( -1 );
}
if ( fcntl( fd, F_SETFL, FASYNC ) != 0)
{
perror( device );
exit( -1 );
}
#else
if ( fcntl( fd, F_SETFL, 0 ) != 0)
{
perror( device );
exit( -1 );
}
#endif
tcgetattr( fd,&oldtio );
memset(&newtio, 0, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD | CRTSCTS;
newtio.c_iflag = 0;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcsetattr( fd,TCSANOW,&newtio );
tcflush( fd, TCIOFLUSH );
return fd;
}
static void sio_speed( int fd, int speed )
{
struct termios oldtio,newtio;
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: baudcode:%d enter\n", fd, speed));
if ( fd < 0 )
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: fd ERROR\n", fd));
exit( -1 );
}
tcgetattr( fd,&oldtio );
memset(&newtio, 0, sizeof(newtio));
newtio.c_cflag = speed | CS8 | CLOCAL | CREAD;
newtio.c_iflag = 0;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcsetattr( fd,TCSANOW,&newtio );
tcflush( fd, TCIOFLUSH );
LWIP_DEBUGF(SIO_DEBUG, ("sio_speed[%d]: leave\n", fd));
}
void sio_send( u8_t c, sio_status_t * siostat )
{
if ( write( siostat->fd, &c, 1 ) <= 0 )
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_send[%d]: write refused\n", siostat->fd));
}
}
void sio_send_string( u8_t *str, sio_status_t * siostat )
{
int len = strlen( (const char *)str );
if ( write( siostat->fd, str, len ) <= 0 )
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_send_string[%d]: write refused\n", siostat->fd));
}
LWIP_DEBUGF(SIO_DEBUG, ("sio_send_string[%d]: sent: %s\n", siostat->fd, str));
}
void sio_flush( sio_status_t * siostat )
{
LWIP_UNUSED_ARG(siostat);
}
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
u8_t sio_recv( sio_status_t * siostat )
{
return fifoGet( &(siostat->myfifo) );
}
s16_t sio_poll(sio_status_t * siostat)
{
return fifoGetNonBlock( &(siostat->myfifo) );
}
void sio_expect_string( u8_t *str, sio_status_t * siostat )
{
u8_t c;
int finger=0;
LWIP_DEBUGF(SIO_DEBUG, ("sio_expect_string[%d]: %s\n", siostat->fd, str));
while ( 1 )
{
c=fifoGet( &(siostat->myfifo) );
LWIP_DEBUGF(SIO_DEBUG, ("_%c", c));
if ( c==str[finger] )
{
finger++;
} else if ( finger > 0 )
{
if ( str[0] == c )
{
finger = 1;
}
}
if ( 0 == str[finger] )
break;
}
LWIP_DEBUGF(SIO_DEBUG, ("sio_expect_string[%d]: [match]\n", siostat->fd));
}
#endif
#if (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
u32_t sio_write(sio_status_t * siostat, const u8_t *buf, u32_t size)
{
ssize_t wsz = write( siostat->fd, buf, size );
return wsz < 0 ? 0 : wsz;
}
u32_t sio_read(sio_status_t * siostat, u8_t *buf, u32_t size)
{
ssize_t rsz = read( siostat->fd, buf, size );
return rsz < 0 ? 0 : rsz;
}
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
{
return -1;
}
void sio_read_abort(sio_status_t * siostat)
{
LWIP_UNUSED_ARG(siostat);
printf("sio_read_abort[%d]: not yet implemented for unix\n", siostat->fd);
}
#endif
sio_fd_t sio_open(u8_t devnum)
{
char dev[20];
sio_status_t * siostate = &statusar[ devnum ];
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: for devnum %d\n", devnum));
#if ! (PPP_SUPPORT || LWIP_HAVE_SLIPIF)
fifoInit( &siostate->myfifo );
#endif
snprintf( dev, sizeof(dev), "/dev/ttyS%d", devnum );
if ( (devnum == 1) || (devnum == 0) )
{
if ( ( siostate->fd = sio_init( dev, devnum, siostate ) ) == 0 )
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: ERROR opening serial device dev=%s\n", dev));
abort( );
return NULL;
}
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: dev=%s open.\n", siostate->fd, dev));
}
#if PPP_SUPPORT
else if (devnum == 2) {
pid_t childpid;
char name[256];
childpid = forkpty(&siostate->fd, name, NULL, NULL);
if(childpid < 0) {
perror("forkpty");
exit (1);
}
if(childpid == 0) {
execl("/usr/sbin/pppd", "pppd",
"ms-dns", "198.168.100.7",
"local", "crtscts",
"debug",
#ifdef LWIP_PPP_CHAP_TEST
"auth",
"require-chap",
"remotename", "lwip",
#else
"noauth",
#endif
#if LWIP_IPV6
"+ipv6",
#endif
"192.168.1.1:192.168.1.2",
NULL);
perror("execl pppd");
exit (1);
} else {
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: spawned pppd pid %d on %s\n",
siostate->fd, childpid, name));
}
}
#endif
#if LWIP_HAVE_SLIPIF
else if (devnum == 3) {
pid_t childpid;
siostate->fd = posix_openpt(O_RDWR | O_NOCTTY);
if (siostate->fd < 0) {
perror("open pty master");
exit (1);
}
if (grantpt(siostate->fd) != 0) {
perror("grant pty master");
exit (1);
}
if (unlockpt(siostate->fd) != 0) {
perror("unlock pty master");
exit (1);
}
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: for %s\n",
siostate->fd, ptsname(siostate->fd)));
#ifndef TARGET_OS_TV
childpid = fork();
#else
childpid = -1;
#endif
if(childpid < 0) {
perror("fork");
exit (1);
}
if(childpid == 0) {
#ifndef TARGET_OS_TV
execl("/sbin/slattach", "slattach",
"-d", "-v", "-L", "-p", "slip",
ptsname(siostate->fd),
NULL);
#endif
perror("execl slattach");
exit (1);
} else {
int ret;
char buf[1024];
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: spawned slattach pid %d on %s\n",
siostate->fd, childpid, ptsname(siostate->fd)));
sleep(1);
snprintf(buf, sizeof(buf),
"/sbin/ifconfig sl0 mtu %d %s pointopoint %s up",
SLIP_MAX_SIZE, "192.168.2.1", "192.168.2.2");
LWIP_DEBUGF(SIO_DEBUG, ("sio_open[%d]: run_command(\"%s\");\n", siostate->fd, buf));
ret = run_command(buf);
if (ret < 0) {
perror("ifconfig failed");
exit(1);
}
}
}
#endif
else
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_open: device %s (%d) is not supported\n", dev, devnum));
return NULL;
}
return siostate;
}
void sio_change_baud( sioBaudrates baud, sio_status_t * siostat )
{
LWIP_DEBUGF(SIO_DEBUG, ("sio_change_baud[%d]\n", siostat->fd));
switch ( baud )
{
case SIO_BAUD_9600:
sio_speed( siostat->fd, B9600 );
break;
case SIO_BAUD_19200:
sio_speed( siostat->fd, B19200 );
break;
case SIO_BAUD_38400:
sio_speed( siostat->fd, B38400 );
break;
case SIO_BAUD_57600:
sio_speed( siostat->fd, B57600 );
break;
case SIO_BAUD_115200:
sio_speed( siostat->fd, B115200 );
break;
default:
LWIP_DEBUGF(SIO_DEBUG, ("sio_change_baud[%d]: Unknown baudrate, code:%d\n",
siostat->fd, baud));
break;
}
}
#endif