Documentation
¶
Overview ¶
Package serial carries a serial port of the host to the client as a local virtual serial port.
Wire protocol, on one session stream named "serial":
- request {op, port, baud, data, parity, stop, dtr, rts}. op "list" answers one JSON {ok, error, ports:[...]} and ends. op "open" opens the port on the host (exclusively, raw, 8N1 unless asked otherwise) and answers {ok, error, port, baud}.
- after an ok open both sides exchange binary frames (the wire package framing, payload at most 64 KiB): 1 DATA raw bytes; 2 CONFIG JSON {baud,data,parity,stop,rtscts} client to host; 3 MODEM JSON, client to host {dtr,rts} (null = unchanged), host to client {cts,dsr,dcd,ri}; 4 BREAK client to host, 2-byte big-endian milliseconds; 5 NOTICE host to client, a text diagnostic (for example a refused CONFIG).
- the stream ending, in either direction, closes the host port.
The client end of a virtual port is a pseudo-terminal whose slave is symlinked to a path of the user's choosing; it exists on Linux and macOS.
Index ¶
- Constants
- Variables
- func DefaultLocalPath(target, port string) (string, error)
- type Active
- type AttachOptions
- type Config
- type Modem
- type OpenOptions
- type Port
- type PortInfo
- type Remote
- func (r *Remote) Break(d time.Duration) error
- func (r *Remote) Close() error
- func (r *Remote) Done() <-chan struct{}
- func (r *Remote) Err() error
- func (r *Remote) Read(b []byte) (int, error)
- func (r *Remote) SetConfig(cfg Config) error
- func (r *Remote) SetModem(dtr, rts *bool) error
- func (r *Remote) Write(b []byte) (int, error)
- type Virtual
Constants ¶
const Service = "serial"
Service is the wire service name.
Variables ¶
var ErrUnsupported = errors.New("serial: not supported on this platform")
ErrUnsupported is returned where a platform has no implementation.
Functions ¶
func DefaultLocalPath ¶
DefaultLocalPath is where a virtual port for target's port lives by default.
Types ¶
type Active ¶
type Active struct {
PID int `json:"pid"`
Target string `json:"target"`
Local string `json:"local"`
Port string `json:"port"`
Config string `json:"config"`
}
Active is one virtual port running on this machine.
func ListActive ¶
ListActive returns the virtual ports run by remohop processes on this machine.
type AttachOptions ¶
type AttachOptions struct {
OpenOptions
// LocalPath is where the virtual port appears (a symlink to the pty
// slave). Required.
LocalPath string
}
AttachOptions configure Attach.
type Config ¶
type Config struct {
Baud int `json:"baud"`
Data int `json:"data"`
Parity byte `json:"-"`
Stop int `json:"stop"`
RTSCTS bool `json:"rtscts,omitempty"`
}
Config is the line setting of a port: Baud, 5-8 data bits, parity 'N', 'E' or 'O', 1 or 2 stop bits, optional RTS/CTS hardware flow control.
func (*Config) ParseFormat ¶
ParseFormat parses "8N1" style frame formats into c's Data/Parity/Stop.
type Modem ¶
type Modem struct {
CTS bool `json:"cts"`
DSR bool `json:"dsr"`
DCD bool `json:"dcd"`
RI bool `json:"ri"`
}
Modem is the state of the input modem status lines.
type OpenOptions ¶
type OpenOptions struct {
// DTR and RTS, when non-nil, are the initial state of those lines.
DTR, RTS *bool
// OnNotice receives host diagnostics (a refused setting); may be nil.
OnNotice func(string)
// OnModem receives host input-line changes (CTS, DSR, DCD, RI); may be nil.
OnModem func(Modem)
}
OpenOptions are optional extras of Open.
type Port ¶
type Port interface {
io.ReadWriteCloser
Apply(Config) error
// SetModem sets DTR and/or RTS (nil = leave alone).
SetModem(dtr, rts *bool) error
// Modem reads the input status lines.
Modem() (Modem, error)
// Break holds the line in break condition for d.
Break(d time.Duration) error
}
Port is an open serial device on the host.
type PortInfo ¶
type PortInfo struct {
Path string `json:"path"` // /dev/ttyUSB0, COM3
Name string `json:"name"` // ttyUSB0, COM3
ByID []string `json:"by_id,omitempty"` // stable /dev/serial/by-id names
Driver string `json:"driver,omitempty"` // kernel driver
Description string `json:"description,omitempty"` // manufacturer product
VID string `json:"vid,omitempty"`
PID string `json:"pid,omitempty"`
Serial string `json:"serial,omitempty"`
}
PortInfo describes one serial port of a machine.
type Remote ¶
type Remote struct {
// Path is the device path on the host; Config what it was opened with.
Path string
Config Config
// contains filtered or unexported fields
}
Remote is an open remote serial port: an io.ReadWriteCloser of the bytes of the port plus control. It is safe for concurrent use.
func Open ¶
func Open(ctx context.Context, c *peer.Conn, port string, cfg Config, opts OpenOptions) (*Remote, error)
Open opens the host's port (exclusively) with cfg.
func (*Remote) Done ¶
func (r *Remote) Done() <-chan struct{}
Done is closed when the port or the session ends.
func (*Remote) Read ¶
Read returns bytes from the remote port; io.EOF once the port or session is gone.
type Virtual ¶
type Virtual struct {
Remote *Remote
// Path is the symlink the user gave; Slave the real pty device behind it.
Path, Slave string
// contains filtered or unexported fields
}
Virtual is a local virtual serial port wired to a remote one.
func Attach ¶
func Attach(ctx context.Context, c *peer.Conn, port string, cfg Config, o AttachOptions) (*Virtual, error)
Attach opens the remote port and creates the local virtual serial port.