F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
Checksum.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CFDP/Checksum/Checksum.cpp
3 // \author bocchino
4 // \brief cpp file for CFDP checksum class
5 //
6 // \copyright
7 // Copyright 2009-2016, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include "Fw/Types/Assert.hpp"
15 
16 static U32 min(const U32 a, const U32 b) {
17  return (a < b) ? a : b;
18 }
19 
20 namespace CFDP {
21 
23  Checksum() : value(0)
24  {
25 
26  }
27 
29  Checksum(const U32 value) : value(value)
30  {
31 
32  }
33 
35  Checksum(const Checksum &original)
36  {
37  this->value = original.getValue();
38  }
39 
41  ~Checksum()
42  {
43 
44  }
45 
47  operator=(const Checksum& checksum)
48  {
49  this->value = checksum.value;
50  return *this;
51  }
52 
54  operator==(const Checksum& checksum) const
55  {
56  return this->value == checksum.value;
57  }
58 
60  operator!=(const Checksum& checksum) const
61  {
62  return not (*this == checksum);
63  }
64 
66  getValue(void) const
67  {
68  return this->value;
69  }
70 
72  update(
73  const U8 *const data,
74  const U32 offset,
75  const U32 length
76  )
77  {
78  U32 index = 0;
79 
80  // Add the first word unaligned if necessary
81  const U32 offsetMod4 = offset % 4;
82  if (offsetMod4 != 0) {
83  const U8 wordLength = static_cast<U8>(min(length, 4 - offsetMod4));
84  this->addWordUnaligned(
85  &data[index],
86  static_cast<U8>(offset + index),
87  wordLength
88  );
89  index += wordLength;
90  }
91 
92  // Add the middle words aligned
93  for ( ; index + 4 <= length; index += 4)
94  addWordAligned(&data[index]);
95 
96  // Add the last word unaligned if necessary
97  if (index < length) {
98  const U8 wordLength = static_cast<U8>(length - index);
99  this->addWordUnaligned(
100  &data[index],
101  static_cast<U8>(offset + index),
102  wordLength
103  );
104  }
105 
106  }
107 
108  void Checksum ::
109  addWordAligned(const U8 *const word)
110  {
111  for (U8 i = 0; i < 4; ++i)
112  addByteAtOffset(word[i], i);
113  }
114 
117  const U8 *word,
118  const U8 position,
119  const U8 length
120  )
121  {
122  FW_ASSERT(length < 4);
123  U8 offset = position % 4;
124  for (U8 i = 0; i < length; ++i) {
125  addByteAtOffset(word[i], offset);
126  ++offset;
127  if (offset == 4)
128  offset = 0;
129  }
130  }
131 
134  const U8 byte,
135  const U8 offset
136  )
137  {
138  FW_ASSERT(offset < 4);
139  const U32 addend = byte << (8*(3-offset));
140  this->value += addend;
141  }
142 
143 }
CFDP::Checksum::operator!=
bool operator!=(const Checksum &checksum) const
Compare checksum and this for inequality.
Definition: Checksum.cpp:60
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
CFDP::Checksum::update
void update(const U8 *const data, const U32 offset, const U32 length)
Update the checksum value by accumulating the words in the data.
Definition: Checksum.cpp:72
CFDP::Checksum::addByteAtOffset
void addByteAtOffset(const U8 byte, const U8 offset)
Add byte to value at offset in word.
Definition: Checksum.cpp:133
CFDP::Checksum::getValue
U32 getValue(void) const
Get the checksum value.
Definition: Checksum.cpp:66
Assert.hpp
CFDP::Checksum::Checksum
Checksum()
Construct a fresh Checksum object.
Definition: Checksum.cpp:23
CFDP::Checksum::addWordUnaligned
void addWordUnaligned(const U8 *const word, const U8 position, const U8 length)
Add a four-byte unaligned word to the checksum value.
Definition: Checksum.cpp:116
CFDP::Checksum::operator=
const Checksum & operator=(const Checksum &checksum)
Assign checksum to this.
Definition: Checksum.cpp:47
CFDP::Checksum::operator==
bool operator==(const Checksum &checksum) const
Comapre checksum and this for equality.
Definition: Checksum.cpp:54
min
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
CFDP
Definition: Checksum.cpp:20
CFDP::Checksum::~Checksum
~Checksum()
Destroy a Checksum object.
Definition: Checksum.cpp:41
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
CFDP::Checksum
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
Checksum.hpp