mirror of
https://github.com/espressif/openthread.git
synced 2026-08-10 12:47:47 +00:00
For upstream/serial and logging fixes (#28)
* posix: logging: Use `stderr` instead of `stdout` for debug logging. * posix: serial: Restore socket `termios` settings for `stdin` and `stdout` at exit.
This commit is contained in:
committed by
Jonathan Hui
parent
1f24fa44fa
commit
4c560dc94e
@@ -50,63 +50,64 @@ void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat
|
||||
switch (aLogLevel)
|
||||
{
|
||||
case kLogLevelNone:
|
||||
printf("NONE ");
|
||||
fprintf(stderr, "NONE ");
|
||||
break;
|
||||
|
||||
case kLogLevelCrit:
|
||||
printf("CRIT ");
|
||||
fprintf(stderr, "CRIT ");
|
||||
break;
|
||||
|
||||
case kLogLevelWarn:
|
||||
printf("WARN ");
|
||||
fprintf(stderr, "WARN ");
|
||||
break;
|
||||
|
||||
case kLogLevelInfo:
|
||||
printf("INFO ");
|
||||
fprintf(stderr, "INFO ");
|
||||
break;
|
||||
|
||||
case kLogLevelDebg:
|
||||
printf("DEBG ");
|
||||
fprintf(stderr, "DEBG ");
|
||||
break;
|
||||
}
|
||||
|
||||
switch (aLogRegion)
|
||||
{
|
||||
case kLogRegionApi:
|
||||
printf("API ");
|
||||
fprintf(stderr, "API ");
|
||||
break;
|
||||
|
||||
case kLogRegionMle:
|
||||
printf("MLE ");
|
||||
fprintf(stderr, "MLE ");
|
||||
break;
|
||||
|
||||
case kLogRegionArp:
|
||||
printf("ARP ");
|
||||
fprintf(stderr, "ARP ");
|
||||
break;
|
||||
|
||||
case kLogRegionNetData:
|
||||
printf("NETD ");
|
||||
fprintf(stderr, "NETD ");
|
||||
break;
|
||||
|
||||
case kLogRegionIp6:
|
||||
printf("IPV6 ");
|
||||
fprintf(stderr, "IPV6 ");
|
||||
break;
|
||||
|
||||
case kLogRegionIcmp:
|
||||
printf("ICMP ");
|
||||
fprintf(stderr, "ICMP ");
|
||||
break;
|
||||
|
||||
case kLogRegionMac:
|
||||
printf("MAC ");
|
||||
fprintf(stderr, "MAC ");
|
||||
break;
|
||||
|
||||
case kLogRegionMem:
|
||||
printf("MEM ");
|
||||
fprintf(stderr, "MEM ");
|
||||
break;
|
||||
}
|
||||
|
||||
va_start(args, aFormat);
|
||||
vprintf(aFormat, args);
|
||||
vfprintf(stderr, aFormat, args);
|
||||
fprintf(stderr, "\r");
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,19 @@ static int s_out_fd;
|
||||
static pthread_t s_pthread;
|
||||
static sem_t *s_semaphore;
|
||||
|
||||
static struct termios original_stdin_termios;
|
||||
static struct termios original_stdout_termios;
|
||||
|
||||
static void restore_stdin_termios()
|
||||
{
|
||||
tcsetattr(s_in_fd, TCSAFLUSH, &original_stdin_termios);
|
||||
}
|
||||
|
||||
static void restore_stdout_termios()
|
||||
{
|
||||
tcsetattr(s_out_fd, TCSAFLUSH, &original_stdout_termios);
|
||||
}
|
||||
|
||||
ThreadError otPlatSerialEnable(void)
|
||||
{
|
||||
ThreadError error = kThreadError_None;
|
||||
@@ -69,6 +82,18 @@ ThreadError otPlatSerialEnable(void)
|
||||
s_in_fd = dup(STDIN_FILENO);
|
||||
s_out_fd = dup(STDOUT_FILENO);
|
||||
dup2(STDERR_FILENO, STDOUT_FILENO);
|
||||
|
||||
if (isatty(s_in_fd))
|
||||
{
|
||||
tcgetattr(s_in_fd, &original_stdin_termios);
|
||||
atexit(&restore_stdin_termios);
|
||||
}
|
||||
|
||||
if (isatty(s_out_fd))
|
||||
{
|
||||
tcgetattr(s_out_fd, &original_stdin_termios);
|
||||
atexit(&restore_stdout_termios);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -106,7 +131,6 @@ ThreadError otPlatSerialEnable(void)
|
||||
{
|
||||
// get current configuration
|
||||
VerifyOrExit(tcgetattr(s_in_fd, &termios) == 0, perror("tcgetattr"); error = kThreadError_Error);
|
||||
s_in_termios = termios;
|
||||
|
||||
// turn off input processing
|
||||
termios.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
|
||||
@@ -116,7 +140,7 @@ ThreadError otPlatSerialEnable(void)
|
||||
|
||||
// turn off character processing
|
||||
termios.c_cflag &= ~(CSIZE | PARENB);
|
||||
termios.c_cflag |= CS8;
|
||||
termios.c_cflag |= CS8|HUPCL|CREAD|CLOCAL;
|
||||
|
||||
// return 1 byte at a time
|
||||
termios.c_cc[VMIN] = 1;
|
||||
@@ -135,14 +159,13 @@ ThreadError otPlatSerialEnable(void)
|
||||
{
|
||||
// get current configuration
|
||||
VerifyOrExit(tcgetattr(s_out_fd, &termios) == 0, perror("tcgetattr"); error = kThreadError_Error);
|
||||
s_out_termios = termios;
|
||||
|
||||
// turn off output processing
|
||||
termios.c_oflag = 0;
|
||||
|
||||
// turn off character processing
|
||||
termios.c_cflag &= ~(CSIZE | PARENB);
|
||||
termios.c_cflag |= CS8;
|
||||
termios.c_cflag |= CS8|HUPCL|CREAD|CLOCAL;
|
||||
|
||||
// configure baud rate
|
||||
VerifyOrExit(cfsetospeed(&termios, B115200) == 0, perror("cfsetospeed"); error = kThreadError_Error);
|
||||
@@ -167,18 +190,8 @@ ThreadError otPlatSerialDisable(void)
|
||||
{
|
||||
ThreadError error = kThreadError_None;
|
||||
|
||||
if (isatty(s_out_fd))
|
||||
{
|
||||
tcsetattr(s_out_fd, TCSANOW, &s_out_termios);
|
||||
}
|
||||
|
||||
if (isatty(s_in_fd))
|
||||
{
|
||||
tcsetattr(s_in_fd, TCSANOW, &s_in_termios);
|
||||
}
|
||||
|
||||
close(s_out_fd);
|
||||
close(s_in_fd);
|
||||
close(s_out_fd);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user