QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerial Namespace Reference

Classes

struct  JniMethodCache
struct  JniPortInfoFields
class  PortListener
class  DeviceWatcher
struct  JavaBinding
struct  JavaBindingList
 The Java methods or fields that C++ binds, in the order in which they are declared. More...
struct  JavaMethod
struct  JavaField
 A field of QGCUsbSerialPortInfo.java, declared by its JNI type, from which its signature is derived. More...

Typedefs

using PortHandle = qint64
 Names an open port in every call to Java and in Java's callbacks. registerPort() makes one.

Enumerations

enum class  FlowControl {
  None = 0 , RtsCts , DtrDsr , XonXoff ,
  XonXoffInline
}
enum class  Result : int {
  Ok = 0 , Failed = -1 , IoError = -2 , Unsupported = -3 ,
  NotOpen = -4 , DeviceNotFound = -5 , PermissionDenied = -6 , AlreadyOpen = -7 ,
  PermissionRequested = -8 , PermissionPending = -9 , PermissionAwaitsAndroid = -10 , PermissionAwaitsFocus = -11 ,
  AmbiguousName = -12
}
enum class  ResourceErrorReason : int { DeviceDetached = 1 , ReadFailed = 2 , WriteFailed = 3 }
 Why a port can no longer transfer data. QGCUsbSerialResourceError.java lists the same values. More...

Functions

static void cacheMethodIds (JNIEnv *env, jclass javaClass)
static void cachePortInfoFieldIds (QJniEnvironment &env)
static jclass getSerialManagerClass ()
 The serial manager class, or null until setNativeMethods() has found it.
static bool resolveSerialManagerClass (QJniEnvironment &env)
static jboolean jniDeviceNewData (JNIEnv *env, jclass javaClass, jlong token, jbyteArray data, jint length)
static void jniDeviceBytesWritten (JNIEnv *env, jclass javaClass, jlong token, jint count)
static void jniDeviceResourceError (JNIEnv *env, jclass javaClass, jlong token, jint reason, jstring detail)
static void jniDevicesChanged (JNIEnv *env, jclass javaClass)
bool setNativeMethods ()
static bool javaThrew (QJniEnvironment &env, const char *methodName)
static jclass findJavaMethod (QJniEnvironment &env, const JavaBinding< jmethodID > &method)
 Returns the serial manager class, or null after a warning if it or the method is unavailable.
template<typename Ret, typename... Args>
static Ret callJava (QJniEnvironment &env, const JavaMethod< Ret, Args... > &method, std::type_identity_t< Ret > fallback, Args... args)
template<typename Ret, typename... Args>
static auto callJavaObject (QJniEnvironment &env, const JavaMethod< Ret, Args... > &method, Args... args)
template<typename... Args>
static Result callJavaCommand (const JavaMethod< jint, Args... > &method, Args... args)
 Calls a static Java method that returns a QGCUsbSerialResult.
static jboolean toJava (bool value)
static jint toJava (std::chrono::milliseconds timeout)
static bool callJavaBool (const JavaMethod< jboolean, jlong > &method, PortHandle handle)
static bool callJavaBool (const JavaMethod< jboolean, jstring > &method, const QString &portName)
static QList< int > callJavaIntArray (const JavaMethod< jintArray, jlong > &method, PortHandle handle)
static AndroidInterface::JniLocalRef< jbyteArray > toJavaBytes (QJniEnvironment &env, const char *data, int length, const char *caller)
static QString stringField (JNIEnv *env, jobject object, const JavaField< jstring > &field)
static quint16 usbIdField (JNIEnv *env, jobject object, const JavaField< jint > &field)
QList< QSerialPortInfo > availablePorts ()
QList< QSerialPortInfo > portsAwaitingPermission ()
 The ports of attached devices that still need USB permission.
bool hasPermission (const QString &portName)
 Whether QGC may open the device of this port.
bool requestPermission (const QString &portName)
bool isPermissionDenied (const QString &portName)
bool isPortNameValid (const QString &portName)
 Whether an attached device has a port with this name.
bool isPortOpen (const QString &portName)
PortHandle getPortHandle (const QString &portName)
 The handle of the open port with this name, or kNoPort.
QString getLastErrorDetail ()
int open (const QString &portName, PortHandle handle)
Result close (PortHandle handle)
Result startReading (PortHandle handle)
Result stopReading (PortHandle handle)
 Stops delivering the port's input until startReading(); what arrives meanwhile is dropped.
