MLR_Modem
 
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
SerialModemBase Class Referenceabstract

Base class handling low-level serial I/O, debugging, and async transaction logic. More...

#include <SerialModemBase.h>

Inheritance diagram for SerialModemBase:
MLR_Modem

Public Member Functions

 SerialModemBase ()
 
virtual ~SerialModemBase ()=default
 
void setDebugStream (Stream *debugStream)
 Initializes the debug stream.
 
void update ()
 Main processing loop. Must be called frequently. Handles command queue, transmission, and response parsing.
 
bool isIdle () const
 Checks if the internal engine is currently idle (no command processing).
 
bool isLastCommandComplete () const
 Checks if the last processed command has finished. Useful for implementing synchronous wrappers.
 
ModemError getLastCommandResult () const
 Gets the result of the last processed command.
 
const uint8_t * getRxBuffer () const
 Accessor for the shared RX buffer.
 
uint16_t getRxIndex () const
 
bool isQueueFull () const
 Checks if the command queue is full.
 
bool isQueueEmpty () const
 Checks if the command queue is empty.
 
uint8_t getQueueCount () const
 Gets the number of commands currently in the queue.
 

Protected Member Functions

void initSerial (Stream &stream)
 Sets the UART stream to use for modem communication.
 
ModemError enqueueCommand (const char *cmd, CommandType type, uint32_t timeoutMs=1000)
 Enqueues a command for processing. Non-blocking.
 
ModemError enqueueTxCommand (const char *cmdHeader, const uint8_t *payload, uint8_t len, const char *suffix=nullptr, uint32_t timeoutMs=2000)
 Enqueues a data transmission command with payload.
 
ModemError setByteValue (const char *cmd, uint8_t value, bool save, const char *respPrefix, size_t respLen)
 Helper to set a 1-byte value (e.g., @CH0E) and verify the response.
 
ModemError getByteValue (const char *cmd, uint8_t *pValue, const char *respPrefix, size_t respLen)
 Helper to get a 1-byte value.
 
ModemError setBoolValue (const char *baseCmd, bool enabled, bool save, const char *respPrefix)
 Helper to set a boolean value (ON/OF).
 
ModemError getBoolValue (const char *cmd, bool *pValue, const char *respPrefix)
 Helper to get a boolean value (ON/OF).
 
ModemError sendRawCommand (const char *command, char *responseBuffer, size_t bufferSize, uint32_t timeoutMs)
 Sends a raw command string and fills the provided buffer with the response.
 
ModemError waitForSyncComplete (uint32_t timeoutMs)
 Helper to wait for the current command to finish (Pseudo-blocking). Calls update() internally.
 
void writeString (const char *str, bool printPrefix=true)
 
void writeData (const uint8_t *data, size_t len)
 
int readByte ()
 
void unreadByte (uint8_t c)
 
void clearUnreadByte ()
 
void flushGarbage (char keepChar=' *')
 
void startTimeout (uint32_t ms)
 
bool isTimeout ()
 
ModemError parseResponseHex (const uint8_t *buffer, size_t length, const char *prefix, uint8_t hexDigits, uint32_t *pResult)
 
ModemError parseResponseDec (const uint8_t *buffer, size_t length, const char *prefix, const char *suffix, size_t suffixLen, int32_t *pResult)
 
virtual ModemParseResult parse ()=0
 Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.
 
virtual void onRxDataReceived ()=0
 
virtual const char * getLogPrefix () const =0
 
virtual void onCommandComplete (ModemError result)
 Optional hook called when a queued command finishes.
 

Static Protected Member Functions

static bool parseHex (const uint8_t *pData, size_t len, uint32_t *pResult)
 
static bool parseDec (const uint8_t *pData, size_t len, uint32_t *pResult)
 
static char * appendStr (char *dest, const char *src, const char *destEnd)
 
static char * appendHex2 (char *dest, uint8_t val, const char *destEnd)
 
template<size_t N>
static char * appendStr (char(&destBuf)[N], char *currentPtr, const char *src)
 
template<size_t N>
static char * appendHex2 (char(&destBuf)[N], char *currentPtr, uint8_t val)
 

Protected Attributes

Stream * _uart = nullptr
 
Stream * _debugStream = nullptr
 
uint8_t _rxBuffer [RX_BUFFER_SIZE]
 
uint16_t _rxIndex = 0
 
int16_t _oneByteBuf = -1
 
bool _debugRxNewLine = true
 

Static Protected Attributes

static constexpr char CD_WRITE_OK_RESPONSE [] = "*WR=PS"
 
static constexpr size_t CD_WRITE_OK_RESPONSE_LEN = 6
 
static constexpr char CD_VAL_ON [] = "ON"
 
static constexpr char CD_VAL_OFF [] = "OF"
 
static constexpr char CD_CMD_WRITE_SUFFIX [] = "/W"
 
static constexpr size_t RX_BUFFER_SIZE = 300
 

Detailed Description

