add bluetooth manager

pull/135/head
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
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<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);
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. */

View File

@ -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<QBluetoothDeviceInfo> m_deviceInfos;
QTimer *m_timer;

View File

@ -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<DevicePlugin *> DeviceManager::plugins() const
{

View File

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

View File

@ -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()

View File

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

View File

@ -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

View File

@ -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();
}
}

View File

@ -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<AvahiServiceEntry> serviceEntries() const;
signals:
@ -54,6 +54,9 @@ public slots:
bool disable();
private:
explicit QtAvahiServiceBrowser(QObject *parent = 0);
~QtAvahiServiceBrowser();
QtAvahiServiceBrowserPrivate *d_ptr;
QList<AvahiServiceEntry> m_serviceEntries;

View File

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

View File

@ -506,6 +506,11 @@ QList<Device *> 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.

View File

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