F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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) :
26  receiveMode(START),
27  lastSequenceIndex(0),
28  filesReceived(this),
29  packetsReceived(this),
30  warnings(this)
31  {
32 
33  }
34 
36  init(
37  const NATIVE_INT_TYPE queueDepth,
38  const NATIVE_INT_TYPE instance
39  )
40  {
41  FileUplinkComponentBase::init(queueDepth, instance);
42  }
43 
45  ~FileUplink(void)
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  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
63  const Fw::FilePacket::Header& header = filePacket.asHeader();
64  switch (header.type) {
66  this->handleStartPacket(filePacket.asStartPacket());
67  break;
69  this->handleDataPacket(filePacket.asDataPacket());
70  break;
72  this->handleEndPacket(filePacket.asEndPacket());
73  break;
75  this->handleCancelPacket();
76  break;
77  default:
78  FW_ASSERT(0);
79  break;
80  }
81  this->bufferSendOut_out(0, buffer);
82  }
83 
86  const NATIVE_INT_TYPE portNum,
87  U32 key
88  )
89  {
90  // return key
91  this->pingOut_out(0,key);
92  }
93 
94  // ----------------------------------------------------------------------
95  // Private helper functions
96  // ----------------------------------------------------------------------
97 
98  void FileUplink ::
99  handleStartPacket(const Fw::FilePacket::StartPacket& startPacket)
100  {
101  // Clear all event throttles in preparation for new start packet
106  this->packetsReceived.packetReceived();
107  if (this->receiveMode != START) {
108  this->file.osFile.close();
109  this->warnings.invalidReceiveMode(Fw::FilePacket::T_START);
110  }
111  const Os::File::Status status = this->file.open(startPacket);
112  if (status == Os::File::OP_OK) {
113  this->goToDataMode();
114  }
115  else {
116  this->warnings.fileOpen(this->file.name);
117  this->goToStartMode();
118  }
119  }
120 
123  {
124  this->packetsReceived.packetReceived();
125  if (this->receiveMode != DATA) {
126  this->warnings.invalidReceiveMode(Fw::FilePacket::T_DATA);
127  return;
128  }
129  const U32 sequenceIndex = dataPacket.header.sequenceIndex;
130  this->checkSequenceIndex(sequenceIndex);
131  const U32 byteOffset = dataPacket.byteOffset;
132  const U32 dataSize = dataPacket.dataSize;
133  if (byteOffset + dataSize > this->file.size) {
134  this->warnings.packetOutOfBounds(sequenceIndex, this->file.name);
135  return;
136  }
137  const Os::File::Status status = this->file.write(
138  dataPacket.data,
139  byteOffset,
140  dataSize
141  );
142  if (status != Os::File::OP_OK) {
143  this->warnings.fileWrite(this->file.name);
144  }
145  }
146 
149  {
150  this->packetsReceived.packetReceived();
151  if (this->receiveMode == DATA) {
152  this->filesReceived.fileReceived();
153  this->checkSequenceIndex(endPacket.header.sequenceIndex);
154  this->compareChecksums(endPacket);
156  }
157  else {
158  this->warnings.invalidReceiveMode(Fw::FilePacket::T_END);
159  }
160  this->goToStartMode();
161  }
162 
165  {
166  this->packetsReceived.packetReceived();
168  this->goToStartMode();
169  }
170 
172  checkSequenceIndex(const U32 sequenceIndex)
173  {
174  if (sequenceIndex != this->lastSequenceIndex + 1) {
175  this->warnings.packetOutOfOrder(
176  sequenceIndex,
177  this->lastSequenceIndex
178  );
179  }
180  this->lastSequenceIndex = sequenceIndex;
181  }
182 
185  {
186  CFDP::Checksum computed, stored;
187  this->file.getChecksum(computed);
188  endPacket.getChecksum(stored);
189  if (computed != stored) {
190  this->warnings.badChecksum(
191  computed.getValue(),
192  stored.getValue()
193  );
194  }
195  }
196 
198  goToStartMode(void)
199  {
200  this->file.osFile.close();
201  this->receiveMode = START;
202  this->lastSequenceIndex = 0;
203  }
204 
206  goToDataMode(void)
207  {
208  this->receiveMode = DATA;
209  this->lastSequenceIndex = 0;
210  }
211 
212 }
Fw::FilePacket::DataPacket::header
Header header
The packet header.
Definition: FilePacket.hpp:169
Fw::FilePacket::asHeader
const Header & asHeader(void) const
Definition: FilePacket.cpp:35
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::FilePacket::asEndPacket
const EndPacket & asEndPacket(void) const
Definition: FilePacket.cpp:55
Fw::FilePacket::StartPacket
The type of a start packet.
Definition: FilePacket.hpp:118
Fw::FilePacket::asStartPacket
const StartPacket & asStartPacket(void) const
Definition: FilePacket.cpp:41
CFDP::Checksum::getValue
U32 getValue(void) const
Get the checksum value.
Definition: Checksum.cpp:66
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: BufferSerializableAc.hpp:24
Fw::FilePacket::T_CANCEL
@ T_CANCEL
Definition: FilePacket.hpp:40
Fw::FilePacket::EndPacket
The type of an end packet.
Definition: FilePacket.hpp:216
Assert.hpp
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw::FilePacket::EndPacket::header
Header header
The packet header.
Definition: FilePacket.hpp:223
Fw::FilePacket
A file packet.
Definition: FilePacket.hpp:27
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
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
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
CFDP::Checksum
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
Fw::FilePacket::DataPacket::byteOffset
U32 byteOffset
The byte offset of the packet data into the destination file.
Definition: FilePacket.hpp:172
Fw::FilePacket::asDataPacket
const DataPacket & asDataPacket(void) const
Definition: FilePacket.cpp:48
Fw::FilePacket::T_END
@ T_END
Definition: FilePacket.hpp:39
Svc
Definition: ActiveLoggerComponentAc.cpp:22
Os::File::Status
Status
Definition: File.hpp:24
BasicTypes.hpp
Declares ISF basic types.
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
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Fw::FilePacket::T_DATA
@ T_DATA
Definition: FilePacket.hpp:38
Fw::FilePacket::DataPacket::dataSize
U16 dataSize
The size of the file data in the packet.
Definition: FilePacket.hpp:175