fixed segfault

This commit is contained in:
bernhard.trinnes 2020-08-19 20:55:54 +02:00
parent 3286471e50
commit a2bb2ccf10
2 changed files with 37 additions and 31 deletions

View File

@ -139,9 +139,12 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info)
} }
connect(m_owfsInterface, &Owfs::devicesDiscovered, this, &IntegrationPluginOneWire::onOneWireDevicesDiscovered); connect(m_owfsInterface, &Owfs::devicesDiscovered, this, &IntegrationPluginOneWire::onOneWireDevicesDiscovered);
return info->finish(Thing::ThingErrorNoError); 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(); qCDebug(dcOneWire) << "Setup one wire temperature sensor" << thing->params();
if (m_owfsInterface) { //in case the child was setup before the interface if (m_owfsInterface) { //in case the child was setup before the interface
@ -155,18 +158,16 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info)
} else { } else {
return info->finish(Thing::ThingErrorHardwareNotAvailable, tr("No 1-Wire interface available")); 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(); qCDebug(dcOneWire) << "Setup one wire switch" << thing->params();
if (!m_owfsInterface) { if (!m_owfsInterface) {
QByteArray address = thing->paramValue(singleChannelSwitchThingAddressParamTypeId).toByteArray(); QByteArray address = thing->paramValue(singleChannelSwitchThingAddressParamTypeId).toByteArray();
thing->setStateValue(singleChannelSwitchDigitalOutputStateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_A)); thing->setStateValue(singleChannelSwitchDigitalOutputStateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_A));
} }
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
}
if (thing->thingClassId() == dualChannelSwitchThingClassId) { } else if (thing->thingClassId() == dualChannelSwitchThingClassId) {
qCDebug(dcOneWire) << "Setup one wire dual switch" << thing->params(); qCDebug(dcOneWire) << "Setup one wire dual switch" << thing->params();
if (!m_owfsInterface) { if (!m_owfsInterface) {
QByteArray address = thing->paramValue(dualChannelSwitchThingAddressParamTypeId).toByteArray(); 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)); thing->setStateValue(dualChannelSwitchDigitalOutput2StateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_B));
} }
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
}
if (thing->thingClassId() == eightChannelSwitchThingClassId) { } else if (thing->thingClassId() == eightChannelSwitchThingClassId) {
qCDebug(dcOneWire) << "Setup one wire eight channel switch" << thing->params(); qCDebug(dcOneWire) << "Setup one wire eight channel switch" << thing->params();
if (!m_owfsInterface) { if (!m_owfsInterface) {
QByteArray address = thing->paramValue(eightChannelSwitchThingAddressParamTypeId).toByteArray(); 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)); thing->setStateValue(eightChannelSwitchDigitalOutput8StateTypeId, m_owfsInterface->getSwitchOutput(address, Owfs::SwitchChannel::PIO_H));
} }
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
} else {
return info->finish(Thing::ThingErrorThingNotFound);
} }
return info->finish(Thing::ThingErrorThingNotFound);
} }
void IntegrationPluginOneWire::postSetupThing(Thing *thing) void IntegrationPluginOneWire::postSetupThing(Thing *thing)
@ -285,6 +286,11 @@ void IntegrationPluginOneWire::thingRemoved(Thing *thing)
return; return;
} }
if (myThings().filterByThingClassId(temperatureSensorThingClassId).isEmpty()) {
m_w1Interface->deleteLater();
m_w1Interface = nullptr;
}
if (myThings().empty()) { if (myThings().empty()) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer); hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
m_pluginTimer = nullptr; m_pluginTimer = nullptr;
@ -305,8 +311,12 @@ void IntegrationPluginOneWire::onPluginTimer()
if (thing->thingClassId() == temperatureSensorThingClassId) { if (thing->thingClassId() == temperatureSensorThingClassId) {
QByteArray address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray(); QByteArray address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray();
double temperature = 0;
double temperature = m_owfsInterface->getTemperature(address); if (m_owfsInterface) {
temperature = m_owfsInterface->getTemperature(address);
} else if (m_w1Interface) {
temperature = m_w1Interface->getTemperature(address);
}
thing->setStateValue(temperatureSensorTemperatureStateTypeId, temperature); thing->setStateValue(temperatureSensorTemperatureStateTypeId, temperature);
} }

View File

@ -46,25 +46,16 @@ QStringList W1::discoverDevices()
qCDebug(dcOneWire()) << "W1 kernel not loaded"; qCDebug(dcOneWire()) << "W1 kernel not loaded";
return deviceList; return deviceList;
} }
w1SysFSDir.setFilter(QDir::Dirs | QDir::NoSymLinks); w1SysFSDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
w1SysFSDir.setSorting(QDir::Name); w1SysFSDir.setSorting(QDir::Name);
QFileInfoList list = w1SysFSDir.entryInfoList(); QFileInfoList list = w1SysFSDir.entryInfoList();
for (int i = 0; i < list.size(); ++i) { for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i); QFileInfo fileInfo = list.at(i);
qCDebug(dcOneWire()) << "Found W1 bus master" << fileInfo.fileName() << fileInfo.filePath(); if(fileInfo.fileName()[2] == '-') {
m_w1BusMasters.append(QDir(fileInfo.filePath())); qCDebug(dcOneWire()) << "Found one wire device" << 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);
deviceList.append(fileInfo.fileName()); deviceList.append(fileInfo.fileName());
} }
} }
return deviceList; return deviceList;
} }
@ -77,15 +68,20 @@ bool W1::interfaceIsAvailable()
double W1::getTemperature(const QString &address) double W1::getTemperature(const QString &address)
{ {
Q_FOREACH(QDir busMaster, m_w1BusMasters) { QDir temperatureSensor("/sys/bus/w1/devices/"+address);
QDir temperatureSensor(busMaster.dirName()+address); if (temperatureSensor.exists()) {
if (temperatureSensor.exists()) { qCDebug(dcOneWire()) << "Temperature" << address;
qCDebug(dcOneWire()) << "Temperature" << address; QFile temperature(temperatureSensor.dirName()+"/temperature");
QFile temperature(temperatureSensor.dirName()+"/temperature"); if (!temperature.exists()) {
if (!temperature.open(QIODevice::ReadOnly | QIODevice::Text)) qCWarning(dcOneWire()) << "Directory doesn't exist" << temperature.fileName();
return 0;
return temperature.readLine().toInt()/1000.00;
} }
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; return 0;
} }