From 00c8124a0a9ff4b2b50097d9159177013d9dfc70 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Thu, 11 Apr 2024 21:20:50 +0200 Subject: [PATCH] Add lua modem_send(file,protocol) --- man/tio.1.in | 18 ++++++++++++------ src/script.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/man/tio.1.in b/man/tio.1.in index 3782681..1442975 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -365,20 +365,26 @@ Toggle conversion to uppercase on output .IP "\fBctrl-t v" Show version .IP "\fBctrl-t x" -Send a file using the XMODEM-1K protocol (prompts for file name) -.IP "\fBctrl-t X" -Send a file using the XMODEM-CRC protocol (prompts for file name) +Send file using the XMODEM-1K or XMODEM-CRC protocol (prompts for file name and protocol) .IP "\fBctrl-t y" -Send a file using the YMODEM protocol (prompts for file name) +Send file using the YMODEM protocol (prompts for file name) .IP "\fBctrl-t ctrl-t" Send ctrl-t character .SH "SCRIPT API" .PP -Tio suppots Lua scripting for manipulating the tty device. In addition to the -Lua API tio makes the following functions available: +Tio suppots Lua scripting to easily automate interaction with the tty device. + +This means that in addition to the Lua API tio makes the following functions +available: .TP 6n + +.IP "\fBmodem_send(file, protocol)" +Send file using x/y-modem protocol. + +Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. + .IP "\fBhigh(line)" Set tty line high. .IP "\fBlow(line)" diff --git a/src/script.c b/src/script.c index c06e93e..e017205 100644 --- a/src/script.c +++ b/src/script.c @@ -31,6 +31,7 @@ #include "print.h" #include "options.h" #include "tty.h" +#include "xymodem.h" static int serial_fd; @@ -153,6 +154,38 @@ static int config_apply(lua_State *L) return 0; } +// lua: modem_send(file, protocol) +static int modem_send(lua_State *L) +{ + const char *file = lua_tostring(L, 1); + int protocol = lua_tointeger(L, 2); + + if (file == NULL) + { + return 0; + } + + switch (protocol) + { + case XMODEM_1K: + tio_printf("Sending file '%s' using XMODEM-1K", file); + tio_printf("%s", xymodem_send(serial_fd, file, XMODEM_1K) < 0 ? "Aborted" : "Done"); + break; + + case XMODEM_CRC: + tio_printf("Sending file '%s' using XMODEM-CRC", file); + tio_printf("%s", xymodem_send(serial_fd, file, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + break; + + case YMODEM: + tio_printf("Sending file '%s' using YMODEM", file); + tio_printf("%s", xymodem_send(serial_fd, file, YMODEM) < 0 ? "Aborted" : "Done"); + break; + } + + return 0; +} + static void script_buffer_run(lua_State *L, const char *script_buffer) { int error; @@ -176,6 +209,7 @@ static const struct luaL_Reg tio_lib[] = { "config_high", config_high}, { "config_low", config_low}, { "config_apply", config_apply}, + { "modem_send", modem_send}, {NULL, NULL} }; @@ -238,6 +272,9 @@ void script_set_globals(lua_State *L) script_set_global(L, "DSR", TIOCM_DSR); script_set_global(L, "CD", TIOCM_CD); script_set_global(L, "RI", TIOCM_RI); + script_set_global(L, "XMODEM_CRC", XMODEM_CRC); + script_set_global(L, "XMODEM_1K", XMODEM_1K); + script_set_global(L, "YMODEM", YMODEM); } void script_run(int fd)