Go back to the newer version of osint_linux.c

This commit is contained in:
David Betz 2015-01-27 20:55:10 -05:00
parent 198c52d00f
commit d494eba116
1 changed files with 80 additions and 32 deletions

View File

@ -44,7 +44,7 @@
#include <signal.h>
#include "osint.h"
#ifdef RASPBERRY_PI
#ifdef RASPBERRY_PI
#include "gpio_sysfs.h"
#endif
@ -55,11 +55,15 @@ static int continue_terminal = 1;
#ifdef RASPBERRY_PI
static int propellerResetGpioPin = 17;
static int propellerResetGpioLevel = 1;
static int propellerResetGpioLevel = 0;
#endif
/* Normally we use DTR for reset */
#ifdef RASPBERRY_PI
static reset_method_t reset_method = RESET_WITH_GPIO;
#else
static reset_method_t reset_method = RESET_WITH_DTR;
#endif
int use_reset_method(char* method)
{
@ -171,6 +175,56 @@ int serial_init(const char* port, unsigned long baud)
return 0;
}
fcntl(hSerial, F_SETFL, 0);
/* set the baud rate */
if (!serial_baud(baud)) {
close(hSerial);
return 0;
}
fcntl(hSerial, F_SETFL, 0);
/* get the current options */
chk("tcgetattr", tcgetattr(hSerial, &old_sparm));
sparm = old_sparm;
/* set raw input */
#ifdef MACOSX
cfmakeraw(&sparm);
sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1;
chk("cfsetspeed", cfsetspeed(&sparm, tbaud));
#else
memset(&sparm, 0, sizeof(sparm));
sparm.c_cflag = CS8 | CLOCAL | CREAD;
sparm.c_lflag = 0; // &= ~(ICANON | ECHO | ECHOE | ISIG);
sparm.c_oflag = 0; // &= ~OPOST;
sparm.c_iflag = IGNPAR | IGNBRK;
sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1;
chk("cfsetispeed", cfsetispeed(&sparm, tbaud));
chk("cfsetospeed", cfsetospeed(&sparm, tbaud));
#endif
/* set the options */
chk("tcflush", tcflush(hSerial, TCIFLUSH));
chk("tcsetattr", tcsetattr(hSerial, TCSANOW, &sparm));
return 1;
}
/**
* change the baud rate of the serial port
* @param baud - baud rate
* @returns 1 for success and 0 for failure
*/
int serial_baud(unsigned long baud)
{
struct termios sparm;
int tbaud = 0;
switch(baud) {
case 0: // default
tbaud = B115200;
@ -209,8 +263,15 @@ int serial_init(const char* port, unsigned long baud)
case 38400:
tbaud = B38400;
break;
case 19200:
tbaud = B19200;
break;
case 9600:
tbaud = B9600;
break;
default:
printf("Unsupported baudrate. Use ");
tbaud = baud; break;
#ifdef B921600
printf("921600, ");
#endif
@ -226,33 +287,19 @@ int serial_init(const char* port, unsigned long baud)
#ifdef B230400
printf("230400, ");
#endif
printf("115200, 57600, or 38400\n");
printf("115200, 57600, 38400, 19200, or 9600\n");
serial_done();
exit(2);
break;
}
fcntl(hSerial, F_SETFL, 0);
/* get the current options */
chk("tcgetattr", tcgetattr(hSerial, &old_sparm));
sparm = old_sparm;
chk("tcgetattr", tcgetattr(hSerial, &sparm));
/* set raw input */
#ifdef MACOSX
cfmakeraw(&sparm);
sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1;
chk("cfsetspeed", cfsetspeed(&sparm, tbaud));
chk("cfsetspeed", cfsetspeed(&sparm, tbaud));
#else
memset(&sparm, 0, sizeof(sparm));
sparm.c_cflag = CS8 | CLOCAL | CREAD;
sparm.c_lflag = 0; // &= ~(ICANON | ECHO | ECHOE | ISIG);
sparm.c_oflag = 0; // &= ~OPOST;
sparm.c_iflag = IGNPAR | IGNBRK;
sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1;
chk("cfsetispeed", cfsetispeed(&sparm, tbaud));
chk("cfsetospeed", cfsetospeed(&sparm, tbaud));
#endif
@ -260,7 +307,7 @@ int serial_init(const char* port, unsigned long baud)
/* set the options */
chk("tcflush", tcflush(hSerial, TCIFLUSH));
chk("tcsetattr", tcsetattr(hSerial, TCSANOW, &sparm));
return 1;
}
@ -276,10 +323,10 @@ void serial_done(void)
close(hSerial);
hSerial = -1;
}
#ifdef RASPBERRY_PI
#ifdef RASPBERRY_PI
if (reset_method == RESET_WITH_GPIO)
{
// gpio_unexport(propellerResetGpioPin); FIXME: This does not work on OpenWRT on DLINK-615-D1
// gpio_unexport(propellerResetGpioPin);
}
#endif
}
@ -369,7 +416,7 @@ static void assert_reset(void)
cmd = TIOCM_RTS;
ioctl(hSerial, TIOCMBIS, &cmd); /* assert bit */
break;
#ifdef RASPBERRY_PI
#ifdef RASPBERRY_PI
case RESET_WITH_GPIO:
gpio_write(propellerResetGpioPin, propellerResetGpioLevel);
break;
@ -398,7 +445,7 @@ static void deassert_reset(void)
cmd = TIOCM_RTS;
ioctl(hSerial, TIOCMBIC, &cmd); /* assert bit */
break;
#ifdef RASPBERRY_PI
#ifdef RASPBERRY_PI
case RESET_WITH_GPIO:
gpio_write(propellerResetGpioPin, propellerResetGpioLevel ^ 1);
break;
@ -446,10 +493,10 @@ void msleep(int ms)
/**
* simple terminal emulator
*/
void terminal_mode(int check_for_exit, int dummy)
void terminal_mode(int check_for_exit, int pst_mode)
{
struct termios oldt, newt;
char buf[128];
char buf[128], realbuf[256];
ssize_t cnt;
fd_set set;
int exit_char = 0xdead; /* not a valid character */
@ -497,17 +544,19 @@ void terminal_mode(int check_for_exit, int dummy)
if (buf[i] == 0) {
sawexit_valid = 1;
} else {
buf[realbytes++] = exit_char;
buf[realbytes++] = buf[i];
realbuf[realbytes++] = exit_char;
realbuf[realbytes++] = buf[i];
sawexit_char = 0;
}
} else if (((int)buf[i] & 0xff) == exit_char) {
sawexit_char = 1;
} else {
buf[realbytes++] = buf[i];
realbuf[realbytes++] = buf[i];
if (pst_mode && buf[i] == '\r')
realbuf[realbytes++] = '\n';
}
}
write(fileno(stdout), buf, realbytes);
write(fileno(stdout), realbuf, realbytes);
}
}
if (FD_ISSET(STDIN_FILENO, &set)) {
@ -534,4 +583,3 @@ done:
}
int serial_baud(unsigned long baud){return 0;}