aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / line_rx.h
| 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 | /* line_rx.h -- the operator line assembler of the service lifecycle: one |
| 18 | * bounded state machine fed byte by byte from the POLLED console receive |
| 19 | * (core/console.c, core/uart_rx.h). Pure logic (no kernel calls, no libc): |
| 20 | * the host test tests/test_console_rx.c compiles it as is; core/lifecycle.c |
| 21 | * uses it for the main prompt and for the cancel poll between generation |
| 22 | * steps, so both follow the same rules. |
| 23 | * |
| 24 | * Rules (feed one byte, or CK_LINE_RX_ERR for a byte the UART dropped): |
| 25 | * - printable ASCII 0x20..0x7e is collected, up to CK_LC_LINE_MAX bytes; |
| 26 | * - Backspace (0x08) or DEL (0x7f) removes the last byte; |
| 27 | * - CR or LF ends the line (a CR LF pair gives one line and one empty line); |
| 28 | * - any other control byte is dropped and counted; |
| 29 | * - a line past CK_LC_LINE_MAX is refused whole at its Enter (OVERLONG), |
| 30 | * the rest of it is dropped until then; |
| 31 | * - a line during which the UART reported a lost or damaged byte is |
| 32 | * refused whole at its Enter (DAMAGED): a command with a missing byte |
| 33 | * never runs. If the lost byte was the Enter itself, the next line is |
| 34 | * joined to the damaged one and refused with it (safe side). */ |
| 35 | #ifndef AIENOS_CK_LINE_RX_H |
| 36 | #define AIENOS_CK_LINE_RX_H |
| 37 | |
| 38 | #include <stddef.h> |
| 39 | #include <stdint.h> |
| 40 | #include "lifecycle_fsm.h" |
| 41 | |
| 42 | #define CK_LINE_RX_ERR (-2) /* same value as ck_console_rx_poll's error result */ |
| 43 | #define CK_LINE_RX_EMPTY (-1) |
| 44 | /* Bytes taken per poll call at most, so a UART that never runs dry (a stuck |
| 45 | * data-ready bit) cannot hold the lifecycle in one call. */ |
| 46 | #define CK_LINE_RX_BURST 256u |
| 47 | |
| 48 | enum ck_line_ev { |
| 49 | CK_LINE_NONE = 0, /* nothing to show */ |
| 50 | CK_LINE_ECHO, /* one byte appended: buf[len - 1] */ |
| 51 | CK_LINE_ERASE, /* one byte removed */ |
| 52 | CK_LINE_DONE, /* buf[0..len) is a complete line, NUL-terminated */ |
| 53 | CK_LINE_OVERLONG, /* the line ended and is refused: longer than CK_LC_LINE_MAX */ |
| 54 | CK_LINE_DAMAGED, /* the line ended and is refused: a byte was lost or damaged */ |
| 55 | }; |
| 56 | |
| 57 | struct ck_line_rx { |
| 58 | char buf[CK_LC_LINE_MAX + 1]; |
| 59 | size_t len; |
| 60 | uint8_t overflow, damaged, done; |
| 61 | unsigned overlong; /* lines refused for length */ |
| 62 | unsigned damaged_lines; /* lines refused for a receive error */ |
| 63 | unsigned dropped; /* control bytes dropped */ |
| 64 | }; |
| 65 | |
| 66 | void ck_line_rx_init(struct ck_line_rx *l); |
| 67 | enum ck_line_ev ck_line_rx_feed(struct ck_line_rx *l, int b); |
| 68 | |
| 69 | /* The cancel check between generation steps: drains at most |
| 70 | * CK_LINE_RX_BURST bytes from poll() (no echo) and returns 1 as soon as a |
| 71 | * complete line decodes to `cancel`, 0 when the input ran dry or the burst |
| 72 | * ended. Every other complete line (a verb other than cancel, an overlong or |
| 73 | * damaged line) is refused: refuse(verb, reason) is called and the line is |
| 74 | * never run. Empty lines are ignored. A partial line stays in l for the |
| 75 | * next call. */ |
| 76 | int ck_line_rx_cancel_poll(struct ck_line_rx *l, int (*poll)(void), void (*refuse)(enum ck_lc_verb v, const char *reason)); |
| 77 | |
| 78 | #endif |