Add hardwaremanager dbus object

This commit is contained in:
Simon Stürz 2017-11-28 12:55:09 +01:00 committed by Michael Zanetti
parent 691473bcb1
commit daf8d0f638
8 changed files with 185 additions and 141 deletions

View File

@ -91,15 +91,14 @@ void BluetoothLowEnergyDevice::onServiceDiscoveryFinished()
{
qCDebug(dcBluetooth()) << "Service discovery finished for" << name() << address().toString();
foreach (const QBluetoothUuid &serviceUuid, m_controller->services()) {
QLowEnergyService *service = m_controller->createServiceObject(serviceUuid, this);
qCDebug(dcBluetooth()) << "--> Service" << serviceUuid.toString();
m_services.append(service);
}
emit servicesDiscoveryFinished();
}
void BluetoothLowEnergyDevice::onStateChanged(const QLowEnergyController::ControllerState &state)
{
qCDebug(dcBluetooth()) << "State changed for" << name() << address().toString() << state;
emit stateChanged(state);
}
@ -139,11 +138,6 @@ bool BluetoothLowEnergyDevice::discovered() const
return m_discovered;
}
QList<QLowEnergyService *> BluetoothLowEnergyDevice::services() const
{
return m_services;
}
QList<QBluetoothUuid> BluetoothLowEnergyDevice::serviceUuids() const
{
return m_controller->services();

View File

@ -50,20 +50,17 @@ public:
bool connected() const;
bool discovered() const;
QList<QLowEnergyService *> services() const;
QList<QBluetoothUuid> serviceUuids() const;
protected:
QLowEnergyController *controller() const;
private:
explicit BluetoothLowEnergyDevice(const QBluetoothDeviceInfo &deviceInfo, const QLowEnergyController::RemoteAddressType &addressType = QLowEnergyController::PublicAddress, QObject *parent = 0);
QBluetoothDeviceInfo m_deviceInfo;
QLowEnergyController *m_controller = nullptr;
QList<QLowEnergyService *> m_services;
// Default enabled and auto connecting
bool m_connected = false;
bool m_autoConnecting = false;
bool m_autoConnecting = true;
bool m_discovered = false;
bool m_enabled = true;

View File

@ -59,14 +59,14 @@ BluetoothDiscoveryReply *BluetoothLowEnergyManager::discoverDevices(const int &i
// Prevent blocking the hardware resource from plugins
int finalInterval = interval;
if (finalInterval > 30) {
if (finalInterval > 30000) {
qCWarning(dcBluetooth()) << "Discovery interval out of range. Reset to 30 seconds.";
finalInterval = 30;
finalInterval = 30000;
}
if (finalInterval <= 0) {
qCWarning(dcBluetooth()) << "Discovery interval out of range. Reset to 5 seconds.";
finalInterval = 5;
finalInterval = 5000;
}
m_timer->start(finalInterval);
@ -75,9 +75,10 @@ BluetoothDiscoveryReply *BluetoothLowEnergyManager::discoverDevices(const int &i
BluetoothLowEnergyDevice *BluetoothLowEnergyManager::registerDevice(const QBluetoothDeviceInfo &deviceInfo, const QLowEnergyController::RemoteAddressType &addressType)
{
QPointer<BluetoothLowEnergyDevice> device = new BluetoothLowEnergyDevice(deviceInfo, addressType, this);
m_devices.append(device);
return device.data();
QPointer<BluetoothLowEnergyDevice> bluetoothDevice = new BluetoothLowEnergyDevice(deviceInfo, addressType, this);
qCDebug(dcBluetooth()) << "Register device" << bluetoothDevice->name() << bluetoothDevice->address().toString();
m_devices.append(bluetoothDevice);
return bluetoothDevice.data();
}
void BluetoothLowEnergyManager::unregisterDevice(BluetoothLowEnergyDevice *bluetoothDevice)
@ -88,6 +89,8 @@ void BluetoothLowEnergyManager::unregisterDevice(BluetoothLowEnergyDevice *bluet
return;
}
qCDebug(dcBluetooth()) << "Unregister device" << bluetoothDevice->name() << bluetoothDevice->address().toString();
foreach (QPointer<BluetoothLowEnergyDevice> dPointer, m_devices) {
if (devicePointer.data() == dPointer.data()) {
m_devices.removeAll(dPointer);
@ -96,8 +99,9 @@ void BluetoothLowEnergyManager::unregisterDevice(BluetoothLowEnergyDevice *bluet
}
}
BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
HardwareResource(HardwareResource::TypeBluetoothLE, "Bluetooth LE manager", parent)
BluetoothLowEnergyManager::BluetoothLowEnergyManager(PluginTimer *reconnectTimer, QObject *parent) :
HardwareResource(HardwareResource::TypeBluetoothLE, "Bluetooth LE manager", parent),
m_reconnectTimer(reconnectTimer)
{
// Check which bluetooth adapter are available
QList<QBluetoothHostInfo> bluetoothAdapters = QBluetoothLocalDevice::allDevices();
@ -124,10 +128,23 @@ BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
m_timer->setSingleShot(true);
connect(m_timer, &QTimer::timeout, this, &BluetoothLowEnergyManager::onDiscoveryTimeout);
// Reconnect timer
connect(m_reconnectTimer, &PluginTimer::timeout, this, &BluetoothLowEnergyManager::onReconnectTimeout);
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
setAvailable(true);
}
void BluetoothLowEnergyManager::onReconnectTimeout()
{
// Reconnect device if enabled and disconnected
foreach (BluetoothLowEnergyDevice *device, m_devices) {
if (device->autoConnecting() && !device->connected()) {
device->connectDevice();
}
}
}
void BluetoothLowEnergyManager::onDiscoveryTimeout()
{
// Stop discovery on all adapters
@ -161,8 +178,9 @@ void BluetoothLowEnergyManager::onDeviceDiscovered(const QBluetoothDeviceInfo &d
}
}
if (!alreadyAdded) {
qCDebug(dcBluetooth()) << "device discovered" << deviceInfo.name() <<deviceInfo.address().toString();
// Note: only show low energy devices
if (!alreadyAdded && deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
qCDebug(dcBluetooth()) << "device discovered" << deviceInfo.name() << deviceInfo.address().toString();
m_discoveredDevices.append(deviceInfo);
}
}

View File

@ -30,6 +30,7 @@
#include <QBluetoothLocalDevice>
#include <QBluetoothDeviceDiscoveryAgent>
#include "plugintimer.h"
#include "hardwareresource.h"
#include "bluetoothdiscoveryreply.h"
#include "bluetoothlowenergydevice.h"
@ -48,7 +49,8 @@ public:
void unregisterDevice(BluetoothLowEnergyDevice *bluetoothDevice);
private:
explicit BluetoothLowEnergyManager(QObject *parent = nullptr);
explicit BluetoothLowEnergyManager(PluginTimer *reconnectTimer, QObject *parent = nullptr);
PluginTimer *m_reconnectTimer = nullptr;
QTimer *m_timer = nullptr;
QList<QPointer<BluetoothLowEnergyDevice>> m_devices;
@ -57,9 +59,9 @@ private:
QPointer<BluetoothDiscoveryReply> m_currentReply;
private slots:
void onReconnectTimeout();
void onDeviceDiscovered(const QBluetoothDeviceInfo &deviceInfo);
void onDiscoveryError(const QBluetoothDeviceDiscoveryAgent::Error &error);
void onDiscoveryTimeout();
public slots:

View File

@ -21,7 +21,6 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "hardwaremanager.h"
#include "plugintimer.h"
#include "loggingcategories.h"
#include "hardware/radio433/radio433.h"
@ -63,12 +62,26 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
m_avahiBrowser->enable();
// Bluetooth LE
m_bluetoothLowEnergyManager = new BluetoothLowEnergyManager(this);
m_bluetoothLowEnergyManager = new BluetoothLowEnergyManager(m_pluginTimerManager->registerTimer(10), this);
m_hardwareResources.append(m_bluetoothLowEnergyManager);
if (m_networkManager->available())
m_networkManager->enable();
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
// Register D-Bus interface for enable/disable hardware resources
bool status = QDBusConnection::systemBus().registerService("io.guh.nymead");
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus service. HardwareManager D-Bus control will not work.";
return;
}
status = QDBusConnection::systemBus().registerObject("/io/guh/nymead/HardwareManager", this, QDBusConnection::ExportScriptableContents);
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus object. HardwareManager D-Bus control will not work.";
return;
}
qCDebug(dcHardware()) << "HardwareManager D-Bus service set up.";
}
Radio433 *HardwareManager::radio433()
@ -118,9 +131,21 @@ bool HardwareManager::isEnabled(const HardwareResource::Type &hardwareResourceTy
return true;
}
}
return false;
}
void HardwareManager::EnableBluetooth(const bool &enabled)
{
qCDebug(dcHardware()) << "Bluetooth hardware resource" << (enabled ? "enabled" : "disabled");
if (enabled) {
m_bluetoothLowEnergyManager->enable();
} else {
m_bluetoothLowEnergyManager->disable();
}
}
bool HardwareManager::enableHardwareReource(const HardwareResource::Type &hardwareResourceType)
{
foreach (HardwareResource *resource, m_hardwareResources) {

View File

@ -24,6 +24,7 @@
#define HARDWAREMANAGER_H
#include <QObject>
#include <QDBusConnection>
#include <QNetworkAccessManager>
#include "hardwareresource.h"
@ -39,6 +40,7 @@ class QtAvahiServiceBrowser;
class HardwareManager : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "io.guh.nymead")
friend class DeviceManager;
@ -55,6 +57,9 @@ public:
bool isAvailable(const HardwareResource::Type &hardwareResourceType) const;
bool isEnabled(const HardwareResource::Type &hardwareResourceType) const;
// D-Bus method for enable/disable bluetooth support
Q_SCRIPTABLE void EnableBluetooth(const bool &enabled);
private:
QList<HardwareResource *> m_hardwareResources;

View File

@ -15,122 +15,122 @@ INSTALLS += target
LIBS += -lavahi-common -lavahi-client
HEADERS += devicemanager.h \
libguh.h \
typeutils.h \
loggingcategories.h \
guhsettings.h \
plugin/device.h \
plugin/deviceclass.h \
plugin/deviceplugin.h \
plugin/devicedescriptor.h \
plugin/devicepairinginfo.h \
hardware/gpio.h \
hardware/gpiomonitor.h \
hardware/pwm.h \
hardware/radio433/radio433.h \
hardware/radio433/radio433transmitter.h \
hardware/radio433/radio433brennenstuhlgateway.h \
network/upnp/upnpdiscovery.h \
network/upnp/upnpdevice.h \
network/upnp/upnpdevicedescriptor.h \
network/upnp/upnpdiscoveryrequest.h \
network/networkaccessmanager.h \
network/oauth2.h \
network/avahi/qt-watch.h \
network/avahi/avahiserviceentry.h \
network/avahi/qtavahiclient.h \
network/avahi/qtavahiservice.h \
network/avahi/qtavahiservice_p.h \
network/avahi/qtavahiservicebrowser.h \
network/avahi/qtavahiservicebrowser_p.h \
bluetooth/bluetoothlowenergydevice.h \
coap/coap.h \
coap/coappdu.h \
coap/coapoption.h \
coap/coaprequest.h \
coap/coapreply.h \
coap/coappdublock.h \
coap/corelinkparser.h \
coap/corelink.h \
coap/coapobserveresource.h \
types/action.h \
types/actiontype.h \
types/state.h \
types/statetype.h \
types/eventtype.h \
types/event.h \
types/eventdescriptor.h \
types/vendor.h \
types/paramtype.h \
types/param.h \
types/paramdescriptor.h \
types/ruleaction.h \
types/ruleactionparam.h \
types/statedescriptor.h \
hardwareresource.h \
plugintimer.h \
hardwaremanager.h \
bluetooth/bluetoothlowenergymanager.h \
network/upnp/upnpdiscoveryreply.h \
bluetooth/bluetoothdiscoveryreply.h
libguh.h \
typeutils.h \
loggingcategories.h \
guhsettings.h \
plugin/device.h \
plugin/deviceclass.h \
plugin/deviceplugin.h \
plugin/devicedescriptor.h \
plugin/devicepairinginfo.h \
hardware/gpio.h \
hardware/gpiomonitor.h \
hardware/pwm.h \
hardware/radio433/radio433.h \
hardware/radio433/radio433transmitter.h \
hardware/radio433/radio433brennenstuhlgateway.h \
network/upnp/upnpdiscovery.h \
network/upnp/upnpdevice.h \
network/upnp/upnpdevicedescriptor.h \
network/upnp/upnpdiscoveryrequest.h \
network/upnp/upnpdiscoveryreply.h \
network/networkaccessmanager.h \
network/oauth2.h \
network/avahi/qt-watch.h \
network/avahi/avahiserviceentry.h \
network/avahi/qtavahiclient.h \
network/avahi/qtavahiservice.h \
network/avahi/qtavahiservice_p.h \
network/avahi/qtavahiservicebrowser.h \
network/avahi/qtavahiservicebrowser_p.h \
bluetooth/bluetoothlowenergydevice.h \
bluetooth/bluetoothdiscoveryreply.h \
bluetooth/bluetoothlowenergymanager.h \
coap/coap.h \
coap/coappdu.h \
coap/coapoption.h \
coap/coaprequest.h \
coap/coapreply.h \
coap/coappdublock.h \
coap/corelinkparser.h \
coap/corelink.h \
coap/coapobserveresource.h \
types/action.h \
types/actiontype.h \
types/state.h \
types/statetype.h \
types/eventtype.h \
types/event.h \
types/eventdescriptor.h \
types/vendor.h \
types/paramtype.h \
types/param.h \
types/paramdescriptor.h \
types/ruleaction.h \
types/ruleactionparam.h \
types/statedescriptor.h \
hardwareresource.h \
plugintimer.h \
hardwaremanager.h \
SOURCES += devicemanager.cpp \
loggingcategories.cpp \
guhsettings.cpp \
plugin/device.cpp \
plugin/deviceclass.cpp \
plugin/deviceplugin.cpp \
plugin/devicedescriptor.cpp \
plugin/devicepairinginfo.cpp \
hardware/gpio.cpp \
hardware/gpiomonitor.cpp \
hardware/pwm.cpp \
hardware/radio433/radio433.cpp \
hardware/radio433/radio433transmitter.cpp \
hardware/radio433/radio433brennenstuhlgateway.cpp \
network/upnp/upnpdiscovery.cpp \
network/upnp/upnpdevice.cpp \
network/upnp/upnpdevicedescriptor.cpp \
network/upnp/upnpdiscoveryrequest.cpp \
network/networkaccessmanager.cpp \
network/oauth2.cpp \
network/avahi/qt-watch.cpp \
network/avahi/avahiserviceentry.cpp \
network/avahi/qtavahiclient.cpp \
network/avahi/qtavahiservice.cpp \
network/avahi/qtavahiservice_p.cpp \
network/avahi/qtavahiservicebrowser.cpp \
network/avahi/qtavahiservicebrowser_p.cpp \
bluetooth/bluetoothlowenergydevice.cpp \
coap/coap.cpp \
coap/coappdu.cpp \
coap/coapoption.cpp \
coap/coaprequest.cpp \
coap/coapreply.cpp \
coap/coappdublock.cpp \
coap/corelinkparser.cpp \
coap/corelink.cpp \
coap/coapobserveresource.cpp \
types/action.cpp \
types/actiontype.cpp \
types/state.cpp \
types/statetype.cpp \
types/eventtype.cpp \
types/event.cpp \
types/eventdescriptor.cpp \
types/vendor.cpp \
types/paramtype.cpp \
types/param.cpp \
types/paramdescriptor.cpp \
types/ruleaction.cpp \
types/ruleactionparam.cpp \
types/statedescriptor.cpp \
hardwareresource.cpp \
plugintimer.cpp \
hardwaremanager.cpp \
bluetooth/bluetoothlowenergymanager.cpp \
network/upnp/upnpdiscoveryreply.cpp \
bluetooth/bluetoothdiscoveryreply.cpp
loggingcategories.cpp \
guhsettings.cpp \
plugin/device.cpp \
plugin/deviceclass.cpp \
plugin/deviceplugin.cpp \
plugin/devicedescriptor.cpp \
plugin/devicepairinginfo.cpp \
hardware/gpio.cpp \
hardware/gpiomonitor.cpp \
hardware/pwm.cpp \
hardware/radio433/radio433.cpp \
hardware/radio433/radio433transmitter.cpp \
hardware/radio433/radio433brennenstuhlgateway.cpp \
network/upnp/upnpdiscovery.cpp \
network/upnp/upnpdevice.cpp \
network/upnp/upnpdevicedescriptor.cpp \
network/upnp/upnpdiscoveryrequest.cpp \
network/upnp/upnpdiscoveryreply.cpp \
network/networkaccessmanager.cpp \
network/oauth2.cpp \
network/avahi/qt-watch.cpp \
network/avahi/avahiserviceentry.cpp \
network/avahi/qtavahiclient.cpp \
network/avahi/qtavahiservice.cpp \
network/avahi/qtavahiservice_p.cpp \
network/avahi/qtavahiservicebrowser.cpp \
network/avahi/qtavahiservicebrowser_p.cpp \
bluetooth/bluetoothlowenergymanager.cpp \
bluetooth/bluetoothlowenergydevice.cpp \
bluetooth/bluetoothdiscoveryreply.cpp \
coap/coap.cpp \
coap/coappdu.cpp \
coap/coapoption.cpp \
coap/coaprequest.cpp \
coap/coapreply.cpp \
coap/coappdublock.cpp \
coap/corelinkparser.cpp \
coap/corelink.cpp \
coap/coapobserveresource.cpp \
types/action.cpp \
types/actiontype.cpp \
types/state.cpp \
types/statetype.cpp \
types/eventtype.cpp \
types/event.cpp \
types/eventdescriptor.cpp \
types/vendor.cpp \
types/paramtype.cpp \
types/param.cpp \
types/paramdescriptor.cpp \
types/ruleaction.cpp \
types/ruleactionparam.cpp \
types/statedescriptor.cpp \
hardwareresource.cpp \
plugintimer.cpp \
hardwaremanager.cpp \
# install plugininfo python script for libguh-dev
generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo
@ -147,4 +147,4 @@ for(header, HEADERS) {
}
RESOURCES += \
interfaces/interfaces.qrc
interfaces/interfaces.qrc

View File

@ -116,6 +116,7 @@ void PluginTimer::stop()
PluginTimer *PluginTimerManager::registerTimer(int seconds)
{
QPointer<PluginTimer> pluginTimer = new PluginTimer(seconds, this);
qCDebug(dcHardware()) << "Register timer" << pluginTimer->interval();
// TODO: start timer depending on the schedule from other timers, loadbalancing
@ -131,6 +132,8 @@ void PluginTimerManager::unregisterTimer(PluginTimer *timer)
return;
}
qCDebug(dcHardware()) << "Unregister timer" << timer->interval();
foreach (QPointer<PluginTimer> tPointer, m_timers) {
if (timerPointer.data() == tPointer.data()) {
m_timers.removeAll(tPointer);