Skip to content

Commit

Permalink
openmv: Add drive matching support for linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwagyeman committed Nov 21, 2024
1 parent ef28da1 commit db98bc4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
21 changes: 6 additions & 15 deletions src/plugins/openmv/openmvplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3962,22 +3962,13 @@ void OpenMVPlugin::setPortPath(bool silent)
const QStorageInfo info = pair.first;
const QString serialNumber = pair.second;

if(info.isValid()
&& info.isReady()
&& (!info.isRoot())
&& (!info.isReadOnly())
&& (QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("fat"), Qt::CaseInsensitive) || QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("msdos"), Qt::CaseInsensitive) || QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("fuseblk"), Qt::CaseInsensitive))
&& ((!Utils::HostOsInfo::isMacHost()) || info.rootPath().startsWith(QStringLiteral("/volumes/"), Qt::CaseInsensitive))
&& ((!Utils::HostOsInfo::isLinuxHost()) || info.rootPath().startsWith(QStringLiteral("/media/"), Qt::CaseInsensitive) || info.rootPath().startsWith(QStringLiteral("/mnt/"), Qt::CaseInsensitive) || info.rootPath().startsWith(QStringLiteral("/run/"), Qt::CaseInsensitive)))
if((((m_major < OPENMV_DISK_ADDED_MAJOR)
|| ((m_major == OPENMV_DISK_ADDED_MAJOR) && (m_minor < OPENMV_DISK_ADDED_MINOR))
|| ((m_major == OPENMV_DISK_ADDED_MAJOR) && (m_minor == OPENMV_DISK_ADDED_MINOR) && (m_patch < OPENMV_DISK_ADDED_PATCH)))
|| QFile::exists(info.rootPath() + QStringLiteral(OPENMV_DISK_ADDED_NAME)))
&& (serialNumber == m_portDriveSerialNumber))
{
if((((m_major < OPENMV_DISK_ADDED_MAJOR)
|| ((m_major == OPENMV_DISK_ADDED_MAJOR) && (m_minor < OPENMV_DISK_ADDED_MINOR))
|| ((m_major == OPENMV_DISK_ADDED_MAJOR) && (m_minor == OPENMV_DISK_ADDED_MINOR) && (m_patch < OPENMV_DISK_ADDED_PATCH)))
|| QFile::exists(info.rootPath() + QStringLiteral(OPENMV_DISK_ADDED_NAME)))
&& (serialNumber == m_portDriveSerialNumber))
{
drives.append(info.rootPath());
}
drives.append(info.rootPath());
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/plugins/openmv/openmvplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,21 @@ class ScanDriveThread: public QObject
}
public slots: void scanDrivesSlot() {
QList<QPair<QStorageInfo, QString> > drives;
for(const QStorageInfo &drive : QStorageInfo::mountedVolumes()) {
drives.append(QPair<QStorageInfo, QString>(drive, driveSerialNumber(drive.rootPath())));
for(const QStorageInfo &info : QStorageInfo::mountedVolumes()) {
if(info.isValid()
&& info.isReady()
&& (!info.isRoot())
&& (!info.isReadOnly())
&& (QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("fat"), Qt::CaseInsensitive) ||
QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("msdos"), Qt::CaseInsensitive) ||
QString::fromUtf8(info.fileSystemType()).contains(QStringLiteral("fuseblk"), Qt::CaseInsensitive))
&& ((!Utils::HostOsInfo::isMacHost()) || info.rootPath().startsWith(QStringLiteral("/volumes/"), Qt::CaseInsensitive))
&& ((!Utils::HostOsInfo::isLinuxHost()) || info.rootPath().startsWith(QStringLiteral("/media/"), Qt::CaseInsensitive) ||
info.rootPath().startsWith(QStringLiteral("/mnt/"), Qt::CaseInsensitive) ||
info.rootPath().startsWith(QStringLiteral("/run/"), Qt::CaseInsensitive)))
{
drives.append(QPair<QStorageInfo, QString>(info, driveSerialNumber(info.rootPath())));
}
}
emit driveScanned(drives);
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/openmv/openmvpluginconnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ QList<QPair<QString, QString> > OpenMVPlugin::querySerialPorts(const QStringList

m_ioport->open(port);

QTimer::singleShot(20, &loop, &QEventLoop::quit);
QTimer::singleShot(50, &loop, &QEventLoop::quit);

loop.exec();

Expand Down
52 changes: 50 additions & 2 deletions src/plugins/openmv/tools/driveserialnumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <utils/hostosinfo.h>
#include <utils/qtcprocess.h>

#include "myqserialportinfo.h"

namespace OpenMV {
namespace Internal {

Expand All @@ -18,14 +20,16 @@ QString serialPortDriveSerialNumber(const QString &portName)
QStringList()
<< QStringLiteral("-Command")
<< QString(QStringLiteral("(Get-PnpDeviceProperty -InstanceId (Get-WmiObject Win32_SerialPort | Where-Object { $_.DeviceID -eq '%1' }).PNPDeviceId | Where-Object { $_.KeyName -eq 'DEVPKEY_Device_Parent' }).Data")).arg(QString(portName))));
process.runBlocking(timeout, Utils::EventLoopMode::Off);
process.runBlocking(timeout, Utils::EventLoopMode::On);

if(process.result() == Utils::ProcessResult::FinishedWithSuccess)
{
QRegularExpressionMatch match = QRegularExpression("\\\\(\\w+)$").match(process.stdOut().trimmed());
if (match.hasMatch()) return match.captured(1);
}
#elif defined(Q_OS_LINUX)
MyQSerialPortInfo info = MyQSerialPortInfo(QSerialPortInfo(portName));
return info.serialNumber();
#elif defined(Q_OS_MAC)
#endif

Expand All @@ -43,14 +47,58 @@ QString driveSerialNumber(const QString &drivePath)
QStringList()
<< QStringLiteral("-Command")
<< QString(QStringLiteral("(Get-Disk -Number (Get-Partition -DriveLetter '%1').DiskNumber).SerialNumber")).arg(QString(drivePath).remove(QStringLiteral(":")).remove(QStringLiteral("/")))));
process.runBlocking(timeout, Utils::EventLoopMode::Off);
process.runBlocking(timeout, Utils::EventLoopMode::On);

