add bluetooth manager

This commit is contained in:
Simon Stürz 2017-11-16 15:07:12 +01:00 committed by Michael Zanetti
parent 19d8f8d4d7
commit f02a9fcdd4
14 changed files with 135 additions and 63 deletions

View File

@ -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<QBluetoothHostInfo> 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;
}

View File

@ -0,0 +1,30 @@
#ifndef BLUETOOTHLOWENERGYMANAGER_H
#define BLUETOOTHLOWENERGYMANAGER_H
#include <QObject>
#include <QBluetoothLocalDevice>
#include "hardwareresource.h"
#include "bluetoothscanner.h"
class BluetoothLowEnergyManager : public HardwareResource
{
Q_OBJECT
friend class HardwareManager;
public:
private:
explicit BluetoothLowEnergyManager(QObject *parent = nullptr);
QList<BluetoothScanner *> m_bluetoothScanners;
signals:
public slots:
bool enable();
bool disable();
};
#endif // BLUETOOTHLOWENERGYMANAGER_H

View File

@ -28,8 +28,6 @@
\inmodule libguh \inmodule libguh
The bluetooth scanner hardware resource allows to discover bluetooth low energy devices. 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" #include "loggingcategories.h"
/*! Construct the hardware resource BluetoothScanner with the given \a parent. */ /*! Construct the hardware resource BluetoothScanner with the given \a parent. */
BluetoothScanner::BluetoothScanner(QObject *parent) : BluetoothScanner::BluetoothScanner(const QBluetoothAddress &adapterAddress, QObject *parent) :
QObject(parent) QObject(parent),
m_adapterAddress(adapterAddress)
{ {
m_timer = new QTimer(this); m_timer = new QTimer(this);
m_timer->setSingleShot(true); m_timer->setSingleShot(true);
m_timer->setInterval(5000); m_timer->setInterval(5000);
connect(m_timer, &QTimer::timeout, this, &BluetoothScanner::discoveryTimeout); connect(m_timer, &QTimer::timeout, this, &BluetoothScanner::discoveryTimeout);
}
/*! Returns true, if a bluetooth hardware is available. */ m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(m_adapterAddress, this);
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<QBluetoothHostInfo> 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);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothScanner::deviceDiscovered); connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothScanner::deviceDiscovered);
connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onError(QBluetoothDeviceDiscoveryAgent::Error))); 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. */ /*! Returns true, if the discovering agent currently is running. */

View File

@ -40,12 +40,12 @@ class LIBGUH_EXPORT BluetoothScanner : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit BluetoothScanner(QObject *parent = 0); explicit BluetoothScanner(const QBluetoothAddress &adapterAddress, QObject *parent = 0);
bool isAvailable();
bool isRunning(); bool isRunning();
bool discover(const PluginId &pluginId); bool discover(const PluginId &pluginId);
private: private:
QBluetoothAddress m_adapterAddress;
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent; QBluetoothDeviceDiscoveryAgent *m_discoveryAgent;
QList<QBluetoothDeviceInfo> m_deviceInfos; QList<QBluetoothDeviceInfo> m_deviceInfos;
QTimer *m_timer; QTimer *m_timer;

View File

