F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
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 <Fw/Types/BasicTypes.hpp>
16 
17 namespace Svc {
18 
19  // ----------------------------------------------------------------------
20  // Construction, initialization, and destruction
21  // ----------------------------------------------------------------------
22 
24  FileUplink(const char *const name) :
25  FileUplinkComponentBase(name),
26  receiveMode(START),
27  lastSequenceIndex(0),
28  filesReceived(this),
29  packetsReceived(this),
30  warnings(this)
31  {
32 
33  }
34 
35  void FileUplink ::
37  const NATIVE_INT_TYPE queueDepth,
38  const NATIVE_INT_TYPE instance
39  )
40  {
41  FileUplinkComponentBase::init(queueDepth, instance);
42  }
43 
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  const Fw::FilePacket::Header& header = filePacket.asHeader();
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.header.sequenceIndex;
133  this->checkSequenceIndex(sequenceIndex);
134  const U32 byteOffset = dataPacket.byteOffset;
135  const U32 dataSize = dataPacket.dataSize;
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.data,
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.header.sequenceIndex);
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 }
Fw::FilePacket::DataPacket::header
Header header
The packet header.
Definition: FilePacket.hpp:169
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::FilePacket::asEndPacket
const EndPacket & asEndPacket() const
Definition: FilePacket.cpp:55
Fw::FilePacket::StartPacket
The type of a start packet.
Definition: FilePacket.hpp:118
Fw::FilePacket::DataPacket
The type of a data packet.
Definition: FilePacket.hpp:162
Fw::FilePacket::Header
The type of a packet header.
Definition: FilePacket.hpp:83
Fw::Buffer
Definition: Buffer.hpp:43
Fw::FilePacket::T_CANCEL
@ T_CANCEL
Definition: FilePacket.hpp:40
Fw::FilePacket::EndPacket
The type of an end packet.
Definition: FilePacket.hpp:216
Fw::FilePacket::asDataPacket
const DataPacket & asDataPacket() const
Definition: FilePacket.cpp:48
Fw::FilePacket::EndPacket::header
Header header
The packet header.
Definition: FilePacket.hpp:223
Fw::FilePacket
A file packet.
Definition: FilePacket.hpp:27
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:27
Fw::FilePacket::T_START
@ T_START
Definition: FilePacket.hpp:37
Fw::FilePacket::EndPacket::getChecksum
void getChecksum(CFDP::Checksum &checksum) const
Get the checksum.
Definition: EndPacket.cpp:54
Fw::FilePacket::Header::sequenceIndex
U32 sequenceIndex
The sequence index.
Definition: FilePacket.hpp:93
CFDP::Checksum
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
CFDP::Checksum::getValue
U32 getValue() const
Get the checksum value.
Definition: Checksum.cpp:66
Fw::FilePacket::DataPacket::byteOffset
U32 byteOffset
The byte offset of the packet data into the destination file.
Definition: FilePacket.hpp:172
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::FilePacket::T_END
@ T_END
Definition: FilePacket.hpp:39
Svc
Definition: ActiveRateGroupCfg.hpp:18
Os::File::Status
Status
Definition: File.hpp:24
Fw::FilePacket::asHeader
const Header & asHeader() const
Definition: FilePacket.cpp:35
Os::File::OP_OK
@ OP_OK
Operation was successful.
Definition: File.hpp:25
Fw::FilePacket::DataPacket::data
const U8 * data
Pointer to the file data.
Definition: FilePacket.hpp:178
Fw::FilePacket::fromBuffer
SerializeStatus fromBuffer(const Buffer &buffer)
Definition: FilePacket.cpp:23
Fw::FilePacket::Header::type
Type type
The packet type.
Definition: FilePacket.hpp:90
Fw::FilePacket::T_DATA
@ T_DATA
Definition: FilePacket.hpp:38
Fw::FilePacket::asStartPacket
const StartPacket & asStartPacket() const
Definition: FilePacket.cpp:41
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw::FilePacket::DataPacket::dataSize
U16 dataSize
The size of the file data in the packet.
Definition: FilePacket.hpp:175