F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
DpWriter.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title DpWriter.cpp
3 // \author bocchino
4 // \brief cpp file for DpWriter component implementation class
5 // ======================================================================
6 
8 #include "Fw/Com/ComPacket.hpp"
12 #include "Os/File.hpp"
13 #include "Utils/Hash/Hash.hpp"
14 #include "config/DpCfg.hpp"
15 
16 namespace Svc {
17 
18 // ----------------------------------------------------------------------
19 // Construction, initialization, and destruction
20 // ----------------------------------------------------------------------
21 
22 DpWriter::DpWriter(const char* const compName) : DpWriterComponentBase(compName), m_dpFileNamePrefix() {}
23 
25 
26 void DpWriter::configure(const Fw::StringBase& dpFileNamePrefix) {
27  this->m_dpFileNamePrefix = dpFileNamePrefix;
28 }
29 
30 // ----------------------------------------------------------------------
31 // Handler implementations for user-defined typed input ports
32 // ----------------------------------------------------------------------
33 
34 void DpWriter::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& buffer) {
36  // portNum is unused
37  (void)portNum;
38  // Update num buffers received
39  ++this->m_numBuffersReceived;
40  // Check that the buffer is valid
41  if (!buffer.isValid()) {
43  status = Fw::Success::FAILURE;
44  }
45  // Check that the buffer is large enough to hold a data product packet
46  const FwSizeType bufferSize = buffer.getSize();
47  if (status == Fw::Success::SUCCESS) {
48  if (bufferSize < Fw::DpContainer::MIN_PACKET_SIZE) {
49  this->log_WARNING_HI_BufferTooSmallForPacket(static_cast<U32>(bufferSize),
51 
52  status = Fw::Success::FAILURE;
53  }
54  }
55  // Set up the container and check that the header hash is valid
56  Fw::DpContainer container;
57  if (status == Fw::Success::SUCCESS) {
58  container.setBuffer(buffer);
59  Utils::HashBuffer storedHash;
60  Utils::HashBuffer computedHash;
61  status = container.checkHeaderHash(storedHash, computedHash);
62  if (status != Fw::Success::SUCCESS) {
63  this->log_WARNING_HI_InvalidHeaderHash(static_cast<U32>(bufferSize), storedHash.asBigEndianU32(),
64  computedHash.asBigEndianU32());
65  }
66  }
67  // Deserialize the packet header
68  if (status == Fw::Success::SUCCESS) {
69  status = this->deserializePacketHeader(buffer, container);
70  }
71  // Check that the packet size fits in the buffer
72  if (status == Fw::Success::SUCCESS) {
73  const FwSizeType packetSize = container.getPacketSize();
74  if (bufferSize < packetSize) {
75  this->log_WARNING_HI_BufferTooSmallForData(static_cast<U32>(bufferSize), static_cast<U32>(packetSize));
76  status = Fw::Success::FAILURE;
77  }
78  }
79  // Perform the requested processing
80  if (status == Fw::Success::SUCCESS) {
81  this->performProcessing(container);
82  }
83  // Construct the file name
84  Fw::FileNameString fileName;
85  if (status == Fw::Success::SUCCESS) {
86  const FwDpIdType containerId = container.getId();
87  const Fw::Time timeTag = container.getTimeTag();
88  fileName.format(DP_FILENAME_FORMAT, this->m_dpFileNamePrefix.toChar(), containerId, timeTag.getSeconds(),
89  timeTag.getUSeconds());
90  }
91  FwSizeType fileSize = 0;
92  // Write the file
93  if (status == Fw::Success::SUCCESS) {
94  status = this->writeFile(container, fileName, fileSize);
95  }
96  // Send the DpWritten notification
97  if (status == Fw::Success::SUCCESS) {
98  this->sendNotification(container, fileName, fileSize);
99  }
100  // Deallocate the buffer
101  if (buffer.isValid()) {
102  this->deallocBufferSendOut_out(0, buffer);
103  }
104  // Update the error count
105  if (status != Fw::Success::SUCCESS) {
106  this->m_numErrors++;
107  }
108 }
109 
110 void DpWriter::schedIn_handler(const FwIndexType portNum, U32 context) {
111  // portNum and context are not used
112  (void)portNum;
113  (void)context;
114  // Write telemetry
115  this->tlmWrite_NumBuffersReceived(this->m_numBuffersReceived);
116  this->tlmWrite_NumBytesWritten(this->m_numBytesWritten);
117  this->tlmWrite_NumSuccessfulWrites(this->m_numSuccessfulWrites);
118  this->tlmWrite_NumFailedWrites(this->m_numFailedWrites);
119  this->tlmWrite_NumErrors(this->m_numErrors);
120 }
121 
122 // ----------------------------------------------------------------------
123 // Handler implementations for commands
124 // ----------------------------------------------------------------------
125 
126 void DpWriter::CLEAR_EVENT_THROTTLE_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) {
127  // opCode and cmdSeq are not used
128  (void)opCode;
129  (void)cmdSeq;
130  // Clear throttling
138  // Return command response
139  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
140 }
141 
142 // ----------------------------------------------------------------------
143 // Private helper functions
144 // ----------------------------------------------------------------------
145 
146 Fw::Success::T DpWriter::deserializePacketHeader(Fw::Buffer& buffer, Fw::DpContainer& container) {
148  container.setBuffer(buffer);
149  const Fw::SerializeStatus serialStatus = container.deserializeHeader();
150  if (serialStatus != Fw::FW_SERIALIZE_OK) {
151  this->log_WARNING_HI_InvalidHeader(static_cast<U32>(buffer.getSize()), static_cast<U32>(serialStatus));
152  status = Fw::Success::FAILURE;
153  }
154  return status;
155 }
156 
157 void DpWriter::performProcessing(const Fw::DpContainer& container) {
158  // Get the buffer
159  Fw::Buffer buffer = container.getBuffer();
160  // Get the bit mask for the processing types
161  const Fw::DpCfg::ProcType::SerialType procTypes = container.getProcTypes();
162  // Do the processing
163  for (FwIndexType portNum = 0; portNum < NUM_PROCBUFFERSENDOUT_OUTPUT_PORTS; ++portNum) {
164  if ((procTypes & (1 << portNum)) != 0) {
165  this->procBufferSendOut_out(portNum, buffer);
166  }
167  }
168 }
169 
170 Fw::Success::T DpWriter::writeFile(const Fw::DpContainer& container,
171  const Fw::FileNameString& fileName,
172  FwSizeType& fileSize) {
174  // Get the buffer
175  Fw::Buffer buffer = container.getBuffer();
176  // Get the file size
177  fileSize = container.getPacketSize();
178  // Open the file
179  Os::File file;
180  Os::File::Status fileStatus = file.open(fileName.toChar(), Os::File::OPEN_CREATE);
181  if (fileStatus != Os::File::OP_OK) {
182  this->log_WARNING_HI_FileOpenError(static_cast<U32>(fileStatus), fileName);
183  status = Fw::Success::FAILURE;
184  }
185  // Write the file
186  if (status == Fw::Success::SUCCESS) {
187  // Set write size to file size
188  // On entry to the write call, this is the number of bytes to write
189  // On return from the write call, this is the number of bytes written
190  FwSizeType writeSize = static_cast<FwSizeType>(fileSize);
191  fileStatus = file.write(buffer.getData(), writeSize);
192  // If a successful write occurred, then update the number of bytes written
193  if (fileStatus == Os::File::OP_OK) {
194  this->m_numBytesWritten += static_cast<U64>(writeSize);
195  }
196  if ((fileStatus == Os::File::OP_OK) and (writeSize == static_cast<FwSizeType>(fileSize))) {
197  // If the write status is success, and the number of bytes written
198  // is the expected number, then record the success
199  this->log_ACTIVITY_LO_FileWritten(static_cast<U32>(writeSize), fileName);
200  } else {
201  // Otherwise record the failure
202  this->log_WARNING_HI_FileWriteError(static_cast<U32>(fileStatus), static_cast<U32>(writeSize),
203  static_cast<U32>(fileSize), fileName);
204  status = Fw::Success::FAILURE;
205  }
206  }
207  // Update the count of successful or failed writes
208  if (status == Fw::Success::SUCCESS) {
209  this->m_numSuccessfulWrites++;
210  } else {
211  this->m_numFailedWrites++;
212  }
213  // Return the status
214  return status;
215 }
216 
217 void DpWriter::sendNotification(const Fw::DpContainer& container,
218  const Fw::FileNameString& fileName,
219  FwSizeType fileSize) {
221  // Get the priority
222  const FwDpPriorityType priority = container.getPriority();
223  this->dpWrittenOut_out(0, fileName, priority, fileSize);
224  }
225 }
226 
227 } // end namespace Svc
Serialization/Deserialization operation was successful.
A data product Container.
Definition: DpContainer.hpp:26
void deallocBufferSendOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port deallocBufferSendOut.
FwDpPriorityType getPriority() const
FwIdType FwOpcodeType
The type of a command opcode.
void log_WARNING_HI_BufferTooSmallForData(FwSizeType bufferSize, U32 minSize)
Representing success.
PlatformSizeType FwSizeType
U32 FwDpPriorityType
The type of a data product priority.
U8 SerialType
The serial representation type.
static constexpr FwSizeType MIN_PACKET_SIZE
Definition: DpContainer.hpp:65
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
U8 * getData() const
Definition: Buffer.cpp:56
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
void setBuffer(const Buffer &buffer)
Set the packet buffer.
void tlmWrite_NumErrors(U32 arg, Fw::Time _tlmTime=Fw::Time())
SerializeStatus
forward declaration for string
void log_WARNING_HI_InvalidBuffer_ThrottleClear()
Reset throttle value for InvalidBuffer.
void tlmWrite_NumFailedWrites(U32 arg, Fw::Time _tlmTime=Fw::Time())
void log_WARNING_HI_FileWriteError_ThrottleClear()
Reset throttle value for FileWriteError.
const char * toChar() const
bool isConnected_dpWrittenOut_OutputPort(FwIndexType portNum)
void procBufferSendOut_out(FwIndexType portNum, Fw::Buffer &fwBuffer)
Invoke output port procBufferSendOut.
bool isValid() const
Definition: Buffer.cpp:52
void dpWrittenOut_out(FwIndexType portNum, const Fw::StringBase &fileName, FwDpPriorityType priority, FwSizeType size)
Invoke output port dpWrittenOut.
void log_WARNING_HI_InvalidHeaderHash(FwSizeType bufferSize, U32 storedHash, U32 computedHash)
T
The raw enum type.
U32 getSeconds() const
Definition: Time.cpp:86
constexpr const char * DP_FILENAME_FORMAT
Definition: DpCfg.hpp:21
void tlmWrite_NumBuffersReceived(U32 arg, Fw::Time _tlmTime=Fw::Time())
Fw::Time getTimeTag() const
void log_WARNING_HI_BufferTooSmallForData_ThrottleClear()
Reset throttle value for BufferTooSmallForData.
void log_WARNING_HI_FileOpenError_ThrottleClear()
Reset throttle value for FileOpenError.
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:187
void log_WARNING_HI_FileOpenError(U32 status, const Fw::StringBase &file)
void log_WARNING_HI_InvalidHeader_ThrottleClear()
Reset throttle value for InvalidHeader.
FwSizeType getPacketSize() const
Get the packet size corresponding to the data size.
Representing failure.
Fw::Buffer getBuffer() const
void log_WARNING_HI_InvalidHeaderHash_ThrottleClear()
Reset throttle value for InvalidHeaderHash.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:55
Command successfully executed.
void log_WARNING_HI_BufferTooSmallForPacket(FwSizeType bufferSize, U32 minSize)
FwSizeType getSize() const
Definition: Buffer.cpp:60
DpCfg::ProcType::SerialType getProcTypes() const
FwIdType FwDpIdType
The type of a data product identifier.
void configure(const Fw::StringBase &dpFileNamePrefix)
Configure writer.
Definition: DpWriter.cpp:26
void log_ACTIVITY_LO_FileWritten(U32 bytes, const Fw::StringBase &file) const
Operation was successful.
Definition: File.hpp:40
U32 getUSeconds() const
Definition: Time.cpp:90
PlatformIndexType FwIndexType
U32 asBigEndianU32() const
Convert bytes 0 through 3 of the hash data to a big-Endian U32 value.
void log_WARNING_HI_FileWriteError(U32 status, U32 bytesWritten, U32 bytesToWrite, const Fw::StringBase &file)
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
Success::T checkHeaderHash(Utils::HashBuffer &storedHash, Utils::HashBuffer &computedHash) const
Check the header hash.
RateGroupDivider component implementation.
Fw::SerializeStatus deserializeHeader()
Definition: DpContainer.cpp:38
void tlmWrite_NumBytesWritten(U64 arg, Fw::Time _tlmTime=Fw::Time())
void log_WARNING_HI_InvalidHeader(FwSizeType bufferSize, U32 errorCode)
FwDpIdType getId() const
Definition: DpContainer.hpp:98
void tlmWrite_NumSuccessfulWrites(U32 arg, Fw::Time _tlmTime=Fw::Time())
void log_WARNING_HI_BufferTooSmallForPacket_ThrottleClear()
Reset throttle value for BufferTooSmallForPacket.
DpWriter(const char *const compName)
Definition: DpWriter.cpp:22
Auto-generated base for DpWriter component.
#define U64(C)
Definition: sha.h:180
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:32