F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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
16namespace Fw {
17
18 // ----------------------------------------------------------------------
19 // Public instance methods
20 // ----------------------------------------------------------------------
21
22 SerializeStatus FilePacket ::
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
34 const FilePacket::Header& FilePacket ::
35 asHeader() const
36 {
37 return this->header;
38 }
39
40 const FilePacket::StartPacket& FilePacket ::
41 asStartPacket() const
42 {
43 FW_ASSERT(this->header.type == T_START);
44 return this->startPacket;
45 }
46
47 const FilePacket::DataPacket& FilePacket ::
48 asDataPacket() const
49 {
50 FW_ASSERT(this->header.type == T_DATA);
51 return this->dataPacket;
52 }
53
54 const FilePacket::EndPacket& FilePacket ::
55 asEndPacket() const
56 {
57 FW_ASSERT(this->header.type == T_END);
58 return this->endPacket;
59 }
60
61 const FilePacket::CancelPacket& FilePacket ::
62 asCancelPacket() const
63 {
64 FW_ASSERT(this->header.type == T_CANCEL);
65 return this->cancelPacket;
66 }
67
68 void FilePacket ::
69 fromStartPacket(const StartPacket& startPacket)
70 {
71 this->startPacket = startPacket;
72 this->header.type = T_START;
73 }
74
75 void FilePacket ::
76 fromDataPacket(const DataPacket& dataPacket)
77 {
78 this->dataPacket = dataPacket;
79 this->header.type = T_DATA;
80 }
81
82 void FilePacket ::
83 fromEndPacket(const EndPacket& endPacket)
84 {
85 this->endPacket = endPacket;
86 this->header.type = T_END;
87 }
88
89 void FilePacket ::
90 fromCancelPacket(const CancelPacket& cancelPacket)
91 {
92 this->cancelPacket = cancelPacket;
93 this->header.type = T_CANCEL;
94 }
95
96 U32 FilePacket ::
97 bufferSize() const
98 {
99 switch (this->header.type) {
100 case T_START:
101 return this->startPacket.bufferSize();
102 case T_DATA:
103 return this->dataPacket.bufferSize();
104 case T_END:
105 return this->endPacket.bufferSize();
106 case T_CANCEL:
107 return this->cancelPacket.bufferSize();
108 case T_NONE:
109 return 0;
110 default:
111 FW_ASSERT(0);
112 return 0;
113 }
114 }
115
116 SerializeStatus FilePacket ::
117 toBuffer(Buffer& buffer) const
118 {
119 switch (this->header.type) {
120 case T_START:
121 return this->startPacket.toBuffer(buffer);
122 case T_DATA:
123 return this->dataPacket.toBuffer(buffer);
124 case T_END:
125 return this->endPacket.toBuffer(buffer);
126 case T_CANCEL:
127 return this->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->header.fromSerialBuffer(serialBuffer);
143 if (status != FW_SERIALIZE_OK)
144 return status;
145 switch (this->header.type) {
146 case T_START:
147 status = this->startPacket.fromSerialBuffer(serialBuffer);
148 break;
149 case T_DATA:
150 status = this->dataPacket.fromSerialBuffer(serialBuffer);
151 break;
152 case T_END:
153 status = this->endPacket.fromSerialBuffer(serialBuffer);
154 break;
155 case T_CANCEL:
156 status = this->cancelPacket.fromSerialBuffer(serialBuffer);
157 break;
158 case T_NONE:
160 break;
161 default:
162 FW_ASSERT(0,status);
163 break;
164 }
165 return status;
166 }
167
168}
#define FW_ASSERT(...)
Definition Assert.hpp:7
The type of a cancel packet.
U32 bufferSize() const
Compute the buffer size needed to hold this CancelPacket.
The type of a data packet.
The type of an end packet.
The type of a packet header.
A variable-length serializable buffer.
void fill()
Fill the buffer to capacity with preexisting data.
Definition Buffer.cpp:21
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.