2014-12-13 17:34:11 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 10:29:25 +02:00
|
|
|
* apps/system/cu/cu_main.c
|
2014-12-13 17:34:11 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 sysmocom - s.f.m.c. GmbH. All rights reserved.
|
|
|
|
* Author: Harald Welte <hwelte@sysmocom.de>
|
|
|
|
*
|
|
|
|
* 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. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "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
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "system/readline.h"
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
#include "cu.h"
|
|
|
|
|
2023-06-08 13:31:48 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_CUTERM_DISABLE_ERROR_PRINT
|
|
|
|
# define cu_error(...)
|
|
|
|
#else
|
2023-03-26 05:56:12 +02:00
|
|
|
# define cu_error(...) dprintf(STDERR_FILENO, __VA_ARGS__)
|
2023-06-08 13:31:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-29 13:39:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-12-13 17:34:11 +01:00
|
|
|
enum parity_mode
|
|
|
|
{
|
|
|
|
PARITY_NONE,
|
|
|
|
PARITY_EVEN,
|
|
|
|
PARITY_ODD,
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-19 15:13:12 +02:00
|
|
|
static struct cu_globals_s g_cu;
|
|
|
|
|
2014-12-13 17:34:11 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cu_listener
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Entry point for the listener thread.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR void *cu_listener(FAR void *parameter)
|
|
|
|
{
|
2023-03-26 05:44:42 +02:00
|
|
|
FAR struct cu_globals_s *cu = (FAR struct cu_globals_s *)parameter;
|
|
|
|
|
2019-11-05 14:07:02 +01:00
|
|
|
for (; ; )
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char ch;
|
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
rc = read(cu->devfd, &ch, 1);
|
2014-12-13 17:34:11 +01:00
|
|
|
if (rc <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-26 05:56:12 +02:00
|
|
|
write(STDOUT_FILENO, &ch, 1);
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Won't get here */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigint(int sig)
|
|
|
|
{
|
2022-04-19 11:26:07 +02:00
|
|
|
g_cu.force_exit = true;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2023-03-26 05:44:42 +02:00
|
|
|
static int set_termios(FAR struct cu_globals_s *cu, int rate,
|
|
|
|
enum parity_mode parity, int rtscts, int nocrlf)
|
2023-03-19 18:07:21 +01:00
|
|
|
#else
|
2023-03-26 05:44:42 +02:00
|
|
|
static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
|
2023-03-19 18:07:21 +01:00
|
|
|
#endif
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct termios tio;
|
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
if (isatty(cu->devfd))
|
|
|
|
{
|
|
|
|
tio = cu->devtio;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-03-19 18:07:21 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2023-09-03 20:31:17 +02:00
|
|
|
tio.c_cflag &= ~(PARENB | PARODD | CRTSCTS);
|
2021-07-19 10:40:28 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
switch (parity)
|
|
|
|
{
|
|
|
|
case PARITY_EVEN:
|
|
|
|
tio.c_cflag |= PARENB;
|
|
|
|
break;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
case PARITY_ODD:
|
|
|
|
tio.c_cflag |= PARENB | PARODD;
|
|
|
|
break;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
case PARITY_NONE:
|
|
|
|
break;
|
|
|
|
}
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
/* Set baudrate */
|
2020-10-19 17:43:55 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
if (rate != 0)
|
|
|
|
{
|
|
|
|
cfsetspeed(&tio, rate);
|
|
|
|
}
|
2020-10-19 17:43:55 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
if (rtscts)
|
|
|
|
{
|
|
|
|
tio.c_cflag |= CRTS_IFLOW | CCTS_OFLOW;
|
|
|
|
}
|
2023-03-19 18:07:21 +01:00
|
|
|
#endif
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
tio.c_oflag = OPOST;
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
/* Enable or disable \n -> \r\n conversion during write */
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
if (nocrlf == 0)
|
|
|
|
{
|
|
|
|
tio.c_oflag |= ONLCR;
|
|
|
|
}
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
ret = tcsetattr(cu->devfd, TCSANOW, &tio);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 05:35:10 +01:00
|
|
|
/* Let the remote machine to handle all crlf/echo except Ctrl-C */
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
if (cu->stdfd >= 0)
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-03-26 05:44:42 +02:00
|
|
|
tio = cu->stdtio;
|
2023-03-21 05:35:10 +01:00
|
|
|
|
|
|
|
tio.c_iflag = 0;
|
|
|
|
tio.c_oflag = 0;
|
|
|
|
tio.c_lflag &= ~ECHO;
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
ret = tcsetattr(cu->stdfd, TCSANOW, &tio);
|
2020-08-12 23:10:37 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2023-09-03 20:31:17 +02:00
|
|
|
cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
|
|
|
|
return ret;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
return 0;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
static void retrieve_termios(FAR struct cu_globals_s *cu)
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-09-03 20:31:17 +02:00
|
|
|
if (isatty(cu->devfd))
|
|
|
|
{
|
|
|
|
tcsetattr(cu->devfd, TCSANOW, &cu->devtio);
|
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
if (cu->stdfd >= 0)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
2023-03-26 05:44:42 +02:00
|
|
|
tcsetattr(cu->stdfd, TCSANOW, &cu->stdtio);
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help(void)
|
|
|
|
{
|
|
|
|
printf("Usage: cu [options]\n"
|
2014-12-13 20:11:06 +01:00
|
|
|
" -l: Use named device (default %s)\n"
|
2020-08-12 23:10:37 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2014-12-13 17:34:11 +01:00
|
|
|
" -e: Set even parity\n"
|
|
|
|
" -o: Set odd parity\n"
|
2014-12-13 20:11:06 +01:00
|
|
|
" -s: Use given speed (default %d)\n"
|
2023-09-06 03:32:41 +02:00
|
|
|
" -f: Disable RTS/CTS flow control (default: on)\n"
|
2020-08-12 23:10:37 +02:00
|
|
|
#endif
|
2023-03-19 18:07:21 +01:00
|
|
|
" -c: Disable lf -> crlf conversion (default: off)\n"
|
2023-09-06 03:32:41 +02:00
|
|
|
" -E: Set the escape character (default: ~).\n"
|
|
|
|
" To eliminate the escape character, use -E ''\n"
|
2014-12-13 20:11:06 +01:00
|
|
|
" -?: This help\n",
|
2020-10-19 17:43:09 +02:00
|
|
|
CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE
|
2023-03-19 18:07:21 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
, CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD
|
2020-08-12 23:10:37 +02:00
|
|
|
#endif
|
2020-10-19 17:43:09 +02:00
|
|
|
);
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 03:32:41 +02:00
|
|
|
static void print_escape_help(FAR struct cu_globals_s *cu)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
2023-09-06 03:32:41 +02:00
|
|
|
printf("[Escape sequences]\n[%c. hangup]\n", cu->escape);
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 03:32:41 +02:00
|
|
|
static int cu_cmd(FAR struct cu_globals_s *cu, char bcmd)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
switch (bcmd)
|
|
|
|
{
|
|
|
|
case '?':
|
2023-09-06 03:32:41 +02:00
|
|
|
print_escape_help(cu);
|
2014-12-13 17:34:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* FIXME: implement other commands such as send/receive file */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cu_main
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Main entry point for the serial terminal example.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct sigaction sa;
|
2023-03-26 05:44:42 +02:00
|
|
|
FAR const char *devname = CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE;
|
|
|
|
FAR struct cu_globals_s *cu = &g_cu;
|
2020-08-12 23:10:37 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2014-12-13 20:11:06 +01:00
|
|
|
int baudrate = CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD;
|
2014-12-13 17:34:11 +01:00
|
|
|
enum parity_mode parity = PARITY_NONE;
|
|
|
|
int rtscts = 1;
|
2020-08-12 23:10:37 +02:00
|
|
|
#endif
|
2023-03-19 18:07:21 +01:00
|
|
|
int nocrlf = 0;
|
2014-12-13 17:34:11 +01:00
|
|
|
int option;
|
|
|
|
int ret;
|
|
|
|
int start_of_line = 1;
|
2014-12-13 20:11:06 +01:00
|
|
|
int exitval = EXIT_FAILURE;
|
2020-10-19 17:43:09 +02:00
|
|
|
bool badarg = false;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
/* Initialize global data */
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
memset(cu, 0, sizeof(*cu));
|
2023-09-06 03:32:41 +02:00
|
|
|
cu->escape = '~';
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
/* Install signal handlers */
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = sigint;
|
2020-10-23 05:22:33 +02:00
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
optind = 0; /* Global that needs to be reset in FLAT mode */
|
2023-09-06 03:32:41 +02:00
|
|
|
while ((option = getopt(argc, argv, "l:s:ceE:fho?")) != ERROR)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
devname = optarg;
|
|
|
|
break;
|
|
|
|
|
2020-08-12 23:10:37 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2014-12-13 17:34:11 +01:00
|
|
|
case 's':
|
|
|
|
baudrate = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2020-03-13 23:53:53 +01:00
|
|
|
parity = PARITY_EVEN;
|
2014-12-13 17:34:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
2020-03-13 23:53:53 +01:00
|
|
|
parity = PARITY_ODD;
|
2014-12-13 17:34:11 +01:00
|
|
|
break;
|
|
|
|
|
2023-09-06 03:32:41 +02:00
|
|
|
case 'f':
|
2014-12-13 17:34:11 +01:00
|
|
|
rtscts = 0;
|
|
|
|
break;
|
2023-03-19 18:07:21 +01:00
|
|
|
#endif
|
2014-12-13 17:36:37 +01:00
|
|
|
|
2020-08-12 23:10:37 +02:00
|
|
|
case 'c':
|
2020-10-19 17:43:09 +02:00
|
|
|
nocrlf = 1;
|
|
|
|
break;
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-09-06 03:32:41 +02:00
|
|
|
case 'E':
|
|
|
|
cu->escape = atoi(optarg);
|
2020-10-19 17:43:09 +02:00
|
|
|
break;
|
2020-03-13 23:54:18 +01:00
|
|
|
|
2019-11-05 14:07:02 +01:00
|
|
|
case 'h':
|
2014-12-13 17:34:11 +01:00
|
|
|
case '?':
|
|
|
|
print_help();
|
2020-10-19 17:43:09 +02:00
|
|
|
exitval = EXIT_SUCCESS;
|
2023-03-26 05:44:42 +02:00
|
|
|
|
|
|
|
/* Go through */
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
default:
|
2020-10-19 17:43:09 +02:00
|
|
|
badarg = true;
|
|
|
|
break;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 17:43:09 +02:00
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return exitval;
|
|
|
|
}
|
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
/* Open the serial device for reading and writing */
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
cu->devfd = open(devname, O_RDWR);
|
|
|
|
if (cu->devfd < 0)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
2023-06-08 13:31:48 +02:00
|
|
|
cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n",
|
2023-09-03 20:31:17 +02:00
|
|
|
devname, errno);
|
2014-12-13 17:34:11 +01:00
|
|
|
goto errout_with_devinit;
|
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
/* Remember serial device termios attributes */
|
2020-08-12 23:10:37 +02:00
|
|
|
|
2023-09-03 20:31:17 +02:00
|
|
|
if (isatty(cu->devfd))
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-09-03 20:31:17 +02:00
|
|
|
ret = tcgetattr(cu->devfd, &cu->devtio);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
|
|
|
|
goto errout_with_devfd;
|
|
|
|
}
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
/* Remember std termios attributes if it is a tty. Try to select
|
2020-08-12 23:10:37 +02:00
|
|
|
* right descriptor that is used to refer to tty
|
|
|
|
*/
|
|
|
|
|
2023-03-26 05:56:12 +02:00
|
|
|
if (isatty(STDERR_FILENO))
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-03-26 05:56:12 +02:00
|
|
|
cu->stdfd = STDERR_FILENO;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
2023-03-26 05:56:12 +02:00
|
|
|
else if (isatty(STDOUT_FILENO))
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-03-26 05:56:12 +02:00
|
|
|
cu->stdfd = STDOUT_FILENO;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
2023-03-26 05:56:12 +02:00
|
|
|
else if (isatty(STDIN_FILENO))
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-03-26 05:56:12 +02:00
|
|
|
cu->stdfd = STDIN_FILENO;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-26 05:44:42 +02:00
|
|
|
cu->stdfd = -1;
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
if (cu->stdfd >= 0)
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-03-26 05:44:42 +02:00
|
|
|
tcgetattr(cu->stdfd, &cu->stdtio);
|
2020-08-12 23:10:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-19 18:07:21 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2023-03-26 05:44:42 +02:00
|
|
|
if (set_termios(cu, baudrate, parity, rtscts, nocrlf) != 0)
|
2023-03-19 18:07:21 +01:00
|
|
|
#else
|
2023-03-26 05:44:42 +02:00
|
|
|
if (set_termios(cu, nocrlf) != 0)
|
2023-03-19 18:07:21 +01:00
|
|
|
#endif
|
2020-08-12 23:10:37 +02:00
|
|
|
{
|
2023-09-03 20:35:38 +02:00
|
|
|
goto errout_with_devfd_retrieve;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the serial receiver thread */
|
|
|
|
|
|
|
|
ret = pthread_attr_init(&attr);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2023-06-08 13:31:48 +02:00
|
|
|
cu_error("cu_main: pthread_attr_init failed: %d\n", ret);
|
2023-09-03 20:35:38 +02:00
|
|
|
goto errout_with_devfd_retrieve;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:07:02 +01:00
|
|
|
/* Set priority of listener to configured value */
|
|
|
|
|
|
|
|
attr.priority = CONFIG_SYSTEM_CUTERM_PRIORITY;
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
ret = pthread_create(&cu->listener, &attr, cu_listener, cu);
|
2020-10-19 17:43:09 +02:00
|
|
|
pthread_attr_destroy(&attr);
|
2014-12-13 17:34:11 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2023-06-08 13:31:48 +02:00
|
|
|
cu_error("cu_main: Error in thread creation: %d\n", ret);
|
2023-09-03 20:35:38 +02:00
|
|
|
goto errout_with_devfd_retrieve;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send messages and get responses -- forever */
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
while (!cu->force_exit)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
2023-08-30 06:19:50 +02:00
|
|
|
char ch;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
2023-03-26 05:56:12 +02:00
|
|
|
if (read(STDIN_FILENO, &ch, 1) <= 0)
|
2020-03-13 23:54:18 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-09-06 03:32:41 +02:00
|
|
|
if (start_of_line == 1 && ch == cu->escape)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
2014-12-13 20:11:06 +01:00
|
|
|
/* We've seen and escape (~) character, echo it to local
|
2014-12-13 17:34:11 +01:00
|
|
|
* terminal and read the next char from serial
|
|
|
|
*/
|
|
|
|
|
2023-03-26 05:56:12 +02:00
|
|
|
write(STDOUT_FILENO, &ch, 1);
|
|
|
|
|
|
|
|
if (read(STDIN_FILENO, &ch, 1) <= 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == cu->escape)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
/* Escaping a tilde: handle like normal char */
|
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
write(cu->devfd, &ch, 1);
|
2014-12-13 17:34:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-06 03:32:41 +02:00
|
|
|
if (cu_cmd(cu, ch) == 1)
|
2014-12-13 17:34:11 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Normal character */
|
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
write(cu->devfd, &ch, 1);
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
/* Determine if we are now at the start of a new line or not */
|
|
|
|
|
|
|
|
if (ch == '\n' || ch == '\r')
|
|
|
|
{
|
|
|
|
start_of_line = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_of_line = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 05:44:42 +02:00
|
|
|
pthread_cancel(cu->listener);
|
2014-12-13 20:11:06 +01:00
|
|
|
exitval = EXIT_SUCCESS;
|
2014-12-13 17:34:11 +01:00
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
2023-09-03 20:35:38 +02:00
|
|
|
errout_with_devfd_retrieve:
|
2023-03-26 05:44:42 +02:00
|
|
|
retrieve_termios(cu);
|
2023-09-03 20:35:38 +02:00
|
|
|
errout_with_devfd:
|
|
|
|
close(cu->devfd);
|
2014-12-13 17:34:11 +01:00
|
|
|
errout_with_devinit:
|
2014-12-13 20:11:06 +01:00
|
|
|
return exitval;
|
2014-12-13 17:34:11 +01:00
|
|
|
}
|