Merge branch 'development'

This commit is contained in:
John Wellbelove 2016-02-13 11:21:57 +00:00
commit 965cb6a2de
8 changed files with 324 additions and 447 deletions

View File

@ -32,6 +32,7 @@ SOFTWARE.
#include "static_assert.h"
#include "type_traits.h"
#include "binary.h"
#include "frame_check_sequence.h"
///\defgroup checksum Checksum calculation
///\ingroup maths
@ -44,6 +45,8 @@ namespace etl
template <typename T>
struct checksum_policy_sum
{
typedef T value_type;
inline T initial() const
{
return 0;
@ -66,6 +69,8 @@ namespace etl
template <typename T>
struct checksum_policy_bsd
{
typedef T value_type;
inline T initial() const
{
return 0;
@ -88,6 +93,8 @@ namespace etl
template <typename T>
struct checksum_policy_xor
{
typedef T value_type;
inline T initial() const
{
return 0;
@ -104,22 +111,14 @@ namespace etl
}
};
//***************************************************************************
/// Calculates a checksum according to the specified policy.
///\tparam TSum The type used for the sum.
///\tparam TPolicy The type used to enact the policy. Default = checksum_policy_sum
///\ingroup checksum
//***************************************************************************
template <typename TSum, typename TPolicy = checksum_policy_sum<TSum> >
class checksum
//*************************************************************************
/// Standard Checksum.
//*************************************************************************
template <typename T>
class checksum : public etl::frame_check_sequence<etl::checksum_policy_sum<T> >
{
public:
STATIC_ASSERT(etl::is_unsigned<TSum>::value, "Signed TSum template parameter not supported");
typedef TSum value_type;
typedef TPolicy policy_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -141,66 +140,13 @@ namespace etl
reset();
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
sum = policy.initial();
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
sum = policy.add(sum, *begin++);
}
}
//*************************************************************************
/// \param value The uint8_t to add to the checksum.
//*************************************************************************
void add(uint8_t value)
{
sum = policy.add(sum, value);
}
//*************************************************************************
/// Gets the checksum value.
//*************************************************************************
value_type value() const
{
return policy.final(sum);
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return policy.final(sum);
}
private:
value_type sum;
policy_type policy;
};
//*************************************************************************
/// BSD Checksum.
//*************************************************************************
template <typename T>
class bsd_checksum : public etl::checksum<T, etl::checksum_policy_bsd<T> >
class bsd_checksum : public etl::frame_check_sequence<etl::checksum_policy_bsd<T> >
{
public:
@ -231,7 +177,7 @@ namespace etl
/// XOR Checksum.
//*************************************************************************
template <typename T>
class xor_checksum : public etl::checksum<T, etl::checksum_policy_xor<T> >
class xor_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor<T> >
{
public:

91
crc16.h
View File

@ -32,9 +32,10 @@ SOFTWARE.
#define __ETL_CRC16__
#include <stdint.h>
#include <iterator>
#include "static_assert.h"
#include "type_traits.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -52,16 +53,36 @@ namespace etl
extern const uint16_t CRC16[];
//***************************************************************************
/// CRC16 policy.
/// Calculates CRC16 using polynomial 0x8005.
/// \ingroup crc16
//***************************************************************************
class crc16
struct crc_policy_16
{
typedef uint16_t value_type;
inline uint16_t initial() const
{
return 0;
}
inline uint16_t add(uint16_t crc, uint8_t value) const
{
return (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF];
}
inline uint16_t final(uint16_t crc) const
{
return crc;
}
};
//*************************************************************************
/// CRC16
//*************************************************************************
class crc16 : public etl::frame_check_sequence<etl::crc_policy_16>
{
public:
typedef uint16_t value_type;
typedef uint16_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -81,64 +102,8 @@ namespace etl
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = (crc >> 8) ^ CRC16[(crc ^ *begin++) & 0xFF];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = (crc >> 8) ^ CRC16[(crc ^ *begin++) & 0xFF];
}
}
//*************************************************************************
/// \param value The uint8_t to add to the CRC.
/// \return The CRC result.
//*************************************************************************
void add(uint8_t value)
{
crc = (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc;
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return crc;
}
private:
value_type crc;
};
}

View File

@ -32,9 +32,10 @@ SOFTWARE.
#define __ETL_CRC16_CCITT__
#include <stdint.h>
#include <iterator>
#include "static_assert.h"
#include "type_traits.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -50,18 +51,38 @@ namespace etl
/// \ingroup crc16_ccitt
//***************************************************************************
extern const uint16_t CRC_CCITT[];
//***************************************************************************
/// CRC16 CCITT policy.
/// Calculates CRC16 CCITT using polynomial 0x1021
//***************************************************************************
struct crc_policy_16_ccitt
{
typedef uint16_t value_type;
//***************************************************************************
/// Calculates CRC-CCITT using polynomial 0x1021
/// \ingroup crc16_ccitt
//***************************************************************************
class crc16_ccitt
inline uint16_t initial() const
{
return 0xFFFF;
}
inline uint16_t add(uint16_t crc, uint8_t value) const
{
return (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ value) & 0xFF];
}
inline uint16_t final(uint16_t crc) const
{
return crc;
}
};
//*************************************************************************
/// CRC16 CCITT
//*************************************************************************
class crc16_ccitt : public etl::frame_check_sequence<etl::crc_policy_16_ccitt>
{
public:
typedef uint16_t value_type;
typedef uint16_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -81,63 +102,8 @@ namespace etl
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ *begin++) & 0xFF];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0xFFFF;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ *begin++) & 0xFF];
}
}
//*************************************************************************
/// \param value The uint8_t to add to the CRC.
//*************************************************************************
void add(uint8_t value)
{
crc = (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ value) & 0xFF];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc;
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return crc;
}
private:
value_type crc;
};
}

