crc8-rohc fix

This commit is contained in:
John Wellbelove 2020-10-11 19:06:35 +01:00
parent 26b0f84439
commit bd5ded5bde
5 changed files with 151 additions and 236 deletions

View File

@ -43,7 +43,7 @@ SOFTWARE.
namespace etl
{
typedef crc8_poly_0x07<0xFF, 0x00, true, true> crc8_rohc;
typedef crc8_poly_0x07<0xFF, 0x00, true> crc8_rohc;
}
#endif

View File

@ -39,37 +39,40 @@ namespace etl
namespace private_cumulative_moving_average
{
//***************************************************
/// Proxy adder.
/// Returned by the iterator dereference operator.
/// add_insert_iterator
/// An output iterator used to add new values.
//***************************************************
template <typename TCMA>
class proxy_adder
class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, void, void, void, void>
{
public:
friend typename TCMA::iterator;
//***************************************************
/// Copy constuctor
//***************************************************
proxy_adder(const proxy_adder& other)
: p_cma(other.p_cma)
//***********************************
explicit add_insert_iterator(TCMA& cma) ETL_NOEXCEPT
: p_cma(&cma)
{
}
//***************************************************
/// Assignment from proxy_adder
//***************************************************
proxy_adder& operator =(const proxy_adder& rhs)
//***********************************
add_insert_iterator& operator*() ETL_NOEXCEPT
{
p_cma = rhs.p_cma;
return *this;
}
//***************************************************
/// Assignment from value
//***************************************************
proxy_adder& operator =(typename TCMA::value_type value)
//***********************************
add_insert_iterator& operator++() ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator++(int) ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator =(typename TCMA::value_type value)
{
p_cma->add(value);
return *this;
@ -77,89 +80,8 @@ namespace etl
private:
//***************************************************
/// Private constructor
//***************************************************
proxy_adder(TCMA* p_cma_)
: p_cma(p_cma_)
{
}
TCMA* p_cma;
};
//***************************************************
/// iterator
/// An output iterator used to add new values.
//***************************************************
template <typename TCMA>
class iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, typename TCMA::value_type>
{
public:
friend TCMA;
//***************************************************
/// Default constructor
//***************************************************
iterator()
{
}
//***************************************************
/// Copy constuctor
//***************************************************
iterator(const iterator& other)
: adder(other.adder)
{
}
//***************************************************
/// Assignment operator
//***************************************************
iterator& operator =(const iterator& rhs)
{
adder = rhs.adder;
return *this;
}
//***************************************************
/// Pre-increment operator
//***************************************************
iterator& operator ++()
{
return *this;
}
//***************************************************
/// Post-increment operator
//***************************************************
iterator& operator ++(int)
{
return *this;
}
//***************************************************
/// De-reference operator
//***************************************************
proxy_adder<TCMA> operator *() const
{
return adder;
}
private:
//***************************************************
/// Private constructor
//***************************************************
iterator(TCMA* p_cma)
: adder(p_cma)
{
}
/// The adder proxy returned by an iterator dereference.
proxy_adder<TCMA> adder;
};
}
//***************************************************************************
@ -198,7 +120,7 @@ namespace etl
public:
typedef T value_type;
typedef private_cumulative_moving_average::iterator<this_t> iterator;
typedef private_cumulative_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static const size_t SAMPLE_SIZE = SAMPLE_SIZE_; ///< The number of samples averaged over.
static const size_t SCALING = SCALING_; ///< The sample scaling factor.
@ -245,9 +167,9 @@ namespace etl
/// Gets an iterator for input.
/// \return An iterator.
//*************************************************************************
iterator input()
add_insert_iterator input()
{
return iterator(this);
return add_insert_iterator(*this);
}
private:
@ -274,7 +196,7 @@ namespace etl
public:
typedef T value_type;
typedef private_cumulative_moving_average::iterator<this_t> iterator;
typedef private_cumulative_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static const size_t SCALING = SCALING_; ///< The sample scaling factor.
@ -330,9 +252,9 @@ namespace etl
/// Gets an iterator for input.
/// \return An iterator.
//*************************************************************************
iterator input()
add_insert_iterator input()
{
return iterator(this);
return add_insert_iterator(*this);
}
private:
@ -355,7 +277,7 @@ namespace etl
public:
typedef T value_type;
typedef private_cumulative_moving_average::iterator<this_t> iterator;
typedef private_cumulative_moving_average::add_insert_iterator<this_t> add_insert_iterator;
static const size_t SAMPLE_SIZE = SAMPLE_SIZE_;
@ -400,9 +322,9 @@ namespace etl
/// Gets an iterator for input.
/// \return An iterator.
//*************************************************************************
iterator input()
add_insert_iterator input()
{
return iterator(this);
return add_insert_iterator(*this);
}
private:
@ -425,7 +347,7 @@ namespace etl
public:
typedef T value_type;
typedef private_cumulative_moving_average::iterator<this_t> iterator;
typedef private_cumulative_moving_average::add_insert_iterator<this_t> add_insert_iterator;
//*************************************************************************
/// Constructor
@ -477,9 +399,9 @@ namespace etl
/// Gets an iterator for input.
/// \return An iterator.
//*************************************************************************
iterator input()
add_insert_iterator input()
{
return iterator(this);
return add_insert_iterator(*this);
}
private:

View File

@ -46,37 +46,40 @@ namespace etl
namespace private_frame_check_sequence
{
//***************************************************
/// Proxy adder.
/// Returned by the iterator dereference operator.
/// add_insert_iterator
/// An output iterator used to add new values.
//***************************************************
template <typename TFCS>
class proxy_adder
class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, void, void, void, void>
{
public:
friend typename TFCS::iterator;
//***************************************************
/// Copy constuctor
//***************************************************
proxy_adder(const proxy_adder& other)
: p_fcs(other.p_fcs)
//***********************************
explicit add_insert_iterator(TFCS& fcs) ETL_NOEXCEPT
: p_fcs(&fcs)
{
}
//***************************************************
/// Assignment from proxy_adder
//***************************************************
proxy_adder& operator =(const proxy_adder& rhs)
//***********************************
add_insert_iterator& operator*() ETL_NOEXCEPT
{
p_fcs = rhs.p_fcs;
return *this;
}
//***************************************************
/// Assignment from value
//***************************************************
proxy_adder& operator =(uint8_t value)
//***********************************
add_insert_iterator& operator++() ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator++(int) ETL_NOEXCEPT
{
return *this;
}
//***********************************
add_insert_iterator& operator =(uint8_t value)
{
p_fcs->add(value);
return *this;
@ -84,89 +87,8 @@ namespace etl
private:
//***************************************************
/// Private constructor
//***************************************************
proxy_adder(TFCS* p_fcs_)
: p_fcs(p_fcs_)
{
}
TFCS* p_fcs;
};
//***************************************************
/// iterator
/// An output iterator used to add new values.
//***************************************************
template <typename TFCS>
class iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, typename TFCS::value_type>
{
public:
friend TFCS;
//***************************************************
/// Default constructor
//***************************************************
iterator()
{
}
//***************************************************
/// Copy constuctor
//***************************************************
iterator(const iterator& other)
: adder(other.adder)
{
}
//***************************************************
/// Assignment operator
//***************************************************
iterator& operator =(const iterator& rhs)
{
adder = rhs.adder;
return *this;
}
//***************************************************
/// Pre-increment operator
//***************************************************
iterator& operator ++()
{
return *this;
}
//***************************************************
/// Post-increment operator
//***************************************************
iterator& operator ++(int)
{
return *this;
}
//***************************************************
/// De-reference operator
//***************************************************
proxy_adder<TFCS> operator *() const
{
return adder;
}
private:
//***************************************************
/// Private constructor
//***************************************************
iterator(TFCS* p_fcs)
: adder(p_fcs)
{
}
/// The adder proxy returned by an iterator dereference.
proxy_adder<TFCS> adder;
};
}
//***************************************************************************
@ -181,7 +103,7 @@ namespace etl
typedef TPolicy policy_type;
typedef typename policy_type::value_type value_type;
typedef private_frame_check_sequence::iterator<frame_check_sequence<TPolicy>> iterator;
typedef private_frame_check_sequence::add_insert_iterator<frame_check_sequence<TPolicy> > add_insert_iterator;
ETL_STATIC_ASSERT(etl::is_unsigned<value_type>::value, "Signed frame check type not supported");
@ -256,11 +178,11 @@ namespace etl
}
//*************************************************************************
/// Gets an iterator for input.
/// Gets an add_insert_iterator for input.
//*************************************************************************
iterator input()
add_insert_iterator input()
{
return iterator(this);
return add_insert_iterator(*this);
}
private:

View File

@ -93,22 +93,22 @@ namespace etl
{
static const uint8_t table[256] =
{
0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97, 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4, 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11, 0x3F, 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8,
0xC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d8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad, 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1, 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5, 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9, 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd, 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1, 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
};
return table[crc ^ value];

View File

@ -34,6 +34,7 @@ SOFTWARE.
#include <stdint.h>
#include "etl/crc8_ccitt.h"
#include "etl/crc8_rohc.h"
#include "etl/crc16.h"
#include "etl/crc16_ccitt.h"
#include "etl/crc16_aug_ccitt.h"
@ -82,7 +83,7 @@ namespace
uint8_t crc = crc_calculator;
CHECK_EQUAL(0xF4, crc);
CHECK_EQUAL(0xF4, int(crc));
}
//*************************************************************************
@ -96,7 +97,7 @@ namespace
uint8_t crc = crc_calculator.value();
CHECK_EQUAL(0xF4, crc);
CHECK_EQUAL(0xF4, int(crc));
}
//*************************************************************************
@ -110,7 +111,7 @@ namespace
uint8_t crc = crc_calculator.value();
CHECK_EQUAL(0xF4, crc);
CHECK_EQUAL(0xF4, int(crc));
}
//*************************************************************************
@ -128,6 +129,76 @@ namespace
CHECK_EQUAL(int(crc1), int(crc3));
}
//*************************************************************************
TEST(test_crc8_rohc_constructor)
{
std::string data("123456789");
uint8_t crc = etl::crc8_rohc(data.begin(), data.end());
CHECK_EQUAL(0xD0, int(crc));
}
//*************************************************************************
TEST(test_crc8_rohc_add_values)
{
std::string data("123456789");
etl::crc8_rohc crc_calculator;
for (size_t i = 0; i < data.size(); ++i)
{
crc_calculator.add(data[i]);
}
uint8_t crc = crc_calculator;
CHECK_EQUAL(0xD0, int(crc));
}
//*************************************************************************
TEST(test_crc8_rohc_add_range)
{
std::string data("123456789");
etl::crc8_rohc crc_calculator;
crc_calculator.add(data.begin(), data.end());
uint8_t crc = crc_calculator.value();
CHECK_EQUAL(0xD0, int(crc));
}
//*************************************************************************
TEST(test_crc8_rohc_add_range_via_iterator)
{
std::string data("123456789");
etl::crc8_rohc crc_calculator;
std::copy(data.begin(), data.end(), crc_calculator.input());
uint8_t crc = crc_calculator.value();
CHECK_EQUAL(0xD0, int(crc));
}
//*************************************************************************
TEST(test_crc8_rohc_add_range_endian)
{
std::vector<uint8_t> data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
std::vector<uint32_t> data2 = { 0x04030201, 0x08070605 };
std::vector<uint8_t> data3 = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
uint8_t crc1 = etl::crc32(data1.begin(), data1.end());
uint8_t crc2 = etl::crc32((uint8_t*)&data2[0], (uint8_t*)(&data2[0] + data2.size()));
CHECK_EQUAL(int(crc1), int(crc2));
uint8_t crc3 = etl::crc32(data3.rbegin(), data3.rend());
CHECK_EQUAL(int(crc1), int(crc3));
}
//*************************************************************************
TEST(test_crc16)
{