F´ Flight Software - C/C++ Documentation
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  SerialBuffer serialBuffer(const_cast<Buffer&>(buffer).getData(), const_cast<Buffer&>(buffer).getSize());
24  serialBuffer.fill();
25  const SerializeStatus status = this->fromSerialBuffer(serialBuffer);
26  return status;
27 }
28 
30  return this->m_header;
31 }
32 
34  FW_ASSERT(this->m_header.m_type == T_START);
35  return this->m_startPacket;
36 }
37 
39  FW_ASSERT(this->m_header.m_type == T_DATA);
40  return this->m_dataPacket;
41 }
42 
44  FW_ASSERT(this->m_header.m_type == T_END);
45  return this->m_endPacket;
46 }
47 
49  FW_ASSERT(this->m_header.m_type == T_CANCEL);
50  return this->m_cancelPacket;
51 }
52 
53 void FilePacket ::fromStartPacket(const StartPacket& startPacket) {
54  this->m_startPacket = startPacket;
55  this->m_header.m_type = T_START;
56 }
57 
58 void FilePacket ::fromDataPacket(const DataPacket& dataPacket) {
59  this->m_dataPacket = dataPacket;
60  this->m_header.m_type = T_DATA;
61 }
62 
63 void FilePacket ::fromEndPacket(const EndPacket& endPacket) {
64  this->m_endPacket = endPacket;
65  this->m_header.m_type = T_END;
66 }
67 
68 void FilePacket ::fromCancelPacket(const CancelPacket& cancelPacket) {
69  this->m_cancelPacket = cancelPacket;
70  this->m_header.m_type = T_CANCEL;
71 }
72 
74  switch (this->m_header.m_type) {
75  case T_START:
76  return this->m_startPacket.bufferSize();
77  case T_DATA:
78  return this->m_dataPacket.bufferSize();
79  case T_END:
80  return this->m_endPacket.bufferSize();
81  case T_CANCEL:
82  return this->m_cancelPacket.bufferSize();
83  case T_NONE:
84  return 0;
85  default:
86  FW_ASSERT(0);
87  return 0;
88  }
89 }
90 
92  switch (this->m_header.m_type) {
93  case T_START:
94  return this->m_startPacket.toBuffer(buffer);
95  case T_DATA:
96  return this->m_dataPacket.toBuffer(buffer);
97  case T_END:
98  return this->m_endPacket.toBuffer(buffer);
99  case T_CANCEL:
100  return this->m_cancelPacket.toBuffer(buffer);
101  default:
102  FW_ASSERT(0);
103  return static_cast<SerializeStatus>(0);
104  }
105 }
106 
107 // ----------------------------------------------------------------------
108 // Private instance methods
109 // ----------------------------------------------------------------------
110 
111 SerializeStatus FilePacket ::fromSerialBuffer(SerialBuffer& serialBuffer) {
112  SerializeStatus status;
113  status = this->m_header.fromSerialBuffer(serialBuffer);
114  if (status != FW_SERIALIZE_OK) {
115  return status;
116  }
117 
118  switch (this->m_header.m_type) {
119  case T_START:
120  status = this->m_startPacket.fromSerialBuffer(serialBuffer);
121  break;
122  case T_DATA:
123  status = this->m_dataPacket.fromSerialBuffer(serialBuffer);
124  break;
125  case T_END:
126  status = this->m_endPacket.fromSerialBuffer(serialBuffer);
127  break;
128  case T_CANCEL:
129  status = this->m_cancelPacket.fromSerialBuffer(serialBuffer);
130  break;
131  case T_NONE:
133  break;
134  default:
135  FW_ASSERT(0, status);
136  break;
137  }
138  return status;
139 }
140 
141 } // namespace Fw
Serialization/Deserialization operation was successful.
U32 bufferSize() const
Compute the buffer size needed to hold this EndPacket.
Definition: EndPacket.cpp:25
void fromCancelPacket(const CancelPacket &cancelPacket)
Definition: FilePacket.cpp:68
A variable-length serializable buffer.
void fromDataPacket(const DataPacket &dataPacket)
Definition: FilePacket.cpp:58
SerializeStatus toBuffer(Buffer &buffer) const
Convert this DataPacket to a Buffer.
Definition: DataPacket.cpp:33
U32 bufferSize() const
Compute the buffer size needed to hold this CancelPacket.
U32 bufferSize() const
Compute the buffer size needed to hold this StartPacket.
Definition: StartPacket.cpp:27
The type of a cancel packet.
Definition: FilePacket.hpp:273
void fromEndPacket(const EndPacket &endPacket)
Definition: FilePacket.cpp:63
The type of a data packet.
Definition: FilePacket.hpp:171
void fill()
Fill the buffer to capacity with preexisting data.
const DataPacket & asDataPacket() const
Definition: FilePacket.cpp:38
void fromStartPacket(const StartPacket &startPacket)
Definition: FilePacket.cpp:53
SerializeStatus toBuffer(Buffer &buffer) const
Definition: FilePacket.cpp:91
SerializeStatus
forward declaration for string
SerializeStatus toBuffer(Buffer &buffer) const
Convert this EndPacket to a Buffer.
Definition: EndPacket.cpp:29
U32 bufferSize() const
Definition: FilePacket.cpp:73
const Header & asHeader() const
Definition: FilePacket.cpp:29
SerializeStatus toBuffer(Buffer &buffer) const
Convert this StartPacket to a Buffer.
Definition: StartPacket.cpp:32
The type of a start packet.
Definition: FilePacket.hpp:121
U32 bufferSize() const
Compute the buffer size needed to hold this DataPacket.
Definition: DataPacket.cpp:28
const StartPacket & asStartPacket() const
Definition: FilePacket.cpp:33
The type of a packet header.
Definition: FilePacket.hpp:82
Deserialized type ID didn&#39;t match.
const EndPacket & asEndPacket() const
Definition: FilePacket.cpp:43
SerializeStatus fromBuffer(const Buffer &buffer)
Definition: FilePacket.cpp:22
SerializeStatus toBuffer(Buffer &buffer) const
Convert this CancelPacket to a Buffer.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
The type of an end packet.
Definition: FilePacket.hpp:230
const CancelPacket & asCancelPacket() const
Definition: FilePacket.cpp:48