Base class handling low-level serial I/O, debugging, and async transaction logic.

Constructor & Destructor Documentation

◆ SerialModemBase()

SerialModemBase::SerialModemBase ( )

◆ ~SerialModemBase()

virtual SerialModemBase::~SerialModemBase ( )
virtualdefault

Member Function Documentation

◆ appendHex2() [1/2]

char * SerialModemBase::appendHex2 ( char *  dest,
uint8_t  val,
const char *  destEnd 
)
staticprotected

◆ appendHex2() [2/2]

template<size_t N>
static char * SerialModemBase::appendHex2 ( char(&)  destBuf[N],
char *  currentPtr,
uint8_t  val 
)
inlinestaticprotected

◆ appendStr() [1/2]

char * SerialModemBase::appendStr ( char *  dest,
const char *  src,
const char *  destEnd 
)
staticprotected

◆ appendStr() [2/2]

template<size_t N>
static char * SerialModemBase::appendStr ( char(&)  destBuf[N],
char *  currentPtr,
const char *  src 
)
inlinestaticprotected

◆ clearUnreadByte()

void SerialModemBase::clearUnreadByte ( )
protected

◆ enqueueCommand()

ModemError SerialModemBase::enqueueCommand ( const char *  cmd,
CommandType  type,
uint32_t  timeoutMs = 1000 
)
protected

Enqueues a command for processing. Non-blocking.

Returns
ModemError::Ok if queued, ModemError::Busy if queue is full.

◆ enqueueTxCommand()

ModemError SerialModemBase::enqueueTxCommand ( const char *  cmdHeader,
const uint8_t *  payload,
uint8_t  len,
const char *  suffix = nullptr,
uint32_t  timeoutMs = 2000 
)
protected

Enqueues a data transmission command with payload.

◆ flushGarbage()

void SerialModemBase::flushGarbage ( char  keepChar = '*')
protected

◆ getBoolValue()

ModemError SerialModemBase::getBoolValue ( const char *  cmd,
bool *  pValue,
const char *  respPrefix 
)
protected

Helper to get a boolean value (ON/OF).

  • Parameters
    cmdCommand string (e.g., "@RR").
    pValueOutput pointer for the value.
    respPrefixExpected response prefix.
    Returns
    ModemError::Ok on success.

◆ getByteValue()

ModemError SerialModemBase::getByteValue ( const char *  cmd,
uint8_t *  pValue,
const char *  respPrefix,
size_t  respLen 
)
protected

Helper to get a 1-byte value.

  • Parameters
    cmdCommand string (e.g., "@CH").
    pValueOutput pointer for the value.
    respPrefixExpected response prefix.
    respLenTotal length of expected response.
    Returns
    ModemError::Ok on success.

◆ getLastCommandResult()

ModemError SerialModemBase::getLastCommandResult ( ) const
inline

Gets the result of the last processed command.

◆ getLogPrefix()

virtual const char * SerialModemBase::getLogPrefix ( ) const
protectedpure virtual

Implemented in MLR_Modem.

◆ getQueueCount()

uint8_t SerialModemBase::getQueueCount ( ) const

Gets the number of commands currently in the queue.

◆ getRxBuffer()

const uint8_t * SerialModemBase::getRxBuffer ( ) const
inline

Accessor for the shared RX buffer.

◆ getRxIndex()

uint16_t SerialModemBase::getRxIndex ( ) const
inline

◆ initSerial()

void SerialModemBase::initSerial ( Stream &  stream)
protected

Sets the UART stream to use for modem communication.

Parameters
streamReference to the serial stream.

◆ isIdle()

bool SerialModemBase::isIdle ( ) const

Checks if the internal engine is currently idle (no command processing).

◆ isLastCommandComplete()

bool SerialModemBase::isLastCommandComplete ( ) const
inline

Checks if the last processed command has finished. Useful for implementing synchronous wrappers.

◆ isQueueEmpty()

bool SerialModemBase::isQueueEmpty ( ) const

Checks if the command queue is empty.

◆ isQueueFull()

bool SerialModemBase::isQueueFull ( ) const

Checks if the command queue is full.

◆ isTimeout()

bool SerialModemBase::isTimeout ( )
protected

◆ onCommandComplete()

virtual void SerialModemBase::onCommandComplete ( ModemError  result)
inlineprotectedvirtual

Optional hook called when a queued command finishes.

Reimplemented in MLR_Modem.

◆ onRxDataReceived()

virtual void SerialModemBase::onRxDataReceived ( )
protectedpure virtual

Implemented in MLR_Modem.

◆ parse()

virtual ModemParseResult SerialModemBase::parse ( )
protectedpure virtual

Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.

Returns
Current status of parsing.

Implemented in MLR_Modem.

◆ parseDec()

bool SerialModemBase::parseDec ( const uint8_t *  pData,
size_t  len,
uint32_t *  pResult 
)
staticprotected

◆ parseHex()

bool SerialModemBase::parseHex ( const uint8_t *  pData,
size_t  len,
uint32_t *  pResult 
)
staticprotected

