F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <FileDownlinkCfg.hpp>
16 #include <Svc/FileDownlink/FileDownlinkComponentAc.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 :
26  public FileDownlinkComponentBase
27  {
28 
29  PRIVATE:
30 
31  // ----------------------------------------------------------------------
32  // Types
33  // ----------------------------------------------------------------------
34 
36  class Mode {
37 
38  public:
39 
41  typedef enum { IDLE, DOWNLINK, CANCEL, WAIT, COOLDOWN } Type;
42 
43  public:
44 
46  Mode(void) : value(IDLE) { }
47 
48  public:
49 
51  void set(const Type value) {
52  this->mutex.lock();
53  this->value = value;
54  this->mutex.unLock();
55  }
56 
58  Type get(void) {
59  this->mutex.lock();
60  const Type value = this->value;
61  this->mutex.unLock();
62  return value;
63  }
64 
65  private:
66 
68  Type value;
69 
71  Os::Mutex mutex;
72  };
73 
75  class File {
76 
77  public:
78 
80  File() : size(0) { }
81 
82  public:
83 
85  Fw::LogStringArg sourceName;
86 
88  Fw::LogStringArg destName;
89 
91  Os::File osFile;
92 
94  U32 size;
95 
96  PRIVATE:
97 
99  CFDP::Checksum checksum;
100 
101  public:
102 
104  Os::File::Status open(
105  const char *const sourceFileName,
106  const char *const destFileName
107  );
108 
110  Os::File::Status read(
111  U8 *const data,
112  const U32 byteOffset,
113  const U32 size
114  );
115 
117  void getChecksum(CFDP::Checksum& checksum) {
118  checksum = this->checksum;
119  }
120  };
121 
123  class FilesSent {
124 
125  public:
126 
128  FilesSent(FileDownlink *const fileDownlink) :
129  n(0), fileDownlink(fileDownlink)
130  { }
131 
132  public:
133 
135  void fileSent(void) {
136  ++this->n;
137  this->fileDownlink->tlmWrite_FilesSent(n);
138  }
139 
140  PRIVATE:
141 
143  U32 n;
144 
146  FileDownlink *const fileDownlink;
147 
148  };
149 
151  class PacketsSent {
152 
153  public:
154 
156  PacketsSent(FileDownlink *const fileDownlink) :
157  n(0), fileDownlink(fileDownlink)
158  { }
159 
160  public:
161 
163  void packetSent(void) {
164  ++this->n;
165  this->fileDownlink->tlmWrite_PacketsSent(n);
166  }
167 
168  PRIVATE:
169 
171  U32 n;
172 
174  FileDownlink *const fileDownlink;
175 
176  };
177 
179  class Warnings {
180 
181  public:
182 
184  Warnings(FileDownlink *const fileDownlink) :
185  n(0), fileDownlink(fileDownlink)
186  { }
187 
188  public:
189 
191  void fileOpenError(void);
192 
194  void fileRead(const Os::File::Status status);
195 
196  PRIVATE:
197 
199  void warning(void) {
200  ++this->n;
201  this->fileDownlink->tlmWrite_Warnings(n);
202  }
203 
204  PRIVATE:
205 
207  U32 n;
208 
210  FileDownlink *const fileDownlink;
211 
212  };
213 
215  enum CallerSource { COMMAND, PORT };
216 
217  #define FILE_ENTRY_FILENAME_LEN 101
218 
220  struct FileEntry {
221  char srcFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
222  char destFilename[FILE_ENTRY_FILENAME_LEN]; // Name of requested file
223  U32 offset;
224  U32 length;
225  CallerSource source; // Source of the downlink request
226  FwOpcodeType opCode; // Op code of command, only set for CMD sources.
227  U32 cmdSeq; // CmdSeq number, only set for CMD sources.
228  U32 context; // Context id of request, only set for PORT sources.
229  };
230 
233  enum PacketType {
234  FILE_PACKET,
235  CANCEL_PACKET,
236  COUNT_PACKET_TYPE
237  };
238 
239  public:
240 
241  // ----------------------------------------------------------------------
242  // Construction, initialization, and destruction
243  // ----------------------------------------------------------------------
244 
247  FileDownlink(
248  const char *const compName
249  );
250 
253  void init(
254  const NATIVE_INT_TYPE queueDepth,
255  const NATIVE_INT_TYPE instance
256  );
257 
260  void configure(
261  U32 timeout,
262  U32 cooldown,
263  U32 cycleTime,
264  U32 fileQueueDepth
265  );
266 
270  void start(
271  NATIVE_INT_TYPE identifier,
272  NATIVE_INT_TYPE priority,
273  NATIVE_INT_TYPE stackSize,
274  NATIVE_INT_TYPE cpuAffinity=-1
275  );
276 
279  ~FileDownlink(void);
280 
281  PRIVATE:
282 
283  // ----------------------------------------------------------------------
284  // Handler implementations for user-defined typed input ports
285  // ----------------------------------------------------------------------
286 
289  void Run_handler(
290  const NATIVE_INT_TYPE portNum,
291  NATIVE_UINT_TYPE context
292  );
293 
294 
297  Svc::SendFileResponse SendFile_handler(
298  const NATIVE_INT_TYPE portNum,
299  sourceFileNameString sourceFilename,
300  destFileNameString destFilename,
301  U32 offset,
302  U32 length
303  );
304 
307  void bufferReturn_handler(
308  const NATIVE_INT_TYPE portNum,
309  Fw::Buffer &fwBuffer
310  );
311 
314  void pingIn_handler(
315  const NATIVE_INT_TYPE portNum,
316  U32 key
317  );
318 
319 
320 
321  PRIVATE:
322 
323  // ----------------------------------------------------------------------
324  // Command handler implementations
325  // ----------------------------------------------------------------------
326 
329  void SendFile_cmdHandler(
330  const FwOpcodeType opCode,
331  const U32 cmdSeq,
332  const Fw::CmdStringArg& sourceFilename,
333  const Fw::CmdStringArg& destFilename
334  );
335 
338  void Cancel_cmdHandler(
339  const FwOpcodeType opCode,
340  const U32 cmdSeq
341  );
342 
345  void SendPartial_cmdHandler(
346  FwOpcodeType opCode,
347  U32 cmdSeq,
348  const Fw::CmdStringArg& sourceFilename,
349  const Fw::CmdStringArg& destFilename,
350  U32 startOffset,
351  U32 length
352  );
353 
354 
355  PRIVATE:
356 
357  // ----------------------------------------------------------------------
358  // Private helper methods
359  // ----------------------------------------------------------------------
360 
361 
362  void sendFile(
363  const char* sourceFilename,
364  const char* destFilename,
365  U32 startOffset,
366  U32 length
367  );
368 
369  //Individual packet transfer functions
370  Os::File::Status sendDataPacket(U32 &byteOffset);
371  void sendCancelPacket(void);
372  void sendEndPacket(void);
373  void sendStartPacket(void);
374  void sendFilePacket(const Fw::FilePacket& filePacket);
375 
376  //State-helper functions
377  void exitFileTransfer(void);
378  void enterCooldown(void);
379 
380  //Function to acquire a buffer internally
381  void getBuffer(Fw::Buffer& buffer, PacketType type);
382  //Downlink the "next" packet
383  void downlinkPacket();
384  //Finish the file transfer
385  void finishHelper(bool is_cancel);
386  // Convert internal status enum to a command response;
387  Fw::CommandResponse statusToCmdResp(SendFileStatus status);
388  //Send response after completing file downlink
389  void sendResponse(SendFileStatus resp);
390 
391  PRIVATE:
392 
393  // ----------------------------------------------------------------------
394  // Member variables
395  // ----------------------------------------------------------------------
396 
398  bool configured;
399 
401  Os::Queue fileQueue;
402 
404  U8 memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
405 
407  Mode mode;
408 
410  File file;
411 
413  FilesSent filesSent;
414 
416  PacketsSent packetsSent;
417 
419  Warnings warnings;
420 
422  U32 sequenceIndex;
423 
425  U32 timeout;
426 
428  U32 cooldown;
429 
431  U32 curTimer;
432 
434  U32 cycleTime;
435 
437  Fw::Buffer buffer;
438 
440  U32 bufferSize;
441 
443  U32 byteOffset;
444 
446  U32 endOffset;
447 
449  Fw::FilePacket::Type lastCompletedType;
450 
452  U32 lastBufferId;
453 
455  struct FileEntry curEntry;
456 
458  U32 cntxId;
459  };
460 
461 } // end namespace Svc
462 
463 #endif
Fw::LogStringArg
Definition: LogString.hpp:11
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Svc::FILEDOWNLINK_INTERNAL_BUFFER_SIZE
static const U32 FILEDOWNLINK_INTERNAL_BUFFER_SIZE
Definition: FileDownlinkCfg.hpp:23
Fw::Buffer
Definition: Buffer.hpp:43
Fw::CmdStringArg
Definition: CmdString.hpp:11
Fw::FilePacket
A file packet.
Definition: FilePacket.hpp:27
Os::Mutex
Definition: Mutex.hpp:8
FwOpcodeType
#define FwOpcodeType
Type representation for a command opcode.
Definition: FpConfig.hpp:62
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Fw::FilePacket::Type
Type
Packet type.
Definition: FilePacket.hpp:36
File.hpp
CFDP::Checksum
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
Svc
Definition: ActiveRateGroupImplCfg.hpp:18
Os::File::Status
Status
Definition: File.hpp:24
FilePacket.hpp
Mutex.hpp
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Os::Queue
Definition: Queue.hpp:24
Os::File
Definition: File.hpp:11
Queue.hpp