QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerialRegistry.cc
Go to the documentation of this file.
1// The registry of the ports that Java reports to. It is apart from the JNI calls in AndroidSerialJni.cc so that it
2// also builds for the host, where the tests of the serial port run on it in place of a fake.
3
4#include <QtCore/QHash>
5#include <QtCore/QReadWriteLock>
6
7#include "AndroidSerialJni.h"
9
10QGC_LOGGING_CATEGORY(AndroidSerialLog, "Android.AndroidSerial");
11
12namespace AndroidSerial {
13
14// Java gets a handle that is never used twice instead of a C++ pointer, so a report that arrives after its port
15// has closed finds nothing. A report looks the listener up and calls it under the read lock; unregisterPort()
16// takes the write lock, and so returns only when no report is still using the listener. The pattern follows Qt
17// Bluetooth's LowEnergyNotificationHub.
18static QReadWriteLock s_listenersLock;
19static QHash<PortHandle, PortListener*> s_listeners;
21
23{
24 if (!listener) {
25 qCWarning(AndroidSerialLog) << "registerPort called with null listener";
26 return kNoPort;
27 }
28
29 QWriteLocker locker(&s_listenersLock);
30
31 const PortHandle handle = ++s_lastHandle;
32 s_listeners.insert(handle, listener);
33 return handle;
34}
35
37{
38 QWriteLocker locker(&s_listenersLock);
39 s_listeners.remove(handle);
40}
41
42bool deliverNewData(PortHandle handle, const char* bytes, int length)
43{
44 QReadLocker locker(&s_listenersLock);
45 PortListener* const listener = s_listeners.value(handle, nullptr);
46 if (!listener) {
47 qCDebug(AndroidSerialLog) << "nativeDeviceNewData: stale token, port already closed";
48 return true;
49 }
50
51 return listener->newDataArrived(bytes, length);
52}
53
54void deliverBytesWritten(PortHandle handle, int count)
55{
56 QReadLocker locker(&s_listenersLock);
57 PortListener* const listener = s_listeners.value(handle, nullptr);
58 if (!listener) {
59 qCDebug(AndroidSerialLog) << "nativeDeviceBytesWritten: stale token, port already closed";
60 return;
61 }
62
63 listener->bytesWritten(count);
64}
65
66void deliverResourceError(PortHandle handle, ResourceErrorReason reason, const QString& detail)
67{
68 QReadLocker locker(&s_listenersLock);
69 PortListener* const listener = s_listeners.value(handle, nullptr);
70 if (!listener) {
71 qCDebug(AndroidSerialLog) << "nativeDeviceResourceError: stale token, port already closed";
72 return;
73 }
74
75 listener->resourceErrorArrived(reason, detail);
76}
77
78} // namespace AndroidSerial
#define QGC_LOGGING_CATEGORY(name, categoryStr)
virtual void resourceErrorArrived(ResourceErrorReason reason, const QString &detail)=0
virtual bool newDataArrived(const char *bytes, int length)=0
virtual void bytesWritten(int count)=0
void deliverBytesWritten(PortHandle handle, int count)
qint64 PortHandle
Names an open port in every call to Java and in Java's callbacks. registerPort() makes one.
bool deliverNewData(PortHandle handle, const char *bytes, int length)
void deliverResourceError(PortHandle handle, ResourceErrorReason reason, const QString &detail)
PortHandle registerPort(PortListener *listener)
Returns a handle under which Java's callbacks reach the listener until unregisterPort() has returned.
constexpr PortHandle kNoPort
static QHash< PortHandle, PortListener * > s_listeners
static QReadWriteLock s_listenersLock
static PortHandle s_lastHandle
ResourceErrorReason
Why a port can no longer transfer data. QGCUsbSerialResourceError.java lists the same values.
void unregisterPort(PortHandle handle)