From e25feb6824d726c040e36c1c09ad5226c32cdba6 Mon Sep 17 00:00:00 2001 From: DashyFox Date: Fri, 28 Aug 2026 14:30:44 +0300 Subject: [PATCH] reconstruct: restore brave-tape3 packet types stage --- PacketTypes.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++------ PacketTypes.h | 29 +++++++++------ 2 files changed, 103 insertions(+), 21 deletions(-) diff --git a/PacketTypes.cpp b/PacketTypes.cpp index d1c9dc4..db7fd5c 100644 --- a/PacketTypes.cpp +++ b/PacketTypes.cpp @@ -2,12 +2,49 @@ namespace PacketTypes { - bool BasePack::checkAddress() { return true; }; - void BasePack::set(IR_FOX::PackInfo *packInfo, uint16_t id) + uint8_t minimumPacketSize(uint8_t msgType) { + switch (msgType) + { + case IR_MSG_DATA_ACCEPT: + case IR_MSG_DATA_NOACCEPT: + case IR_MSG_BACK_TO: + case IR_MSG_REQUEST: + return uint8_t(msgBytes + addrBytes + addrBytes + crcBytes); + case IR_MSG_BACK: + return uint8_t(msgBytes + addrBytes + crcBytes); + case IR_MSG_ACCEPT: + return uint8_t(msgBytes + addrBytes + 1U + crcBytes); + default: + return 0; + } + } + + bool isTypedPacketSizeValid(uint8_t msgType, uint8_t packSize) + { + const uint8_t minimum = minimumPacketSize(msgType); + return minimum != 0 && packSize >= minimum; + } + + bool BasePack::checkAddress() { return true; } + bool BasePack::set(IR_FOX::PackInfo *packInfo, uint16_t id, bool requireTypedSize) + { + isAvailable = false; + isRawAvailable = false; this->packInfo = packInfo; this->id = id; + if (packInfo == nullptr || packInfo->buffer == nullptr) + { + return false; + } + + const uint8_t msgType = (packInfo->buffer[msgOffset] >> 5) & IR_MASK_MSG_TYPE; + if (requireTypedSize && !isTypedPacketSizeValid(msgType, packInfo->packSize)) + { + return false; + } + if (checkAddress()) { isAvailable = true; @@ -23,29 +60,65 @@ namespace PacketTypes Serial.print(" NOT-OK "); #endif } + return isAvailable; } uint16_t BasePack::_getAddrFrom(BasePack *obj) { + if (obj == nullptr || obj->packInfo == nullptr || obj->packInfo->buffer == nullptr || + obj->packInfo->packSize < crcBytes || + uint16_t(obj->addressFromOffset) + 1U >= uint16_t(obj->packInfo->packSize - crcBytes)) + { + return 0; + } return (obj->packInfo->buffer[obj->addressFromOffset] << 8) | obj->packInfo->buffer[obj->addressFromOffset + 1]; - }; + } uint16_t BasePack::_getAddrTo(BasePack *obj) { + if (obj == nullptr || obj->packInfo == nullptr || obj->packInfo->buffer == nullptr || + obj->packInfo->packSize < crcBytes || + uint16_t(obj->addressToOffset) + 1U >= uint16_t(obj->packInfo->packSize - crcBytes)) + { + return 0; + } return (obj->packInfo->buffer[obj->addressToOffset] << 8) | obj->packInfo->buffer[obj->addressToOffset + 1]; - }; + } uint8_t BasePack::_getDataSize(BasePack *obj) { - return obj->packInfo->packSize - crcBytes - obj->DataOffset; - }; + if (obj == nullptr || obj->packInfo == nullptr || obj->packInfo->buffer == nullptr) + { + return 0; + } + const uint16_t frameOverhead = uint16_t(crcBytes) + uint16_t(obj->DataOffset); + if (uint16_t(obj->packInfo->packSize) <= frameOverhead) + { + return 0; + } + return uint8_t(uint16_t(obj->packInfo->packSize) - frameOverhead); + } uint8_t *BasePack::_getDataPrt(BasePack *obj) { + if (obj == nullptr || obj->packInfo == nullptr || obj->packInfo->buffer == nullptr || + obj->packInfo->packSize < crcBytes) + { + return nullptr; + } + const uint16_t dataEnd = uint16_t(obj->packInfo->packSize) - uint16_t(crcBytes); + if (uint16_t(obj->DataOffset) > dataEnd) + { + return nullptr; + } return obj->packInfo->buffer + obj->DataOffset; - }; + } uint8_t BasePack::_getDataRawSize(BasePack *obj) { + if (obj == nullptr || obj->packInfo == nullptr) + { + return 0; + } return obj->packInfo->packSize; - }; + } bool BasePack::available() { @@ -59,7 +132,7 @@ namespace PacketTypes { return false; } - }; + } bool BasePack::availableRaw() { if (isRawAvailable) @@ -71,7 +144,7 @@ namespace PacketTypes { return false; } - }; + } bool Data::checkAddress() { @@ -104,4 +177,4 @@ namespace PacketTypes IR_FOX::checkAddressRuleApply(getAddrTo(), this->id, ret); return ret; } -} \ No newline at end of file +} diff --git a/PacketTypes.h b/PacketTypes.h index 37e18a8..734913a 100644 --- a/PacketTypes.h +++ b/PacketTypes.h @@ -4,25 +4,34 @@ class IR_Decoder; namespace PacketTypes { + /** + * Minimum complete frame size (header, addresses/data required by the type, + * and CRC). Unknown/reserved message types return 0. + */ + uint8_t minimumPacketSize(uint8_t msgType); + + /** True only for a known typed packet whose complete frame is long enough. */ + bool isTypedPacketSizeValid(uint8_t msgType, uint8_t packSize); + class BasePack { friend IR_Decoder; protected: - bool isAvailable; - bool isRawAvailable; - bool isNeedAccept; + bool isAvailable = false; + bool isRawAvailable = false; + bool isNeedAccept = false; - uint8_t msgOffset; - uint8_t addressFromOffset; - uint8_t addressToOffset; - uint8_t DataOffset; + uint8_t msgOffset = 0; + uint8_t addressFromOffset = 0; + uint8_t addressToOffset = 0; + uint8_t DataOffset = 0; - IR_FOX::PackInfo *packInfo; - uint16_t id; + IR_FOX::PackInfo *packInfo = nullptr; + uint16_t id = 0; virtual bool checkAddress(); - void set(IR_FOX::PackInfo *packInfo, uint16_t id); + bool set(IR_FOX::PackInfo *packInfo, uint16_t id, bool requireTypedSize = true); static uint16_t _getAddrFrom(BasePack *obj); static uint16_t _getAddrTo(BasePack *obj);