◆ parseResponseDec()

ModemError SerialModemBase::parseResponseDec ( const uint8_t *  buffer,
size_t  length,
const char *  prefix,
const char *  suffix,
size_t  suffixLen,
int32_t *  pResult 
)
protected

◆ parseResponseHex()

ModemError SerialModemBase::parseResponseHex ( const uint8_t *  buffer,
size_t  length,
const char *  prefix,
uint8_t  hexDigits,
uint32_t *  pResult 
)
protected

◆ readByte()

int SerialModemBase::readByte ( )
protected

◆ sendRawCommand()

ModemError SerialModemBase::sendRawCommand ( const char *  command,
char *  responseBuffer,
size_t  bufferSize,
uint32_t  timeoutMs 
)
protected

Sends a raw command string and fills the provided buffer with the response.

  • Parameters
    commandCommand string.
    responseBufferOutput buffer.
    bufferSizeSize of output buffer.
    timeoutMsTimeout.
    Returns
    ModemError::Ok on success.

◆ setBoolValue()

ModemError SerialModemBase::setBoolValue ( const char *  baseCmd,
bool  enabled,
bool  save,
const char *  respPrefix 
)
protected

Helper to set a boolean value (ON/OF).

  • Parameters
    baseCmdBase command string (e.g., "@RR"). Appends "ON" or "OF".
    enabledTrue for ON, False for OFF.
    saveWhether to save to NVM (/W).
    respPrefixExpected response prefix (e.g., "*RR=").
    Returns
    ModemError::Ok if set and verified successfully.

◆ setByteValue()

ModemError SerialModemBase::setByteValue ( const char *  cmd,
uint8_t  value,
bool  save,
const char *  respPrefix,
size_t  respLen 
)
protected

Helper to set a 1-byte value (e.g., @CH0E) and verify the response.

  • Handles the sequence:
  1. Send CMD + Value + (Save ? /W : "")
  2. Wait for response
  3. If Save, handle intermediate "*WR=PS" and wait for final response
  4. Parse final response to verify the value matches.
  • Parameters
    cmdCommand string (e.g., "@CH").
    valueThe byte value to set.
    saveWhether to save to NVM (/W).
    respPrefixExpected response prefix (e.g., "*CH=").
    respLenTotal length of expected response.
    Returns
    ModemError::Ok if set and verified successfully.

◆ setDebugStream()

void SerialModemBase::setDebugStream ( Stream *  debugStream)

Initializes the debug stream.

Parameters
debugStreamPointer to the Stream for debug output (can be nullptr).

◆ startTimeout()

void SerialModemBase::startTimeout ( uint32_t  ms)
protected

◆ unreadByte()

void SerialModemBase::unreadByte ( uint8_t  c)
protected

◆ update()

void SerialModemBase::update ( )

Main processing loop. Must be called frequently. Handles command queue, transmission, and response parsing.

◆ waitForSyncComplete()

ModemError SerialModemBase::waitForSyncComplete ( uint32_t  timeoutMs)
protected

Helper to wait for the current command to finish (Pseudo-blocking). Calls update() internally.

◆ writeData()

void SerialModemBase::writeData ( const uint8_t *  data,
size_t  len 
)
protected

◆ writeString()

void SerialModemBase::writeString ( const char *  str,
bool  printPrefix = true 
)
protected

Member Data Documentation

◆ _debugRxNewLine

bool SerialModemBase::_debugRxNewLine = true
protected

◆ _debugStream

Stream* SerialModemBase::_debugStream = nullptr
protected

◆ _oneByteBuf

int16_t SerialModemBase::_oneByteBuf = -1
protected

◆ _rxBuffer

uint8_t SerialModemBase::_rxBuffer[RX_BUFFER_SIZE]
protected

◆ _rxIndex

uint16_t SerialModemBase::_rxIndex = 0
protected

◆ _uart

Stream* SerialModemBase::_uart = nullptr
protected

◆ CD_CMD_WRITE_SUFFIX

constexpr char SerialModemBase::CD_CMD_WRITE_SUFFIX = "/W"
staticconstexprprotected

◆ CD_VAL_OFF

constexpr char SerialModemBase::CD_VAL_OFF = "OF"
staticconstexprprotected

◆ CD_VAL_ON

constexpr char SerialModemBase::CD_VAL_ON = "ON"
staticconstexprprotected

◆ CD_WRITE_OK_RESPONSE

constexpr char SerialModemBase::CD_WRITE_OK_RESPONSE = "*WR=PS"
staticconstexprprotected

◆ CD_WRITE_OK_RESPONSE_LEN

constexpr size_t SerialModemBase::CD_WRITE_OK_RESPONSE_LEN = 6
staticconstexprprotected

◆ RX_BUFFER_SIZE

constexpr size_t SerialModemBase::RX_BUFFER_SIZE = 300
staticconstexprprotected

The documentation for this class was generated from the following files: