diff --git a/components/comms/CommsChannelMsg/CommsChannelMsg.h b/components/comms/CommsChannelMsg/CommsChannelMsg.h index 64bdd919..7bdf3145 100644 --- a/components/comms/CommsChannelMsg/CommsChannelMsg.h +++ b/components/comms/CommsChannelMsg/CommsChannelMsg.h @@ -190,11 +190,24 @@ class CommsChannelMsg return _cmdVector; } + // Whether the buffer may be taken (moved out, leaving it empty) by the channel sending the message, rather + // than copied. Set by a protocol codec on a message it made to send and doesn't use afterwards - a message + // passed on as it is may be sent again (e.g. on each channel), so its buffer mustn't be taken + void setBufTakeable(bool takeable) + { + _bufTakeable = takeable; + } + bool isBufTakeable() const + { + return _bufTakeable; + } + private: uint32_t _channelID; CommsMsgProtocol _msgProtocol; uint32_t _msgNum; CommsMsgTypeCode _msgTypeCode; + bool _bufTakeable = false; #ifdef IMPLEMENT_NO_PSRAM_FOR_COMMS_CHANNEL_MSG std::vector _cmdVector; #else diff --git a/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp b/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp index e6fa69f6..4e24fb89 100644 --- a/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp +++ b/components/comms/ProtocolRICSerial/ProtocolRICSerial.cpp @@ -162,6 +162,9 @@ void ProtocolRICSerial::encodeTxMsgAndSend(CommsChannelMsg& msg) _pHDLC->appendFramePayload(encoded, fcs, ricSerialRec, sizeof(ricSerialRec)); _pHDLC->appendFramePayload(encoded, fcs, msg.getBuf(), msg.getBufLen()); _pHDLC->appendFrameEnd(encoded, fcs); + + // Nothing uses the encoded message after it is sent, so the channel may take its buffer + encodedMsg.setBufTakeable(true); #endif // Debug diff --git a/components/comms/ProtocolRawMsg/ProtocolRawMsg.h b/components/comms/ProtocolRawMsg/ProtocolRawMsg.h index 2aad38fc..ce6a0e07 100644 --- a/components/comms/ProtocolRawMsg/ProtocolRawMsg.h +++ b/components/comms/ProtocolRawMsg/ProtocolRawMsg.h @@ -41,6 +41,17 @@ class ProtocolRawMsg _cmdVector.assign(pBuf, pBuf+bufLen); } + // Take the contents of a buffer without copying (it is left empty) + void takeBuffer(SpiramAwareUint8Vector& buf) + { +#ifdef IMPLEMENT_NO_PSRAM_FOR_PROTOCOL_RAW_MSG + _cmdVector.assign(buf.begin(), buf.end()); + buf.clear(); +#else + _cmdVector.swap(buf); +#endif + } + void setBufferSize(uint32_t bufSize) { _cmdVector.resize(bufSize); @@ -55,11 +66,11 @@ class ProtocolRawMsg } // Access to command vector - const uint8_t* getBuf() + const uint8_t* getBuf() const { return _cmdVector.data(); } - uint32_t getBufLen() + uint32_t getBufLen() const { return _cmdVector.size(); }