QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidSerialJni.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4
5#include <QtCore/QByteArray>
6#include <QtCore/QLoggingCategory>
7#include <QtCore/QString>
8#include <QtCore/QStringList>
9
10#include "AndroidSerialApp.h"
11#include "AndroidSerialPort.h"
13
14Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
15
16
18namespace AndroidSerial {
29
30constexpr const char* kJniUsbSerialManagerClassName = "org/mavlink/qgroundcontrol/serial/QGCUsbSerialManager";
31
34enum class Result : int
35{
36 Ok = 0,
37 Failed = -1,
38 IoError = -2,
40 NotOpen = -4,
44
45 // Opening a device that QGC may not use, as with PermissionDenied, by what has become of the permission.
50
52};
53
55enum class ResourceErrorReason : int
56{
60};
61
63using PortHandle = qint64;
64constexpr PortHandle kNoPort = 0;
65
69{
70public:
73 virtual bool newDataArrived(const char* bytes, int length) = 0;
76 virtual void bytesWritten(int count) = 0;
79 virtual void resourceErrorArrived(ResourceErrorReason reason, const QString& detail) = 0;
80
81protected:
82 PortListener() = default;
83 ~PortListener() = default;
84
85private:
86 // The registry holds a pointer to each listener.
87 Q_DISABLE_COPY_MOVE(PortListener)
88};
89
92void unregisterPort(PortHandle handle);
96bool deliverNewData(PortHandle handle, const char* bytes, int length);
97void deliverBytesWritten(PortHandle handle, int count);
98void deliverResourceError(PortHandle handle, ResourceErrorReason reason, const QString& detail);
99
102QList<QSerialPortInfo> availablePorts();
104QList<QSerialPortInfo> portsAwaitingPermission();
106bool hasPermission(const QString& portName);
110bool requestPermission(const QString& portName);
113bool isPermissionDenied(const QString& portName);
115bool isPortNameValid(const QString& portName);
116bool isPortOpen(const QString& portName);
118PortHandle getPortHandle(const QString& portName);
119
123QString getLastErrorDetail();
124
135int open(const QString& portName, PortHandle handle);
138Result close(PortHandle handle);
139
145bool isReading(PortHandle handle);
146
150QByteArray read(PortHandle handle, int length, std::chrono::milliseconds timeout);
152int write(PortHandle handle, const char* data, int length, std::chrono::milliseconds timeout);
156int writeWithProgress(PortHandle handle, const char* data, int length, std::chrono::milliseconds timeout);
162int writeAsync(PortHandle handle, const char* data, int length);
163
164Result setParameters(PortHandle handle, qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits,
165 QSerialPort::Parity parity);
170QList<FlowControl> getSupportedFlowControl(PortHandle handle);
172bool getXon(PortHandle handle);
174Result setLatencyTimer(PortHandle handle, int milliseconds);
175Result setBreak(PortHandle handle, bool set);
179Result purgeBuffers(PortHandle handle, QSerialPort::Directions directions);
180
181Result setDataTerminalReady(PortHandle handle, bool set);
182Result setRequestToSend(PortHandle handle, bool set);
184QSerialPort::PinoutSignals getControlLines(PortHandle handle);
188QSerialPort::PinoutSignals getSupportedControlLines(PortHandle handle);
191
192// For selfTest() only.
193
197Result echoToCallbacks(PortHandle handle, const char* data, int length, int deliveredLength, int written,
198 ResourceErrorReason reason, const QString& detail);
201QStringList unboundJavaNames();
202} // namespace AndroidSerial
virtual void resourceErrorArrived(ResourceErrorReason reason, const QString &detail)=0
virtual bool newDataArrived(const char *bytes, int length)=0
virtual void bytesWritten(int count)=0
bool getXon(PortHandle handle)
Under XON/XOFF flow control, false while the device has paused transmission.
bool isReading(PortHandle handle)
Result echoToCallbacks(PortHandle handle, const char *data, int length, int deliveredLength, int written, ResourceErrorReason reason, const QString &detail)
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 isControlLineSupported(PortHandle handle, QSerialPort::PinoutSignal line)
Whether the port has this one of the control lines.
QString getLastErrorDetail()
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.
bool deliverNewData(PortHandle handle, const char *bytes, int length)
FlowControl getFlowControl(PortHandle handle)
None also if the port cannot be asked.
void deliverResourceError(PortHandle handle, ResourceErrorReason reason, const QString &detail)
Result stopReading(PortHandle handle)
Stops delivering the port's input until startReading(); what arrives meanwhile is dropped.
constexpr const char * kJniUsbSerialManagerClassName
PortHandle registerPort(PortListener *listener)
Returns a handle under which Java's callbacks reach the listener until unregisterPort() has returned.
QByteArray read(PortHandle handle, int length, std::chrono::milliseconds timeout)
constexpr PortHandle kNoPort
Result setDataTerminalReady(PortHandle handle, bool set)
Result setRequestToSend(PortHandle handle, bool set)
PortHandle getPortHandle(const QString &portName)
The handle of the open port with this name, or kNoPort.
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() report...
bool hasPermission(const QString &portName)
Whether QGC may open the device of this port.
bool isControlLineActive(PortHandle handle, QSerialPort::PinoutSignal line)
Whether this one of the control lines is active.
Result startReading(PortHandle handle)
QList< QSerialPortInfo > portsAwaitingPermission()
The ports of attached devices that still need USB permission.
bool isPortOpen(const QString &portName)
QSerialPort::PinoutSignals getControlLines(PortHandle handle)
The control lines that are active.
Result purgeBuffers(PortHandle handle, QSerialPort::Directions directions)
QSerialPort::PinoutSignals getSupportedControlLines(PortHandle handle)
The control lines the port has at all. None also if the port cannot be asked.
QStringList unboundJavaNames()
Result setFlowControl(PortHandle handle, QSerialPort::FlowControl flowControl)
Result setBreak(PortHandle handle, bool set)
QList< QSerialPortInfo > availablePorts()
bool isPermissionDenied(const QString &portName)
int writeWithProgress(PortHandle handle, const char *data, int length, std::chrono::milliseconds timeout)
Result close(PortHandle handle)
bool isPortNameValid(const QString &portName)
Whether an attached device has a port with this name.
ResourceErrorReason
Why a port can no longer transfer data. QGCUsbSerialResourceError.java lists the same values.
@ WriteFailed
A write to the device failed.
@ ReadFailed
A read from the device failed.
@ DeviceDetached
The device was unplugged.
int open(const QString &portName, PortHandle handle)
bool requestPermission(const QString &portName)
Result setParameters(PortHandle handle, qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits, QSerialPort::Parity parity)
@ AlreadyOpen
Opening: the port is already open.
@ AmbiguousName
Opening: several attached devices share this stable name, so it opens none.
@ PermissionPending
The user was asked before, and has not answered yet.
@ PermissionDenied
Opening: QGC may not use the device, and the user denied the request to allow it.
@ PermissionAwaitsAndroid
The device was just attached, and Android asks the user first.
@ Unsupported
The port or its driver cannot do this.
@ NotOpen
No port is open under this handle.
@ DeviceNotFound
Opening: there is no such device.
@ PermissionRequested
This open has asked the user to allow it.
@ Failed
The call could not be made, or failed for a reason not listed here.
@ PermissionAwaitsFocus
The user is not asked while another window has the focus.
@ IoError
The device did not carry it out, and may be gone.
void unregisterPort(PortHandle handle)
QList< FlowControl > getSupportedFlowControl(PortHandle handle)
The flow control the port's driver has.
int writeAsync(PortHandle handle, const char *data, int length)