|
10 | 10 | // |
11 | 11 | // ====================================================================== |
12 | 12 |
|
13 | | -#include <Svc/FileUplink/FileUplink.hpp> |
14 | | -#include <Fw/Types/Assert.hpp> |
| 13 | +#include <Fw/Com/ComPacket.hpp> |
15 | 14 | #include <Fw/FPrimeBasicTypes.hpp> |
| 15 | +#include <Fw/Types/Assert.hpp> |
| 16 | +#include <Svc/FileUplink/FileUplink.hpp> |
16 | 17 |
|
17 | 18 | namespace Svc { |
18 | 19 |
|
19 | | - // ---------------------------------------------------------------------- |
20 | | - // Construction, initialization, and destruction |
21 | | - // ---------------------------------------------------------------------- |
| 20 | +// ---------------------------------------------------------------------- |
| 21 | +// Construction, initialization, and destruction |
| 22 | +// ---------------------------------------------------------------------- |
22 | 23 |
|
23 | | - FileUplink :: |
24 | | - FileUplink(const char *const name) : |
25 | | - FileUplinkComponentBase(name), |
| 24 | +FileUplink ::FileUplink(const char* const name) |
| 25 | + : FileUplinkComponentBase(name), |
26 | 26 | m_receiveMode(START), |
27 | 27 | m_lastSequenceIndex(0), |
28 | 28 | m_lastPacketWriteStatus(Os::File::MAX_STATUS), |
29 | 29 | m_filesReceived(this), |
30 | 30 | m_packetsReceived(this), |
31 | | - m_warnings(this) |
32 | | - { |
| 31 | + m_warnings(this) {} |
33 | 32 |
|
34 | | - } |
| 33 | +FileUplink ::~FileUplink() {} |
35 | 34 |
|
36 | | - FileUplink :: |
37 | | - ~FileUplink() |
38 | | - { |
| 35 | +// ---------------------------------------------------------------------- |
| 36 | +// Handler implementations for user-defined typed input ports |
| 37 | +// ---------------------------------------------------------------------- |
39 | 38 |
|
40 | | - } |
| 39 | +void FileUplink ::bufferSendIn_handler(const FwIndexType portNum, Fw::Buffer& buffer) { |
| 40 | + // If packet is too small to contain a packet type, log + deallocate and return |
| 41 | + if (buffer.getSize() < sizeof(FwPacketDescriptorType)) { |
| 42 | + this->log_WARNING_HI_InvalidPacketReceived(Fw::ComPacket::FW_PACKET_UNKNOWN); |
| 43 | + this->bufferSendOut_out(0, buffer); |
| 44 | + return; |
| 45 | + } |
41 | 46 |
|
42 | | - // ---------------------------------------------------------------------- |
43 | | - // Handler implementations for user-defined typed input ports |
44 | | - // ---------------------------------------------------------------------- |
| 47 | + // Read the packet type from the packet buffer |
| 48 | + FwPacketDescriptorType packetType; |
| 49 | + Fw::SerializeStatus status = buffer.getDeserializer().deserialize(packetType); |
| 50 | + FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status); |
45 | 51 |
|
46 | | - void FileUplink :: |
47 | | - bufferSendIn_handler( |
48 | | - const FwIndexType portNum, |
49 | | - Fw::Buffer& buffer |
50 | | - ) |
51 | | - { |
| 52 | + // If packet type is not a file packet, log + deallocate and return |
| 53 | + if (packetType != Fw::ComPacket::FW_PACKET_FILE) { |
| 54 | + this->log_WARNING_HI_InvalidPacketReceived(packetType); |
| 55 | + this->bufferSendOut_out(0, buffer); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Deserialize the file packet contents into Fw::FilePacket (remove packet type token) |
| 60 | + Fw::Buffer packetBuffer(buffer.getData() + sizeof(packetType), |
| 61 | + buffer.getSize() - static_cast<Fw::Buffer::SizeType>(sizeof(packetType))); |
52 | 62 | Fw::FilePacket filePacket; |
53 | | - const Fw::SerializeStatus status = filePacket.fromBuffer(buffer); |
| 63 | + status = filePacket.fromBuffer(packetBuffer); |
54 | 64 | if (status != Fw::FW_SERIALIZE_OK) { |
55 | 65 | this->log_WARNING_HI_DecodeError(status); |
56 | 66 | } else { |
57 | 67 | Fw::FilePacket::Type header_type = filePacket.asHeader().getType(); |
58 | 68 | switch (header_type) { |
59 | | - case Fw::FilePacket::T_START: |
60 | | - this->handleStartPacket(filePacket.asStartPacket()); |
61 | | - break; |
62 | | - case Fw::FilePacket::T_DATA: |
63 | | - this->handleDataPacket(filePacket.asDataPacket()); |
64 | | - break; |
65 | | - case Fw::FilePacket::T_END: |
66 | | - this->handleEndPacket(filePacket.asEndPacket()); |
67 | | - break; |
68 | | - case Fw::FilePacket::T_CANCEL: |
69 | | - this->handleCancelPacket(); |
70 | | - break; |
71 | | - default: |
72 | | - FW_ASSERT(0); |
73 | | - break; |
| 69 | + case Fw::FilePacket::T_START: |
| 70 | + this->handleStartPacket(filePacket.asStartPacket()); |
| 71 | + break; |
| 72 | + case Fw::FilePacket::T_DATA: |
| 73 | + this->handleDataPacket(filePacket.asDataPacket()); |
| 74 | + break; |
| 75 | + case Fw::FilePacket::T_END: |
| 76 | + this->handleEndPacket(filePacket.asEndPacket()); |
| 77 | + break; |
| 78 | + case Fw::FilePacket::T_CANCEL: |
| 79 | + this->handleCancelPacket(); |
| 80 | + break; |
| 81 | + default: |
| 82 | + FW_ASSERT(0); |
| 83 | + break; |
74 | 84 | } |
75 | 85 | } |
76 | 86 | this->bufferSendOut_out(0, buffer); |
77 | | - } |
78 | | - |
79 | | - void FileUplink :: |
80 | | - pingIn_handler( |
81 | | - const FwIndexType portNum, |
82 | | - U32 key |
83 | | - ) |
84 | | - { |
85 | | - // return key |
86 | | - this->pingOut_out(0,key); |
87 | | - } |
88 | | - |
89 | | - // ---------------------------------------------------------------------- |
90 | | - // Private helper functions |
91 | | - // ---------------------------------------------------------------------- |
92 | | - |
93 | | - void FileUplink :: |
94 | | - handleStartPacket(const Fw::FilePacket::StartPacket& startPacket) |
95 | | - { |
| 87 | +} |
| 88 | + |
| 89 | +void FileUplink ::pingIn_handler(const FwIndexType portNum, U32 key) { |
| 90 | + // return key |
| 91 | + this->pingOut_out(0, key); |
| 92 | +} |
| 93 | + |
| 94 | +// ---------------------------------------------------------------------- |
| 95 | +// Private helper functions |
| 96 | +// ---------------------------------------------------------------------- |
| 97 | + |
| 98 | +void FileUplink ::handleStartPacket(const Fw::FilePacket::StartPacket& startPacket) { |
96 | 99 | // Clear all event throttles in preparation for new start packet |
97 | 100 | this->log_WARNING_HI_FileWriteError_ThrottleClear(); |
98 | 101 | this->log_WARNING_HI_InvalidReceiveMode_ThrottleClear(); |
99 | 102 | this->log_WARNING_HI_PacketOutOfBounds_ThrottleClear(); |
100 | 103 | this->log_WARNING_HI_PacketOutOfOrder_ThrottleClear(); |
101 | 104 | this->m_packetsReceived.packetReceived(); |
102 | 105 | if (this->m_receiveMode != START) { |
103 | | - this->m_file.osFile.close(); |
104 | | - this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_START); |
| 106 | + this->m_file.osFile.close(); |
| 107 | + this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_START); |
105 | 108 | } |
106 | 109 | const Os::File::Status status = this->m_file.open(startPacket); |
107 | 110 | if (status == Os::File::OP_OK) { |
108 | | - this->goToDataMode(); |
109 | | - } |
110 | | - else { |
111 | | - this->m_warnings.fileOpen(this->m_file.name); |
112 | | - this->goToStartMode(); |
| 111 | + this->goToDataMode(); |
| 112 | + } else { |
| 113 | + this->m_warnings.fileOpen(this->m_file.name); |
| 114 | + this->goToStartMode(); |
113 | 115 | } |
114 | | - } |
| 116 | +} |
115 | 117 |
|
116 | | - void FileUplink :: |
117 | | - handleDataPacket(const Fw::FilePacket::DataPacket& dataPacket) |
118 | | - { |
| 118 | +void FileUplink ::handleDataPacket(const Fw::FilePacket::DataPacket& dataPacket) { |
119 | 119 | this->m_packetsReceived.packetReceived(); |
120 | 120 | if (this->m_receiveMode != DATA) { |
121 | | - this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_DATA); |
122 | | - return; |
| 121 | + this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_DATA); |
| 122 | + return; |
123 | 123 | } |
124 | 124 |
|
125 | 125 | const U32 sequenceIndex = dataPacket.asHeader().getSequenceIndex(); |
126 | 126 |
|
127 | 127 | // skip this packet if it is a duplicate and it has already been written |
128 | | - if (this->m_lastPacketWriteStatus == Os::File::OP_OK && |
129 | | - this->checkDuplicatedPacket(sequenceIndex)) { |
| 128 | + if (this->m_lastPacketWriteStatus == Os::File::OP_OK && this->checkDuplicatedPacket(sequenceIndex)) { |
130 | 129 | return; |
131 | 130 | } |
132 | 131 |
|
133 | 132 | this->checkSequenceIndex(sequenceIndex); |
134 | 133 | const U32 byteOffset = dataPacket.getByteOffset(); |
135 | 134 | const U32 dataSize = dataPacket.getDataSize(); |
136 | 135 | if (byteOffset + dataSize > this->m_file.size) { |
137 | | - this->m_warnings.packetOutOfBounds(sequenceIndex, this->m_file.name); |
138 | | - return; |
| 136 | + this->m_warnings.packetOutOfBounds(sequenceIndex, this->m_file.name); |
| 137 | + return; |
139 | 138 | } |
140 | | - const Os::File::Status status = this->m_file.write( |
141 | | - dataPacket.getData(), |
142 | | - byteOffset, |
143 | | - dataSize |
144 | | - ); |
| 139 | + const Os::File::Status status = this->m_file.write(dataPacket.getData(), byteOffset, dataSize); |
145 | 140 | if (status != Os::File::OP_OK) { |
146 | | - this->m_warnings.fileWrite(this->m_file.name); |
| 141 | + this->m_warnings.fileWrite(this->m_file.name); |
147 | 142 | } |
148 | 143 |
|
149 | 144 | this->m_lastPacketWriteStatus = status; |
150 | | - } |
| 145 | +} |
151 | 146 |
|
152 | | - void FileUplink :: |
153 | | - handleEndPacket(const Fw::FilePacket::EndPacket& endPacket) |
154 | | - { |
| 147 | +void FileUplink ::handleEndPacket(const Fw::FilePacket::EndPacket& endPacket) { |
155 | 148 | this->m_packetsReceived.packetReceived(); |
156 | 149 | if (this->m_receiveMode == DATA) { |
157 | | - this->m_filesReceived.fileReceived(); |
158 | | - this->checkSequenceIndex(endPacket.asHeader().getSequenceIndex()); |
159 | | - this->compareChecksums(endPacket); |
160 | | - this->log_ACTIVITY_HI_FileReceived(this->m_file.name); |
161 | | - } |
162 | | - else { |
163 | | - this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_END); |
| 150 | + this->m_filesReceived.fileReceived(); |
| 151 | + this->checkSequenceIndex(endPacket.asHeader().getSequenceIndex()); |
| 152 | + this->compareChecksums(endPacket); |
| 153 | + this->log_ACTIVITY_HI_FileReceived(this->m_file.name); |
| 154 | + } else { |
| 155 | + this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_END); |
164 | 156 | } |
165 | 157 | this->goToStartMode(); |
166 | | - } |
| 158 | +} |
167 | 159 |
|
168 | | - void FileUplink :: |
169 | | - handleCancelPacket() |
170 | | - { |
| 160 | +void FileUplink ::handleCancelPacket() { |
171 | 161 | this->m_packetsReceived.packetReceived(); |
172 | 162 | this->log_ACTIVITY_HI_UplinkCanceled(); |
173 | 163 | this->goToStartMode(); |
174 | | - } |
| 164 | +} |
175 | 165 |
|
176 | | - void FileUplink :: |
177 | | - checkSequenceIndex(const U32 sequenceIndex) |
178 | | - { |
| 166 | +void FileUplink ::checkSequenceIndex(const U32 sequenceIndex) { |
179 | 167 | if (sequenceIndex != this->m_lastSequenceIndex + 1) { |
180 | | - this->m_warnings.packetOutOfOrder( |
181 | | - sequenceIndex, |
182 | | - this->m_lastSequenceIndex |
183 | | - ); |
| 168 | + this->m_warnings.packetOutOfOrder(sequenceIndex, this->m_lastSequenceIndex); |
184 | 169 | } |
185 | 170 | this->m_lastSequenceIndex = sequenceIndex; |
186 | | - } |
| 171 | +} |
187 | 172 |
|
188 | | - bool FileUplink :: |
189 | | - checkDuplicatedPacket(const U32 sequenceIndex) |
190 | | - { |
| 173 | +bool FileUplink ::checkDuplicatedPacket(const U32 sequenceIndex) { |
191 | 174 | // check for duplicate packet |
192 | 175 | if (sequenceIndex == this->m_lastSequenceIndex) { |
193 | | - this->m_warnings.packetDuplicate(sequenceIndex); |
194 | | - return true; |
| 176 | + this->m_warnings.packetDuplicate(sequenceIndex); |
| 177 | + return true; |
195 | 178 | } |
196 | 179 |
|
197 | 180 | return false; |
198 | | - } |
| 181 | +} |
199 | 182 |
|
200 | | - void FileUplink :: |
201 | | - compareChecksums(const Fw::FilePacket::EndPacket& endPacket) |
202 | | - { |
| 183 | +void FileUplink ::compareChecksums(const Fw::FilePacket::EndPacket& endPacket) { |
203 | 184 | CFDP::Checksum computed, stored; |
204 | 185 | this->m_file.getChecksum(computed); |
205 | 186 | endPacket.getChecksum(stored); |
206 | 187 | if (computed != stored) { |
207 | | - this->m_warnings.badChecksum( |
208 | | - computed.getValue(), |
209 | | - stored.getValue() |
210 | | - ); |
| 188 | + this->m_warnings.badChecksum(computed.getValue(), stored.getValue()); |
211 | 189 | } |
212 | | - } |
| 190 | +} |
213 | 191 |
|
214 | | - void FileUplink :: |
215 | | - goToStartMode() |
216 | | - { |
| 192 | +void FileUplink ::goToStartMode() { |
217 | 193 | this->m_file.osFile.close(); |
218 | 194 | this->m_receiveMode = START; |
219 | 195 | this->m_lastSequenceIndex = 0; |
220 | 196 | this->m_lastPacketWriteStatus = Os::File::MAX_STATUS; |
221 | | - } |
| 197 | +} |
222 | 198 |
|
223 | | - void FileUplink :: |
224 | | - goToDataMode() |
225 | | - { |
| 199 | +void FileUplink ::goToDataMode() { |
226 | 200 | this->m_receiveMode = DATA; |
227 | 201 | this->m_lastSequenceIndex = 0; |
228 | 202 | this->m_lastPacketWriteStatus = Os::File::MAX_STATUS; |
229 | | - } |
230 | | - |
231 | 203 | } |
| 204 | + |
| 205 | +} // namespace Svc |
0 commit comments