F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
FileUplink.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FileUplink.cpp
3 // \author bocchino
4 // \brief cpp file for FileUplink component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2016, 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 <FpConfig.hpp>
16 
17 namespace Svc {
18 
19  // ----------------------------------------------------------------------
20  // Construction, initialization, and destruction
21  // ----------------------------------------------------------------------
22 
24  FileUplink(const char *const name) :
26  m_receiveMode(START),
27  m_lastSequenceIndex(0),
28  m_filesReceived(this),
29  m_packetsReceived(this),
30  m_warnings(this)
31  {
32 
33  }
34 
36  init(
37  const NATIVE_INT_TYPE queueDepth,
38  const NATIVE_INT_TYPE instance
39  )
40  {
41  FileUplinkComponentBase::init(queueDepth, instance);
42  }
43 
46  {
47 
48  }
49 
50  // ----------------------------------------------------------------------
51  // Handler implementations for user-defined typed input ports
52  // ----------------------------------------------------------------------
53 
54  void FileUplink ::
55  bufferSendIn_handler(
56  const NATIVE_INT_TYPE portNum,
57  Fw::Buffer& buffer
58  )
59  {
60  Fw::FilePacket filePacket;
61  const Fw::SerializeStatus status = filePacket.fromBuffer(buffer);
62  if (status != Fw::FW_SERIALIZE_OK) {
63  this->log_WARNING_HI_DecodeError(status);
64  } else {
65  Fw::FilePacket::Type header_type = filePacket.asHeader().getType();
66  switch (header_type) {
68  this->handleStartPacket(filePacket.asStartPacket());
69  break;
71  this->handleDataPacket(filePacket.asDataPacket());
72  break;
74  this->handleEndPacket(filePacket.asEndPacket());
75  break;
77  this->handleCancelPacket();
78  break;
79  default:
80  FW_ASSERT(0);
81  break;
82  }
83  }
84  this->bufferSendOut_out(0, buffer);
85  }
86 
87  void FileUplink ::
88  pingIn_handler(
89  const NATIVE_INT_TYPE portNum,
90  U32 key
91  )
92  {
93  // return key
94  this->pingOut_out(0,key);
95  }
96 
97  // ----------------------------------------------------------------------
98  // Private helper functions
99  // ----------------------------------------------------------------------
100 
101  void FileUplink ::
102  handleStartPacket(const Fw::FilePacket::StartPacket& startPacket)
103  {
104  // Clear all event throttles in preparation for new start packet
109  this->m_packetsReceived.packetReceived();
110  if (this->m_receiveMode != START) {
111  this->m_file.osFile.close();
112  this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_START);
113  }
114  const Os::File::Status status = this->m_file.open(startPacket);
115  if (status == Os::File::OP_OK) {
116  this->goToDataMode();
117  }
118  else {
119  this->m_warnings.fileOpen(this->m_file.name);
120  this->goToStartMode();
121  }
122  }
123 
124  void FileUplink ::
125  handleDataPacket(const Fw::FilePacket::DataPacket& dataPacket)
126  {
127  this->m_packetsReceived.packetReceived();
128  if (this->m_receiveMode != DATA) {
129  this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_DATA);
130  return;
131  }
132  const U32 sequenceIndex = dataPacket.asHeader().getSequenceIndex();
133  this->checkSequenceIndex(sequenceIndex);
134  const U32 byteOffset = dataPacket.getByteOffset();
135  const U32 dataSize = dataPacket.getDataSize();
136  if (byteOffset + dataSize > this->m_file.size) {
137  this->m_warnings.packetOutOfBounds(sequenceIndex, this->m_file.name);
138  return;
139  }
140  const Os::File::Status status = this->m_file.write(
141  dataPacket.getData(),
142  byteOffset,
143  dataSize
144  );
145  if (status != Os::File::OP_OK) {
146  this->m_warnings.fileWrite(this->m_file.name);
147  }
148  }
149 
150  void FileUplink ::
151  handleEndPacket(const Fw::FilePacket::EndPacket& endPacket)
152  {
153  this->m_packetsReceived.packetReceived();
154  if (this->m_receiveMode == DATA) {
155  this->m_filesReceived.fileReceived();
156  this->checkSequenceIndex(endPacket.asHeader().getSequenceIndex());
157  this->compareChecksums(endPacket);
158  this->log_ACTIVITY_HI_FileReceived(this->m_file.name);
159  }
160  else {
161  this->m_warnings.invalidReceiveMode(Fw::FilePacket::T_END);
162  }
163  this->goToStartMode();
164  }
165 
166  void FileUplink ::
167  handleCancelPacket()
168  {
169  this->m_packetsReceived.packetReceived();
171  this->goToStartMode();
172  }
173 
174  void FileUplink ::
175  checkSequenceIndex(const U32 sequenceIndex)
176  {
177  if (sequenceIndex != this->m_lastSequenceIndex + 1) {
178  this->m_warnings.packetOutOfOrder(
179  sequenceIndex,
180  this->m_lastSequenceIndex
181  );
182  }
183  this->m_lastSequenceIndex = sequenceIndex;
184  }
185 
186  void FileUplink ::
187  compareChecksums(const Fw::FilePacket::EndPacket& endPacket)
188  {
189  CFDP::Checksum computed, stored;
190  this->m_file.getChecksum(computed);
191  endPacket.getChecksum(stored);
192  if (computed != stored) {
193  this->m_warnings.badChecksum(
194  computed.getValue(),
195  stored.getValue()
196  );
197  }
198  }
199 
200  void FileUplink ::
201  goToStartMode()
202  {
203  this->m_file.osFile.close();
204  this->m_receiveMode = START;
205  this->m_lastSequenceIndex = 0;
206  }
207 
208  void FileUplink ::
209  goToDataMode()
210  {
211  this->m_receiveMode = DATA;
212  this->m_lastSequenceIndex = 0;
213  }
214 
215 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
C++-compatible configuration header for fprime configuration.
Class representing a CFDP checksum.
Definition: Checksum.hpp:23
U32 getValue() const
Get the checksum value.
Definition: Checksum.cpp:66
The type of a data packet.
Definition: FilePacket.hpp:197
const FilePacket::Header & asHeader() const
Get this as a Header.
Definition: FilePacket.hpp:237
U32 getByteOffset() const
Get the byte offset.
Definition: FilePacket.hpp:242
const U8 * getData() const
Get the data.
Definition: FilePacket.hpp:252
U32 getDataSize() const
Get the data size.
Definition: FilePacket.hpp:247
The type of an end packet.
Definition: FilePacket.hpp:269
void getChecksum(CFDP::Checksum &checksum) const
Get the checksum.
Definition: EndPacket.cpp:54
const FilePacket::Header & asHeader() const
Get this as a Header.
Definition: FilePacket.hpp:293
U32 getSequenceIndex(void) const
Definition: FilePacket.hpp:132
Type getType(void) const
Definition: FilePacket.hpp:128
void init()
Object initializer.
Definition: ObjBase.cpp:27
@ OP_OK
Operation was successful.
Definition: File.hpp:30
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
The type of a start packet.
Definition: FilePacket.hpp:139
A file packet.
Definition: FilePacket.hpp:27
SerializeStatus fromBuffer(const Buffer &buffer)
Definition: FilePacket.cpp:23
const StartPacket & asStartPacket() const
Definition: FilePacket.cpp:41
const EndPacket & asEndPacket() const
Definition: FilePacket.cpp:55
Type
Packet type.
Definition: FilePacket.hpp:36
const DataPacket & asDataPacket() const
Definition: FilePacket.cpp:48
const Header & asHeader() const
Definition: FilePacket.cpp:35