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/FPrimeBasicTypes.hpp>
14 #include <Fw/Types/Assert.hpp>
15 
16 #if FW_SERIALIZABLE_TO_STRING
17 #include <Fw/Types/String.hpp>
18 #endif
19 #include <cstring>
20 
21 namespace Fw {
22 
23 Buffer::Buffer() : Serializable(), m_serialize_repr(), m_bufferData(nullptr), m_size(0), m_context(0xFFFFFFFF) {}
24 
26  : Serializable(), m_serialize_repr(), m_bufferData(src.m_bufferData), m_size(src.m_size), m_context(src.m_context) {
27  if (src.m_bufferData != nullptr) {
28  this->m_serialize_repr.setExtBuffer(src.m_bufferData, src.m_size);
29  }
30 }
31 
32 Buffer::Buffer(U8* data, FwSizeType size, U32 context)
33  : Serializable(), m_serialize_repr(), m_bufferData(data), m_size(size), m_context(context) {
34  if (m_bufferData != nullptr) {
35  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
36  }
37 }
38 
40  // Ward against self-assignment
41  if (this != &src) {
42  this->set(src.m_bufferData, src.m_size, src.m_context);
43  }
44  return *this;
45 }
46 
47 bool Buffer::operator==(const Buffer& src) const {
48  return (this->m_bufferData == src.m_bufferData) && (this->m_size == src.m_size) &&
49  (this->m_context == src.m_context);
50 }
51 
52 bool Buffer::isValid() const {
53  return (this->m_bufferData != nullptr) && (this->m_size > 0);
54 }
55 
56 U8* Buffer::getData() const {
57  return this->m_bufferData;
58 }
59 
61  return this->m_size;
62 }
63 
64 U32 Buffer::getContext() const {
65  return this->m_context;
66 }
67 
68 void Buffer::setData(U8* const data) {
69  this->m_bufferData = data;
70  if (m_bufferData != nullptr) {
71  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
72  }
73 }
74 
75 void Buffer::setSize(const FwSizeType size) {
76  this->m_size = size;
77  if (m_bufferData != nullptr) {
78  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
79  }
80 }
81 
82 void Buffer::setContext(const U32 context) {
83  this->m_context = context;
84 }
85 
86 void Buffer::set(U8* const data, const FwSizeType size, const U32 context) {
87  this->m_bufferData = data;
88  this->m_size = size;
89  if (m_bufferData != nullptr) {
90  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
91  }
92  this->m_context = context;
93 }
94 
96  if (this->isValid()) {
97  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData, this->m_size);
98  esb.resetSer();
99  return esb;
100  } else {
102  }
103 }
104 
106  if (this->isValid()) {
107  Fw::ExternalSerializeBufferWithMemberCopy esb(this->m_bufferData, this->m_size);
108  Fw::SerializeStatus stat = esb.setBuffLen(this->m_size);
110  return esb;
111  } else {
113  }
114 }
115 
117  Fw::SerializeStatus stat;
118 #if FW_SERIALIZATION_TYPE_ID
119  stat = buffer.serialize(static_cast<U32>(Buffer::TYPE_ID));
120  if (stat != Fw::FW_SERIALIZE_OK) {
121  return stat;
122  }
123 #endif
124  stat = buffer.serialize(reinterpret_cast<PlatformPointerCastType>(this->m_bufferData));
125  if (stat != Fw::FW_SERIALIZE_OK) {
126  return stat;
127  }
128  stat = buffer.serialize(this->m_size);
129  if (stat != Fw::FW_SERIALIZE_OK) {
130  return stat;
131  }
132  stat = buffer.serialize(this->m_context);
133  if (stat != Fw::FW_SERIALIZE_OK) {
134  return stat;
135  }
136  return stat;
137 }
138 
140  Fw::SerializeStatus stat;
141 
142 #if FW_SERIALIZATION_TYPE_ID
143  U32 typeId;
144 
145  stat = buffer.deserialize(typeId);
146  if (stat != Fw::FW_SERIALIZE_OK) {
147  return stat;
148  }
149 
150  if (typeId != Buffer::TYPE_ID) {
152  }
153 #endif
154  PlatformPointerCastType pointer;
155  stat = buffer.deserialize(pointer);
156  if (stat != Fw::FW_SERIALIZE_OK) {
157  return stat;
158  }
159  this->m_bufferData = reinterpret_cast<U8*>(pointer);
160 
161  stat = buffer.deserialize(this->m_size);
162  if (stat != Fw::FW_SERIALIZE_OK) {
163  return stat;
164  }
165  stat = buffer.deserialize(this->m_context);
166  if (stat != Fw::FW_SERIALIZE_OK) {
167  return stat;
168  }
169 
170  if (this->m_bufferData != nullptr) {
171  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
172  }
173  return stat;
174 }
175 
176 #if FW_SERIALIZABLE_TO_STRING
177 void Buffer::toString(Fw::StringBase& text) const {
178  static const char* formatString = "(data = %p, size = %u, context = %u)";
179  text.format(formatString, this->m_bufferData, this->m_size, this->m_context);
180 }
181 #endif
182 
183 #ifdef BUILD_UT
184 std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
185  Fw::String str;
186  obj.toString(str);
187  os << str.toChar();
188  return os;
189 }
190 #endif
191 } // end namespace Fw
Serialization/Deserialization operation was successful.
void setData(U8 *data)
Definition: Buffer.cpp:68
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &serialBuffer) const
Definition: Buffer.cpp:116
void set(U8 *data, FwSizeType size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:86
PlatformSizeType FwSizeType
void resetSer()
reset to beginning of buffer to reuse for serialization
void setContext(U32 context)
Definition: Buffer.cpp:82
void setSize(FwSizeType size)
Definition: Buffer.cpp:75
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
const char * toChar() const
Definition: String.hpp:50
U32 getContext() const
Definition: Buffer.cpp:64
U8 * getData() const
Definition: Buffer.cpp:56
bool operator==(const Buffer &src) const
Definition: Buffer.cpp:47
SerializeStatus
forward declaration for string
ExternalSerializeBufferWithMemberCopy getDeserializer()
Definition: Buffer.cpp:105
bool isValid() const
Definition: Buffer.cpp:52
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
FwSizeType getSize() const
Definition: Buffer.cpp:60
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:139
ExternalSerializeBufferWithMemberCopy getSerializer()
Definition: Buffer.cpp:95
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Buffer & operator=(const Buffer &src)
Definition: Buffer.cpp:39
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data