bool isReading (PortHandle handle)
QByteArray read (PortHandle handle, int length, std::chrono::milliseconds timeout)
template<typename... Args>
static int callJavaWrite (const JavaMethod< jint, jlong, jbyteArray, jint, Args... > &method, PortHandle handle, const char *data, int length, Args... args)
int write (PortHandle handle, const char *data, int length, std::chrono::milliseconds timeout)
 Returns length, or a Result: Failed if the write timed out, otherwise what writeWithProgress() reports.
int writeWithProgress (PortHandle handle, const char *data, int length, std::chrono::milliseconds timeout)
int writeAsync (PortHandle handle, const char *data, int length)
static std::optional< jint > toJavaDataBits (QSerialPort::DataBits dataBits)
static std::optional< jint > toJavaStopBits (QSerialPort::StopBits stopBits)
static std::optional< jint > toJavaParity (QSerialPort::Parity parity)
static std::optional< FlowControl > toJavaFlowControl (QSerialPort::FlowControl flowControl)
static std::optional< FlowControl > fromJavaFlowControl (jint code)
Result setParameters (PortHandle handle, qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits, QSerialPort::Parity parity)
FlowControl getFlowControl (PortHandle handle)
 None also if the port cannot be asked.
Result setFlowControl (PortHandle handle, QSerialPort::FlowControl flowControl)
QList< FlowControl > getSupportedFlowControl (PortHandle handle)
 The flow control the port's driver has.
bool getXon (PortHandle handle)
 Under XON/XOFF flow control, false while the device has paused transmission.
Result setLatencyTimer (PortHandle handle, int milliseconds)
 Sets how long an FTDI chip holds back a partly filled packet, 1 to 255 ms. No other chip supports it.
Result setBreak (PortHandle handle, bool set)
Result purgeBuffers (PortHandle handle, QSerialPort::Directions directions)
Result setDataTerminalReady (PortHandle handle, bool set)
Result setRequestToSend (PortHandle handle, bool set)
static QSerialPort::PinoutSignals callJavaControlLines (const JavaMethod< jintArray, jlong > &method, PortHandle handle)
QSerialPort::PinoutSignals getControlLines (PortHandle handle)
 The control lines that are active.
bool isControlLineActive (PortHandle handle, QSerialPort::PinoutSignal line)
 Whether this one of the control lines is active.
QSerialPort::PinoutSignals getSupportedControlLines (PortHandle handle)
 The control lines the port has at all. None also if the port cannot be asked.
bool isControlLineSupported (PortHandle handle, QSerialPort::PinoutSignal line)
 Whether the port has this one of the control lines.
Result echoToCallbacks (PortHandle handle, const char *data, int length, int deliveredLength, int written, ResourceErrorReason reason, const QString &detail)
template<typename Id>
static void appendUnbound (QStringList &names, const char *kind, const JavaBindingList< Id > &bindings)
 Adds each method or field of the list that JNI did not find in Java to the names.
QStringList unboundJavaNames ()
PortHandle registerPort (PortListener *listener)
 Returns a handle under which Java's callbacks reach the listener until unregisterPort() has returned.
void unregisterPort (PortHandle handle)
bool deliverNewData (PortHandle handle, const char *bytes, int length)
void deliverBytesWritten (PortHandle handle, int count)
void deliverResourceError (PortHandle handle, ResourceErrorReason reason, const QString &detail)
bool selfTest ()

Variables

static JniMethodCache s_methods
static std::atomic< jclass > s_serialManagerClass {nullptr}
static JniPortInfoFields s_portInfoFields
static QString t_exceptionDetail
constexpr const char * kJniUsbSerialManagerClassName = "org/mavlink/qgroundcontrol/serial/QGCUsbSerialManager"
constexpr PortHandle kNoPort = 0
static QReadWriteLock s_listenersLock
static QHash< PortHandle, PortListener * > s_listeners
static PortHandle s_lastHandle = kNoPort
JavaBindingList< jmethodID > s_javaMethods
JavaBindingList< jfieldID > s_javaFields

Detailed Description

The USB serial ports of the Android device, as QGCUsbSerialManager.java provides them. A function that calls a Java method has the name of that method.

What the app itself uses of AndroidSerial, the USB serial ports of the Android device as Java provides them: it binds Java's callbacks as the library loads, tests that binding at boot, and hears when the devices change. AndroidSerialJni.h declares the rest of the namespace, which is private to the serial port library.

Typedef Documentation

◆ PortHandle

using AndroidSerial::PortHandle = qint64

Names an open port in every call to Java and in Java's callbacks. registerPort() makes one.

Definition at line 63 of file AndroidSerialJni.h.

Enumeration Type Documentation

◆ FlowControl

enum class AndroidSerial::FlowControl
strong

The flow control a USB serial driver may have, by the code that flowControlCode() in QGCUsbSerialManager.java gives it. QSerialPort::FlowControl names only three of them.

