#include "tx.h" #include #include #include #include #include #include #include struct command { char op; int count; char *args[5]; }; static int decimal(const char *text, int *out) { if (!text || !*text) return 0; const char *digits = text; if (*digits == '+' || *digits == '-') ++digits; if (!*digits) return 0; for (const char *p = digits; *p; ++p) { if (*p < '0' || *p > '9') return 0; } errno = 0; char *end; long value = strtol(text, &end, 10); if (errno || *end || value < INT_MIN || value > INT_MAX) return 0; *out = (int)value; return 1; } static int hex_digit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; } static int payload(const char *text, uint8_t *data, int *length) { size_t n = strlen(text); if (!n || n % 2 || n > TX_CAPACITY * 2) return 0; for (size_t i = 0; i < n; i += 2) { int high = hex_digit(text[i]); int low = hex_digit(text[i + 1]); if (high < 0 || low < 0) return 0; data[i / 2] = (uint8_t)(high * 16 + low); } *length = (int)(n / 2); return 1; } static int tokenize(char *line, struct command *c) { char *p = line; while (isspace((unsigned char)*p)) ++p; if (!*p || *p == '#') return 0; c->op = *p++; c->count = 0; if (*p && !isspace((unsigned char)*p)) return -1; while (*p) { while (isspace((unsigned char)*p)) ++p; if (!*p) break; if (c->count == 5) return -1; c->args[c->count++] = p; while (*p && !isspace((unsigned char)*p)) ++p; if (*p) *p++ = '\0'; } return 1; } static void print_data(const uint8_t *data, size_t length) { if (!length) { putchar('-'); return; } for (size_t i = 0; i < length; ++i) printf("%02x", (unsigned)data[i]); } static void print_stats(const struct tx_engine *e) { struct tx_stats s; tx_statistics(e, &s); printf("STATS %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %u %u\n", s.begins, s.writes, s.submits, s.completed, s.failures, s.cancelled, s.bytes, s.drafts, s.pending); } static void print_frame(const struct tx_engine *e, int id) { struct tx_frame f; int rc = tx_inspect(e, id, &f); printf("FRAME %d %d %d %zu %zu\n", rc, f.state, f.slot, f.length, f.covered); } static void print_device(const struct tx_engine *e, int id) { const struct port_descriptor *found = NULL; int rc = TX_ABSENT; if (id < 1 || id > 65535) rc = TX_INVALID; else { for (int i = 0; i < TX_SLOTS; ++i) { if (e->port.queue[i].active && e->port.queue[i].id == id) found = &e->port.queue[i]; } } if (!found) { printf("DEVICE %d 0 0 -\n", rc); return; } printf("DEVICE 0 %d %zu ", id, found->length); print_data(e->port.memory[found->slot], found->length); putchar('\n'); } static int numeric_args(const struct command *c, int count, int *values) { if (c->count != count) return 0; for (int i = 0; i < count; ++i) { if (!decimal(c->args[i], &values[i])) return 0; } return 1; } static int execute(struct tx_engine *e, const struct command *c) { int a[4]; int rc; uint8_t bytes[TX_CAPACITY]; int length; switch (c->op) { case 'R': if (!numeric_args(c, 1, a)) return 0; rc = tx_init(e, a[0]); break; case 'B': if (!numeric_args(c, 2, a)) return 0; rc = tx_begin(e, a[0], a[1]); break; case 'W': if (c->count != 3 || !decimal(c->args[0], &a[0]) || !decimal(c->args[1], &a[1]) || !payload(c->args[2], bytes, &length)) return 0; rc = tx_write(e, a[0], a[1], bytes, length); break; case 'F': if (!numeric_args(c, 4, a)) return 0; rc = tx_fill(e, a[0], a[1], a[2], a[3]); break; case 'S': case 'X': case 'P': case 'D': if (!numeric_args(c, 1, a)) return 0; if (c->op == 'P') { print_frame(e, a[0]); return 1; } if (c->op == 'D') { print_device(e, a[0]); return 1; } rc = c->op == 'S' ? tx_send(e, a[0]) : tx_cancel(e, a[0]); break; case 'C': { if (!numeric_args(c, 2, a)) return 0; struct tx_completion completion; rc = tx_complete(e, a[0], a[1], &completion); printf("COMPLETE %d %d %zu ", rc, completion.id, completion.length); print_data(completion.data, completion.length); putchar('\n'); return 1; } case 'Q': if (c->count) return 0; print_stats(e); return 1; default: return 0; } printf("RESULT %c %d\n", c->op, rc); return 1; } int main(void) { struct tx_engine engine; if (tx_init(&engine, 32) != TX_OK) return 2; char line[2048]; while (fgets(line, sizeof(line), stdin)) { struct command c; int parsed = tokenize(line, &c); if (parsed < 0 || (parsed > 0 && !execute(&engine, &c))) puts("ERROR"); } if (ferror(stdin) || ferror(stdout)) return 2; return 0; }