@ -312,6 +312,11 @@ void DeviceManager::setLocale(const QLocale &locale)
emit languageUpdated(); emit languageUpdated();
} }
HardwareManager *DeviceManager::hardwareManager()
{
return m_hardwareManager;
}
/*! Returns all the \l{DevicePlugin}{DevicePlugins} loaded in the system. */ /*! Returns all the \l{DevicePlugin}{DevicePlugins} loaded in the system. */
QList<DevicePlugin *> DeviceManager::plugins() const QList<DevicePlugin *> DeviceManager::plugins() const
{ {

View File

@ -97,6 +97,8 @@ public:
void setLocale(const QLocale &locale); void setLocale(const QLocale &locale);
HardwareManager *hardwareManager();
QList<DevicePlugin*> plugins() const; QList<DevicePlugin*> plugins() const;
DevicePlugin* plugin(const PluginId &id) const; DevicePlugin* plugin(const PluginId &id) const;
DeviceError setPluginConfig(const PluginId &pluginId, const ParamList &pluginConfig); DeviceError setPluginConfig(const PluginId &pluginId, const ParamList &pluginConfig);

View File

@ -1,6 +1,7 @@
#include "hardwaremanager.h" #include "hardwaremanager.h"
#include "plugintimer.h" #include "plugintimer.h"
#include "loggingcategories.h"
#include "bluetooth/bluetoothscanner.h" #include "bluetooth/bluetoothscanner.h"
#include "hardware/radio433/radio433.h" #include "hardware/radio433/radio433.h"
#include "network/networkaccessmanager.h" #include "network/networkaccessmanager.h"
@ -25,7 +26,8 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
// Network manager // Network manager
m_networkManager = new NetworkAccessManager(m_networkAccessManager, this); m_networkManager = new NetworkAccessManager(m_networkAccessManager, this);
m_hardwareResources.append(m_networkManager); m_hardwareResources.append(m_networkManager);
m_networkManager->enable(); if (m_networkManager->available())
m_networkManager->enable();
// UPnP discovery // UPnP discovery
m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this); m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this);
@ -37,12 +39,14 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
m_hardwareResources.append(m_avahiBrowser); m_hardwareResources.append(m_avahiBrowser);
m_avahiBrowser->enable(); m_avahiBrowser->enable();
// Bluetooth LE // // Bluetooth LE
m_bluetoothScanner = new BluetoothScanner(this); // m_bluetoothScanner = new BluetoothScanner(this);
if (!m_bluetoothScanner->isAvailable()) { // if (!m_bluetoothScanner->isAvailable()) {
delete m_bluetoothScanner; // delete m_bluetoothScanner;
m_bluetoothScanner = nullptr; // m_bluetoothScanner = nullptr;
} // }
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
} }
Radio433 *HardwareManager::radio433() Radio433 *HardwareManager::radio433()

View File

@ -17,6 +17,9 @@ class QtAvahiServiceBrowser;
class HardwareManager : public QObject class HardwareManager : public QObject
{ {
Q_OBJECT Q_OBJECT
friend class DeviceManager;
public: public:
explicit HardwareManager(QObject *parent = nullptr); explicit HardwareManager(QObject *parent = nullptr);
@ -43,13 +46,13 @@ private:
QtAvahiServiceBrowser *m_avahiBrowser = nullptr; QtAvahiServiceBrowser *m_avahiBrowser = nullptr;
BluetoothScanner *m_bluetoothScanner = nullptr; BluetoothScanner *m_bluetoothScanner = nullptr;
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
signals: signals:
void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available); void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available);
void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled); void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled);
public slots:
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
}; };

View File

@ -70,7 +70,8 @@ HEADERS += devicemanager.h \
types/statedescriptor.h \ types/statedescriptor.h \
hardwareresource.h \ hardwareresource.h \
plugintimer.h \ plugintimer.h \
hardwaremanager.h hardwaremanager.h \
bluetooth/bluetoothlowenergymanager.h
SOURCES += devicemanager.cpp \ SOURCES += devicemanager.cpp \
loggingcategories.cpp \ loggingcategories.cpp \
@ -126,7 +127,8 @@ SOURCES += devicemanager.cpp \
types/statedescriptor.cpp \ types/statedescriptor.cpp \
hardwareresource.cpp \ hardwareresource.cpp \
plugintimer.cpp \ plugintimer.cpp \
hardwaremanager.cpp hardwaremanager.cpp \
bluetooth/bluetoothlowenergymanager.cpp
# install plugininfo python script for libguh-dev # install plugininfo python script for libguh-dev
generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo

View File

