// 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 WII5SerialManager.h * @brief Serial-port helper: AT-response parsing, command queue. */ #ifndef WII5SerialManager_h #define WII5SerialManager_h #include #include #include #include #include #include #include // TODO Really? #define WII5SERIALMAANGER_FIELD_SIZE 25 /* WII5SerialManager - Base class for managed serial ports Special and Shared Types WII5SERIAL_LAST What was the last message we received WII5SERIAL_PARSERS Which parser we currently using? */ enum WII5SERIAL_PARSERS { WII5SERIALPARSER_NONE, WII5SERIALPARSER_LINE, WII5SERIALPARSER_BINARY1, // likely to have multiple binary fomrats, this one, has the length in the first 2 bytes // and last 2 bytes are checksum WII5SERIALPARSER_RECORD, WII5SERIALPARSER_OKHUH, WII5SERIALPARSER_AT }; enum WII5SERIAL_LAST { WII5SERIALLAST_NONE, WII5SERIALLAST_LINE, WII5SERIALLAST_RECORD, WII5SERIALLAST_OK, WII5SERIALLAST_READY, WII5SERIALLAST_ERROR, WII5SERIALLAST_ATOK, // OK WII5SERIALLAST_ATERR, WII5SERIALLAST_ATSBDI, // including +SBDI and +SBDIX WII5SERIALLAST_BOOTVERSION, WII5SERIALLAST_NUMBER, WII5SERIALLAST_ATCSQ // including +CSQ }; class WII5SerialManager { public: WII5SerialManager() { gps = NULL; } virtual WII5_DRIVERS driverId(); virtual void loop(); void beginSerialManager(); void endSerialManager(); void setPassthrough(bool in); bool getPassthrough(); void setDebug(bool in); bool getDebug(); void setCapture(bool in); bool getCapture(); void repl(uint32_t baud = 0); virtual void sendNewLine(); WII5_SERIALCMDS sendAll(WII5SERIAL_LAST lastUntil = WII5SERIALLAST_OK); WII5_SERIALCMDS sendAndWait(WII5SERIAL_LAST lastUntil = WII5SERIALLAST_OK); virtual void setBaudrate(uint32_t baud); virtual void start(bool force = false); virtual void stop(bool force = false); virtual void setRecords(uint32_t in); virtual uint32_t getRecords(); virtual void writePassthrough(); virtual void writeCapture(); // Public to allow easy access??? TinyGPSPlus *gps; void enableNmea(); // virtual void displaySTATS(); virtual void sendAtDataLine(); void setTimeout(uint32_t in); uint32_t getTimeout(); char* buffer; char* binBuffer; uint16_t binBufferCount; uint16_t binBufferExpect; protected: // Serial stream // TODO Stream or HardwareSerial HardwareSerial *stream; // Must be implemented in inherited class ! bool capture; // Capture to file - see openFile and closeFile bool passthrough; // Passthrough to Serial console bool debug; // Extra debugging to Serial Console uint32_t sendCount; virtual bool processSerial(char in); virtual void processBufferField(char *buf, uint8_t len); virtual void processBufferOK(); virtual void processBufferLine(); virtual void processRecord(); elapsedMillis processWait; uint32_t processTimeout; virtual void programLine(uint16_t l); virtual void sendLine(uint16_t l); virtual void consolePrefix(bool input = false); virtual void consoleBuffer(bool input = false, bool useLog = false); virtual void setBuffer(char* buf, uint8_t sz); uint8_t bufMax; char fieldBuffer[WII5SERIALMAANGER_FIELD_SIZE]; // TODO Configuration etc uint8_t bufCount; uint8_t fieldLoc; uint8_t fieldCount; WII5SERIAL_PARSERS processMode; uint8_t programCount; uint8_t programTotal; uint32_t recordCount; uint32_t recordTotal; WII5SERIAL_LAST last; uint32_t lastVal; uint32_t recordVals[WII5_FIELD_MAX]; char *p; }; #endif