F´ Flight Software - C/C++ Documentation devel
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
FileUplink.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title FileUplink.cpp
3// \author bocchino
4// \brief cpp file for FileUplink component implementation class
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#include <FpConfig.hpp>
16
17namespace Svc {
18
19 // ----------------------------------------------------------------------
20 // Construction, initialization, and destruction
21 // ----------------------------------------------------------------------
22
23 FileUplink ::
24 FileUplink(const char *const name) :
26 receiveMode(START),
27 lastSequenceIndex(0),
28 filesReceived(this),
29 packetsReceived(this),
30 warnings(this)
31 {
32
33 }
34
35 void FileUplink ::
36 init(
37 const NATIVE_INT_TYPE queueDepth,
38 const NATIVE_INT_TYPE instance
39 )
40 {
41 FileUplinkComponentBase::init(queueDepth, instance);
42 }
43
44 FileUplink ::
45 ~FileUplink()
46 {
47
48 }
49
50 // ----------------------------------------------------------------------
51 // Handler implementations for user-defined typed input ports
52 // ----------------------------------------------------------------------
53
54 void FileUplink ::
55 bufferSendIn_handler(
56 const NATIVE_INT_TYPE portNum,
57 Fw::Buffer& buffer
58 )
59 {
60 Fw::FilePacket filePacket;
61 const Fw::SerializeStatus status = filePacket.fromBuffer(buffer);
62 if (status != Fw::FW_SERIALIZE_OK) {
63 this->log_WARNING_HI_DecodeError(status);
64 } else {
65 Fw::FilePacket::Type header_type = filePacket.asHeader().getType();
66 switch (header_type) {
68 this->handleStartPacket(filePacket.asStartPacket());
69 break;
71 this->handleDataPacket(filePacket.asDataPacket());
72 break;
74 this->handleEndPacket(filePacket.asEndPacket());
75 break;
77 this->handleCancelPacket();
78 break;
79 default:
80 FW_ASSERT(0);
81 break;
82 }
83 }
84 this->bufferSendOut_out(0, buffer);
85 }
86
87 void FileUplink ::
88 pingIn_handler(
89 const NATIVE_INT_TYPE portNum,
90 U32 key
91 )
92 {
93 // return key
94 this->pingOut_out(0,key);
95 }
96
97 // ----------------------------------------------------------------------
98 // Private helper functions
99 // ----------------------------------------------------------------------
100
101 void FileUplink ::
102 handleStartPacket(const Fw::FilePacket::StartPacket& startPacket)
103 {
104 // Clear all event throttles in preparation for new start packet
105 this->log_WARNING_HI_FileWriteError_ThrottleClear();
106 this->log_WARNING_HI_InvalidReceiveMode_ThrottleClear();
107 this->log_WARNING_HI_PacketOutOfBounds_ThrottleClear();
108 this->log_WARNING_HI_PacketOutOfOrder_ThrottleClear();
109 this->packetsReceived.packetReceived();
110 if (this->receiveMode != START) {
111 this->file.osFile.close();
112 this->warnings.invalidReceiveMode(Fw::FilePacket::T_START);
113 }
114 const Os::File::Status status = this->file.open(startPacket);
115 if (status == Os::File::OP_OK) {
116 this->goToDataMode();
117 }
118 else {
119 this->warnings.fileOpen(this->file.name);
120 this->goToStartMode();
121 }
122 }
123
124 void FileUplink ::
125 handleDataPacket(const Fw::FilePacket::DataPacket& dataPacket)
126 {
127 this->packetsReceived.packetReceived();
128 if (this->receiveMode != DATA) {
129 this->warnings.invalidReceiveMode(Fw::FilePacket::T_DATA);
130 return;
131 }
132 const U32 sequenceIndex = dataPacket.asHeader().getSequenceIndex();
133 this->checkSequenceIndex(sequenceIndex);
134 const U32 byteOffset = dataPacket.getByteOffset();
135 const U32 dataSize = dataPacket.getDataSize();
136 if (byteOffset + dataSize > this->file.size) {
137 this->warnings.packetOutOfBounds(sequenceIndex, this->file.name);
138 return;
139 }
140 const Os::File::Status status = this->file.write(
141 dataPacket.getData(),
142 byteOffset,
143 dataSize
144 );
145 if (status != Os::File::OP_OK) {
146 this->warnings.fileWrite(this->file.name);
147 }
148 }
149
150 void FileUplink ::
151 handleEndPacket(const Fw::FilePacket::EndPacket& endPacket)
152 {
153 this->packetsReceived.packetReceived();
154 if (this->receiveMode == DATA) {
155 this->filesReceived.fileReceived();
156 this->checkSequenceIndex(endPacket.asHeader().getSequenceIndex());
157 this->compareChecksums(endPacket);
158 this->log_ACTIVITY_HI_FileReceived(this->file.name);
159 }
160 else {
161 this->warnings.invalidReceiveMode(Fw::FilePacket::T_END);
162 }
163 this->goToStartMode();
164 }
165
166 void FileUplink ::
167 handleCancelPacket()
168 {
169 this->packetsReceived.packetReceived();
170 this->log_ACTIVITY_HI_UplinkCanceled();
171 this->goToStartMode();
172 }
173
174 void FileUplink ::
175 checkSequenceIndex(const U32 sequenceIndex)
176 {
177 if (sequenceIndex != this->lastSequenceIndex + 1) {
178 this->warnings.packetOutOfOrder(
179 sequenceIndex,
180 this->lastSequenceIndex
181 );
182 }
183 this->lastSequenceIndex = sequenceIndex;
184 }
185
186 void FileUplink ::
187 compareChecksums(const Fw::FilePacket::EndPacket& endPacket)
188 {
189 CFDP::Checksum computed, stored;
190 this->file.getChecksum(computed);
191 endPacket.getChecksum(stored);
192 if (computed != stored) {
193 this->warnings.badChecksum(
194 computed.getValue(),
195 stored.getValue()
196 );
197 }
198 }
199
200 void FileUplink ::
201 goToStartMode()
202 {
203 this->file.osFile.close();
204 this->receiveMode = START;
205 this->lastSequenceIndex = 0;
206 }
207
208 void FileUplink ::
209 goToDataMode()
210 {
211 this->receiveMode = DATA;
212 this->lastSequenceIndex = 0;
213 }
214
215}
#define FW_ASSERT(...)
Definition Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
C++-compatible configuration header for fprime configuration.
Class representing a CFDP checksum.
Definition Checksum.hpp:23
U32 getValue() const
Get the checksum value.
Definition Checksum.cpp:66
The type of a data packet.
const FilePacket::Header & asHeader() const
Get this as a Header.
U32 getByteOffset() const
Get the byte offset.
const U8 * getData() const
Get the data.
U32 getDataSize() const
Get the data size.
The type of an end packet.
const FilePacket::Header & asHeader() const
Get this as a Header.
void getChecksum(CFDP::Checksum &checksum) const
Get the checksum.
Definition EndPacket.cpp:54
U32 getSequenceIndex(void) const
Type getType(void) const
void init()
Object initializer.
Definition ObjBase.cpp:27
@ OP_OK
Operation was successful.
Definition File.hpp:24
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
The type of a start packet.
A file packet.
SerializeStatus fromBuffer(const Buffer &buffer)
const StartPacket & asStartPacket() const
const EndPacket & asEndPacket() const
Type
Packet type.
const DataPacket & asDataPacket() const
const Header & asHeader() const