F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
FilePacket.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FilePacket.cpp
3 // \author bocchino
4 // \brief cpp file for FilePacket
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 
18  // ----------------------------------------------------------------------
19  // Public instance methods
20  // ----------------------------------------------------------------------
21 
23  fromBuffer(const Buffer& buffer)
24  {
25  SerialBuffer serialBuffer(
26  const_cast<Buffer&>(buffer).getData(),
27  const_cast<Buffer&>(buffer).getSize()
28  );
29  serialBuffer.fill();
30  const SerializeStatus status = this->fromSerialBuffer(serialBuffer);
31  return status;
32  }
33 
35  asHeader() const
36  {
37  return this->m_header;
38  }
39 
41  asStartPacket() const
42  {
43  FW_ASSERT(this->m_header.m_type == T_START);
44  return this->m_startPacket;
45  }
46 
48  asDataPacket() const
49  {
50  FW_ASSERT(this->m_header.m_type == T_DATA);
51  return this->m_dataPacket;
52  }
53 
55  asEndPacket() const
56  {
57  FW_ASSERT(this->m_header.m_type == T_END);
58  return this->m_endPacket;
59  }
60 
62  asCancelPacket() const
63  {
64  FW_ASSERT(this->m_header.m_type == T_CANCEL);
65  return this->m_cancelPacket;
66  }
67 
69  fromStartPacket(const StartPacket& startPacket)
70  {
71  this->m_startPacket = startPacket;
72  this->m_header.m_type = T_START;
73  }
74 
76  fromDataPacket(const DataPacket& dataPacket)
77  {
78  this->m_dataPacket = dataPacket;
79  this->m_header.m_type = T_DATA;
80  }
81 
83  fromEndPacket(const EndPacket& endPacket)
84  {
85  this->m_endPacket = endPacket;
86  this->m_header.m_type = T_END;
87  }
88 
90  fromCancelPacket(const CancelPacket& cancelPacket)
91  {
92  this->m_cancelPacket = cancelPacket;
93  this->m_header.m_type = T_CANCEL;
94  }
95 
97  bufferSize() const
98  {
99  switch (this->m_header.m_type) {
100  case T_START:
101  return this->m_startPacket.bufferSize();
102  case T_DATA:
103  return this->m_dataPacket.bufferSize();
104  case T_END:
105  return this->m_endPacket.bufferSize();
106  case T_CANCEL:
107  return this->m_cancelPacket.bufferSize();
108  case T_NONE:
109  return 0;
110  default:
111  FW_ASSERT(0);
112  return 0;
113  }
114  }
115 
117  toBuffer(Buffer& buffer) const
118  {
119  switch (this->m_header.m_type) {
120  case T_START:
121  return this->m_startPacket.toBuffer(buffer);
122  case T_DATA:
123  return this->m_dataPacket.toBuffer(buffer);
124  case T_END:
125  return this->m_endPacket.toBuffer(buffer);
126  case T_CANCEL:
127  return this->m_cancelPacket.toBuffer(buffer);
128  default:
129  FW_ASSERT(0);
130  return static_cast<SerializeStatus>(0);
131  }
132  }
133 
134  // ----------------------------------------------------------------------
135  // Private instance methods
136  // ----------------------------------------------------------------------
137 
138  SerializeStatus FilePacket ::
139  fromSerialBuffer(SerialBuffer& serialBuffer)
140  {
141  SerializeStatus status;
142  status = this->m_header.fromSerialBuffer(serialBuffer);
143  if (status != FW_SERIALIZE_OK) {
144  return status;
145  }
146 
147  switch (this->m_header.m_type) {
148  case T_START:
149  status = this->m_startPacket.fromSerialBuffer(serialBuffer);
150  break;
151  case T_DATA:
152  status = this->m_dataPacket.fromSerialBuffer(serialBuffer);
153  break;
154  case T_END:
155  status = this->m_endPacket.fromSerialBuffer(serialBuffer);
156  break;
157  case T_CANCEL:
158  status = this->m_cancelPacket.fromSerialBuffer(serialBuffer);
159  break;
160  case T_NONE:
162  break;
163  default:
164  FW_ASSERT(0,status);
165  break;
166  }
167  return status;
168  }
169 
170 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
The type of a cancel packet.
Definition: FilePacket.hpp:318
SerializeStatus toBuffer(Buffer &buffer) const
Convert this CancelPacket to a Buffer.
U32 bufferSize() const
Compute the buffer size needed to hold this CancelPacket.
The type of a data packet.
Definition: FilePacket.hpp:197
SerializeStatus toBuffer(Buffer &buffer) const
Convert this DataPacket to a Buffer.
Definition: DataPacket.cpp:43
U32 bufferSize() const
Compute the buffer size needed to hold this DataPacket.
Definition: DataPacket.cpp:33
The type of an end packet.
Definition: FilePacket.hpp:269
U32 bufferSize() const
Compute the buffer size needed to hold this EndPacket.
Definition: EndPacket.cpp:31
SerializeStatus toBuffer(Buffer &buffer) const
Convert this EndPacket to a Buffer.
Definition: EndPacket.cpp:37
The type of a packet header.
Definition: FilePacket.hpp:93
A variable-length serializable buffer.
void fill()
Fill the buffer to capacity with preexisting data.
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_DESERIALIZE_TYPE_MISMATCH
Deserialized type ID didn't match.
The type of a start packet.
Definition: FilePacket.hpp:139
SerializeStatus toBuffer(Buffer &buffer) const
Convert this StartPacket to a Buffer.
Definition: StartPacket.cpp:42
U32 bufferSize() const
Compute the buffer size needed to hold this StartPacket.
Definition: StartPacket.cpp:32
SerializeStatus fromBuffer(const Buffer &buffer)
Definition: FilePacket.cpp:23
void fromCancelPacket(const CancelPacket &cancelPacket)
Definition: FilePacket.cpp:90
void fromEndPacket(const EndPacket &endPacket)
Definition: FilePacket.cpp:83
const CancelPacket & asCancelPacket() const
Definition: FilePacket.cpp:62
const StartPacket & asStartPacket() const
Definition: FilePacket.cpp:41
const EndPacket & asEndPacket() const
Definition: FilePacket.cpp:55
void fromDataPacket(const DataPacket &dataPacket)
Definition: FilePacket.cpp:76
void fromStartPacket(const StartPacket &startPacket)
Definition: FilePacket.cpp:69
const DataPacket & asDataPacket() const
Definition: FilePacket.cpp:48
U32 bufferSize() const
Definition: FilePacket.cpp:97
const Header & asHeader() const
Definition: FilePacket.cpp:35
SerializeStatus toBuffer(Buffer &buffer) const
Definition: FilePacket.cpp:117