F´ Flight Software - C/C++ Documentation  NASA-v1.5.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.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FileDownlink.hpp
3 // \author bocchino
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 
14 #include <Fw/Types/Assert.hpp>
15 #include <Fw/Types/BasicTypes.hpp>
16 
17 namespace Svc {
18 
19  // ----------------------------------------------------------------------
20  // Construction, initialization, and destruction
21  // ----------------------------------------------------------------------
22 
25  const char *const name,
26  const U16 downlinkPacketSize
27  ) :
29  downlinkPacketSize(downlinkPacketSize),
30  filesSent(this),
31  packetsSent(this),
32  warnings(this),
33  sequenceIndex(0)
34  {
35 
36  }
37 
39  init(
40  const NATIVE_INT_TYPE queueDepth,
41  const NATIVE_INT_TYPE instance
42  )
43  {
44  FileDownlinkComponentBase::init(queueDepth, instance);
45  }
46 
48  ~FileDownlink(void)
49  {
50 
51  }
52 
53  // ----------------------------------------------------------------------
54  // Command handler implementations
55  // ----------------------------------------------------------------------
56 
57  void FileDownlink ::
58  FileDownlink_SendFile_cmdHandler(
59  const FwOpcodeType opCode,
60  const U32 cmdSeq,
61  const Fw::CmdStringArg& sourceFileName,
62  const Fw::CmdStringArg& destFileName
63  )
64  {
65 
66  Os::File::Status status;
67 
68  if (this->mode.get() == Mode::CANCEL) {
69  Fw::LogStringArg sourceLogStringArg(sourceFileName);
70  Fw::LogStringArg destLogStringArg(destFileName);
72  sourceLogStringArg,
73  destLogStringArg
74  );
75  this->mode.set(Mode::IDLE);
76  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_OK);
77  return;
78  }
79 
80  this->mode.set(Mode::DOWNLINK);
81 
82  status = this->file.open(
83  sourceFileName.toChar(),
85  );
86  if (status != Os::File::OP_OK) {
87  this->warnings.fileOpenError();
88  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_EXECUTION_ERROR);
89  return;
90  }
91 
92  this->sendStartPacket();
93 
94  status = this->sendDataPackets();
95  if (status != Os::File::OP_OK) {
96  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_EXECUTION_ERROR);
97  return;
98  }
99 
100  this->file.osFile.close();
101 
102  if (this->mode.get() == Mode::CANCEL) {
103  this->sendCancelPacket();
105  this->file.sourceName,
106  this->file.destName
107  );
108  }
109  else {
110  this->sendEndPacket();
112  this->file.sourceName,
113  this->file.destName
114  );
115  this->filesSent.fileSent();
116  }
117 
118  this->mode.set(Mode::IDLE);
119 
120  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_OK);
121 
122  }
123 
126  const FwOpcodeType opCode,
127  const U32 cmdSeq
128  )
129  {
130  if (this->mode.get() == Mode::DOWNLINK)
131  this->mode.set(Mode::CANCEL);
132  this->cmdResponse_out(opCode, cmdSeq, Fw::COMMAND_OK);
133  }
134 
135  // ----------------------------------------------------------------------
136  // Private helper methods
137  // ----------------------------------------------------------------------
138 
139  Os::File::Status FileDownlink ::
140  sendDataPacket(const U32 byteOffset)
141  {
142 
143  const U32 fileSize = this->file.size;
144  FW_ASSERT(byteOffset < fileSize, byteOffset);
145  const U16 maxDataSize = static_cast<U16>(this->downlinkPacketSize);
146  const U16 dataSize = static_cast<U16>((byteOffset + maxDataSize > fileSize) ?
147  fileSize - byteOffset : maxDataSize);
148  U8 buffer[dataSize];
149 
150  const Os::File::Status status =
151  this->file.read(buffer, byteOffset, dataSize);
152  if (status != Os::File::OP_OK) {
153  this->warnings.fileRead();
154  return status;
155  }
156 
157  const Fw::FilePacket::DataPacket dataPacket = {
159  byteOffset,
160  dataSize,
161  buffer
162  };
163  ++this->sequenceIndex;
164  Fw::FilePacket filePacket;
165  filePacket.fromDataPacket(dataPacket);
166  this->sendFilePacket(filePacket);
167 
168  return Os::File::OP_OK;
169 
170  }
171 
173  sendDataPackets(void)
174  {
175  this->sequenceIndex = 1;
176  for (
177  U32 byteOffset = 0;
178  byteOffset < this->file.size;
179  byteOffset += this->downlinkPacketSize
180  ) {
181  if (this->mode.get() == Mode::CANCEL)
182  return Os::File::OP_OK;
183  const Os::File::Status status =
184  this->sendDataPacket(byteOffset);
185  if (status != Os::File::OP_OK)
186  return status;
187  }
188  return Os::File::OP_OK;
189  }
190 
192  sendCancelPacket(void)
193  {
194  const Fw::FilePacket::CancelPacket cancelPacket = {
196  };
197  Fw::FilePacket filePacket;
198  filePacket.fromCancelPacket(cancelPacket);
199  this->sendFilePacket(filePacket);
200  }
201 
203  sendEndPacket(void)
204  {
205 
206  const Fw::FilePacket::Header header = {
208  this->sequenceIndex
209  };
210  Fw::FilePacket::EndPacket endPacket;
211  endPacket.header = header;
212 
213  CFDP::Checksum checksum;
214  this->file.getChecksum(checksum);
215  endPacket.setChecksum(checksum);
216 
217  Fw::FilePacket filePacket;
218  filePacket.fromEndPacket(endPacket);
219  this->sendFilePacket(filePacket);
220 
221  }
222 
224  sendStartPacket(void)
225  {
226  Fw::FilePacket::StartPacket startPacket;
227  startPacket.initialize(
228  this->file.size,
229  this->file.sourceName.toChar(),
230  this->file.destName.toChar()
231  );
232  Fw::FilePacket filePacket;
233  filePacket.fromStartPacket(startPacket);
234  this->sendFilePacket(filePacket);
235  }
236 
238  sendFilePacket(const Fw::FilePacket& filePacket)
239  {
240  const U32 bufferSize = filePacket.bufferSize();
241  Fw::Buffer buffer = this->bufferGetCaller_out(0, bufferSize);
242  FW_ASSERT(buffer.getdata() != 0);
243  FW_ASSERT(buffer.getsize() == bufferSize, bufferSize, buffer.getsize());
244  const Fw::SerializeStatus status = filePacket.toBuffer(buffer);
245  FW_ASSERT(status == Fw::FW_SERIALIZE_OK, status);
246  this->bufferSendOut_out(0, buffer);
247  this->packetsSent.packetSent();
248  }
249 
252  const NATIVE_INT_TYPE portNum,
253  U32 key
254  )
255  {
256  // return key
257  this->pingOut_out(0,key);
258  }
259 
260 } // end namespace Svc
Fw::Buffer::getsize
U32 getsize(void)
get member size
Definition: BufferSerializableAc.cpp:61
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::CmdStringArg::toChar
const char * toChar(void) const
Definition: CmdString.cpp:34
Fw::FilePacket::toBuffer
SerializeStatus toBuffer(Buffer &buffer) const
Definition: FilePacket.cpp:117
Fw::FilePacket::CancelPacket
The type of a cancel packet.
Definition: FilePacket.hpp:259
Fw::LogStringArg
Definition: LogString.hpp:11
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Fw::FilePacket::StartPacket
The type of a start packet.
Definition: FilePacket.hpp:118
Fw::FilePacket::DataPacket
The type of a data packet.
Definition: FilePacket.hpp:162
Fw::FilePacket::fromDataPacket
void fromDataPacket(const DataPacket &dataPacket)
Definition: FilePacket.cpp:76
Fw::FilePacket::Header
The type of a packet header.
Definition: FilePacket.hpp:83
Fw::Buffer
Definition: BufferSerializableAc.hpp:24
Fw::FilePacket::T_CANCEL
@ T_CANCEL
Definition: FilePacket.hpp:40
Fw::CmdStringArg
Definition: CmdString.hpp:11
Fw::FilePacket::EndPacket
The type of an end packet.
Definition: FilePacket.hpp:216
Assert.hpp
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Fw::FilePacket::EndPacket::header
Header header
The packet header.
Definition: FilePacket.hpp:223
Fw::Buffer::getdata
U64 getdata(void)
get member data
Definition: BufferSerializableAc.cpp:57
Fw::FilePacket
A file packet.
Definition: FilePacket.hpp:27
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
Fw::FilePacket::bufferSize
U32 bufferSize(void) const
Definition: FilePacket.cpp:97
Fw::COMMAND_EXECUTION_ERROR
@ COMMAND_EXECUTION_ERROR
Definition: CmdResponsePortAc.hpp:29
FwOpcodeType
#define FwOpcodeType
Type representation for a command opcode.
Definition: FpConfig.hpp:62
Fw::FilePacket::fromStartPacket
void fromStartPacket(const StartPacket &startPacket)
Definition: FilePacket.cpp:69
Fw::FilePacket::EndPacket::setChecksum
void setChecksum(const CFDP::Checksum &checksum)
Set the checksum.
Definition: EndPacket.cpp:47
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
CFDP::Checksum
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
Fw::FilePacket::fromEndPacket
void fromEndPacket(const EndPacket &endPacket)
Definition: FilePacket.cpp:83
Fw::FilePacket::T_END
@ T_END
Definition: FilePacket.hpp:39
Svc
Definition: ActiveLoggerComponentAc.cpp:22
Os::File::Status
Status
Definition: File.hpp:24
Fw::COMMAND_OK
@ COMMAND_OK
Definition: CmdResponsePortAc.hpp:25
BasicTypes.hpp
Declares ISF basic types.
Os::File::OP_OK
@ OP_OK
Operation was successful.
Definition: File.hpp:25
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Fw::FilePacket::StartPacket::initialize
void initialize(const U32 fileSize, const char *const sourcePath, const char *const destinationPath)
Initialize a StartPacket with sequence number 0.
Definition: StartPacket.cpp:19
Fw::FilePacket::T_DATA
@ T_DATA
Definition: FilePacket.hpp:38
Fw::FilePacket::fromCancelPacket
void fromCancelPacket(const CancelPacket &cancelPacket)
Definition: FilePacket.cpp:90