完成EnumDriversAndDevices函数
This commit is contained in:
parent
a669340c91
commit
f095b5836b
@ -1,11 +1,14 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "QDateTime"
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
uf = new usbFilter();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@ -13,8 +16,15 @@ MainWindow::~MainWindow()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::onPrint(QString &msg){
|
||||
void MainWindow::onPrint(const QString &msg){
|
||||
QDateTime cur = QDateTime::currentDateTime();
|
||||
QString tmp =cur.toString("yyyy-MM-dd HH:mm:ss");
|
||||
ui->textBrowser->append(tmp+" --> "+msg);
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_3_clicked()
|
||||
{
|
||||
string tmp =uf->EnumDriversAndDevices();
|
||||
this->onPrint(QString::fromStdString(tmp));
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,8 @@
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include "../usbFilter.h"
|
||||
#include "QDebug"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
@ -18,10 +19,14 @@ public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
public slots:
|
||||
void onPrint(QString &msg);
|
||||
void onPrint(const QString &msg);
|
||||
|
||||
|
||||
private slots:
|
||||
void on_pushButton_3_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
usbFilter *uf;
|
||||
};
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
@ -14,11 +14,13 @@ SOURCES += \
|
||||
mainwindow.cpp
|
||||
|
||||
HEADERS += \
|
||||
../json.hpp \
|
||||
../usbFilter.h \
|
||||
mainwindow.h
|
||||
|
||||
FORMS += \
|
||||
mainwindow.ui
|
||||
LIBS += -ludev
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#include "usbFilter.h"
|
||||
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
usbFilter::usbFilter(/* args */)
|
||||
{
|
||||
}
|
||||
@ -8,4 +10,92 @@ usbFilter::usbFilter(/* args */)
|
||||
usbFilter::~usbFilter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
string usbFilter::EnumDriversAndDevices()
|
||||
{
|
||||
json list = json::array();
|
||||
|
||||
// 1. 获取USB设备(libudev)
|
||||
{
|
||||
udev* udev_ctx = udev_new();
|
||||
if (udev_ctx) {
|
||||
udev_enumerate* enumerate = udev_enumerate_new(udev_ctx);
|
||||
udev_enumerate_add_match_subsystem(enumerate, "usb");
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
|
||||
|
||||
udev_list_entry* dev_list_entry;
|
||||
udev_list_entry_foreach(dev_list_entry, devices) {
|
||||
const char* path = udev_list_entry_get_name(dev_list_entry);
|
||||
udev_device* dev = udev_device_new_from_syspath(udev_ctx, path);
|
||||
|
||||
const char* vendor = udev_device_get_sysattr_value(dev, "idVendor");
|
||||
const char* product = udev_device_get_sysattr_value(dev, "idProduct");
|
||||
const char* devpath = udev_device_get_syspath(dev);
|
||||
const char* devname = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
|
||||
if (!devname) devname = udev_device_get_property_value(dev, "ID_MODEL");
|
||||
if (!devname) devname = udev_device_get_property_value(dev, "ID_VENDOR_FROM_DATABASE");
|
||||
if (!devname) devname = devpath ? devpath : "";
|
||||
if (vendor && product) {
|
||||
json item;
|
||||
item["VendorId"] = vendor;
|
||||
item["ProductId"] = product;
|
||||
item["DeviceName"] = devname ? devname : "";
|
||||
item["DevPath"] = devpath ? devpath : "";
|
||||
item["Subsystem"] = "USB";
|
||||
list.push_back(item);
|
||||
}
|
||||
udev_device_unref(dev);
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_unref(udev_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 获取PCI/PCIe设备(libudev)
|
||||
{
|
||||
udev* udev_ctx = udev_new();
|
||||
if (udev_ctx) {
|
||||
udev_enumerate* enumerate = udev_enumerate_new(udev_ctx);
|
||||
udev_enumerate_add_match_subsystem(enumerate, "pci");
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
|
||||
|
||||
udev_list_entry* dev_list_entry;
|
||||
udev_list_entry_foreach(dev_list_entry, devices) {
|
||||
const char* path = udev_list_entry_get_name(dev_list_entry);
|
||||
udev_device* dev = udev_device_new_from_syspath(udev_ctx, path);
|
||||
|
||||
const char* vendor = udev_device_get_sysattr_value(dev, "vendor");
|
||||
const char* product = udev_device_get_sysattr_value(dev, "device");
|
||||
// 尝试多种方式获取设备名
|
||||
const char* devpath = udev_device_get_syspath(dev);
|
||||
const char* devname = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
|
||||
if (!devname) devname = udev_device_get_property_value(dev, "ID_MODEL");
|
||||
if (!devname) devname = udev_device_get_property_value(dev, "ID_VENDOR_FROM_DATABASE");
|
||||
if (!devname) devname = devpath ? devpath : "";
|
||||
|
||||
if (vendor && product) {
|
||||
// vendor和device通常是0x开头的16进制,去掉0x
|
||||
string vendor_id = vendor;
|
||||
string product_id = product;
|
||||
if (vendor_id.find("0x") == 0) vendor_id = vendor_id.substr(2);
|
||||
if (product_id.find("0x") == 0) product_id = product_id.substr(2);
|
||||
|
||||
json item;
|
||||
item["VendorId"] = vendor_id;
|
||||
item["ProductId"] = product_id;
|
||||
item["DeviceName"] = devname ? devname : "";
|
||||
item["DevPath"] = devpath ? devpath : "";
|
||||
item["Subsystem"] = "PCI";
|
||||
list.push_back(item);
|
||||
}
|
||||
udev_device_unref(dev);
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
udev_unref(udev_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
return list.dump();
|
||||
}
|
||||
|
||||
17
usbFilter.h
17
usbFilter.h
@ -1,4 +1,17 @@
|
||||
#include "log.h"
|
||||
#include "json.hpp"
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <array>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <libudev.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json;
|
||||
|
||||
class usbFilter
|
||||
{
|
||||
@ -7,8 +20,8 @@ private:
|
||||
public:
|
||||
usbFilter(/* args */);
|
||||
~usbFilter();
|
||||
void StarSrv();
|
||||
void StartSrv();
|
||||
void StopSrv();
|
||||
|
||||
string EnumDriversAndDevices();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user