@ -44,10 +44,11 @@
/*! Constructs a new \l{QtAvahiServiceBrowser} with the given \a parent. */ /*! Constructs a new \l{QtAvahiServiceBrowser} with the given \a parent. */
QtAvahiServiceBrowser::QtAvahiServiceBrowser(QObject *parent) : QtAvahiServiceBrowser::QtAvahiServiceBrowser(QObject *parent) :
HardwareResource(HardwareResource::TypeAvahiBrowser, "Avahi browser", parent), HardwareResource(HardwareResource::TypeAvahiBrowser, "Avahi service browser", parent),
d_ptr(new QtAvahiServiceBrowserPrivate(new QtAvahiClient)) d_ptr(new QtAvahiServiceBrowserPrivate(new QtAvahiClient))
{ {
connect(d_ptr->client, &QtAvahiClient::clientStateChanged, this, &QtAvahiServiceBrowser::onClientStateChanged); connect(d_ptr->client, &QtAvahiClient::clientStateChanged, this, &QtAvahiServiceBrowser::onClientStateChanged);
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
} }
/*! Destructs this \l{QtAvahiServiceBrowser}. */ /*! 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); 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) { } else if (state == QtAvahiClient::QtAvahiClientStateFailure) {
qCWarning(dcAvahi()) << "Service browser client failure:" << d_ptr->client->errorString(); qCWarning(dcAvahi()) << name() << "client failure:" << d_ptr->client->errorString();
} }
} }

View File

@ -36,10 +36,10 @@ class QtAvahiServiceBrowserPrivate;
class LIBGUH_EXPORT QtAvahiServiceBrowser : public HardwareResource class LIBGUH_EXPORT QtAvahiServiceBrowser : public HardwareResource
{ {
Q_OBJECT Q_OBJECT
public:
explicit QtAvahiServiceBrowser(QObject *parent = 0);
~QtAvahiServiceBrowser();
friend class HardwareManager;
public:
QList<AvahiServiceEntry> serviceEntries() const; QList<AvahiServiceEntry> serviceEntries() const;
signals: signals:
@ -54,6 +54,9 @@ public slots:
bool disable(); bool disable();
private: private:
explicit QtAvahiServiceBrowser(QObject *parent = 0);
~QtAvahiServiceBrowser();
QtAvahiServiceBrowserPrivate *d_ptr; QtAvahiServiceBrowserPrivate *d_ptr;
QList<AvahiServiceEntry> m_serviceEntries; QList<AvahiServiceEntry> m_serviceEntries;

View File

@ -67,7 +67,7 @@ UpnpDiscovery::UpnpDiscovery(QNetworkAccessManager *networkAccessManager, QObjec
connect(m_notificationTimer, &QTimer::timeout, this, &UpnpDiscovery::notificationTimeout); connect(m_notificationTimer, &QTimer::timeout, this, &UpnpDiscovery::notificationTimeout);
setAvailable(true); setAvailable(true);
qCDebug(dcDeviceManager) << "-->" << name() << "created successfully."; qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
} }
/*! Destruct this \l{UpnpDiscovery} object. */ /*! Destruct this \l{UpnpDiscovery} object. */
@ -191,7 +191,7 @@ void UpnpDiscovery::sendToMulticast(const QByteArray &data)
void UpnpDiscovery::error(QAbstractSocket::SocketError error) 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() void UpnpDiscovery::readData()
@ -312,7 +312,7 @@ void UpnpDiscovery::replyFinished()
break; break;
} }
default: 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); m_informationRequestList.remove(reply);
} }
@ -449,7 +449,7 @@ bool UpnpDiscovery::enable()
} }
if(!m_socket->joinMulticastGroup(m_host)){ 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; delete m_socket;
m_socket = nullptr; m_socket = nullptr;
return false; return false;

View File

@ -506,6 +506,11 @@ QList<Device *> DevicePlugin::myDevices() const
return ret; return ret;
} }
HardwareManager *DevicePlugin::hardwareManager() const
{
return m_deviceManager->hardwareManager();
}
/*! /*!
Find a certain device from myDevices() by its \a params. All parameters must 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. match or the device will not be found. Be prepared for nullptrs.

View File

@ -108,6 +108,7 @@ signals:
protected: protected:
DeviceManager *deviceManager() const; DeviceManager *deviceManager() const;
QList<Device*> myDevices() const; QList<Device*> myDevices() const;
HardwareManager *hardwareManager() const;
Device* findDeviceByParams(const ParamList &params) const; Device* findDeviceByParams(const ParamList &params) const;
// Radio 433 // Radio 433