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
omega-mv / src / physics0 / pd0_chan.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Learner-side channel to the PD-0 world (spec 2.3, 2.4): request bytes out, |
| 18 | * record bytes in, over a pipe to the world process or an in-process |
| 19 | * function. Holds no generator knowledge and opens no file (G5 / I10). */ |
| 20 | #include "physics0/pd0_world.h" |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | int pd0_read_full(int fd, uint8_t *buf, size_t n) { |
| 25 | size_t got = 0; |
| 26 | while (got < n) { |
| 27 | ssize_t k = read(fd, buf + got, n - got); |
| 28 | if (k <= 0) return -1; |
| 29 | got += (size_t)k; |
| 30 | } |
| 31 | return 0; |
| 32 | } |
| 33 | int pd0_write_full(int fd, const uint8_t *buf, size_t n) { |
| 34 | size_t put = 0; |
| 35 | while (put < n) { |
| 36 | ssize_t k = write(fd, buf + put, n - put); |
| 37 | if (k <= 0) return -1; |
| 38 | put += (size_t)k; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static size_t chan_call(pd0_chan *c, const uint8_t *req, size_t len, uint8_t *resp, size_t cap) { |
| 44 | if (c->fn) return c->fn(c->ctx, req, len, resp, cap); |
| 45 | uint8_t len4[4]; |
| 46 | pd0_put_u32(len4, (uint32_t)len); |
| 47 | if (pd0_write_full(c->out_fd, len4, 4) != 0 || pd0_write_full(c->out_fd, req, len) != 0) return 0; |
| 48 | if (pd0_read_full(c->in_fd, len4, 4) != 0) return 0; |
| 49 | uint32_t n = pd0_get_u32(len4); |
| 50 | if (n == 0 || n > cap) return 0; |
| 51 | if (pd0_read_full(c->in_fd, resp, n) != 0) return 0; |
| 52 | return n; |
| 53 | } |
| 54 | |
| 55 | int pd0_chan_describe(pd0_chan *c, pd0_desc *d) { |
| 56 | uint8_t req[PD0_REQ_MAX], resp[PD0_DESC_MAX]; |
| 57 | size_t n = chan_call(c, req, pd0_req_describe(req), resp, sizeof resp); |
| 58 | return n ? pd0_desc_decode(resp, n, d) : -1; |
| 59 | } |
| 60 | int pd0_chan_reset(pd0_chan *c, uint8_t n_obs, const int64_t *vals, pd0_rec *r) { |
| 61 | uint8_t req[PD0_REQ_MAX], resp[PD0_REC_MAX]; |
| 62 | if (!c || !vals || !r) return -1; |
| 63 | size_t req_len = pd0_req_reset(n_obs, vals, req, sizeof req); |
| 64 | if (req_len == 0 || req_len > sizeof req) return -1; |
| 65 | size_t n = chan_call(c, req, req_len, resp, sizeof resp); |
| 66 | return n ? pd0_rec_decode(resp, n, r) : -1; |
| 67 | } |
| 68 | int pd0_chan_step(pd0_chan *c, uint8_t channel, int64_t value, pd0_rec *r) { |
| 69 | uint8_t req[PD0_REQ_MAX], resp[PD0_REC_MAX]; |
| 70 | size_t n = chan_call(c, req, pd0_req_step(channel, value, req), resp, sizeof resp); |
| 71 | return n ? pd0_rec_decode(resp, n, r) : -1; |
| 72 | } |