Enumerator
None 
RtsCts 
DtrDsr 
XonXoff 
XonXoffInline 

Definition at line 21 of file AndroidSerialJni.h.

◆ ResourceErrorReason

enum class AndroidSerial::ResourceErrorReason : int
strong

Why a port can no longer transfer data. QGCUsbSerialResourceError.java lists the same values.

Enumerator
DeviceDetached 

The device was unplugged.

ReadFailed 

A read from the device failed.

WriteFailed 

A write to the device failed.

Definition at line 55 of file AndroidSerialJni.h.

◆ Result

enum class AndroidSerial::Result : int
strong

How an operation on a port went. QGCUsbSerialResult.java lists the same values. A function that returns a count or a descriptor as an int returns one of the negative values instead when it fails.

Enumerator
Ok 
Failed 

The call could not be made, or failed for a reason not listed here.

IoError 

The device did not carry it out, and may be gone.

Unsupported 

The port or its driver cannot do this.

NotOpen 

No port is open under this handle.

DeviceNotFound 

Opening: there is no such device.

PermissionDenied 

Opening: QGC may not use the device, and the user denied the request to allow it.

AlreadyOpen 

Opening: the port is already open.

PermissionRequested 

This open has asked the user to allow it.

PermissionPending 

The user was asked before, and has not answered yet.

PermissionAwaitsAndroid 

The device was just attached, and Android asks the user first.

PermissionAwaitsFocus 

The user is not asked while another window has the focus.

AmbiguousName 

Opening: several attached devices share this stable name, so it opens none.

Definition at line 34 of file AndroidSerialJni.h.

Function Documentation

◆ appendUnbound()

template<typename Id>
void AndroidSerial::appendUnbound ( QStringList & names,
const char * kind,
const JavaBindingList< Id > & bindings )
static

Adds each method or field of the list that JNI did not find in Java to the names.

Definition at line 945 of file AndroidSerialJni.cc.

References appendUnbound(), and AndroidSerial::JavaBindingList< Id >::first.

Referenced by appendUnbound(), and unboundJavaNames().

◆ availablePorts()

◆ cacheMethodIds()

void AndroidSerial::cacheMethodIds ( JNIEnv * env,
jclass javaClass )
static

Definition at line 67 of file AndroidSerialJni.cc.

References cacheMethodIds(), and s_javaMethods.

Referenced by cacheMethodIds(), and resolveSerialManagerClass().

◆ cachePortInfoFieldIds()

void AndroidSerial::cachePortInfoFieldIds ( QJniEnvironment & env)
static

Definition at line 97 of file AndroidSerialJni.cc.

References cachePortInfoFieldIds(), and s_javaFields.

Referenced by cachePortInfoFieldIds(), and resolveSerialManagerClass().

◆ callJava()

template<typename Ret, typename... Args>
Ret AndroidSerial::callJava ( QJniEnvironment & env,
const JavaMethod< Ret, Args... > & method,
std::type_identity_t< Ret > fallback,
Args... args )
static

Calls a static Java method that returns an int, a long or a boolean. Returns fallback, after a warning, if the method is unavailable or throws. The arguments have the JNI types that the method declares.

Definition at line 340 of file AndroidSerialJni.cc.

References AndroidSerial::JavaMethod< Ret, Args >::call(), callJava(), findJavaMethod(), javaThrew(), and AndroidSerial::JavaBinding< Id >::name.

Referenced by callJava(), callJavaBool(), callJavaBool(), callJavaCommand(), callJavaWrite(), getFlowControl(), getPortHandle(), and open().

◆ callJavaBool() [1/2]

bool AndroidSerial::callJavaBool ( const JavaMethod< jboolean, jlong > & method,
PortHandle handle )
static

◆ callJavaBool() [2/2]

bool AndroidSerial::callJavaBool ( const JavaMethod< jboolean, jstring > & method,
const QString & portName )
static

◆ callJavaCommand()

template<typename... Args>
Result AndroidSerial::callJavaCommand ( const JavaMethod< jint, Args... > & method,
Args... args )
static

◆ callJavaControlLines()

◆ callJavaIntArray()

QList< int > AndroidSerial::callJavaIntArray ( const JavaMethod< jintArray, jlong > & method,
PortHandle handle )
static

◆ callJavaObject()

template<typename Ret, typename... Args>
auto AndroidSerial::callJavaObject ( QJniEnvironment & env,
const JavaMethod< Ret, Args... > & method,
Args... args )
static

Calls a static Java method that returns an object, such as an array. The reference is null, after a warning, if the method is unavailable, throws or returns null.

