reconstruct: restore brave-tape3 packet types stage

This commit is contained in:
2026-08-28 14:30:44 +03:00
parent 96ffb91b97
commit e25feb6824
2 changed files with 103 additions and 21 deletions

View File

@ -2,12 +2,49 @@
namespace PacketTypes namespace PacketTypes
{ {
bool BasePack::checkAddress() { return true; }; uint8_t minimumPacketSize(uint8_t msgType)
void BasePack::set(IR_FOX::PackInfo *packInfo, uint16_t id)
{ {
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->packInfo = packInfo;
this->id = id; 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()) if (checkAddress())
{ {
isAvailable = true; isAvailable = true;
@ -23,29 +60,65 @@ namespace PacketTypes
Serial.print(" NOT-OK "); Serial.print(" NOT-OK ");
#endif #endif
} }
return isAvailable;
} }
uint16_t BasePack::_getAddrFrom(BasePack *obj) 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]; return (obj->packInfo->buffer[obj->addressFromOffset] << 8) | obj->packInfo->buffer[obj->addressFromOffset + 1];
}; }
uint16_t BasePack::_getAddrTo(BasePack *obj) 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]; return (obj->packInfo->buffer[obj->addressToOffset] << 8) | obj->packInfo->buffer[obj->addressToOffset + 1];
}; }
uint8_t BasePack::_getDataSize(BasePack *obj) 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) 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; return obj->packInfo->buffer + obj->DataOffset;
}; }
uint8_t BasePack::_getDataRawSize(BasePack *obj) uint8_t BasePack::_getDataRawSize(BasePack *obj)
{ {
if (obj == nullptr || obj->packInfo == nullptr)
{
return 0;
}
return obj->packInfo->packSize; return obj->packInfo->packSize;
}; }
bool BasePack::available() bool BasePack::available()
{ {
@ -59,7 +132,7 @@ namespace PacketTypes
{ {
return false; return false;
} }
}; }
bool BasePack::availableRaw() bool BasePack::availableRaw()
{ {
if (isRawAvailable) if (isRawAvailable)
@ -71,7 +144,7 @@ namespace PacketTypes
{ {
return false; return false;
} }
}; }
bool Data::checkAddress() bool Data::checkAddress()
{ {

View File

@ -4,25 +4,34 @@
class IR_Decoder; class IR_Decoder;
namespace PacketTypes 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 class BasePack
{ {
friend IR_Decoder; friend IR_Decoder;
protected: protected:
bool isAvailable; bool isAvailable = false;
bool isRawAvailable; bool isRawAvailable = false;
bool isNeedAccept; bool isNeedAccept = false;
uint8_t msgOffset; uint8_t msgOffset = 0;
uint8_t addressFromOffset; uint8_t addressFromOffset = 0;
uint8_t addressToOffset; uint8_t addressToOffset = 0;
uint8_t DataOffset; uint8_t DataOffset = 0;
IR_FOX::PackInfo *packInfo; IR_FOX::PackInfo *packInfo = nullptr;
uint16_t id; uint16_t id = 0;
virtual bool checkAddress(); 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 _getAddrFrom(BasePack *obj);
static uint16_t _getAddrTo(BasePack *obj); static uint16_t _getAddrTo(BasePack *obj);