QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MAVLinkLogManager.cc
Go to the documentation of this file.
1#include "MAVLinkLogManager.h"
2#include "QGCFileHelper.h"
5#include "SettingsManager.h"
6#include "AppSettings.h"
7#include "Vehicle.h"
8
9#include <QtCore/QDirIterator>
10#include <QtCore/QFile>
11#include <QtCore/QFileInfo>
12#include <QtCore/QSettings>
13#include <QtNetwork/QNetworkReply>
14
15#include "QGCNetworkHelper.h"
16
17QGC_LOGGING_CATEGORY(MAVLinkLogManagerLog, "Vehicle.MAVLinkLogManager")
18
19static constexpr const char *kSidecarExtension = ".uploaded";
20
21MAVLinkLogFiles::MAVLinkLogFiles(MAVLinkLogManager *manager, const QString &filePath, bool newFile)
22 : QObject(manager)
23{
24 // qCDebug(MAVLinkLogManagerLog) << Q_FUNC_INFO << this;
25
27
28 const QFileInfo fi(filePath);
29 _name = fi.baseName();
30 if (!newFile) {
31 _size = fi.size();
32 QString sideCar = filePath;
33 sideCar = sideCar.replace(manager->logExtension(), kSidecarExtension);
34 const QFileInfo sc(sideCar);
35 _uploaded = sc.exists();
36 }
37}
38
40{
41 // qCDebug(MAVLinkLogManagerLog) << Q_FUNC_INFO << this;
42}
43
45{
46 if (size != _size) {
47 _size = size;
48 emit sizeChanged();
49 }
50}
51
53{
54 if (selected != _selected) {
55 _selected = selected;
56 emit selectedChanged();
57 }
58}
59
61{
62 if (uploading != _uploading) {
63 _uploading = uploading;
64 emit uploadingChanged();
65 }
66}
67
69{
70 if (progress != _progress) {
71 _progress = progress;
72 emit progressChanged();
73 }
74}
75
77{
78 if (writing != _writing) {
79 _writing = writing;
80 emit writingChanged();
81 }
82}
83
85{
86 if (uploaded != _uploaded) {
87 _uploaded = uploaded;
88 emit uploadedChanged();
89 }
90}
91
92/*===========================================================================*/
93
95{
96 // qCDebug(MAVLinkLogManagerLog) << Q_FUNC_INFO << this;
97}
98
100{
101 close();
102
103 // qCDebug(MAVLinkLogManagerLog) << Q_FUNC_INFO << this;
104}
105
107{
108 if (_file.isOpen()) {
109 _file.close();
110 }
111}
112
113bool MAVLinkLogProcessor::create(MAVLinkLogManager* manager, QStringView path, quint32 id)
114{
115 _fileName =
116 _fileName.asprintf("%s/%03u-%s%s", path.toLatin1().constData(), id,
117 QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss-zzz").toLocal8Bit().constData(),
118 manager->logExtension().toLocal8Bit().constData());
119
120 _file.setFileName(_fileName);
121 if (!_file.open(QIODevice::WriteOnly)) {
122 qCWarning(MAVLinkLogManagerLog) << "Failed to open file for writing:" << _file.errorString();
123 return false;
124 }
125
126 _record = new MAVLinkLogFiles(manager, _fileName, true);
127 _record->setWriting(true);
128 _sequence = -1;
129
130 return true;
131}
132
133bool MAVLinkLogProcessor::_checkSequence(uint16_t seq, int &num_drops)
134{
135 num_drops = 0;
136 //-- Check if a sequence is newer than the one previously received and if
137 // there were dropped messages between the last one and this.
138 if (_sequence == -1) {
139 _sequence = seq;
140 return true;
141 }
142
143 if (static_cast<uint16_t>(_sequence) == seq) {
144 return false;
145 }
146
147 if (seq > static_cast<uint16_t>(_sequence)) {
148 // Account for wrap-arounds, sequence is 2 bytes
149 if ((seq - _sequence) > kSequenceSize) { // Assume reordered
150 return false;
151 }
152
153 num_drops = seq - _sequence - 1;
154 _numDrops += num_drops;
155 _sequence = seq;
156 return true;
157 }
158
159 if ((_sequence - seq) > kSequenceSize) {
160 num_drops = (1 << 16) - _sequence - 1 + seq;
161 _numDrops += num_drops;
162 _sequence = seq;
163 return true;
164 }
165
166 return false;
167}
168
169void MAVLinkLogProcessor::_writeData(const void *data, int len)
170{
171 if (_error) {
172 return;
173 }
174
175 const qint64 bytesWritten = _file.write(reinterpret_cast<const char*>(data), len);
176 if (bytesWritten != len) {
177 _error = true;
178 qCDebug(MAVLinkLogManagerLog) << "File IO error:" << len << "bytes into" << _fileName;
179 return;
180 }
181
182 _written += len;
183 if (_record) {
184 _record->setSize(_written);
185 }
186}
187
188QByteArray MAVLinkLogProcessor::_writeUlogMessage(QByteArray &data)
189{
190 // Write ulog data w/o integrity checking, assuming data starts with a
191 // valid ulog message. returns the remaining data at the end.
192 while (data.length() > 2) {
193 // ULog message header: little-endian uint16 payload size, then a uint8 type.
194 const int message_length =
195 static_cast<uint8_t>(data.at(0)) + (static_cast<uint8_t>(data.at(1)) * 256) + kUlogMessageHeader;
196 if (message_length > data.length()) {
197 break;
198 }
199
200 _writeData(data.constData(), message_length);
201 (void) data.remove(0, message_length);
202 }
203
204 return data;
205}
206
207bool MAVLinkLogProcessor::processStreamData(uint16_t sequence, uint8_t first_message, const QByteArray &in)
208{
209 int num_drops = 0;
210 _error = false;
211
212 QByteArray data(in);
213 while (_checkSequence(sequence, num_drops)) {
214 if (!_gotHeader) {
215 if (data.size() < 16) {
216 qCWarning(MAVLinkLogManagerLog) << "Corrupt log header. Canceling log download.";
217 return false;
218 }
219
220 _writeData(data.constData(), 16);
221 (void) data.remove(0, 16);
222 _gotHeader = true;
223 // What about data start offset now that we removed 16 bytes off the start?
224 }
225
226 if (_gotHeader && (num_drops > 0)) {
227 if (num_drops > 25) {
228 num_drops = 25;
229 }
230
231 // Write a dropout message. We don't really know the actual duration,
232 // so just use the number of drops * 10 ms
233 const uint8_t duration = static_cast<uint8_t>(num_drops) * 10;
234 const uint8_t bogus[] = {2, 0, 79, duration, 0};
235 _writeData(bogus, sizeof(bogus));
236 }
237
238 if (num_drops > 0) {
239 (void) _writeUlogMessage(_ulogMessage);
240 _ulogMessage.clear();
241
242 if (first_message == 255) {
243 break;
244 }
245
246 if (first_message > 0) {
247 (void) data.remove(0, first_message);
248 first_message = 0;
249 }
250 }
251
252 if ((first_message == 255) && (!_ulogMessage.isEmpty())) {
253 (void) _ulogMessage.append(data);
254 break;
255 }
256
257 if (_ulogMessage.length()) {
258 _writeData(_ulogMessage.constData(), _ulogMessage.length());
259 if (first_message) {
260 _writeData(data.left(first_message).constData(), first_message);
261 }
262 _ulogMessage.clear();
263 }
264
265 if (first_message) {
266 (void) data.remove(0, first_message);
267 }
268
269 _ulogMessage = _writeUlogMessage(data);
270 break;
271 }
272
273 return !_error;
274}
275
276/*===========================================================================*/
277
279 : QObject(parent)
280 , _vehicle(vehicle)
281 , _networkManager(new QNetworkAccessManager(this))
282 , _logFiles(new QmlObjectListModel(this))
283 , _ulogExtension(QStringLiteral(".") + SettingsManager::instance()->appSettings()->logFileExtension)
284 , _logPath(SettingsManager::instance()->appSettings()->logSavePath())
285{
286 qCDebug(MAVLinkLogManagerLog) << this;
287
288 QGCNetworkHelper::configureProxy(_networkManager);
289
290 QSettings settings;
291 settings.beginGroup(kMAVLinkLogGroup);
292
293 setEmailAddress(settings.value(kEmailAddressKey, QString()).toString());
294 setDescription(settings.value(kDescriptionsKey, QString(kDefaultDescr)).toString());
295 setUploadURL(settings.value(kPx4URLKey, QString(kDefaultPx4URL)).toString());
296 setVideoURL(settings.value(kVideoURLKey, QString()).toString());
297 setEnableAutoUpload(settings.value(kEnableAutoUploadKey, true).toBool());
298 setEnableAutoStart(settings.value(kEnableAutoStartKey, false).toBool());
299 setDeleteAfterUpload(settings.value(kEnableDeletetKey, false).toBool());
300 setWindSpeed(settings.value(kWindSpeedKey, -1).toInt());
301 setRating(settings.value(kRateKey, "notset").toString());
302 setPublicLog(settings.value(kPublicLogKey, true).toBool());
303
304 settings.endGroup();
305
307 qCWarning(MAVLinkLogManagerLog) << "Could not create MAVLink log download path:" << _logPath;
308 _loggingDisabled = true;
309 }
310
311 if (!_loggingDisabled) {
312 const QString filter = "*" + _ulogExtension;
313 QDirIterator it(_logPath, QStringList() << filter, QDir::Files);
314 while (it.hasNext()) {
315 _insertNewLog(new MAVLinkLogFiles(this, it.next()));
316 }
317
318 qCDebug(MAVLinkLogManagerLog) << "MAVLink logs directory:" << _logPath;
319 if (_vehicle->px4Firmware()) {
320 _loggingDenied = false;
321 (void) connect(_vehicle, &Vehicle::armedChanged, this, &MAVLinkLogManager::_armedChanged);
322 (void) connect(_vehicle, &Vehicle::mavlinkLogData, this, &MAVLinkLogManager::_mavlinkLogData);
323 (void) connect(_vehicle, &Vehicle::mavCommandResult, this, &MAVLinkLogManager::_mavCommandResult);
324 emit canStartLogChanged();
325 }
326 }
327}
328
330{
331 _logFiles->clearAndDeleteContents();
332
333 qCDebug(MAVLinkLogManagerLog) << this;
334}
335
336void MAVLinkLogManager::setEmailAddress(const QString &email)
337{
338 if (email != _emailAddress) {
339 _emailAddress = email;
340 QSettings settings;
341 settings.beginGroup(kMAVLinkLogGroup);
342 settings.setValue(kEmailAddressKey, email);
343 emit emailAddressChanged();
344 }
345}
346
348{
349 if (description != _description) {
350 _description = description;
351 QSettings settings;
352 settings.beginGroup(kMAVLinkLogGroup);
353 settings.setValue(kDescriptionsKey, description);
354 emit descriptionChanged();
355 }
356}
357
358void MAVLinkLogManager::setUploadURL(const QString &url)
359{
360 if (url != _uploadURL) {
361 _uploadURL = url.isEmpty() ? kDefaultPx4URL : url;
362
363 QSettings settings;
364 settings.beginGroup(kMAVLinkLogGroup);
365 settings.setValue(kPx4URLKey, _uploadURL);
366 emit uploadURLChanged();
367 }
368}
369
370void MAVLinkLogManager::setFeedback(const QString &fb)
371{
372 if (fb != _feedback) {
373 _feedback = fb;
374 emit feedbackChanged();
375 }
376}
377
378void MAVLinkLogManager::setVideoURL(const QString &url)
379{
380 if (url != _videoURL) {
381 _videoURL = url;
382 QSettings settings;
383 settings.beginGroup(kMAVLinkLogGroup);
384 settings.setValue(kVideoURLKey, url);
385 emit videoURLChanged();
386 }
387}
388
390{
391 if (enable != _enableAutoUpload) {
392 _enableAutoUpload = enable;
393 QSettings settings;
394 settings.beginGroup(kMAVLinkLogGroup);
395 settings.setValue(kEnableAutoUploadKey, enable);
397 }
398}
399
401{
402 if (enable != _enableAutoStart) {
403 _enableAutoStart = enable;
404 QSettings settings;
405 settings.beginGroup(kMAVLinkLogGroup);
406 settings.setValue(kEnableAutoStartKey, enable);
408 }
409}
410
412{
413 if (enable != _deleteAfterUpload) {
414 _deleteAfterUpload = enable;
415 QSettings settings;
416 settings.beginGroup(kMAVLinkLogGroup);
417 settings.setValue(kEnableDeletetKey, enable);
419 }
420}
421
423{
424 if (speed != _windSpeed) {
425 _windSpeed = speed;
426 QSettings settings;
427 settings.beginGroup(kMAVLinkLogGroup);
428 settings.setValue(kWindSpeedKey, speed);
429 emit windSpeedChanged();
430 }
431}
432
433void MAVLinkLogManager::setRating(const QString &rate)
434{
435 if (rate != _rating) {
436 _rating = rate;
437 QSettings settings;
438 settings.beginGroup(kMAVLinkLogGroup);
439 settings.setValue(kRateKey, rate);
440 emit ratingChanged();
441 }
442}
443
445{
446 if (pub != _publicLog) {
447 _publicLog = pub;
448 QSettings settings;
449 settings.beginGroup(kMAVLinkLogGroup);
450 settings.setValue(kPublicLogKey, pub);
451 emit publicLogChanged();
452 }
453}
454
456{
457 if (_currentLogfile) {
458 _currentLogfile->setUploading(false);
459 }
460
461 for (int i = 0; i < _logFiles->count(); i++) {
462 _currentLogfile = qobject_cast<MAVLinkLogFiles*>(_logFiles->get(i));
463 if (!_currentLogfile) {
464 qCWarning(MAVLinkLogManagerLog) << "Internal error";
465 continue;
466 }
467
468 if (!_currentLogfile->selected()) {
469 continue;
470
471 }
472
473 _currentLogfile->setSelected(false);
474 if (_currentLogfile->uploaded() || _emailAddress.isEmpty() || _uploadURL.isEmpty()) {
475 continue;
476 }
477
478 _currentLogfile->setUploading(true);
479 _currentLogfile->setProgress(0.0);
480 const QString filePath = _makeFilename(_currentLogfile->name());
481 (void) _sendLog(filePath);
482 emit uploadingChanged();
483 return;
484 }
485
486 _currentLogfile = nullptr;
487 emit uploadingChanged();
488}
489
490void MAVLinkLogManager::_insertNewLog(MAVLinkLogFiles *newLog)
491{
492 const int count = _logFiles->count();
493 if (!count) {
494 _logFiles->append(newLog);
495 return;
496 }
497
498 for (int i = 0; i < count; i++) {
499 const MAVLinkLogFiles *const f = qobject_cast<const MAVLinkLogFiles*>(_logFiles->get(i));
500 if (newLog->name() < f->name()) {
501 (void) _logFiles->insert(i, newLog);
502 return;
503 }
504 }
505
506 _logFiles->append(newLog);
507}
508
509int MAVLinkLogManager::_getFirstSelected() const
510{
511 for (int i = 0; i < _logFiles->count(); i++) {
512 const MAVLinkLogFiles *const f = qobject_cast<const MAVLinkLogFiles*>(_logFiles->get(i));
513 if (!f) {
514 qCWarning(MAVLinkLogManagerLog) << "Internal error";
515 continue;
516 }
517
518 if (f->selected()) {
519 return i;
520 }
521 }
522
523 return -1;
524}
525
527{
528 while (true) {
529 const int idx = _getFirstSelected();
530 if (idx < 0) {
531 break;
532 }
533
534 MAVLinkLogFiles *const log = qobject_cast<MAVLinkLogFiles*>(_logFiles->get(idx));
535 _deleteLog(log);
536 }
537}
538
539void MAVLinkLogManager::_deleteLog(MAVLinkLogFiles *log)
540{
541 QString filePath = _makeFilename(log->name());
542 QFile gone(filePath);
543 if (!gone.remove()) {
544 qCWarning(MAVLinkLogManagerLog) << "Could not delete MAVLink log file:" << _logPath;
545 }
546
547 filePath.replace(_ulogExtension, kSidecarExtension);
548 QFile sgone(filePath);
549 if (sgone.exists()) {
550 (void) sgone.remove();
551 }
552
553 (void) _logFiles->removeOne(log);
554 delete log;
555
556 emit logFilesChanged();
557}
558
560{
561 for (int i = 0; i < _logFiles->count(); i++) {
562 MAVLinkLogFiles *const pLogFile = qobject_cast<MAVLinkLogFiles*>(_logFiles->get(i));
563 if (!pLogFile) {
564 qCWarning(MAVLinkLogManagerLog) << "Internal error";
565 continue;
566 }
567
568 if (pLogFile->selected() && (pLogFile != _currentLogfile)) {
569 pLogFile->setSelected(false);
570 }
571 }
572
573 if (_currentLogfile) {
574 emit abortUpload();
575 }
576}
577
579{
580 AppSettings *const appSettings = SettingsManager::instance()->appSettings();
581 if (appSettings->disableAllPersistence()->rawValue().toBool()) {
582 return;
583 }
584
585 if (!_vehicle || !_vehicle->px4Firmware() || _loggingDenied) {
586 return;
587 }
588
589 if (!_createNewLog()) {
590 return;
591 }
592
593 _vehicle->startMavlinkLog();
594 _logRunning = true;
595 emit logRunningChanged();
596}
597
599{
600 if (_vehicle && _vehicle->px4Firmware()) {
601 _vehicle->stopMavlinkLog();
602 }
603
604 if (!_logProcessor) {
605 return;
606 }
607
608 _logProcessor->close();
609 if (_logProcessor->record()) {
610 _logProcessor->record()->setWriting(false);
611 if (_enableAutoUpload) {
612 _logProcessor->record()->setSelected(true);
613 if (!uploading()) {
614 uploadLog();
615 }
616 }
617 }
618
619 delete _logProcessor;
620 _logProcessor = nullptr;
621 _logRunning = false;
622 emit logRunningChanged();
623}
624
625QHttpPart MAVLinkLogManager::_createFormPart(QStringView name, QStringView value)
626{
627 QHttpPart formPart;
628 formPart.setHeader(QNetworkRequest::ContentDispositionHeader, QStringLiteral("form-data; name=\"%1\"").arg(name));
629 formPart.setBody(value.toUtf8());
630 return formPart;
631}
632
633bool MAVLinkLogManager::_sendLog(const QString &logFile)
634{
635 QString defaultDescription = _description;
636 if (_description.isEmpty()) {
637 qCWarning(MAVLinkLogManagerLog) << "Log description missing. Using defaults.";
638 defaultDescription = kDefaultDescr;
639 }
640
641 if (_emailAddress.isEmpty()) {
642 qCWarning(MAVLinkLogManagerLog) << "User email missing.";
643 return false;
644 }
645
646 if (_uploadURL.isEmpty()) {
647 qCWarning(MAVLinkLogManagerLog) << "Upload URL missing.";
648 return false;
649 }
650
651 const QFileInfo fi(logFile);
652 if (!fi.exists()) {
653 qCWarning(MAVLinkLogManagerLog) << "Log file missing:" << logFile;
654 return false;
655 }
656
657 QFile *file = new QFile(logFile, this);
658 if (!file || !file->open(QIODevice::ReadOnly)) {
659 delete file;
660 file = nullptr;
661 qCWarning(MAVLinkLogManagerLog) << "Could not open log file:" << logFile;
662 return false;
663 }
664
665 QHttpMultiPart *const multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
666 const QHttpPart emailPart = _createFormPart(u"email", _emailAddress);
667 const QHttpPart descriptionPart = _createFormPart(u"description", _description);
668 const QHttpPart sourcePart = _createFormPart(u"source", u"QGroundControl");
669 const QHttpPart versionPart = _createFormPart(u"version", QCoreApplication::applicationVersion());
670 const QHttpPart typePart = _createFormPart(u"type", u"flightreport");
671 const QHttpPart windPart = _createFormPart(u"windSpeed", QString::number(_windSpeed));
672 const QHttpPart ratingPart = _createFormPart(u"rating", _rating);
673 const QHttpPart publicPart = _createFormPart(u"public", _publicLog ? u"true" : u"false");
674
675 multiPart->append(emailPart);
676 multiPart->append(descriptionPart);
677 multiPart->append(sourcePart);
678 multiPart->append(versionPart);
679 multiPart->append(typePart);
680 multiPart->append(windPart);
681 multiPart->append(ratingPart);
682 multiPart->append(publicPart);
683
684 QHttpPart feedbackPart;
685 if (_feedback.isEmpty()) {
686 feedbackPart = _createFormPart(QString(kFeedback), u"None Given");
687 } else {
688 feedbackPart = _createFormPart(QString(kFeedback), _feedback);
689 }
690 multiPart->append(feedbackPart);
691
692 QHttpPart videoPart;
693 if(_videoURL.isEmpty()) {
694 videoPart = _createFormPart(QString(kVideoURL), u"None");
695 } else {
696 videoPart = _createFormPart(QString(kVideoURL), _videoURL);
697 }
698 multiPart->append(videoPart);
699
700 QHttpPart logPart;
701 logPart.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
702 logPart.setHeader(QNetworkRequest::ContentDispositionHeader, QStringLiteral("form-data; name=\"filearg\"; filename=\"%1\"").arg(fi.fileName()));
703 logPart.setBodyDevice(file);
704 multiPart->append(logPart);
705 file->setParent(multiPart);
706
707 QNetworkRequest request(QUrl::fromUserInput(_uploadURL));
708 request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, true);
709 QNetworkReply *const reply = _networkManager->post(request, multiPart);
710 (void) connect(this, &MAVLinkLogManager::abortUpload, reply, &QNetworkReply::abort);
711 (void) connect(reply, &QNetworkReply::finished, this, &MAVLinkLogManager::_uploadFinished);
712 (void) connect(reply, &QNetworkReply::uploadProgress, this, &MAVLinkLogManager::_uploadProgress);
713#ifdef QT_DEBUG
714 if (MAVLinkLogManagerLog().isDebugEnabled()) {
715 (void) connect(reply, &QNetworkReply::readyRead, this, &MAVLinkLogManager::_dataAvailable);
716 }
717#endif
718
719 multiPart->setParent(reply);
720 qCDebug(MAVLinkLogManagerLog) << "Log" << fi.baseName() << "Uploading." << fi.size() << "bytes.";
721
722 return true;
723}
724
725bool MAVLinkLogManager::_processUploadResponse(int http_code, const QByteArray &data)
726{
727 qCDebug(MAVLinkLogManagerLog) << "Uploaded response:" << QString::fromUtf8(data);
728 emit readyRead(data);
729
730 return (http_code == 200);
731}
732
733void MAVLinkLogManager::_dataAvailable()
734{
735 QNetworkReply *const reply = qobject_cast<QNetworkReply*>(sender());
736 if (!reply) {
737 return;
738 }
739
740 const QByteArray data = reply->readAll();
741 qCDebug(MAVLinkLogManagerLog) << "Uploaded response data:" << QString::fromUtf8(data);
742}
743
744void MAVLinkLogManager::_uploadFinished()
745{
746 QNetworkReply *const reply = qobject_cast<QNetworkReply*>(sender());
747 if (!reply) {
748 return;
749 }
750
751 const int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
752 const QByteArray data = reply->readAll();
753
754 if (_processUploadResponse(http_code, data)) {
755 qCDebug(MAVLinkLogManagerLog) << "Log uploaded.";
756 emit succeed();
757 if (_deleteAfterUpload) {
758 if (_currentLogfile) {
759 _deleteLog(_currentLogfile);
760 _currentLogfile = nullptr;
761 }
762 } else if (_currentLogfile) {
763 _currentLogfile->setUploaded(true);
764 QString sideCar = _makeFilename(_currentLogfile->name());
765 (void) sideCar.replace(_ulogExtension, kSidecarExtension);
766
767 QFile file(sideCar.toLatin1().constData());
768 if (file.open(QIODevice::WriteOnly)) {
769 file.close();
770 }
771 }
772 } else {
773 qCWarning(MAVLinkLogManagerLog) << QString("Log Upload Error: %1 status: %2").arg(reply->errorString(), reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString());
774 emit failed();
775 }
776
777 reply->deleteLater();
778 uploadLog();
779}
780
781void MAVLinkLogManager::_uploadProgress(qint64 bytesSent, qint64 bytesTotal)
782{
783 if (bytesTotal) {
784 const qreal progress = static_cast<qreal>(bytesSent) / static_cast<qreal>(bytesTotal);
785 if (_currentLogfile) {
786 _currentLogfile->setProgress(progress);
787 }
788 }
789
790 qCDebug(MAVLinkLogManagerLog) << bytesSent << "of" << bytesTotal;
791}
792
793void MAVLinkLogManager::_mavlinkLogData(Vehicle* /*vehicle*/, quint32 /*target_system*/, uint8_t /*target_component*/,
794 uint16_t sequence, uint8_t first_message, const QByteArray& data,
795 bool /*acked*/)
796{
797 if (!_logProcessor || !_logProcessor->valid()) {
798 qCDebug(MAVLinkLogManagerLog) << "MAVLink log data received when not expected.";
799 return;
800 }
801
802 if (_logProcessor->processStreamData(sequence, first_message, data)) {
803 return;
804 }
805
806 qCWarning(MAVLinkLogManagerLog) << "Error writing MAVLink log file:" << _logProcessor->fileName();
807 delete _logProcessor;
808 _logProcessor = nullptr;
809 _logRunning = false;
810 _vehicle->stopMavlinkLog();
811 emit logRunningChanged();
812}
813
814void MAVLinkLogManager::_mavCommandResult(quint32 vehicleId, int component, int command, int result, int failureCode)
815{
816 Q_UNUSED(vehicleId); Q_UNUSED(component); Q_UNUSED(failureCode)
817
818 if ((command != MAV_CMD_LOGGING_START) && (command != MAV_CMD_LOGGING_STOP)) {
819 return;
820 }
821
822 if (result == MAV_RESULT_ACCEPTED) {
823 return;
824 }
825
826 if (command == MAV_CMD_LOGGING_STOP) {
827 qCWarning(MAVLinkLogManagerLog) << "Stop MAVLink log command failed.";
828 return;
829 }
830
831 if (result == MAV_RESULT_DENIED) {
832 _loggingDenied = true;
833 qCWarning(MAVLinkLogManagerLog) << "Start MAVLink log command denied.";
834 } else {
835 qCWarning(MAVLinkLogManagerLog) << "Start MAVLink log command failed:" << result;
836 }
837
838 _discardLog();
839}
840
841void MAVLinkLogManager::_discardLog()
842{
843 if (_logProcessor) {
844 _logProcessor->close();
845 if (_logProcessor->record()) {
846 _deleteLog(_logProcessor->record());
847 }
848 delete _logProcessor;
849 _logProcessor = nullptr;
850 }
851
852 _logRunning = false;
853 emit logRunningChanged();
854}
855
856bool MAVLinkLogManager::_createNewLog()
857{
858 delete _logProcessor;
859 _logProcessor = new MAVLinkLogProcessor();
860
861 if (_logProcessor->create(this, _logPath, _vehicle->id())) {
862 _insertNewLog(_logProcessor->record());
863 emit logFilesChanged();
864 } else {
865 qCWarning(MAVLinkLogManagerLog) << "Could not create MAVLink log file:" << _logProcessor->fileName();
866 delete _logProcessor;
867 _logProcessor = nullptr;
868 }
869
870 return (_logProcessor != nullptr);
871}
872
873void MAVLinkLogManager::_armedChanged(bool armed)
874{
875 if (!_vehicle || !_vehicle->px4Firmware()) {
876 return;
877 }
878
879 if (armed) {
880 if (_enableAutoStart) {
881 startLogging();
882 }
883 return;
884 }
885
886 if (_logRunning && _enableAutoStart) {
887 stopLogging();
888 }
889}
890
891QString MAVLinkLogManager::_makeFilename(const QString &baseName) const
892{
893 QString filePath = _logPath;
894 filePath += "/";
895 filePath += baseName;
896 filePath += _ulogExtension;
897 return filePath;
898}
static constexpr const char * kSidecarExtension
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Application Settings.
Definition AppSettings.h:10
void setSize(quint32 size)
void uploadingChanged()
void setUploading(bool uploading)
void writingChanged()
bool writing() const
void setSelected(bool selected)
void selectedChanged()
bool selected() const
void setProgress(qreal progress)
bool uploading() const
qreal progress() const
void progressChanged()
bool uploaded() const
void setWriting(bool writing)
quint32 size() const
QString name() const
void uploadedChanged()
MAVLinkLogFiles(MAVLinkLogManager *manager, const QString &filePath, bool newFile=false)
void setUploaded(bool uploaded)
void logRunningChanged()
void setDeleteAfterUpload(bool enable)
Q_INVOKABLE void stopLogging()
void enableAutoUploadChanged()
Q_INVOKABLE void uploadLog()
void canStartLogChanged()
void selectedCountChanged()
void setEmailAddress(const QString &email)
void setDescription(const QString &description)
MAVLinkLogManager(Vehicle *vehicle, QObject *parent=nullptr)
void setPublicLog(bool publicLog)
Q_INVOKABLE void deleteLog()
void setEnableAutoUpload(bool enable)
void setWindSpeed(int speed)
void readyRead(const QByteArray &data)
QString description() const
void setRating(const QString &rate)
void setVideoURL(const QString &url)
~MAVLinkLogManager()
Destructor for the MAVLinkLogManager class.
void emailAddressChanged()
void descriptionChanged()
void deleteAfterUploadChanged()
Q_INVOKABLE void cancelUpload()
QString logExtension() const
void enableAutoStartChanged()
void setFeedback(const QString &feedback)
Q_INVOKABLE void startLogging()
void setEnableAutoStart(bool enable)
void setUploadURL(const QString &url)
bool create(MAVLinkLogManager *manager, QStringView path, quint32 id)
bool processStreamData(uint16_t _sequence, uint8_t first_message, const QByteArray &in)
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
int count() const override final
Provides access to all app settings.
static SettingsManager * instance()
AppSettings * appSettings() const
void armedChanged(bool armed)
void mavlinkLogData(Vehicle *vehicle, quint32 target_system, uint8_t target_component, uint16_t sequence, uint8_t first_message, QByteArray data, bool acked)
void mavCommandResult(quint32 vehicleId, int targetComponent, int command, int ackResult, int failureCode)
bool ensureDirectoryExists(const QString &path)
void configureProxy(QNetworkAccessManager *manager)
Set up default proxy configuration on a network manager.