Crc16 --> TransferCRC

This commit is contained in:
Pavel Kirienko
2014-02-25 17:58:31 +04:00
parent 366c5f5460
commit a6ab9c416f
9 changed files with 18 additions and 18 deletions
@@ -14,7 +14,7 @@ namespace uavcan
* Initial value: 0x0000
* Coefficient: 0x1021
*/
class Crc16
class TransportCRC
{
static const uint16_t Table[256];
uint16_t value_;
@@ -22,11 +22,11 @@ class Crc16
public:
enum { NumBytes = 2 };
Crc16()
TransportCRC()
: value_(0x0000)
{ }
Crc16(const uint8_t* bytes, unsigned int len)
TransportCRC(const uint8_t* bytes, unsigned int len)
: value_(0x0000)
{
add(bytes, len);
@@ -88,7 +88,7 @@ public:
class TransferListenerBase : public LinkedListNode<TransferListenerBase>
{
const DataTypeDescriptor& data_type_;
const Crc16 crc_base_; ///< Pre-initialized with data type hash, thus constant
const TransportCRC crc_base_; ///< Pre-initialized with data type hash, thus constant
bool checkPayloadCrc(const uint16_t compare_with, const ITransferBuffer& tbb) const;
@@ -21,7 +21,7 @@ class TransferSender
const uint64_t max_transfer_interval_;
const DataTypeDescriptor& data_type_;
const CanTxQueue::Qos qos_;
const Crc16 crc_base_;
const TransportCRC crc_base_;
Dispatcher& dispatcher_;
+3 -3
View File
@@ -9,7 +9,7 @@ namespace uavcan
{
// print ', '.join(map(lambda x: '%04x' % x, map(lambda x: int(x, 0), c.crc_ccitt_tab)))
const uint16_t Crc16::Table[256] =
const uint16_t TransportCRC::Table[256] =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
@@ -45,13 +45,13 @@ const uint16_t Crc16::Table[256] =
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
uint16_t Crc16::add(uint8_t byte)
uint16_t TransportCRC::add(uint8_t byte)
{
value_ = (value_ << 8) ^ Table[((value_ >> 8) ^ byte) & 0xFF];
return value_;
}
uint16_t Crc16::add(const uint8_t* bytes, unsigned int len)
uint16_t TransportCRC::add(const uint8_t* bytes, unsigned int len)
{
assert(bytes);
while (len--)
@@ -67,7 +67,7 @@ int MultiFrameIncomingTransfer::read(unsigned int offset, uint8_t* data, unsigne
*/
bool TransferListenerBase::checkPayloadCrc(const uint16_t compare_with, const ITransferBuffer& tbb) const
{
Crc16 crc = crc_base_;
TransportCRC crc = crc_base_;
unsigned int offset = 0;
while (true)
{
@@ -56,7 +56,7 @@ bool TransferReceiver::validate(const RxFrame& frame) const
if (iface_index_ != frame.getIfaceIndex())
return false;
if (frame.isFirst() && !frame.isLast() && (frame.getPayloadLen() < Crc16::NumBytes))
if (frame.isFirst() && !frame.isLast() && (frame.getPayloadLen() < TransportCRC::NumBytes))
{
UAVCAN_TRACE("TransferReceiver", "CRC expected, %s", frame.toString().c_str());
return false;
@@ -90,13 +90,13 @@ bool TransferReceiver::writePayload(const RxFrame& frame, ITransferBuffer& buf)
if (frame.isFirst()) // First frame contains CRC, we need to extract it now
{
if (frame.getPayloadLen() < Crc16::NumBytes) // Must have been validated earlier though. I think I'm paranoid.
if (frame.getPayloadLen() < TransportCRC::NumBytes) // Must have been validated earlier though. I think I'm paranoid.
return false;
this_transfer_crc_ = (payload[0] & 0xFF) | (uint16_t(payload[1] & 0xFF) << 8); // Little endian.
const int effective_payload_len = payload_len - Crc16::NumBytes;
const int res = buf.write(buffer_write_pos_, payload + Crc16::NumBytes, effective_payload_len);
const int effective_payload_len = payload_len - TransportCRC::NumBytes;
const int res = buf.write(buffer_write_pos_, payload + TransportCRC::NumBytes, effective_payload_len);
const bool success = res == effective_payload_len;
if (success)
buffer_write_pos_ += effective_payload_len;
+1 -1
View File
@@ -33,7 +33,7 @@ int TransferSender::send(const uint8_t* payload, int payload_len, uint64_t monot
{
int offset = 0;
{
Crc16 crc = crc_base_;
TransportCRC crc = crc_base_;
crc.add(payload, payload_len);
static const int BUFLEN = sizeof(CanFrame::data);
+3 -3
View File
@@ -7,9 +7,9 @@
#include <uavcan/internal/transport/crc.hpp>
TEST(Crc16, Correctness)
TEST(TransportCRC, Correctness)
{
uavcan::Crc16 crc;
uavcan::TransportCRC crc;
ASSERT_EQ(0x0000, crc.get());
@@ -22,5 +22,5 @@ TEST(Crc16, Correctness)
ASSERT_EQ(53881, crc.get());
// Initializing constructor
ASSERT_EQ(crc.get(), uavcan::Crc16(reinterpret_cast<const uint8_t*>("123Foobar"), 9).get());
ASSERT_EQ(crc.get(), uavcan::TransportCRC(reinterpret_cast<const uint8_t*>("123Foobar"), 9).get());
}
@@ -165,7 +165,7 @@ std::vector<uavcan::RxFrame> serializeTransfer(const Transfer& transfer)
std::vector<uint8_t> raw_payload;
if (need_crc)
{
uavcan::Crc16 payload_crc(transfer.data_type.hash.value, uavcan::DataTypeHash::NumBytes);
uavcan::TransportCRC payload_crc(transfer.data_type.hash.value, uavcan::DataTypeHash::NumBytes);
payload_crc.add(reinterpret_cast<const uint8_t*>(transfer.payload.c_str()), transfer.payload.length());
// Little endian
raw_payload.push_back(payload_crc.get() & 0xFF);