F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Buffer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Buffer.cpp
3 // \author mstarch
4 // \brief cpp file for Fw::Buffer implementation
5 //
6 // \copyright
7 // Copyright 2009-2020, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 #include <Fw/Buffer/Buffer.hpp>
13 #include <Fw/Types/Assert.hpp>
14 #include <Fw/FPrimeBasicTypes.hpp>
15 
16 #if FW_SERIALIZABLE_TO_STRING
17  #include <Fw/Types/String.hpp>
18 #endif
19 #include <cstring>
20 
21 namespace Fw {
22 
24  m_serialize_repr(),
25  m_bufferData(nullptr),
26  m_size(0),
27  m_context(0xFFFFFFFF)
28 {}
29 
31  m_serialize_repr(),
32  m_bufferData(src.m_bufferData),
33  m_size(src.m_size),
34  m_context(src.m_context)
35 {
36  if(src.m_bufferData != nullptr){
37  this->m_serialize_repr.setExtBuffer(src.m_bufferData, src.m_size);
38  }
39 }
40 
41 Buffer::Buffer(U8* data, SizeType size, U32 context) : Serializable(),
42  m_serialize_repr(),
43  m_bufferData(data),
44  m_size(size),
45  m_context(context)
46 {
47  if(m_bufferData != nullptr){
48  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
49  }
50 }
51 
53  // Ward against self-assignment
54  if (this != &src) {
55  this->set(src.m_bufferData, src.m_size, src.m_context);
56  }
57  return *this;
58 }
59 
60 bool Buffer::operator==(const Buffer& src) const {
61  return (this->m_bufferData == src.m_bufferData) && (this->m_size == src.m_size) && (this->m_context == src.m_context);
62 }
63 
64 bool Buffer::isValid() const {
65  return (this->m_bufferData != nullptr) && (this->m_size > 0);
66 }
67 
68 U8* Buffer::getData() const {
69  return this->m_bufferData;
70 }
71 
73  return this->m_size;
74 }
75 
76 U32 Buffer::getContext() const {
77  return this->m_context;
78 }
79 
80 void Buffer::setData(U8* const data) {
81  this->m_bufferData = data;
82  if (m_bufferData != nullptr) {
83  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
84  }
85 }
86 
87 void Buffer::setSize(const SizeType size) {
88  this->m_size = size;
89  if (m_bufferData != nullptr) {
90  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
91  }
92 }
93 
94 void Buffer::setContext(const U32 context) {
95  this->m_context = context;
96 }
97 
98 void Buffer::set(U8* const data, const SizeType size, const U32 context) {
99  this->m_bufferData = data;
100  this->m_size = size;
101  if (m_bufferData != nullptr) {
102  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
103  }
104  this->m_context = context;
105 }
106 
108  if (this->isValid()) {
109  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData, static_cast<Fw::Serializable::SizeType>(this->m_size));
110  esb.resetSer();
111  return esb;
112  } else {
114  }
115 }
116 
118  if (this->isValid()) {
119  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData, static_cast<Fw::Serializable::SizeType>(this->m_size));
120  Fw::SerializeStatus stat = esb.setBuffLen(static_cast<Fw::Serializable::SizeType>(this->m_size));
122  return esb;
123  } else {
125  }
126 }
127 
129  Fw::SerializeStatus stat;
130 #if FW_SERIALIZATION_TYPE_ID
131  stat = buffer.serialize(static_cast<U32>(Buffer::TYPE_ID));
132  if (stat != Fw::FW_SERIALIZE_OK) {
133  return stat;
134  }
135 #endif
136  stat = buffer.serialize(reinterpret_cast<PlatformPointerCastType>(this->m_bufferData));
137  if (stat != Fw::FW_SERIALIZE_OK) {
138  return stat;
139  }
140  stat = buffer.serialize(this->m_size);
141  if (stat != Fw::FW_SERIALIZE_OK) {
142  return stat;
143  }
144  stat = buffer.serialize(this->m_context);
145  if (stat != Fw::FW_SERIALIZE_OK) {
146  return stat;
147  }
148  return stat;
149 }
150 
152  Fw::SerializeStatus stat;
153 
154 #if FW_SERIALIZATION_TYPE_ID
155  U32 typeId;
156 
157  stat = buffer.deserialize(typeId);
158  if (stat != Fw::FW_SERIALIZE_OK) {
159  return stat;
160  }
161 
162  if (typeId != Buffer::TYPE_ID) {
164  }
165 #endif
166  PlatformPointerCastType pointer;
167  stat = buffer.deserialize(pointer);
168  if (stat != Fw::FW_SERIALIZE_OK) {
169  return stat;
170  }
171  this->m_bufferData = reinterpret_cast<U8*>(pointer);
172 
173  stat = buffer.deserialize(this->m_size);
174  if (stat != Fw::FW_SERIALIZE_OK) {
175  return stat;
176  }
177  stat = buffer.deserialize(this->m_context);
178  if (stat != Fw::FW_SERIALIZE_OK) {
179  return stat;
180  }
181 
182  if (this->m_bufferData != nullptr) {
183  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
184  }
185  return stat;
186 }
187 
188 #if FW_SERIALIZABLE_TO_STRING
189 void Buffer::toString(Fw::StringBase& text) const {
190  static const char * formatString = "(data = %p, size = %u, context = %u)";
191  text.format(formatString, this->m_bufferData, this->m_size, this->m_context);
192 }
193 #endif
194 
195 #ifdef BUILD_UT
196  std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
197  Fw::String str;
198  obj.toString(str);
199  os << str.toChar();
200  return os;
201  }
202 #endif
203 } // end namespace Fw
Serialization/Deserialization operation was successful.
void setData(U8 *data)
Definition: Buffer.cpp:80
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &serialBuffer) const
Definition: Buffer.cpp:128
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
void setContext(SizeType context)
Definition: Buffer.cpp:94
const char * toChar() const
Definition: String.hpp:50
void setSize(SizeType size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
U8 * getData() const
Definition: Buffer.cpp:68
bool operator==(const Buffer &src) const
Definition: Buffer.cpp:60
SerializeStatus
forward declaration for string
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:117
bool isValid() const
Definition: Buffer.cpp:64
Deserialized type ID didn&#39;t match.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:55
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
forward declaration
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Definition: Buffer.cpp:151
SizeType getSize() const
Definition: Buffer.cpp:72
void set(U8 *data, SizeType size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:98
U32 SizeType
The size type for a buffer.
Definition: Buffer.hpp:47
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:107
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Buffer & operator=(const Buffer &src)
Definition: Buffer.cpp:52
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data