ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
vxi11.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file vxi11.h
6 * @brief VXI-11 (TCP/IP Instrument Protocol) codec over ONC RPC / XDR (PROTOCORE_ENABLE_VXI11) - a
7 * zero-heap codec for the legacy LXI instrument transport that predates HiSLIP.
8 *
9 * VXI-11 rides on ONC RPC (Sun RPC, RFC 5531) with XDR (RFC 4506) over TCP. A session:
10 * portmap GETPORT(0x0607AF, 1, TCP) -> the DEVICE_CORE port ; connect there ;
11 * create_link("inst0") -> a link id ; device_write("*IDN?\n") ; device_read() -> the reply ;
12 * destroy_link().
13 *
14 * This codec provides the reusable ONC-RPC framing (the TCP record-marking header + the CALL /
15 * accepted-REPLY message headers with AUTH_NONE) over XDR (big-endian, 4-byte aligned,
16 * length-prefixed opaque/string - no sub-word types on the wire), plus the VXI-11 DEVICE_CORE
17 * procedure builders + response parsers and the portmapper GETPORT call. Pairs with @c
18 * PROTOCORE_ENABLE_SCPI (the payload). Pure codec, host-tested; the TCP connection is the application's.
19 *
20 * References: VXI-11 (VXIbus Consortium, 1995); RFC 5531 (ONC RPC), RFC 4506 (XDR), RFC 1833 (portmap).
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef PROTOCORE_VXI11_H
27#define PROTOCORE_VXI11_H
28
29#include "protocore_config.h" // the entry point: protocore_types.h for the widths
30
31#if PROTOCORE_ENABLE_VXI11
32
33EMBED_BEGIN_DECLS
34
35// ── ONC RPC / portmapper / VXI-11 program constants ─────────────────────────────────────────────
36#define PROTOCORE_VXI11_CORE_PROG 0x0607AFu ///< DEVICE_CORE RPC program number
37#define PROTOCORE_VXI11_CORE_VERS 1 ///< DEVICE_CORE version
38#define PROTOCORE_RPC_PMAP_PROG 100000u ///< portmapper / rpcbind v2 program
39#define PROTOCORE_RPC_PMAP_VERS 2
40#define PROTOCORE_RPC_PMAP_PORT 111 ///< the well-known portmapper TCP port
41#define PROTOCORE_RPC_PMAP_GETPORT 3 ///< PMAPPROC_GETPORT
42#define PROTOCORE_RPC_PROTO_TCP 6 ///< IPPROTO_TCP (for the GETPORT mapping)
43#define PROTOCORE_RPC_AUTH_NONE 0 ///< the AUTH_NONE auth flavor
44#define PROTOCORE_RPC_MSG_ACCEPTED 0 ///< reply_stat: the call was accepted
45#define PROTOCORE_RPC_ACCEPT_SUCCESS 0 ///< accept_stat: the procedure ran
46
47/** @brief VXI-11 DEVICE_CORE procedure numbers (procs 21 and 24 are intentionally unused). */
48EMBED_ENUM_BYTE(Vxi11Proc, VXI11_PROC_CREATE_LINK = 10, VXI11_PROC_DEVICE_WRITE = 11, VXI11_PROC_DEVICE_READ = 12,
49 VXI11_PROC_DEVICE_READSTB = 13, VXI11_PROC_DEVICE_TRIGGER = 14, VXI11_PROC_DEVICE_CLEAR = 15,
50 VXI11_PROC_DEVICE_REMOTE = 16, VXI11_PROC_DEVICE_LOCAL = 17, VXI11_PROC_DEVICE_LOCK = 18,
51 VXI11_PROC_DEVICE_UNLOCK = 19, VXI11_PROC_DEVICE_ENABLE_SRQ = 20, VXI11_PROC_DEVICE_DOCMD = 22,
52 VXI11_PROC_DESTROY_LINK = 23, VXI11_PROC_CREATE_INTR_CHAN = 25, VXI11_PROC_DESTROY_INTR_CHAN = 26, );
53
54// Device_Flags bits:
55#define PROTOCORE_VXI11_FLAG_WAITLOCK 0x01 ///< block up to lock_timeout on lock contention
56#define PROTOCORE_VXI11_FLAG_END 0x08 ///< (write) assert END/EOI with the last byte
57#define PROTOCORE_VXI11_FLAG_TERMCHRSET 0x80 ///< (read) termChar is an active read terminator
58// device_read `reason` return bits (may combine):
59#define PROTOCORE_VXI11_REASON_REQCNT 0x01 ///< requestSize bytes were transferred
60#define PROTOCORE_VXI11_REASON_CHR 0x02 ///< the termChar was seen
61#define PROTOCORE_VXI11_REASON_END 0x04 ///< an END/EOI indicator was received
62// Device_ErrorCode (common values):
63#define PROTOCORE_VXI11_ERR_NONE 0
64#define PROTOCORE_VXI11_ERR_SYNTAX 1
65#define PROTOCORE_VXI11_ERR_NOT_ACCESSIBLE 3
66#define PROTOCORE_VXI11_ERR_INVALID_LINK 4
67#define PROTOCORE_VXI11_ERR_PARAMETER 5
68#define PROTOCORE_VXI11_ERR_NO_LOCK 12
69#define PROTOCORE_VXI11_ERR_IO_TIMEOUT 15
70#define PROTOCORE_VXI11_ERR_IO_ERROR 17
71#define PROTOCORE_VXI11_ERR_ABORT 23
72
73/** @brief A short human-readable string for a Device_ErrorCode (static; never null). */
74const char *protocore_vxi11_error_str(int32_t error);
75
76// ── reusable ONC RPC framing ────────────────────────────────────────────────────────────────────
77
78/**
79 * @brief Write the 4-byte TCP record-marking header for a single-fragment message: the high bit is
80 * the last-fragment flag (always set here), the low 31 bits are @p payload_len.
81 * @return 4, or 0 if @p cap < 4 or @p payload_len exceeds 31 bits.
82 */
83size_t protocore_rpc_record_mark(uint8_t *buf, size_t cap, uint32_t payload_len);
84
85/**
86 * @brief Parse a 4-byte record-marking header.
87 * @param last receives the last-fragment flag.
88 * @param frag_len receives the fragment payload byte length (excludes the 4 RM bytes).
89 * @return true if @p len >= 4; false otherwise.
90 */
91embed_bool protocore_rpc_parse_record_mark(const uint8_t *buf, size_t len, embed_bool *last, uint32_t *frag_len);
92
93/**
94 * @brief Parse an accepted ONC-RPC reply header (the bytes AFTER the record mark): xid, REPLY,
95 * MSG_ACCEPTED, verf, accept_stat.
96 * @param xid receives the transaction id (echoes the call).
97 * @param accept_stat receives the accept_stat (0 = SUCCESS; the caller checks before reading results).
98 * @param result_off receives the offset (into @p rpc) where the procedure results begin.
99 * @return true on a well-formed accepted reply; false if truncated, not a REPLY, or MSG_DENIED.
100 */
101embed_bool protocore_rpc_parse_reply(const uint8_t *rpc, size_t len, uint32_t *xid, uint32_t *accept_stat,
102 size_t *result_off);
103
104// ── portmapper ──────────────────────────────────────────────────────────────────────────────────
105
106/**
107 * @brief Build a PMAPPROC_GETPORT call (with record mark): maps (prog, vers, proto) to a TCP port.
108 * @return total bytes written (incl. the 4-byte record mark), or 0 on overflow.
109 */
110size_t protocore_vxi11_build_getport(uint8_t *buf, size_t cap, uint32_t xid, uint32_t prog, uint32_t vers,
111 uint32_t proto);
112
113/**
114 * @brief Parse a GETPORT reply (bytes after the record mark). @p port is 0 if not registered.
115 * @return true on a well-formed successful reply; false otherwise.
116 */
117embed_bool protocore_vxi11_parse_getport_resp(const uint8_t *rpc, size_t len, uint32_t *port);
118
119// ── VXI-11 DEVICE_CORE ──────────────────────────────────────────────────────────────────────────
120
121/**
122 * @brief Build a create_link call: open a link to @p device (e.g. "inst0"). @p client_id is
123 * caller-chosen; @p lock_device requests an exclusive lock.
124 * @return total bytes written (incl. record mark), or 0 on overflow / bad input.
125 */
126size_t protocore_vxi11_build_create_link(uint8_t *buf, size_t cap, uint32_t xid, int32_t client_id,
127 embed_bool lock_device, uint32_t lock_timeout, const char *device);
128
129/** @brief A decoded Create_LinkResp. */
130typedef struct
131{
132 int32_t error; ///< Device_ErrorCode (0 = no error)
133 int32_t lid; ///< the link id, for subsequent calls
134 uint32_t abort_port; ///< the DEVICE_ASYNC abort-channel port
135 uint32_t max_recv_size; ///< the largest data block the device accepts in one device_write
136} Vxi11CreateLinkResp;
137embed_bool protocore_vxi11_parse_create_link_resp(const uint8_t *rpc, size_t len, Vxi11CreateLinkResp *out);
138
139/**
140 * @brief Build a device_write call: write @p data (e.g. a SCPI command) to link @p lid. Set
141 * @ref PROTOCORE_VXI11_FLAG_END in @p flags to assert END with the last byte.
142 * @return total bytes written (incl. record mark), or 0 on overflow / bad input.
143 */
144size_t protocore_vxi11_build_device_write(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid, uint32_t io_timeout,
145 uint32_t lock_timeout, uint32_t flags, const uint8_t *data, size_t data_len);
146
147/** @brief A decoded Device_WriteResp. */
148typedef struct
149{
150 int32_t error;
151 uint32_t size; ///< number of bytes written
152} Vxi11WriteResp;
153embed_bool protocore_vxi11_parse_write_resp(const uint8_t *rpc, size_t len, Vxi11WriteResp *out);
154
155/**
156 * @brief Build a device_read call: read up to @p request_size bytes from link @p lid. Set
157 * @ref PROTOCORE_VXI11_FLAG_TERMCHRSET in @p flags to stop at @p term_char.
158 * @return total bytes written (incl. record mark), or 0 on overflow.
159 */
160size_t protocore_vxi11_build_device_read(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid, uint32_t request_size,
161 uint32_t io_timeout, uint32_t lock_timeout, uint32_t flags, uint8_t term_char);
162
163/** @brief A decoded Device_ReadResp. @ref data points INTO @p rpc. */
164typedef struct
165{
166 int32_t error;
167 int32_t reason; ///< OR of PROTOCORE_VXI11_REASON_* (why the read ended)
168 const uint8_t *data;
169 size_t data_len;
170} Vxi11ReadResp;
171embed_bool protocore_vxi11_parse_read_resp(const uint8_t *rpc, size_t len, Vxi11ReadResp *out);
172
173/**
174 * @brief Build a device_readstb call: read the IEEE 488.2 status byte from link @p lid.
175 * @return total bytes written (incl. record mark), or 0 on overflow.
176 */
177size_t protocore_vxi11_build_device_readstb(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid, uint32_t flags,
178 uint32_t lock_timeout, uint32_t io_timeout);
179
180/** @brief A decoded Device_ReadStbResp. */
181typedef struct
182{
183 int32_t error;
184 uint8_t stb; ///< the status byte
185} Vxi11ReadStbResp;
186embed_bool protocore_vxi11_parse_readstb_resp(const uint8_t *rpc, size_t len, Vxi11ReadStbResp *out);
187
188/**
189 * @brief Build a device_clear call: clear link @p lid's device (the protocol-level Selected Device Clear -
190 * it empties the instrument's input / output buffers). Carries the Device_GenericParms (lid + flags +
191 * the lock / io timeouts), as device_readstb does. @return total bytes written, or 0 on overflow.
192 */
193size_t protocore_vxi11_build_device_clear(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid, uint32_t flags,
194 uint32_t lock_timeout, uint32_t io_timeout);
195
196/**
197 * @brief Build a device_trigger call: assert a trigger on link @p lid (the protocol-level Group Execute
198 * Trigger, i.e. IEEE 488.2 `*TRG`). Same Device_GenericParms as device_clear. @return bytes, or 0.
199 */
200size_t protocore_vxi11_build_device_trigger(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid, uint32_t flags,
201 uint32_t lock_timeout, uint32_t io_timeout);
202
203/**
204 * @brief Build a destroy_link call: close link @p lid.
205 * @return total bytes written (incl. record mark), or 0 on overflow.
206 */
207size_t protocore_vxi11_build_destroy_link(uint8_t *buf, size_t cap, uint32_t xid, int32_t lid);
208
209/**
210 * @brief Parse a bare Device_Error result (destroy_link / device_trigger / device_clear / ...).
211 * @param error receives the Device_ErrorCode.
212 * @return true on a well-formed successful reply; false otherwise.
213 */
214embed_bool protocore_vxi11_parse_error_resp(const uint8_t *rpc, size_t len, int32_t *error);
215
216EMBED_END_DECLS
217
218#endif // PROTOCORE_ENABLE_VXI11
219
220#endif // PROTOCORE_VXI11_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.