F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FileDownlink.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FileDownlink.hpp
3 // \author bocchino, mstarch
4 // \brief hpp file for FileDownlink component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 // ======================================================================
11 
12 #ifndef Svc_FileDownlink_HPP
13 #define Svc_FileDownlink_HPP
14 
17 #include <Os/File.hpp>
18 #include <Os/Mutex.hpp>
19 #include <Os/Queue.hpp>
21 #include <config/FileDownlinkCfg.hpp>
22 
23 namespace Svc {
24 
26  friend class FileDownlinkTester;
27 
28  private:
29  // ----------------------------------------------------------------------
30  // Types
31  // ----------------------------------------------------------------------
32 
34  class Mode {
35  friend class FileDownlinkTester;
36 
37  public:
39  typedef enum { IDLE, DOWNLINK, CANCEL, WAIT, COOLDOWN } Type;
40 
41  public:
43  Mode() : m_value(IDLE) {}
44 
45  public:
47  void set(const Type value) {
48  this->m_mutex.lock();
49  this->m_value = value;
50  this->m_mutex.unLock();
51  }
52 
54  Type get() {
55  this->m_mutex.lock();
56  const Type value = this->m_value;
57  this->m_mutex.unLock();
58  return value;
59  }
60 
61  private:
63  Type m_value;
64 
66  Os::Mutex m_mutex;
67  };
68 
70  class File {
71  friend class FileDownlinkTester;
72 
73  public:
75  File() : m_size(0) {}
76 
77  private:
79  Fw::LogStringArg m_sourceName;
80 
82  Fw::LogStringArg m_destName;
83 
85  Os::File m_osFile;
86 
88  U32 m_size;
89 
91  CFDP::Checksum m_checksum;
92 
93  public:
95  Os::File::Status open(const Fw::FileNameString& sourceFileName,
96  const Fw::FileNameString& destFileName
97  );
98 
100  Os::File::Status read(U8* const data, const U32 byteOffset, const U32 size);
101 
103  void getChecksum(CFDP::Checksum& checksum) { checksum = this->m_checksum; }
104 
106  Fw::LogStringArg& getSourceName(void) { return this->m_sourceName; }
107 
109  Fw::LogStringArg& getDestName(void) { return this->m_destName; }
110 
112  Os::File& getOsFile(void) { return this->m_osFile; }
113 
115  U32 getSize(void) { return this->m_size; }
116  };
117 
119  class FilesSent {
120  friend class FileDownlinkTester;
121 
122  public:
124  FilesSent(FileDownlink* const fileDownlink) : m_sent_file_count(0), m_fileDownlink(fileDownlink) {}
125 
126  public:
128  void fileSent() {
129  ++this->m_sent_file_count;
130  this->m_fileDownlink->tlmWrite_FilesSent(m_sent_file_count);
131  }
132 
133  private:
135  U32 m_sent_file_count;
136 
138  FileDownlink* const m_fileDownlink;
139  };
140 
142  class PacketsSent {
143  friend class FileDownlinkTester;
144 
145  public:
147  PacketsSent(FileDownlink* const fileDownlink) : m_sent_packet_count(0), m_fileDownlink(fileDownlink) {}
148 
149  public:
151  void packetSent() {
152  ++this->m_sent_packet_count;
153  this->m_fileDownlink->tlmWrite_PacketsSent(m_sent_packet_count);
154  }
155 
156  private:
158  U32 m_sent_packet_count;
159 
161  FileDownlink* const m_fileDownlink;
162  };
163 
165  class Warnings {
166  friend class FileDownlinkTester;
167 
168  public:
170  Warnings(FileDownlink* const fileDownlink) : m_warning_count(0), m_fileDownlink(fileDownlink) {}
171 
172  public:
174  void fileOpenError();
175 
177  void fileRead(const Os::File::Status status);
178 
180  void zeroSize();
181 
182  private:
184  void warning() {
185  ++this->m_warning_count;
186  this->m_fileDownlink->tlmWrite_Warnings(m_warning_count);
187  }
188 
189  private:
191  U32 m_warning_count;
192 
194  FileDownlink* const m_fileDownlink;
195  };
196 
198  enum CallerSource { COMMAND, PORT };
199 
201  struct FileEntry {
202  Fw::FileNameString srcFilename; // Name of requested file
203  Fw::FileNameString destFilename; // Name of requested file
204  U32 offset;
205  U32 length;
206  CallerSource source; // Source of the downlink request
207  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
208  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
209  U32 context; // Context id of request, only set for PORT sources.
210  };
211 
214  enum PacketType { FILE_PACKET, CANCEL_PACKET, COUNT_PACKET_TYPE };
215 
216  public:
217  // ----------------------------------------------------------------------
218  // Construction, initialization, and destruction
219  // ----------------------------------------------------------------------
220 
223  FileDownlink(const char* const compName
224  );
225 
228  void configure(U32 cooldown,
229  U32 cycleTime,
230  U32 fileQueueDepth
231  );
232 
234  void deinit();
235 
239  void preamble();
240 
243  ~FileDownlink();
244 
245  private:
246  // ----------------------------------------------------------------------
247  // Handler implementations for user-defined typed input ports
248  // ----------------------------------------------------------------------
249 
252  void Run_handler(const FwIndexType portNum,
253  U32 context
254  );
255 
258  Svc::SendFileResponse SendFile_handler(
259  const FwIndexType portNum,
260  const Fw::StringBase& sourceFilename,
261  const Fw::StringBase& destFilename,
262  U32 offset,
263  U32 length
264  );
265 
268  void bufferReturn_handler(const FwIndexType portNum,
269  Fw::Buffer& fwBuffer);
270 
273  void pingIn_handler(const FwIndexType portNum,
274  U32 key
275  );
276 
277  private:
278  // ----------------------------------------------------------------------
279  // Command handler implementations
280  // ----------------------------------------------------------------------
281 
284  void SendFile_cmdHandler(const FwOpcodeType opCode,
285  const U32 cmdSeq,
286  const Fw::CmdStringArg& sourceFilename,
287  const Fw::CmdStringArg& destFilename
288  );
289 
292  void Cancel_cmdHandler(const FwOpcodeType opCode,
293  const U32 cmdSeq
294  );
295 
298  void SendPartial_cmdHandler(
299  FwOpcodeType opCode,
300  U32 cmdSeq,
301  const Fw::CmdStringArg& sourceFilename,
302  const Fw::CmdStringArg& destFilename,
303  U32 startOffset,
304  U32 length
305  );
306 
307  private:
308  // ----------------------------------------------------------------------
309  // Private helper methods
310  // ----------------------------------------------------------------------
311 
312  void sendFile(
313  const Fw::FileNameString& sourceFilename,
314  const Fw::FileNameString& destFilename,
315  U32 startOffset,
316  U32 length
317  );
318 
319  // Individual packet transfer functions
320  Os::File::Status sendDataPacket(U32& byteOffset);
321  void sendCancelPacket();
322  void sendEndPacket();
323  void sendStartPacket();
324  void sendFilePacket(const Fw::FilePacket& filePacket);
325 
326  // State-helper functions
327  void exitFileTransfer();
328  void enterCooldown();
329 
330  // Function to acquire a buffer internally
331  void getBuffer(Fw::Buffer& buffer, PacketType type);
332  // Downlink the "next" packet
333  void downlinkPacket();
334  // Finish the file transfer
335  void finishHelper(bool is_cancel);
336  // Convert internal status enum to a command response;
337  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
338  // Send response after completing file downlink
339  void sendResponse(SendFileStatus resp);
340 
341  private:
342  // ----------------------------------------------------------------------
343  // Member variables
344  // ----------------------------------------------------------------------
345 
347  bool m_configured;
348 
350  Os::Queue m_fileQueue;
351 
353  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
354 
356  Mode m_mode;
357 
359  File m_file;
360 
362  FilesSent m_filesSent;
363 
365  PacketsSent m_packetsSent;
366 
368  Warnings m_warnings;
369 
371  U32 m_sequenceIndex;
372 
374  U32 m_timeout;
375 
377  U32 m_cooldown;
378 
380  U32 m_curTimer;
381 
383  U32 m_cycleTime;
384 
386  Fw::Buffer m_buffer;
387 
389  U32 m_bufferSize;
390 
392  U32 m_byteOffset;
393 
395  U32 m_endOffset;
396 
398  Fw::FilePacket::Type m_lastCompletedType;
399 
401  U32 m_lastBufferId;
402 
404  struct FileEntry m_curEntry;
405 
407  U32 m_cntxId;
408 };
409 
410 } // end namespace Svc
411 
412 #endif
FwIdType FwOpcodeType
The type of a command opcode.
Type
Packet type.
Definition: FilePacket.hpp:40
Enum representing a command response.
Class representing a 32-bit checksum as mandated by the CCSDS File Delivery Protocol.
Definition: Checksum.hpp:53
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
PlatformIndexType FwIndexType
Send file response struct.
RateGroupDivider component implementation.
A file packet.
Definition: FilePacket.hpp:33
static const U32 FILEDOWNLINK_INTERNAL_BUFFER_SIZE