F´ Flight Software - C/C++ Documentation NASA-v1.6.0
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
23namespace Svc {
24
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() : 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() {
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() {
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() {
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();
192
194 void fileRead(const Os::File::Status status);
195
196 PRIVATE:
197
199 void warning() {
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
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 preamble();
271
275
276 PRIVATE:
277
278 // ----------------------------------------------------------------------
279 // Handler implementations for user-defined typed input ports
280 // ----------------------------------------------------------------------
281
284 void Run_handler(
285 const NATIVE_INT_TYPE portNum,
286 NATIVE_UINT_TYPE context
287 );
288
289
292 Svc::SendFileResponse SendFile_handler(
293 const NATIVE_INT_TYPE portNum,
294 const sourceFileNameString& sourceFilename,
295 const destFileNameString& destFilename,
296 U32 offset,
297 U32 length
298 );
299
302 void bufferReturn_handler(
303 const NATIVE_INT_TYPE portNum,
304 Fw::Buffer &fwBuffer
305 );
306
309 void pingIn_handler(
310 const NATIVE_INT_TYPE portNum,
311 U32 key
312 );
313
314
315
316 PRIVATE:
317
318 // ----------------------------------------------------------------------
319 // Command handler implementations
320 // ----------------------------------------------------------------------
321
324 void SendFile_cmdHandler(
325 const FwOpcodeType opCode,
326 const U32 cmdSeq,
327 const Fw::CmdStringArg& sourceFilename,
328 const Fw::CmdStringArg& destFilename
329 );
330
333 void Cancel_cmdHandler(
334 const FwOpcodeType opCode,
335 const U32 cmdSeq
336 );
337
340 void SendPartial_cmdHandler(
341 FwOpcodeType opCode,
342 U32 cmdSeq,
343 const Fw::CmdStringArg& sourceFilename,
344 const Fw::CmdStringArg& destFilename,
345 U32 startOffset,
346 U32 length
347 );
348
349
350 PRIVATE:
351
352 // ----------------------------------------------------------------------
353 // Private helper methods
354 // ----------------------------------------------------------------------
355
356
357 void sendFile(
358 const char* sourceFilename,
359 const char* destFilename,
360 U32 startOffset,
361 U32 length
362 );
363
364 //Individual packet transfer functions
365 Os::File::Status sendDataPacket(U32 &byteOffset);
366 void sendCancelPacket();
367 void sendEndPacket();
368 void sendStartPacket();
369 void sendFilePacket(const Fw::FilePacket& filePacket);
370
371 //State-helper functions
372 void exitFileTransfer();
373 void enterCooldown();
374
375 //Function to acquire a buffer internally
376 void getBuffer(Fw::Buffer& buffer, PacketType type);
377 //Downlink the "next" packet
378 void downlinkPacket();
379 //Finish the file transfer
380 void finishHelper(bool is_cancel);
381 // Convert internal status enum to a command response;
382 Fw::CmdResponse statusToCmdResp(SendFileStatus status);
383 //Send response after completing file downlink
384 void sendResponse(SendFileStatus resp);
385
386 PRIVATE:
387
388 // ----------------------------------------------------------------------
389 // Member variables
390 // ----------------------------------------------------------------------
391
393 bool configured;
394
396 Os::Queue fileQueue;
397
399 U8 memoryStore[COUNT_PACKET_TYPE][FILEDOWNLINK_INTERNAL_BUFFER_SIZE];
400
402 Mode mode;
403
405 File file;
406
408 FilesSent filesSent;
409
411 PacketsSent packetsSent;
412
414 Warnings warnings;
415
417 U32 sequenceIndex;
418
420 U32 timeout;
421
423 U32 cooldown;
424
426 U32 curTimer;
427
429 U32 cycleTime;
430
432 Fw::Buffer buffer;
433
435 U32 bufferSize;
436
438 U32 byteOffset;
439
441 U32 endOffset;
442
444 Fw::FilePacket::Type lastCompletedType;
445
447 U32 lastBufferId;
448
450 struct FileEntry curEntry;
451
453 U32 cntxId;
454 };
455
456} // end namespace Svc
457
458#endif
PlatformIntType NATIVE_INT_TYPE
Definition BasicTypes.h:51
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
PlatformUIntType NATIVE_UINT_TYPE
Definition BasicTypes.h:52
U32 FwOpcodeType
Definition FpConfig.h:56
Class representing a CFDP checksum.
Definition Checksum.hpp:23
static const U32 FILEDOWNLINK_INTERNAL_BUFFER_SIZE
A file packet.
Type
Packet type.