QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PosixSerialPorts.cc
Go to the documentation of this file.
1// The serial device nodes of the Android device, which QSerialPortInfo lists if the app says so.
2
3#include <algorithm>
4#include <optional>
5#include <unistd.h>
6#include <utility>
7
8#include <QtCore/QDir>
9#include <QtCore/QFile>
10#include <QtCore/QFileInfo>
11#include <QtCore/QStringList>
12
14
15namespace {
16
17QString sysfsAttribute(const QDir& dir, const QString& name)
18{
19 QFile file(dir.filePath(name));
20 return file.open(QIODevice::ReadOnly) ? QString::fromLatin1(file.readAll()).simplified() : QString();
21}
22
24bool hasDriver(const QString& sysTtyPath)
25{
26 return QFileInfo::exists(sysTtyPath + QStringLiteral("/device/driver"));
27}
28
32bool isUartWithoutHardware(const QString& sysTtyPath)
33{
34 return sysfsAttribute(QDir(sysTtyPath), QStringLiteral("type")) == QLatin1String("0");
35}
36
38std::optional<QDir> usbDeviceOf(const QString& sysTtyPath)
39{
40 const QString ttyPath = QFileInfo(sysTtyPath).canonicalFilePath();
41 if (ttyPath.isEmpty()) {
42 return std::nullopt;
43 }
44
45 QDir dir(ttyPath);
46 while (!dir.exists(QStringLiteral("idVendor"))) {
47 if (!dir.cdUp() || dir.isRoot()) {
48 return std::nullopt;
49 }
50 }
51
52 return dir;
53}
54
57bool isListedUsbDevice(const QDir& usbDevice, const QList<QSerialPortInfo>& usbPorts)
58{
59 bool hasBus = false;
60 bool hasDevice = false;
61 const int bus = sysfsAttribute(usbDevice, QStringLiteral("busnum")).toInt(&hasBus);
62 const int device = sysfsAttribute(usbDevice, QStringLiteral("devnum")).toInt(&hasDevice);
63 if (!hasBus || !hasDevice) {
64 return false;
65 }
66
67 const QString deviceName =
68 QStringLiteral("/dev/bus/usb/%1/%2").arg(bus, 3, 10, QLatin1Char('0')).arg(device, 3, 10, QLatin1Char('0'));
69 return std::any_of(usbPorts.cbegin(), usbPorts.cend(), [&deviceName](const QSerialPortInfo& port) {
70 const QString location = port.systemLocation();
71 return (location == deviceName) || location.startsWith(deviceName + QLatin1Char('#'));
72 });
73}
74
75void describeUsbDevice(const QDir& usbDevice, QSerialPortInfo::Data& info)
76{
77 info.vendorIdentifier =
78 sysfsAttribute(usbDevice, QStringLiteral("idVendor")).toUShort(&info.hasVendorIdentifier, 16);
80 sysfsAttribute(usbDevice, QStringLiteral("idProduct")).toUShort(&info.hasProductIdentifier, 16);
81 info.description = sysfsAttribute(usbDevice, QStringLiteral("product"));
82 info.manufacturer = sysfsAttribute(usbDevice, QStringLiteral("manufacturer"));
83 info.serialNumber = sysfsAttribute(usbDevice, QStringLiteral("serial"));
84}
85
86} // namespace
87
88QList<QSerialPortInfo> QSerialPortInfo::posixPorts(const QString& devDir, const QString& sysTtyDir,
89 const QList<QSerialPortInfo>& usbPorts)
90{
91 // The UARTs of common SoCs, and the USB serial adapters that the kernel drives itself. Their names find them
92 // where the app may not read sysfs.
93 static const QStringList kNodePatterns = {
94 QStringLiteral("ttyS*"), QStringLiteral("ttyHS*"), QStringLiteral("ttyMSM*"),
95 QStringLiteral("ttyHSL*"), QStringLiteral("ttymxc*"), QStringLiteral("ttyAMA*"),
96 QStringLiteral("ttyTHS*"), QStringLiteral("ttyUSB*"), QStringLiteral("ttyACM*"),
97 };
98
99 QStringList deviceNames = QDir(devDir).entryList(kNodePatterns, QDir::System | QDir::Files);
100 // As for desktop QSerialPort, any other tty that has a driver is a serial port too: a UART by another name.
101 const QStringList ttyNames = QDir(sysTtyDir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
102 for (const QString& ttyName : ttyNames) {
103 if (!deviceNames.contains(ttyName) && hasDriver(sysTtyDir + QLatin1Char('/') + ttyName)) {
104 deviceNames.append(ttyName);
105 }
106 }
107 deviceNames.sort();
108
109 QList<QSerialPortInfo> ports;
110
111 for (const QString& deviceName : std::as_const(deviceNames)) {
112 const QString systemLocation = devDir + QLatin1Char('/') + deviceName;
113 const QString sysTtyPath = sysTtyDir + QLatin1Char('/') + deviceName;
114 if ((::access(systemLocation.toLocal8Bit().constData(), R_OK | W_OK) != 0) ||
115 isUartWithoutHardware(sysTtyPath)) {
116 continue;
117 }
118 // One device, one port. Java takes a USB device from the kernel's driver as it opens it, which would end
119 // a connection that is open on the device's node.
120 const std::optional<QDir> usbDevice = usbDeviceOf(sysTtyPath);
121 if (usbDevice && isListedUsbDevice(*usbDevice, usbPorts)) {
122 continue;
123 }
124
125 Data info;
126 info.portName = deviceName;
128 if (usbDevice) {
129 describeUsbDevice(*usbDevice, info);
130 }
131 ports.append(QSerialPortInfo(info));
132 }
133
134 return ports;
135}
QSerialPortInfo()=default
A null port info, which describes no port.
static QList< QSerialPortInfo > posixPorts(const QString &devDir, const QString &sysTtyDir, const QList< QSerialPortInfo > &usbPorts={})
QString systemLocation() const
QCameraDevice device(const QString &description)
The attached camera of description description; a null device when none is.
Definition UVCDevices.cc:21
What is known of a port.