From 039619ccfd1edc0aa3e438a9c84eb421e94019e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 10 Nov 2017 20:48:44 +0100 Subject: [PATCH] introduce hardwaremanager and remove BLUETOOTH_LE ifdef --- guh.pri | 5 -- guh.pro | 7 --- libguh/bluetooth/bluetoothscanner.h | 6 +-- libguh/devicemanager.cpp | 84 ++++++++++------------------- libguh/devicemanager.h | 49 ++++++----------- libguh/hardwaremanager.cpp | 59 ++++++++++++++++++++ libguh/hardwaremanager.h | 46 ++++++++++++++++ libguh/hardwareresource.cpp | 60 +++++++++++++++++++++ libguh/hardwareresource.h | 72 +++++++++++++++++++++++++ libguh/libguh.pro | 22 ++++---- libguh/plugin/deviceplugin.cpp | 36 ++++++------- libguh/plugin/deviceplugin.h | 11 +--- libguh/plugintimer.cpp | 34 ++++++++++++ libguh/plugintimer.h | 28 ++++++++++ plugins/mock/devicepluginmock.cpp | 4 +- plugins/mock/devicepluginmock.h | 2 +- plugins/plugins.pri | 7 +-- 17 files changed, 379 insertions(+), 153 deletions(-) create mode 100644 libguh/hardwaremanager.cpp create mode 100644 libguh/hardwaremanager.h create mode 100644 libguh/hardwareresource.cpp create mode 100644 libguh/hardwareresource.h create mode 100644 libguh/plugintimer.cpp create mode 100644 libguh/plugintimer.h diff --git a/guh.pri b/guh.pri index 06b0afb0..f49697c8 100644 --- a/guh.pri +++ b/guh.pri @@ -34,11 +34,6 @@ debian { QMAKE_LFLAGS *= $(shell dpkg-buildflags --get LDFLAGS) } -# Check for Bluetoot LE support (Qt >= 5.4) -equals(QT_MAJOR_VERSION, 5):greaterThan(QT_MINOR_VERSION, 3) { - DEFINES += BLUETOOTH_LE -} - # Enable coverage option coverage { # Note: this works only if you build in the source dir diff --git a/guh.pro b/guh.pro index 9d4196c8..7b43f904 100644 --- a/guh.pro +++ b/guh.pro @@ -55,13 +55,6 @@ disabletesting { SUBDIRS += tests } -# Bluetooth LE support -contains(DEFINES, BLUETOOTH_LE) { - message("Bluetooth LE enabled.") -} else { - message("Bluetooth LE disabled (Qt $${QT_VERSION} < 5.4.0).") -} - # GPIO RF 433 MHz support contains(DEFINES, GPIO433) { message("Radio 433 for GPIO's enabled") diff --git a/libguh/bluetooth/bluetoothscanner.h b/libguh/bluetooth/bluetoothscanner.h index ede9bb01..a0dc68d7 100644 --- a/libguh/bluetooth/bluetoothscanner.h +++ b/libguh/bluetooth/bluetoothscanner.h @@ -20,8 +20,8 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifndef BLUETOOTHLE_H -#define BLUETOOTHLE_H +#ifndef BLUETOOTHSCANNER_H +#define BLUETOOTHSCANNER_H #include #include @@ -61,4 +61,4 @@ private slots: void discoveryTimeout(); }; -#endif // BLUETOOTHLE_H +#endif // BLUETOOTHSCANNER_H diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 0e331e0f..b4ee4276 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -34,22 +34,22 @@ \l{DevicePlugin}{device plugins}. */ -/*! \enum DeviceManager::HardwareResource +/*! \enum HardwareResource::Type This enum type specifies hardware resources which can be requested by \l{DevicePlugin}{DevicePlugins}. - \value HardwareResourceNone + \value HardwareResource::TypeNone No Resource required. - \value HardwareResourceRadio433 + \value HardwareResource::TypeRadio433 Refers to the 433 MHz radio. - \value HardwareResourceTimer + \value HardwareResource::TypeTimer Refers to the global timer managed by the \l{DeviceManager}. Plugins should not create their own timers, but rather request the global timer using the hardware resources. - \value HardwareResourceNetworkManager + \value HardwareResource::TypeNetworkManager Allows to send network requests and receive replies. - \value HardwareResourceUpnpDisovery + \value HardwareResource::TypeUpnpDisovery Allows to search a UPnP devices in the network. - \value HardwareResourceBluetoothLE + \value HardwareResource::TypeBluetoothLE Allows to interact with bluetooth low energy devices. */ @@ -179,8 +179,6 @@ #include "devicemanager.h" #include "loggingcategories.h" -#include "hardware/radio433/radio433.h" - #include "plugin/devicepairinginfo.h" #include "plugin/deviceplugin.h" #include "typeutils.h" @@ -200,41 +198,17 @@ * Use \c guhserver::GuhCore::instance()->deviceManager() instead to access the DeviceManager. */ DeviceManager::DeviceManager(const QLocale &locale, QObject *parent) : QObject(parent), - m_locale(locale), - m_radio433(0) + m_locale(locale) { qRegisterMetaType(); qRegisterMetaType(); - m_pluginTimer.setInterval(10000); - connect(&m_pluginTimer, &QTimer::timeout, this, &DeviceManager::timerEvent); - - m_radio433 = new Radio433(this); - m_radio433->enable(); - - // Network manager - m_networkManager = new NetworkAccessManager(this); - connect(m_networkManager, &NetworkAccessManager::replyReady, this, &DeviceManager::replyReady); - - // UPnP discovery - m_upnpDiscovery = new UpnpDiscovery(this); - connect(m_upnpDiscovery, &UpnpDiscovery::discoveryFinished, this, &DeviceManager::upnpDiscoveryFinished); - connect(m_upnpDiscovery, &UpnpDiscovery::upnpNotify, this, &DeviceManager::upnpNotifyReceived); - - // Avahi Browser - m_avahiBrowser = new QtAvahiServiceBrowser(this); - m_avahiBrowser->enable(); - - // Bluetooth LE -#ifdef BLUETOOTH_LE - m_bluetoothScanner = new BluetoothScanner(this); - if (!m_bluetoothScanner->isAvailable()) { - delete m_bluetoothScanner; - m_bluetoothScanner = 0; - } else { - connect(m_bluetoothScanner, &BluetoothScanner::bluetoothDiscoveryFinished, this, &DeviceManager::bluetoothDiscoveryFinished); - } -#endif + m_hardwareManager = new HardwareManager(this); + connect(m_hardwareManager->pluginTimer(), &PluginTimer::timerEvent, this, &DeviceManager::timerEvent); + connect(m_hardwareManager->networkManager(), &NetworkAccessManager::replyReady, this, &DeviceManager::replyReady); + connect(m_hardwareManager->upnpDiscovery(), &UpnpDiscovery::discoveryFinished, this, &DeviceManager::upnpDiscoveryFinished); + connect(m_hardwareManager->upnpDiscovery(), &UpnpDiscovery::upnpNotify, this, &DeviceManager::upnpNotifyReceived); + connect(m_hardwareManager->bluetoothScanner(), &BluetoothScanner::bluetoothDiscoveryFinished, this, &DeviceManager::bluetoothDiscoveryFinished); // Give hardware a chance to start up before loading plugins etc. QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection); @@ -781,7 +755,7 @@ DeviceManager::DeviceError DeviceManager::removeConfiguredDevice(const DeviceId if (!pluginNeedsTimer) { m_pluginTimerUsers.removeAll(plugin(device->pluginId())); if (m_pluginTimerUsers.isEmpty()) { - m_pluginTimer.stop(); + m_hardwareManager->pluginTimer()->disable(); } } device->deleteLater(); @@ -1244,9 +1218,9 @@ void DeviceManager::slotDeviceSetupFinished(Device *device, DeviceManager::Devic } DevicePlugin *plugin = m_devicePlugins.value(device->pluginId()); - if (plugin->requiredHardware().testFlag(HardwareResourceTimer)) { - if (!m_pluginTimer.isActive()) { - m_pluginTimer.start(); + if (plugin->requiredHardware().testFlag(HardwareResource::TypeTimer)) { + if (!m_hardwareManager->pluginTimer()->enabled()) { + m_hardwareManager->pluginTimer()->enable(); // Additionally fire off one event to initialize stuff QTimer::singleShot(0, this, SLOT(timerEvent())); } @@ -1457,12 +1431,12 @@ void DeviceManager::radio433SignalReceived(QList rawData) foreach (Device *device, m_configuredDevices) { DeviceClass deviceClass = m_supportedDevices.value(device->deviceClassId()); DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); - if (plugin->requiredHardware().testFlag(HardwareResourceRadio433) && !targetPlugins.contains(plugin)) { + if (plugin->requiredHardware().testFlag(HardwareResource::TypeRadio433) && !targetPlugins.contains(plugin)) { targetPlugins.append(plugin); } } foreach (DevicePlugin *plugin, m_discoveringPlugins) { - if (plugin->requiredHardware().testFlag(HardwareResourceRadio433) && !targetPlugins.contains(plugin)) { + if (plugin->requiredHardware().testFlag(HardwareResource::TypeRadio433) && !targetPlugins.contains(plugin)) { targetPlugins.append(plugin); } } @@ -1475,7 +1449,7 @@ void DeviceManager::radio433SignalReceived(QList rawData) void DeviceManager::replyReady(const PluginId &pluginId, QNetworkReply *reply) { foreach (DevicePlugin *devicePlugin, m_devicePlugins) { - if (devicePlugin->requiredHardware().testFlag(HardwareResourceNetworkManager) && devicePlugin->pluginId() == pluginId) { + if (devicePlugin->requiredHardware().testFlag(HardwareResource::TypeNetworkManager) && devicePlugin->pluginId() == pluginId) { devicePlugin->networkManagerReplyReady(reply); } } @@ -1484,7 +1458,7 @@ void DeviceManager::replyReady(const PluginId &pluginId, QNetworkReply *reply) void DeviceManager::upnpDiscoveryFinished(const QList &deviceDescriptorList, const PluginId &pluginId) { foreach (DevicePlugin *devicePlugin, m_devicePlugins) { - if (devicePlugin->requiredHardware().testFlag(HardwareResourceUpnpDisovery) && devicePlugin->pluginId() == pluginId) { + if (devicePlugin->requiredHardware().testFlag(HardwareResource::TypeUpnpDisovery) && devicePlugin->pluginId() == pluginId) { devicePlugin->upnpDiscoveryFinished(deviceDescriptorList); } } @@ -1493,27 +1467,25 @@ void DeviceManager::upnpDiscoveryFinished(const QList &dev void DeviceManager::upnpNotifyReceived(const QByteArray ¬ifyData) { foreach (DevicePlugin *devicePlugin, m_devicePlugins) { - if (devicePlugin->requiredHardware().testFlag(HardwareResourceUpnpDisovery)) { + if (devicePlugin->requiredHardware().testFlag(HardwareResource::TypeUpnpDisovery)) { devicePlugin->upnpNotifyReceived(notifyData); } } } -#ifdef BLUETOOTH_LE void DeviceManager::bluetoothDiscoveryFinished(const PluginId &pluginId, const QList &deviceInfos) { foreach (DevicePlugin *devicePlugin, m_devicePlugins) { - if (devicePlugin->requiredHardware().testFlag(HardwareResourceBluetoothLE) && devicePlugin->pluginId() == pluginId) { + if (devicePlugin->requiredHardware().testFlag(HardwareResource::TypeBluetoothLE) && devicePlugin->pluginId() == pluginId) { devicePlugin->bluetoothDiscoveryFinished(deviceInfos); } } } -#endif void DeviceManager::timerEvent() { foreach (DevicePlugin *plugin, m_pluginTimerUsers) { - if (plugin->requiredHardware().testFlag(HardwareResourceTimer)) { + if (plugin->requiredHardware().testFlag(HardwareResource::TypeTimer)) { plugin->guhTimer(); } } @@ -1557,10 +1529,10 @@ DeviceManager::DeviceSetupStatus DeviceManager::setupDevice(Device *device) return status; } - if (plugin->requiredHardware().testFlag(HardwareResourceTimer)) { + if (plugin->requiredHardware().testFlag(HardwareResource::TypeTimer)) { - if (!m_pluginTimer.isActive()) { - m_pluginTimer.start(); + if (!m_hardwareManager->pluginTimer()->enabled()) { + m_hardwareManager->pluginTimer()->enable(); // Additionally fire off one event to initialize stuff QTimer::singleShot(0, this, SLOT(timerEvent())); } diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 90da495e..ad8f86e6 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -34,25 +34,17 @@ #include "types/action.h" #include "types/vendor.h" -#include "network/networkaccessmanager.h" -#include "network/upnp/upnpdiscovery.h" -#include "network/upnp/upnpdevicedescriptor.h" -#include "network/avahi/qtavahiservicebrowser.h" - -#ifdef BLUETOOTH_LE -#include "bluetooth/bluetoothscanner.h" -#endif - #include #include #include #include +#include "hardwaremanager.h" + class Device; class DevicePlugin; class DevicePairingInfo; -class Radio433; -class UpnpDiscovery; +class HardwareManager; class LIBGUH_EXPORT DeviceManager : public QObject { @@ -62,16 +54,6 @@ class LIBGUH_EXPORT DeviceManager : public QObject friend class DevicePlugin; public: - enum HardwareResource { - HardwareResourceNone = 0, - HardwareResourceRadio433 = 1, - HardwareResourceTimer = 2, - HardwareResourceNetworkManager = 4, - HardwareResourceUpnpDisovery = 8, - HardwareResourceBluetoothLE = 16 - }; - Q_DECLARE_FLAGS(HardwareResources, HardwareResource) - enum DeviceError { DeviceErrorNoError, DeviceErrorPluginNotFound, @@ -98,12 +80,14 @@ public: DeviceErrorPairingTransactionIdNotFound, DeviceErrorParameterNotWritable }; + Q_ENUM(DeviceError) enum DeviceSetupStatus { DeviceSetupStatusSuccess, DeviceSetupStatusFailure, DeviceSetupStatusAsync }; + Q_ENUM(DeviceSetupStatus) explicit DeviceManager(const QLocale &locale, QObject *parent = 0); ~DeviceManager(); @@ -187,9 +171,7 @@ private slots: void upnpDiscoveryFinished(const QList &deviceDescriptorList, const PluginId &pluginId); void upnpNotifyReceived(const QByteArray ¬ifyData); - #ifdef BLUETOOTH_LE void bluetoothDiscoveryFinished(const PluginId &pluginId, const QList &deviceInfos); - #endif void timerEvent(); @@ -211,18 +193,18 @@ private: QHash m_discoveredDevices; QHash m_devicePlugins; - - // Hardware Resources - Radio433* m_radio433; - QTimer m_pluginTimer; QList m_pluginTimerUsers; - NetworkAccessManager *m_networkManager; - UpnpDiscovery* m_upnpDiscovery; - QtAvahiServiceBrowser *m_avahiBrowser; - #ifdef BLUETOOTH_LE - BluetoothScanner *m_bluetoothScanner; - #endif + HardwareManager *m_hardwareManager; +// // Hardware Resources +// Radio433 *m_radio433; +// PluginTimer *m_pluginTimer; +// NetworkAccessManager *m_networkManager; +// UpnpDiscovery *m_upnpDiscovery; +// QtAvahiServiceBrowser *m_avahiBrowser; +// BluetoothScanner *m_bluetoothScanner; + + QHash m_pairingsJustAdd; QHash m_pairingsDiscovery; @@ -231,7 +213,6 @@ private: QList m_discoveringPlugins; }; -Q_DECLARE_OPERATORS_FOR_FLAGS(DeviceManager::HardwareResources) Q_DECLARE_METATYPE(DeviceManager::DeviceError) #endif // DEVICEMANAGER_H diff --git a/libguh/hardwaremanager.cpp b/libguh/hardwaremanager.cpp new file mode 100644 index 00000000..22d36eda --- /dev/null +++ b/libguh/hardwaremanager.cpp @@ -0,0 +1,59 @@ +#include "hardwaremanager.h" + +HardwareManager::HardwareManager(QObject *parent) : QObject(parent) +{ + // Init hardware resources + m_pluginTimer = new PluginTimer(10000, this); + + m_radio433 = new Radio433(this); + m_radio433->enable(); + + // Network manager + m_networkManager = new NetworkAccessManager(this); + + // UPnP discovery + m_upnpDiscovery = new UpnpDiscovery(this); + + // Avahi Browser + m_avahiBrowser = new QtAvahiServiceBrowser(this); + m_avahiBrowser->enable(); + + // Bluetooth LE + m_bluetoothScanner = new BluetoothScanner(this); + if (!m_bluetoothScanner->isAvailable()) { + delete m_bluetoothScanner; + m_bluetoothScanner = nullptr; + } + +} + +Radio433 *HardwareManager::radio433() +{ + return m_radio433; +} + +PluginTimer *HardwareManager::pluginTimer() +{ + return m_pluginTimer; +} + +NetworkAccessManager *HardwareManager::networkManager() +{ + return m_networkManager; +} + +UpnpDiscovery *HardwareManager::upnpDiscovery() +{ + return m_upnpDiscovery; +} + +QtAvahiServiceBrowser *HardwareManager::avahiBrowser() +{ + return m_avahiBrowser; +} + +BluetoothScanner *HardwareManager::bluetoothScanner() +{ + return m_bluetoothScanner; +} + diff --git a/libguh/hardwaremanager.h b/libguh/hardwaremanager.h new file mode 100644 index 00000000..8d52c95d --- /dev/null +++ b/libguh/hardwaremanager.h @@ -0,0 +1,46 @@ +#ifndef HARDWAREMANAGER_H +#define HARDWAREMANAGER_H + +#include + +#include "plugintimer.h" +#include "bluetooth/bluetoothscanner.h" +#include "hardware/radio433/radio433.h" +#include "network/networkaccessmanager.h" +#include "network/upnp/upnpdiscovery.h" +#include "network/upnp/upnpdevicedescriptor.h" +#include "network/avahi/qtavahiservicebrowser.h" + +class PluginTimer; +class UpnpDiscovery; + +class HardwareManager : public QObject +{ + Q_OBJECT +public: + explicit HardwareManager(QObject *parent = nullptr); + + Radio433 *radio433(); + PluginTimer *pluginTimer(); + NetworkAccessManager *networkManager(); + UpnpDiscovery *upnpDiscovery(); + QtAvahiServiceBrowser *avahiBrowser(); + BluetoothScanner *bluetoothScanner(); + +private: + // Hardware Resources + Radio433 *m_radio433 = nullptr; + PluginTimer *m_pluginTimer = nullptr; + NetworkAccessManager *m_networkManager = nullptr; + UpnpDiscovery *m_upnpDiscovery = nullptr; + QtAvahiServiceBrowser *m_avahiBrowser = nullptr; + BluetoothScanner *m_bluetoothScanner = nullptr; + +signals: + +public slots: + +}; + + +#endif // HARDWAREMANAGER_H diff --git a/libguh/hardwareresource.cpp b/libguh/hardwareresource.cpp new file mode 100644 index 00000000..c746a708 --- /dev/null +++ b/libguh/hardwareresource.cpp @@ -0,0 +1,60 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * This file is part of guh. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "hardwareresource.h" +#include "guhsettings.h" +#include "hardwaremanager.h" + +HardwareResource::HardwareResource(const Type &hardwareReourceType, QObject *parent) : + QObject(parent), + m_hardwareReourceType(hardwareReourceType) +{ + // TODO: load if hardware resource is enabled or not +} + +bool HardwareResource::available() const +{ + return m_available; +} + +bool HardwareResource::enabled() const +{ + return m_enabled; +} + +HardwareResource::Type HardwareResource::hardwareReourceType() const +{ + return m_hardwareReourceType; +} + +void HardwareResource::setEnabled(const bool &enabled) +{ + // TODO: save this information to settings + m_enabled = enabled; + emit enabledChanged(m_enabled); +} + +void HardwareResource::setAvailable(const bool &available) +{ + m_available = available; + emit availableChanged(m_available); +} diff --git a/libguh/hardwareresource.h b/libguh/hardwareresource.h new file mode 100644 index 00000000..c71a1fae --- /dev/null +++ b/libguh/hardwareresource.h @@ -0,0 +1,72 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * This file is part of guh. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HARDWARERESOURCE_H +#define HARDWARERESOURCE_H + +#include + +class HardwareResource : public QObject +{ + Q_OBJECT +public: + enum Type { + TypeNone = 0, + TypeRadio433 = 1, + TypeTimer = 2, + TypeNetworkManager = 4, + TypeUpnpDisovery = 8, + TypeBluetoothLE = 16 + }; + Q_ENUM(Type) + Q_DECLARE_FLAGS(Types, Type) + + explicit HardwareResource(const HardwareResource::Type &hardwareReourceType, QObject *parent = nullptr); + + bool available() const; + bool enabled() const; + + HardwareResource::Type hardwareReourceType() const; + +private: + bool m_available = false; + bool m_enabled = true; + HardwareResource::Type m_hardwareReourceType; + +protected: + void setEnabled(const bool &enabled); + void setAvailable(const bool &available); + +signals: + void enabledChanged(const bool &enabled); + void availableChanged(const bool &available); + +public slots: + virtual bool enable() = 0; + virtual bool disable() = 0; + +}; + +Q_DECLARE_METATYPE(HardwareResource::Type) +Q_DECLARE_OPERATORS_FOR_FLAGS(HardwareResource::Types) + +#endif // HARDWARERESOURCE_H diff --git a/libguh/libguh.pro b/libguh/libguh.pro index 14fbaac3..c7ad82bb 100644 --- a/libguh/libguh.pro +++ b/libguh/libguh.pro @@ -3,7 +3,7 @@ include(../guh.pri) TARGET = guh TEMPLATE = lib -QT += network +QT += network bluetooth DEFINES += LIBGUH_LIBRARY QMAKE_LFLAGS += -fPIC @@ -14,16 +14,6 @@ INSTALLS += target # Avahi libs LIBS += -lavahi-common -lavahi-client -# check Bluetooth LE support -contains(DEFINES, BLUETOOTH_LE) { - HEADERS += bluetooth/bluetoothscanner.h \ - bluetooth/bluetoothlowenergydevice.h \ - - SOURCES += bluetooth/bluetoothscanner.cpp \ - bluetooth/bluetoothlowenergydevice.cpp \ - -} - HEADERS += devicemanager.h \ libguh.h \ typeutils.h \ @@ -53,6 +43,8 @@ HEADERS += devicemanager.h \ network/avahi/qtavahiservice_p.h \ network/avahi/qtavahiservicebrowser.h \ network/avahi/qtavahiservicebrowser_p.h \ + bluetooth/bluetoothscanner.h \ + bluetooth/bluetoothlowenergydevice.h \ coap/coap.h \ coap/coappdu.h \ coap/coapoption.h \ @@ -76,6 +68,9 @@ HEADERS += devicemanager.h \ types/ruleaction.h \ types/ruleactionparam.h \ types/statedescriptor.h \ + hardwareresource.h \ + plugintimer.h \ + hardwaremanager.h SOURCES += devicemanager.cpp \ loggingcategories.cpp \ @@ -104,6 +99,8 @@ SOURCES += devicemanager.cpp \ network/avahi/qtavahiservice_p.cpp \ network/avahi/qtavahiservicebrowser.cpp \ network/avahi/qtavahiservicebrowser_p.cpp \ + bluetooth/bluetoothscanner.cpp \ + bluetooth/bluetoothlowenergydevice.cpp \ coap/coap.cpp \ coap/coappdu.cpp \ coap/coapoption.cpp \ @@ -127,6 +124,9 @@ SOURCES += devicemanager.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 diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 924f5f58..7db82a0a 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -33,11 +33,11 @@ */ /*! - \fn DeviceManager::HardwareResources DevicePlugin::requiredHardware() const + \fn HardwareResource::Types DevicePlugin::requiredHardware() const Return flags describing the common hardware resources required by this plugin. If you want to use more than one resource, you can combine them ith the OR operator. - \sa DeviceManager::HardwareResource + \sa HardwareResource::Type */ /*! @@ -66,7 +66,7 @@ If a UPnP device will notify a NOTIFY message in the network, the \l{UpnpDiscovery} will catch the notification data and call this method with the \a notifyData. - \note Only if if the plugin has requested the \l{DeviceManager::HardwareResourceUpnpDisovery} resource + \note Only if if the plugin has requested the \l{HardwareResource::TypeUpnpDisovery} resource using \l{DevicePlugin::requiredHardware()}, this slot will be called. \sa UpnpDiscovery @@ -76,7 +76,7 @@ \fn DevicePlugin::networkManagerReplyReady(QNetworkReply *reply) This method will be called whenever a pending network \a reply for this plugin is finished. - \note Only if if the plugin has requested the \l{DeviceManager::HardwareResourceNetworkManager} + \note Only if if the plugin has requested the \l{HardwareResource::TypeNetworkManager} resource using \l{DevicePlugin::requiredHardware()}, this slot will be called. \sa NetworkAccessManager::replyReady() @@ -534,8 +534,8 @@ Device *DevicePlugin::findDeviceByParams(const ParamList ¶ms) const bool DevicePlugin::transmitData(int delay, QList rawData, int repetitions) { switch (requiredHardware()) { - case DeviceManager::HardwareResourceRadio433: - return deviceManager()->m_radio433->sendData(delay, rawData, repetitions); + case HardwareResource::TypeRadio433: + return deviceManager()->m_hardwareManager->radio433()->sendData(delay, rawData, repetitions); default: qCWarning(dcDeviceManager) << "Unknown harware type. Cannot send."; } @@ -551,8 +551,8 @@ bool DevicePlugin::transmitData(int delay, QList rawData, int repetitions) */ QNetworkReply *DevicePlugin::networkManagerGet(const QNetworkRequest &request) { - if (requiredHardware().testFlag(DeviceManager::HardwareResourceNetworkManager)) { - return deviceManager()->m_networkManager->get(pluginId(), request); + if (requiredHardware().testFlag(HardwareResource::TypeNetworkManager)) { + return deviceManager()->m_hardwareManager->networkManager()->get(pluginId(), request); } else { qCWarning(dcDeviceManager) << "Network manager hardware resource not set for plugin" << pluginName(); } @@ -568,8 +568,8 @@ QNetworkReply *DevicePlugin::networkManagerGet(const QNetworkRequest &request) */ QNetworkReply *DevicePlugin::networkManagerPost(const QNetworkRequest &request, const QByteArray &data) { - if (requiredHardware().testFlag(DeviceManager::HardwareResourceNetworkManager)) { - return deviceManager()->m_networkManager->post(pluginId(), request, data); + if (requiredHardware().testFlag(HardwareResource::TypeNetworkManager)) { + return deviceManager()->m_hardwareManager->networkManager()->post(pluginId(), request, data); } else { qCWarning(dcDeviceManager) << "Network manager hardware resource not set for plugin" << pluginName(); } @@ -584,8 +584,8 @@ QNetworkReply *DevicePlugin::networkManagerPost(const QNetworkRequest &request, */ QNetworkReply *DevicePlugin::networkManagerPut(const QNetworkRequest &request, const QByteArray &data) { - if (requiredHardware().testFlag(DeviceManager::HardwareResourceNetworkManager)) { - return deviceManager()->m_networkManager->put(pluginId(), request, data); + if (requiredHardware().testFlag(HardwareResource::TypeNetworkManager)) { + return deviceManager()->m_hardwareManager->networkManager()->put(pluginId(), request, data); } else { qCWarning(dcDeviceManager) << "Network manager hardware resource not set for plugin" << pluginName(); } @@ -1015,8 +1015,8 @@ void DevicePlugin::loadMetaData() */ void DevicePlugin::upnpDiscover(QString searchTarget, QString userAgent) { - if(requiredHardware().testFlag(DeviceManager::HardwareResourceUpnpDisovery)){ - deviceManager()->m_upnpDiscovery->discoverDevices(searchTarget, userAgent, pluginId()); + if(requiredHardware().testFlag(HardwareResource::TypeUpnpDisovery)){ + deviceManager()->m_hardwareManager->upnpDiscovery()->discoverDevices(searchTarget, userAgent, pluginId()); } else { qCWarning(dcDeviceManager) << "UPnP discovery resource not set for plugin" << pluginName(); } @@ -1025,20 +1025,18 @@ void DevicePlugin::upnpDiscover(QString searchTarget, QString userAgent) /*! Returns the pointer to the central \l{QtAvahiService}{service} browser. */ QtAvahiServiceBrowser *DevicePlugin::avahiServiceBrowser() const { - return deviceManager()->m_avahiBrowser; + return deviceManager()->m_hardwareManager->avahiBrowser(); } -#ifdef BLUETOOTH_LE bool DevicePlugin::discoverBluetooth() { - if(requiredHardware().testFlag(DeviceManager::HardwareResourceBluetoothLE)){ - return deviceManager()->m_bluetoothScanner->discover(pluginId()); + if(requiredHardware().testFlag(HardwareResource::TypeBluetoothLE)){ + return deviceManager()->m_hardwareManager->bluetoothScanner()->discover(pluginId()); } else { qCWarning(dcDeviceManager) << "Bluetooth LE resource not set for plugin" << pluginName(); } return false; } -#endif QStringList DevicePlugin::verifyFields(const QStringList &fields, const QJsonObject &value) const { diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index 8ed2cb9f..b36fd557 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -35,16 +35,13 @@ #include "types/vendor.h" #include "types/param.h" -#ifdef BLUETOOTH_LE -#include -#endif - #include #include #include #include #include #include +#include class DeviceManager; class Device; @@ -66,7 +63,7 @@ public: QTranslator *translator(); bool setLocale(const QLocale &locale); - virtual DeviceManager::HardwareResources requiredHardware() const = 0; + virtual HardwareResource::Types requiredHardware() const = 0; virtual void startMonitoringAutoDevices(); virtual DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); @@ -88,9 +85,7 @@ public: virtual void networkManagerReplyReady(QNetworkReply *reply) {Q_UNUSED(reply)} - #ifdef BLUETOOTH_LE virtual void bluetoothDiscoveryFinished(const QList &deviceInfos) { Q_UNUSED(deviceInfos) } - #endif // Configuration @@ -122,9 +117,7 @@ protected: QtAvahiServiceBrowser *avahiServiceBrowser() const; // Bluetooth LE discovery - #ifdef BLUETOOTH_LE bool discoverBluetooth(); - #endif // Network manager QNetworkReply *networkManagerGet(const QNetworkRequest &request); diff --git a/libguh/plugintimer.cpp b/libguh/plugintimer.cpp new file mode 100644 index 00000000..f610cc66 --- /dev/null +++ b/libguh/plugintimer.cpp @@ -0,0 +1,34 @@ +#include "plugintimer.h" + +PluginTimer::PluginTimer(int intervall, QObject *parent) : + HardwareResource(HardwareResource::TypeTimer, parent), + m_intervall(intervall) +{ + // FIXME: the timer should be able to emit timerEvents with different resolutions + m_timer = new QTimer(this); + m_timer->setSingleShot(false); + m_timer->setInterval(m_intervall); + + connect(m_timer, &QTimer::timeout, this, &PluginTimer::timerEvent); + setAvailable(true); +} + +bool PluginTimer::enable() +{ + if (!available()) + return false; + + m_timer->start(); + setEnabled(true); + return true; +} + +bool PluginTimer::disable() +{ + if (!available()) + return false; + + m_timer->stop(); + setEnabled(false); + return true; +} diff --git a/libguh/plugintimer.h b/libguh/plugintimer.h new file mode 100644 index 00000000..85877ac2 --- /dev/null +++ b/libguh/plugintimer.h @@ -0,0 +1,28 @@ +#ifndef PLUGINTIMER_H +#define PLUGINTIMER_H + +#include +#include + +#include "hardwareresource.h" + +class PluginTimer : public HardwareResource +{ + Q_OBJECT +public: + explicit PluginTimer(int intervall, QObject *parent = nullptr); + +private: + QTimer *m_timer = nullptr; + int m_intervall = 10000; + +signals: + void timerEvent(); + +public slots: + bool enable(); + bool disable(); + +}; + +#endif // PLUGINTIMER_H diff --git a/plugins/mock/devicepluginmock.cpp b/plugins/mock/devicepluginmock.cpp index 25873def..5e70e60f 100644 --- a/plugins/mock/devicepluginmock.cpp +++ b/plugins/mock/devicepluginmock.cpp @@ -61,9 +61,9 @@ DevicePluginMock::~DevicePluginMock() { } -DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const +HardwareResource::Types DevicePluginMock::requiredHardware() const { - return DeviceManager::HardwareResourceTimer; + return HardwareResource::TypeTimer; } DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) diff --git a/plugins/mock/devicepluginmock.h b/plugins/mock/devicepluginmock.h index ab598641..065ab47e 100644 --- a/plugins/mock/devicepluginmock.h +++ b/plugins/mock/devicepluginmock.h @@ -41,7 +41,7 @@ public: explicit DevicePluginMock(); ~DevicePluginMock(); - DeviceManager::HardwareResources requiredHardware() const override; + HardwareResource::Types requiredHardware() const override; DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; diff --git a/plugins/plugins.pri b/plugins/plugins.pri index 5c72af80..e258cd06 100644 --- a/plugins/plugins.pri +++ b/plugins/plugins.pri @@ -3,12 +3,7 @@ include(../guh.pri) TEMPLATE = lib CONFIG += plugin -QT += network - -# Check Bluetooth LE support -contains(DEFINES, BLUETOOTH_LE) { - QT += bluetooth -} +QT += network bluetooth INCLUDEPATH += $$top_srcdir/libguh LIBS += -L../../libguh -lguh