mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-24 21:43:33 +08:00
Integrated DataTypeSignature; hash removed
This commit is contained in:
@@ -85,50 +85,28 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct DataTypeHash
|
|
||||||
{
|
|
||||||
enum { NumBytes = 16 };
|
|
||||||
uint8_t value[NumBytes];
|
|
||||||
|
|
||||||
DataTypeHash()
|
|
||||||
{
|
|
||||||
std::fill(value, value + NumBytes, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
DataTypeHash(const uint8_t source[NumBytes])
|
|
||||||
{
|
|
||||||
std::copy(source, source + NumBytes, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const DataTypeHash& rhs) const { return !operator==(rhs); }
|
|
||||||
bool operator==(const DataTypeHash& rhs) const
|
|
||||||
{
|
|
||||||
return std::equal(value, value + NumBytes, rhs.value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct DataTypeDescriptor
|
struct DataTypeDescriptor
|
||||||
{
|
{
|
||||||
DataTypeKind kind;
|
DataTypeKind kind;
|
||||||
uint16_t id;
|
uint16_t id;
|
||||||
DataTypeHash hash;
|
DataTypeSignature signature;
|
||||||
|
|
||||||
DataTypeDescriptor()
|
DataTypeDescriptor()
|
||||||
: kind(DataTypeKind(0))
|
: kind(DataTypeKind(0))
|
||||||
, id(0)
|
, id(0)
|
||||||
|
, signature(DataTypeSignature::zero())
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
DataTypeDescriptor(DataTypeKind kind, uint16_t id, const DataTypeHash& hash)
|
DataTypeDescriptor(DataTypeKind kind, uint16_t id, const DataTypeSignature& signature)
|
||||||
: kind(kind)
|
: kind(kind)
|
||||||
, id(id)
|
, id(id)
|
||||||
, hash(hash)
|
, signature(signature)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
bool operator!=(const DataTypeDescriptor& rhs) const { return !operator==(rhs); }
|
bool operator!=(const DataTypeDescriptor& rhs) const { return !operator==(rhs); }
|
||||||
bool operator==(const DataTypeDescriptor& rhs) const
|
bool operator==(const DataTypeDescriptor& rhs) const
|
||||||
{
|
{
|
||||||
return (kind == rhs.kind) && (id == rhs.id) && (hash == rhs.hash);
|
return (kind == rhs.kind) && (id == rhs.id) && (signature == rhs.signature);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -35,12 +35,6 @@ public:
|
|||||||
: value_(0xFFFF)
|
: value_(0xFFFF)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
TransferCRC(const uint8_t* bytes, unsigned int len)
|
|
||||||
: value_(0xFFFF)
|
|
||||||
{
|
|
||||||
add(bytes, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void add(uint8_t byte)
|
void add(uint8_t byte)
|
||||||
{
|
{
|
||||||
value_ = ((value_ << 8) ^ Table[((value_ >> 8) ^ byte) & 0xFF]) & 0xFFFF;
|
value_ = ((value_ << 8) ^ Table[((value_ >> 8) ^ byte) & 0xFF]) & 0xFFFF;
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class TransferListenerBase : public LinkedListNode<TransferListenerBase>
|
|||||||
protected:
|
protected:
|
||||||
TransferListenerBase(const DataTypeDescriptor& data_type)
|
TransferListenerBase(const DataTypeDescriptor& data_type)
|
||||||
: data_type_(data_type)
|
: data_type_(data_type)
|
||||||
, crc_base_(data_type.hash.value, DataTypeHash::NumBytes)
|
, crc_base_(data_type.signature.toTransferCRC())
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
virtual ~TransferListenerBase() { }
|
virtual ~TransferListenerBase() { }
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public:
|
|||||||
: max_transfer_interval_(max_transfer_interval)
|
: max_transfer_interval_(max_transfer_interval)
|
||||||
, data_type_(data_type)
|
, data_type_(data_type)
|
||||||
, qos_(qos)
|
, qos_(qos)
|
||||||
, crc_base_(data_type.hash.value, DataTypeHash::NumBytes)
|
, crc_base_(data_type.signature.toTransferCRC())
|
||||||
, dispatcher_(dispatcher)
|
, dispatcher_(dispatcher)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|||||||
@@ -82,4 +82,12 @@ TEST(DataTypeSignature, Correctness)
|
|||||||
crc.add((old_signature >> i) & 0xFF);
|
crc.add((old_signature >> i) & 0xFF);
|
||||||
|
|
||||||
ASSERT_EQ(crc.get(), signature.get());
|
ASSERT_EQ(crc.get(), signature.get());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Comparison
|
||||||
|
*/
|
||||||
|
ASSERT_TRUE(signature == DataTypeSignature(signature.get()));
|
||||||
|
ASSERT_FALSE(signature == DataTypeSignature::zero());
|
||||||
|
ASSERT_FALSE(signature != DataTypeSignature(signature.get()));
|
||||||
|
ASSERT_TRUE(signature != DataTypeSignature::zero());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,4 @@ TEST(TransferCRC, Correctness)
|
|||||||
|
|
||||||
crc.add(reinterpret_cast<const uint8_t*>("456789"), 6);
|
crc.add(reinterpret_cast<const uint8_t*>("456789"), 6);
|
||||||
ASSERT_EQ(0x29B1, crc.get());
|
ASSERT_EQ(0x29B1, crc.get());
|
||||||
|
|
||||||
// Initializing constructor
|
|
||||||
ASSERT_EQ(crc.get(), uavcan::TransferCRC(reinterpret_cast<const uint8_t*>("123456789"), 9).get());
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,9 +30,7 @@ public:
|
|||||||
|
|
||||||
TEST(TransferListener, BasicMFT)
|
TEST(TransferListener, BasicMFT)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i | (i << 4);
|
|
||||||
|
|
||||||
static const int NUM_POOL_BLOCKS = 12; // This number is just enough to pass the test
|
static const int NUM_POOL_BLOCKS = 12; // This number is just enough to pass the test
|
||||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * NUM_POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * NUM_POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||||
@@ -87,9 +85,7 @@ TEST(TransferListener, BasicMFT)
|
|||||||
|
|
||||||
TEST(TransferListener, CrcFailure)
|
TEST(TransferListener, CrcFailure)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i | (i << 4);
|
|
||||||
|
|
||||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
||||||
TestSubscriber<256, 2, 2> subscriber(type, poolmgr); // Static buffer only, 2 entries
|
TestSubscriber<256, 2, 2> subscriber(type, poolmgr); // Static buffer only, 2 entries
|
||||||
@@ -132,9 +128,7 @@ TEST(TransferListener, CrcFailure)
|
|||||||
|
|
||||||
TEST(TransferListener, BasicSFT)
|
TEST(TransferListener, BasicSFT)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i | (i << 4);
|
|
||||||
|
|
||||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory. At all.
|
uavcan::PoolManager<1> poolmgr; // No dynamic memory. At all.
|
||||||
TestSubscriber<0, 0, 5> subscriber(type, poolmgr); // Max buf size is 0, i.e. SFT-only
|
TestSubscriber<0, 0, 5> subscriber(type, poolmgr); // Max buf size is 0, i.e. SFT-only
|
||||||
@@ -169,9 +163,7 @@ TEST(TransferListener, BasicSFT)
|
|||||||
|
|
||||||
TEST(TransferListener, Cleanup)
|
TEST(TransferListener, Cleanup)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i | (i << 4);
|
|
||||||
|
|
||||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
||||||
TestSubscriber<256, 1, 2> subscriber(type, poolmgr); // Static buffer only, 1 entry
|
TestSubscriber<256, 1, 2> subscriber(type, poolmgr); // Static buffer only, 1 entry
|
||||||
@@ -225,9 +217,7 @@ TEST(TransferListener, Cleanup)
|
|||||||
|
|
||||||
TEST(TransferListener, MaximumTransferLength)
|
TEST(TransferListener, MaximumTransferLength)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i | (i << 4);
|
|
||||||
|
|
||||||
uavcan::PoolManager<1> poolmgr;
|
uavcan::PoolManager<1> poolmgr;
|
||||||
TestSubscriber<uavcan::MaxTransferPayloadLen * 2, 2, 2> subscriber(type, poolmgr);
|
TestSubscriber<uavcan::MaxTransferPayloadLen * 2, 2, 2> subscriber(type, poolmgr);
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ TEST(TransferTestHelpers, Transfer)
|
|||||||
|
|
||||||
TEST(TransferTestHelpers, MFTSerialization)
|
TEST(TransferTestHelpers, MFTSerialization)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i;
|
|
||||||
|
|
||||||
static const std::string DATA = "To go wrong in one's own way is better than to go right in someone else's.";
|
static const std::string DATA = "To go wrong in one's own way is better than to go right in someone else's.";
|
||||||
const Transfer transfer(1, 100000, uavcan::TransferTypeMessageUnicast, 2, 42, 127, DATA, type);
|
const Transfer transfer(1, 100000, uavcan::TransferTypeMessageUnicast, 2, 42, 127, DATA, type);
|
||||||
@@ -63,9 +61,7 @@ TEST(TransferTestHelpers, MFTSerialization)
|
|||||||
|
|
||||||
TEST(TransferTestHelpers, SFTSerialization)
|
TEST(TransferTestHelpers, SFTSerialization)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeHash());
|
uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i++)
|
|
||||||
type.hash.value[i] = i;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
const Transfer transfer(1, 100000, uavcan::TransferTypeMessageBroadcast, 7, 42, 0, "Nvrfrget", type);
|
const Transfer transfer(1, 100000, uavcan::TransferTypeMessageBroadcast, 7, 42, 0, "Nvrfrget", type);
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ std::vector<uavcan::RxFrame> serializeTransfer(const Transfer& transfer)
|
|||||||
std::vector<uint8_t> raw_payload;
|
std::vector<uint8_t> raw_payload;
|
||||||
if (need_crc)
|
if (need_crc)
|
||||||
{
|
{
|
||||||
uavcan::TransferCRC payload_crc(transfer.data_type.hash.value, uavcan::DataTypeHash::NumBytes);
|
uavcan::TransferCRC payload_crc = transfer.data_type.signature.toTransferCRC();
|
||||||
payload_crc.add(reinterpret_cast<const uint8_t*>(transfer.payload.c_str()), transfer.payload.length());
|
payload_crc.add(reinterpret_cast<const uint8_t*>(transfer.payload.c_str()), transfer.payload.length());
|
||||||
// Little endian
|
// Little endian
|
||||||
raw_payload.push_back(payload_crc.get() & 0xFF);
|
raw_payload.push_back(payload_crc.get() & 0xFF);
|
||||||
@@ -212,13 +212,8 @@ std::vector<uavcan::RxFrame> serializeTransfer(const Transfer& transfer)
|
|||||||
|
|
||||||
uavcan::DataTypeDescriptor makeDataType(uavcan::DataTypeKind kind, uint16_t id)
|
uavcan::DataTypeDescriptor makeDataType(uavcan::DataTypeKind kind, uint16_t id)
|
||||||
{
|
{
|
||||||
uavcan::DataTypeDescriptor dtd(kind, id, uavcan::DataTypeHash());
|
const uavcan::DataTypeSignature signature((uint64_t(kind) << 16) | (id << 8) | (id & 0xFF));
|
||||||
for (int i = 0; i < uavcan::DataTypeHash::NumBytes; i += 2)
|
return uavcan::DataTypeDescriptor(kind, id, signature);
|
||||||
{
|
|
||||||
dtd.hash.value[i] = id & 0xFF;
|
|
||||||
dtd.hash.value[i + 1] = id >> 8;
|
|
||||||
}
|
|
||||||
return dtd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user