#include "eeprom.hpp" #include #include #include #include #include #include #include namespace eeprom { namespace { std::string code_name(Code code) { switch (code) { case Code::OK: return "OK"; case Code::RANGE: return "RANGE"; case Code::EXISTS: return "EXISTS"; case Code::ABSENT: return "ABSENT"; case Code::BUSY: return "BUSY"; case Code::TIMEOUT: return "TIMEOUT"; case Code::SYNTAX: return "SYNTAX"; case Code::LIMIT: return "LIMIT"; } return "INVALID(" + std::to_string(static_cast(code)) + ")"; } std::string bytes_text(const std::vector& values) { if (values.empty()) { return "-"; } const char* digits = "0123456789abcdef"; std::string text; bool all_bytes = true; for (Number value : values) { if (value < 0 || value > 255) { all_bytes = false; } } if (!all_bytes) { text = "["; for (std::size_t i = 0; i < values.size(); ++i) { if (i != 0) { text += ','; } text += std::to_string(values[i]); } text += ']'; return text; } for (Number value : values) { text += digits[value / 16]; text += digits[value % 16]; } return text; } void emit(std::ostream& output, const std::string& line) { output.write(line.data(), static_cast(line.size())); output.put('\n'); } bool number(const std::string& text, Number& value) { if (text.empty()) { return false; } const char* begin = text.data(); const char* end = begin + text.size(); auto parsed = std::from_chars(begin, end, value, 10); return parsed.ec == std::errc() && parsed.ptr == end; } int nibble(char character) { if (character >= '0' && character <= '9') { return character - '0'; } if (character >= 'a' && character <= 'f') { return character - 'a' + 10; } if (character >= 'A' && character <= 'F') { return character - 'A' + 10; } return -1; } bool decode(const std::string& text, std::vector& values) { values.clear(); if (text == "-") { return true; } if (text.empty() || text.size() % 2 != 0 || text.size() > 512) { return false; } for (std::size_t i = 0; i < text.size(); i += 2) { int high = nibble(text[i]); int low = nibble(text[i + 1]); if (high < 0 || low < 0) { return false; } values.push_back(high * 16 + low); } return true; } std::vector words(const std::string& line) { std::vector result; std::size_t position = 0; while (position < line.size()) { if (line[position] == ' ' || line[position] == '\t') { ++position; continue; } std::size_t start = position; while (position < line.size() && line[position] != ' ' && line[position] != '\t') { ++position; } result.push_back(line.substr(start, position - start)); } return result; } Reply syntax() { return {Code::SYNTAX, 0, {}}; } void dispatch(Bus& bus, const std::vector& fields, std::ostream& output) { if (fields.empty()) { print_reply(output, syntax()); return; } const std::string& command = fields[0]; std::size_t arity = 0; if (command == "M") arity = 4; else if (command == "U" || command == "P" || command == "I") arity = 2; else if (command == "S" || command == "W" || command == "R") arity = 3; else if (command == "T") arity = 5; if (arity == 0 || fields.size() != arity) { print_reply(output, syntax()); return; } Number id = 0; if (!number(fields[1], id)) { print_reply(output, syntax()); return; } Number first = 0; Number second = 0; std::vector values; if (command == "M") { if (!number(fields[2], first) || !number(fields[3], second)) { print_reply(output, syntax()); return; } print_reply(output, bus.mount(id, first, second)); return; } if (command == "U") { print_reply(output, bus.unmount(id)); return; } if (command == "P") { print_reply(output, bus.poll(id)); return; } if (command == "I") { print_snapshot(output, bus.inspect(id)); return; } if (command == "S" || command == "R") { if (!number(fields[2], first)) { print_reply(output, syntax()); return; } Reply reply = command == "S" ? bus.seek(id, first) : bus.read(id, first); print_reply(output, reply); return; } if (command == "W") { if (!decode(fields[2], values)) { print_reply(output, syntax()); return; } print_reply(output, bus.page(id, values)); return; } if (!number(fields[2], first) || !number(fields[3], second) || !decode(fields[4], values)) { print_reply(output, syntax()); return; } print_reply(output, bus.transfer(id, first, values, second)); } } void print_reply(std::ostream& out, const Reply& reply) { std::string line = code_name(reply.code); line += ' '; line += std::to_string(reply.completed); line += ' '; line += bytes_text(reply.data); emit(out, line); } void print_snapshot(std::ostream& out, const Snapshot& snapshot) { std::string line = code_name(snapshot.code); line += ' ' + std::to_string(snapshot.cursor); line += ' ' + std::to_string(snapshot.busy); line += ' ' + std::to_string(snapshot.accepted); line += ' ' + std::to_string(snapshot.committed); line += ' ' + std::to_string(snapshot.polls); line += ' ' + std::to_string(snapshot.pending_start); line += ' ' + bytes_text(snapshot.pending); line += ' ' + bytes_text(snapshot.bytes); emit(out, line); } int run_cli(Bus& bus, std::istream& input, std::ostream& output) { std::string line; std::size_t physical = 0; while (std::getline(input, line)) { ++physical; if (physical > 4096) { print_reply(output, {Code::LIMIT, 0, {}}); continue; } if (line.size() > 2048 || line.find('\0') != std::string::npos || line.find('\r') != std::string::npos) { print_reply(output, syntax()); continue; } dispatch(bus, words(line), output); } return 0; } }