/* transport_stdio.c — host transport over stdin/stdout (docs/flow.md §3). * * read() must never block, so stdin is put into non-blocking mode. This is * the whole of what the Pico USB CDC transport will have to provide too. */ #include "rubo/rubo.h" #include #include #include #include static int stdio_open(rubo_transport_t *t) { (void)t; int fl = fcntl(STDIN_FILENO, F_GETFL, 0); if (fl < 0) return -1; return fcntl(STDIN_FILENO, F_SETFL, fl | O_NONBLOCK); } static int stdio_read(rubo_transport_t *t, uint8_t *buf, size_t len) { (void)t; ssize_t n = read(STDIN_FILENO, buf, len); if (n < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) return 0; return -1; } if (n == 0) return -1; /* EOF */ return (int)n; } static int stdio_write(rubo_transport_t *t, const uint8_t *buf, size_t len) { (void)t; size_t off = 0; while (off < len) { ssize_t n = write(STDOUT_FILENO, buf + off, len - off); if (n <= 0) return -1; off += (size_t)n; } return (int)len; } static bool stdio_connected(rubo_transport_t *t) { (void)t; return true; } static void stdio_close(rubo_transport_t *t) { (void)t; } rubo_transport_t *rubo_transport_stdio(void) { static rubo_transport_t t = { .open = stdio_open, .read = stdio_read, .write = stdio_write, .connected = stdio_connected, .close = stdio_close, .mtu = 256, .ctx = NULL, }; return &t; }