interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / net / wire / omtk.om

273 lines · 6122 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/net/wire/omtk.om: OMTK v1 binary wire frame codec and validator for Pi 5 <-> Spark.
3note Library unit: no fn main, no Linux syscalls, no heap. Prefix omk / OMK.
4note All multi-byte integers are unsigned little-endian. Fixed 16-byte header:
5note 0..3 u32 magic = 0x4B544D4F ("OMTK" LE = 1263816015)
6note 4 u8 ver = 1
7note 5 u8 op = 1..9 (PING,PONG,RESET_KV,PREFILL,DECODE,TOKEN,DONE,CANCEL,ERR)
8note 6..7 u16 flags = bit0 F_EOS (1), bit1 F_GREEDY (2); bits 2..15 must be 0
9note 8..11 u32 req_id = nonzero for PREFILL/DECODE/TOKEN/CANCEL; 0 allowed for PING/PONG/RESET_KV/DONE/ERR
10note 12..15 u32 payload_len = 0..16384 (OMK_MAXPAY)
11
12const OMK_HDR ulong 16
13const OMK_MAXPAY ulong 16384
14const OMK_MAXFRM ulong 16400
15const OMK_MAXTOK ulong 4096
16const OMK_MAGIC ulong 1263816015
17const OMK_VER ulong 1
18
19note Opcodes
20const OMK_PING ulong 1
21const OMK_PONG ulong 2
22const OMK_RESET ulong 3
23const OMK_PREFILL ulong 4
24const OMK_DECODE ulong 5
25const OMK_TOKEN ulong 6
26const OMK_DONE ulong 7
27const OMK_CANCEL ulong 8
28const OMK_ERR ulong 9
29
30note Flags (only bits 0..1 valid)
31const OMK_FEOS ulong 1
32const OMK_FGREEDY ulong 2
33const OMK_FMASK ulong 3
34
35note DONE reasons
36const OMK_RMAX ulong 0
37const OMK_REOS ulong 1
38const OMK_RCANCEL ulong 2
39const OMK_RPREF ulong 3
40const OMK_RRESET ulong 4
41
42note Error codes
43const OMK_OK ulong 0
44const OMK_EBADMAG ulong 1
45const OMK_EBADVER ulong 2
46const OMK_EBADOP ulong 3
47const OMK_EBADFLG ulong 4
48const OMK_ETOOBIG ulong 5
49const OMK_EBADLEN ulong 6
50const OMK_EBADREQ ulong 7
51const OMK_EBADTOK ulong 8
52const OMK_ERANGE ulong 9
53const OMK_EBUSY ulong 10
54const OMK_EUNSUP ulong 11
55const OMK_EGSTAGE ulong 12
56const OMK_EGKICK ulong 13
57const OMK_EGTMO ulong 14
58const OMK_EGHALT ulong 15
59const OMK_ESEM ulong 16
60const OMK_EGVER ulong 17
61const OMK_EFAULT ulong 18
62
63note Parsed header fields (omkhdr): 0 magic, 1 ver, 2 op, 3 flags, 4 req_id, 5 payload_len
64global omkhdr ulong 8
65note Scratch staging buffer for encoding/decoding single frames (16 B hdr + up to 16384 B payload)
66global omktx uchar 16400
67global omktxn ulong 1
68
69fn omk_r16 buf view uchar * off ulong to ulong
70a = ulong get buf off
71b = ulong get buf add off 1
72ret or a shl b 8
73end
74
75fn omk_r32 buf view uchar * off ulong to ulong
76a = ulong get buf off
77b = ulong get buf add off 1
78c = ulong get buf add off 2
79d = ulong get buf add off 3
80ret or or or a shl b 8 shl c 16 shl d 24
81end
82
83fn omk_w16 buf edit uchar * off ulong v ulong to void
84put buf off uchar and v 255
85put buf add off 1 uchar and shr v 8 255
86end
87
88fn omk_w32 buf edit uchar * off ulong v ulong to void
89put buf off uchar and v 255
90put buf add off 1 uchar and shr v 8 255
91put buf add off 2 uchar and shr v 16 255
92put buf add off 3 uchar and shr v 24 255
93end
94
95note Global-buffer LE helpers (for omktx and rx accumulators without passing globals)
96fn omk_txw16 off ulong v ulong to void
97put omktx off uchar and v 255
98put omktx add off 1 uchar and shr v 8 255
99end
100
101fn omk_txw32 off ulong v ulong to void
102put omktx off uchar and v 255
103put omktx add off 1 uchar and shr v 8 255
104put omktx add off 2 uchar and shr v 16 255
105put omktx add off 3 uchar and shr v 24 255
106end
107
108note Write 16-byte OMTK v1 header into omktx[0..16); sets omktxn = 16 + plen.
109fn omk_mkhdr op ulong flags ulong req_id ulong plen ulong to ulong
110if gt plen OMK_MAXPAY
111ret OMK_ETOOBIG
112end
113omk_txw32 0 OMK_MAGIC
114put omktx 4 uchar OMK_VER
115put omktx 5 uchar and op 255
116omk_txw16 6 and flags 65535
117omk_txw32 8 req_id
118omk_txw32 12 plen
119put omktxn 0 add OMK_HDR plen
120ret OMK_OK
121end
122
123note Validate header fields already unpacked into omkhdr[0..5]. Returns OMK_OK or OMK_E*.
124fn omk_chk_hdr to ulong
125mag = get omkhdr 0
126if ne mag OMK_MAGIC
127ret OMK_EBADMAG
128end
129ver = get omkhdr 1
130if ne ver OMK_VER
131ret OMK_EBADVER
132end
133op = get omkhdr 2
134if or lt op OMK_PING gt op OMK_ERR
135ret OMK_EBADOP
136end
137flg = get omkhdr 3
138if ne and flg not OMK_FMASK 0
139ret OMK_EBADFLG
140end
141req = get omkhdr 4
142plen = get omkhdr 5
143if gt plen OMK_MAXPAY
144ret OMK_ETOOBIG
145end
146if eq op OMK_PING
147if ne plen 0
148ret OMK_EBADLEN
149end
150ret OMK_OK
151end
152if eq op OMK_PONG
153if and ne plen 0 ne plen 16
154ret OMK_EBADLEN
155end
156ret OMK_OK
157end
158if eq op OMK_RESET
159if ne plen 4
160ret OMK_EBADLEN
161end
162ret OMK_OK
163end
164if eq op OMK_PREFILL
165if eq req 0
166ret OMK_EBADREQ
167end
168if or lt plen 4 ne rem plen 4 0
169ret OMK_EBADLEN
170end
171ret OMK_OK
172end
173if eq op OMK_DECODE
174if eq req 0
175ret OMK_EBADREQ
176end
177if ne plen 16
178ret OMK_EBADLEN
179end
180ret OMK_OK
181end
182if eq op OMK_TOKEN
183if eq req 0
184ret OMK_EBADREQ
185end
186if ne plen 8
187ret OMK_EBADLEN
188end
189ret OMK_OK
190end
191if eq op OMK_DONE
192if ne plen 8
193ret OMK_EBADLEN
194end
195ret OMK_OK
196end
197if eq op OMK_CANCEL
198if eq req 0
199ret OMK_EBADREQ
200end
201if ne plen 0
202ret OMK_EBADLEN
203end
204ret OMK_OK
205end
206if eq op OMK_ERR
207if ne plen 8
208ret OMK_EBADLEN
209end
210ret OMK_OK
211end
212ret OMK_EBADOP
213end
214
215note Parse 16-byte header from a view buffer at offset off into omkhdr[0..5] and validate.
216fn omk_parse_hdr buf view uchar * off ulong to ulong
217if gt off len buf
218ret OMK_EBADLEN
219end
220if lt sub len buf off OMK_HDR
221ret OMK_EBADLEN
222end
223put omkhdr 0 omk_r32 buf off
224put omkhdr 1 ulong get buf add off 4
225put omkhdr 2 ulong get buf add off 5
226put omkhdr 3 omk_r16 buf add off 6
227put omkhdr 4 omk_r32 buf add off 8
228put omkhdr 5 omk_r32 buf add off 12
229ret omk_chk_hdr
230end
231
232note Encode standard Spark -> Pi replies into omktx[0..omktxn).
233fn omk_enc_pong req_id ulong st ulong kv ulong mctx ulong gerr ulong to ulong
234rc = omk_mkhdr OMK_PONG 0 req_id 16
235if ne rc OMK_OK
236ret rc
237end
238omk_txw32 16 st
239omk_txw32 20 kv
240omk_txw32 24 mctx
241omk_txw32 28 gerr
242ret OMK_OK
243end
244
245fn omk_enc_token req_id ulong flags ulong seq ulong tok ulong to ulong
246rc = omk_mkhdr OMK_TOKEN flags req_id 8
247if ne rc OMK_OK
248ret rc
249end
250omk_txw32 16 seq
251omk_txw32 20 tok
252ret OMK_OK
253end
254
255fn omk_enc_done req_id ulong flags ulong kv ulong reason ulong to ulong
256rc = omk_mkhdr OMK_DONE flags req_id 8
257if ne rc OMK_OK
258ret rc
259end
260omk_txw32 16 kv
261omk_txw32 20 reason
262ret OMK_OK
263end
264
265fn omk_enc_err req_id ulong ecode ulong detail ulong to ulong
266rc = omk_mkhdr OMK_ERR 0 req_id 8
267if ne rc OMK_OK
268ret rc
269end
270omk_txw32 16 ecode
271omk_txw32 20 detail
272ret OMK_OK
273end