GCC Code Coverage Report


Directory: ./
File: Svc/FileUplink/FileUplink.hpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 21 21 100.0%
Functions: 9 9 100.0%
Branches: 8 8 100.0%

Line Branch Exec Source
1 // ======================================================================
2 // \title FileUplink.hpp
3 // \author bocchino
4 // \brief hpp 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
13 #ifndef Svc_FileUplink_HPP
14 #define Svc_FileUplink_HPP
15
16 #include <Fw/FilePacket/FilePacket.hpp>
17 #include <Os/File.hpp>
18 #include <Os/SandboxedFile.hpp>
19 #include <Svc/FileUplink/FileUplinkComponentAc.hpp>
20
21 namespace Svc {
22
23 class FileUplink final : public FileUplinkComponentBase {
24 friend class FileUplinkTester;
25
26 private:
27 // ----------------------------------------------------------------------
28 // Types
29 // ----------------------------------------------------------------------
30
31 //! The ReceiveMode type
32 typedef enum { START, DATA } ReceiveMode;
33
34 //! An object representing an incoming file
35 class File {
36 friend class FileUplinkTester;
37
38 public:
39 //! The file size
40 U32 size;
41
42 //! The file name
43 Fw::LogStringArg name;
44
45 //! The underlying OS file (sandboxed to restrict write locations)
46 Os::SandboxedFile osFile;
47
48 private:
49 //! The checksum for the file
50 ::CFDP::Checksum m_checksum;
51
52 public:
53 //! Open the OS file for writing and initialize the checksum
54 Os::File::Status open(const Fw::FilePacket::StartPacket& startPacket);
55
56 //! Write bytes into the OS file and update the checksum
57 //! \return OP_OK on success, NO_SPACE on a short write, or the OS status on failure
58 Os::File::Status write(const U8* const data, const U32 byteOffset, const U32 length);
59
60 //! Get the checksum
61 2 void getChecksum(::CFDP::Checksum& checksum) { checksum = this->m_checksum; }
62 };
63
64 //! Object to record files received
65 class FilesReceived {
66 friend class FileUplinkTester;
67
68 public:
69 //! Construct a FilesReceived object
70 12 FilesReceived(FileUplink* const fileUplink) : m_received_files_counter(0), m_fileUplink(fileUplink) {}
71
72 public:
73 //! Record a received file
74 1 void fileReceived() {
75 1 ++this->m_received_files_counter;
76
2/2
✓ Branch 8 taken 1 times.
✓ Branch 12 taken 1 times.
1 this->m_fileUplink->tlmWrite_FilesReceived(m_received_files_counter);
77 1 }
78
79 private:
80 //! The total number of files received
81 U32 m_received_files_counter;
82
83 //! The enclosing FileUplink object
84 FileUplink* const m_fileUplink;
85 };
86
87 //! Object to record files rejected for a bad checksum
88 class FilesReceivedFailed {
89 friend class FileUplinkTester;
90
91 public:
92 //! Construct a FilesReceivedFailed object
93 12 FilesReceivedFailed(FileUplink* const fileUplink) : m_failed_files_counter(0), m_fileUplink(fileUplink) {}
94
95 public:
96 //! Record a file rejected for a bad checksum
97 1 void fileReceivedFailed() {
98 1 ++this->m_failed_files_counter;
99
2/2
✓ Branch 8 taken 1 times.
✓ Branch 12 taken 1 times.
1 this->m_fileUplink->tlmWrite_FilesReceivedFailed(m_failed_files_counter);
100 1 }
101
102 private:
103 //! The total number of files rejected for a bad checksum
104 U32 m_failed_files_counter;
105
106 //! The enclosing FileUplink object
107 FileUplink* const m_fileUplink;
108 };
109
110 //! Object to record packets received
111 class PacketsReceived {
112 friend class FileUplinkTester;
113
114 public:
115 //! Construct a PacketsReceived object
116 12 PacketsReceived(FileUplink* const fileUplink) : m_received_packet_count(0), m_fileUplink(fileUplink) {}
117
118 public:
119 //! Record a packet received
120 27 void packetReceived() {
121 27 ++this->m_received_packet_count;
122
2/2
✓ Branch 8 taken 27 times.
✓ Branch 12 taken 27 times.
27 this->m_fileUplink->tlmWrite_PacketsReceived(m_received_packet_count);
123 27 }
124
125 private:
126 //! The total number of received packets
127 U32 m_received_packet_count;
128
129 //! The enclosing FileUplink object
130 FileUplink* const m_fileUplink;
131 };
132
133 //! Object to record warnings
134 class Warnings {
135 friend class FileUplinkTester;
136
137 public:
138 //! Construct a Warnings object
139 12 Warnings(FileUplink* const fileUplink) : m_warning_count(0), m_fileUplink(fileUplink) {}
140
141 public:
142 //! Record an Invalid Receive Mode warning
143 void invalidReceiveMode(const Fw::FilePacket::Type packetType);
144
145 //! Record a File Open warning
146 void fileOpen(Fw::LogStringArg& fileName);
147
148 //! Record a Packet Out of Bounds warning
149 void packetOutOfBounds(const U32 sequenceIndex, Fw::LogStringArg& fileName);
150
151 //! Record a Packet Out of Order warning
152 void packetOutOfOrder(const U32 sequenceIndex, const U32 lastSequenceIndex);
153
154 //! Record a Duplicate Packet warning
155 void packetDuplicate(const U32 sequenceIndex);
156
157 //! Record a File Write warning
158 void fileWrite(Fw::LogStringArg& fileName);
159
160 //! Record a Bad Checksum warning
161 void badChecksum(const U32 computed, const U32 read);
162
163 private:
164 //! Record a warning
165 9 void warning() {
166 9 ++this->m_warning_count;
167
2/2
✓ Branch 8 taken 9 times.
✓ Branch 12 taken 9 times.
9 this->m_fileUplink->tlmWrite_Warnings(m_warning_count);
168 9 }
169
170 private:
171 //! The total number of warnings
172 U32 m_warning_count;
173
174 //! The enclosing FileUplink object
175 FileUplink* const m_fileUplink;
176 };
177
178 public:
179 // ----------------------------------------------------------------------
180 // Construction, initialization, and destruction
181 // ----------------------------------------------------------------------
182
183 //! Construct object FileUplink
184 //!
185 FileUplink(const char* const name //!< The component name
186 );
187
188 //! Destroy object FileUplink
189 //!
190 ~FileUplink();
191
192 //! Configure the allowed uplink directory
193 //!
194 //! Restricts all file writes to the given directory.
195 //! Fail-open: until called, the sandbox defaults to `/` (any writable path is allowed).
196 //! Must be called before any files are received.
197 //! The directory must be an absolute path ending with `/`.
198 //!
199 //! \param directory: absolute path of the allowed uplink directory (must end with `/`)
200 //!
201 void configure(const char* directory);
202
203 private:
204 // ----------------------------------------------------------------------
205 // Handler implementations for user-defined typed input ports
206 // ----------------------------------------------------------------------
207
208 //! Handler implementation for bufferSendIn
209 //!
210 void bufferSendIn_handler(const FwIndexType portNum, //!< The port number
211 Fw::Buffer& buffer //!< Buffer wrapping data
212 );
213
214 //! Handler implementation for pingIn
215 //!
216 void pingIn_handler(const FwIndexType portNum, /*!< The port number*/
217 U32 key /*!< Value to return to pinger*/
218 );
219
220 private:
221 // ----------------------------------------------------------------------
222 // Private helper functions
223 // ----------------------------------------------------------------------
224
225 //! Handle a start packet
226 void handleStartPacket(const Fw::FilePacket::StartPacket& startPacket);
227
228 //! Handle a data packet
229 void handleDataPacket(const Fw::FilePacket::DataPacket& dataPacket);
230
231 //! Handle an end packet
232 void handleEndPacket(const Fw::FilePacket::EndPacket& endPacket);
233
234 //! Handle a cancel packet
235 void handleCancelPacket();
236
237 //! Check sequence index
238 void checkSequenceIndex(const U32 sequenceIndex);
239
240 //! Check if a received packet is a duplicate
241 bool checkDuplicatedPacket(const U32 sequenceIndex);
242
243 //! Compare checksums; returns true if the computed checksum matches the value in the END packet
244 bool compareChecksums(const Fw::FilePacket::EndPacket& endPacket);
245
246 //! Go to START mode
247 void goToStartMode();
248
249 //! Go to DATA mode
250 void goToDataMode();
251
252 private:
253 // ----------------------------------------------------------------------
254 // Member variables
255 // ----------------------------------------------------------------------
256
257 //! The receive mode
258 ReceiveMode m_receiveMode;
259
260 //! The sequence index of the last packet received
261 U32 m_lastSequenceIndex;
262
263 //! The write status of the last packet received
264 Os::File::Status m_lastPacketWriteStatus;
265
266 //! The file being assembled
267 File m_file;
268
269 //! The total number of files received
270 FilesReceived m_filesReceived;
271
272 //! The total number of files rejected for a bad checksum
273 FilesReceivedFailed m_filesReceivedFailed;
274
275 //! The total number of packets received
276 PacketsReceived m_packetsReceived;
277
278 //! The total number of warnings
279 Warnings m_warnings;
280 };
281
282 } // namespace Svc
283
284 #endif
285