Commit bc71d49d authored by Lorenz Meier's avatar Lorenz Meier

Merge pull request #1557 from DonLakeFlyer/FileManager

File Manager fixes and unit test
parents 886b40b5 f534aa7e
......@@ -236,6 +236,7 @@ HEADERS += \
src/comm/LinkManager.h \
src/comm/MAVLinkProtocol.h \
src/comm/MockLink.h \
src/comm/MockLinkFileServer.h \
src/comm/MockLinkMissionItemHandler.h \
src/comm/ProtocolInterface.h \
src/comm/QGCFlightGearLink.h \
......@@ -374,6 +375,7 @@ SOURCES += \
src/comm/LinkManager.cc \
src/comm/MAVLinkProtocol.cc \
src/comm/MockLink.cc \
src/comm/MockLinkFileServer.cc \
src/comm/MockLinkMissionItemHandler.cc \
src/comm/QGCFlightGearLink.cc \
src/comm/QGCJSBSimLink.cc \
......@@ -518,8 +520,6 @@ INCLUDEPATH += \
HEADERS += \
src/qgcunittest/FlightGearTest.h \
src/qgcunittest/MockMavlinkFileServer.h \
src/qgcunittest/MockMavlinkInterface.h \
src/qgcunittest/MultiSignalSpy.h \
src/qgcunittest/TCPLinkTest.h \
src/qgcunittest/TCPLoopBackServer.h \
......@@ -534,10 +534,10 @@ HEADERS += \
src/qgcunittest/PX4RCCalibrationTest.h \
src/qgcunittest/UnitTest.h \
src/VehicleSetup/SetupViewTest.h \
src/qgcunittest/FileManagerTest.h \
SOURCES += \
src/qgcunittest/FlightGearTest.cc \
src/qgcunittest/MockMavlinkFileServer.cc \
src/qgcunittest/MultiSignalSpy.cc \
src/qgcunittest/TCPLinkTest.cc \
src/qgcunittest/TCPLoopBackServer.cc \
......@@ -552,6 +552,7 @@ SOURCES += \
src/qgcunittest/PX4RCCalibrationTest.cc \
src/qgcunittest/UnitTest.cc \
src/VehicleSetup/SetupViewTest.cc \
src/qgcunittest/FileManagerTest.cc \
} # DebugBuild|WindowsDebugAndRelease
} # AndroidBuild
......
......@@ -75,7 +75,8 @@ MockLink::MockLink(MockConfiguration* config) :
_mavlinkStarted(false),
_mavBaseMode(MAV_MODE_FLAG_MANUAL_INPUT_ENABLED | MAV_MODE_FLAG_CUSTOM_MODE_ENABLED),
_mavState(MAV_STATE_STANDBY),
_autopilotType(MAV_AUTOPILOT_PX4)
_autopilotType(MAV_AUTOPILOT_PX4),
_fileServer(NULL)
{
_config = config;
union px4_custom_mode px4_cm;
......@@ -84,6 +85,9 @@ MockLink::MockLink(MockConfiguration* config) :
px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_MANUAL;
_mavCustomMode = px4_cm.data;
_fileServer = new MockLinkFileServer(_vehicleSystemId, _vehicleComponentId, this);
Q_CHECK_PTR(_fileServer);
_missionItemHandler = new MockLinkMissionItemHandler(_vehicleSystemId, this);
Q_CHECK_PTR(_missionItemHandler);
......@@ -218,7 +222,6 @@ void MockLink::_loadParams(void)
void MockLink::_sendHeartBeat(void)
{
mavlink_message_t msg;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_heartbeat_pack(_vehicleSystemId,
_vehicleComponentId,
......@@ -228,7 +231,14 @@ void MockLink::_sendHeartBeat(void)
_mavBaseMode, // MAV_MODE
_mavCustomMode, // custom mode
_mavState); // MAV_STATE
respondWithMavlinkMessage(msg);
}
void MockLink::respondWithMavlinkMessage(const mavlink_message_t& msg)
{
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
int cBuffer = mavlink_msg_to_send_buffer(buffer, &msg);
QByteArray bytes((char *)buffer, cBuffer);
emit bytesReceived(this, bytes);
......@@ -332,6 +342,10 @@ void MockLink::_handleIncomingMavlinkBytes(const uint8_t* bytes, int cBytes)
_handleMissionCount(msg);
break;
#endif
case MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL:
_handleFTP(msg);
break;
default:
qDebug() << "MockLink: Unhandled mavlink message, id:" << msg.msgid;
......@@ -340,15 +354,6 @@ void MockLink::_handleIncomingMavlinkBytes(const uint8_t* bytes, int cBytes)
}
}
void MockLink::_emitMavlinkMessage(const mavlink_message_t& msg)
{
uint8_t outputBuffer[MAVLINK_MAX_PACKET_LEN];
int cBuffer = mavlink_msg_to_send_buffer(outputBuffer, &msg);
QByteArray bytes((char *)outputBuffer, cBuffer);
emit bytesReceived(this, bytes);
}
void MockLink::_handleHeartBeat(const mavlink_message_t& msg)
{
Q_UNUSED(msg);
......@@ -494,7 +499,7 @@ void MockLink::_handleParamRequestList(const mavlink_message_t& msg)
paramType, // MAV_PARAM_TYPE
cParameters, // Total number of parameters
paramIndex++); // Index of this parameter
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
// Only first parameter the first time through
break;
......@@ -533,7 +538,7 @@ void MockLink::_handleParamRequestList(const mavlink_message_t& msg)
paramType, // MAV_PARAM_TYPE
cParameters, // Total number of parameters
paramIndex++); // Index of this parameter
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
}
}
......@@ -571,7 +576,7 @@ void MockLink::_handleParamSet(const mavlink_message_t& msg)
request.param_type, // Send same type back
_mapParamName2Value[componentId].count(), // Total number of parameters
_mapParamName2Value[componentId].keys().indexOf(paramId)); // Index of this parameter
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
void MockLink::_handleParamRequestRead(const mavlink_message_t& msg)
......@@ -613,7 +618,7 @@ void MockLink::_handleParamRequestRead(const mavlink_message_t& msg)
_mapParamName2MavParamType[paramId], // Parameter type
_mapParamName2Value[componentId].count(), // Total number of parameters
_mapParamName2Value[componentId].keys().indexOf(paramId)); // Index of this parameter
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
void MockLink::_handleMissionRequestList(const mavlink_message_t& msg)
......@@ -632,7 +637,7 @@ void MockLink::_handleMissionRequestList(const mavlink_message_t& msg)
msg.sysid, // Target is original sender
msg.compid, // Target is original sender
_missionItems.count()); // Number of mission items
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
void MockLink::_handleMissionRequest(const mavlink_message_t& msg)
......@@ -660,7 +665,7 @@ void MockLink::_handleMissionRequest(const mavlink_message_t& msg)
item.autocontinue,
item.param1, item.param2, item.param3, item.param4,
item.x, item.y, item.z);
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
void MockLink::_handleMissionItem(const mavlink_message_t& msg)
......@@ -711,5 +716,11 @@ void MockLink::emitRemoteControlChannelRawChanged(int channel, uint16_t raw)
chanRaw[16], // channel raw value
chanRaw[17], // channel raw value
0); // rss
_emitMavlinkMessage(responseMsg);
respondWithMavlinkMessage(responseMsg);
}
void MockLink::_handleFTP(const mavlink_message_t& msg)
{
Q_ASSERT(_fileServer);
_fileServer->handleFTPMessage(msg);
}
......@@ -28,6 +28,7 @@
#include <QLoggingCategory>
#include "MockLinkMissionItemHandler.h"
#include "MockLinkFileServer.h"
#include "LinkManager.h"
#include "QGCMAVLink.h"
......@@ -71,6 +72,11 @@ public:
MAV_AUTOPILOT getAutopilotType(void) { return _autopilotType; }
void setAutopilotType(MAV_AUTOPILOT autopilot) { _autopilotType = autopilot; }
void emitRemoteControlChannelRawChanged(int channel, uint16_t raw);
/// Sends the specified mavlink message to QGC
void respondWithMavlinkMessage(const mavlink_message_t& msg);
MockLinkFileServer* getFileServer(void) { return _fileServer; }
// These are left unimplemented in order to cause linker errors which indicate incorrect usage of
// connect/disconnect on link directly. All connect/disconnect calls should be made through LinkManager.
......@@ -109,7 +115,6 @@ private:
void _handleIncomingNSHBytes(const char* bytes, int cBytes);
void _handleIncomingMavlinkBytes(const uint8_t* bytes, int cBytes);
void _loadParams(void);
void _emitMavlinkMessage(const mavlink_message_t& msg);
void _handleHeartBeat(const mavlink_message_t& msg);
void _handleSetMode(const mavlink_message_t& msg);
void _handleParamRequestList(const mavlink_message_t& msg);
......@@ -118,6 +123,7 @@ private:
void _handleMissionRequestList(const mavlink_message_t& msg);
void _handleMissionRequest(const mavlink_message_t& msg);
void _handleMissionItem(const mavlink_message_t& msg);
void _handleFTP(const mavlink_message_t& msg);
float _floatUnionForParam(int componentId, const QString& paramName);
void _setParamFloatUnionIntoMap(int componentId, const QString& paramName, float paramFloat);
......@@ -144,6 +150,8 @@ private:
MockConfiguration* _config;
MAV_AUTOPILOT _autopilotType;
MockLinkFileServer* _fileServer;
};
#endif
......@@ -21,29 +21,25 @@
======================================================================*/
#ifndef MOCKMAVLINKFILESERVER_H
#define MOCKMAVLINKFILESERVER_H
#include "MockMavlinkInterface.h"
#include "FileManager.h"
/// @file
/// @brief Mock implementation of Mavlink FTP server. Used as mavlink plugin to MockUAS.
/// Only root directory access is supported.
///
/// @author Don Gagne <don@thegagnes.com>
#ifndef MockLinkFileServer_H
#define MockLinkFileServer_H
#include "FileManager.h"
#include <QStringList>
class MockMavlinkFileServer : public MockMavlinkInterface
class MockLink;
/// Mock implementation of Mavlink FTP server.
class MockLinkFileServer : public QObject
{
Q_OBJECT
public:
/// @brief Constructor for MockMavlinkFileServer
/// @param System ID for QGroundControl App
/// @pqram System ID for this Server
MockMavlinkFileServer(uint8_t systemIdQGC, uint8_t systemIdServer);
MockLinkFileServer(uint8_t systemIdServer, uint8_t componentIdServer, MockLink* mockLink);
/// @brief Sets the list of files returned by the List command. Prepend names with F or D
/// to indicate (F)ile or (D)irectory.
......@@ -70,8 +66,8 @@ public:
/// @brief The number of ErrorModes in the rgFailureModes array.
static const size_t cFailureModes;
// From MockMavlinkInterface
virtual void sendMessage(mavlink_message_t message);
/// Called to handle an FTP message
void handleFTPMessage(const mavlink_message_t& message);
/// @brief Used to represent a single test case for download testing.
struct FileTestCase {
......@@ -88,18 +84,22 @@ public:
static const FileTestCase rgFileTestCases[cFileTestCases];
signals:
/// @brief You can connect to this signal to be notified when the server receives a Terminate command.
/// You can connect to this signal to be notified when the server receives a Terminate command.
void terminateCommandReceived(void);
/// You can connect to this signal to be notified when the server receives a Reset command.
void resetCommandReceived(void);
private:
void _sendAck(uint16_t seqNumber, FileManager::Opcode reqOpcode);
void _sendNak(FileManager::ErrorCode error, uint16_t seqNumber, FileManager::Opcode reqOpcode);
void _emitResponse(FileManager::Request* request, uint16_t seqNumber);
void _listCommand(FileManager::Request* request, uint16_t seqNumber);
void _openCommand(FileManager::Request* request, uint16_t seqNumber);
void _readCommand(FileManager::Request* request, uint16_t seqNumber);
void _streamCommand(FileManager::Request* request, uint16_t seqNumber);
void _terminateCommand(FileManager::Request* request, uint16_t seqNumber);
void _sendAck(uint8_t targetSystemId, uint8_t targetComponentId, uint16_t seqNumber, FileManager::Opcode reqOpcode);
void _sendNak(uint8_t targetSystemId, uint8_t targetComponentId, FileManager::ErrorCode error, uint16_t seqNumber, FileManager::Opcode reqOpcode);
void _sendResponse(uint8_t targetSystemId, uint8_t targetComponentId, FileManager::Request* request, uint16_t seqNumber);
void _listCommand(uint8_t senderSystemId, uint8_t senderComponentId, FileManager::Request* request, uint16_t seqNumber);
void _openCommand(uint8_t senderSystemId, uint8_t senderComponentId, FileManager::Request* request, uint16_t seqNumber);
void _readCommand(uint8_t senderSystemId, uint8_t senderComponentId, FileManager::Request* request, uint16_t seqNumber);
void _streamCommand(uint8_t senderSystemId, uint8_t senderComponentId, FileManager::Request* request, uint16_t seqNumber);
void _terminateCommand(uint8_t senderSystemId, uint8_t senderComponentId, FileManager::Request* request, uint16_t seqNumber);
void _resetCommand(uint8_t senderSystemId, uint8_t senderComponentId, uint16_t seqNumber);
uint16_t _nextSeqNumber(uint16_t seqNumber);
QStringList _fileList; ///< List of files returned by List command
......@@ -108,7 +108,8 @@ private:
uint8_t _readFileLength; ///< Length of active file being read
ErrorMode_t _errMode; ///< Currently set error mode, as specified by setErrorMode
const uint8_t _systemIdServer; ///< System ID for server
const uint8_t _systemIdQGC; ///< QGC System ID
const uint8_t _componentIdServer; ///< Component ID for server
MockLink* _mockLink; ///< MockLink to communicate through
};
#endif
This diff is collapsed.
......@@ -28,9 +28,8 @@
#include <QtTest/QtTest>
#include "UnitTest.h"
#include "MockUAS.h"
#include "MockMavlinkFileServer.h"
#include "FileManager.h"
#include "MockLink.h"
#include "MultiSignalSpy.h"
/// @file
......@@ -47,18 +46,13 @@ public:
private slots:
// Test case initialization
void initTestCase(void);
void cleanupTestCase(void);
void init(void);
void cleanup(void);
// Test cases
void _ackTest(void);
void _noAckTest(void);
void _resetTest(void);
void _listTest(void);
void _readDownloadTest(void);
void _streamDownloadTest(void);
// Connected to FileManager listEntry signal
void listEntry(const QString& entry);
......@@ -76,16 +70,15 @@ private:
enum {
listEntrySignalMask = 1 << listEntrySignalIndex,
commandCompleteSignalMask = 1 << commandCompleteSignalIndex,
commandErrorSignalMask = 1 << errorMessageSignalIndex,
commandErrorSignalMask = 1 << commandErrorSignalIndex,
};
static const uint8_t _systemIdQGC = 255;
static const uint8_t _systemIdServer = 128;
MockUAS* _mockUAS;
MockMavlinkFileServer _mockFileServer;
FileManager* _fileManager;
MockLink* _mockLink;
MockLinkFileServer* _fileServer;
FileManager* _fileManager;
MultiSignalSpy* _multiSpy;
static const size_t _cSignals = maxSignalIndex;
......
//
// MockMavlinkInterface.cc
// QGroundControl
//
// Created by Donald Gagne on 6/19/14.
// Copyright (c) 2014 Donald Gagne. All rights reserved.
//
#include "MockMavlinkInterface.h"
/*=====================================================================
QGroundControl Open Source Ground Control Station
(c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
This file is part of the QGROUNDCONTROL project
QGROUNDCONTROL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QGROUNDCONTROL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
#include <QObject>
#include "QGCMAVLink.h"
#include "LinkInterface.h"
#ifndef MOCKMAVLINKINTERFACE_H
#define MOCKMAVLINKINTERFACE_H
class MockMavlinkInterface : public QObject
{
Q_OBJECT
public:
virtual void sendMessage(mavlink_message_t message) = 0;
signals:
// link argument will always be NULL
void messageReceived(LinkInterface* link, mavlink_message_t message);
};
#endif
......@@ -32,13 +32,13 @@
QGC_LOGGING_CATEGORY(FileManagerLog, "FileManagerLog")
FileManager::FileManager(QObject* parent, UASInterface* uas, uint8_t unitTestSystemIdQGC) :
FileManager::FileManager(QObject* parent, UASInterface* uas) :
QObject(parent),
_currentOperation(kCOIdle),
_mav(uas),
_lastOutgoingSeqNumber(0),
_activeSession(0),
_systemIdQGC(unitTestSystemIdQGC)
_systemIdQGC(0)
{
connect(&_ackTimer, &QTimer::timeout, this, &FileManager::_ackTimeout);
......@@ -104,8 +104,8 @@ void FileManager::_closeDownloadSession(bool success)
emit commandComplete();
}
// If !success error is emitted elsewhere
_readFileAccumulator.clear();
// Close the open session
_sendResetCommand();
}
......@@ -153,24 +153,19 @@ void FileManager::_downloadAckResponse(Request* readAck, bool readFile)
emit commandProgress(100 * ((float)_readFileAccumulator.length() / (float)_downloadFileSize));
}
if (readAck->hdr.size == sizeof(readAck->data)) {
if (readFile || readAck->hdr.burstComplete) {
// Possibly still more data to read, send next read request
if (readFile || readAck->hdr.burstComplete) {
// Possibly still more data to read, send next read request
Request request;
request.hdr.session = _activeSession;
request.hdr.opcode = readFile ? kCmdReadFile : kCmdBurstReadFile;
request.hdr.offset = _downloadOffset;
request.hdr.size = 0;
Request request;
request.hdr.session = _activeSession;
request.hdr.opcode = readFile ? kCmdReadFile : kCmdBurstReadFile;
request.hdr.offset = _downloadOffset;
request.hdr.size = 0;
_sendRequest(&request);
} else {
// Streaming, so next ack should come automatically
_setupAckTimeout();
}
} else if (readFile) {
// We only receieved a partial buffer back. These means we are at EOF
_closeDownloadSession(true /* success */);
_sendRequest(&request);
} else if (!readFile) {
// Streaming, so next ack should come automatically
_setupAckTimeout();
}
}
......@@ -340,7 +335,30 @@ void FileManager::receiveMessage(LinkInterface* link, mavlink_message_t message)
// Make sure we have a good sequence number
uint16_t expectedSeqNumber = _lastOutgoingSeqNumber + 1;
if (incomingSeqNumber != expectedSeqNumber) {
_currentOperation = kCOIdle;
switch (_currentOperation) {
case kCOBurst:
case kCORead:
_closeDownloadSession(false /* failure */);
break;
case kCOWrite:
_closeUploadSession(false /* failure */);
break;
case kCOOpenRead:
case kCOOpenBurst:
case kCOCreate:
// We could have an open session hanging around
_currentOperation = kCOIdle;
_sendResetCommand();
break;
default:
// Don't need to do anything special
_currentOperation = kCOIdle;
break;
}
_emitErrorMessage(tr("Bad sequence number on received message: expected(%1) received(%2)").arg(expectedSeqNumber).arg(incomingSeqNumber));
return;
}
......@@ -457,12 +475,22 @@ void FileManager::_sendListCommand(void)
void FileManager::downloadPath(const QString& from, const QDir& downloadDir)
{
if (_currentOperation != kCOIdle) {
_emitErrorMessage(tr("Command not sent. Waiting for previous command to complete."));
return;
}
qCDebug(FileManagerLog) << "downloadPath from:" << from << "to:" << downloadDir;
_downloadWorker(from, downloadDir, true /* read file */);
}
void FileManager::streamPath(const QString& from, const QDir& downloadDir)
{
if (_currentOperation != kCOIdle) {
_emitErrorMessage(tr("Command not sent. Waiting for previous command to complete."));
return;
}
qCDebug(FileManagerLog) << "streamPath from:" << from << "to:" << downloadDir;
_downloadWorker(from, downloadDir, false /* stream file */);
}
......@@ -677,7 +705,6 @@ void FileManager::_emitListEntry(const QString& entry)
/// @brief Sends the specified Request out to the UAS.
void FileManager::_sendRequest(Request* request)
{
qCDebug(FileManagerLog) << "_sendRequest opcode:" << request->hdr.opcode;
mavlink_message_t message;
......@@ -687,6 +714,8 @@ void FileManager::_sendRequest(Request* request)
request->hdr.seqNumber = _lastOutgoingSeqNumber;
qCDebug(FileManagerLog) << "_sendRequest opcode:" << request->hdr.opcode << "seqNumber:" << request->hdr.seqNumber;
if (_systemIdQGC == 0) {
_systemIdQGC = MAVLinkProtocol::instance()->getSystemId();
}
......
......@@ -37,12 +37,11 @@ class FileManager : public QObject
Q_OBJECT
public:
FileManager(QObject* parent, UASInterface* uas, uint8_t unitTestSystemIdQGC = 0);
FileManager(QObject* parent, UASInterface* uas);
/// These methods are only used for testing purposes.
bool _sendCmdTestAck(void) { return _sendOpcodeOnlyCmd(kCmdNone, kCOAck); };
bool _sendCmdTestNoAck(void) { return _sendOpcodeOnlyCmd(kCmdTestNoAck, kCOAck); };
bool _sendCmdReset(void) { return _sendOpcodeOnlyCmd(kCmdResetSessions, kCOAck); };
/// Timeout in msecs to wait for an Ack time come back. This is public so we can write unit tests which wait long enough
/// for the FileManager to timeout.
......@@ -226,9 +225,9 @@ private:
uint8_t _systemIdQGC; ///< System ID for QGC
uint8_t _systemIdServer; ///< System ID for server
// We give MockMavlinkFileServer friend access so that it can use the data structures and opcodes
// We give MockLinkFileServer friend access so that it can use the data structures and opcodes
// to build a mock mavlink file server for testing.
friend class MockMavlinkFileServer;
friend class MockLinkFileServer;
};
#endif // FileManager_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment