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>
20 #include <Os/SandboxedFile.hpp>
22 #include <config/FileDownlinkCfg.hpp>
23 
24 namespace Svc {
25 
27  friend class FileDownlinkTester;
28 
29  private:
30  // ----------------------------------------------------------------------
31  // Types
32  // ----------------------------------------------------------------------
33 
35  class Mode {
36  friend class FileDownlinkTester;
37 
38  public:
40  typedef enum { IDLE, DOWNLINK, CANCEL, WAIT, COOLDOWN } Type;
41 
42  public:
44  Mode() : m_value(IDLE) {}
45 
46  public:
48  void set(const Type value) {
49  this->m_mutex.lock();
50  this->m_value = value;
51  this->m_mutex.unLock();
52  }
53 
55  Type get() const {
56  this->m_mutex.lock();
57  const Type value = this->m_value;
58  this->m_mutex.unLock();
59  return value;
60  }
61 
62  private:
64  Type m_value;
65 
67  mutable Os::Mutex m_mutex;
68  };
69 
71  class File {
72  friend class FileDownlinkTester;
73 
74  public:
76  File() : m_size(0) {}
77 
78  private:
80  Fw::LogStringArg m_sourceName;
81 
83  Fw::LogStringArg m_destName;
84 
86  Os::SandboxedFile m_osFile;
87 
89  U32 m_size;
90 
92  CFDP::Checksum m_checksum;
93 
94  public:
96  Os::File::Status open(const Fw::FileNameString& sourceFileName,
97  const Fw::FileNameString& destFileName
98  );
99 
101  Os::File::Status read(U8* const data, const U32 byteOffset, const U32 size);
102 
104  void getChecksum(CFDP::Checksum& checksum) { checksum = this->m_checksum; }
105 
107  Fw::LogStringArg& getSourceName(void) { return this->m_sourceName; }
108 
110  Fw::LogStringArg& getDestName(void) { return this->m_destName; }
111 
113  void configureSandbox(const char* directory) { this->m_osFile.configure(directory); }
114 
116  Os::SandboxedFile& getOsFile(void) { return this->m_osFile; }
117 
119  U32 getSize(void) { return this->m_size; }
120  };
121 
123  class FilesSent {
124  friend class FileDownlinkTester;
125 
126  public:
128  FilesSent(FileDownlink* const fileDownlink) : m_sent_file_count(0), m_fileDownlink(fileDownlink) {}
129 
130  public:
132  void fileSent() {
133  ++this->m_sent_file_count;
134  this->m_fileDownlink->tlmWrite_FilesSent(m_sent_file_count);
135  }
136 
137  private:
139  U32 m_sent_file_count;
140 
142  FileDownlink* const m_fileDownlink;
143  };
144 
146  class PacketsSent {
147  friend class FileDownlinkTester;
148 
149  public:
151  PacketsSent(FileDownlink* const fileDownlink) : m_sent_packet_count(0), m_fileDownlink(fileDownlink) {}
152 
153  public:
155  void packetSent() {
156  ++this->m_sent_packet_count;
157  this->m_fileDownlink->tlmWrite_PacketsSent(m_sent_packet_count);
158  }
159 
160  private:
162  U32 m_sent_packet_count;
163 
165  FileDownlink* const m_fileDownlink;
166  };
167 
169  class Warnings {
170  friend class FileDownlinkTester;
171 
172  public:
174  Warnings(FileDownlink* const fileDownlink) : m_warning_count(0), m_fileDownlink(fileDownlink) {}
175 
176  public:
178  void fileOpenError();
179 
181  void fileRead(const Os::File::Status status);
182 
184  void zeroSize();
185 
187  void sourceOutOfSandbox();
188 
189  private:
191  void warning() {
192  ++this->m_warning_count;
193  this->m_fileDownlink->tlmWrite_Warnings(m_warning_count);
194  }
195 
196  private:
198  U32 m_warning_count;
199 
201  FileDownlink* const m_fileDownlink;
202  };
203 
205  enum CallerSource { COMMAND, PORT };
206 
208  struct FileEntry {
209  Fw::FileNameString srcFilename; // Name of requested file
210  Fw::FileNameString destFilename; // Name of requested file
211  U32 offset;
212  U32 length;
213  CallerSource source; // Source of the downlink request
214  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
215  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
216  U32 context; // Context id of request, only set for PORT sources.
217  };
218 
221  enum PacketType { FILE_PACKET, CANCEL_PACKET, COUNT_PACKET_TYPE };
222 
223  public:
224  // ----------------------------------------------------------------------
225  // Construction, initialization, and destruction
226  // ----------------------------------------------------------------------
227 
230  FileDownlink(const char* const compName
231  );
232 
235  void configure(U32 cooldown,
236  U32 cycleTime,
237  U32 fileQueueDepth
238  );
239 
242  void configure(const char* directory);
243 
245  void deinit();
246 
250  void preamble();
251 
254  ~FileDownlink();
255 
256  private:
257  // ----------------------------------------------------------------------
258  // Handler implementations for user-defined typed input ports
259  // ----------------------------------------------------------------------
260 
263  void Run_handler(const FwIndexType portNum,
264  U32 context
265  );
266 
269  Svc::SendFileResponse SendFile_handler(
270  const FwIndexType portNum,
271  const Fw::StringBase& sourceFilename,
272  const Fw::StringBase& destFilename,
273  U32 offset,
274  U32 length
275  );
276 
279  void bufferReturn_handler(const FwIndexType portNum,
280  Fw::Buffer& fwBuffer);
281 
284  void pingIn_handler(const FwIndexType portNum,
285  U32 key
286  );
287 
288  private:
289  // ----------------------------------------------------------------------
290  // Command handler implementations
291  // ----------------------------------------------------------------------
292 
295  void SendFile_cmdHandler(const FwOpcodeType opCode,
296  const U32 cmdSeq,
297  const Fw::CmdStringArg& sourceFilename,
298  const Fw::CmdStringArg& destFilename
299  );
300 
303  void Cancel_cmdHandler(const FwOpcodeType opCode,
304  const U32 cmdSeq
305  );
306 
309  void SendPartial_cmdHandler(
310  FwOpcodeType opCode,
311  U32 cmdSeq,
312  const Fw::CmdStringArg& sourceFilename,
313  const Fw::CmdStringArg& destFilename,
314  U32 startOffset,
315  U32 length
316  );
317 
318  private:
319  // ----------------------------------------------------------------------
320  // Private helper methods
321  // ----------------------------------------------------------------------
322 
323  void sendFile(
324  const Fw::FileNameString& sourceFilename,
325  const Fw::FileNameString& destFilename,
326  U32 startOffset,
327  U32 length
328  );
329 
330  // Individual packet transfer functions
331  Os::File::Status sendDataPacket(U32& byteOffset);
332  void sendCancelPacket();
333  void sendEndPacket();
334  void sendStartPacket();
335  void sendFilePacket(const Fw::FilePacket& filePacket);
336 
337  // State-helper functions
338  void exitFileTransfer();
339  void enterCooldown();
340 
341  // Function to acquire a buffer internally
342  void getBuffer(Fw::Buffer& buffer, PacketType type);
343  // Downlink the "next" packet
344  void downlinkPacket();
345  // Finish the file transfer
346  void finishHelper(bool is_cancel);
347  // Convert internal status enum to a command response
348  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
349  // Send response after completing file downlink
350  void sendResponse(SendFileStatus resp);
351 
352  private:
353  // ----------------------------------------------------------------------
354  // Member variables
355  // ----------------------------------------------------------------------
356 
358  bool m_configured;
359 
361  Os::Queue m_fileQueue;
362 
364  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
365 
367  Mode m_mode;
368 
370  File m_file;
371 
373  FilesSent m_filesSent;
374 
376  PacketsSent m_packetsSent;
377 
379  Warnings m_warnings;
380 
382  U32 m_sequenceIndex;
383 
385  U32 m_timeout;
386 
388  U32 m_cooldown;
389 
391  U32 m_curTimer;
392 
394  U32 m_cycleTime;
395 
397  Fw::Buffer m_buffer;
398 
400  U32 m_bufferSize;
401 
403  U32 m_byteOffset;
404 
406  U32 m_endOffset;
407 
409  Fw::FilePacket::Type m_lastCompletedType;
410 
412  U32 m_lastBufferId;
413 
415  struct FileEntry m_curEntry;
416 
418  U32 m_cntxId;
419 };
420 
421 } // end namespace Svc
422 
423 #endif
void configure(const char *allowedDirectory)
Configure the allowed sandbox directory.
FwIdType FwOpcodeType
The type of a command opcode.
A wrapper around Os::File that restricts file access to an allowed directory.
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:54
PlatformIndexType FwIndexType
Send file response struct.
RateGroupDivider component implementation.
A file packet.
Definition: FilePacket.hpp:33
static const U32 FILEDOWNLINK_INTERNAL_BUFFER_SIZE