if(process.result() == Utils::ProcessResult::FinishedWithSuccess)
{
QString serialNumber = process.stdOut().trimmed();
return serialNumber;
}
#elif defined(Q_OS_LINUX)
Utils::Process process;
std::chrono::seconds timeout(10);
process.setTextChannelMode(Utils::Channel::Output, Utils::TextChannelMode::MultiLine);
process.setTextChannelMode(Utils::Channel::Error, Utils::TextChannelMode::MultiLine);
process.setCommand(Utils::CommandLine(Utils::FilePath::fromString(QStringLiteral("lsblk")),
QStringList()
<< QStringLiteral("-J")
<< QStringLiteral("-o")
<< QStringLiteral("MOUNTPOINT,NAME,SERIAL")));
process.runBlocking(timeout, Utils::EventLoopMode::On);

if(process.result() == Utils::ProcessResult::FinishedWithSuccess)
{
QJsonDocument doc = QJsonDocument::fromJson(process.stdOut().toUtf8());

if (doc.isObject())
{
QString cleanDrivePath = QDir::fromNativeSeparators(QDir::cleanPath(drivePath));

for (const QJsonValue &val : doc.object().value(QStringLiteral("blockdevices")).toArray())
{
QJsonObject obj = val.toObject();
QString serialNumber = obj.value(QStringLiteral("serial")).toString();
QString mountPoint = QDir::fromNativeSeparators(QDir::cleanPath(obj.value(QStringLiteral("mountpoint")).toString()));

if (mountPoint == cleanDrivePath)
{
return serialNumber;
}

for (const QJsonValue &childVal : obj.value(QStringLiteral("children")).toArray())
{
QJsonObject childObj = childVal.toObject();
QString childSerialNumber = childObj.value(QStringLiteral("serial")).toString();
QString childMountPoint = QDir::fromNativeSeparators(QDir::cleanPath(childObj.value(QStringLiteral("mountpoint")).toString()));

if (childMountPoint == cleanDrivePath)
{
return childSerialNumber.isEmpty() ? serialNumber : childSerialNumber;
}
}
}
}
}
#elif defined(Q_OS_MAC)
#endif

Expand Down

0 comments on commit db98bc4

Please sign in to comment.