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 
15 #include <config/FileDownlinkCfg.hpp>
18 #include <Os/File.hpp>
19 #include <Os/Mutex.hpp>
20 #include <Os/Queue.hpp>
21 
22 
23 namespace Svc {
24 
25  class FileDownlink final :
27  {
28 
29  friend class FileDownlinkTester;
30 
31  private:
32 
33  // ----------------------------------------------------------------------
34  // Types
35  // ----------------------------------------------------------------------
36 
38  class Mode {
39 
40  friend class FileDownlinkTester;
41 
42  public:
43 
45  typedef enum { IDLE, DOWNLINK, CANCEL, WAIT, COOLDOWN } Type;
46 
47  public:
48 
50  Mode() : m_value(IDLE) { }
51 
52  public:
53 
55  void set(const Type value) {
56  this->m_mutex.lock();
57  this->m_value = value;
58  this->m_mutex.unLock();
59  }
60 
62  Type get() {
63  this->m_mutex.lock();
64  const Type value = this->m_value;
65  this->m_mutex.unLock();
66  return value;
67  }
68 
69  private:
70 
72  Type m_value;
73 
75  Os::Mutex m_mutex;
76  };
77 
79  class File {
80 
81  friend class FileDownlinkTester;
82 
83  public:
84 
86  File() : m_size(0) { }
87 
88  private:
89 
91  Fw::LogStringArg m_sourceName;
92 
94  Fw::LogStringArg m_destName;
95 
97  Os::File m_osFile;
98 
100  U32 m_size;
101 
103  CFDP::Checksum m_checksum;
104 
105  public:
106 
108  Os::File::Status open(
109  const char *const sourceFileName,
110  const char *const destFileName
111  );
112 
114  Os::File::Status read(
115  U8 *const data,
116  const U32 byteOffset,
117  const U32 size
118  );
119 
121  void getChecksum(CFDP::Checksum& checksum) {
122  checksum = this->m_checksum;
123  }
124 
126  Fw::LogStringArg& getSourceName(void) {
127  return this->m_sourceName;
128  }
129 
131  Fw::LogStringArg& getDestName(void) {
132  return this->m_destName;
133  }
134 
136  Os::File& getOsFile(void) {
137  return this->m_osFile;
138  }
139 
141  U32 getSize(void) {
142  return this->m_size;
143  }
144  };
145 
147  class FilesSent {
148 
149  friend class FileDownlinkTester;
150 
151  public:
152 
154  FilesSent(FileDownlink *const fileDownlink) :
155  m_sent_file_count(0),
156  m_fileDownlink(fileDownlink)
157  { }
158 
159  public:
160 
162  void fileSent() {
163  ++this->m_sent_file_count;
164  this->m_fileDownlink->tlmWrite_FilesSent(m_sent_file_count);
165  }
166 
167  private:
168 
170  U32 m_sent_file_count;
171 
173  FileDownlink *const m_fileDownlink;
174 
175  };
176 
178  class PacketsSent {
179 
180  friend class FileDownlinkTester;
181 
182  public:
183 
185  PacketsSent(FileDownlink *const fileDownlink) :
186  m_sent_packet_count(0),
187  m_fileDownlink(fileDownlink)
188  { }
189 
190  public:
191 
193  void packetSent() {
194  ++this->m_sent_packet_count;
195  this->m_fileDownlink->tlmWrite_PacketsSent(m_sent_packet_count);
196  }
197 
198  private:
199 
201  U32 m_sent_packet_count;
202 
204  FileDownlink *const m_fileDownlink;
205 
206  };
207 
209  class Warnings {
210 
211  friend class FileDownlinkTester;
212 
213  public:
214 
216  Warnings(FileDownlink *const fileDownlink) :
217  m_warning_count(0),
218  m_fileDownlink(fileDownlink)
219  { }
220 
221  public:
222 
224  void fileOpenError();
225 
227  void fileRead(const Os::File::Status status);
228 
229  private:
230 
232  void warning() {
233  ++this->m_warning_count;
234  this->m_fileDownlink->tlmWrite_Warnings(m_warning_count);
235  }
236 
237  private:
238 
240  U32 m_warning_count;
241 
243  FileDownlink *const m_fileDownlink;
244 
245  };
246 
248  enum CallerSource { COMMAND, PORT };
249 
250  #define FILE_ENTRY_FILENAME_LEN 101
251 
253  struct FileEntry {
254  char srcFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
255  char destFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
256  U32 offset;
257  U32 length;
258  CallerSource source; // Source of the downlink request
259  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
260  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
261  U32 context; // Context id of request, only set for PORT sources.
262  };
263 
266  enum PacketType {
267  FILE_PACKET,
268  CANCEL_PACKET,
269  COUNT_PACKET_TYPE
270  };
271 
272  public:
273 
274  // ----------------------------------------------------------------------
275  // Construction, initialization, and destruction
276  // ----------------------------------------------------------------------
277 
280  FileDownlink(
281  const char *const compName
282  );
283 
286  void configure(
287  U32 timeout,
288  U32 cooldown,
289  U32 cycleTime,
290  U32 fileQueueDepth
291  );
292 
296  void preamble();
297 
300  ~FileDownlink();
301 
302  private:
303 
304  // ----------------------------------------------------------------------
305  // Handler implementations for user-defined typed input ports
306  // ----------------------------------------------------------------------
307 
310  void Run_handler(
311  const FwIndexType portNum,
312  U32 context
313  );
314 
315 
318  Svc::SendFileResponse SendFile_handler(
319  const FwIndexType portNum,
320  const Fw::StringBase& sourceFilename,
321  const Fw::StringBase& destFilename,
322  U32 offset,
323  U32 length
324  );
325 
328  void bufferReturn_handler(
329  const FwIndexType portNum,
330  Fw::Buffer &fwBuffer
331  );
332 
335  void pingIn_handler(
336  const FwIndexType portNum,
337  U32 key
338  );
339 
340 
341 
342  private:
343 
344  // ----------------------------------------------------------------------
345  // Command handler implementations
346  // ----------------------------------------------------------------------
347 
350  void SendFile_cmdHandler(
351  const FwOpcodeType opCode,
352  const U32 cmdSeq,
353  const Fw::CmdStringArg& sourceFilename,
354  const Fw::CmdStringArg& destFilename
355  );
356 
359  void Cancel_cmdHandler(
360  const FwOpcodeType opCode,
361  const U32 cmdSeq
362  );
363 
366  void SendPartial_cmdHandler(
367  FwOpcodeType opCode,
368  U32 cmdSeq,
369  const Fw::CmdStringArg& sourceFilename,
370  const Fw::CmdStringArg& destFilename,
371  U32 startOffset,
372  U32 length
373  );
374 
375 
376  private:
377 
378  // ----------------------------------------------------------------------
379  // Private helper methods
380  // ----------------------------------------------------------------------
381 
382 
383  void sendFile(
384  const char* sourceFilename,
385  const char* destFilename,
386  U32 startOffset,
387  U32 length
388  );
389 
390  //Individual packet transfer functions
391  Os::File::Status sendDataPacket(U32 &byteOffset);
392  void sendCancelPacket();
393  void sendEndPacket();
394  void sendStartPacket();
395  void sendFilePacket(const Fw::FilePacket& filePacket);
396 
397  //State-helper functions
398  void exitFileTransfer();
399  void enterCooldown();
400 
401  //Function to acquire a buffer internally
402  void getBuffer(Fw::Buffer& buffer, PacketType type);
403  //Downlink the "next" packet
404  void downlinkPacket();
405  //Finish the file transfer
406  void finishHelper(bool is_cancel);
407  // Convert internal status enum to a command response;
408  Fw::CmdResponse statusToCmdResp(SendFileStatus status);
409  //Send response after completing file downlink
410  void sendResponse(SendFileStatus resp);
411 
412  private:
413 
414  // ----------------------------------------------------------------------
415  // Member variables
416  // ----------------------------------------------------------------------
417 
419  bool m_configured;
420 
422  Os::Queue m_fileQueue;
423 
425  U8 m_memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
426 
428  Mode m_mode;
429 
431  File m_file;
432 
434  FilesSent m_filesSent;
435 
437  PacketsSent m_packetsSent;
438 
440  Warnings m_warnings;
441 
443  U32 m_sequenceIndex;
444 
446  U32 m_timeout;
447 
449  U32 m_cooldown;
450 
452  U32 m_curTimer;
453 
455  U32 m_cycleTime;
456 
458  Fw::Buffer m_buffer;
459 
461  U32 m_bufferSize;
462 
464  U32 m_byteOffset;
465 
467  U32 m_endOffset;
468 
470  Fw::FilePacket::Type m_lastCompletedType;
471 
473  U32 m_lastBufferId;
474 
476  struct FileEntry m_curEntry;
477 
479  U32 m_cntxId;
480  };
481 
482 } // end namespace Svc
483 
484 #endif
FwIdType FwOpcodeType
The type of a command opcode.
Type
Packet type.
Definition: FilePacket.hpp:42
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:56
PlatformIndexType FwIndexType
Send file response struct.
RateGroupDivider component implementation.
A file packet.
Definition: FilePacket.hpp:33
static const U32 FILEDOWNLINK_INTERNAL_BUFFER_SIZE