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