F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.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, U32 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 
72 U32 Buffer::getSize() const {
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 U32 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 U32 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  return m_serialize_repr;
109 }
110 
112  Fw::SerializeStatus stat;
113 #if FW_SERIALIZATION_TYPE_ID
114  stat = buffer.serialize(static_cast<U32>(Buffer::TYPE_ID));
115  if (stat != Fw::FW_SERIALIZE_OK) {
116  return stat;
117  }
118 #endif
119  stat = buffer.serialize(reinterpret_cast<POINTER_CAST>(this->m_bufferData));
120  if (stat != Fw::FW_SERIALIZE_OK) {
121  return stat;
122  }
123  stat = buffer.serialize(this->m_size);
124  if (stat != Fw::FW_SERIALIZE_OK) {
125  return stat;
126  }
127  stat = buffer.serialize(this->m_context);
128  if (stat != Fw::FW_SERIALIZE_OK) {
129  return stat;
130  }
131  return stat;
132 }
133 
135  Fw::SerializeStatus stat;
136 
137 #if FW_SERIALIZATION_TYPE_ID
138  U32 typeId;
139 
140  stat = buffer.deserialize(typeId);
141  if (stat != Fw::FW_SERIALIZE_OK) {
142  return stat;
143  }
144 
145  if (typeId != Buffer::TYPE_ID) {
147  }
148 #endif
149  POINTER_CAST pointer;
150  stat = buffer.deserialize(pointer);
151  if (stat != Fw::FW_SERIALIZE_OK) {
152  return stat;
153  }
154  this->m_bufferData = reinterpret_cast<U8*>(pointer);
155 
156  stat = buffer.deserialize(this->m_size);
157  if (stat != Fw::FW_SERIALIZE_OK) {
158  return stat;
159  }
160  stat = buffer.deserialize(this->m_context);
161  if (stat != Fw::FW_SERIALIZE_OK) {
162  return stat;
163  }
164 
165  if (this->m_bufferData != nullptr) {
166  this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
167  }
168  return stat;
169 }
170 
171 #if FW_SERIALIZABLE_TO_STRING
172 void Buffer::toString(Fw::StringBase& text) const {
173  static const char * formatString = "(data = %p, size = %u, context = %u)";
174  text.format(formatString, this->m_bufferData, this->m_size, this->m_context);
175 }
176 #endif
177 
178 #ifdef BUILD_UT
179  std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
180  Fw::String str;
181  obj.toString(str);
182  os << str.toChar();
183  return os;
184  }
185 #endif
186 } // end namespace Fw
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:57
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
C++-compatible configuration header for fprime configuration.
bool operator==(const Buffer &src) const
Definition: Buffer.cpp:60
U8 * getData() const
Definition: Buffer.cpp:68
void set(U8 *data, U32 size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:98
bool isValid() const
Definition: Buffer.cpp:64
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Definition: Buffer.cpp:134
U32 getSize() const
Definition: Buffer.cpp:72
void setData(U8 *data)
Definition: Buffer.cpp:80
void setContext(U32 context)
Definition: Buffer.cpp:94
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &serialBuffer) const
Definition: Buffer.cpp:111
Buffer & operator=(const Buffer &src)
Definition: Buffer.cpp:52
void setSize(U32 size)
Definition: Buffer.cpp:87
U32 getContext() const
Definition: Buffer.cpp:76
SerializeBufferBase & getSerializeRepr()
Definition: Buffer.cpp:107
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
forward declaration
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
void format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:56
const char * toChar() const
Definition: String.hpp:50
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_DESERIALIZE_TYPE_MISMATCH
Deserialized type ID didn't match.