This commit is contained in:
yabu76 2026-02-14 00:45:24 +00:00 committed by GitHub
commit 4b621a1cd8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 830 additions and 499 deletions

View file

@ -3,3 +3,7 @@ IndentWidth: 4
AllowShortFunctionsOnASingleLine: None
KeepEmptyLinesAtTheStartOfBlocks: false
BreakBeforeBraces: Allman
IndentCaseLabels: true
ColumnLimit: 144
SortIncludes: false
SkipMacroDefinitionBody: true

View file

@ -43,7 +43,7 @@ when used in combination with [tmux](https://tmux.github.io).
* Useful for reconnecting when serial device has no serial device by ID
* Support for non-standard baud rates
* Support for mark and space parity
* X-modem (1K/CRC) and Y-modem file upload
* X-modem (1K/CRC/Checksum) and Y-modem file upload
* Support for RS-485 mode
* List available serial devices
* By device
@ -431,7 +431,7 @@ Returns the `tio` table.
Send file using x/y-modem protocol.
Protocol can be any of `XMODEM_1K`, `XMODEM_CRC`, `YMODEM`.
Protocol can be any of `XMODEM_1K`, `XMODEM_CRC`, `XMODEM_SUM`, `YMODEM`.
#### `tio.ttysearch()`

View file

@ -205,8 +205,8 @@ Strip control characters and escape sequences from log.
.TP
.BR \-m ", " "\-\-map " \fI<flags>
Map (replace, translate) characters on input to the serial device or output
from the serial device. The following mapping flags are supported:
Map (replace, translate) characters on input from the serial device or output
to the serial device. The following mapping flags are supported:
.RS
.TP 12n
@ -426,7 +426,7 @@ Toggle line timestamp mode
.IP "\fBctrl-t v"
Show version
.IP "\fBctrl-t x"
Send file using the XMODEM-1K or XMODEM-CRC protocol (prompts for file name and protocol)
Send file using the XMODEM-1K or XMODEM-CRC or XMODEM-SUM protocol (prompts for file name and protocol)
.IP "\fBctrl-t y"
Send file using the YMODEM protocol (prompts for file name)
.IP "\fBctrl-t ctrl-t"
@ -468,7 +468,7 @@ Returns the tio table.
.IP "\fBtio.send(file, protocol)"
Send file using x/y-modem protocol.
Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.
Protocol can be any of XMODEM_1K, XMODEM_CRC, XMODEM_SUM, YMODEM.
.IP "\fBtio.ttysearch()"
Search for serial devices.

View file

@ -158,7 +158,7 @@ OPTIONS
-m, --map <flags>
Map (replace, translate) characters on input to the serial device or output from the serial device. The following mapping flags are supported:
Map (replace, translate) characters on input from the serial device or output to the serial device. The following mapping flags are supported:
ICRNL Map CR to NL on input (unless IGNCR is set)
@ -342,7 +342,7 @@ KEY COMMANDS
ctrl-t v Show version
ctrl-t x Send file using the XMODEM-1K or XMODEM-CRC protocol (prompts for file name and protocol)
ctrl-t x Send file using the XMODEM-1K or XMODEM-CRC or XMODEM-SUM protocol (prompts for file name and protocol)
ctrl-t y Send file using the YMODEM protocol (prompts for file name)
@ -382,7 +382,7 @@ SCRIPT API
send(file, protocol)
Send file using x/y-modem protocol.
Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM.
Protocol can be any of XMODEM_1K, XMODEM_CRC, XMODEM_SUM, YMODEM.
tty_search()
Search for serial devices.

View file

@ -110,7 +110,8 @@ int main(int argc, char *argv[])
if (interactive_mode)
{
tio_printf("Press ctrl-%c q to quit", option.prefix_key);
} else
}
else
{
tio_printf("Non-interactive mode enabled");
tio_printf("Press ctrl-c to quit");

View file

@ -98,12 +98,17 @@ int read_poll(int fd, void *data, size_t len, int timeout)
if (fds.revents & POLLIN)
{
// Read ready data
// return value should not be 0
return read(fd, data, len);
}
else /* if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) */
{
return -1;
}
}
/* Timeout */
return ret;
return 0;
}
// Function to calculate djb2 hash of string
@ -170,6 +175,7 @@ bool match_patterns(const char *string, const char *patterns)
pattern = strtok(patterns_copy, ",");
while (pattern != NULL)
{
// clang-format off
// Check if the string matches the current pattern
#ifdef FNM_EXTMATCH
if (fnmatch(pattern, string, FNM_EXTMATCH) == 0)
@ -180,6 +186,7 @@ bool match_patterns(const char *string, const char *patterns)
free(patterns_copy);
return true;
}
// clang-format on
// Move to the next pattern
pattern = strtok(NULL, ",");