View File

@ -28,13 +28,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef __etl_crc16_kermit__
#define __etl_crc16_kermit__
#ifndef __ETL_CRC16_KERMIT__
#define __ETL_CRC16_KERMIT__
#include <stdint.h>
#include <iterator>
#include "static_assert.h"
#include "type_traits.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -50,18 +51,38 @@ namespace etl
/// \ingroup crc
//***************************************************************************
extern const uint16_t CRC_KERMIT[];
//***************************************************************************
/// Calculates CRC-Kermit using polynomial 0x1021
/// \ingroup crc16_kermit
/// CRC16 Kermit policy.
/// Calculates CRC16 Kermit using polynomial 0x1021
//***************************************************************************
class crc16_kermit
struct crc_policy_16_kermit
{
typedef uint16_t value_type;
inline uint16_t initial() const
{
return 0;
}
inline uint16_t add(uint16_t crc, uint8_t value) const
{
return (crc >> 8) ^ CRC_KERMIT[(crc ^ value) & 0xFF];
}
inline uint16_t final(uint16_t crc) const
{
return crc;
}
};
//*************************************************************************
/// CRC16 Kermit
//*************************************************************************
class crc16_kermit : public etl::frame_check_sequence<etl::crc_policy_16_kermit>
{
public:
typedef uint16_t value_type;
typedef uint16_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -81,63 +102,8 @@ namespace etl
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ *begin++) & 0xFF];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ *begin++) & 0xFF];
}
}
//*************************************************************************
/// \param value The uint8_t to add to the CRC.
//*************************************************************************
void add(uint8_t value)
{
crc = (crc >> 8) ^ CRC_KERMIT[(crc ^ value) & 0xFF];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc;
}
//*************************************************************************
/// Conversion operator to value_type
//*************************************************************************
operator value_type () const
{
return crc;
}
private:
value_type crc;
};
}

89
crc32.h
View File

@ -35,6 +35,7 @@ SOFTWARE.
#include <iterator>
#include "static_assert.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -52,16 +53,36 @@ namespace etl
extern const uint32_t CRC32[];
//***************************************************************************
/// CRC32 policy.
/// Calculates CRC32 using polynomial 0x04C11DB7.
/// \ingroup crc32
//***************************************************************************
class crc32
struct crc_policy_32
{
typedef uint32_t value_type;
inline uint32_t initial() const
{
return 0xFFFFFFFF;
}
inline uint32_t add(uint32_t crc, uint8_t value) const
{
return (crc >> 8) ^ CRC32[(crc ^ value) & 0xFF];
}
inline uint32_t final(uint32_t crc) const
{
return crc ^ 0xFFFFFFFF;
}
};
//*************************************************************************
/// CRC32
//*************************************************************************
class crc32 : public etl::frame_check_sequence<etl::crc_policy_32>
{
public:
typedef uint32_t value_type;
typedef uint32_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -79,64 +100,10 @@ namespace etl
crc32(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = (crc >> 8) ^ CRC32[(crc ^ *begin++) & 0xFF];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0xFFFFFFFF;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = (crc >> 8) ^ CRC32[(crc ^ *begin++) & 0xFF];
}
}
//*************************************************************************
/// \param value The uint8_t to add to the CRC.
//*************************************************************************
void add(uint8_t value)
{
crc = (crc >> 8) ^ CRC32[(crc ^ value) & 0xFF];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc ^ 0xFFFFFFFF;
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return crc ^ 0xFFFFFFFF;
}
private:
value_type crc;
};
}

View File

