From a2bb2ccf10119a705b350e0b849ab3dce87a2ba5 Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Wed, 19 Aug 2020 20:55:54 +0200 Subject: [PATCH] fixed segfault --- onewire/integrationpluginonewire.cpp | 32 ++++++++++++++++--------- onewire/w1.cpp | 36 +++++++++++++--------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/onewire/integrationpluginonewire.cpp b/onewire/integrationpluginonewire.cpp index e10ed33a..a423bde2 100644 --- a/onewire/integrationpluginonewire.cpp +++ b/onewire/integrationpluginonewire.cpp @@ -139,9 +139,12 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) } connect(m_owfsInterface, &Owfs::devicesDiscovered, this, &IntegrationPluginOneWire::onOneWireDevicesDiscovered); return info->finish(Thing::ThingErrorNoError); - } - if (thing->thingClassId() == temperatureSensorThingClassId) { + } else if (thing->thingClassId() == temperatureSensorThingClassId) { + + if (!m_w1Interface) { + m_w1Interface = new W1(this); + } qCDebug(dcOneWire) << "Setup one wire temperature sensor" << thing->params(); if (m_owfsInterface) { //in case the child was setup before the interface @@ -155,18 +158,16 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) } else { return info->finish(Thing::ThingErrorHardwareNotAvailable, tr("No 1-Wire interface available")); } - } - if (thing->thingClassId() == singleChannelSwitchThingClassId) { + } else if (thing->thingClassId() == singleChannelSwitchThingClassId) { qCDebug(dcOneWire) << "Setup one wire switch" << thing->params(); if (!m_owfsInterface) { QByteArray address = thing->paramValue(singleChannelSwitchThingAddressParamTypeId).toByteArray(); thing->setStateValue(singleChannelSwitchDigitalOutputStateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_A)); } return info->finish(Thing::ThingErrorNoError); - } - if (thing->thingClassId() == dualChannelSwitchThingClassId) { + } else if (thing->thingClassId() == dualChannelSwitchThingClassId) { qCDebug(dcOneWire) << "Setup one wire dual switch" << thing->params(); if (!m_owfsInterface) { QByteArray address = thing->paramValue(dualChannelSwitchThingAddressParamTypeId).toByteArray(); @@ -174,9 +175,8 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) thing->setStateValue(dualChannelSwitchDigitalOutput2StateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_B)); } return info->finish(Thing::ThingErrorNoError); - } - if (thing->thingClassId() == eightChannelSwitchThingClassId) { + } else if (thing->thingClassId() == eightChannelSwitchThingClassId) { qCDebug(dcOneWire) << "Setup one wire eight channel switch" << thing->params(); if (!m_owfsInterface) { QByteArray address = thing->paramValue(eightChannelSwitchThingAddressParamTypeId).toByteArray(); @@ -190,8 +190,9 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) thing->setStateValue(eightChannelSwitchDigitalOutput8StateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_H)); } return info->finish(Thing::ThingErrorNoError); + } else { + return info->finish(Thing::ThingErrorThingNotFound); } - return info->finish(Thing::ThingErrorThingNotFound); } void IntegrationPluginOneWire::postSetupThing(Thing *thing) @@ -285,6 +286,11 @@ void IntegrationPluginOneWire::thingRemoved(Thing *thing) return; } + if (myThings().filterByThingClassId(temperatureSensorThingClassId).isEmpty()) { + m_w1Interface->deleteLater(); + m_w1Interface = nullptr; + } + if (myThings().empty()) { hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer); m_pluginTimer = nullptr; @@ -305,8 +311,12 @@ void IntegrationPluginOneWire::onPluginTimer() if (thing->thingClassId() == temperatureSensorThingClassId) { QByteArray address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray(); - - double temperature = m_owfsInterface->getTemperature(address); + double temperature = 0; + if (m_owfsInterface) { + temperature = m_owfsInterface->getTemperature(address); + } else if (m_w1Interface) { + temperature = m_w1Interface->getTemperature(address); + } thing->setStateValue(temperatureSensorTemperatureStateTypeId, temperature); } diff --git a/onewire/w1.cpp b/onewire/w1.cpp index 3722e181..1d93d991 100644 --- a/onewire/w1.cpp +++ b/onewire/w1.cpp @@ -46,25 +46,16 @@ QStringList W1::discoverDevices() qCDebug(dcOneWire()) << "W1 kernel not loaded"; return deviceList; } - w1SysFSDir.setFilter(QDir::Dirs | QDir::NoSymLinks); + w1SysFSDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); w1SysFSDir.setSorting(QDir::Name); QFileInfoList list = w1SysFSDir.entryInfoList(); for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); - qCDebug(dcOneWire()) << "Found W1 bus master" << fileInfo.fileName() << fileInfo.filePath(); - m_w1BusMasters.append(QDir(fileInfo.filePath())); - } - - Q_FOREACH(QDir busMaster, m_w1BusMasters) { - busMaster.setFilter(QDir::Dirs | QDir::NoSymLinks); - busMaster.setSorting(QDir::Name); - QFileInfoList list = busMaster.entryInfoList(); - for (int i = 0; i < list.size(); ++i) { - QFileInfo fileInfo = list.at(i); + if(fileInfo.fileName()[2] == '-') { + qCDebug(dcOneWire()) << "Found one wire device" << fileInfo.filePath(); deviceList.append(fileInfo.fileName()); } - } return deviceList; } @@ -77,15 +68,20 @@ bool W1::interfaceIsAvailable() double W1::getTemperature(const QString &address) { - Q_FOREACH(QDir busMaster, m_w1BusMasters) { - QDir temperatureSensor(busMaster.dirName()+address); - if (temperatureSensor.exists()) { - qCDebug(dcOneWire()) << "Temperature" << address; - QFile temperature(temperatureSensor.dirName()+"/temperature"); - if (!temperature.open(QIODevice::ReadOnly | QIODevice::Text)) - return 0; - return temperature.readLine().toInt()/1000.00; + QDir temperatureSensor("/sys/bus/w1/devices/"+address); + if (temperatureSensor.exists()) { + qCDebug(dcOneWire()) << "Temperature" << address; + QFile temperature(temperatureSensor.dirName()+"/temperature"); + if (!temperature.exists()) { + qCWarning(dcOneWire()) << "Directory doesn't exist" << temperature.fileName(); } + if (!temperature.open(QIODevice::ReadOnly | QIODevice::Text)){ + qCWarning(dcOneWire()) << "Could not open file" << temperature.fileName(); + return 0; + } + return temperature.readLine().toInt()/1000.00; + } else { + qCWarning(dcOneWire()) << "Could not find device" << temperatureSensor; } return 0; }