QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StatusTextHandler.cc
Go to the documentation of this file.
1#include "StatusTextHandler.h"
2#include "MAVLinkLib.h"
4
5#include <QtCore/QTimer>
6#include <QtCore/QDateTime>
7
8QGC_LOGGING_CATEGORY(StatusTextHandlerLog, "MAVLink.StatusTextHandler")
9
10namespace {
11
12// Placeholders inside the formatted text; _displayText() turns them into <b> for unread messages
13const QString kUnreadStart = QStringLiteral("<#U>");
14const QString kUnreadEnd = QStringLiteral("</#U>");
15
16// Raw bytes up to the first NUL. A NUL anywhere in the field marks the last chunk of a chunked sequence.
17QByteArray statusTextBytes(const mavlink_statustext_t& statusText, bool& includesNullTerminator)
18{
19 constexpr int textLength = MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN;
20 const char* const textEnd = static_cast<const char*>(memchr(statusText.text, '\0', textLength));
21 includesNullTerminator = (textEnd != nullptr);
22 return QByteArray(statusText.text, includesNullTerminator ? (textEnd - statusText.text) : textLength);
23}
24
25// resize() default-constructs the slot in place; append(std::nullopt) trips GCC 13 -Wmaybe-uninitialized
26void appendMissingChunk(QList<std::optional<QByteArray>>& chunks)
27{
28 chunks.resize(chunks.size() + 1);
29}
30
31} // namespace
32
33StatusText::StatusText(MAV_COMPONENT componentid, MAV_SEVERITY severity, const QString &text)
34 : m_compId(componentid)
35 , m_severity(severity)
36 , m_text(text)
37{
38 // qCDebug(StatusTextHandlerLog) << Q_FUNC_INFO << this;
39}
40
42{
43 switch (m_severity) {
44 case MAV_SEVERITY_EMERGENCY:
45 case MAV_SEVERITY_ALERT:
46 case MAV_SEVERITY_CRITICAL:
47 case MAV_SEVERITY_ERROR:
48 return true;
49
50 default:
51 return false;
52 }
53}
54
56{
57 switch (m_severity) {
58 case MAV_SEVERITY_WARNING:
59 case MAV_SEVERITY_NOTICE:
60 return true;
61
62 default:
63 return false;
64 }
65}
66
68 : QObject(parent)
69 , m_chunkedStatusTextTimer(new QTimer(this))
70{
71 // qCDebug(StatusTextHandlerLog) << Q_FUNC_INFO << this;
72
73 m_chunkedStatusTextTimer->setSingleShot(true);
74 m_chunkedStatusTextTimer->setInterval(1000);
75 (void) connect(m_chunkedStatusTextTimer, &QTimer::timeout, this, &StatusTextHandler::_chunkedStatusTextTimeout);
76}
77
79{
81
82 // qCDebug(StatusTextHandlerLog) << Q_FUNC_INFO << this;
83}
84
86{
87 // Warning: There is a bug in mavlink which causes mavlink_msg_statustext_get_text to work incorrect.
88 // It ends up copying crap off the end of the buffer, so don't use it for now.
89
90 mavlink_statustext_t statusText;
91 mavlink_msg_statustext_decode(&message, &statusText);
92
93 bool includesNullTerminator;
94 return QString::fromUtf8(statusTextBytes(statusText, includesNullTerminator));
95}
96
97QString StatusTextHandler::_displayText(const StatusText& message)
98{
99 QString text = message.getFormattedText();
100 (void) text.replace(kUnreadStart, message.isRead() ? QString() : QStringLiteral("<b>"));
101 (void) text.replace(kUnreadEnd, message.isRead() ? QString() : QStringLiteral("</b>"));
102 return text;
103}
104
106{
107 QString result;
108 for (const StatusText *message: messages()) {
109 (void) result.prepend(_displayText(*message));
110 }
111
112 return result;
113}
114
116{
117 const bool hadMessages = !m_messages.isEmpty();
118
119 qDeleteAll(m_messages);
120 m_messages.clear();
121
122 _resetUnreadSeverityCounts();
123
124 if (m_unreadMessageCount != 0) {
125 m_unreadMessageCount = 0;
128 }
129
130 if (hadMessages) {
132 }
133}
134
136{
137 if (m_unreadMessageCount == 0) {
138 return;
139 }
140
141 for (StatusText* message : m_messages) {
142 message->markRead();
143 }
144
145 m_unreadMessageCount = 0;
148
149 _resetUnreadSeverityCounts();
150
152}
153
154void StatusTextHandler::_resetUnreadSeverityCounts()
155{
156 if (m_unreadCriticalMessageCount != 0) {
157 m_unreadCriticalMessageCount = 0;
159 }
160
161 if (m_unreadWarningMessageCount != 0) {
162 m_unreadWarningMessageCount = 0;
164 }
165}
166
167void StatusTextHandler::handleHTMLEscapedTextMessage(MAV_COMPONENT compId, MAV_SEVERITY severity, const QString &text, const QString &description)
168{
169 QString htmlText(text);
170
171 (void) htmlText.replace("\n", "<br/>");
172
173 // TODO: handle text + description separately in the UI
174 if (!description.isEmpty()) {
175 QString htmlDescription(description);
176 (void) htmlDescription.replace("\n", "<br/>");
177 (void) htmlText.append(QStringLiteral("<br/><small><small>"));
178 (void) htmlText.append(htmlDescription);
179 (void) htmlText.append(QStringLiteral("</small></small>"));
180 }
181
182 if (m_activeComponent == MAV_COMPONENT::MAV_COMPONENT_ENUM_END) {
183 m_activeComponent = compId;
184 }
185
186 if (compId != m_activeComponent) {
187 m_multiComp = true;
188 }
189
190 // Color the output depending on the message severity. We have 3 distinct cases:
191 // 1: If we have an ERROR or worse, make it bigger, bolder, and highlight it red.
192 // 2: If we have a warning or notice, just make it bold and color it orange.
193 // 3: Otherwise color it the standard color, white.
194 QString style;
195 switch (severity) {
196 case MAV_SEVERITY_EMERGENCY:
197 case MAV_SEVERITY_ALERT:
198 case MAV_SEVERITY_CRITICAL:
199 case MAV_SEVERITY_ERROR:
200 style = QStringLiteral("<#E>");
201 break;
202
203 case MAV_SEVERITY_NOTICE:
204 case MAV_SEVERITY_WARNING:
205 style = QStringLiteral("<#I>");
206 break;
207
208 default:
209 style = QStringLiteral("<#N>");
210 break;
211 }
212
213 QString severityText;
214 switch (severity) {
215 case MAV_SEVERITY_EMERGENCY:
216 severityText = tr("EMERGENCY");
217 break;
218
219 case MAV_SEVERITY_ALERT:
220 severityText = tr("ALERT");
221 break;
222
223 case MAV_SEVERITY_CRITICAL:
224 severityText = tr("Critical");
225 break;
226
227 case MAV_SEVERITY_ERROR:
228 severityText = tr("Error");
229 break;
230
231 case MAV_SEVERITY_WARNING:
232 severityText = tr("Warning");
233 break;
234
235 case MAV_SEVERITY_NOTICE:
236 severityText = tr("Notice");
237 break;
238
239 case MAV_SEVERITY_INFO:
240 severityText = tr("Info");
241 break;
242
243 case MAV_SEVERITY_DEBUG:
244 severityText = tr("Debug");
245 break;
246
247 default:
248 qCWarning(StatusTextHandlerLog) << Q_FUNC_INFO << "Invalid MAV_SEVERITY";
249 break;
250 }
251
252 QString compString;
253 if (m_multiComp) {
254 compString = QString("COMP:%1").arg(compId);
255 }
256
257 const QString dateString = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
258
259 const QString formatText =
260 QString("<font style=\"%1\">%2[%3 %4] %5: %6%7</font><br/>")
261 .arg(style, kUnreadStart, dateString, compString, severityText, htmlText, kUnreadEnd);
262
263 StatusText* const message = new StatusText(compId, severity, text);
264 message->setFormatedText(formatText);
265
266 (void) m_messages.append(message);
267
268 if (m_unreadMessageCount++ == 0) {
270 }
272
273 emit newFormattedMessage(_displayText(*message));
274
275 if (message->severityIsError()) {
276 m_unreadCriticalMessageCount++;
278 emit newErrorMessage(message->getText());
279 } else if (message->severityIsWarning()) {
280 m_unreadWarningMessageCount++;
282 }
283}
284
286{
287 if (message.msgid != MAVLINK_MSG_ID_STATUSTEXT) {
288 return;
289 }
290
291 _handleStatusText(message);
292}
293
294void StatusTextHandler::_handleStatusText(const mavlink_message_t &message)
295{
296 mavlink_statustext_t statustext;
297 mavlink_msg_statustext_decode(&message, &statustext);
298
299 bool includesNullTerminator;
300 const QByteArray chunkBytes = statusTextBytes(statustext, includesNullTerminator);
301
302 const MAV_COMPONENT compId = static_cast<MAV_COMPONENT>(message.compid);
303 if (m_chunkedStatusTextInfoMap.contains(compId) && (m_chunkedStatusTextInfoMap.value(compId).chunkId != statustext.id)) {
304 // We have an incomplete chunked status still pending
305 appendMissingChunk(m_chunkedStatusTextInfoMap[compId].rgMessageChunks);
306 _chunkedStatusTextCompleted(compId);
307 }
308
309 if (statustext.id == 0) {
310 // Non-chunked status text. We still use common chunked text output mechanism.
311 ChunkedStatusTextInfo_t chunkedInfo;
312 chunkedInfo.chunkId = 0;
313 chunkedInfo.severity = static_cast<MAV_SEVERITY>(statustext.severity);
314 chunkedInfo.rgMessageChunks.append(chunkBytes);
315 (void) m_chunkedStatusTextInfoMap.insert(compId, chunkedInfo);
316 } else {
317 if (!m_chunkedStatusTextInfoMap.contains(compId)) {
318 // Starting a new chunk sequence
319 ChunkedStatusTextInfo_t chunkedInfo;
320 chunkedInfo.chunkId = statustext.id;
321 chunkedInfo.severity = static_cast<MAV_SEVERITY>(statustext.severity);
322 (void) m_chunkedStatusTextInfoMap.insert(compId, chunkedInfo);
323 }
324
325 QList<std::optional<QByteArray>>& chunks = m_chunkedStatusTextInfoMap[compId].rgMessageChunks;
326 if (statustext.chunk_seq >= chunks.size()) {
327 // Chunks skipped in between stay nullopt and are rendered as missing
328 chunks.resize(statustext.chunk_seq + 1);
329 }
330 chunks[statustext.chunk_seq] = chunkBytes;
331
332 m_chunkedStatusTextTimer->start();
333 }
334
335 if ((statustext.id == 0) || includesNullTerminator) {
336 m_chunkedStatusTextTimer->stop();
337 _chunkedStatusTextCompleted(compId);
338 }
339}
340
341void StatusTextHandler::_chunkedStatusTextTimeout()
342{
343 for (auto compId : m_chunkedStatusTextInfoMap.keys()) {
344 auto& chunkedInfo = m_chunkedStatusTextInfoMap[compId];
345 appendMissingChunk(chunkedInfo.rgMessageChunks);
346 _chunkedStatusTextCompleted(compId);
347 }
348}
349
350void StatusTextHandler::_chunkedStatusTextCompleted(MAV_COMPONENT compId)
351{
352 const ChunkedStatusTextInfo_t& chunkedInfo = m_chunkedStatusTextInfoMap.value(compId);
353 const MAV_SEVERITY severity = chunkedInfo.severity;
354
355 // Decode only after reassembly so multibyte characters split across chunks survive
356 QString messageText;
357 QByteArray pendingBytes;
358 for (const std::optional<QByteArray>& chunk : std::as_const(chunkedInfo.rgMessageChunks)) {
359 if (chunk.has_value()) {
360 pendingBytes.append(*chunk);
361 } else {
362 (void) messageText.append(QString::fromUtf8(pendingBytes));
363 pendingBytes.clear();
364 (void) messageText.append(tr(" ... ", "Indicates missing chunk from chunked STATUS_TEXT"));
365 }
366 }
367 (void) messageText.append(QString::fromUtf8(pendingBytes));
368
369 (void) m_chunkedStatusTextInfoMap.remove(compId);
370
371 emit textMessageReceived(compId, severity, messageText, "");
372}
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void unreadMessageCountChanged()
StatusTextHandler(QObject *parent=nullptr)
void handleHTMLEscapedTextMessage(MAV_COMPONENT componentid, MAV_SEVERITY severity, const QString &text, const QString &description)
const QList< StatusText * > & messages() const
void formattedMessagesChanged()
void hasUnreadMessagesChanged()
void newErrorMessage(QString message)
static QString getMessageText(const mavlink_message_t &message)
void mavlinkMessageReceived(const mavlink_message_t &message)
void textMessageReceived(MAV_COMPONENT componentid, MAV_SEVERITY severity, QString text, QString description)
void newFormattedMessage(QString message)
QString formattedMessages() const
Unread messages are wrapped in so they render bold.
void unreadWarningMessageCountChanged()
void unreadCriticalMessageCountChanged()
StatusText(MAV_COMPONENT componentid, MAV_SEVERITY severity, const QString &text)
bool isRead() const
bool severityIsWarning() const
bool severityIsError() const
void setFormatedText(const QString &formatedText)
QString getFormattedText() const
QString getText() const