From eb1053adb52785aa02992690d5444d4068e0e780 Mon Sep 17 00:00:00 2001 From: luoqing Date: Wed, 17 Jan 2024 16:32:34 +0800 Subject: [PATCH 5/6] fix(*):When the driver is enabled, the device is scanned and the corresponding device object is created.When the driver is disabled,the corresponding device object is released. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 启用驱动时,扫描当前设备,创建对应的设备对象;禁用驱动时,释放当前对应的设备对象 Close #25387 --- include/auth-enum.h | 15 +++- src/auth-device-manager.cpp | 69 +++++++++++++------ src/auth-device-manager.h | 1 + src/config-helper.cpp | 48 +++++++++++++ src/config-helper.h | 6 +- src/device/device-creator.cpp | 1 + src/device/finger-vein/fv-sd-device.cpp | 1 + src/device/fingerprint/fp-zk-device.cpp | 1 + .../multi-function/mf-iristar-device.cpp | 2 + src/device/ukey/ukey-skf-device.cpp | 1 + src/utils.cpp | 24 +++++++ src/utils.h | 4 ++ 12 files changed, 149 insertions(+), 24 deletions(-) diff --git a/include/auth-enum.h b/include/auth-enum.h index b9e9e49..1b663cd 100644 --- a/include/auth-enum.h +++ b/include/auth-enum.h @@ -26,7 +26,7 @@ namespace Kiran #define UKEY_CONTAINER_NAME "1003-3001" #define UKEY_SKF_DRIVER_NAME "ukey-skf" -#define IRISTAR_DRIVER_NAME "irs_sdk2" +#define IRISTAR_DRIVER_NAME "irs_sdk2" #define FINGERPRINT_ZK_DRIVER_NAME "zkfp" #define FINGER_VEIN_SD_DRIVER_NAME "sdfv" @@ -55,6 +55,19 @@ struct DeviceInfo return false; }; + + bool operator==(const DeviceInfo& dev) const + { + if (this->idVendor == dev.idVendor && + this->idProduct == dev.idProduct) + { + return true; + } + else + { + return false; + } + } }; enum GeneralResult diff --git a/src/auth-device-manager.cpp b/src/auth-device-manager.cpp index a9f5fb0..481caf3 100644 --- a/src/auth-device-manager.cpp +++ b/src/auth-device-manager.cpp @@ -183,43 +183,53 @@ void AuthDeviceManager::onRemove(const QDBusMessage& message, const QString& fea // TODO:是否需要监听配置文件的改变 void AuthDeviceManager::onSetEnableDriver(const QDBusMessage& message, const QString& driver_name, bool enable) { + KLOG_DEBUG() << "set driver name:" << driver_name << "enable:" << enable; QStringList driverList = ConfigHelper::getDriverList(); QDBusMessage replyMessage; - do + if (!driverList.contains(driver_name)) { - if (!driverList.contains(driver_name)) - { - replyMessage = message.createErrorReply(QDBusError::Failed, "No driver with the corresponding name was found."); - break; - } - ConfigHelper::setDriverEnabled(driver_name, enable); - replyMessage = message.createReply(); + replyMessage = message.createErrorReply(QDBusError::Failed, "No driver with the corresponding name was found."); + QDBusConnection::systemBus().send(replyMessage); + return; + } + + bool oldDriverStatus = ConfigHelper::driverEnabled(driver_name); - if (enable) + ConfigHelper::setDriverEnabled(driver_name, enable); + replyMessage = message.createReply(); + QDBusConnection::systemBus().send(replyMessage); + + // 若之前驱动已经关闭,启用驱动后,遍历设备,加载可用的设备 + if (enable && !oldDriverStatus) + { + //NOTE:注意,此处是从配置文件中读出所支持的Vid和Pid,获取不到所支持的设备的BusPath,因此这里返回的DeviceInfo的BusPath为空 + QList devices = ConfigHelper::getDeviceIDsSupportedByDriver(driver_name); + for (auto device : devices) { - break; + if(!Utils::isExistDevice(device.idVendor,device.idProduct)) + { + continue; + } + device.busPath = Utils::getBusPath(device.idVendor,device.idProduct); + handleDeviceAdded(device); } + return; + } - // 驱动被禁用,将当前正在使用的设备释放掉 + // 若之前驱动开启,现在关闭驱动,将当前正在使用的设备释放掉 + if (!enable && oldDriverStatus) + { auto devices = m_deviceMap.values(); - Q_FOREACH (AuthDevicePtr device, devices) + for (AuthDevicePtr device : devices) { if (device->driverName() != driver_name) { continue; } - QString deviceID = device->deviceID(); - int deviceType = device->deviceType(); - device->deleteLater(); - QString key = m_deviceMap.key(device); - m_deviceMap.remove(key); - Q_EMIT m_dbusAdaptor->DeviceDeleted(deviceType, deviceID); - KLOG_INFO() << QString("destroyed deviceType: %1, deviceID:%2").arg(deviceType).arg(deviceID); + removeDevice(device); } - } while (false); - - QDBusConnection::systemBus().send(replyMessage); + } } AuthDeviceList AuthDeviceManager::createDevices(const DeviceInfo& deviceInfo) @@ -258,6 +268,21 @@ AuthDeviceList AuthDeviceManager::createDevices(const DeviceInfo& deviceInfo) return deviceList; } +void AuthDeviceManager::removeDevice(QSharedPointer device) +{ + QString deviceID = device->deviceID(); + int deviceType = device->deviceType(); + QString deviceName = device->driverName(); + + QString key = m_deviceMap.key(device); + int count = m_deviceMap.remove(key); + KLOG_DEBUG() << "remove device count:" << count; + + device->deleteLater(); + Q_EMIT m_dbusAdaptor->DeviceDeleted(deviceType, deviceID); + KLOG_INFO() << QString("destroyed deviceName: %1 , bus path : %2 , deviceType: %3, deviceID:%4").arg(deviceName).arg(key).arg(deviceType).arg(deviceID); +} + CHECK_AUTH_WITH_1ARGS(AuthDeviceManager, Remove, onRemove, AUTH_USER_ADMIN, const QString&) CHECK_AUTH_WITH_2ARGS(AuthDeviceManager, SetEnableDriver, onSetEnableDriver, AUTH_USER_ADMIN, const QString&, bool) diff --git a/src/auth-device-manager.h b/src/auth-device-manager.h index 95e68e9..150a372 100644 --- a/src/auth-device-manager.h +++ b/src/auth-device-manager.h @@ -62,6 +62,7 @@ private: void onRemove(const QDBusMessage &message, const QString &feature_id); void onSetEnableDriver(const QDBusMessage &message, const QString &driver_name, bool enable); QList> createDevices(const DeviceInfo &deviceInfo); + void removeDevice(QSharedPointer device); private: static AuthDeviceManager *m_instance; diff --git a/src/config-helper.cpp b/src/config-helper.cpp index 8fc5a47..880296b 100644 --- a/src/config-helper.cpp +++ b/src/config-helper.cpp @@ -113,6 +113,18 @@ bool ConfigHelper::driverEnabled(const QString &vid, const QString &pid) return enable; } +bool ConfigHelper::driverEnabled(const QString &driverName) +{ + bool enable = false; + QSettings confSettings(DRIVER_CONF, QSettings::NativeFormat); + QStringList driverList = confSettings.childGroups(); + if (driverList.contains(driverName)) + { + enable = confSettings.value(QString("%1/Enable").arg(driverName)).toBool(); + } + return enable; +} + void ConfigHelper::setDriverEnabled(const QString &driverName, bool enable) { QSettings confSettings(DRIVER_CONF, QSettings::NativeFormat); @@ -147,4 +159,40 @@ bool ConfigHelper::isDeviceSupported(const QString &vid, const QString &pid) return false; } +QList ConfigHelper::getDeviceIDsSupportedByDriver(const QString &driverName) +{ + QList deviceInfos; + + QSettings confSettings(DEVICE_CONF, QSettings::NativeFormat); + QStringList deviceList = confSettings.childGroups(); + for (auto deviceConf : deviceList) + { + confSettings.beginGroup(deviceConf); + if (confSettings.value("Driver").toString() != driverName) + { + confSettings.endGroup(); + continue; + } + + QStringList idList = confSettings.value("Id").toStringList(); + for (const QString &id : idList) + { + QStringList idItems = id.split(":"); + if (idItems.count() != 2) + { + continue; + } + + DeviceInfo deviceinfo; + deviceinfo.idVendor = idItems.value(0); + deviceinfo.idProduct = idItems.value(1); + + deviceInfos << deviceinfo; + } + confSettings.endGroup(); + } + + return deviceInfos; +} + } // namespace Kiran \ No newline at end of file diff --git a/src/config-helper.h b/src/config-helper.h index 91cf895..0e1a5f5 100644 --- a/src/config-helper.h +++ b/src/config-helper.h @@ -13,6 +13,7 @@ */ #pragma once #include +#include "auth-enum.h" namespace Kiran { @@ -50,9 +51,12 @@ public: static QStringList getDriverList(); static bool driverEnabled(const QString &vid, const QString &pid); + static bool driverEnabled(const QString & driverName); + static void setDriverEnabled(const QString& driverName, bool enable); static bool isDeviceSupported(const QString &vid, const QString &pid); - + + static QList getDeviceIDsSupportedByDriver(const QString& driverName); private: }; diff --git a/src/device/device-creator.cpp b/src/device/device-creator.cpp index 1fb760f..90ea201 100644 --- a/src/device/device-creator.cpp +++ b/src/device/device-creator.cpp @@ -45,6 +45,7 @@ DeviceCereator::~DeviceCereator() { } +//TODO:将设备的BusPath作为入參创建设备,处理存在多个一模一样(vid和pid相同)的设备同时存在的场景 AuthDeviceList DeviceCereator::createDevices(const QString &vid, const QString &pid, DriverPtr driver) { AuthDeviceList deviceList; diff --git a/src/device/finger-vein/fv-sd-device.cpp b/src/device/finger-vein/fv-sd-device.cpp index 442f227..80ca4be 100644 --- a/src/device/finger-vein/fv-sd-device.cpp +++ b/src/device/finger-vein/fv-sd-device.cpp @@ -42,6 +42,7 @@ FVSDDevice::FVSDDevice(const QString &vid, const QString &pid, DriverPtr driver, FVSDDevice::~FVSDDevice() { + KLOG_DEBUG() << "destroy FVSD Device"; if (m_driver->isLoaded()) { acquireFeatureStop(); diff --git a/src/device/fingerprint/fp-zk-device.cpp b/src/device/fingerprint/fp-zk-device.cpp index 3a818f3..659e9ab 100644 --- a/src/device/fingerprint/fp-zk-device.cpp +++ b/src/device/fingerprint/fp-zk-device.cpp @@ -49,6 +49,7 @@ FPZKDevice::FPZKDevice(const QString& vid, const QString& pid, DriverPtr driver, // 析构时对设备进行资源回收 FPZKDevice::~FPZKDevice() { + KLOG_DEBUG() << "destroy FPZK Device"; acquireFeatureStop(); if (m_driver->isLoaded()) diff --git a/src/device/multi-function/mf-iristar-device.cpp b/src/device/multi-function/mf-iristar-device.cpp index 27e1809..58ed89c 100644 --- a/src/device/multi-function/mf-iristar-device.cpp +++ b/src/device/multi-function/mf-iristar-device.cpp @@ -33,6 +33,7 @@ MFIriStarDevice::MFIriStarDevice(const QString &vid, const QString &pid, DriverP m_driver = driver.dynamicCast(); m_driver->ref(); m_driver->setDeviceInfo(vid,pid); + setDriverName(IRISTAR_DRIVER_NAME); qRegisterMetaType("EnrollProcess"); qRegisterMetaType("IdentifyProcess"); @@ -44,6 +45,7 @@ MFIriStarDevice::MFIriStarDevice(const QString &vid, const QString &pid, DriverP MFIriStarDevice::~MFIriStarDevice() { + KLOG_DEBUG() << "destroy MFIriStar Device"; m_driver->unref(); if (m_driver->refCount() <= 0) { diff --git a/src/device/ukey/ukey-skf-device.cpp b/src/device/ukey/ukey-skf-device.cpp index 82bd13d..c8feb7b 100644 --- a/src/device/ukey/ukey-skf-device.cpp +++ b/src/device/ukey/ukey-skf-device.cpp @@ -51,6 +51,7 @@ UKeySKFDevice::UKeySKFDevice(const QString &vid, const QString &pid, DriverPtr d UKeySKFDevice::~UKeySKFDevice() { + KLOG_DEBUG() << "destroy UKey SKF Device"; int index = m_existingSerialNumber.indexOf(deviceSerialNumber()); m_existingSerialNumber.removeAt(index); KLOG_DEBUG() << "destory device, serialNumber:" << deviceSerialNumber(); diff --git a/src/utils.cpp b/src/utils.cpp index 3061263..0e11b04 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -101,5 +101,29 @@ QJsonValue getValueFromJsonString(const QString& json, const QString& key) return jsonObject.value(key); } +bool isExistDevice(const QString& idVendor, const QString& idProduct) +{ + DeviceInfo deviceInfo; + deviceInfo.idVendor = idVendor; + deviceInfo.idProduct = idProduct; + deviceInfo.busPath = ""; + QList devices = enumerateDevices(); + return devices.contains(deviceInfo); +} + +QString getBusPath(const QString& idVendor, const QString& idProduct) +{ + QList devices = enumerateDevices(); + for (auto device : devices) + { + if (device.idVendor == idVendor && + device.idProduct == idProduct) + { + return device.busPath; + } + } + return QString(); +} + } // namespace Utils } // namespace Kiran \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index 1c28065..9ba5598 100644 --- a/src/utils.h +++ b/src/utils.h @@ -28,5 +28,9 @@ QString getDeviceName(const QString& idVendor, const QString& idProduct); QJsonValue getValueFromJsonString(const QString& json, const QString& key); +bool isExistDevice(const QString& idVendor, const QString& idProduct); + +QString getBusPath(const QString& idVendor, const QString& idProduct); + } // namespace Utils } // namespace Kiran -- 2.33.0