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 char* const sourceFileName,
96  const char* const 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 
179  private:
181  void warning() {
182  ++this->m_warning_count;
183  this->m_fileDownlink->tlmWrite_Warnings(m_warning_count);
184  }
185 
186  private:
188  U32 m_warning_count;
189 
191  FileDownlink* const m_fileDownlink;
192  };
193 
195  enum CallerSource { COMMAND, PORT };
196 
198  struct FileEntry {
199  char srcFilename[Fw::FileNameString::STRING_SIZE]; // Name of requested file
200  char destFilename[Fw::FileNameString::STRING_SIZE]; // Name of requested file
201  U32 offset;
202  U32 length;
203  CallerSource source; // Source of the downlink request
204  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
205  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
206  U32 context; // Context id of request, only set for PORT sources.
207  };
208 
211  enum PacketType { FILE_PACKET, CANCEL_PACKET, COUNT_PACKET_TYPE };
212 
213  public:
214  // ----------------------------------------------------------------------
215  // Construction, initialization, and destruction
216  // ----------------------------------------------------------------------
217 
220  FileDownlink(const char* const compName
221  );
222 
225  void configure(U32 cooldown,
226  U32 cycleTime,
227  U32 fileQueueDepth
228  );
229 
231  void deinit();
232 
236  void preamble();
237 
240  ~FileDownlink();
241 
242  private:
243  // ----------------------------------------------------------------------
244  // Handler implementations for user-defined typed input ports
245  // ----------------------------------------------------------------------
246 
249  void Run_handler(const FwIndexType portNum,
250  U32 context
251  );
252 
255  Svc::SendFileResponse SendFile_handler(
256  const FwIndexType portNum,
257  const Fw::StringBase& sourceFilename,
258  const Fw::StringBase& destFilename,
259  U32 offset,
260  U32 length
261  );
262 
265  void bufferReturn_handler(const FwIndexType portNum,
266  Fw::Buffer& fwBuffer);
267 
270  void pingIn_handler(const FwIndexType portNum,
271  U32 key
272  );
273 
274  private:
275  // ----------------------------------------------------------------------
276  // Command handler implementations
277  // ----------------------------------------------------------------------
278 
281  void SendFile_cmdHandler(const FwOpcodeType opCode,
282  const U32 cmdSeq,
283  const Fw::CmdStringArg& sourceFilename,
284  const Fw::CmdStringArg& destFilename
285  );
286 
289  void Cancel_cmdHandler(const FwOpcodeType opCode,
290  const U32 cmdSeq
291  );
292 
295  void SendPartial_cmdHandler(
296  FwOpcodeType opCode,
297  U32 cmdSeq,
298  const Fw::CmdStringArg& sourceFilename,
299  const Fw::CmdStringArg& destFilename,
300  U32 startOffset,
301  U32 length
302  );
303 
304  private:
305  // ----------------------------------------------------------------------
306  // Private helper methods
307  // ----------------------------------------------------------------------
308 
309  void sendFile(
310  const char* sourceFilename,
311  const char* destFilename,
312  U32 startOffset,
313  U32 length
314  );
315 
316  // Individual packet transfer functions
317  Os::File::Status sendDataPacket(U32& byteOffset);
318  void sendCancelPacket();
319  void sendEndPacket();
320  void sendStartPacket();
321  void sendFilePacket(const Fw::FilePacket& filePacket);
322 
323  // State-helper functions
324  void exitFileTransfer();
325  void enterCooldown();
326 
327  // Function to acquire a buffer internally
328  void getBuffer(Fw::Buffer& buffer, PacketType type);
329  // Downlink the "next" packet
330  void downlinkPacket();
331  // Finish the file transfer
332  void finishHelper(bool is_cancel);
333  // Convert internal status enum to a command response;
334  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
335  // Send response after completing file downlink
336  void sendResponse(SendFileStatus resp);
337 
338  private:
339  // ----------------------------------------------------------------------
340  // Member variables
341  // ----------------------------------------------------------------------
342 
344  bool m_configured;
345 
347  Os::Queue m_fileQueue;
348 
350  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
351 
353  Mode m_mode;
354 
356  File m_file;
357 
359  FilesSent m_filesSent;
360 
362  PacketsSent m_packetsSent;
363 
365  Warnings m_warnings;
366 
368  U32 m_sequenceIndex;
369 
371  U32 m_timeout;
372 
374  U32 m_cooldown;
375 
377  U32 m_curTimer;
378 
380  U32 m_cycleTime;
381 
383  Fw::Buffer m_buffer;
384 
386  U32 m_bufferSize;
387 
389  U32 m_byteOffset;
390 
392  U32 m_endOffset;
393 
395  Fw::FilePacket::Type m_lastCompletedType;
396 
398  U32 m_lastBufferId;
399 
401  struct FileEntry m_curEntry;
402 
404  U32 m_cntxId;
405 };
406 
407 } // end namespace Svc
408 
409 #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