@ -32,9 +32,10 @@ SOFTWARE.
#define __ETL_CRC64_ECMA__
#include <stdint.h>
#include <iterator>
#include "static_assert.h"
#include "type_traits.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -52,16 +53,36 @@ namespace etl
extern const uint64_t CRC64_ECMA[];
//***************************************************************************
/// Calculates CRC64-ECMA using polynomial 0x42F0E1EBA9EA3693.
/// \ingroup crc64_ecma
/// CRC64 policy.
/// Calculates CRC64 ECMA using polynomial 0x42F0E1EBA9EA3693.
//***************************************************************************
class crc64_ecma
struct crc_policy_64_ecma
{
typedef uint64_t value_type;
inline uint64_t initial() const
{
return 0;
}
inline uint64_t add(uint64_t crc, uint8_t value) const
{
return (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ value) & 0xFF];
}
inline uint64_t final(uint64_t crc) const
{
return crc;
}
};
//*************************************************************************
/// CRC64 ECMA
//*************************************************************************
class crc64_ecma : public etl::frame_check_sequence<etl::crc_policy_64_ecma>
{
public:
typedef uint64_t value_type;
typedef uint64_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -81,63 +102,8 @@ namespace etl
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ *begin++) & 0xFF];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ *begin++) & 0xFF];
}
}
//*************************************************************************
/// \param value The value to add to the CRC.
//*************************************************************************
void add(uint8_t value)
{
crc = (crc << 8) ^ CRC64_ECMA[((crc >> 56) ^ value) & 0xFF];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc;
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return crc;
}
private:
value_type crc;
};
}

View File

@ -32,9 +32,10 @@ SOFTWARE.
#define __ETL_CRC8_CCITT__
#include <stdint.h>
#include <iterator>
#include "static_assert.h"
#include "type_traits.h"
#include "frame_check_sequence.h"
#if defined(COMPILER_KEIL)
#pragma diag_suppress 1300
@ -52,16 +53,36 @@ namespace etl
extern const uint8_t CRC8_CCITT[];
//***************************************************************************
/// CRC8 CCITT policy.
/// Calculates CRC8 CCITT using polynomial 0x07.
/// \ingroup crc8_ccitt
//***************************************************************************
class crc8_ccitt
struct crc_policy_8_ccitt
{
typedef uint8_t value_type;
inline uint8_t initial() const
{
return 0;
}
inline uint8_t add(uint8_t crc, uint8_t value) const
{
return CRC8_CCITT[crc ^ value];
}
inline uint8_t final(uint8_t crc) const
{
return crc;
}
};
//*************************************************************************
/// CRC8 CCITT
//*************************************************************************
class crc8_ccitt : public etl::frame_check_sequence<etl::crc_policy_8_ccitt>
{
public:
typedef uint8_t value_type;
typedef uint8_t argument_type;
//*************************************************************************
/// Default constructor.
//*************************************************************************
@ -79,63 +100,10 @@ namespace etl
crc8_ccitt(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
while (begin != end)
{
crc = CRC8_CCITT[crc ^ *begin++];
}
add(begin, end);
}
//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
crc = 0;
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
crc = CRC8_CCITT[crc ^ *begin++];
}
}
//*************************************************************************
/// \param value The char to add to the CRC.
//*************************************************************************
void add(uint8_t value)
{
crc = CRC8_CCITT[crc ^ value];
}
//*************************************************************************
/// Gets the CRC value.
//*************************************************************************
value_type value() const
{
return crc;
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return crc;
}
private:
value_type crc;
};
}

133
frame_check_sequence.h Normal file
View File

@ -0,0 +1,133 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
http://www.etlcpp.com
Copyright(c) 2014 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef __ETL_FRAME_CHECK_SEQUENCE__
#define __ETL_FRAME_CHECK_SEQUENCE__
#include <stdint.h>
#include "static_assert.h"
#include "type_traits.h"
#include "binary.h"
///\defgroup frame_check_sequence Frame check sequence calculation
///\ingroup maths
namespace etl
{
//***************************************************************************
/// Calculates a frame check sequence according to the specified policy.
///\tparam TPolicy The type used to enact the policy.
///\ingroup frame_check_sequence
//***************************************************************************
template <typename TPolicy>
class frame_check_sequence
{
public:
typedef TPolicy policy_type;
typedef typename policy_type::value_type value_type;
STATIC_ASSERT(etl::is_unsigned<value_type>::value, "Signed frame check type not supported");
//*************************************************************************
/// Default constructor.
//*************************************************************************
frame_check_sequence()
{
reset();
}
//*************************************************************************
/// Constructor from range.
/// \param begin Start of the range.
/// \param end End of the range.
//*************************************************************************
template<typename TIterator>
frame_check_sequence(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
reset();
add(begin, end);
}
//*************************************************************************
/// Resets the FCS to the initial state.
//*************************************************************************
void reset()
{
frame_check = policy.initial();
}
//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
while (begin != end)
{
frame_check = policy.add(frame_check, *begin++);
}
}
//*************************************************************************
/// \param value The uint8_t to add to the FCS.
//*************************************************************************
void add(uint8_t value)
{
frame_check = policy.add(frame_check, value);
}
//*************************************************************************
/// Gets the FCS value.
//*************************************************************************
value_type value() const
{
return policy.final(frame_check);
}
//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type () const
{
return policy.final(frame_check);
}
private:
value_type frame_check;
policy_type policy;
};
}
#endif