Definition at line 361 of file AndroidSerialJni.cc.

References AndroidSerial::JavaMethod< Ret, Args >::call(), callJavaObject(), findJavaMethod(), javaThrew(), and AndroidSerial::JavaBinding< Id >::name.

Referenced by availablePorts(), callJavaIntArray(), callJavaObject(), getLastErrorDetail(), and read().

◆ callJavaWrite()

template<typename... Args>
int AndroidSerial::callJavaWrite ( const JavaMethod< jint, jlong, jbyteArray, jint, Args... > & method,
PortHandle handle,
const char * data,
int length,
Args... args )
static

Calls one of the Java write methods with the data as a Java array. Returns what the method returns, or Result::Failed if the call cannot be made.

Definition at line 687 of file AndroidSerialJni.cc.

References callJava(), callJavaWrite(), Failed, AndroidSerial::JavaBinding< Id >::name, and toJavaBytes().

Referenced by callJavaWrite(), writeAsync(), and writeWithProgress().

◆ close()

Result AndroidSerial::close ( PortHandle handle)

Ok also if no port is open under the handle. IoError if the device did not close cleanly; Java has given the port up all the same.

Definition at line 635 of file AndroidSerialJni.cc.

References callJavaCommand(), close(), and s_methods.

Referenced by close().

◆ deliverBytesWritten()

void AndroidSerial::deliverBytesWritten ( PortHandle handle,
int count )

◆ deliverNewData()

bool AndroidSerial::deliverNewData ( PortHandle handle,
const char * bytes,
int length )

Pass what Java reports for the port with this handle to its listener, or drop it if no port is registered under the handle any more. Java's callbacks call these on Java's threads; a test calls them in Java's place. deliverNewData() returns what the listener answers, or true if there is none.

Definition at line 42 of file AndroidSerialRegistry.cc.

References AndroidSerial::PortListener::newDataArrived(), s_listeners, and s_listenersLock.

Referenced by jniDeviceNewData().

◆ deliverResourceError()

void AndroidSerial::deliverResourceError ( PortHandle handle,
ResourceErrorReason reason,
const QString & detail )

◆ echoToCallbacks()

Result AndroidSerial::echoToCallbacks ( PortHandle handle,
const char * data,
int length,
int deliveredLength,
int written,
ResourceErrorReason reason,
const QString & detail )

Has Java call each of its callbacks once, on the calling thread: the listener of the handle gets the first deliveredLength of the length bytes of data, a report of this many written bytes and a resource error with this reason and detail, and DeviceWatcher emits its signal.

Definition at line 928 of file AndroidSerialJni.cc.

References callJavaCommand(), echoToCallbacks(), Failed, s_methods, and toJavaBytes().

Referenced by echoToCallbacks(), and selfTest().

◆ findJavaMethod()

jclass AndroidSerial::findJavaMethod ( QJniEnvironment & env,
const JavaBinding< jmethodID > & method )
static

Returns the serial manager class, or null after a warning if it or the method is unavailable.

Definition at line 316 of file AndroidSerialJni.cc.

References findJavaMethod(), getSerialManagerClass(), AndroidSerial::JavaBinding< Id >::id, and AndroidSerial::JavaBinding< Id >::name.

Referenced by callJava(), callJavaObject(), and findJavaMethod().

◆ fromJavaFlowControl()

std::optional< FlowControl > AndroidSerial::fromJavaFlowControl ( jint code)
static

◆ getControlLines()

QSerialPort::PinoutSignals AndroidSerial::getControlLines ( PortHandle handle)

The control lines that are active.

Definition at line 904 of file AndroidSerialJni.cc.

References callJavaControlLines(), getControlLines(), and s_methods.

Referenced by getControlLines(), and isControlLineActive().

◆ getFlowControl()

FlowControl AndroidSerial::getFlowControl ( PortHandle handle)

None also if the port cannot be asked.

Definition at line 813 of file AndroidSerialJni.cc.

References callJava(), fromJavaFlowControl(), getFlowControl(), None, and s_methods.

Referenced by getFlowControl().

◆ getLastErrorDetail()

QString AndroidSerial::getLastErrorDetail ( )

What the failure of this thread's last call to Java says of itself beyond its Result, such as the message of Java's exception, or nothing. Ask only right after a call that returned a failure, on its thread: a later call replaces the detail, and a call that fails before it reaches Java leaves that of the one before.

Definition at line 601 of file AndroidSerialJni.cc.

References callJavaObject(), getLastErrorDetail(), s_methods, and t_exceptionDetail.

Referenced by getLastErrorDetail().

◆ getPortHandle()

PortHandle AndroidSerial::getPortHandle ( const QString & portName)

