QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MockLinkFTP.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4
5#include <QtCore/QByteArray>
6#include <QtCore/QFile>
7#include <QtCore/QHash>
8#include <QtCore/QObject>
9#include <QtCore/QStringList>
10
11#include "MAVLinkFTP.h"
12
13class MockLink;
14
17class MockLinkFTP : public QObject
18{
19 Q_OBJECT
20
21public:
22 MockLinkFTP(quint32 systemIdServer, uint8_t componentIdServer, MockLink* mockLink);
24
27 void setFileList(const QStringList &fileList) { _fileList = fileList; }
28
30 struct LogFile {
31 QString name;
32 int size = 0;
33 uint32_t mtime = 0;
34 };
35
38 void setLogFiles(const QList<LogFile> &logFiles);
39
41 QList<LogFile> logFiles() const { return _logFiles; }
42
45 QByteArray logFileContents(const QString &name) const;
46
49 void setBurstReadDelayMs(int delayMs) { _burstReadDelayMs = delayMs; }
50
52 void setResponseDelayMs(int delayMs) { _responseDelayMs = delayMs; }
53
55 void setParamPckEnabled(bool enabled) { _paramPckEnabled = enabled; }
56
58 void setSingleSessionEnforced(bool enforced) { _singleSessionEnforced = enforced; }
59
62
64 void setIgnoreResetSessions(bool ignore) { _ignoreResetSessions = ignore; }
65
68 void setExpireSessionAfterBursts(int burstCount) { _expireSessionAfterBursts = burstCount; _burstsServed = 0; }
69
71 void setDropBurstPacketOnce(uint32_t offset) { _dropBurstPacketOffset = offset; _dropBurstPacketPending = true; }
72
75 void setReorderBurstPacketOnce(uint32_t offset) { _reorderBurstPacketOffset = offset; _reorderBurstPacketPending = true; }
76
78 int openFileROCount() const { return _openFileROCount; }
79
81 int readFileCount() const { return _readFileCount; }
82
84 int lastBurstReadRequestSize() const { return _lastBurstReadRequestSize; }
85
87 int lastReadFileRequestSize() const { return _lastReadFileRequestSize; }
88
90 void mavlinkMessageReceived(const mavlink_message_t &message);
91
92 void enableRandomDrops(bool enable) { _randomDropsEnabled = enable; }
93
95 QStringList uploadedFiles() const { return _uploadedFiles.keys(); }
96
98 QByteArray uploadedFileContents(const QString& remotePath) const { return _uploadedFiles.value(remotePath); }
99
101 void clearUploadedFiles() { _uploadedFiles.clear(); }
102
113
115 void setErrorMode(ErrorMode_t errMode) { _errMode = errMode; };
116
119 void setListDirectoryWithTimeSupported(bool supported) { _listDirectoryWithTimeSupported = supported; }
120
123 void setListDirectoryWithTimeNakError(MavlinkFTP::ErrorCode_t error) { _listDirectoryWithTimeNakError = error; }
124
126 void setLogDirEntriesWithTime(bool enabled) { _logDirEntriesWithTime = enabled; }
127
129 void setLogDirDotEntries(bool enabled) { _logDirDotEntries = enabled; }
130
141
143 static constexpr const size_t cFailureModes = std::size(MockLinkFTP::rgFailureModes);
144
145 static constexpr const char *sizeFilenamePrefix = "mocklink-size-";
146
149 static constexpr uint32_t kMockModificationTime = 1700000000;
150
151signals:
154
157
158private:
160 void _sendAck(quint32 targetSystemId, uint8_t targetComponentId, uint16_t seqNumber,
161 MavlinkFTP::OpCode_t reqOpCode);
162 void _sendNak(quint32 targetSystemId, uint8_t targetComponentId, MavlinkFTP::ErrorCode_t error, uint16_t seqNumber,
163 MavlinkFTP::OpCode_t reqOpCode);
164 void _sendNakErrno(quint32 targetSystemId, uint8_t targetComponentId, uint8_t nakErrno, uint16_t seqNumber,
165 MavlinkFTP::OpCode_t reqOpCode);
167 void _sendResponse(quint32 targetSystemId, uint8_t targetComponentId, MavlinkFTP::Request* request,
168 uint16_t seqNumber);
169 void _sendLastReply();
171 void _listCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
172 uint16_t seqNumber, bool withTime);
173 void _openCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
174 uint16_t seqNumber);
175 void _createFileCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
176 uint16_t seqNumber);
177 void _openFileWOCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
178 uint16_t seqNumber);
179 void _readCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
180 uint16_t seqNumber);
181 void _burstReadCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
182 uint16_t seqNumber);
183 void _removeFileCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
184 uint16_t seqNumber);
185 void _terminateCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
186 uint16_t seqNumber);
187 void _resetCommand(quint32 senderSystemId, uint8_t senderComponentId, uint16_t seqNumber);
188 void _writeCommand(quint32 senderSystemId, uint8_t senderComponentId, MavlinkFTP::Request* request,
189 uint16_t seqNumber);
190 void _finalizeActiveUpload();
193 uint16_t _nextSeqNumber(uint16_t seqNumber) const;
194 static QString _createTestTempFile(int size);
195 QString _generateParamPck(bool withDefaults);
196 QString _logFileTempPath(const QString &name);
198 std::optional<QStringList> _logDirectoryEntries(const QString& subdir, bool withTime) const;
199 static QByteArray _generateLogFileContents(const QString &name, int size);
200
202 static void ensureNullTemination(MavlinkFTP::Request *request);
203
204 const quint32 _systemIdServer;
205 const uint8_t _componentIdServer;
206 MockLink *_mockLink;
207
208 bool _lastReplyValid = false;
209 bool _randomDropsEnabled = false;
210 int _responseDelayMs = 0;
211 int _burstReadDelayMs = 0;
212 ErrorMode_t _errMode = errModeNone;
213 bool _listDirectoryWithTimeSupported = true;
214 MavlinkFTP::ErrorCode_t _listDirectoryWithTimeNakError = MavlinkFTP::kErrUnknownCommand;
215 bool _logDirEntriesWithTime = false;
216 bool _logDirDotEntries = false;
217 bool _paramPckEnabled = true;
218 bool _singleSessionEnforced = false;
219 bool _ignoreResetSessions = false;
220 int _expireSessionAfterBursts = 0;
221 int _burstsServed = 0;
222 uint32_t _dropBurstPacketOffset = 0;
223 bool _dropBurstPacketPending = false;
224 uint32_t _reorderBurstPacketOffset = 0;
225 bool _reorderBurstPacketPending = false;
226 int _openFileROCount = 0;
227 int _readFileCount = 0;
228 int _lastBurstReadRequestSize = -1;
229 int _lastReadFileRequestSize = -1;
230 mavlink_message_t _lastReply{};
231 QFile _currentFile;
232 QString _paramPckTempFile;
233 struct UploadSession {
234 bool active = false;
235 QString remotePath;
236 QByteArray buffer;
237
238 void reset() {
239 active = false;
240 remotePath.clear();
241 buffer.clear();
242 }
243 };
244 UploadSession _uploadSession;
245 QHash<QString, QByteArray> _uploadedFiles;
246 QStringList _fileList;
247 QList<LogFile> _logFiles;
248 QHash<QString, QString> _logFileTempPaths;
249 uint16_t _lastReplySequence = 0;
250
251 static constexpr uint8_t _sessionId = 1;
252};
struct __mavlink_message mavlink_message_t
ErrorCode_t
Error codes returned in Nak response PayloadHeader.data[0].
Definition MAVLinkFTP.h:62
@ kErrUnknownCommand
Unknown command opcode.
Definition MAVLinkFTP.h:70
void setParamPckEnabled(bool enabled)
When false, OpenFileRO of @PARAM/param.pck NAKs errno ENOENT, as PX4 without the virtual file does.
Definition MockLinkFTP.h:55
void setLogDirDotEntries(bool enabled)
When true, @MAV_LOG listings start with "D." and "D.." entries, as ArduPilot sends.
void enableRandomDrops(bool enable)
Definition MockLinkFTP.h:92
void resetCommandReceived()
You can connect to this signal to be notified when the server receives a Reset command.
void setFileList(const QStringList &fileList)
Definition MockLinkFTP.h:27
static constexpr uint32_t kMockModificationTime
void setReorderBurstPacketOnce(uint32_t offset)
Definition MockLinkFTP.h:75
void terminateCommandReceived()
You can connect to this signal to be notified when the server receives a Terminate command.
QList< LogFile > logFiles() const
Returns the log files served from the @MAV_LOG virtual directory.
Definition MockLinkFTP.h:41
void setLogFiles(const QList< LogFile > &logFiles)
void openStaleSessionForTest()
Opens a read session server-side without any client request, simulating a session left behind by a cr...
MockLinkFTP(quint32 systemIdServer, uint8_t componentIdServer, MockLink *mockLink)
void setDropBurstPacketOnce(uint32_t offset)
Drops the burst data packet at the given file offset, once, to force a hole the client must fill.
Definition MockLinkFTP.h:71
void setSingleSessionEnforced(bool enforced)
When true, OpenFileRO NAKs kErrNoSessionsAvailable while a read session is already open,...
Definition MockLinkFTP.h:58
void setBurstReadDelayMs(int delayMs)
Definition MockLinkFTP.h:49
void setExpireSessionAfterBursts(int burstCount)
Definition MockLinkFTP.h:68
void setListDirectoryWithTimeSupported(bool supported)
void setResponseDelayMs(int delayMs)
Delays replies asynchronously so timeout tests can keep processing events.
Definition MockLinkFTP.h:52
static constexpr const char * sizeFilenamePrefix
void mavlinkMessageReceived(const mavlink_message_t &message)
Called to handle an FTP message.
QByteArray logFileContents(const QString &name) const
static constexpr const ErrorMode_t rgFailureModes[]
int readFileCount() const
Number of ReadFile (non-burst) requests received since construction.
Definition MockLinkFTP.h:81
ErrorMode_t
By calling setErrorMode with one of these modes you can cause the server to simulate an error.
@ errModeBadSequence
Return response with bad sequence number.
@ errModeNoSecondResponseAllowRetry
No response to subsequent request to initial command, error will be cleared after this so retry will ...
@ errModeNakSecondResponse
Nak subsequent request to initial command.
@ errModeNoResponse
No response to any request, client should eventually time out with no Ack.
@ errModeNone
No error, respond correctly.
@ errModeNoSecondResponse
No response to subsequent request to initial command.
@ errModeNakResponse
Nak all requests.
QStringList uploadedFiles() const
Returns the list of remote paths which have been uploaded in this session.
Definition MockLinkFTP.h:95
void setIgnoreResetSessions(bool ignore)
When true, ResetSessions requests get no reply.
Definition MockLinkFTP.h:64
int lastBurstReadRequestSize() const
hdr.size of the most recent BurstReadFile request, -1 if none received yet.
Definition MockLinkFTP.h:84
static constexpr const size_t cFailureModes
The number of ErrorModes in the rgFailureModes array.
void clearUploadedFiles()
Clears the stored uploaded file contents.
void setLogDirEntriesWithTime(bool enabled)
When true, kCmdListDirectoryWithTime directory entries under @MAV_LOG carry "\t0\t<mtime>",...
void setErrorMode(ErrorMode_t errMode)
Sets the error mode for command responses. This allows you to simulate various server errors.
void setListDirectoryWithTimeNakError(MavlinkFTP::ErrorCode_t error)
QByteArray uploadedFileContents(const QString &remotePath) const
Returns the contents of an uploaded file. Empty if the path is unknown.
Definition MockLinkFTP.h:98
int openFileROCount() const
Number of OpenFileRO requests acked since construction.
Definition MockLinkFTP.h:78
int lastReadFileRequestSize() const
hdr.size of the most recent ReadFile request, -1 if none received yet.
Definition MockLinkFTP.h:87
Describes a simulated onboard log file served from the @MAV_LOG virtual directory.
Definition MockLinkFTP.h:30
int size
File size in bytes.
Definition MockLinkFTP.h:32
QString name
File name, e.g. "log_1.ulg", or "<subdir>/log_1.ulg" to serve it from a subdirectory.
Definition MockLinkFTP.h:31
uint32_t mtime
Modification time (seconds since UNIX epoch UTC).
Definition MockLinkFTP.h:33