Skip to content

add serial module#413

Open
dangowrt wants to merge 7 commits into
jow-:masterfrom
dangowrt:serial-module
Open

add serial module#413
dangowrt wants to merge 7 commits into
jow-:masterfrom
dangowrt:serial-module

Conversation

@dangowrt

@dangowrt dangowrt commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

serial: native serial/tty configuration module

Adds a serial module to configure serial/tty devices from ucode, replacing setserial/stty called via system().

Every function operates on an fs.open() handle (first argument) and settings are composed from raw exported constants, no string enums.

  • Line settings (termios): attr, setattr, setspeed, setraw, setblocking (VMIN/VTIME for line-based and TLV reads)
  • Out-of-band modem lines: mget, mset, mbis, mbic, dtr, rts
  • Control ops: sendbreak, drain, flush, input_waiting, output_waiting
  • setserial (Linux): getinfo, setinfo, lowlatency

Built as serial.so behind SERIAL_SUPPORT (no extra deps). Includes a minimalist gated 05_lib_serial test.

dangowrt added 7 commits July 6, 2026 01:14
Introduce the serial module, a native replacement for configuring
tty/serial devices without invoking setserial or stty via system().

Add the SERIAL_SUPPORT build option and the shared infrastructure:
get_fd() resolves the descriptor from any fs handle exposing a fileno
method (or a bare integer), so every function operates on handles created
via fs.open(); error() reports the last errno through the VM registry,
mirroring the fs module; isatty() tests for a terminal.

The module owns no resource type; device lifetime stays with the fs
handle.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Add attr() and setattr() for full termios control. attr() returns the
line settings as an object (iflag, oflag, cflag, lflag, ispeed, ospeed
and the cc array); setattr() performs a read-modify-write, applying only
the fields present and honouring an optional TCSANOW, TCSADRAIN or
TCSAFLUSH selector.

Export the raw termios constants (optional actions, the c_cflag, c_iflag,
c_oflag and c_lflag bits, the c_cc subscripts with NCCS, and the Bxxx
baud values) so callers compose configurations from named bits rather
than strings. Each constant is guarded with ifdef for portability.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Layer convenience helpers over the raw termios access. setspeed() sets
the input and output line speed from a Bxxx constant. setraw() switches
to raw mode via cfmakeraw(), the basis for binary and TLV framing.
setblocking() sets the VMIN and VTIME read controls, so a subsequent
read() on the fs handle can block for an exact byte count (length-prefixed
reads) or apply an inter-byte timeout.

Define _DEFAULT_SOURCE so cfmakeraw() is visible under glibc.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Expose the RS-232 modem control lines. mget() returns the modem status
bitmask (test individual lines by masking with the exported TIOCM_*
constants); mset() replaces it; mbis() and mbic() assert and deassert
individual bits. dtr() and rts() are convenience wrappers for the two
output lines.

Export the TIOCM_* line constants. Pseudo-terminals do not implement
these ioctls and report ENOTTY via error(); real UART devices honour
them.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Add the remaining line-control operations: sendbreak() transmits a break,
drain() blocks until queued output has been transmitted, flush() discards
buffered input and/or output, and input_waiting() and output_waiting()
report the pending byte counts via TIOCINQ and TIOCOUTQ.

Export the TCIFLUSH, TCOFLUSH and TCIOFLUSH queue selectors.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Provide the low-level driver configuration setserial exposes, via the
Linux TIOCGSERIAL and TIOCSSERIAL ioctls; compiled only on Linux.

getinfo() returns the serial_struct as an object; setinfo() applies a
read-modify-write of the supplied fields, covering baud_base and
custom_divisor for non-standard rates and the driver flags; lowlatency()
toggles ASYNC_LOW_LATENCY for the common setserial low_latency case.

Export the ASYNC_* driver flags and the PORT_* UART types.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Add a test under the gated 05_lib_serial category, skipped when serial.so
is absent. It exercises the hardware-independent paths over a pseudo
terminal: isatty(), attr() decoding, the setblocking() VMIN round-trip,
setraw() clearing canonical mode, the exported constant relationships,
and the modem error path where mget() on a non-terminal reports ENOTTY.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant