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 static_cast<U32>(
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 
63  status = serialBuffer.deserialize(this->m_dataSize);
64  if (status != FW_SERIALIZE_OK) {
65  return status;
66  }
67 
68  if (serialBuffer.getBuffLeft() != this->m_dataSize) {
70  }
71 
72  U8 *const addr = serialBuffer.getBuffAddr();
73  this->m_data = &addr[this->fixedLengthSize()];
74 
75  return FW_SERIALIZE_OK;
76 
77  }
78 
79  U32 FilePacket::DataPacket ::
80  fixedLengthSize() const
81  {
82  return static_cast<U32>(
83  this->m_header.bufferSize() +
84  sizeof(this->m_byteOffset) +
85  sizeof(this->m_dataSize));
86  }
87 
88  SerializeStatus FilePacket::DataPacket ::
89  toSerialBuffer(SerialBuffer& serialBuffer) const
90  {
91 
92  FW_ASSERT(this->m_header.m_type == T_DATA);
93 
94  SerializeStatus status;
95 
96  status = this->m_header.toSerialBuffer(serialBuffer);
97  if (status != FW_SERIALIZE_OK) {
98  return status;
99  }
100 
101  status = serialBuffer.serialize(this->m_byteOffset);
102  if (status != FW_SERIALIZE_OK) {
103  return status;
104  }
105 
106  status = serialBuffer.serialize(this->m_dataSize);
107  if (status != FW_SERIALIZE_OK) {
108  return status;
109  }
110 
111  status = serialBuffer.pushBytes(this->m_data, this->m_dataSize);
112 
113  return status;
114 
115  }
116 
117 }
#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
Serializable::SizeType 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.