QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerialSelfTest.cc
Go to the documentation of this file.
1// QGC's boot-time proof that C++ and Java reach each other in the app as it is packaged. It uses the JNI layer as a
2// port does, through AndroidSerialJni.h, and knows nothing of JNI itself.
3
4#include <utility>
5
6#include <QtCore/QByteArray>
7#include <QtCore/QObject>
8#include <QtCore/QStringList>
9#include <QtCore/QThread>
10
11#include "AndroidSerialJni.h"
12#include "QGCLoggingCategory.h"
13
14QGC_LOGGING_CATEGORY(AndroidSerialSelfTestLog, "Android.AndroidSerial.SelfTest")
15
16namespace AndroidSerial {
17
19{
20 // Notes what Java's callbacks deliver.
21 struct Echo final : PortListener
22 {
23 bool newDataArrived(const char* bytes, int length) override
24 {
25 heard.append(QStringLiteral("data ") + QString::fromLatin1(QByteArray(bytes, length).toHex()));
26 return true;
27 }
28
29 void bytesWritten(int count) override { heard.append(QStringLiteral("written %1").arg(count)); }
30
31 void resourceErrorArrived(ResourceErrorReason reason, const QString& detail) override
32 {
33 heard.append(QStringLiteral("resource error %1 %2").arg(static_cast<int>(reason)).arg(detail));
34 }
35
36 QStringList heard;
37 } echo;
38
39 // One byte more than Java is told to deliver, bytes that are negative in Java, a count that needs more than
40 // 16 bits and a text that is not ASCII: what a wrong signature or conversion would change.
41 static constexpr char kData[] = {'\x00', '\x7f', '\x80', '\xff', '\x51'};
42 constexpr int kDataLength = 4;
43 constexpr int kWritten = 70000;
44 const QString detail = QStringLiteral("d\u00e9tail");
45 const QStringList expected = {
46 QStringLiteral("data 007f80ff"),
47 QStringLiteral("written 70000"),
48 QStringLiteral("resource error %1 %2").arg(static_cast<int>(ResourceErrorReason::WriteFailed)).arg(detail),
49 QStringLiteral("devices changed"),
50 };
51
52 DeviceWatcher* const watcher = DeviceWatcher::instance();
53 // Direct, because Java calls back on this thread before echoToCallbacks returns. An Android thread emits the
54 // signal as well, for a device that is attached or detached or a permission that is answered meanwhile.
55 // That is no part of the echo, and the thread must not write to it.
56 const Qt::HANDLE selfTestThread = QThread::currentThreadId();
57 const QMetaObject::Connection watching = QObject::connect(
58 watcher, &DeviceWatcher::devicesChanged, watcher,
59 [&echo, selfTestThread]() {
60 if (QThread::currentThreadId() == selfTestThread) {
61 echo.heard.append(QStringLiteral("devices changed"));
62 }
63 },
64 Qt::DirectConnection);
65 const PortHandle handle = registerPort(&echo);
66
67 const Result echoed = echoToCallbacks(handle, kData, static_cast<int>(sizeof(kData)), kDataLength, kWritten,
69
70 unregisterPort(handle);
71 (void) QObject::disconnect(watching);
72
73 QStringList failures;
74 // A method or field that Java lacks is only logged when the IDs are cached, and QGC runs on without it. The
75 // calls here reach three of the methods and none of the fields.
76 const QStringList unbound = unboundJavaNames();
77 for (const QString& name : unbound) {
78 failures.append(QStringLiteral("Java %1 is not bound").arg(name));
79 }
80 if (echoed != Result::Ok) {
81 failures.append(QStringLiteral("echoToCallbacks returned %1").arg(static_cast<int>(echoed)));
82 }
83 if (echo.heard != expected) {
84 failures.append(QStringLiteral("Java's callbacks delivered (%1), not (%2)")
85 .arg(echo.heard.join(QStringLiteral(", ")), expected.join(QStringLiteral(", "))));
86 }
87
88 // Calls that Java itself answers, with a result that a call which fails on its way cannot return.
89 const int opened = open(QStringLiteral("usb:self-test"), handle);
90 if (opened != static_cast<int>(Result::DeviceNotFound)) {
91 failures.append(QStringLiteral("Opening a device that does not exist returned %1, not %2")
92 .arg(opened)
93 .arg(static_cast<int>(Result::DeviceNotFound)));
94 }
95 const int written = writeAsync(handle, kData, static_cast<int>(sizeof(kData)));
96 if (written != static_cast<int>(Result::NotOpen)) {
97 failures.append(QStringLiteral("Writing to a port that is not open returned %1, not %2")
98 .arg(written)
99 .arg(static_cast<int>(Result::NotOpen)));
100 }
101
102 for (const QString& failure : std::as_const(failures)) {
103 qCCritical(AndroidSerialSelfTestLog).noquote() << failure;
104 }
105 if (failures.isEmpty()) {
106 qCInfo(AndroidSerialSelfTestLog) << "USB serial JNI self-test passed";
107 }
108
109 return failures.isEmpty();
110}
111
112} // namespace AndroidSerial
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static DeviceWatcher * instance()
virtual void resourceErrorArrived(ResourceErrorReason reason, const QString &detail)=0
virtual bool newDataArrived(const char *bytes, int length)=0
virtual void bytesWritten(int count)=0
Result echoToCallbacks(PortHandle handle, const char *data, int length, int deliveredLength, int written, ResourceErrorReason reason, const QString &detail)
qint64 PortHandle
Names an open port in every call to Java and in Java's callbacks. registerPort() makes one.
PortHandle registerPort(PortListener *listener)
Returns a handle under which Java's callbacks reach the listener until unregisterPort() has returned.
QStringList unboundJavaNames()
ResourceErrorReason
Why a port can no longer transfer data. QGCUsbSerialResourceError.java lists the same values.
@ WriteFailed
A write to the device failed.
int open(const QString &portName, PortHandle handle)
@ NotOpen
No port is open under this handle.
@ DeviceNotFound
Opening: there is no such device.
void unregisterPort(PortHandle handle)
int writeAsync(PortHandle handle, const char *data, int length)