Embedded Template Library 1.0
frame_check_sequence.h
Go to the documentation of this file.
1
3
4/******************************************************************************
5The MIT License(MIT)
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9Copyright(c) 2014 John Wellbelove
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25******************************************************************************/
26
27#ifndef ETL_FRAME_CHECK_SEQUENCE_INCLUDED
28#define ETL_FRAME_CHECK_SEQUENCE_INCLUDED
29
30#include "platform.h"
31#include "static_assert.h"
32#include "type_traits.h"
33#include "binary.h"
34#include "iterator.h"
35
36#include <stdint.h>
37
38ETL_STATIC_ASSERT(ETL_USING_8BIT_TYPES, "This file does not currently support targets with no 8bit type");
39
42
43namespace etl
44{
45 namespace private_frame_check_sequence
46 {
47 //***************************************************
50 //***************************************************
51 template <typename TFCS>
52 class add_insert_iterator : public etl::iterator<ETL_OR_STD::output_iterator_tag, void, void, void, void>
53 {
54 public:
55
56 //***********************************
57 explicit add_insert_iterator(TFCS& fcs) ETL_NOEXCEPT
58 : p_fcs(&fcs)
59 {
60 }
61
62 //***********************************
63 add_insert_iterator& operator*() ETL_NOEXCEPT
64 {
65 return *this;
66 }
67
68 //***********************************
69 add_insert_iterator& operator++() ETL_NOEXCEPT
70 {
71 return *this;
72 }
73
74 //***********************************
75 add_insert_iterator& operator++(int) ETL_NOEXCEPT
76 {
77 return *this;
78 }
79
80 //***********************************
81 add_insert_iterator& operator =(uint8_t value)
82 {
83 p_fcs->add(value);
84 return *this;
85 }
86
87 private:
88
89 TFCS* p_fcs;
90 };
91 }
92
93 //***************************************************************************
97 //***************************************************************************
98 template <typename TPolicy>
100 {
101 public:
102
103 typedef TPolicy policy_type;
104 typedef typename policy_type::value_type value_type;
106
107 ETL_STATIC_ASSERT(etl::is_unsigned<value_type>::value, "Signed frame check type not supported");
108
109 //*************************************************************************
111 //*************************************************************************
113 {
114 reset();
115 }
116
117 //*************************************************************************
121 //*************************************************************************
122 template<typename TIterator>
123 frame_check_sequence(TIterator begin, const TIterator end)
124 {
125 ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
126
127 reset();
128 add(begin, end);
129 }
130
131 //*************************************************************************
133 //*************************************************************************
134 void reset()
135 {
136 frame_check = policy.initial();
137 }
138
139 //*************************************************************************
143 //*************************************************************************
144 template<typename TIterator>
145 void add(TIterator begin, const TIterator end)
146 {
147 ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
148
149 while (begin != end)
150 {
151 frame_check = policy.add(frame_check, *begin);
152 ++begin;
153 }
154 }
155
156 //*************************************************************************
158 //*************************************************************************
159 void add(uint8_t value_)
160 {
161 frame_check = policy.add(frame_check, value_);
162 }
163
164 //*************************************************************************
166 //*************************************************************************
167 value_type value() const
168 {
169 return policy.final(frame_check);
170 }
171
172 //*************************************************************************
174 //*************************************************************************
175 operator value_type () const
176 {
177 return policy.final(frame_check);
178 }
179
180 //*************************************************************************
182 //*************************************************************************
184 {
185 return add_insert_iterator(*this);
186 }
187
188 private:
189
190 value_type frame_check;
191 policy_type policy;
192 };
193}
194
195#endif
Definition: frame_check_sequence.h:53
void reset()
Resets the FCS to the initial state.
Definition: frame_check_sequence.h:134
add_insert_iterator input()
Gets an add_insert_iterator for input.
Definition: frame_check_sequence.h:183
void add(uint8_t value_)
Definition: frame_check_sequence.h:159
frame_check_sequence(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:123
void add(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:145
frame_check_sequence()
Default constructor.
Definition: frame_check_sequence.h:112
value_type value() const
Gets the FCS value.
Definition: frame_check_sequence.h:167
Definition: frame_check_sequence.h:100
is_unsigned
Definition: type_traits_generator.h:1021
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: iterator.h:931
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: iterator.h:961
iterator
Definition: iterator.h:399