#include "edge.hpp" #include #include #include #include #include #include #include #include namespace edge { namespace { std::vector split(const std::string& line) { std::vector tokens; std::string token; for (unsigned char byte : line) { if (byte == ' ' || byte == '\t' || byte == '\r') { if (!token.empty()) { tokens.push_back(token); token.clear(); } } else { token.push_back(static_cast(byte)); } } if (!token.empty()) { tokens.push_back(token); } return tokens; } bool integer(const std::string& token, std::int64_t& output) { if (token.empty()) { return false; } std::size_t cursor = 0; bool negative = false; if (token[0] == '+' || token[0] == '-') { negative = token[0] == '-'; ++cursor; } if (cursor == token.size()) { return false; } const std::uint64_t positive = static_cast(std::numeric_limits::max()); const std::uint64_t limit = negative ? positive + 1 : positive; std::uint64_t magnitude = 0; for (; cursor < token.size(); ++cursor) { const unsigned char byte = static_cast(token[cursor]); if (byte < '0' || byte > '9') { return false; } const std::uint64_t digit = byte - '0'; if (magnitude > (limit - digit) / 10) { return false; } magnitude = magnitude * 10 + digit; } if (negative && magnitude == positive + 1) { output = std::numeric_limits::min(); } else if (negative) { output = -static_cast(magnitude); } else { output = static_cast(magnitude); } return true; } std::string hex(const std::string& input) { if (input.empty()) { return "-"; } const char digits[] = "0123456789abcdef"; std::string output; for (unsigned char byte : input) { output.push_back(digits[byte >> 4]); output.push_back(digits[byte & 15]); } return output; } void render(std::ostream& output, const Reply& reply) { if (!reply.error.empty()) { output << "ERR " << reply.error << '\n'; } else { output << "OK " << reply.number << ' ' << hex(reply.bytes) << '\n'; } } void render_pin(std::ostream& output, const PinView& view) { if (!view.error.empty()) { output << "ERR " << view.error << '\n'; } else { output << "PIN " << view.value << ' ' << view.mode << ' ' << view.changes << '\n'; } } void render_fd(std::ostream& output, const FdView& view) { if (!view.error.empty()) { output << "ERR " << view.error << '\n'; } else { output << "FD " << view.pin << ' ' << view.cursor << ' ' << (view.pending ? 1 : 0) << '\n'; } } int arity(const std::string& verb) { if (verb == "config" || verb == "drive" || verb == "open") { return 2; } if (verb == "seek" || verb == "read" || verb == "poll") { return 2; } if (verb == "close" || verb == "pin" || verb == "fd") { return 1; } return -1; } void dispatch(Bank& bank, const std::vector& tokens, std::ostream& output) { if (tokens.empty()) { output << "SYNTAX\n"; return; } const int expected = arity(tokens.front()); if (expected < 0 || tokens.size() != static_cast(expected + 1)) { output << "SYNTAX\n"; return; } std::int64_t first = 0; std::int64_t second = 0; if (!integer(tokens[1], first)) { output << "SYNTAX\n"; return; } if (expected == 2 && !integer(tokens[2], second)) { output << "SYNTAX\n"; return; } const auto& verb = tokens.front(); if (verb == "config") { render(output, bank.configure(first, second)); } else if (verb == "drive") { render(output, bank.drive(first, second)); } else if (verb == "open") { render(output, bank.open(first, second)); } else if (verb == "close") { render(output, bank.close(first)); } else if (verb == "seek") { render(output, bank.seek(first, second)); } else if (verb == "read") { render(output, bank.read(first, second)); } else if (verb == "poll") { render(output, bank.poll(first, second)); } else if (verb == "pin") { render_pin(output, bank.pin(first)); } else if (verb == "fd") { render_fd(output, bank.descriptor(first)); } } } CliResult run_cli(std::istream& input, std::ostream& output) { Bank bank; int accepted = 0; std::string line; while (std::getline(input, line)) { if (accepted == 64) { output.write("LIMIT\n", 6); break; } ++accepted; std::ostringstream reply; reply.imbue(std::locale::classic()); dispatch(bank, split(line), reply); const std::string bytes = reply.str(); output.write(bytes.data(), static_cast(bytes.size())); } CliResult result; result.lines = accepted; for (int pin = 0; pin < 8; ++pin) { result.pins[pin] = bank.pin(pin); } for (int fd = 0; fd < 16; ++fd) { result.descriptors[fd] = bank.descriptor(fd); } return result; } }