The handle of the open port with this name, or kNoPort.

Definition at line 585 of file AndroidSerialJni.cc.

References callJava(), getPortHandle(), kNoPort, and s_methods.

Referenced by getPortHandle(), and isPortOpen().

◆ getSerialManagerClass()

jclass AndroidSerial::getSerialManagerClass ( )
static

The serial manager class, or null until setNativeMethods() has found it.

Definition at line 122 of file AndroidSerialJni.cc.

References getSerialManagerClass(), and s_serialManagerClass.

Referenced by findJavaMethod(), getSerialManagerClass(), and unboundJavaNames().

◆ getSupportedControlLines()

QSerialPort::PinoutSignals AndroidSerial::getSupportedControlLines ( PortHandle handle)

The control lines the port has at all. None also if the port cannot be asked.

Definition at line 914 of file AndroidSerialJni.cc.

References callJavaControlLines(), getSupportedControlLines(), and s_methods.

Referenced by getSupportedControlLines(), and isControlLineSupported().

◆ getSupportedFlowControl()

QList< FlowControl > AndroidSerial::getSupportedFlowControl ( PortHandle handle)

The flow control the port's driver has.

Definition at line 832 of file AndroidSerialJni.cc.

References callJavaIntArray(), fromJavaFlowControl(), getSupportedFlowControl(), and s_methods.

Referenced by getSupportedFlowControl().

◆ getXon()

bool AndroidSerial::getXon ( PortHandle handle)

Under XON/XOFF flow control, false while the device has paused transmission.

Definition at line 846 of file AndroidSerialJni.cc.

References callJavaBool(), getXon(), and s_methods.

Referenced by getXon().

◆ hasPermission()

bool AndroidSerial::hasPermission ( const QString & portName)

Whether QGC may open the device of this port.

Definition at line 560 of file AndroidSerialJni.cc.

References callJavaBool(), hasPermission(), and s_methods.

Referenced by hasPermission(), AndroidSerialAccess::hasUsbPermission(), and AndroidSerialAccess::requestUsbPermission().

◆ isControlLineActive()

bool AndroidSerial::isControlLineActive ( PortHandle handle,
QSerialPort::PinoutSignal line )

Whether this one of the control lines is active.

Definition at line 909 of file AndroidSerialJni.cc.

References getControlLines(), and isControlLineActive().

Referenced by isControlLineActive().

◆ isControlLineSupported()

bool AndroidSerial::isControlLineSupported ( PortHandle handle,
QSerialPort::PinoutSignal line )

Whether the port has this one of the control lines.

Definition at line 919 of file AndroidSerialJni.cc.

References getSupportedControlLines(), and isControlLineSupported().

Referenced by isControlLineSupported().

◆ isPermissionDenied()

bool AndroidSerial::isPermissionDenied ( const QString & portName)

Whether the user has denied QGC the device of this port, and has not been asked again since. DeviceWatcher tells of a denial, as it does of a grant.

Definition at line 570 of file AndroidSerialJni.cc.

References callJavaBool(), isPermissionDenied(), and s_methods.

Referenced by isPermissionDenied(), and AndroidSerialAccess::isUsbPermissionDenied().

◆ isPortNameValid()

bool AndroidSerial::isPortNameValid ( const QString & portName)

Whether an attached device has a port with this name.

Definition at line 575 of file AndroidSerialJni.cc.

References callJavaBool(), isPortNameValid(), and s_methods.

Referenced by isPortNameValid().

◆ isPortOpen()

bool AndroidSerial::isPortOpen ( const QString & portName)

Definition at line 580 of file AndroidSerialJni.cc.

References getPortHandle(), isPortOpen(), and kNoPort.

Referenced by isPortOpen().

◆ isReading()

bool AndroidSerial::isReading ( PortHandle handle)

Definition at line 654 of file AndroidSerialJni.cc.

References callJavaBool(), isReading(), and s_methods.

Referenced by isReading().

◆ javaThrew()

bool AndroidSerial::javaThrew ( QJniEnvironment & env,
const char * methodName )
static

Returns whether the call into Java that this thread has just made threw, after a warning. What the exception says of itself is kept for getLastErrorDetail().

Definition at line 298 of file AndroidSerialJni.cc.

References AndroidInterface::JniLocalRef< T >::get(), javaThrew(), and t_exceptionDetail.

Referenced by callJava(), callJavaObject(), and javaThrew().

◆ jniDeviceBytesWritten()

void AndroidSerial::jniDeviceBytesWritten ( JNIEnv * env,
jclass javaClass,
jlong token,
jint count )
static

Definition at line 238 of file AndroidSerialJni.cc.

