From f02a9fcdd45650deab0a240a5572ff5e423bbfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 16 Nov 2017 15:07:12 +0100 Subject: [PATCH] add bluetooth manager --- .../bluetooth/bluetoothlowenergymanager.cpp | 51 +++++++++++++++++++ libguh/bluetooth/bluetoothlowenergymanager.h | 30 +++++++++++ libguh/bluetooth/bluetoothscanner.cpp | 45 ++-------------- libguh/bluetooth/bluetoothscanner.h | 4 +- libguh/devicemanager.cpp | 5 ++ libguh/devicemanager.h | 2 + libguh/hardwaremanager.cpp | 18 ++++--- libguh/hardwaremanager.h | 9 ++-- libguh/libguh.pro | 6 ++- .../network/avahi/qtavahiservicebrowser.cpp | 5 +- libguh/network/avahi/qtavahiservicebrowser.h | 9 ++-- libguh/network/upnp/upnpdiscovery.cpp | 8 +-- libguh/plugin/deviceplugin.cpp | 5 ++ libguh/plugin/deviceplugin.h | 1 + 14 files changed, 135 insertions(+), 63 deletions(-) create mode 100644 libguh/bluetooth/bluetoothlowenergymanager.cpp create mode 100644 libguh/bluetooth/bluetoothlowenergymanager.h diff --git a/libguh/bluetooth/bluetoothlowenergymanager.cpp b/libguh/bluetooth/bluetoothlowenergymanager.cpp new file mode 100644 index 00000000..4453baa3 --- /dev/null +++ b/libguh/bluetooth/bluetoothlowenergymanager.cpp @@ -0,0 +1,51 @@ +#include "bluetoothlowenergymanager.h" +#include "loggingcategories.h" + +BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) : + HardwareResource(HardwareResource::TypeBluetoothLE, "Bluetooth LE manager", parent) +{ + // Check which bluetooth adapter are available + QList bluetoothAdapters = QBluetoothLocalDevice::allDevices(); + if (bluetoothAdapters.isEmpty()) { + qCWarning(dcHardware()) << name() << "No bluetooth adapter found. Resource not available."; + setAvailable(false); + return; + } + + // Create a scanner for each adapter + foreach (const QBluetoothHostInfo &hostInfo, bluetoothAdapters) { + qCDebug(dcHardware()) << name() << "Adapter:" << hostInfo.name() << hostInfo.address().toString(); + m_bluetoothScanners.append(new BluetoothScanner(hostInfo.address(), this)); + } + + + +// // Check if Bluetooth is available on this device +// if (!localDevice.isValid()) { +// qCWarning(dcHardware()) << "No Bluetooth device found."; +// setAvailable(false); +// return false; +// } + +// // Turn Bluetooth on +// localDevice.powerOn(); + +// // Make it visible to others +// localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable); + + qCDebug(dcHardware()) << "-->" << name() << "created successfully."; + + setAvailable(true); +} + +bool BluetoothLowEnergyManager::enable() +{ + // TODO: enable all devices created by this + + return true; +} + +bool BluetoothLowEnergyManager::disable() +{ + return true; +} diff --git a/libguh/bluetooth/bluetoothlowenergymanager.h b/libguh/bluetooth/bluetoothlowenergymanager.h new file mode 100644 index 00000000..3309e98c --- /dev/null +++ b/libguh/bluetooth/bluetoothlowenergymanager.h @@ -0,0 +1,30 @@ +#ifndef BLUETOOTHLOWENERGYMANAGER_H +#define BLUETOOTHLOWENERGYMANAGER_H + +#include +#include + +#include "hardwareresource.h" +#include "bluetoothscanner.h" + +class BluetoothLowEnergyManager : public HardwareResource +{ + Q_OBJECT + + friend class HardwareManager; + +public: + +private: + explicit BluetoothLowEnergyManager(QObject *parent = nullptr); + QList m_bluetoothScanners; + +signals: + +public slots: + bool enable(); + bool disable(); + +}; + +#endif // BLUETOOTHLOWENERGYMANAGER_H diff --git a/libguh/bluetooth/bluetoothscanner.cpp b/libguh/bluetooth/bluetoothscanner.cpp index fcd71de5..64b985b4 100644 --- a/libguh/bluetooth/bluetoothscanner.cpp +++ b/libguh/bluetooth/bluetoothscanner.cpp @@ -28,8 +28,6 @@ \inmodule libguh The bluetooth scanner hardware resource allows to discover bluetooth low energy devices. - - \note: Only available for Qt >= 5.4.0! */ /*! @@ -42,53 +40,20 @@ #include "loggingcategories.h" /*! Construct the hardware resource BluetoothScanner with the given \a parent. */ -BluetoothScanner::BluetoothScanner(QObject *parent) : - QObject(parent) +BluetoothScanner::BluetoothScanner(const QBluetoothAddress &adapterAddress, QObject *parent) : + QObject(parent), + m_adapterAddress(adapterAddress) { m_timer = new QTimer(this); m_timer->setSingleShot(true); m_timer->setInterval(5000); + connect(m_timer, &QTimer::timeout, this, &BluetoothScanner::discoveryTimeout); -} -/*! Returns true, if a bluetooth hardware is available. */ -bool BluetoothScanner::isAvailable() -{ - //Using default Bluetooth adapter - QBluetoothLocalDevice localDevice; - - // Check if Bluetooth is available on this device - if (!localDevice.isValid()) { - qCWarning(dcHardware) << "No Bluetooth device found."; - m_available = false; - return false; - } - - // Turn Bluetooth on - localDevice.powerOn(); - - // Make it visible to others - localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable); - - // Get connected devices - QList remotes = localDevice.allDevices(); - if (remotes.isEmpty()) { - qCWarning(dcHardware) << "No Bluetooth host info found."; - m_available = false; - return false; - } - - QBluetoothHostInfo hostInfo = remotes.first(); - - // Create a discovery agent and connect to its signals - m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(hostInfo.address(), this); + m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(m_adapterAddress, this); connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothScanner::deviceDiscovered); connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onError(QBluetoothDeviceDiscoveryAgent::Error))); - - qCDebug(dcDeviceManager) << "--> Bluetooth discovery created successfully."; - m_available = true; - return true; } /*! Returns true, if the discovering agent currently is running. */ diff --git a/libguh/bluetooth/bluetoothscanner.h b/libguh/bluetooth/bluetoothscanner.h index a0dc68d7..613c7788 100644 --- a/libguh/bluetooth/bluetoothscanner.h +++ b/libguh/bluetooth/bluetoothscanner.h @@ -40,12 +40,12 @@ class LIBGUH_EXPORT BluetoothScanner : public QObject { Q_OBJECT public: - explicit BluetoothScanner(QObject *parent = 0); - bool isAvailable(); + explicit BluetoothScanner(const QBluetoothAddress &adapterAddress, QObject *parent = 0); bool isRunning(); bool discover(const PluginId &pluginId); private: + QBluetoothAddress m_adapterAddress; QBluetoothDeviceDiscoveryAgent *m_discoveryAgent; QList m_deviceInfos; QTimer *m_timer; diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 3dd751f2..5dc4edca 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -312,6 +312,11 @@ void DeviceManager::setLocale(const QLocale &locale) emit languageUpdated(); } +HardwareManager *DeviceManager::hardwareManager() +{ + return m_hardwareManager; +} + /*! Returns all the \l{DevicePlugin}{DevicePlugins} loaded in the system. */ QList DeviceManager::plugins() const { diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index a3aa0ba6..faab60c9 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -97,6 +97,8 @@ public: void setLocale(const QLocale &locale); + HardwareManager *hardwareManager(); + QList plugins() const; DevicePlugin* plugin(const PluginId &id) const; DeviceError setPluginConfig(const PluginId &pluginId, const ParamList &pluginConfig); diff --git a/libguh/hardwaremanager.cpp b/libguh/hardwaremanager.cpp index a767c6af..fd9acfeb 100644 --- a/libguh/hardwaremanager.cpp +++ b/libguh/hardwaremanager.cpp @@ -1,6 +1,7 @@ #include "hardwaremanager.h" #include "plugintimer.h" +#include "loggingcategories.h" #include "bluetooth/bluetoothscanner.h" #include "hardware/radio433/radio433.h" #include "network/networkaccessmanager.h" @@ -25,7 +26,8 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent) // Network manager m_networkManager = new NetworkAccessManager(m_networkAccessManager, this); m_hardwareResources.append(m_networkManager); - m_networkManager->enable(); + if (m_networkManager->available()) + m_networkManager->enable(); // UPnP discovery m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this); @@ -37,12 +39,14 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent) m_hardwareResources.append(m_avahiBrowser); m_avahiBrowser->enable(); - // Bluetooth LE - m_bluetoothScanner = new BluetoothScanner(this); - if (!m_bluetoothScanner->isAvailable()) { - delete m_bluetoothScanner; - m_bluetoothScanner = nullptr; - } +// // Bluetooth LE +// m_bluetoothScanner = new BluetoothScanner(this); +// if (!m_bluetoothScanner->isAvailable()) { +// delete m_bluetoothScanner; +// m_bluetoothScanner = nullptr; +// } + + qCDebug(dcHardware()) << "Hardware manager initialized successfully"; } Radio433 *HardwareManager::radio433() diff --git a/libguh/hardwaremanager.h b/libguh/hardwaremanager.h index de50bc8d..25223960 100644 --- a/libguh/hardwaremanager.h +++ b/libguh/hardwaremanager.h @@ -17,6 +17,9 @@ class QtAvahiServiceBrowser; class HardwareManager : public QObject { Q_OBJECT + + friend class DeviceManager; + public: explicit HardwareManager(QObject *parent = nullptr); @@ -43,13 +46,13 @@ private: QtAvahiServiceBrowser *m_avahiBrowser = nullptr; BluetoothScanner *m_bluetoothScanner = nullptr; + bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType); + bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType); + signals: void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available); void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled); -public slots: - bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType); - bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType); }; diff --git a/libguh/libguh.pro b/libguh/libguh.pro index c7ad82bb..d36ad2a6 100644 --- a/libguh/libguh.pro +++ b/libguh/libguh.pro @@ -70,7 +70,8 @@ HEADERS += devicemanager.h \ types/statedescriptor.h \ hardwareresource.h \ plugintimer.h \ - hardwaremanager.h + hardwaremanager.h \ + bluetooth/bluetoothlowenergymanager.h SOURCES += devicemanager.cpp \ loggingcategories.cpp \ @@ -126,7 +127,8 @@ SOURCES += devicemanager.cpp \ types/statedescriptor.cpp \ hardwareresource.cpp \ plugintimer.cpp \ - hardwaremanager.cpp + hardwaremanager.cpp \ + bluetooth/bluetoothlowenergymanager.cpp # install plugininfo python script for libguh-dev generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo diff --git a/libguh/network/avahi/qtavahiservicebrowser.cpp b/libguh/network/avahi/qtavahiservicebrowser.cpp index e87d021f..448e8229 100644 --- a/libguh/network/avahi/qtavahiservicebrowser.cpp +++ b/libguh/network/avahi/qtavahiservicebrowser.cpp @@ -44,10 +44,11 @@ /*! Constructs a new \l{QtAvahiServiceBrowser} with the given \a parent. */ QtAvahiServiceBrowser::QtAvahiServiceBrowser(QObject *parent) : - HardwareResource(HardwareResource::TypeAvahiBrowser, "Avahi browser", parent), + HardwareResource(HardwareResource::TypeAvahiBrowser, "Avahi service browser", parent), d_ptr(new QtAvahiServiceBrowserPrivate(new QtAvahiClient)) { connect(d_ptr->client, &QtAvahiClient::clientStateChanged, this, &QtAvahiServiceBrowser::onClientStateChanged); + qCDebug(dcHardware()) << "-->" << name() << "created successfully."; } /*! Destructs this \l{QtAvahiServiceBrowser}. */ @@ -86,7 +87,7 @@ void QtAvahiServiceBrowser::onClientStateChanged(const QtAvahiClient::QtAvahiCli d_ptr->serviceTypeBrowser = avahi_service_type_browser_new(d_ptr->client->m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, (AvahiLookupFlags) 0, QtAvahiServiceBrowserPrivate::callbackServiceTypeBrowser, this); } else if (state == QtAvahiClient::QtAvahiClientStateFailure) { - qCWarning(dcAvahi()) << "Service browser client failure:" << d_ptr->client->errorString(); + qCWarning(dcAvahi()) << name() << "client failure:" << d_ptr->client->errorString(); } } diff --git a/libguh/network/avahi/qtavahiservicebrowser.h b/libguh/network/avahi/qtavahiservicebrowser.h index 1d928354..1256db1c 100644 --- a/libguh/network/avahi/qtavahiservicebrowser.h +++ b/libguh/network/avahi/qtavahiservicebrowser.h @@ -36,10 +36,10 @@ class QtAvahiServiceBrowserPrivate; class LIBGUH_EXPORT QtAvahiServiceBrowser : public HardwareResource { Q_OBJECT -public: - explicit QtAvahiServiceBrowser(QObject *parent = 0); - ~QtAvahiServiceBrowser(); + friend class HardwareManager; + +public: QList serviceEntries() const; signals: @@ -54,6 +54,9 @@ public slots: bool disable(); private: + explicit QtAvahiServiceBrowser(QObject *parent = 0); + ~QtAvahiServiceBrowser(); + QtAvahiServiceBrowserPrivate *d_ptr; QList m_serviceEntries; diff --git a/libguh/network/upnp/upnpdiscovery.cpp b/libguh/network/upnp/upnpdiscovery.cpp index 69e2215c..c8ce747d 100644 --- a/libguh/network/upnp/upnpdiscovery.cpp +++ b/libguh/network/upnp/upnpdiscovery.cpp @@ -67,7 +67,7 @@ UpnpDiscovery::UpnpDiscovery(QNetworkAccessManager *networkAccessManager, QObjec connect(m_notificationTimer, &QTimer::timeout, this, &UpnpDiscovery::notificationTimeout); setAvailable(true); - qCDebug(dcDeviceManager) << "-->" << name() << "created successfully."; + qCDebug(dcHardware()) << "-->" << name() << "created successfully."; } /*! Destruct this \l{UpnpDiscovery} object. */ @@ -191,7 +191,7 @@ void UpnpDiscovery::sendToMulticast(const QByteArray &data) void UpnpDiscovery::error(QAbstractSocket::SocketError error) { - qCWarning(dcHardware) << "UPnP socket error:" << error << m_socket->errorString(); + qCWarning(dcHardware) << name() << "socket error:" << error << m_socket->errorString(); } void UpnpDiscovery::readData() @@ -312,7 +312,7 @@ void UpnpDiscovery::replyFinished() break; } default: - qCWarning(dcHardware) << "HTTP request error" << reply->request().url().toString() << status; + qCWarning(dcHardware) << name() << "HTTP request error" << reply->request().url().toString() << status; m_informationRequestList.remove(reply); } @@ -449,7 +449,7 @@ bool UpnpDiscovery::enable() } if(!m_socket->joinMulticastGroup(m_host)){ - qCWarning(dcHardware()) << name() << "could not join multicast group" << m_host; + qCWarning(dcHardware()) << name() << "could not join multicast group" << m_host; delete m_socket; m_socket = nullptr; return false; diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 9ca5beb9..dac15801 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -506,6 +506,11 @@ QList DevicePlugin::myDevices() const return ret; } +HardwareManager *DevicePlugin::hardwareManager() const +{ + return m_deviceManager->hardwareManager(); +} + /*! Find a certain device from myDevices() by its \a params. All parameters must match or the device will not be found. Be prepared for nullptrs. diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index fb72831f..7b9f5475 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -108,6 +108,7 @@ signals: protected: DeviceManager *deviceManager() const; QList myDevices() const; + HardwareManager *hardwareManager() const; Device* findDeviceByParams(const ParamList ¶ms) const; // Radio 433