From c49faa7337bf30f28f51cf27f95e5415d2b477be Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Sat, 30 Nov 2024 11:03:40 +0100 Subject: [PATCH] Clean up lua API Rename modem_send() to send() Rename send to write() --- README.md | 32 ++++++++++++------- examples/lua/automatic-linux-login.lua | 4 +-- examples/lua/read.lua | 8 ++--- examples/lua/read_line.lua | 6 ++-- man/tio.1.in | 43 +++++++++++++------------- src/script.c | 6 ++-- 6 files changed, 55 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index fd93d9e..0f1a0aa 100644 --- a/README.md +++ b/README.md @@ -401,12 +401,30 @@ expect(string, timeout) On successful match it also returns the match string as second return value. -send(string) - Send string. +read(size, timeout) + Read from serial device. If timeout is 0 or not provided it will wait + forever until data is ready to read. + + Returns number of bytes read on success, 0 on timeout, or -1 on error. + + On success, returns read string as second return value. + +read_line(timeout) + Read line from serial device. If timeout is 0 or not provided it will + wait forever until data is ready to read. + + Returns number of bytes read on success, 0 on timeout, or -1 on error. + + On success, returns the string that was read as second return value. + Also emits a single timestamp to stdout and log file per options.timestamp + and options.log. + +write(string) + Write string to serial device. Returns number of bytes written on success or -1 on error. -modem_send(file, protocol) +send(file, protocol) Send file using x/y-modem protocol. Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. @@ -421,14 +439,6 @@ tty_search() Returns nil if no serial devices are found. -read(size, timeout) - Read from serial device. If timeout is 0 or not provided it will wait - forever until data is ready to read. - - Returns number of bytes read on success, 0 on timeout, or -1 on error. - - On success, returns read string as second return value. - set{line=state, ...} Set state of one or multiple tty modem lines. diff --git a/examples/lua/automatic-linux-login.lua b/examples/lua/automatic-linux-login.lua index a287b40..428caa1 100644 --- a/examples/lua/automatic-linux-login.lua +++ b/examples/lua/automatic-linux-login.lua @@ -18,9 +18,9 @@ if (1 == found) then local hostname = string.match(match_str, "^%w+") local login = logins[hostname] if (nil ~= login) then - send(login.username .. "\n") + write(login.username .. "\n") expect("Password:") - send(login.password .. "\n") + write(login.password .. "\n") else io.write("\r\nDon't know login info for " .. hostname .. "\r\n") end diff --git a/examples/lua/read.lua b/examples/lua/read.lua index 2ee99db..6baa032 100644 --- a/examples/lua/read.lua +++ b/examples/lua/read.lua @@ -1,14 +1,14 @@ read(1000, 6000) -- initial config -send("\n") +write("\n") msleep(100) read(650, 60) -- main menu -send("S") -- S menu +write("S") -- S menu msleep(30) read(650, 60) -send("t") -- Parallel Value Table +write("t") -- Parallel Value Table read(650, 60) while true do msleep(1000) - send("t") + write("t") read(650, 50) -- repeat PVT forever end diff --git a/examples/lua/read_line.lua b/examples/lua/read_line.lua index 97f157d..ef6ec20 100644 --- a/examples/lua/read_line.lua +++ b/examples/lua/read_line.lua @@ -1,13 +1,13 @@ read(1000, 8000) -- read initial config -send("\n") +write("\n") read(650, 100) -- main menu -send("S") -- S menu +write("S") -- S menu n = 1 while n > 0 do -- while not empty, read more n, str = read_line(25) end while true do - send("t") -- query PV table + write("t") -- query PV table msleep(880) n = 1 while n > 0 do -- while not empty, read more diff --git a/man/tio.1.in b/man/tio.1.in index 4ad15ed..9d75c45 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -428,7 +428,8 @@ Send ctrl-t character .PP Tio suppots Lua scripting to easily automate interaction with the tty device. -In addition to the Lua API tio makes the following functions available: +In addition to the standard Lua API tio makes the following functions +available: .TP 6n @@ -441,26 +442,6 @@ Returns 1 on successful match, 0 on timeout, or -1 on error. On successful match it also returns the match string as second return value. -.IP "\fBsend(string)" -Send string. - -Returns number of bytes written on success or -1 on error. - -.IP "\fBmodem_send(file, protocol)" -Send file using x/y-modem protocol. - -Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. - -.IP "\fBtty_search()" -Search for serial devices. - -Returns a table of number indexed tables, one for each serial device found. -Each of these tables contains the serial device information accessible via the -following string indexed elements "path", "tid", "uptime", "driver", -"description". - -Returns nil if no serial devices are found. - .IP "\fBread(size, timeout)" Read from serial device. If timeout is 0 or not provided it will wait forever until data is ready to read. @@ -480,6 +461,26 @@ On success, returns the string that was read as second return value. Also emits a single timestamp to stdout and log file per options.timestamp and options.log. +.IP "\fBwrite(string)" +Write string to serial device. + +Returns number of bytes written on success or -1 on error. + +.IP "\fBsend(file, protocol)" +Send file using x/y-modem protocol. + +Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. + +.IP "\fBtty_search()" +Search for serial devices. + +Returns a table of number indexed tables, one for each serial device found. +Each of these tables contains the serial device information accessible via the +following string indexed elements "path", "tid", "uptime", "driver", +"description". + +Returns nil if no serial devices are found. + .IP "\fBset{line=state, ...}" Set state of one or multiple tty modem lines. diff --git a/src/script.c b/src/script.c index b565741..f6023eb 100644 --- a/src/script.c +++ b/src/script.c @@ -185,7 +185,7 @@ static int modem_send(lua_State *L) } // lua: send(string) -static int send_(lua_State *L) +static int write_(lua_State *L) { const char *string = lua_tostring(L, 1); int ret; @@ -550,8 +550,8 @@ static const struct luaL_Reg tio_lib[] = { "sleep", sleep_}, { "msleep", msleep}, { "line_set", line_set}, - { "modem_send", modem_send}, - { "send", send_}, + { "send", modem_send}, + { "write", write_}, { "read", read_string}, { "read_line", read_line}, { "expect", expect},