References deliverBytesWritten(), and jniDeviceBytesWritten().

Referenced by jniDeviceBytesWritten(), and setNativeMethods().

◆ jniDeviceNewData()

jboolean AndroidSerial::jniDeviceNewData ( JNIEnv * env,
jclass javaClass,
jlong token,
jbyteArray data,
jint length )
static

Definition at line 199 of file AndroidSerialJni.cc.

References deliverNewData(), and jniDeviceNewData().

Referenced by jniDeviceNewData(), and setNativeMethods().

◆ jniDeviceResourceError()

void AndroidSerial::jniDeviceResourceError ( JNIEnv * env,
jclass javaClass,
jlong token,
jint reason,
jstring detail )
static

◆ jniDevicesChanged()

void AndroidSerial::jniDevicesChanged ( JNIEnv * env,
jclass javaClass )
static

◆ open()

int AndroidSerial::open ( const QString & portName,
PortHandle handle )

Opens the port under a handle from registerPort(). Returns the file descriptor of the port's USB connection, which is not negative, or a Result. For a device that still needs USB permission the result is one of the five Permission results, which tells what has become of the permission, and getLastErrorDetail() has nothing to add to it. Such an open also asks the user for the permission, who answers later; DeviceWatcher tells of the answer. Callers open ports without being told to and try again every few seconds, so the open asks once for each time the device is attached, not in the first seconds after that, which Android's own prompt for the device takes to appear, and only while QGC's window has the focus. It asks once more if Android closes the dialog without an answer. After a denial, the user is asked again once the device has been replugged, or by requestPermission(). A stable name that no device with permission has stands for the only attached device with its vendor and product IDs that still needs permission, if there is exactly one.

Definition at line 617 of file AndroidSerialJni.cc.

References callJava(), Failed, kNoPort, open(), and s_methods.

Referenced by open(), and selfTest().

◆ portsAwaitingPermission()

QList< QSerialPortInfo > AndroidSerial::portsAwaitingPermission ( )

The ports of attached devices that still need USB permission.

Definition at line 552 of file AndroidSerialJni.cc.

References availablePorts(), and portsAwaitingPermission().

Referenced by portsAwaitingPermission().

◆ purgeBuffers()

Result AndroidSerial::purgeBuffers ( PortHandle handle,
QSerialPort::Directions directions )

Discards what the device has buffered in the given directions. With Output, it also stops the write thread from sending what is left of the data handed to writeAsync() so far, also where the driver has no purge command. The listener's bytesWritten() still follows for all of that data.

Definition at line 861 of file AndroidSerialJni.cc.

References callJavaCommand(), QSerialPort::Input, QSerialPort::Output, purgeBuffers(), s_methods, and toJava().

Referenced by purgeBuffers().

◆ read()

QByteArray AndroidSerial::read ( PortHandle handle,
int length,
std::chrono::milliseconds timeout )

Reads up to length bytes from the device, waiting no longer than timeout. Usable only before startReading() has first run on the port, which QSerialPort does as it opens a port for reading: from then on input arrives through the port instead, and this returns nothing. A negative timeout waits 1 ms.

Definition at line 659 of file AndroidSerialJni.cc.

References callJavaObject(), read(), s_methods, and toJava().

Referenced by read().

◆ registerPort()

PortHandle AndroidSerial::registerPort ( PortListener * listener)

Returns a handle under which Java's callbacks reach the listener until unregisterPort() has returned.

Definition at line 22 of file AndroidSerialRegistry.cc.

References kNoPort, s_lastHandle, s_listeners, and s_listenersLock.

Referenced by selfTest().

◆ requestPermission()

bool AndroidSerial::requestPermission ( const QString & portName)

Asks the user to let QGC open the device of this port, unless it already may. The user answers later. Unlike open(), this asks whenever it is called, also after the user has denied the permission. Returns false if there is no such device.

Definition at line 565 of file AndroidSerialJni.cc.

References callJavaBool(), requestPermission(), and s_methods.

Referenced by requestPermission(), and AndroidSerialAccess::requestUsbPermission().

◆ resolveSerialManagerClass()

bool AndroidSerial::resolveSerialManagerClass ( QJniEnvironment & env)
static

Finds the serial manager class and the methods and fields that C++ binds. Returns false if Java has no such class.

Definition at line 129 of file AndroidSerialJni.cc.

References cacheMethodIds(), cachePortInfoFieldIds(), kJniUsbSerialManagerClassName, resolveSerialManagerClass(), and s_serialManagerClass.

Referenced by resolveSerialManagerClass(), and setNativeMethods().

◆ selfTest()

bool AndroidSerial::selfTest ( )
nodiscard

