F´ Flight Software - C/C++ Documentation  NASA-v2.1.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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/Types/BasicTypes.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(NULL),
26  m_size(0),
27  m_context(0xFFFFFFFF)
28 {}
29 
31  m_serialize_repr(src.m_bufferData, src.m_size),
32  m_bufferData(src.m_bufferData),
33  m_size(src.m_size),
34  m_context(src.m_context)
35 {}
36 
37 Buffer::Buffer(U8* data, U32 size, U32 context) : Serializable(),
38  m_serialize_repr(),
39  m_bufferData(data),
40  m_size(size),
41  m_context(context)
42 {
43  if(m_bufferData != NULL){
44  this->m_serialize_repr.setExtBuffer(m_bufferData, m_size);
45  }
46 }
47 
49  // Ward against self-assignment
50  if (this != &src) {
51  this->set(src.m_bufferData, src.m_size, src.m_context);
52  }
53  return *this;
54 }
55 
56 bool Buffer::operator==(const Buffer& src) const {
57  return (this->m_bufferData == src.m_bufferData) && (this->m_size == src.m_size) && (this->m_context == src.m_context);
58 }
59 
60 U8* Buffer::getData() const {
61  return this->m_bufferData;
62 }
63 
64 U32 Buffer::getSize() const {
65  return this->m_size;
66 }
67 
68 U32 Buffer::getContext() const {
69  return this->m_context;
70 }
71 
72 void Buffer::setData(U8* const data) {
73  this->m_bufferData = data;
74  if (m_bufferData != NULL) {
75  this->m_serialize_repr.setExtBuffer(m_bufferData, m_size);
76  }
77 }
78 
79 void Buffer::setSize(const U32 size) {
80  this->m_size = size;
81  if (m_bufferData != NULL) {
82  this->m_serialize_repr.setExtBuffer(m_bufferData, m_size);
83  }
84 }
85 
86 void Buffer::setContext(const U32 context) {
87  this->m_context = context;
88 }
89 
90 void Buffer::set(U8* const data, const U32 size, const U32 context) {
91  this->m_bufferData = data;
92  this->m_size = size;
93  if (m_bufferData != NULL) {
94  this->m_serialize_repr.setExtBuffer(m_bufferData, m_size);
95  }
96  this->m_context = context;
97 }
98 
100  return m_serialize_repr;
101 }
102 
104  Fw::SerializeStatus stat;
105 #if FW_SERIALIZATION_TYPE_ID
106  stat = buffer.serialize((U32)Buffer::TYPE_ID);
107  if (stat != Fw::FW_SERIALIZE_OK) {
108  return stat;
109  }
110 #endif
111  stat = buffer.serialize(reinterpret_cast<POINTER_CAST>(this->m_bufferData));
112  if (stat != Fw::FW_SERIALIZE_OK) {
113  return stat;
114  }
115  stat = buffer.serialize(this->m_size);
116  if (stat != Fw::FW_SERIALIZE_OK) {
117  return stat;
118  }
119  stat = buffer.serialize(this->m_context);
120  if (stat != Fw::FW_SERIALIZE_OK) {
121  return stat;
122  }
123  return stat;
124 }
125 
127  Fw::SerializeStatus stat;
128 
129 #if FW_SERIALIZATION_TYPE_ID
130  U32 typeId;
131 
132  stat = buffer.deserialize(typeId);
133  if (stat != Fw::FW_SERIALIZE_OK) {
134  return stat;
135  }
136 
137  if (typeId != Buffer::TYPE_ID) {
139  }
140 #endif
141  POINTER_CAST pointer;
142  stat = buffer.deserialize(pointer);
143  if (stat != Fw::FW_SERIALIZE_OK) {
144  return stat;
145  }
146  this->m_bufferData = reinterpret_cast<U8*>(pointer);
147 
148  stat = buffer.deserialize(this->m_size);
149  if (stat != Fw::FW_SERIALIZE_OK) {
150  return stat;
151  }
152  stat = buffer.deserialize(this->m_context);
153  if (stat != Fw::FW_SERIALIZE_OK) {
154  return stat;
155  }
156  return stat;
157 }
158 
159 #if FW_SERIALIZABLE_TO_STRING || BUILD_UT
160 void Buffer::toString(Fw::StringBase& text) const {
161  static const char * formatString = "(data = %p, size = %u,context = %u)";
162  char outputString[FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE];
163 
164  (void)snprintf(outputString, FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE, formatString, this->m_bufferData, this->m_size,
165  this->m_context);
166  // Force NULL termination
167  outputString[FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE-1] = 0;
168  text = outputString;
169 }
170 #endif
171 
172 #ifdef BUILD_UT
173  std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
174  Fw::String str;
175  obj.toString(str);
176  os << str.toChar();
177  return os;
178  }
179 #endif
180 } // end namespace Fw
Fw::SerializeBufferBase
Definition: Serializable.hpp:43
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::Buffer::operator=
Buffer & operator=(const Buffer &src)
Definition: Buffer.cpp:48
Fw::Buffer::setContext
void setContext(U32 context)
Definition: Buffer.cpp:86
Fw::Buffer::getData
U8 * getData() const
Definition: Buffer.cpp:60
Fw::SerializeBufferBase::serialize
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Serializable.cpp:67
Fw::StringBase
Definition: StringType.hpp:23
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Buffer.hpp
Fw::Buffer::setData
void setData(U8 *data)
Definition: Buffer.cpp:72
Fw::Buffer::getContext
U32 getContext() const
Definition: Buffer.cpp:68
Fw::Buffer
Definition: Buffer.hpp:43
Fw::Serializable
forward declaration
Definition: Serializable.hpp:26
Fw::String::toChar
const char * toChar() const
gets char buffer
Definition: String.cpp:40
Assert.hpp
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw::Buffer::getSerializeRepr
SerializeBufferBase & getSerializeRepr()
Definition: Buffer.cpp:99
Fw::Buffer::getSize
U32 getSize() const
Definition: Buffer.cpp:64
FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE
#define FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE
Size of string to store toString() string output.
Definition: FpConfig.hpp:306
Fw::Buffer::setSize
void setSize(U32 size)
Definition: Buffer.cpp:79
Fw::Buffer::set
void set(U8 *data, U32 size, U32 context=NO_CONTEXT)
Definition: Buffer.cpp:90
Fw::ExternalSerializeBuffer::setExtBuffer
void setExtBuffer(U8 *buffPtr, NATIVE_UINT_TYPE size)
Set the external buffer.
Definition: Serializable.cpp:705
String.hpp
Fw::String
Definition: String.hpp:10
Fw::SerializeBufferBase::deserialize
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
Definition: Serializable.cpp:290
Fw::FW_DESERIALIZE_TYPE_MISMATCH
@ FW_DESERIALIZE_TYPE_MISMATCH
Deserialized type ID didn't match.
Definition: Serializable.hpp:21
Fw::Buffer::Buffer
Buffer()
Definition: Buffer.cpp:23
BasicTypes.hpp
Declares ISF basic types.
Fw::Buffer::operator==
bool operator==(const Buffer &src) const
Definition: Buffer.cpp:56
Fw::Buffer::serialize
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &serialBuffer) const
Definition: Buffer.cpp:103
Fw::Buffer::deserialize
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Definition: Buffer.cpp:126
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100
Fw
Definition: Buffer.cpp:21