F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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
21namespace Fw {
22
24 m_serialize_repr(),
25 m_bufferData(nullptr),
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
37Buffer::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 != nullptr){
44 this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->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
56bool 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
61 return this->m_bufferData;
62}
63
64U32 Buffer::getSize() const {
65 return this->m_size;
66}
67
68U32 Buffer::getContext() const {
69 return this->m_context;
70}
71
72void Buffer::setData(U8* const data) {
73 this->m_bufferData = data;
74 if (m_bufferData != nullptr) {
75 this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
76 }
77}
78
79void Buffer::setSize(const U32 size) {
80 this->m_size = size;
81 if (m_bufferData != nullptr) {
82 this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
83 }
84}
85
86void Buffer::setContext(const U32 context) {
87 this->m_context = context;
88}
89
90void 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 != nullptr) {
94 this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
95 }
96 this->m_context = context;
97}
98
100 return m_serialize_repr;
101}
102
105#if FW_SERIALIZATION_TYPE_ID
106 stat = buffer.serialize(static_cast<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
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
157 if (this->m_bufferData != nullptr) {
158 this->m_serialize_repr.setExtBuffer(this->m_bufferData, this->m_size);
159 }
160 return stat;
161}
162
163#if FW_SERIALIZABLE_TO_STRING || BUILD_UT
164void Buffer::toString(Fw::StringBase& text) const {
165 static const char * formatString = "(data = %p, size = %u,context = %u)";
166 char outputString[FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE];
167
168 (void)snprintf(outputString, FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE, formatString, this->m_bufferData, this->m_size,
169 this->m_context);
170 // Force NULL termination
171 outputString[FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE-1] = 0;
172 text = outputString;
173}
174#endif
175
176#ifdef BUILD_UT
177 std::ostream& operator<<(std::ostream& os, const Buffer& obj) {
178 Fw::String str;
179 obj.toString(str);
180 os << str.toChar();
181 return os;
182 }
183#endif
184} // end namespace Fw
PlatformPointerCastType POINTER_CAST
Definition BasicTypes.h:53
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
C++-compatible configuration header for fprime configuration.
bool operator==(const Buffer &src) const
Definition Buffer.cpp:56
U8 * getData() const
Definition Buffer.cpp:60
void set(U8 *data, U32 size, U32 context=NO_CONTEXT)
Definition Buffer.cpp:90
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)
Definition Buffer.cpp:126
U32 getSize() const
Definition Buffer.cpp:64
void setData(U8 *data)
Definition Buffer.cpp:72
void setContext(U32 context)
Definition Buffer.cpp:86
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &serialBuffer) const
Definition Buffer.cpp:103
Buffer & operator=(const Buffer &src)
Definition Buffer.cpp:48
void setSize(U32 size)
Definition Buffer.cpp:79
U32 getContext() const
Definition Buffer.cpp:68
SerializeBufferBase & getSerializeRepr()
Definition Buffer.cpp:99
void setExtBuffer(U8 *buffPtr, NATIVE_UINT_TYPE size)
Set the external buffer.
forward declaration
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
const char * toChar() const
gets char buffer
Definition String.cpp:48
Definition Buffer.cpp:21
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_DESERIALIZE_TYPE_MISMATCH
Deserialized type ID didn't match.