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 
16 #include <Os/File.hpp>
17 #include <Os/Mutex.hpp>
18 #include <Os/Queue.hpp>
20 #include <config/FileDownlinkCfg.hpp>
21 
22 namespace Svc {
23 
25  friend class FileDownlinkTester;
26 
27  private:
28  // ----------------------------------------------------------------------
29  // Types
30  // ----------------------------------------------------------------------
31 
33  class Mode {
34  friend class FileDownlinkTester;
35 
36  public:
38  typedef enum { IDLE, DOWNLINK, CANCEL, WAIT, COOLDOWN } Type;
39 
40  public:
42  Mode() : m_value(IDLE) {}
43 
44  public:
46  void set(const Type value) {
47  this->m_mutex.lock();
48  this->m_value = value;
49  this->m_mutex.unLock();
50  }
51 
53  Type get() {
54  this->m_mutex.lock();
55  const Type value = this->m_value;
56  this->m_mutex.unLock();
57  return value;
58  }
59 
60  private:
62  Type m_value;
63 
65  Os::Mutex m_mutex;
66  };
67 
69  class File {
70  friend class FileDownlinkTester;
71 
72  public:
74  File() : m_size(0) {}
75 
76  private:
78  Fw::LogStringArg m_sourceName;
79 
81  Fw::LogStringArg m_destName;
82 
84  Os::File m_osFile;
85 
87  U32 m_size;
88 
90  CFDP::Checksum m_checksum;
91 
92  public:
94  Os::File::Status open(const char* const sourceFileName,
95  const char* const destFileName
96  );
97 
99  Os::File::Status read(U8* const data, const U32 byteOffset, const U32 size);
100 
102  void getChecksum(CFDP::Checksum& checksum) { checksum = this->m_checksum; }
103 
105  Fw::LogStringArg& getSourceName(void) { return this->m_sourceName; }
106 
108  Fw::LogStringArg& getDestName(void) { return this->m_destName; }
109 
111  Os::File& getOsFile(void) { return this->m_osFile; }
112 
114  U32 getSize(void) { return this->m_size; }
115  };
116 
118  class FilesSent {
119  friend class FileDownlinkTester;
120 
121  public:
123  FilesSent(FileDownlink* const fileDownlink) : m_sent_file_count(0), m_fileDownlink(fileDownlink) {}
124 
125  public:
127  void fileSent() {
128  ++this->m_sent_file_count;
129  this->m_fileDownlink->tlmWrite_FilesSent(m_sent_file_count);
130  }
131 
132  private:
134  U32 m_sent_file_count;
135 
137  FileDownlink* const m_fileDownlink;
138  };
139 
141  class PacketsSent {
142  friend class FileDownlinkTester;
143 
144  public:
146  PacketsSent(FileDownlink* const fileDownlink) : m_sent_packet_count(0), m_fileDownlink(fileDownlink) {}
147 
148  public:
150  void packetSent() {
151  ++this->m_sent_packet_count;
152  this->m_fileDownlink->tlmWrite_PacketsSent(m_sent_packet_count);
153  }
154 
155  private:
157  U32 m_sent_packet_count;
158 
160  FileDownlink* const m_fileDownlink;
161  };
162 
164  class Warnings {
165  friend class FileDownlinkTester;
166 
167  public:
169  Warnings(FileDownlink* const fileDownlink) : m_warning_count(0), m_fileDownlink(fileDownlink) {}
170 
171  public:
173  void fileOpenError();
174 
176  void fileRead(const Os::File::Status status);
177 
178  private:
180  void warning() {
181  ++this->m_warning_count;
182  this->m_fileDownlink->tlmWrite_Warnings(m_warning_count);
183  }
184 
185  private:
187  U32 m_warning_count;
188 
190  FileDownlink* const m_fileDownlink;
191  };
192 
194  enum CallerSource { COMMAND, PORT };
195 
196 #define FILE_ENTRY_FILENAME_LEN 101
197 
199  struct FileEntry {
200  char srcFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
201  char destFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
202  U32 offset;
203  U32 length;
204  CallerSource source; // Source of the downlink request
205  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
206  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
207  U32 context; // Context id of request, only set for PORT sources.
208  };
209 
212  enum PacketType { FILE_PACKET, CANCEL_PACKET, COUNT_PACKET_TYPE };
213 
214  public:
215  // ----------------------------------------------------------------------
216  // Construction, initialization, and destruction
217  // ----------------------------------------------------------------------
218 
221  FileDownlink(const char* const compName
222  );
223 
226  void configure(U32 timeout,
227  U32 cooldown,
228  U32 cycleTime,
229  U32 fileQueueDepth
230  );
231 
235  void preamble();
236 
239  ~FileDownlink();
240 
241  private:
242  // ----------------------------------------------------------------------
243  // Handler implementations for user-defined typed input ports
244  // ----------------------------------------------------------------------
245 
248  void Run_handler(const FwIndexType portNum,
249  U32 context
250  );
251 
254  Svc::SendFileResponse SendFile_handler(
255  const FwIndexType portNum,
256  const Fw::StringBase& sourceFilename,
257  const Fw::StringBase& destFilename,
258  U32 offset,
259  U32 length
260  );
261 
264  void bufferReturn_handler(const FwIndexType portNum,
265  Fw::Buffer& fwBuffer);
266 
269  void pingIn_handler(const FwIndexType portNum,
270  U32 key
271  );
272 
273  private:
274  // ----------------------------------------------------------------------
275  // Command handler implementations
276  // ----------------------------------------------------------------------
277 
280  void SendFile_cmdHandler(const FwOpcodeType opCode,
281  const U32 cmdSeq,
282  const Fw::CmdStringArg& sourceFilename,
283  const Fw::CmdStringArg& destFilename
284  );
285 
288  void Cancel_cmdHandler(const FwOpcodeType opCode,
289  const U32 cmdSeq
290  );
291 
294  void SendPartial_cmdHandler(
295  FwOpcodeType opCode,
296  U32 cmdSeq,
297  const Fw::CmdStringArg& sourceFilename,
298  const Fw::CmdStringArg& destFilename,
299  U32 startOffset,
300  U32 length
301  );
302 
303  private:
304  // ----------------------------------------------------------------------
305  // Private helper methods
306  // ----------------------------------------------------------------------
307 
308  void sendFile(
309  const char* sourceFilename,
310  const char* destFilename,
311  U32 startOffset,
312  U32 length
313  );
314 
315  // Individual packet transfer functions
316  Os::File::Status sendDataPacket(U32& byteOffset);
317  void sendCancelPacket();
318  void sendEndPacket();
319  void sendStartPacket();
320  void sendFilePacket(const Fw::FilePacket& filePacket);
321 
322  // State-helper functions
323  void exitFileTransfer();
324  void enterCooldown();
325 
326  // Function to acquire a buffer internally
327  void getBuffer(Fw::Buffer& buffer, PacketType type);
328  // Downlink the "next" packet
329  void downlinkPacket();
330  // Finish the file transfer
331  void finishHelper(bool is_cancel);
332  // Convert internal status enum to a command response;
333  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
334  // Send response after completing file downlink
335  void sendResponse(SendFileStatus resp);
336 
337  private:
338  // ----------------------------------------------------------------------
339  // Member variables
340  // ----------------------------------------------------------------------
341 
343  bool m_configured;
344 
346  Os::Queue m_fileQueue;
347 
349  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
350 
352  Mode m_mode;
353 
355  File m_file;
356 
358  FilesSent m_filesSent;
359 
361  PacketsSent m_packetsSent;
362 
364  Warnings m_warnings;
365 
367  U32 m_sequenceIndex;
368 
370  U32 m_timeout;
371 
373  U32 m_cooldown;
374 
376  U32 m_curTimer;
377 
379  U32 m_cycleTime;
380 
382  Fw::Buffer m_buffer;
383 
385  U32 m_bufferSize;
386 
388  U32 m_byteOffset;
389 
391  U32 m_endOffset;
392 
394  Fw::FilePacket::Type m_lastCompletedType;
395 
397  U32 m_lastBufferId;
398 
400  struct FileEntry m_curEntry;
401 
403  U32 m_cntxId;
404 };
405 
406 } // end namespace Svc
407 
408 #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