Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers frame_check_sequence.h Source File

frame_check_sequence.h

Go to the documentation of this file.
00001 
00002 ///\file
00003 
00004 /******************************************************************************
00005 The MIT License(MIT)
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 Copyright(c) 2014 jwellbelove
00010 Permission is hereby granted, free of charge, to any person obtaining a copy
00011 of this software and associated documentation files(the "Software"), to deal
00012 in the Software without restriction, including without limitation the rights
00013 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00014 copies of the Software, and to permit persons to whom the Software is
00015 furnished to do so, subject to the following conditions :
00016 The above copyright notice and this permission notice shall be included in all
00017 copies or substantial portions of the Software.
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00024 SOFTWARE.
00025 ******************************************************************************/
00026 
00027 #ifndef __ETL_FRAME_CHECK_SEQUENCE__
00028 #define __ETL_FRAME_CHECK_SEQUENCE__
00029 
00030 #include <stdint.h>
00031 
00032 #include "platform.h "
00033 #include "static_assert.h"
00034 #include "type_traits.h "
00035 #include "binary.h "
00036 
00037 STATIC_ASSERT(ETL_8BIT_SUPPORT, "This file does not currently support targets with no 8bit type");
00038 
00039 ///\defgroup frame_check_sequence Frame check sequence calculation
00040 ///\ingroup maths
00041 
00042 namespace etl
00043 {
00044   //***************************************************************************
00045   /// Calculates a frame check sequence according to the specified policy.
00046   ///\tparam TPolicy The type used to enact the policy.
00047   ///\ingroup frame_check_sequence
00048   //***************************************************************************
00049   template <typename TPolicy>
00050   class frame_check_sequence
00051   {
00052   public:
00053 
00054     typedef TPolicy policy_type;
00055     typedef typename policy_type::value_type value_type;
00056 
00057     STATIC_ASSERT(etl::is_unsigned<value_type>::value, "Signed frame check type not supported");
00058 
00059     //*************************************************************************
00060     /// Default constructor.
00061     //*************************************************************************
00062     frame_check_sequence()
00063     {
00064       reset();
00065     }
00066 
00067     //*************************************************************************
00068     /// Constructor from range.
00069     /// \param begin Start of the range.
00070     /// \param end   End of the range.
00071     //*************************************************************************
00072     template<typename TIterator>
00073     frame_check_sequence(TIterator begin, const TIterator end)
00074     {
00075       STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
00076 
00077       reset();
00078       add(begin, end);
00079     }
00080 
00081     //*************************************************************************
00082     /// Resets the FCS to the initial state.
00083     //*************************************************************************
00084     void reset()
00085     {
00086       frame_check = policy.initial();
00087     }
00088 
00089     //*************************************************************************
00090     /// Adds a range.
00091     /// \param begin
00092     /// \param end
00093     //*************************************************************************
00094     template<typename TIterator>
00095     void add(TIterator begin, const TIterator end)
00096     {
00097       STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
00098 
00099       while (begin != end)
00100       {
00101         frame_check = policy.add(frame_check, *begin++);
00102       }
00103     }
00104 
00105     //*************************************************************************
00106     /// \param value The uint8_t to add to the FCS.
00107     //*************************************************************************
00108     void add(uint8_t value_)
00109     {
00110       frame_check = policy.add(frame_check, value_);
00111     }
00112 
00113     //*************************************************************************
00114     /// Gets the FCS value.
00115     //*************************************************************************
00116     value_type value()
00117     {
00118       return policy.final(frame_check);
00119     }
00120 
00121     //*************************************************************************
00122     /// Conversion operator to value_type.
00123     //*************************************************************************
00124     operator value_type ()
00125     {
00126       return policy.final(frame_check);
00127     }
00128 
00129   private:
00130 
00131     value_type  frame_check;
00132     policy_type policy;
00133   };
00134 }
00135 
00136 #endif
00137