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 
233  void deinit();
234 
238  void preamble();
239 
242  ~FileDownlink();
243 
244  private:
245  // ----------------------------------------------------------------------
246  // Handler implementations for user-defined typed input ports
247  // ----------------------------------------------------------------------
248 
251  void Run_handler(const FwIndexType portNum,
252  U32 context
253  );
254 
257  Svc::SendFileResponse SendFile_handler(
258  const FwIndexType portNum,
259  const Fw::StringBase& sourceFilename,
260  const Fw::StringBase& destFilename,
261  U32 offset,
262  U32 length
263  );
264 
267  void bufferReturn_handler(const FwIndexType portNum,
268  Fw::Buffer& fwBuffer);
269 
272  void pingIn_handler(const FwIndexType portNum,
273  U32 key
274  );
275 
276  private:
277  // ----------------------------------------------------------------------
278  // Command handler implementations
279  // ----------------------------------------------------------------------
280 
283  void SendFile_cmdHandler(const FwOpcodeType opCode,
284  const U32 cmdSeq,
285  const Fw::CmdStringArg& sourceFilename,
286  const Fw::CmdStringArg& destFilename
287  );
288 
291  void Cancel_cmdHandler(const FwOpcodeType opCode,
292  const U32 cmdSeq
293  );
294 
297  void SendPartial_cmdHandler(
298  FwOpcodeType opCode,
299  U32 cmdSeq,
300  const Fw::CmdStringArg& sourceFilename,
301  const Fw::CmdStringArg& destFilename,
302  U32 startOffset,
303  U32 length
304  );
305 
306  private:
307  // ----------------------------------------------------------------------
308  // Private helper methods
309  // ----------------------------------------------------------------------
310 
311  void sendFile(
312  const char* sourceFilename,
313  const char* destFilename,
314  U32 startOffset,
315  U32 length
316  );
317 
318  // Individual packet transfer functions
319  Os::File::Status sendDataPacket(U32& byteOffset);
320  void sendCancelPacket();
321  void sendEndPacket();
322  void sendStartPacket();
323  void sendFilePacket(const Fw::FilePacket& filePacket);
324 
325  // State-helper functions
326  void exitFileTransfer();
327  void enterCooldown();
328 
329  // Function to acquire a buffer internally
330  void getBuffer(Fw::Buffer& buffer, PacketType type);
331  // Downlink the "next" packet
332  void downlinkPacket();
333  // Finish the file transfer
334  void finishHelper(bool is_cancel);
335  // Convert internal status enum to a command response;
336  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
337  // Send response after completing file downlink
338  void sendResponse(SendFileStatus resp);
339 
340  private:
341  // ----------------------------------------------------------------------
342  // Member variables
343  // ----------------------------------------------------------------------
344 
346  bool m_configured;
347 
349  Os::Queue m_fileQueue;
350 
352  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
353 
355  Mode m_mode;
356 
358  File m_file;
359 
361  FilesSent m_filesSent;
362 
364  PacketsSent m_packetsSent;
365 
367  Warnings m_warnings;
368 
370  U32 m_sequenceIndex;
371 
373  U32 m_timeout;
374 
376  U32 m_cooldown;
377 
379  U32 m_curTimer;
380 
382  U32 m_cycleTime;
383 
385  Fw::Buffer m_buffer;
386 
388  U32 m_bufferSize;
389 
391  U32 m_byteOffset;
392 
394  U32 m_endOffset;
395 
397  Fw::FilePacket::Type m_lastCompletedType;
398 
400  U32 m_lastBufferId;
401 
403  struct FileEntry m_curEntry;
404 
406  U32 m_cntxId;
407 };
408 
409 } // end namespace Svc
410 
411 #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