From 6ffd8a61dbdb149190d5abf487d7ab30c1cae3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 28 Oct 2020 09:56:46 +0100 Subject: [PATCH] Introduce adapter and improve network manager --- libnymea-zigbee/libnymea-zigbee.pro | 2 + libnymea-zigbee/zigbee.h | 6 ++ libnymea-zigbee/zigbeeadapter.cpp | 106 +++++++++++++++++++++ libnymea-zigbee/zigbeeadapter.h | 77 +++++++++++++++ libnymea-zigbee/zigbeebridgecontroller.cpp | 2 +- libnymea-zigbee/zigbeenetwork.cpp | 1 + libnymea-zigbee/zigbeenetwork.h | 3 +- libnymea-zigbee/zigbeenetworkmanager.cpp | 58 ++++++++++- libnymea-zigbee/zigbeenetworkmanager.h | 10 +- 9 files changed, 253 insertions(+), 12 deletions(-) create mode 100644 libnymea-zigbee/zigbeeadapter.cpp create mode 100644 libnymea-zigbee/zigbeeadapter.h diff --git a/libnymea-zigbee/libnymea-zigbee.pro b/libnymea-zigbee/libnymea-zigbee.pro index 602d43d..830fa64 100644 --- a/libnymea-zigbee/libnymea-zigbee.pro +++ b/libnymea-zigbee/libnymea-zigbee.pro @@ -31,6 +31,7 @@ SOURCES += \ zdo/zigbeedeviceobject.cpp \ zdo/zigbeedeviceobjectreply.cpp \ zdo/zigbeedeviceprofile.cpp \ + zigbeeadapter.cpp \ zigbeeadpu.cpp \ zigbeebridgecontroller.cpp \ zigbeechannelmask.cpp \ @@ -88,6 +89,7 @@ HEADERS += \ zdo/zigbeedeviceobject.h \ zdo/zigbeedeviceobjectreply.h \ zdo/zigbeedeviceprofile.h \ + zigbeeadapter.h \ zigbeeadpu.h \ zigbeebridgecontroller.h \ zigbeechannelmask.h \ diff --git a/libnymea-zigbee/zigbee.h b/libnymea-zigbee/zigbee.h index d2248bc..ea03016 100644 --- a/libnymea-zigbee/zigbee.h +++ b/libnymea-zigbee/zigbee.h @@ -40,6 +40,12 @@ class Zigbee Q_GADGET public: + enum BackendType { + BackendTypeDeconz, + BackendTypeNxp + }; + Q_ENUM(BackendType) + enum ZigbeeProfile { ZigbeeProfileDevice = 0x0000, ZigbeeProfileIndustrialPlantMonitoring = 0x0101, diff --git a/libnymea-zigbee/zigbeeadapter.cpp b/libnymea-zigbee/zigbeeadapter.cpp new file mode 100644 index 0000000..1cee3c5 --- /dev/null +++ b/libnymea-zigbee/zigbeeadapter.cpp @@ -0,0 +1,106 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea-zigbee. +* This project including source code and documentation is protected by copyright law, and +* remains the property of nymea GmbH. All rights, including reproduction, publication, +* editing and translation, are reserved. The use of this project is subject to the terms of a +* license agreement to be concluded with nymea GmbH in accordance with the terms +* of use of nymea GmbH, available under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the terms of the GNU +* Lesser General Public License as published by the Free Software Foundation; version 3. +* this project 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 project. +* If not, see . +* +* For any further details and any questions please contact us under contact@nymea.io +* or see our FAQ/Licensing Information on https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "zigbeeadapter.h" + +ZigbeeAdapter::ZigbeeAdapter() +{ + +} + +QString ZigbeeAdapter::name() const +{ + return m_name; +} + +void ZigbeeAdapter::setName(const QString &name) +{ + m_name = name; +} + +QString ZigbeeAdapter::description() const +{ + return m_description; +} + +void ZigbeeAdapter::setDescription(const QString &description) +{ + m_description = description; +} + +QString ZigbeeAdapter::systemLocation() const +{ + return m_systemLocation; +} + +void ZigbeeAdapter::setSystemLocation(const QString &systemLocation) +{ + m_systemLocation = systemLocation; +} + +bool ZigbeeAdapter::backendSuggestionAvailable() const +{ + return m_backendSuggestionAvailable; +} + +void ZigbeeAdapter::setBackendSuggestionAvailable(bool backendSuggestionAvailable) +{ + m_backendSuggestionAvailable = backendSuggestionAvailable; +} + +Zigbee::BackendType ZigbeeAdapter::suggestedBackendType() const +{ + return m_suggestedBackendType; +} + +void ZigbeeAdapter::setSuggestedBackendType(Zigbee::BackendType backendType) +{ + m_suggestedBackendType = backendType; +} + +qint32 ZigbeeAdapter::suggestedBaudRate() const +{ + return m_suggestedBaudRate; +} + +void ZigbeeAdapter::setSuggestedBaudRate(qint32 baudRate) +{ + m_suggestedBaudRate = baudRate; +} + +QDebug operator<<(QDebug debug, const ZigbeeAdapter &adapter) +{ + debug.nospace() << "ZigbeeAdapter(" << adapter.name() << " - " << adapter.description(); + debug.nospace() << ", " << adapter.systemLocation(); + if (adapter.backendSuggestionAvailable()) { + debug.nospace() << "Suggested backend: " << adapter.suggestedBackendType(); + debug.nospace() << ", " << adapter.suggestedBaudRate(); + } + + debug.nospace() << ")"; + return debug.space(); +} diff --git a/libnymea-zigbee/zigbeeadapter.h b/libnymea-zigbee/zigbeeadapter.h new file mode 100644 index 0000000..5cf5881 --- /dev/null +++ b/libnymea-zigbee/zigbeeadapter.h @@ -0,0 +1,77 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea-zigbee. +* This project including source code and documentation is protected by copyright law, and +* remains the property of nymea GmbH. All rights, including reproduction, publication, +* editing and translation, are reserved. The use of this project is subject to the terms of a +* license agreement to be concluded with nymea GmbH in accordance with the terms +* of use of nymea GmbH, available under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the terms of the GNU +* Lesser General Public License as published by the Free Software Foundation; version 3. +* this project 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 project. +* If not, see . +* +* For any further details and any questions please contact us under contact@nymea.io +* or see our FAQ/Licensing Information on https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef ZIGBEEADAPTER_H +#define ZIGBEEADAPTER_H + +#include +#include + +#include "zigbee.h" + +class ZigbeeAdapter +{ + Q_GADGET + Q_PROPERTY(QString name READ name) + Q_PROPERTY(QString description READ description) + Q_PROPERTY(QString systemLocation READ systemLocation) + +public: + explicit ZigbeeAdapter(); + + QString name() const; + void setName(const QString &name); + + QString description() const; + void setDescription(const QString &description); + + QString systemLocation() const; + void setSystemLocation(const QString &systemLocation); + + bool backendSuggestionAvailable() const; + void setBackendSuggestionAvailable(bool backendSuggestionAvailable); + + Zigbee::BackendType suggestedBackendType() const; + void setSuggestedBackendType(Zigbee::BackendType backendType); + + qint32 suggestedBaudRate() const; + void setSuggestedBaudRate(qint32 baudRate); + +private: + QString m_name; + QString m_description; + QString m_systemLocation; + + bool m_backendSuggestionAvailable = false; + Zigbee::BackendType m_suggestedBackendType = Zigbee::BackendTypeDeconz; + qint32 m_suggestedBaudRate = 38400; +}; + +QDebug operator<<(QDebug debug, const ZigbeeAdapter &adapter); + + +#endif // ZIGBEEADAPTER_H diff --git a/libnymea-zigbee/zigbeebridgecontroller.cpp b/libnymea-zigbee/zigbeebridgecontroller.cpp index 8b62d2e..2a57878 100644 --- a/libnymea-zigbee/zigbeebridgecontroller.cpp +++ b/libnymea-zigbee/zigbeebridgecontroller.cpp @@ -30,7 +30,7 @@ ZigbeeBridgeController::ZigbeeBridgeController(QObject *parent) : QObject(parent) { - qRegisterMetaType(); + } QString ZigbeeBridgeController::firmwareVersion() const diff --git a/libnymea-zigbee/zigbeenetwork.cpp b/libnymea-zigbee/zigbeenetwork.cpp index 2f57db4..2cba55b 100644 --- a/libnymea-zigbee/zigbeenetwork.cpp +++ b/libnymea-zigbee/zigbeenetwork.cpp @@ -29,6 +29,7 @@ #include "zigbeenetwork.h" #include "loggingcategory.h" #include "zdo/zigbeedeviceprofile.h" +#include "zigbeebridgecontroller.h" #include #include diff --git a/libnymea-zigbee/zigbeenetwork.h b/libnymea-zigbee/zigbeenetwork.h index d5158d9..51de183 100644 --- a/libnymea-zigbee/zigbeenetwork.h +++ b/libnymea-zigbee/zigbeenetwork.h @@ -38,9 +38,10 @@ #include "zigbeechannelmask.h" #include "zigbeenodeendpoint.h" #include "zigbeenetworkdatabase.h" -#include "zigbeebridgecontroller.h" #include "zigbeesecurityconfiguration.h" +class ZigbeeBridgeController; + class ZigbeeNetwork : public QObject { Q_OBJECT diff --git a/libnymea-zigbee/zigbeenetworkmanager.cpp b/libnymea-zigbee/zigbeenetworkmanager.cpp index 46483cf..1ab2a36 100644 --- a/libnymea-zigbee/zigbeenetworkmanager.cpp +++ b/libnymea-zigbee/zigbeenetworkmanager.cpp @@ -32,21 +32,73 @@ #include "backends/deconz/zigbeenetworkdeconz.h" #include +#include + +QList ZigbeeNetworkManager::availableAdapters() +{ + QList adapters; + qCDebug(dcZigbeeNetwork()) << "Loading available adapters" ; + foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { + qCDebug(dcZigbeeNetwork()) << "Adapter candidate" << serialPortInfo.portName(); + qCDebug(dcZigbeeNetwork()) << " Description:" << serialPortInfo.description(); + qCDebug(dcZigbeeNetwork()) << " System location:" << serialPortInfo.systemLocation(); + qCDebug(dcZigbeeNetwork()) << " Manufacturer:" << serialPortInfo.manufacturer(); + qCDebug(dcZigbeeNetwork()) << " Serialnumber:" << serialPortInfo.serialNumber(); + + if (serialPortInfo.hasProductIdentifier()) { + qCDebug(dcZigbeeNetwork()) << " Product identifier:" << serialPortInfo.productIdentifier(); + } + + if (serialPortInfo.hasVendorIdentifier()) { + qCDebug(dcZigbeeNetwork()) << " Vendor identifier:" << serialPortInfo.vendorIdentifier(); + } + + // Check if we recognize this controller + ZigbeeAdapter adapter; + if (serialPortInfo.portName().isEmpty()) { + adapter.setName("Zigbee adapter"); + } else { + adapter.setName(serialPortInfo.portName()); + } + + if (serialPortInfo.description().isEmpty()) { + adapter.setDescription("Unknown"); + } else { + adapter.setDescription(serialPortInfo.description()); + } + adapter.setSystemLocation(serialPortInfo.systemLocation()); + + // Check if we recognize this adapter from USB information + if (serialPortInfo.manufacturer().toLower().contains("dresden elektronik")) { + adapter.setBackendSuggestionAvailable(true); + adapter.setSuggestedBackendType(Zigbee::BackendTypeDeconz); + adapter.setSuggestedBaudRate(38400); + } else if (serialPortInfo.manufacturer().toLower().contains("nxp")) { + adapter.setBackendSuggestionAvailable(true); + adapter.setSuggestedBackendType(Zigbee::BackendTypeNxp); + adapter.setSuggestedBaudRate(115200); + } + + adapters.append(adapter); + } + + return adapters; +} QStringList ZigbeeNetworkManager::availableBackendTypes() { return {"deCONZ", "NXP"}; } -ZigbeeNetwork *ZigbeeNetworkManager::createZigbeeNetwork(ZigbeeNetworkManager::BackendType backend, QObject *parent) +ZigbeeNetwork *ZigbeeNetworkManager::createZigbeeNetwork(Zigbee::BackendType backend, QObject *parent) { // Note: required for generating random PAN ID srand(static_cast(QDateTime::currentMSecsSinceEpoch() / 1000)); switch (backend) { - case BackendTypeNxp: + case Zigbee::BackendTypeNxp: return qobject_cast(new ZigbeeNetworkNxp(parent)); - case BackendTypeDeconz: + case Zigbee::BackendTypeDeconz: return qobject_cast(new ZigbeeNetworkDeconz(parent)); } diff --git a/libnymea-zigbee/zigbeenetworkmanager.h b/libnymea-zigbee/zigbeenetworkmanager.h index 2e86a22..ebc003a 100644 --- a/libnymea-zigbee/zigbeenetworkmanager.h +++ b/libnymea-zigbee/zigbeenetworkmanager.h @@ -30,20 +30,16 @@ #include +#include "zigbeeadapter.h" #include "zigbeenetwork.h" class ZigbeeNetworkManager { Q_GADGET public: - enum BackendType { - BackendTypeDeconz, - BackendTypeNxp - }; - Q_ENUM(BackendType) - + static QList availableAdapters(); static QStringList availableBackendTypes(); - static ZigbeeNetwork *createZigbeeNetwork(BackendType backend, QObject *parent = nullptr); + static ZigbeeNetwork *createZigbeeNetwork(Zigbee::BackendType backend, QObject *parent = nullptr); }; Q_DECLARE_METATYPE(ZigbeeNetworkManager)