ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hislip.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 hislip.h
6 * @brief HiSLIP (High-Speed LAN Instrument Protocol) message codec (PROTOCORE_ENABLE_HISLIP) - a zero-heap
7 * codec for the IVI Foundation's modern LXI instrument transport (IVI-6.1, HiSLIP 2.0) on
8 * TCP port 4880, the successor to VXI-11 that carries SCPI at higher throughput.
9 *
10 * A HiSLIP session runs over TWO TCP connections to the same port 4880 - a synchronous channel
11 * (the ordered SCPI command/response stream: Data / DataEND, Trigger, device-clear) and an
12 * asynchronous channel (out-of-band control: lock, status/SRQ, remote-local, interrupt) - bound by
13 * a 16-bit SessionID negotiated in the handshake.
14 *
15 * Every message is a fixed 16-byte header optionally followed by a payload:
16 * @code
17 * "HS" (2) MessageType (1) ControlCode (1) MessageParameter (4, BE) PayloadLength (8, BE)
18 * @endcode
19 * This codec builds + parses that header (@ref protocore_hislip_build_header / @ref protocore_hislip_parse_header),
20 * the Initialize / AsyncInitialize handshake (the MessageParameter carries the protocol version +
21 * vendor id, then the negotiated version + SessionID), and the Data / DataEND messages that carry a
22 * SCPI payload keyed by a MessageID. Pairs with @c PROTOCORE_ENABLE_SCPI (the payload). Pure codec,
23 * host-tested; the two TCP connections are the application's.
24 *
25 * Reference: IVI-6.1 "IVI High-Speed LAN Instrument Protocol (HiSLIP)" v2.0 (2020-04-23).
26 *
27 * @author Douglas Quigg (dstroy0)
28 * @date 2026
29 */
30
31#ifndef PROTOCORE_HISLIP_H
32#define PROTOCORE_HISLIP_H
33
34#include "protocore_config.h" // the entry point: protocore_types.h for the widths
35
36#if PROTOCORE_ENABLE_HISLIP
37
38EMBED_BEGIN_DECLS
39
40/** @brief The IANA-assigned HiSLIP TCP port (both channels connect here). */
41#define PROTOCORE_HISLIP_PORT 4880
42
43/** @brief The fixed header length: prologue(2) + type(1) + control(1) + parameter(4) + length(8). */
44#define PROTOCORE_HISLIP_HEADER_LEN 16
45
46/** @brief Protocol version words (`<major><minor>`), encoded in the high 16 bits of the handshake
47 * MessageParameter. The client offers its max; the server returns min(client, server). */
48#define PROTOCORE_HISLIP_VERSION_1_0 0x0100
49#define PROTOCORE_HISLIP_VERSION_1_1 0x0101
50#define PROTOCORE_HISLIP_VERSION_2_0 0x0200
51
52/** @brief The MessageID a client starts at; each subsequent Data/DataEND/Trigger increments by 2
53 * (unsigned 32-bit, wraps). A server response echoes the request's MessageID. */
54#define PROTOCORE_HISLIP_MESSAGE_ID_INIT 0xFFFFFF00u
55
56// ControlCode bits carried by an InitializeResponse:
57#define PROTOCORE_HISLIP_INITRESP_OVERLAP 0x01 ///< bit 0: prefer overlapped (vs synchronized) mode
58#define PROTOCORE_HISLIP_INITRESP_ENC_MANDATORY 0x02 ///< bit 1: encryption mandatory (2.0)
59#define PROTOCORE_HISLIP_INITRESP_ENC_INITIAL 0x04 ///< bit 2: initial encryption required (2.0)
60// ControlCode bit carried by a Data or DataEND message:
61#define PROTOCORE_HISLIP_DATA_RMT_DELIVERED 0x01 ///< bit 0: message delivered following a Response Message Terminator
62
63/** @brief HiSLIP MessageType codes (IVI-6.1). Codes 0-24 are HiSLIP 1.x; 25-38 were added in 2.0. */
64EMBED_ENUM_BYTE(HislipMsg, HISLIP_MSG_INITIALIZE = 0, HISLIP_MSG_INITIALIZE_RESPONSE = 1, HISLIP_MSG_FATAL_ERROR = 2,
65 HISLIP_MSG_ERROR = 3, HISLIP_MSG_ASYNC_LOCK = 4, HISLIP_MSG_ASYNC_LOCK_RESPONSE = 5,
66 HISLIP_MSG_DATA = 6, HISLIP_MSG_DATA_END = 7, HISLIP_MSG_DEVICE_CLEAR_COMPLETE = 8,
67 HISLIP_MSG_DEVICE_CLEAR_ACKNOWLEDGE = 9, HISLIP_MSG_ASYNC_REMOTE_LOCAL_CONTROL = 10,
68 HISLIP_MSG_ASYNC_REMOTE_LOCAL_RESPONSE = 11, HISLIP_MSG_TRIGGER = 12, HISLIP_MSG_INTERRUPTED = 13,
69 HISLIP_MSG_ASYNC_INTERRUPTED = 14, HISLIP_MSG_ASYNC_MAX_MSG_SIZE = 15,
70 HISLIP_MSG_ASYNC_MAX_MSG_SIZE_RESPONSE = 16, HISLIP_MSG_ASYNC_INITIALIZE = 17,
71 HISLIP_MSG_ASYNC_INITIALIZE_RESPONSE = 18, HISLIP_MSG_ASYNC_DEVICE_CLEAR = 19,
72 HISLIP_MSG_ASYNC_SERVICE_REQUEST = 20, HISLIP_MSG_ASYNC_STATUS_QUERY = 21,
73 HISLIP_MSG_ASYNC_STATUS_RESPONSE = 22, HISLIP_MSG_ASYNC_DEVICE_CLEAR_ACKNOWLEDGE = 23,
74 HISLIP_MSG_ASYNC_LOCK_INFO = 24, HISLIP_MSG_ASYNC_LOCK_INFO_RESPONSE = 25,
75 HISLIP_MSG_GET_DESCRIPTORS = 26, HISLIP_MSG_GET_DESCRIPTORS_RESPONSE = 27, HISLIP_MSG_START_TLS = 28,
76 HISLIP_MSG_ASYNC_START_TLS = 29, HISLIP_MSG_ASYNC_START_TLS_RESPONSE = 30, HISLIP_MSG_END_TLS = 31,
77 HISLIP_MSG_ASYNC_END_TLS = 32, HISLIP_MSG_ASYNC_END_TLS_RESPONSE = 33,
78 HISLIP_MSG_GET_SASL_MECHANISM_LIST = 34, HISLIP_MSG_GET_SASL_MECHANISM_LIST_RESPONSE = 35,
79 HISLIP_MSG_AUTHENTICATION_START = 36, HISLIP_MSG_AUTHENTICATION_EXCHANGE = 37,
80 HISLIP_MSG_AUTHENTICATION_RESULT = 38, );
81
82/** @brief A decoded HiSLIP header. */
83typedef struct
84{
85 HislipMsg type;
86 uint8_t control; ///< ControlCode (message-specific flag; 0 when undefined)
87 uint32_t parameter; ///< MessageParameter (message-specific; 0 when undefined)
88 uint64_t payload_len; ///< byte length of the payload that follows the 16-byte header
89} HislipHeader;
90
91/**
92 * @brief Build the 16-byte header into @p buf.
93 * @return 16 (@ref PROTOCORE_HISLIP_HEADER_LEN), or 0 if @p cap < 16 or @p buf is null.
94 */
95size_t protocore_hislip_build_header(uint8_t *buf, size_t cap, HislipMsg type, uint8_t control, uint32_t parameter,
96 uint64_t payload_len);
97
98/**
99 * @brief Parse a 16-byte header from the head of [buf, buf+len).
100 * @return true on a valid `"HS"` prologue with @p len >= 16; false otherwise.
101 * @note The message type is copied through even if beyond 38 (forward-compat); the caller decides.
102 */
103embed_bool protocore_hislip_parse_header(const uint8_t *buf, size_t len, HislipHeader *out);
104
105// ── handshake builders ─────────────────────────────────────────────────────────────────────────
106
107/**
108 * @brief Build an Initialize message (client -> server, sync channel): parameter = (version << 16)
109 * | vendor_id, payload = the sub-address string (e.g. "hislip0").
110 * @return total bytes written (16 + sub-address length), or 0 on overflow / bad input.
111 */
112size_t protocore_hislip_build_initialize(uint8_t *buf, size_t cap, uint16_t protocol_version, uint16_t vendor_id,
113 const char *sub_address);
114
115/**
116 * @brief Build an InitializeResponse (server -> client): control (overlap / encryption bits),
117 * parameter = (negotiated version << 16) | session_id, no payload.
118 * @return 16, or 0 on overflow.
119 */
120size_t protocore_hislip_build_initialize_response(uint8_t *buf, size_t cap, uint8_t control, uint16_t protocol_version,
121 uint16_t session_id);
122
123/**
124 * @brief Build an AsyncInitialize (client -> server, async channel): parameter = session_id, no payload.
125 * @return 16, or 0 on overflow.
126 */
127size_t protocore_hislip_build_async_initialize(uint8_t *buf, size_t cap, uint16_t session_id);
128
129/**
130 * @brief Build an AsyncInitializeResponse (server -> client): parameter = server_vendor_id, no payload.
131 * @return 16, or 0 on overflow.
132 */
133size_t protocore_hislip_build_async_initialize_response(uint8_t *buf, size_t cap, uint8_t control,
134 uint16_t server_vendor_id);
135
136/**
137 * @brief Build a Data (@p is_end false) or DataEND (@p is_end true) message carrying @p payload
138 * keyed by @p message_id (parameter). @p control is usually 0 (set @ref
139 * PROTOCORE_HISLIP_DATA_RMT_DELIVERED on a server response after a terminator).
140 * @return total bytes written (16 + payload_len), or 0 on overflow / bad input.
141 */
142size_t protocore_hislip_build_data(uint8_t *buf, size_t cap, embed_bool is_end, uint8_t control, uint32_t message_id,
143 const uint8_t *payload, size_t payload_len);
144
145/** @brief The next client MessageID (increments by 2, wraps) - see @ref PROTOCORE_HISLIP_MESSAGE_ID_INIT. */
146uint32_t protocore_hislip_next_message_id(uint32_t id);
147
148// ── handshake parsers ──────────────────────────────────────────────────────────────────────────
149
150/** @brief A decoded Initialize message. @ref sub_address points INTO the source buffer. */
151typedef struct
152{
153 uint16_t protocol_version;
154 uint16_t vendor_id;
155 const char *sub_address;
156 size_t sub_address_len;
157} HislipInitialize;
158
159/**
160 * @brief Parse a full Initialize message (header + payload) from [buf, buf+len).
161 * @return true on a complete, well-formed Initialize; false otherwise.
162 */
163embed_bool protocore_hislip_parse_initialize(const uint8_t *buf, size_t len, HislipInitialize *out);
164
165/** @brief A decoded InitializeResponse message. */
166typedef struct
167{
168 uint16_t protocol_version;
169 uint16_t session_id;
170 embed_bool overlap; ///< ControlCode bit 0 (prefer overlapped)
171 embed_bool encryption_mandatory; ///< ControlCode bit 1 (2.0)
172} HislipInitializeResponse;
173
174/**
175 * @brief Parse an InitializeResponse header from [buf, buf+len).
176 * @return true on a well-formed InitializeResponse; false otherwise.
177 */
178embed_bool protocore_hislip_parse_initialize_response(const uint8_t *buf, size_t len, HislipInitializeResponse *out);
179
180EMBED_END_DECLS
181
182#endif // PROTOCORE_ENABLE_HISLIP
183
184#endif // PROTOCORE_HISLIP_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.