QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
FailureInjection.cc
Go to the documentation of this file.
1#include "FailureInjection.h"
2
3#include <QtCore/QMetaEnum>
4#include <QtCore/QVariantMap>
5
6#include "MAVLinkEnumsQml.h" // MAVLinkEnums::FAILURE_UNIT / FAILURE_TYPE, Q_ENUM_NS-reflected from the MAVLink dialect
7#include "MAVLinkLib.h" // MAV_RESULT_* for resolveResult()
8#include "QGCMAVLink.h" // QGCMAVLink::mavResultToString() fallback for resolveResult()
9#include "VehicleTypes.h" // MavCmdResultFailureCode_t
10
11namespace {
12
13QVariantMap makeUnit(const QString& name, int unit)
14{
15 return QVariantMap{{"name", name}, {"unit", unit}};
16}
17
18QVariantMap makeType(const QString& name, int type)
19{
20 return QVariantMap{{"name", name}, {"type", type}};
21}
22
24QString stripPrefix(const QString& key, const QStringList& prefixesLongestFirst)
25{
26 for (const QString& prefix : prefixesLongestFirst) {
27 if (key.startsWith(prefix)) {
28 return key.mid(prefix.length());
29 }
30 }
31 return key;
32}
33
36QVariantList buildCatalog(const char* enumName, const QStringList& prefixesLongestFirst,
37 QVariantMap (*makeEntry)(const QString&, int))
38{
39 QVariantList list;
40 const QMetaObject& enumsMetaObject = MAVLinkEnums::staticMetaObject;
41 const QMetaEnum me = enumsMetaObject.enumerator(enumsMetaObject.indexOfEnumerator(enumName));
42 for (int i = 0; i < me.keyCount(); ++i) {
43 const QString key = QString::fromLatin1(me.key(i));
44 if (key.endsWith(QStringLiteral("_ENUM_END"))) {
45 continue; // dialect sentinel, not a real value
46 }
47 list.append(makeEntry(stripPrefix(key, prefixesLongestFirst), me.value(i)));
48 }
49 return list;
50}
51
53QString failureCodeReason(int failureCode)
54{
55 switch (failureCode) {
57 return FailureInjection::tr("No response");
59 return FailureInjection::tr("Duplicate command");
60 default:
61 return QString();
62 }
63}
64
65} // namespace
66
68 : QObject(parent)
69{
70 _units = buildCatalog("FAILURE_UNIT",
71 {QStringLiteral("FAILURE_UNIT_SENSOR_"), QStringLiteral("FAILURE_UNIT_SYSTEM_"),
72 QStringLiteral("FAILURE_UNIT_DATALINK_"), QStringLiteral("FAILURE_UNIT_BUS_"),
73 QStringLiteral("FAILURE_UNIT_")},
74 makeUnit);
75 _types = buildCatalog("FAILURE_TYPE", {QStringLiteral("FAILURE_TYPE_")}, makeType);
76}
77
78void FailureInjection::logRow(const QString& unitName, const QString& typeName, const QString& instanceLabel,
79 const QString& time)
80{
81 _activity.prepend(QVariantMap{{"time", time},
82 {"unitName", unitName},
83 {"typeName", typeName},
84 {"instance", instanceLabel},
85 {"result", QStringLiteral("pending")}});
86 emit activityChanged();
87}
88
89void FailureInjection::logInjection(const QString& unitName, const QString& typeName, int unitEnum,
90 const QString& instanceLabel, const QString& time)
91{
92 logRow(unitName, typeName, instanceLabel, time);
93 if (!_injectedUnits.contains(unitEnum)) {
94 _injectedUnits.append(unitEnum);
95 }
96}
97
98void FailureInjection::resolveResult(int ackResult, int failureCode)
99{
100 // ACKs for MAV_CMD_INJECT_FAILURE arrive in send order; resolve the oldest pending row (highest index, since newest
101 // is prepended).
102 int pendingIndex = -1;
103 for (int i = _activity.size() - 1; i >= 0; --i) {
104 if (_activity.at(i).toMap().value(QStringLiteral("result")).toString() == QStringLiteral("pending")) {
105 pendingIndex = i;
106 break;
107 }
108 }
109 if (pendingIndex < 0) {
110 return;
111 }
112
113 QString reason = failureCodeReason(failureCode);
114 if (reason.isEmpty()) {
115 switch (ackResult) {
116 // Not tr()'d: FailureInjectionComponent.qml matches this exact literal to style the row green.
117 case MAV_RESULT_ACCEPTED:
118 reason = QStringLiteral("accepted");
119 break;
120 case MAV_RESULT_TEMPORARILY_REJECTED:
121 reason = tr("Temporarily rejected");
122 break;
123 case MAV_RESULT_DENIED:
124 reason = tr("Denied");
125 break;
126 case MAV_RESULT_UNSUPPORTED:
127 reason = tr("Unsupported");
128 break;
129 case MAV_RESULT_FAILED:
130 reason = tr("Failed");
131 break;
132 case MAV_RESULT_CANCELLED:
133 reason = tr("Cancelled");
134 break;
135 default:
136 reason = QGCMAVLink::mavResultToString(static_cast<uint8_t>(ackResult));
137 break;
138 }
139 }
140
141 QVariantMap row = _activity.at(pendingIndex).toMap();
142 row[QStringLiteral("result")] = reason;
143 _activity[pendingIndex] = row;
144 emit activityChanged();
145}
146
147QVariantList FailureInjection::injectedUnits(void) const
148{
149 QVariantList list;
150 for (int unitEnum : _injectedUnits) {
151 list.append(unitEnum);
152 }
153 return list;
154}
155
157{
158 _injectedUnits.removeAll(unitEnum);
159}
160
162{
163 bool changed = false;
164 for (int i = 0; i < _activity.size(); ++i) {
165 QVariantMap row = _activity.at(i).toMap();
166 if (row.value(QStringLiteral("result")).toString() == QStringLiteral("pending")) {
167 row[QStringLiteral("result")] = tr("Interrupted");
168 _activity[i] = row;
169 changed = true;
170 }
171 }
172 if (changed) {
173 emit activityChanged();
174 }
175}
176
178{
179 if ((vehicleId == 0) || (vehicleId == _currentVehicleId)) {
180 // No vehicle / transient disconnect, or the same vehicle (e.g. after a reboot) — keep the session.
181 return;
182 }
183 _currentVehicleId = vehicleId;
184 _injectedUnits.clear();
185 _activity.clear();
186 emit activityChanged();
187}
188
189QVariantList FailureInjection::detailParams(int unitEnum, int typeEnum) const
190{
191 // Vehicle parameters that refine how a failure manifests, keyed by (unit, type). The page shows
192 // an editor per entry, but only when the connected vehicle actually exposes the parameter.
193 // Adding a new combo is one more table line.
194 struct DetailParam
195 {
196 int unitEnum;
197 int typeEnum;
198 const char* param;
199 const char* label;
200 };
201
202 static const DetailParam table[] = {
203 {FAILURE_UNIT_SENSOR_GPS, FAILURE_TYPE_WRONG, "SYS_FAIL_GPS_WRG", QT_TR_NOOP("GPS fix type")},
204 {FAILURE_UNIT_SYSTEM_BATTERY, FAILURE_TYPE_WRONG, "SYS_FAIL_BAT_LVL", QT_TR_NOOP("Battery level")},
205 };
206
207 QVariantList list;
208 for (const DetailParam& entry : table) {
209 if (entry.unitEnum == unitEnum && entry.typeEnum == typeEnum) {
210 list.append(QVariantMap{{"param", QString::fromLatin1(entry.param)}, {"label", tr(entry.label)}});
211 }
212 }
213 return list;
214}
Q_INVOKABLE void resolvePendingInterrupted(void)
Q_INVOKABLE void notifyActiveVehicle(quint32 vehicleId)
void activityChanged(void)
Q_INVOKABLE void markUnitReset(int unitEnum)
Untrack one FAILURE_UNIT once its reset is accepted, so an interrupted Reset all keeps the rest retry...
FailureInjection(QObject *parent=nullptr)
< newest first: [{ time, unitName, typeName, instance, result }]
Q_INVOKABLE QVariantList detailParams(int unitEnum, int typeEnum) const
Q_INVOKABLE void logRow(const QString &unitName, const QString &typeName, const QString &instanceLabel, const QString &time)
Q_INVOKABLE void logInjection(const QString &unitName, const QString &typeName, int unitEnum, const QString &instanceLabel, const QString &time)
Record one injected failure: adds a log row and remembers the unit so Reset can restore it.
Q_INVOKABLE void resolveResult(int ackResult, int failureCode=0)
Q_INVOKABLE QVariantList injectedUnits(void) const
Distinct FAILURE_UNIT values injected this session, so Reset restores only those.
@ MavCmdResultFailureDuplicateCommand
Unable to send command since duplicate is already being waited on for response.
@ MavCmdResultFailureNoResponseToCommand
No response from vehicle to command.