Proves, for QGC's boot test, that C++ and Java reach each other in the app as it is packaged: Java has every method and field that C++ binds, passes arguments that C++ gave it to each callback, and answers calls about a port that does not exist. Returns false after it has logged what failed, as critical messages of the category Android.AndroidSerial.SelfTest.

Definition at line 18 of file AndroidSerialSelfTest.cc.

References AndroidSerial::PortListener::bytesWritten(), DeviceNotFound, AndroidSerial::DeviceWatcher::devicesChanged(), echoToCallbacks(), AndroidSerial::DeviceWatcher::instance(), AndroidSerial::PortListener::newDataArrived(), NotOpen, Ok, open(), registerPort(), AndroidSerial::PortListener::resourceErrorArrived(), selfTest(), unboundJavaNames(), unregisterPort(), writeAsync(), and WriteFailed.

Referenced by QGCApplication::init(), and selfTest().

◆ setBreak()

Result AndroidSerial::setBreak ( PortHandle handle,
bool set )

Definition at line 856 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, setBreak(), and toJava().

Referenced by setBreak().

◆ setDataTerminalReady()

Result AndroidSerial::setDataTerminalReady ( PortHandle handle,
bool set )

Definition at line 872 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, setDataTerminalReady(), and toJava().

Referenced by setDataTerminalReady().

◆ setFlowControl()

Result AndroidSerial::setFlowControl ( PortHandle handle,
QSerialPort::FlowControl flowControl )

◆ setLatencyTimer()

Result AndroidSerial::setLatencyTimer ( PortHandle handle,
int milliseconds )

Sets how long an FTDI chip holds back a partly filled packet, 1 to 255 ms. No other chip supports it.

Definition at line 851 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, and setLatencyTimer().

Referenced by setLatencyTimer().

◆ setNativeMethods()

bool AndroidSerial::setNativeMethods ( )
nodiscard

Registers the callbacks that Java calls on its threads, and finds the Java methods that C++ calls: call it as the library loads, before anything else of the namespace. Returns false, after a warning, if Java does not declare the callbacks as they are registered here: a callback would then throw on its Java thread.

Definition at line 161 of file AndroidSerialJni.cc.

References jniDeviceBytesWritten(), jniDeviceNewData(), jniDeviceResourceError(), jniDevicesChanged(), kJniUsbSerialManagerClassName, resolveSerialManagerClass(), and setNativeMethods().

Referenced by JNI_OnLoad(), and setNativeMethods().

◆ setParameters()

Result AndroidSerial::setParameters ( PortHandle handle,
qint32 baudRate,
QSerialPort::DataBits dataBits,
QSerialPort::StopBits stopBits,
QSerialPort::Parity parity )

◆ setRequestToSend()

Result AndroidSerial::setRequestToSend ( PortHandle handle,
bool set )

Definition at line 877 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, setRequestToSend(), and toJava().

Referenced by setRequestToSend().

◆ startReading()

Result AndroidSerial::startReading ( PortHandle handle)

Starts delivering the port's input to its listener, or has Java's read thread read on after the listener answered that the port had no room for more.

Definition at line 644 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, and startReading().

Referenced by startReading().

◆ stopReading()

Result AndroidSerial::stopReading ( PortHandle handle)

Stops delivering the port's input until startReading(); what arrives meanwhile is dropped.

Definition at line 649 of file AndroidSerialJni.cc.

References callJavaCommand(), s_methods, and stopReading().

Referenced by stopReading().

◆ stringField()

QString AndroidSerial::stringField ( JNIEnv * env,
jobject object,
const JavaField< jstring > & field )
static

◆ toJava() [1/2]

jboolean AndroidSerial::toJava ( bool value)
static

◆ toJava() [2/2]

jint AndroidSerial::toJava ( std::chrono::milliseconds timeout)
static

Java takes a timeout as a count of milliseconds in an int, and zero as no limit. A negative timeout is that of a deadline that has passed, so it becomes the shortest wait there is, not zero.

Definition at line 402 of file AndroidSerialJni.cc.

References toJava().

◆ toJavaBytes()

AndroidInterface::JniLocalRef< jbyteArray > AndroidSerial::toJavaBytes ( QJniEnvironment & env,
const char * data,
int length,
const char * caller )
static

◆ toJavaDataBits()

std::optional< jint > AndroidSerial::toJavaDataBits ( QSerialPort::DataBits dataBits)
static

◆ toJavaFlowControl()

std::optional< FlowControl > AndroidSerial::toJavaFlowControl ( QSerialPort::FlowControl flowControl)
static

◆ toJavaParity()

std::optional< jint > AndroidSerial::toJavaParity ( QSerialPort::Parity parity)
static

