18#include <QtCore/QApplicationStatic>
20#include <QtCore/QTimeZone>
21#include <QtCore/QTimer>
34 , _timer(new QTimer(this))
37 qCDebug(OnboardLogControllerLog) <<
this;
40 (void) connect(_timer, &QTimer::timeout,
this, &OnboardLogController::_processDownload);
42 _timer->setSingleShot(
false);
49 qCDebug(OnboardLogControllerLog) <<
this;
52bool OnboardLogController::_vehicleArmed()
const
54 return _vehicle && _vehicle->
armed();
57void OnboardLogController::_vehicleArmedChanged(
bool armed)
59 if (armed && (_requestingLogEntries || _downloadingLogs)) {
60 qCDebug(OnboardLogControllerLog) <<
"vehicle armed - canceling log transfer";
67 if (_vehicleArmed()) {
68 qCWarning(OnboardLogControllerLog) <<
"ignored - vehicle is armed";
73 if (_transport == Transport::Ftp) {
74 _ftpDownloadToDirectory(dir);
76 _downloadToDirectory(dir);
80void OnboardLogController::_downloadToDirectory(
const QString &dir)
82 _receivedAllEntries();
84 _downloadData.reset();
87 if (_downloadPath.isEmpty()) {
91 if (!_downloadPath.endsWith(QDir::separator())) {
92 _downloadPath += QDir::separator();
95 QGCOnboardLogEntry *
const log = _getNextSelected();
100 _setDownloading(
true);
104void OnboardLogController::_processDownload()
106 if (_requestingLogEntries) {
107 _findMissingEntries();
108 }
else if (_downloadingLogs) {
113void OnboardLogController::_findMissingEntries()
115 const int num_logs = _logEntriesModel->count();
118 for (
int i = 0; i < num_logs; i++) {
119 const QGCOnboardLogEntry *
const entry = _logEntriesModel->value<
const QGCOnboardLogEntry*>(i);
130 }
else if (start >= 0) {
136 _receivedAllEntries();
140 if (_retries++ > 2) {
141 for (
int i = 0; i < num_logs; i++) {
142 QGCOnboardLogEntry *
const entry = _logEntriesModel->value<QGCOnboardLogEntry*>(i);
148 _receivedAllEntries();
149 qCWarning(OnboardLogControllerLog) <<
"Too many errors retreiving log list. Giving up.";
160 _requestLogList(
static_cast<uint32_t
>(start),
static_cast<uint32_t
>(end));
163void OnboardLogController::_setActiveVehicle(
Vehicle *vehicle)
165 if (vehicle == _vehicle) {
174 if (_downloadData->file.exists()) {
175 (void) _downloadData->file.remove();
177 _downloadData.reset();
179 _setDownloading(
false);
182 _logEntriesModel->clearAndDeleteContents();
183 (void) disconnect(_vehicle, &
Vehicle::logEntry,
this, &OnboardLogController::_logEntry);
184 (void) disconnect(_vehicle, &
Vehicle::logData,
this, &OnboardLogController::_logData);
185 (void) disconnect(_vehicle, &
Vehicle::armedChanged,
this, &OnboardLogController::_vehicleArmedChanged);
187 FTPManager *
const ftp = _vehicle->ftpManager();
193 _ftpListState = FtpListState::Idle;
194 _ftpDirsToList.clear();
195 _ftpLogIdCounter = 0;
196 _ftpDownloadQueue.clear();
197 _ftpDeleteQueue.clear();
198 _ftpDeleting =
false;
199 _ftpCurrentDownloadEntry =
nullptr;
200 _ftpDisabled =
false;
201 _ftpDownloadHadError =
false;
202 _setTransport(Transport::Messages);
208 (void) connect(_vehicle, &
Vehicle::logEntry,
this, &OnboardLogController::_logEntry);
209 (void) connect(_vehicle, &
Vehicle::logData,
this, &OnboardLogController::_logData);
212 FTPManager *
const ftp = _vehicle->ftpManager();
220void OnboardLogController::_logEntry(uint32_t time_utc, uint32_t size, uint16_t
id, uint16_t num_logs, uint16_t last_log_num)
222 Q_UNUSED(last_log_num);
224 if (!_requestingLogEntries || (_transport != Transport::Messages)) {
228 if ((_logEntriesModel->count() == 0) && (num_logs > 0)) {
229 if (_vehicle->firmwareType() == MAV_AUTOPILOT_ARDUPILOTMEGA) {
234 for (
int i = 0; i < num_logs; i++) {
235 QGCOnboardLogEntry *
const entry =
new QGCOnboardLogEntry(i);
237 _logEntriesModel->append(entry);
242 if ((size > 0) || (_vehicle->firmwareType() != MAV_AUTOPILOT_ARDUPILOTMEGA)) {
244 if (id < _logEntriesModel->count()) {
245 QGCOnboardLogEntry *
const entry = _logEntriesModel->value<QGCOnboardLogEntry*>(id);
247 entry->
setTime(QDateTime::fromSecsSinceEpoch(time_utc));
251 qCWarning(OnboardLogControllerLog) <<
"Received onboard log entry for out-of-bound index:" << id;
255 _receivedAllEntries();
260 if (_entriesComplete()) {
261 _receivedAllEntries();
263 _timer->start(kTimeOutMs);
267void OnboardLogController::_receivedAllEntries()
273bool OnboardLogController::_entriesComplete()
const
275 const int num_logs = _logEntriesModel->count();
276 for (
int i = 0; i < num_logs; i++) {
277 const QGCOnboardLogEntry *
const entry = _logEntriesModel->value<
const QGCOnboardLogEntry*>(i);
290void OnboardLogController::_logData(uint32_t ofs, uint16_t
id, uint8_t count,
const uint8_t *data)
292 if (!_downloadingLogs || !_downloadData || (_transport != Transport::Messages)) {
297 if (_downloadData->ID !=
id) {
298 qCWarning(OnboardLogControllerLog) <<
"Received log data for wrong log";
302 if ((ofs % MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN) != 0) {
303 qCWarning(OnboardLogControllerLog) <<
"Ignored misaligned incoming packet @" << ofs;
308 if (ofs <= _downloadData->entry->
size()) {
311 if (chunk != _downloadData->current_chunk) {
312 qCWarning(OnboardLogControllerLog) <<
"Ignored packet for out of order chunk actual:expected" << chunk << _downloadData->current_chunk;
317 if (bin >= _downloadData->chunk_table.size()) {
318 qCWarning(OnboardLogControllerLog) <<
"Out of range bin received";
320 _downloadData->chunk_table.setBit(bin);
323 if (_downloadData->file.pos() != ofs) {
324 if (!_downloadData->file.seek(ofs)) {
325 qCWarning(OnboardLogControllerLog) <<
"Error while seeking log file offset";
330 if (_downloadData->file.write(
reinterpret_cast<const char*
>(data), count)) {
331 _downloadData->written += count;
332 _downloadData->rate_bytes += count;
338 _timer->start(kTimeOutMs);
339 if (_logComplete()) {
340 _downloadData->entry->setStatus(tr(
"Downloaded"));
342 }
else if (_chunkComplete()) {
343 _downloadData->advanceChunk();
344 _requestLogData(_downloadData->ID,
346 _downloadData->chunk_table.size() * MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN);
347 }
else if ((bin < (_downloadData->chunk_table.size() - 1)) && _downloadData->chunk_table.at(bin + 1)) {
352 qCWarning(OnboardLogControllerLog) <<
"Error while writing log file chunk";
355 qCWarning(OnboardLogControllerLog) <<
"Received log offset greater than expected";
359 _downloadData->entry->setStatus(tr(
"Error"));
363void OnboardLogController::_findMissingData()
365 if (_logComplete()) {
370 if (_chunkComplete()) {
371 _downloadData->advanceChunk();
378 uint16_t start = 0, end = 0;
379 const int size = _downloadData->chunk_table.size();
380 for (; start < size; start++) {
381 if (!_downloadData->chunk_table.testBit(start)) {
386 for (end = start; end < size; end++) {
387 if (_downloadData->chunk_table.testBit(end)) {
393 const uint32_t len = (end - start) * MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN;
394 _requestLogData(_downloadData->ID, pos, len, _retries);
397void OnboardLogController::_updateDataRate()
399 constexpr uint kSizeUpdateThreshold = 102400;
400 const bool timeThresholdMet = _downloadData->elapsed.elapsed() >= kGUIRateMs;
401 const bool sizeThresholdMet = (_downloadData->written - _downloadData->last_status_written) >= kSizeUpdateThreshold;
403 if (!timeThresholdMet && !sizeThresholdMet) {
408 if (timeThresholdMet) {
410 const qreal rate = _downloadData->rate_bytes / (_downloadData->elapsed.elapsed() / 1000.0);
411 _downloadData->rate_avg = (_downloadData->rate_avg * 0.95) + (rate * 0.05);
412 _downloadData->rate_bytes = 0;
416 _downloadData->elapsed.start();
423 _downloadData->entry->setStatus(status);
424 _downloadData->last_status_written = _downloadData->written;
427bool OnboardLogController::_chunkComplete()
const
429 return _downloadData->chunkEquals(
true);
432bool OnboardLogController::_logComplete()
const
434 return (_chunkComplete() && ((_downloadData->current_chunk + 1) == _downloadData->numChunks()));
437void OnboardLogController::_receivedAllData()
440 while (_prepareLogDownload()) {
441 if (_downloadData->chunk_table.isEmpty()) {
445 _downloadData->entry->setStatus(tr(
"Downloaded"));
446 _downloadData.reset();
450 _requestLogData(_downloadData->ID, 0, _downloadData->chunk_table.size() * MAVLINK_MSG_LOG_DATA_FIELD_DATA_LEN);
451 _timer->start(kTimeOutMs);
456 _setDownloading(
false);
459bool OnboardLogController::_prepareLogDownload()
461 _downloadData.reset();
463 QGCOnboardLogEntry *
const entry = _getNextSelected();
471 const QString ftime = (entry->
time().date().year() >= 2010) ? entry->
time().toString(QStringLiteral(
"yyyy-M-d-hh-mm-ss")) : QStringLiteral(
"UnknownDate");
473 _downloadData = std::make_unique<OnboardLogDownloadData>(entry);
474 _downloadData->filename = QStringLiteral(
"log_") + QString::number(entry->
id()) +
"_" + ftime;
476 if (_vehicle->firmwareType() == MAV_AUTOPILOT_PX4) {
477 const QString loggerParam = QStringLiteral(
"SYS_LOGGER");
478 ParameterManager *
const parameterManager = _vehicle->parameterManager();
480 if (loggerFact && (loggerFact->
rawValue().toInt() == 0)) {
481 _downloadData->filename +=
".px4log";
483 _downloadData->filename +=
".ulg";
486 _downloadData->filename +=
".bin";
489 _downloadData->file.setFileName(_downloadPath + _downloadData->filename);
491 if (_downloadData->file.exists()) {
492 uint32_t numDups = 0;
493 const QStringList filename_spl = _downloadData->filename.split(
'.');
496 const QString filename = filename_spl[0] +
'_' + QString::number(numDups) +
'.' + filename_spl[1];
497 _downloadData->file.setFileName(_downloadPath + filename);
498 }
while ( _downloadData->file.exists());
502 if (!_downloadData->file.open(QIODevice::WriteOnly)) {
503 qCWarning(OnboardLogControllerLog) <<
"Failed to create log file:" << _downloadData->filename;
504 }
else if (!_downloadData->file.resize(entry->
size())) {
505 qCWarning(OnboardLogControllerLog) <<
"Failed to allocate space for log file:" << _downloadData->filename;
507 _downloadData->current_chunk = 0;
508 _downloadData->chunk_table = QBitArray(_downloadData->chunkBins(),
false);
509 _downloadData->elapsed.start();
514 if (_downloadData->file.exists()) {
515 (void) _downloadData->file.remove();
518 _downloadData->entry->setStatus(tr(
"Error"));
519 _downloadData.reset();
527 if (_downloadingLogs || _requestingLogEntries || _ftpDeleting) {
530 qCDebug(OnboardLogControllerLog) <<
"refresh: ignored - transfer in progress";
534 if (_vehicleArmed()) {
535 qCWarning(OnboardLogControllerLog) <<
"ignored - vehicle is armed";
539 _logEntriesModel->clearAndDeleteContents();
542 if (_vehicle && !_ftpDisabled && _vehicle->capabilitiesKnown() && (_vehicle->capabilityBits() & MAV_PROTOCOL_CAPABILITY_FTP)) {
543 qCDebug(OnboardLogControllerLog) <<
"refresh: using ftp transport";
544 _setTransport(Transport::Ftp);
547 qCDebug(OnboardLogControllerLog) <<
"refresh: using message transport - ftpDisabled:" << _ftpDisabled
548 <<
"capabilitiesKnown:" << (_vehicle && _vehicle->capabilitiesKnown())
549 <<
"ftpCapable:" << bool(_vehicle && (_vehicle->capabilityBits() & MAV_PROTOCOL_CAPABILITY_FTP));
550 _setTransport(Transport::Messages);
551 _requestLogList(0, 0xffff);
557 const int numLogs = _logEntriesModel->
count();
558 for (
int i = 0; i < numLogs; i++) {
574 if (_transport == Transport::Ftp) {
576 if (_requestingLogEntries) {
579 _ftpListState = FtpListState::Idle;
580 _ftpDirsToList.clear();
581 _vehicle->ftpManager()->cancelListDirectory();
588 _ftpDeleteQueue.clear();
589 _resetSelection(
true);
593 if (_downloadingLogs) {
595 if (_ftpCurrentDownloadEntry) {
596 _ftpCurrentDownloadEntry->setStatus(tr(
"Canceled"));
597 _ftpCurrentDownloadEntry =
nullptr;
599 _ftpDownloadQueue.clear();
600 _vehicle->ftpManager()->cancelDownload();
605 _receivedAllEntries();
608 _downloadData->entry->setStatus(tr(
"Canceled"));
609 if (_downloadData->file.exists()) {
610 (void) _downloadData->file.remove();
613 _downloadData.reset();
617 _resetSelection(
true);
618 _setDownloading(
false);
623 const int count = _logEntriesModel->count();
624 for (
int i = 0; i < count; i++) {
645 const int count = _logEntriesModel->count();
646 for (
int i = 0; i < count; i++) {
660 const int count = _logEntriesModel->count();
661 for (
int i = 0; i < count; i++) {
671 return (selectable > 0) && (selected == selectable);
681 if (_sortAscending == ascending) {
685 _sortAscending = ascending;
686 _sortEntriesByTimestamp();
690void OnboardLogController::_resetSelection(
bool canceled)
692 const int num_logs = _logEntriesModel->
count();
693 for (
int i = 0; i < num_logs; i++) {
710void OnboardLogController::_sortEntriesByTimestamp()
712 QObjectList sortedEntries = *_logEntriesModel->objectList();
713 std::stable_sort(sortedEntries.begin(), sortedEntries.end(), [
this](
const QObject *lhsObj,
const QObject *rhsObj) {
714 const QGCOnboardLogEntry *const lhs = qobject_cast<const QGCOnboardLogEntry*>(lhsObj);
715 const QGCOnboardLogEntry *const rhs = qobject_cast<const QGCOnboardLogEntry*>(rhsObj);
726 const bool lhsHasTime = lhs->received() && (lhs->time().toSecsSinceEpoch() > 0);
727 const bool rhsHasTime = rhs->received() && (rhs->time().toSecsSinceEpoch() > 0);
728 if (lhsHasTime != rhsHasTime) {
733 if (lhsHasTime && rhsHasTime) {
734 if (lhs->time() == rhs->time()) {
735 return _sortAscending ? (lhs->id() < rhs->id()) : (lhs->id() > rhs->id());
738 return _sortAscending ? (lhs->time() < rhs->time()) : (lhs->time() > rhs->time());
742 return _sortAscending ? (lhs->id() < rhs->id()) : (lhs->id() > rhs->id());
745 (void) _logEntriesModel->swapObjectList(sortedEntries);
751 qCWarning(OnboardLogControllerLog) <<
"Vehicle Unavailable";
755 if (_vehicleArmed()) {
756 qCWarning(OnboardLogControllerLog) <<
"ignored - vehicle is armed";
762 qCWarning(OnboardLogControllerLog) <<
"Link Unavailable";
767 (void) mavlink_msg_log_erase_pack_chan(
770 sharedLink->mavlinkChannel(),
773 _vehicle->defaultComponentId()
776 if (!_vehicle->sendMessageOnLinkThreadSafe(sharedLink.get(), msg)) {
777 qCWarning(OnboardLogControllerLog) <<
"Failed to send";
787 qCWarning(OnboardLogControllerLog) <<
"Vehicle Unavailable";
791 if (_vehicleArmed()) {
792 qCWarning(OnboardLogControllerLog) <<
"ignored - vehicle is armed";
796 if (_transport != Transport::Ftp) {
797 qCWarning(OnboardLogControllerLog) <<
"ftp: selective erase requires the FTP transport";
801 _ftpDeleteQueue.clear();
802 const int numLogs = _logEntriesModel->count();
803 for (
int i = 0; i < numLogs; i++) {
806 _ftpDeleteQueue.enqueue(entry);
810 if (_ftpDeleteQueue.isEmpty()) {
811 qCWarning(OnboardLogControllerLog) <<
"ftp: no selected logs have FTP paths for erase";
815 qCDebug(OnboardLogControllerLog) <<
"ftp: erasing" << _ftpDeleteQueue.size() <<
"selected logs";
817 _setDownloading(
true);
821void OnboardLogController::_ftpDeleteNext()
823 if (_ftpDeleteQueue.isEmpty()) {
824 _ftpDeleting =
false;
825 _setDownloading(
false);
826 if (_vehicleArmed()) {
836 QGCOnboardLogEntry *
const entry = _ftpDeleteQueue.dequeue();
840 qCDebug(OnboardLogControllerLog) <<
"ftp: deleting" << entry->
ftpPath();
842 if (!_vehicle->ftpManager()->deleteFile(MAV_COMP_ID_AUTOPILOT1, entry->
ftpPath())) {
843 qCWarning(OnboardLogControllerLog) <<
"ftp: failed to start delete for" << entry->
ftpPath();
849void OnboardLogController::_ftpDeleteComplete(
const QString &file,
const QString &errorMsg)
855 if (!errorMsg.isEmpty()) {
856 qCWarning(OnboardLogControllerLog) <<
"ftp: delete error:" << file << errorMsg;
862void OnboardLogController::_requestLogList(uint32_t start, uint32_t end)
865 qCWarning(OnboardLogControllerLog) <<
"Vehicle Unavailable";
871 qCWarning(OnboardLogControllerLog) <<
"Link Unavailable";
876 (void) mavlink_msg_log_request_list_pack_chan(
879 sharedLink->mavlinkChannel(),
882 _vehicle->defaultComponentId(),
887 if (!_vehicle->sendMessageOnLinkThreadSafe(sharedLink.get(), msg)) {
888 qCWarning(OnboardLogControllerLog) <<
"Failed to send";
892 qCDebug(OnboardLogControllerLog) <<
"Request onboard log entry list (" << start <<
"through" << end <<
")";
894 _timer->start(kRequestLogListTimeoutMs);
897void OnboardLogController::_requestLogData(uint16_t
id, uint32_t offset, uint32_t count,
int retryCount)
900 qCWarning(OnboardLogControllerLog) <<
"Vehicle Unavailable";
906 qCWarning(OnboardLogControllerLog) <<
"Link Unavailable";
911 qCDebug(OnboardLogControllerLog) <<
"Request log data (id:" <<
id <<
"offset:" << offset <<
"size:" << count <<
"retryCount" << retryCount <<
")";
914 (void) mavlink_msg_log_request_data_pack_chan(
917 sharedLink->mavlinkChannel(),
920 _vehicle->defaultComponentId(),
926 if (!_vehicle->sendMessageOnLinkThreadSafe(sharedLink.get(), msg)) {
927 qCWarning(OnboardLogControllerLog) <<
"Failed to send";
931void OnboardLogController::_requestLogEnd()
934 qCWarning(OnboardLogControllerLog) <<
"Vehicle Unavailable";
940 qCWarning(OnboardLogControllerLog) <<
"Link Unavailable";
945 (void) mavlink_msg_log_request_end_pack_chan(
948 sharedLink->mavlinkChannel(),
951 _vehicle->defaultComponentId()
954 if (!_vehicle->sendMessageOnLinkThreadSafe(sharedLink.get(), msg)) {
955 qCWarning(OnboardLogControllerLog) <<
"Failed to send";
959void OnboardLogController::_setDownloading(
bool active)
961 if (_downloadingLogs != active) {
962 _downloadingLogs = active;
964 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(!active);
970void OnboardLogController::_setListing(
bool active)
972 if (_requestingLogEntries != active) {
973 _requestingLogEntries = active;
975 _vehicle->vehicleLinkManager()->setCommunicationLostEnabled(!active);
978 _sortEntriesByTimestamp();
984void OnboardLogController::_setTransport(
Transport transport)
992void OnboardLogController::_ftpStartListing()
994 _ftpDirsToList.clear();
995 _ftpLogIdCounter = 0;
997 _ftpTriedFallbackRoot =
false;
1003void OnboardLogController::_ftpListRoot()
1005 _ftpListState = FtpListState::ListingRoot;
1007 qCDebug(OnboardLogControllerLog) <<
"ftp: listing root" << _ftpLogRoot;
1009 if (!_vehicle->ftpManager()->listDirectory(MAV_COMP_ID_AUTOPILOT1, _ftpLogRoot)) {
1010 qCWarning(OnboardLogControllerLog) <<
"ftp: failed to start root listing for" << _ftpLogRoot;
1011 _ftpFallbackToMessages();
1015void OnboardLogController::_ftpListDirComplete(
const QStringList &dirList,
const QString &errorMsg)
1017 if (_ftpListState == FtpListState::Idle) {
1021 if (!errorMsg.isEmpty()) {
1022 if ((_ftpListState == FtpListState::ListingRoot) && !_ftpTriedFallbackRoot && _vehicle) {
1023 const char *fallback =
nullptr;
1024 if (_vehicle->px4Firmware()) {
1026 }
else if (_vehicle->apmFirmware()) {
1031 qCDebug(OnboardLogControllerLog) <<
"ftp: root listing of" << _ftpLogRoot <<
"failed (" << errorMsg
1032 <<
"), falling back to" << fallback;
1033 _ftpTriedFallbackRoot =
true;
1034 _ftpLogRoot = QString::fromLatin1(fallback);
1040 qCWarning(OnboardLogControllerLog) <<
"ftp: listing error:" << errorMsg;
1041 _ftpFallbackToMessages();
1046 qCDebug(OnboardLogControllerLog) <<
"ftp: raw entries for"
1047 << ((_ftpListState == FtpListState::ListingRoot) ? _ftpLogRoot : (_ftpDirsToList.isEmpty() ? QString() : _ftpDirsToList.first()))
1050 if (_ftpListState == FtpListState::ListingRoot) {
1053 const uint flatLogs = _ftpProcessFileEntries(dirList, QString());
1060 const bool withTime = _vehicle && !_vehicle->ftpManager()->listDirectoryWithTimeUnsupported();
1062 for (
const QString &entry : dirList) {
1063 if (entry.startsWith(QLatin1Char(
'D'))) {
1064 QString dirName = entry.mid(1);
1066 dirName = dirName.section(QLatin1Char(
'\t'), 0, 0);
1071 if (!dirName.isEmpty() &&
1072 (dirName != QStringLiteral(
".")) && (dirName != QStringLiteral(
".."))) {
1073 _ftpDirsToList.append(dirName);
1078 _ftpDirsToList.sort();
1079 qCDebug(OnboardLogControllerLog) <<
"ftp: root listing of" << _ftpLogRoot
1080 <<
"found" << flatLogs <<
"flat logs and" << _ftpDirsToList.size() <<
"subdirectories";
1082 _ftpListState = FtpListState::ListingSubdir;
1083 _ftpListNextSubdir();
1087 const QString currentDir = _ftpDirsToList.isEmpty() ? QString() : _ftpDirsToList.first();
1088 const uint logsFoundInDir = _ftpProcessFileEntries(dirList, currentDir);
1090 qCDebug(OnboardLogControllerLog) <<
"ftp:" << currentDir <<
"->" << logsFoundInDir <<
"logs";
1092 if (!_ftpDirsToList.isEmpty()) {
1093 _ftpDirsToList.removeFirst();
1096 _ftpListNextSubdir();
1099uint OnboardLogController::_ftpProcessFileEntries(
const QStringList &dirList,
const QString &subdir)
1101 const QDate dirDate = subdir.isEmpty() ? QDate() : QDate::fromString(subdir, QStringLiteral(
"yyyy-MM-dd"));
1104 for (
const QString &entry : dirList) {
1105 if (!entry.startsWith(QLatin1Char(
'F'))) {
1111 const QString fileInfo = entry.mid(1);
1112 const int tabIdx = fileInfo.indexOf(QLatin1Char(
'\t'));
1117 const QString fileName = fileInfo.left(tabIdx);
1118 const QString sizeStr = fileInfo.section(QLatin1Char(
'\t'), 1, 1);
1119 const QString mtimeStr = fileInfo.section(QLatin1Char(
'\t'), 2, 2);
1121 if (!fileName.endsWith(QStringLiteral(
".ulg"), Qt::CaseInsensitive) &&
1122 !fileName.endsWith(QStringLiteral(
".bin"), Qt::CaseInsensitive)) {
1126 bool sizeOk =
false;
1127 const uint fileSize = sizeStr.toUInt(&sizeOk);
1135 bool mtimeOk =
false;
1136 const qint64 mtimeSecs = mtimeStr.toLongLong(&mtimeOk);
1137 if (mtimeOk && (mtimeSecs > 0)) {
1138 dateTime = QDateTime::fromSecsSinceEpoch(mtimeSecs, QTimeZone::UTC);
1142 if (!dateTime.isValid() && dirDate.isValid()) {
1143 const QString baseName = fileName.left(fileName.lastIndexOf(QLatin1Char(
'.')));
1144 const QTime fileTime = QTime::fromString(baseName, QStringLiteral(
"HH_mm_ss"));
1145 if (fileTime.isValid()) {
1146 dateTime = QDateTime(dirDate, fileTime, QTimeZone::UTC);
1148 dateTime = QDateTime(dirDate, QTime(), QTimeZone::UTC);
1152 const QString ftpPath = subdir.isEmpty()
1153 ? (_ftpLogRoot + QStringLiteral(
"/") + fileName)
1154 : (_ftpLogRoot + QStringLiteral(
"/") + subdir + QStringLiteral(
"/") + fileName);
1156 QGCOnboardLogEntry *
const logEntry =
new QGCOnboardLogEntry(_ftpLogIdCounter++, dateTime, fileSize,
true);
1160 _logEntriesModel->append(logEntry);
1167void OnboardLogController::_ftpListNextSubdir()
1169 if (_ftpDirsToList.isEmpty()) {
1170 qCDebug(OnboardLogControllerLog) <<
"ftp: listing complete, found" << _logEntriesModel->count() <<
"logs";
1171 _ftpFinishListing();
1175 const QString subdir = _ftpDirsToList.first();
1176 const QString path = _ftpLogRoot + QStringLiteral(
"/") + subdir;
1178 qCDebug(OnboardLogControllerLog) <<
"ftp: listing subdir" << path;
1180 if (!_vehicle->ftpManager()->listDirectory(MAV_COMP_ID_AUTOPILOT1, path)) {
1181 qCWarning(OnboardLogControllerLog) <<
"ftp: failed to list subdir" << path;
1182 _ftpDirsToList.removeFirst();
1183 _ftpListNextSubdir();
1187void OnboardLogController::_ftpFinishListing()
1189 _ftpListState = FtpListState::Idle;
1193 if (_vehicle && _vehicle->ftpManager()->listDirectoryWithTimeUnsupported()) {
1194 _ftpFallbackToMessages();
1201void OnboardLogController::_ftpFallbackToMessages()
1203 qCDebug(OnboardLogControllerLog) <<
"ftp: transport failed, falling back to message based log download";
1205 _ftpListState = FtpListState::Idle;
1206 _ftpDirsToList.clear();
1207 _ftpDisabled =
true;
1208 _setTransport(Transport::Messages);
1210 _logEntriesModel->clearAndDeleteContents();
1214 _requestLogList(0, 0xffff);
1217void OnboardLogController::_ftpDownloadToDirectory(
const QString &dir)
1219 _downloadPath = dir;
1220 if (_downloadPath.isEmpty()) {
1224 if (!_downloadPath.endsWith(QDir::separator())) {
1225 _downloadPath += QDir::separator();
1228 _ftpDownloadQueue.clear();
1229 _ftpDownloadHadError =
false;
1230 const int numLogs = _logEntriesModel->count();
1231 for (
int i = 0; i < numLogs; i++) {
1232 QGCOnboardLogEntry *
const entry = _logEntriesModel->value<QGCOnboardLogEntry*>(i);
1235 _ftpDownloadQueue.enqueue(entry);
1239 if (_ftpDownloadQueue.isEmpty()) {
1240 qCWarning(OnboardLogControllerLog) <<
"ftp: no selected logs have FTP paths for download";
1244 qCDebug(OnboardLogControllerLog) <<
"ftp: queued" << _ftpDownloadQueue.size() <<
"logs for download to" << _downloadPath;
1245 _setDownloading(
true);
1247 _ftpDownloadEntry(_ftpDownloadQueue.dequeue());
1252 if (!entry || !_vehicle) {
1258 _ftpCurrentDownloadEntry = entry;
1259 _ftpDownloadBytesAtLastUpdate = 0;
1260 _ftpDownloadRateAvg = 0.;
1261 _ftpDownloadElapsed.start();
1266 QString localFilename = entry->
ftpPath().section(QLatin1Char(
'/'), -1);
1267 if (localFilename.isEmpty()) {
1268 localFilename = QStringLiteral(
"log_") + QString::number(entry->
id()) + QStringLiteral(
".ulg");
1271 if (QFile::exists(_downloadPath + localFilename)) {
1272 const int dotIdx = localFilename.lastIndexOf(QLatin1Char(
'.'));
1273 const QString
base = (dotIdx > 0) ? localFilename.left(dotIdx) : localFilename;
1274 const QString ext = (dotIdx > 0) ? localFilename.mid(dotIdx) : QString();
1278 localFilename =
base + QStringLiteral(
"_") + QString::number(numDups) + ext;
1279 }
while (QFile::exists(_downloadPath + localFilename));
1282 qCDebug(OnboardLogControllerLog) <<
"ftp: downloading" << entry->
ftpPath() <<
"to" << _downloadPath + localFilename;
1284 if (!_vehicle->ftpManager()->download(MAV_COMP_ID_AUTOPILOT1, entry->
ftpPath(), _downloadPath, localFilename,
true)) {
1285 qCWarning(OnboardLogControllerLog) <<
"ftp: failed to start download for" << entry->
ftpPath();
1287 _ftpCurrentDownloadEntry =
nullptr;
1288 _ftpDownloadHadError =
true;
1289 _ftpDownloadQueueNext();
1293void OnboardLogController::_ftpDownloadQueueNext()
1295 if (!_ftpDownloadQueue.isEmpty()) {
1296 _ftpDownloadEntry(_ftpDownloadQueue.dequeue());
1300 if (_ftpDownloadHadError) {
1301 qCDebug(OnboardLogControllerLog) <<
"ftp: download errors occurred, using message based transport for subsequent refreshes";
1302 _ftpDisabled =
true;
1305 _setDownloading(
false);
1308void OnboardLogController::_ftpDownloadComplete(
const QString &file,
const QString &errorMsg)
1310 if (!_ftpCurrentDownloadEntry) {
1314 if (errorMsg.isEmpty()) {
1315 _ftpCurrentDownloadEntry->setStatus(tr(
"Downloaded"));
1316 qCDebug(OnboardLogControllerLog) <<
"ftp: download complete" << file;
1318 _ftpCurrentDownloadEntry->setStatus(tr(
"Error"));
1319 _ftpDownloadHadError =
true;
1320 qCWarning(OnboardLogControllerLog) <<
"ftp: download error:" << errorMsg;
1323 _ftpCurrentDownloadEntry =
nullptr;
1324 _ftpDownloadQueueNext();
1327void OnboardLogController::_ftpDownloadProgress(
float value)
1329 if (!_ftpCurrentDownloadEntry) {
1333 if (_ftpDownloadElapsed.elapsed() < kGUIRateMs) {
1337 const size_t totalBytes =
static_cast<size_t>(
static_cast<qreal
>(_ftpCurrentDownloadEntry->size()) *
static_cast<qreal
>(value));
1338 if (totalBytes < _ftpDownloadBytesAtLastUpdate) {
1340 _ftpDownloadBytesAtLastUpdate = totalBytes;
1344 const size_t bytesSinceLastUpdate = totalBytes - _ftpDownloadBytesAtLastUpdate;
1345 const qreal elapsedSec = _ftpDownloadElapsed.elapsed() / 1000.0;
1346 const qreal rate = (elapsedSec > 0) ? (bytesSinceLastUpdate / elapsedSec) : 0;
1347 _ftpDownloadRateAvg = (_ftpDownloadRateAvg * 0.95) + (rate * 0.05);
1348 _ftpDownloadBytesAtLastUpdate = totalBytes;
1349 _ftpDownloadElapsed.start();
1351 const QString status = QStringLiteral(
"%1 (%2/s)").arg(
1355 _ftpCurrentDownloadEntry->setStatus(status);
1360 if (_compressLogs != compress) {
1361 _compressLogs = compress;
1369 qCWarning(OnboardLogControllerLog) <<
"Log compression not yet implemented (decompression-only API)";
1378void OnboardLogController::_handleCompressionProgress(qreal progress)
1384void OnboardLogController::_handleCompressionFinished(
bool success)
@ Transport
The link could not read, write, or change its baud rate.
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
static constexpr const char * kApmLogRootFallback
static constexpr const char * kPx4LogRootFallback
static constexpr const char * kMavlinkLogRoot
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void commandProgress(float value)
void downloadComplete(const QString &file, const QString &errorMsg)
void deleteComplete(const QString &file, const QString &errorMsg)
void listDirectoryComplete(const QStringList &dirList, const QString &errorMsg)
QVariant rawValue() const
static int getComponentId()
static MAVLinkProtocol * instance()
static MultiVehicleManager * instance()
void activeVehicleChanged(Vehicle *activeVehicle)
int selectedCount() const
void downloadingLogsChanged()
void requestingListChanged()
QString transport() const
Transport currently used to list/download logs: "messages" (LOG_* messages) or "ftp" (MAVLink FTP).
Q_INVOKABLE void selectAll(bool select)
Q_INVOKABLE void eraseAll()
Q_INVOKABLE void eraseSelected()
Q_INVOKABLE void download(const QString &path=QString())
OnboardLogController(QObject *parent=nullptr)
Q_INVOKABLE void toggleSortByDate()
Q_INVOKABLE void refresh()
bool allLogsSelected() const
void setCompressLogs(bool compress)
void sortAscendingChanged()
void setSortAscending(bool ascending)
void compressLogsChanged()
Q_INVOKABLE void cancel()
Q_INVOKABLE bool compressLogFile(const QString &logPath)
Compress a single log file.
Q_INVOKABLE void cancelCompression()
Cancel compression.
bool parameterExists(int componentId, const QString ¶mName) const
Fact * getParameter(int componentId, const QString ¶mName)
static constexpr int defaultComponentId
void setStatus(const QString &stat)
void setFtpPath(const QString &path)
void setSelected(bool sel)
void setTime(const QDateTime &date)
void setReceived(bool rec)
QString ftpPath() const
Remote path when the entry was listed via MAVLink FTP; empty for message-based entries.
int count() const override final
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
static SettingsManager * instance()
AppSettings * appSettings() const
void armedChanged(bool armed)
void logEntry(uint32_t time_utc, uint32_t size, uint16_t id, uint16_t num_logs, uint16_t last_log_num)
void logData(uint32_t ofs, uint16_t id, uint8_t count, const uint8_t *data)
QString bigSizeToString(quint64 size)
Byte size with unit: B, KB, MB, GB, TB. 1 fractional digit above 1 KB.
GPSCommandSequence base(QByteArrayView port, const GPSBaseStationConfig &base)
Streams RTCM3 corrections and the PVTGeodetic survey status over port in the mode base selects.
static const uint32_t kChunkSize