View file

@ -61,6 +61,7 @@ enum opt_t
OPT_EXEC,
};
// clang-format off
/* Default options */
struct option_t option =
{
@ -125,6 +126,7 @@ struct option_t option =
.map_i_msb2lsb = false,
.map_o_ign_cr = false,
};
// clang-format on
void option_print_help(char *argv[])
{
@ -833,12 +835,14 @@ void options_print()
tio_printf(" Output line delay: %d", option.output_line_delay);
tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect));
tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false");
// clang-format off
tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration,
option.rts_pulse_duration,
option.cts_pulse_duration,
option.dsr_pulse_duration,
option.dcd_pulse_duration,
option.ri_pulse_duration);
// clang-format on
tio_printf(" Input mode: %s", option_input_mode_to_string(option.input_mode));
tio_printf(" Output mode: %s", option_output_mode_to_string(option.output_mode));
tio_printf(" Alert: %s", option_alert_state_to_string(option.alert));
@ -887,8 +891,9 @@ void options_parse(int argc, char *argv[])
option.vt100 = true;
}
while (1)
while (true)
{
// clang-format off
static struct option long_options[] =
{
{"baudrate", required_argument, 0, 'b' },
@ -932,6 +937,7 @@ void options_parse(int argc, char *argv[])
{"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES },
{0, 0, 0, 0 }
};
// clang-format on
/* getopt_long stores the option index here */
int option_index = 0;
@ -1176,6 +1182,7 @@ void options_parse_final(int argc, char *argv[])
#ifdef __CYGWIN__
unsigned char portnum;
char *tty_win;
// clang-format off
if ( ((strncmp("COM", option.target, 3) == 0)
|| (strncmp("com", option.target, 3) == 0) )
&& (sscanf(option.target + 3, "%hhu", &portnum) == 1)
@ -1184,5 +1191,6 @@ void options_parse_final(int argc, char *argv[])
asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1);
option.target = tty_win;
}
// clang-format on
#endif
}

View file

@ -139,6 +139,7 @@ static void readline_input_left_bracket(void)
}
else
{
readline_input_char('[');
rl_escape = 0;
}
}

View file

@ -45,6 +45,7 @@
static int device_fd;
// clang-format off
static char script_init[] =
"tio.set = function(arg)\n"
" local dtr = arg.DTR or -1\n"
@ -71,6 +72,7 @@ static char script_init[] =
"end\n"
"tio.alwaysecho = true\n"
"setmetatable(tio, tio)\n";
// clang-format on
static bool alwaysecho(lua_State *L)
{
@ -100,7 +102,9 @@ static int api_echo(lua_State *L)
log_printf("\n[%s] %s", pTimeStampNow, str);
}
}
} else {
}
else
{
for (size_t i=0; i<len; i++)
{
putchar(str[i]);
@ -237,6 +241,12 @@ static int api_send(lua_State *L)
tio_printf("%s", ret < 0 ? "Aborted" : "Done");
break;
case XMODEM_SUM:
tio_printf("Sending file '%s' using XMODEM-SUM", file);
ret = xymodem_send(device_fd, file, XMODEM_SUM);
tio_printf("%s", ret < 0 ? "Aborted" : "Done");
break;
case YMODEM:
tio_printf("Sending file '%s' using YMODEM", file);
ret = xymodem_send(device_fd, file, YMODEM);
@ -255,7 +265,8 @@ static int api_write(lua_State *L)
ssize_t ret;
int attempts = 100;
do {
do
{
ret = write(device_fd, string, len);
if (ret < 0)
return luaL_error(L, "%s", strerror(errno));
@ -319,7 +330,8 @@ static int api_read(lua_State *L)
}
// lua: string = tio.readline(timeout)
static int api_readline(lua_State *L) {
static int api_readline(lua_State *L)
{
int timeout = lua_tointeger(L, 1); //ms
luaL_Buffer b;
char ch;
@ -331,7 +343,8 @@ static int api_readline(lua_State *L) {
luaL_buffinit(L, &b);
luaL_prepbuffer(&b);
while (true) {
while (true)
{
int ret = read_poll(device_fd, &ch, 1, timeout);
if (ret < 0)
@ -440,6 +453,7 @@ static void script_file_run(lua_State *L, const char *filename)
}
}
// clang-format off
static const struct luaL_Reg tio_lib[] =
{
{ "echo", api_echo},
@ -453,6 +467,7 @@ static const struct luaL_Reg tio_lib[] =
{ "ttysearch", api_ttysearch},
{NULL, NULL}
};
// clang-format on
static void script_load(lua_State *L)
{
@ -477,6 +492,7 @@ static void script_set_globals(lua_State *L)
script_set_global(L, "toggle", 2);
script_set_global(L, "high", 1);
script_set_global(L, "low", 0);
script_set_global(L, "XMODEM_SUM", XMODEM_SUM);
script_set_global(L, "XMODEM_CRC", XMODEM_CRC);
script_set_global(L, "XMODEM_1K", XMODEM_1K);
script_set_global(L, "YMODEM", YMODEM);

View file

@ -147,6 +147,7 @@ typedef enum
SUBCOMMAND_MAP,
} sub_command_t;
// clang-format off
const char random_array[] =
{
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x0A, 0x20,
@ -160,6 +161,7 @@ const char random_array[] =
0x66, 0x65, 0x65, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6B, 0x21, 0x0A, 0x20, 0x0A,
0x00
};
// clang-format on
bool interactive_mode = true;
@ -212,7 +214,7 @@ inline static bool is_valid_hex(char c)
inline static unsigned char char_to_nibble(char c)
{
if(c >= '0' && c <= '9')
if (c >= '0' && c <= '9')
{
return c - '0';
}
@ -257,22 +259,23 @@ ssize_t tty_write(int fd, const void *buffer, size_t count)
{
ssize_t retval = 0, bytes_written = 0;
size_t i;
unsigned char *cbuf = (unsigned char *)buffer;
if (option.map_o_ltu)
{
// Convert lower case to upper case
for (i = 0; i<count; i++)
for (i = 0; i < count; i++)
{
*((unsigned char*)buffer+i) = toupper(*((unsigned char*)buffer+i));
cbuf[i] = toupper(cbuf[i]);
}
}
if (option.output_delay || option.output_line_delay)
{
// Write byte by byte with output delay
for (i=0; i<count; i++)
for (i = 0; i < count; i++)
{
retval = write(fd, buffer, 1);
retval = write(fd, &cbuf[i], 1);
if (retval < 0)
{
// Error
@ -281,15 +284,14 @@ ssize_t tty_write(int fd, const void *buffer, size_t count)
}
bytes_written += retval;
if (option.output_line_delay && *(unsigned char*)buffer == '\n')
{
delay(option.output_line_delay);
}
fsync(fd);
tcdrain(fd);
if (option.output_delay)
if (option.output_line_delay && cbuf[i] == '\n')
{
delay(option.output_line_delay);
}
else if (option.output_delay)
{
delay(option.output_delay);
}
@ -331,7 +333,7 @@ void *tty_stdin_input_thread(void *arg)
pthread_mutex_unlock(&mutex_input_ready);
// Input loop for stdin
while (1)
while (true)
{
/* Input from stdin ready */
byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ);
@ -360,7 +362,8 @@ void *tty_stdin_input_thread(void *arg)
for (int i = 0; i<byte_count; i++)
{
// first do key hit check for xmodem abort
if (!key_hit) {
if (!key_hit)
{
key_hit = input_buffer[i];
byte_count--;
memcpy(input_buffer+i, input_buffer+i+1, byte_count-i);
@ -383,7 +386,7 @@ void *tty_stdin_input_thread(void *arg)
exit(EXIT_SUCCESS);
break;
case KEY_SHIFT_F:
tio_printf("Flushed data I/O buffers")
tio_printf("Flushed data I/O buffers");
tcflush(device_fd, TCIOFLUSH);
break;
default:
@ -414,7 +417,8 @@ void tty_input_thread_create(void)
{
pthread_mutex_lock(&mutex_input_ready);
if (pthread_create(&thread, NULL, tty_stdin_input_thread, NULL) != 0) {
if (pthread_create(&thread, NULL, tty_stdin_input_thread, NULL) != 0)
{
tio_error_printf("pthread_create() error");
exit(1);
}
@ -631,6 +635,7 @@ void tty_output_mode_set(output_mode_t mode)
static void mappings_print(void)
{
// clang-format off
if (option.map_i_cr_nl || option.map_ign_cr || option.map_i_ff_escc ||
option.map_i_nl_cr || option.map_i_nl_crnl || option.map_i_cr_crnl ||
option.map_o_cr_nl || option.map_o_del_bs || option.map_o_nl_crnl ||
@ -656,6 +661,7 @@ static void mappings_print(void)
{
tio_printf(" Mappings: none");
}
// clang-format on
}
void handle_command_sequence(char input_char, char *output_char, bool *forward)
@ -756,7 +762,35 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
tio_printf("Ready to receiving file '%s' ", line);
tio_printf("Press any key to abort transfer");
ret = xymodem_send(device_fd, line, XMODEM_CRC);
ret = xymodem_receive(device_fd, line, XMODEM_CRC);
tio_printf("%s", ret < 0 ? "Aborted" : "Done");
}
break;
case KEY_3:
tio_printf("Send file with XMODEM-SUM");
tio_printf_raw("Enter file name: ");
if (tio_readln())
{
int ret;
tio_printf("Sending file '%s' ", line);
tio_printf("Press any key to abort transfer");
ret = xymodem_send(device_fd, line, XMODEM_SUM);
tio_printf("%s", ret < 0 ? "Aborted" : "Done");
}
break;
case KEY_4:
tio_printf("Receive file with XMODEM-SUM");
tio_printf_raw("Enter file name: ");
if (tio_readln())
{
int ret;
tio_printf("Ready to receiving file '%s' ", line);
tio_printf("Press any key to abort transfer");
ret = xymodem_receive(device_fd, line, XMODEM_SUM);
tio_printf("%s", ret < 0 ? "Aborted" : "Done");
}
break;
@ -1011,6 +1045,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
break;
case KEY_M:
// clang-format off
/* Change mapping of characters on input or output */
tio_printf("Please enter which mapping to set or unset:");
tio_printf(" (0) ICRNL: %s mapping CR to NL on input (unless IGNCR is set)",
@ -1039,6 +1074,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
option.map_o_nulbrk ? "Unset" : "Set");
tio_printf(" (c) OIGNCR: %s ignoring CR on output",
option.map_o_ign_cr ? "Unset" : "Set");
// clang-format on
// Process next input character as sub command
sub_command = SUBCOMMAND_MAP;
@ -1050,7 +1086,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case KEY_R:
/* Run script */
tio_printf("Run Lua script")
tio_printf("Run Lua script");
tio_printf_raw("Enter file name: ");
if (tio_readln())
{
@ -1119,6 +1155,8 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
tio_printf(" (0) XMODEM-1K send");
tio_printf(" (1) XMODEM-CRC send");
tio_printf(" (2) XMODEM-CRC receive");
tio_printf(" (3) XMODEM-SUM send");
tio_printf(" (4) XMODEM-SUM receive");
// Process next input character as sub command
sub_command = SUBCOMMAND_XMODEM;
break;
@ -1126,7 +1164,8 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward)
case KEY_Y:
tio_printf("Send file with YMODEM");
tio_printf_raw("Enter file name: ");
if (tio_readln()) {
if (tio_readln())
{
int ret;
tio_printf("Sending file '%s' ", line);
@ -2044,7 +2083,8 @@ GList *tty_search_for_serial_devices(void)
}
/* Populate device info */
*device_info = (device_t) {
*device_info = (device_t)
{
.path = devicePath,
.tid = g_strdup(tid),
.uptime = uptime,
@ -2361,7 +2401,7 @@ void tty_wait_for_device(void)
// Happens when port unpluged
if (errno == EACCES)
{
goto error;
break;
}
#elif defined(__APPLE__)
if (errno == EBADF)

File diff suppressed because it is too large Load diff

View file

@ -21,14 +21,15 @@
#pragma once
typedef enum {
typedef enum
{
XMODEM_1K,
XMODEM_CRC,
XMODEM_SUM,
YMODEM,
} modem_mode_t;
extern char key_hit;
int xymodem_send(int sio, const char *filename, modem_mode_t mode);
int xymodem_receive(int sio, const char *filename, modem_mode_t mode);