Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 0 additions & 92 deletions system/cu/cu.h

This file was deleted.

90 changes: 60 additions & 30 deletions system/cu/cu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,26 @@
#include <stdlib.h>
#include <signal.h>
#include <debug.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <termios.h>

#include "system/readline.h"

#include "cu.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

/* Configuration ************************************************************/

#ifndef CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE
# define CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE "/dev/ttyS0"
#endif

#ifndef CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD
# define CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD 115200
#endif

#ifdef CONFIG_SYSTEM_CUTERM_DISABLE_ERROR_PRINT
# define cu_error(...)
Expand All @@ -75,12 +91,25 @@ enum parity_mode
PARITY_ODD,
};

/* All terminal state data is packaged in a single structure to minimize
* name conflicts with other global symbols -- a poor man's name space.
*/

struct cu_globals_s
{
int devfd; /* I/O data to serial port */
int stdfd; /* I/O data to standard console */
int escape; /* Escape char */
struct termios devtio; /* Original serial port setting */
struct termios stdtio; /* Original standard console setting */
pthread_t listener; /* Terminal listener thread */
bool force_exit; /* Force exit */
};

/****************************************************************************
* Private Data
****************************************************************************/

static struct cu_globals_s g_cu;

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -127,7 +156,8 @@ static FAR void *cu_listener(FAR void *parameter)
#ifdef CONFIG_ENABLE_ALL_SIGNALS
static void sigint(int sig)
{
g_cu.force_exit = true;
FAR struct cu_globals_s *cu = siginfo->si_user;
cu->force_exit = true;
}
#endif

Expand Down Expand Up @@ -280,8 +310,8 @@ int main(int argc, FAR char *argv[])
#ifdef CONFIG_ENABLE_ALL_SIGNALS
struct sigaction sa;
#endif
struct cu_globals_s cu;
FAR const char *devname = CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE;
FAR struct cu_globals_s *cu = &g_cu;
#ifdef CONFIG_SERIAL_TERMIOS
int baudrate = CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD;
enum parity_mode parity = PARITY_NONE;
Expand All @@ -297,8 +327,8 @@ int main(int argc, FAR char *argv[])

/* Initialize global data */

memset(cu, 0, sizeof(*cu));
cu->escape = '~';
memset(&cu, 0, sizeof(cu));
cu.escape = '~';
#ifdef CONFIG_ENABLE_ALL_SIGNALS
/* Install signal handlers */

Expand Down Expand Up @@ -338,7 +368,7 @@ int main(int argc, FAR char *argv[])
break;

case 'E':
cu->escape = atoi(optarg);
cu.escape = atoi(optarg);
break;

case 'h':
Expand All @@ -361,8 +391,8 @@ int main(int argc, FAR char *argv[])

/* Open the serial device for reading and writing */

cu->devfd = open(devname, O_RDWR);
if (cu->devfd < 0)
cu.devfd = open(devname, O_RDWR);
if (cu.devfd < 0)
{
cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n",
devname, errno);
Expand All @@ -371,9 +401,9 @@ int main(int argc, FAR char *argv[])

/* Remember serial device termios attributes */

if (isatty(cu->devfd))
if (isatty(cu.devfd))
{
ret = tcgetattr(cu->devfd, &cu->devtio);
ret = tcgetattr(cu.devfd, &cu.devtio);
if (ret)
{
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
Expand All @@ -387,30 +417,30 @@ int main(int argc, FAR char *argv[])

if (isatty(STDERR_FILENO))
{
cu->stdfd = STDERR_FILENO;
cu.stdfd = STDERR_FILENO;
}
else if (isatty(STDOUT_FILENO))
{
cu->stdfd = STDOUT_FILENO;
cu.stdfd = STDOUT_FILENO;
}
else if (isatty(STDIN_FILENO))
{
cu->stdfd = STDIN_FILENO;
cu.stdfd = STDIN_FILENO;
}
else
{
cu->stdfd = -1;
cu.stdfd = -1;
}

if (cu->stdfd >= 0)
if (cu.stdfd >= 0)
{
tcgetattr(cu->stdfd, &cu->stdtio);
tcgetattr(cu.stdfd, &cu.stdtio);
}

#ifdef CONFIG_SERIAL_TERMIOS
if (set_termios(cu, baudrate, parity, rtscts, nocrlf) != 0)
if (set_termios(&cu, baudrate, parity, rtscts, nocrlf) != 0)
#else
if (set_termios(cu, nocrlf) != 0)
if (set_termios(&cu, nocrlf) != 0)
#endif
{
goto errout_with_devfd_retrieve;
Expand All @@ -429,7 +459,7 @@ int main(int argc, FAR char *argv[])

attr.priority = CONFIG_SYSTEM_CUTERM_PRIORITY;

ret = pthread_create(&cu->listener, &attr, cu_listener, cu);
ret = pthread_create(&cu.listener, &attr, cu_listener, &cu);
pthread_attr_destroy(&attr);
if (ret != 0)
{
Expand All @@ -439,7 +469,7 @@ int main(int argc, FAR char *argv[])

/* Send messages and get responses -- forever */

while (!cu->force_exit)
while (!cu.force_exit)
{
char buf[CONFIG_LINE_MAX];
ssize_t nwrite = 0;
Expand Down Expand Up @@ -469,21 +499,21 @@ int main(int argc, FAR char *argv[])
*/

escaping = false;
if (cu_cmd(cu, ch) == 1)
if (cu_cmd(&cu, ch) == 1)
{
cu->force_exit = true;
cu.force_exit = true;
nread = i;
break;
}
}

if (start_of_line == 1 && ch == cu->escape)
if (start_of_line == 1 && ch == cu.escape)
{
/* Normal character */

if (i > nwrite)
{
write(cu->devfd, &buf[nwrite], i - nwrite);
write(cu.devfd, &buf[nwrite], i - nwrite);
}

nwrite = i + 1;
Expand Down Expand Up @@ -514,19 +544,19 @@ int main(int argc, FAR char *argv[])

if (nread > nwrite)
{
write(cu->devfd, &buf[nwrite], nread - nwrite);
write(cu.devfd, &buf[nwrite], nread - nwrite);
}
}

pthread_cancel(cu->listener);
pthread_cancel(cu.listener);
exitval = EXIT_SUCCESS;

/* Error exits */

errout_with_devfd_retrieve:
retrieve_termios(cu);
retrieve_termios(&cu);
errout_with_devfd:
close(cu->devfd);
close(cu.devfd);
errout_with_devinit:
return exitval;
}
Loading