◆ toJavaStopBits()

std::optional< jint > AndroidSerial::toJavaStopBits ( QSerialPort::StopBits stopBits)
static

◆ unboundJavaNames()

QStringList AndroidSerial::unboundJavaNames ( )

What C++ binds and JNI did not find in Java, each by its kind, name and JNI signature: the class alone if that is missing, else every such method and field. Nothing if all is bound.

Definition at line 956 of file AndroidSerialJni.cc.

References appendUnbound(), getSerialManagerClass(), kJniUsbSerialManagerClassName, s_javaFields, s_javaMethods, and unboundJavaNames().

Referenced by selfTest(), and unboundJavaNames().

◆ unregisterPort()

void AndroidSerial::unregisterPort ( PortHandle handle)

Definition at line 36 of file AndroidSerialRegistry.cc.

References s_listeners, and s_listenersLock.

Referenced by selfTest().

◆ usbIdField()

quint16 AndroidSerial::usbIdField ( JNIEnv * env,
jobject object,
const JavaField< jint > & field )
static

Definition at line 499 of file AndroidSerialJni.cc.

References AndroidSerial::JavaBinding< Id >::id, and usbIdField().

Referenced by availablePorts(), and usbIdField().

◆ write()

int AndroidSerial::write ( PortHandle handle,
const char * data,
int length,
std::chrono::milliseconds timeout )

Returns length, or a Result: Failed if the write timed out, otherwise what writeWithProgress() reports.

Definition at line 705 of file AndroidSerialJni.cc.

References Failed, write(), and writeWithProgress().

Referenced by write().

◆ writeAsync()

int AndroidSerial::writeAsync ( PortHandle handle,
const char * data,
int length )

Hands the data to the port's write thread and returns length at once, or a Result: Failed if the thread still has too much to write, IoError if the listener's resourceErrorArrived() has been called since startReading() last ran, NotOpen if the port is not open. The listener's bytesWritten() follows for the whole length: once the device has taken the data, however long that takes, or once purgeBuffers() has discarded what was not yet sent. If the device is lost instead, its resourceErrorArrived() follows.

Definition at line 716 of file AndroidSerialJni.cc.

References callJavaWrite(), s_methods, and writeAsync().

Referenced by selfTest(), and writeAsync().

◆ writeWithProgress()

int AndroidSerial::writeWithProgress ( PortHandle handle,
const char * data,
int length,
std::chrono::milliseconds timeout )

Returns the bytes written, fewer than length on timeout, or a Result: IoError or NotOpen if the port can no longer transfer data, as when its device is unplugged. A zero timeout waits without limit. A negative one is that of a deadline that has passed, and waits 1 ms, the shortest wait that Java takes.

Definition at line 711 of file AndroidSerialJni.cc.

References callJavaWrite(), s_methods, toJava(), and writeWithProgress().

Referenced by write(), and writeWithProgress().

Variable Documentation

◆ kJniUsbSerialManagerClassName

const char* AndroidSerial::kJniUsbSerialManagerClassName = "org/mavlink/qgroundcontrol/serial/QGCUsbSerialManager"
constexpr

◆ kNoPort

PortHandle AndroidSerial::kNoPort = 0
constexpr

Definition at line 64 of file AndroidSerialJni.h.

Referenced by getPortHandle(), isPortOpen(), open(), and registerPort().

◆ s_javaFields

JavaBindingList<jfieldID> AndroidSerial::s_javaFields
inline

◆ s_javaMethods

JavaBindingList<jmethodID> AndroidSerial::s_javaMethods
inline

◆ s_lastHandle

PortHandle AndroidSerial::s_lastHandle = kNoPort
static

Definition at line 20 of file AndroidSerialRegistry.cc.

Referenced by registerPort().

◆ s_listeners

QHash<PortHandle, PortListener*> AndroidSerial::s_listeners
static

◆ s_listenersLock

QReadWriteLock AndroidSerial::s_listenersLock
static

◆ s_methods

◆ s_portInfoFields

JniPortInfoFields AndroidSerial::s_portInfoFields
static

Definition at line 95 of file AndroidSerialJni.cc.

Referenced by availablePorts().

◆ s_serialManagerClass

std::atomic<jclass> AndroidSerial::s_serialManagerClass {nullptr}
static

Definition at line 65 of file AndroidSerialJni.cc.

Referenced by getSerialManagerClass(), and resolveSerialManagerClass().

◆ t_exceptionDetail

QString AndroidSerial::t_exceptionDetail
thread_localstatic

Definition at line 294 of file AndroidSerialJni.cc.

Referenced by getLastErrorDetail(), and javaThrew().