diff options
-rw-r--r-- | smartpenmanager.cc | 23 | ||||
-rw-r--r-- | smartpenmanager.h | 1 |
2 files changed, 18 insertions, 6 deletions
diff --git a/smartpenmanager.cc b/smartpenmanager.cc index 1eeb685..d375fea 100644 --- a/smartpenmanager.cc +++ b/smartpenmanager.cc @@ -197,9 +197,8 @@ void SmartpenManager::handleTimerNextTry() void SmartpenManager::processDeviceAdded(udev_device *dev) { - uint busnum = atol(udev_device_get_sysattr_value(dev, "busnum")); - uint devnum = atol(udev_device_get_sysattr_value(dev, "devnum")); - Smartpen::Address addr(busnum, devnum); + Smartpen::Address addr = getDeviceAddress(dev); + if (!addr.first && !addr.second) return; QString name = parseUdevEscapedString(udev_device_get_property_value(dev, "ID_MODEL_ENC")); @@ -223,9 +222,8 @@ void SmartpenManager::processDeviceAdded(udev_device *dev) void SmartpenManager::processDeviceRemoved(udev_device *dev) { - uint busnum = atol(udev_device_get_sysattr_value(dev, "busnum")); - uint devnum = atol(udev_device_get_sysattr_value(dev, "devnum")); - Smartpen::Address addr(busnum, devnum); + Smartpen::Address addr = getDeviceAddress(dev); + if (!addr.first && !addr.second) return; qDebug() << "Device removed with address:" << addr; @@ -325,3 +323,16 @@ QString SmartpenManager::parseUdevEscapedString(const char *s) } return r; } + +Smartpen::Address SmartpenManager::getDeviceAddress(udev_device *dev) +{ + const char *busnumS = udev_device_get_property_value(dev, "BUSNUM"); + const char *devnumS = udev_device_get_property_value(dev, "DEVNUM"); + if (!busnumS || !devnumS) { + qWarning() << "Cannot find busnum/devnum env var for udev device"; + return Smartpen::Address(); + } + uint busnum = atol(busnumS); + uint devnum = atol(devnumS); + return Smartpen::Address(busnum, devnum); +} diff --git a/smartpenmanager.h b/smartpenmanager.h index a823709..6a5e39f 100644 --- a/smartpenmanager.h +++ b/smartpenmanager.h @@ -61,6 +61,7 @@ private: void scheduleNextTry(); static QString parseUdevEscapedString(const char *s); + static Smartpen::Address getDeviceAddress(udev_device *dev); private: udev *_udev; |