From 045edd52aa0476d67f143082174b983efff9e6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 5 Jul 2018 11:21:33 +0200 Subject: [PATCH] Update bluetooth functionality --- libnymea-app-core/bluetoothinterface.cpp | 21 +++++------ .../discovery/bluetoothservicediscovery.cpp | 35 ++++++++++++++----- .../discovery/bluetoothservicediscovery.h | 5 +-- libnymea-app-core/discovery/discoverymodel.h | 8 ++--- .../discovery/nymeadiscovery.cpp | 7 ++-- libnymea-app-core/nymeaconnection.cpp | 6 ++++ nymea-app/ui/ConnectPage.qml | 31 +++++++++++----- 7 files changed, 77 insertions(+), 36 deletions(-) diff --git a/libnymea-app-core/bluetoothinterface.cpp b/libnymea-app-core/bluetoothinterface.cpp index ee5a9092..e9ba1e83 100644 --- a/libnymea-app-core/bluetoothinterface.cpp +++ b/libnymea-app-core/bluetoothinterface.cpp @@ -24,6 +24,7 @@ #include #include +#include BluetoothInterface::BluetoothInterface(QObject *parent) : NymeaInterface(parent) @@ -47,9 +48,12 @@ void BluetoothInterface::connect(const QUrl &url) return; } - QString macAddress = url.host(); - qDebug() << "Connecting to bluetooth server" << macAddress; - m_socket->connectToService(QBluetoothAddress(macAddress), QBluetoothUuid(QUuid("997936b5-d2cd-4c57-b41b-c6048320cd2b"))); + QUrlQuery query(url); + QString macAddressString = query.queryItemValue("mac"); + QBluetoothAddress macAddress = QBluetoothAddress(macAddressString); + + qDebug() << "Connecting to bluetooth server" << macAddressString << macAddress.toString(); + m_socket->connectToService(macAddress, QBluetoothUuid(QUuid("997936b5-d2cd-4c57-b41b-c6048320cd2b"))); } void BluetoothInterface::disconnect() @@ -99,13 +103,6 @@ void BluetoothInterface::onDisconnected() void BluetoothInterface::onDataReady() { - QByteArray message; - while (m_socket->canReadLine()) { - QByteArray dataLine = m_socket->readLine(); - message.append(dataLine); - if (dataLine.endsWith('\n')) { - emit dataReady(message); - message.clear(); - } - } + QByteArray data = m_socket->readAll(); + emit dataReady(data); } diff --git a/libnymea-app-core/discovery/bluetoothservicediscovery.cpp b/libnymea-app-core/discovery/bluetoothservicediscovery.cpp index f07684a9..6854725c 100644 --- a/libnymea-app-core/discovery/bluetoothservicediscovery.cpp +++ b/libnymea-app-core/discovery/bluetoothservicediscovery.cpp @@ -6,6 +6,9 @@ BluetoothServiceDiscovery::BluetoothServiceDiscovery(DiscoveryModel *discoveryMo QObject(parent), m_discoveryModel(discoveryModel) { + + m_nymeaServiceUuid = QBluetoothUuid(QUuid("997936b5-d2cd-4c57-b41b-c6048320cd2b")); + m_localDevice = new QBluetoothLocalDevice(this); connect(m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged, this, &BluetoothServiceDiscovery::onHostModeChanged); @@ -21,18 +24,25 @@ bool BluetoothServiceDiscovery::discovering() const bool BluetoothServiceDiscovery::available() const { - return m_available; + return m_localDevice->isValid() && !m_localDevice->hostMode() != QBluetoothLocalDevice::HostPoweredOff; } -void BluetoothServiceDiscovery::discover(const QBluetoothUuid &uuid) +void BluetoothServiceDiscovery::discover() { m_enabed = true; + if (!m_localDevice->isValid() || m_localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) { + qWarning() << "BluetoothServiceDiscovery: Not restart discovery, the bluetooth device is not available"; + return; + } + + m_serviceDiscovery->setUuidFilter(m_nymeaServiceUuid); + if (m_discovering) return; - qDebug() << "BluetoothServiceDiscovery: Start scanning services"; + qDebug() << "BluetoothServiceDiscovery: Start scanning for service" << m_nymeaServiceUuid.toString(); setDiscovering(true); - m_serviceDiscovery->setUuidFilter(uuid); + m_serviceDiscovery->setUuidFilter(m_nymeaServiceUuid); m_serviceDiscovery->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); } @@ -40,7 +50,7 @@ void BluetoothServiceDiscovery::stopDiscovery() { m_enabed = false; setDiscovering(false); - m_deviceDiscovery->stop(); + m_serviceDiscovery->stop(); } void BluetoothServiceDiscovery::setDiscovering(const bool &discovering) @@ -55,6 +65,11 @@ void BluetoothServiceDiscovery::setDiscovering(const bool &discovering) void BluetoothServiceDiscovery::onHostModeChanged(const QBluetoothLocalDevice::HostMode &mode) { qDebug() << "BluetoothServiceDiscovery: Host mode changed" << mode; + + if (mode != QBluetoothLocalDevice::HostPoweredOff && m_enabed) { + qDebug() << "Bluetooth available again, continue discovery"; + m_serviceDiscovery->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); + } } void BluetoothServiceDiscovery::onServiceDiscovered(const QBluetoothServiceInfo &serviceInfo) @@ -76,7 +91,7 @@ void BluetoothServiceDiscovery::onServiceDiscovered(const QBluetoothServiceInfo return; if (serviceInfo.serviceClassUuids().first() == QBluetoothUuid(QUuid("997936b5-d2cd-4c57-b41b-c6048320cd2b"))) { - qDebug() << "Found nymea rfcom service!"; + qDebug() << "BluetoothServiceDiscovery: Found nymea rfcom service!"; DiscoveryDevice* device = m_discoveryModel->find(serviceInfo.device().address()); if (!device) { @@ -100,8 +115,12 @@ void BluetoothServiceDiscovery::onServiceDiscoveryFinished() // If discover was called, but never stopDiscover, continue discovery if (m_enabed) { - qDebug() << "BluetoothServiceDiscovery: Restart bluetooth discovery"; - m_serviceDiscovery->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); + if (!m_localDevice->isValid() || m_localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) { + qWarning() << "BluetoothServiceDiscovery: Not restart discovery, the bluetooth adapter is not available."; + return; + } + qDebug() << "BluetoothServiceDiscovery: Restart service discovery"; + discover(); } } diff --git a/libnymea-app-core/discovery/bluetoothservicediscovery.h b/libnymea-app-core/discovery/bluetoothservicediscovery.h index 567e697d..1da95907 100644 --- a/libnymea-app-core/discovery/bluetoothservicediscovery.h +++ b/libnymea-app-core/discovery/bluetoothservicediscovery.h @@ -2,6 +2,7 @@ #define BLUETOOTHSERVICEDISCOVERY_H #include +#include #include #include @@ -16,14 +17,14 @@ public: bool discovering() const; bool available() const; - Q_INVOKABLE void discover(const QBluetoothUuid &uuid); + Q_INVOKABLE void discover(); Q_INVOKABLE void stopDiscovery(); private: DiscoveryModel *m_discoveryModel = nullptr; QBluetoothLocalDevice *m_localDevice = nullptr; - QBluetoothDeviceDiscoveryAgent *m_deviceDiscovery = nullptr; QBluetoothServiceDiscoveryAgent *m_serviceDiscovery = nullptr; + QBluetoothUuid m_nymeaServiceUuid; bool m_enabed = false; bool m_discovering = false; diff --git a/libnymea-app-core/discovery/discoverymodel.h b/libnymea-app-core/discovery/discoverymodel.h index badcfc31..e2c4c13a 100644 --- a/libnymea-app-core/discovery/discoverymodel.h +++ b/libnymea-app-core/discovery/discoverymodel.h @@ -48,9 +48,9 @@ public: void addDevice(DiscoveryDevice *device); - Q_INVOKABLE DiscoveryDevice* get(int index) const; - Q_INVOKABLE DiscoveryDevice* find(const QUuid &uuid); - Q_INVOKABLE DiscoveryDevice* find(const QBluetoothAddress &bluetoothAddress); + Q_INVOKABLE DiscoveryDevice *get(int index) const; + Q_INVOKABLE DiscoveryDevice *find(const QUuid &uuid); + Q_INVOKABLE DiscoveryDevice *find(const QBluetoothAddress &bluetoothAddress); void clearModel(); @@ -61,7 +61,7 @@ protected: QHash roleNames() const; private: - QList m_devices; + QList m_devices; }; #endif // DISCOVERYMODEL_H diff --git a/libnymea-app-core/discovery/nymeadiscovery.cpp b/libnymea-app-core/discovery/nymeadiscovery.cpp index 49bae5d5..1feee188 100644 --- a/libnymea-app-core/discovery/nymeadiscovery.cpp +++ b/libnymea-app-core/discovery/nymeadiscovery.cpp @@ -4,6 +4,9 @@ #include "zeroconfdiscovery.h" #include "bluetoothservicediscovery.h" +#include +#include + NymeaDiscovery::NymeaDiscovery(QObject *parent) : QObject(parent) { m_discoveryModel = new DiscoveryModel(this); @@ -26,9 +29,9 @@ void NymeaDiscovery::setDiscovering(bool discovering) m_discovering = discovering; // For zeroconf we'll ignore it as zeroconf doesn't do active discovery but just listens for changes in the net all the time if (discovering) { - //m_upnp->discover(); + m_upnp->discover(); // Note: this is the nymea uuid - m_bluetooth->discover(QBluetoothUuid(QUuid("997936b5-d2cd-4c57-b41b-c6048320cd2b"))); + m_bluetooth->discover(); } else { m_upnp->stopDiscovery(); m_bluetooth->stopDiscovery(); diff --git a/libnymea-app-core/nymeaconnection.cpp b/libnymea-app-core/nymeaconnection.cpp index 4ef00727..b07fed90 100644 --- a/libnymea-app-core/nymeaconnection.cpp +++ b/libnymea-app-core/nymeaconnection.cpp @@ -3,12 +3,14 @@ #include #include #include +#include #include #include #include "nymeainterface.h" #include "tcpsocketinterface.h" #include "websocketinterface.h" +#include "bluetoothinterface.h" NymeaConnection::NymeaConnection(QObject *parent) : QObject(parent) { @@ -17,6 +19,9 @@ NymeaConnection::NymeaConnection(QObject *parent) : QObject(parent) iface = new WebsocketInterface(this); registerInterface(iface); + + iface = new BluetoothInterface(this); + registerInterface(iface); } void NymeaConnection::connect(const QString &url) @@ -25,6 +30,7 @@ void NymeaConnection::connect(const QString &url) qWarning() << "Already connected. Cannot connect multiple times"; return; } + m_currentUrl = QUrl(url); m_currentInterface = m_interfaces.value(m_currentUrl.scheme()); if (!m_currentInterface) { diff --git a/nymea-app/ui/ConnectPage.qml b/nymea-app/ui/ConnectPage.qml index c1881875..973880ed 100644 --- a/nymea-app/ui/ConnectPage.qml +++ b/nymea-app/ui/ConnectPage.qml @@ -197,8 +197,21 @@ Page { ColorIcon { Layout.fillHeight: true Layout.preferredWidth: height - property bool hasSecurePort: discoveryDeviceDelegate.discoveryDevice.portConfigs.get(discoveryDeviceDelegate.defaultPortConfigIndex).sslEnabled - property bool isTrusted: Engine.connection.isTrusted(discoveryDeviceDelegate.discoveryDevice.toUrl(discoveryDeviceDelegate.defaultPortConfigIndex)) + property bool hasSecurePort: { + if (model.type === DiscoveryDevice.DeviceTypeNetwork) { + return discoveryDeviceDelegate.discoveryDevice.portConfigs.get(discoveryDeviceDelegate.defaultPortConfigIndex).sslEnabled + } else { + return false + } + + } + property bool isTrusted: { + if (model.type === DiscoveryDevice.DeviceTypeNetwork) { + Engine.connection.isTrusted(discoveryDeviceDelegate.discoveryDevice.toUrl(discoveryDeviceDelegate.defaultPortConfigIndex)) + } else { + return false + } + } visible: hasSecurePort name: "../images/network-secure.svg" color: isTrusted ? app.guhAccent : keyColor @@ -208,8 +221,8 @@ Page { onClicked: { if (model.type === DiscoveryDevice.DeviceTypeNetwork) { Engine.connection.connect(discoveryDevice.toUrl(defaultPortConfigIndex)) - } else if (model.type === DiscoveryDevice.DeviceTypeNetwork) { - Engine.connection.connect(discoveryDevice.toUrl(model.bluetoothAddress)) + } else if (model.type === DiscoveryDevice.DeviceTypeBluetooth) { + Engine.connection.connect("rfcom://bluetooth.local?mac=" + model.bluetoothAddress) } pageStack.push(connectingPage) @@ -225,9 +238,11 @@ Page { name: "../images/info.svg" } onClicked: { - swipe.close() - var popup = infoDialog.createObject(app,{discoveryDevice: discovery.discoveryModel.get(index)}) - popup.open() + if (model.type === DiscoveryDevice.DeviceTypeNetwork) { + swipe.close() + var popup = infoDialog.createObject(app,{discoveryDevice: discovery.discoveryModel.get(index)}) + popup.open() + } } } } @@ -351,7 +366,7 @@ Page { ColumnLayout { id: certLayout anchors.fill: parent -// spacing: app.margins + // spacing: app.margins RowLayout { Layout.fillWidth: true