// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2012-2024 Scott Penrose and WII5 Buoy contributors // // This file is part of WII5 Buoy firmware. // See LICENSE for full terms. /** * @file WII5Commands.h * @brief Command protocol handler: parses and dispatches @-prefixed commands. */ #ifndef WII5Commands_h #define WII5Commands_h #include #include /** * @brief Text-protocol command parser and dispatcher. * * Accepts commands from multiple inputs (local console, Iridium SBD, * radio, buttons) in two syntaxes: * - "@AREA,cmd,arg1,arg2,..." (the AT-style protocol) * - "X;" single-letter "standard" style (used in manual test mode) * * Records the most recent command in lastCommand* members so result and * acknowledge replies can refer back to it. */ class WII5Commands : public WII5 { public: virtual WII5_CONTROLLERS controllerId() {return WII5CONTROLLER_COMMANDS;} /** @brief One-time bring-up. */ void begin(); /** @brief Feed an AT-style command into the command pipeline as if it came from the console. */ bool injectCommand(char *sendCmd); /** @brief Drain pending "X;" single-letter commands from the console. */ bool processConsoleStandard(); /** @brief Drain pending "@AREA,cmd,..." AT-style commands from the console. */ bool processConsoleAT(); /** @brief Translate physical button events (click/long-hold) into commands. */ bool processButtons(); /** @brief Drain pending packets from the LoRa network (if enabled). */ bool processNetwork(); /** @brief Process any incoming BinData payload (typically from Iridium). */ bool processBinData(); /** @brief Execute a parsed command; returns true on success. */ bool runCommand(WII5_COMMANDS cmd, char** params = NULL, uint8_t paramsLen = 0); /** @brief Record the source/area of the command currently being processed. */ void setupLast(WII5_DEVICES device, WII5_PORTS port, WII5_AREAS area, WII5_COMMANDS cmd = WII5COMMAND_NONE); /** @brief Finalize lastCommand bookkeeping (call after runCommand). */ void doneLast(); /** @brief Set the result + message for the last command (mutable buffer). */ void resultLast(WII5_RESULTS result, char *message); /** @brief Set the result + message for the last command (PROGMEM string). */ void resultLast(WII5_RESULTS result, const __FlashStringHelper *area); /** @brief Set the result for the last command without altering the message. */ void resultLast(WII5_RESULTS result); /** @brief Print a debug dump of command state to the console. */ void dump(); elapsedMillis disableButtons; // ------------------------------------------------------------------------------ // lastCommand! // When? elapsedMillis lastCommandAge; time_t lastCommandT; // Who sent this? WII5_PORTS lastCommandPort; WII5_DEVICES lastCommandDevice; // Which area and commands was parsed (e.g. area=WII5) WII5_AREAS lastCommandArea; // Main command - e.g. SETTINGS_DEVICEID WII5_COMMANDS lastCommandCmd; uint32_t lastCommandParam1; // Keep just the params in int format - should be enough uint32_t lastCommandParam2; // Keep just the params in int format - should be enough uint32_t lastCommandParam3; // Keep just the params in int format - should be enough // Result of command - success = yes/no WII5_RESULTS lastCommandResult; char lastCommandMessage[WII5_RESULT_MESSAGE]; // Error string // Acknowldge reply bool lastCommandAckComplete; protected: elapsedMillis delayWait; time_t when; // ------------------------------------------------------------------------------ // Button managmement elapsedMillis buttonHeld; elapsedMillis buttonReleaseWait; bool buttonActive; bool buttonReleased; uint8_t buttonMode; // ------------------------------------------------------------------------------ }; extern WII5Commands wii5Commands; #endif