F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
DataPacket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title DataPacket.cpp
3 // \author bocchino
4 // \brief cpp file for FilePacket::DataPacket
5 //
6 // \copyright
7 // Copyright 2009-2016, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include <Fw/Types/Assert.hpp>
15 
16 namespace Fw {
17 
20  const U32 sequenceIndex,
21  const U32 byteOffset,
22  const U16 dataSize,
23  const U8 *const data
24  )
25  {
26  this->m_header.initialize(FilePacket::T_DATA, sequenceIndex);
27  this->m_byteOffset = byteOffset;
28  this->m_dataSize = dataSize;
29  this->m_data = data;
30  }
31 
33  bufferSize() const
34  {
35  return
36  this->m_header.bufferSize() +
37  sizeof(this->m_byteOffset) +
38  sizeof(this->m_dataSize) +
39  this->m_dataSize;
40  }
41 
43  toBuffer(Buffer& buffer) const
44  {
45  SerialBuffer serialBuffer(
46  buffer.getData(),
47  buffer.getSize()
48  );
49  return this->toSerialBuffer(serialBuffer);
50  }
51 
52  SerializeStatus FilePacket::DataPacket ::
53  fromSerialBuffer(SerialBuffer& serialBuffer)
54  {
55 
56  FW_ASSERT(this->m_header.m_type == T_DATA);
57 
58  SerializeStatus status = serialBuffer.deserialize(this->m_byteOffset);
59  if (status != FW_SERIALIZE_OK)
60  return status;
61 
62  status = serialBuffer.deserialize(this->m_dataSize);
63  if (status != FW_SERIALIZE_OK)
64  return status;
65 
66  if (serialBuffer.getBuffLeft() != this->m_dataSize)
68 
69  U8 *const addr = serialBuffer.getBuffAddr();
70  this->m_data = &addr[this->fixedLengthSize()];
71 
72  return FW_SERIALIZE_OK;
73 
74  }
75 
76  U32 FilePacket::DataPacket ::
77  fixedLengthSize() const
78  {
79  return
80  this->m_header.bufferSize() +
81  sizeof(this->m_byteOffset) +
82  sizeof(this->m_dataSize);
83  }
84 
85  SerializeStatus FilePacket::DataPacket ::
86  toSerialBuffer(SerialBuffer& serialBuffer) const
87  {
88 
89  FW_ASSERT(this->m_header.m_type == T_DATA);
90 
91  SerializeStatus status;
92 
93  status = this->m_header.toSerialBuffer(serialBuffer);
94  if (status != FW_SERIALIZE_OK)
95  return status;
96 
97  status = serialBuffer.serialize(this->m_byteOffset);
98  if (status != FW_SERIALIZE_OK)
99  return status;
100 
101  status = serialBuffer.serialize(this->m_dataSize);
102  if (status != FW_SERIALIZE_OK)
103  return status;
104 
105  status = serialBuffer.pushBytes(this->m_data, this->m_dataSize);
106 
107  return status;
108 
109  }
110 
111 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
U8 * getData() const
Definition: Buffer.cpp:68
U32 getSize() const
Definition: Buffer.cpp:72
SerializeStatus toBuffer(Buffer &buffer) const
Convert this DataPacket to a Buffer.
Definition: DataPacket.cpp:43
void initialize(const U32 sequenceIndex, const U32 byteOffset, const U16 dataSize, const U8 *const data)
Initialize a data packet.
Definition: DataPacket.cpp:19
U32 bufferSize() const
Compute the buffer size needed to hold this DataPacket.
Definition: DataPacket.cpp:33
A variable-length serializable buffer.
U8 * getBuffAddr()
gets buffer address for data filling
NATIVE_UINT_TYPE getBuffLeft() const
returns how much deserialization buffer is left
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_DESERIALIZE_SIZE_MISMATCH
Data was left in the buffer, but not enough to deserialize.