Improve debug prints and rename adapter members

pull/11/head
Simon Stürz 2020-11-01 22:17:59 +01:00
parent d64b4711d1
commit 3d62162f5e
13 changed files with 144 additions and 77 deletions

View File

@ -36,8 +36,6 @@
ZigbeeBridgeControllerDeconz::ZigbeeBridgeControllerDeconz(QObject *parent) :
ZigbeeBridgeController(parent)
{
qRegisterMetaType<QSerialPort::SerialPortError>();
m_interface = new ZigbeeInterfaceDeconz(this);
connect(m_interface, &ZigbeeInterfaceDeconz::availableChanged, this, &ZigbeeBridgeControllerDeconz::onInterfaceAvailableChanged);
connect(m_interface, &ZigbeeInterfaceDeconz::packageReceived, this, &ZigbeeBridgeControllerDeconz::onInterfacePackageReceived);

View File

@ -37,6 +37,7 @@ ZigbeeNetworkDeconz::ZigbeeNetworkDeconz(const QUuid &networkUuid, QObject *pare
{
m_controller = new ZigbeeBridgeControllerDeconz(this);
connect(m_controller, &ZigbeeBridgeControllerDeconz::availableChanged, this, &ZigbeeNetworkDeconz::onControllerAvailableChanged);
connect(m_controller, &ZigbeeBridgeControllerDeconz::firmwareVersionChanged, this, &ZigbeeNetworkDeconz::firmwareVersionChanged);
connect(m_controller, &ZigbeeBridgeControllerDeconz::apsDataConfirmReceived, this, &ZigbeeNetworkDeconz::onApsDataConfirmReceived);
connect(m_controller, &ZigbeeBridgeControllerDeconz::apsDataIndicationReceived, this, &ZigbeeNetworkDeconz::onApsDataIndicationReceived);
@ -188,9 +189,9 @@ void ZigbeeNetworkDeconz::setCreateNetworkState(ZigbeeNetworkDeconz::CreateNetwo
QByteArray paramData;
QDataStream stream(&paramData, QIODevice::WriteOnly);
stream << static_cast<quint64>(0);
stream << static_cast<quint64>(extendedPanId());
stream.setByteOrder(QDataStream::LittleEndian);
qCDebug(dcZigbeeNetwork()) << "Configure APS extended PANID" << 0;
qCDebug(dcZigbeeNetwork()) << "Configure APS extended PANID" << extendedPanId();
ZigbeeInterfaceDeconzReply *reply = m_controller->requestWriteParameter(Deconz::ParameterApsExtendedPanId, paramData);
connect(reply, &ZigbeeInterfaceDeconzReply::finished, this, [this, reply](){
if (reply->statusCode() != Deconz::StatusCodeSuccess) {
@ -434,9 +435,17 @@ void ZigbeeNetworkDeconz::startNetworkInternally()
{
qCDebug(dcZigbeeNetwork()) << "Start zigbee network internally";
if (!m_database) {
QString networkDatabaseFileName = settingsDirectory().absolutePath() + QDir::separator() + QString("zigbee-network-%1.db").arg(networkUuid().toString().remove('{').remove('}'));
qCDebug(dcZigbeeNetwork()) << "Using ZigBee network database" << QFileInfo(networkDatabaseFileName).fileName();
m_database = new ZigbeeNetworkDatabase(this, networkDatabaseFileName, this);
}
m_createNewNetwork = false;
// Check if we have to create a pan ID and select the channel
if (panId() == 0 || !m_coordinatorNode) {
qCDebug(dcZigbeeNetwork()) << "Generate new extended PAN ID...";
setExtendedPanId(ZigbeeUtils::generateRandomPanId());
m_createNewNetwork = true;
}

View File

@ -1,3 +1,30 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* 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 <https://www.gnu.org/licenses/>.
*
* 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 NXP_H
#define NXP_H
@ -21,7 +48,6 @@ public:
CommandGetNetworkState = 0x08,
CommandSetPermitJoinCoordinator = 0x09,
CommandSendApsDataRequest = 0x20
};
Q_ENUM(Command)
@ -56,7 +82,6 @@ public:
};
Q_ENUM(LogLevel)
enum KeyType {
KeyTypeGlobalLinkKey = 0x00,
KeyTypeUniqueLinkKey = 0x01

View File

@ -36,6 +36,7 @@ ZigbeeNetworkNxp::ZigbeeNetworkNxp(const QUuid &networkUuid, QObject *parent) :
{
m_controller = new ZigbeeBridgeControllerNxp(this);
connect(m_controller, &ZigbeeBridgeControllerNxp::availableChanged, this, &ZigbeeNetworkNxp::onControllerAvailableChanged);
connect(m_controller, &ZigbeeBridgeControllerNxp::firmwareVersionChanged, this, &ZigbeeNetworkNxp::firmwareVersionChanged);
connect(m_controller, &ZigbeeBridgeControllerNxp::interfaceNotificationReceived, this, &ZigbeeNetworkNxp::onInterfaceNotificationReceived);
connect(m_controller, &ZigbeeBridgeControllerNxp::controllerStateChanged, this, &ZigbeeNetworkNxp::onControllerStateChanged);
connect(m_controller, &ZigbeeBridgeControllerNxp::apsDataConfirmReceived, this, &ZigbeeNetworkNxp::onApsDataConfirmReceived);
@ -324,8 +325,8 @@ void ZigbeeNetworkNxp::onControllerStateChanged(ZigbeeBridgeControllerNxp::Contr
qCDebug(dcZigbeeNetwork()) << "We already have the coordinator node. Network starting done.";
m_database->saveNode(m_coordinatorNode);
setState(StateRunning);
setPermitJoiningInternal(false);
setState(StateRunning);
return;
}
@ -355,7 +356,14 @@ void ZigbeeNetworkNxp::onControllerStateChanged(ZigbeeBridgeControllerNxp::Contr
setState(StateStarting);
break;
case ZigbeeBridgeControllerNxp::ControllerStateRunningUninitialized: {
// Create the database if there is no database available
setState(StateStarting);
if (!m_database) {
QString networkDatabaseFileName = settingsDirectory().absolutePath() + QDir::separator() + QString("zigbee-network-%1.db").arg(networkUuid().toString().remove('{').remove('}'));
qCDebug(dcZigbeeNetwork()) << "Using ZigBee network database" << QFileInfo(networkDatabaseFileName).fileName();
m_database = new ZigbeeNetworkDatabase(this, networkDatabaseFileName, this);
}
qCDebug(dcZigbeeNetwork()) << "Request controller version";
ZigbeeInterfaceNxpReply *reply = m_controller->requestVersion();
connect(reply, &ZigbeeInterfaceNxpReply::finished, this, [this, reply](){
@ -586,6 +594,6 @@ void ZigbeeNetworkNxp::factoryResetNetwork()
void ZigbeeNetworkNxp::destroyNetwork()
{
qCDebug(dcZigbeeNetwork()) << "Destroy network and delete the database";
m_controller->disable();
m_controller->disable();
clearSettings();
}

View File

@ -228,7 +228,7 @@ QDebug operator<<(QDebug debug, const ZigbeeDeviceProfile::MacCapabilities &macC
debug.nospace() << " Power source main power: " << macCapabilities.powerSourceFlagMainPower << "\n";
debug.nospace() << " Receiver on when idle: " << macCapabilities.receiverOnWhenIdle << "\n";
debug.nospace() << " Security capability: " << macCapabilities.securityCapability << "\n";
debug.nospace() << " Allocate address: " << macCapabilities.allocateAddress << "\n";
debug.nospace() << " Allocate address: " << macCapabilities.allocateAddress;
return debug;
}
@ -241,7 +241,7 @@ QDebug operator<<(QDebug debug, const ZigbeeDeviceProfile::ServerMask &serverMas
debug.nospace() << " Backup binding cache: " << serverMask.backupBindingCache << "\n";
debug.nospace() << " Primary discovery cache: " << serverMask.primaryDiscoveryCache << "\n";
debug.nospace() << " Backup discovery cache: " << serverMask.backupDiscoveryCache << "\n";
debug.nospace() << " Network manager: " << serverMask.networkManager << "\n";
debug.nospace() << " Network manager: " << serverMask.networkManager;
return debug;
}
@ -249,7 +249,7 @@ QDebug operator<<(QDebug debug, const ZigbeeDeviceProfile::DescriptorCapabilitie
{
debug.nospace() << "DescriptorCapabilities(" << ZigbeeUtils::convertByteToHexString(descriptorCapabilities.descriptorCapabilitiesFlag) << ")" << "\n";
debug.nospace() << " Extended active endpoint list available: " << descriptorCapabilities.extendedActiveEndpointListAvailable << "\n";
debug.nospace() << " Extended simple descriptor list available: " << descriptorCapabilities.extendedSimpleDescriptorListAvailable << "\n";
debug.nospace() << " Extended simple descriptor list available: " << descriptorCapabilities.extendedSimpleDescriptorListAvailable;
return debug;
}
@ -259,6 +259,6 @@ QDebug operator<<(QDebug debug, const ZigbeeDeviceProfile::PowerDescriptor &powe
debug.nospace() << " Power mode: " << powerDescriptor.powerMode << "\n";
debug.nospace() << " Available power sources: " << powerDescriptor.availablePowerSources << "\n";
debug.nospace() << " Power source: " << powerDescriptor.powerSource << "\n";
debug.nospace() << " Power level: " << powerDescriptor.powerLevel << "\n";
debug.nospace() << " Power level: " << powerDescriptor.powerLevel;
return debug;
}

View File

@ -30,7 +30,7 @@
ZigbeeBridgeController::ZigbeeBridgeController(QObject *parent) : QObject(parent)
{
qRegisterMetaType<QSerialPort::SerialPortError>();
}
QString ZigbeeBridgeController::firmwareVersion() const

View File

@ -112,6 +112,15 @@ void ZigbeeNetwork::setMacAddress(const ZigbeeAddress &zigbeeAddress)
emit macAddressChanged(m_macAddress);
}
QString ZigbeeNetwork::firmwareVersion() const
{
if (bridgeController()) {
return bridgeController()->firmwareVersion();
} else {
return QString();
}
}
quint16 ZigbeeNetwork::panId()
{
return m_panId;
@ -272,6 +281,34 @@ void ZigbeeNetwork::removeZigbeeNode(const ZigbeeAddress &address)
}
void ZigbeeNetwork::printNetwork()
{
qCDebug(dcZigbeeNetwork()) << this;
foreach (ZigbeeNode *node, nodes()) {
qCDebug(dcZigbeeNetwork()) << " ---> " << node;
qCDebug(dcZigbeeNetwork()) << " " << node->nodeDescriptor();
qCDebug(dcZigbeeNetwork()) << " " << node->powerDescriptor();
qCDebug(dcZigbeeNetwork()) << " Endpoints: " << node->endpoints().count();
foreach (ZigbeeNodeEndpoint *endpoint, node->endpoints()) {
qCDebug(dcZigbeeNetwork()) << " - " << endpoint;
qCDebug(dcZigbeeNetwork()) << " Input clusters:";
foreach (ZigbeeCluster *cluster, endpoint->inputClusters()) {
qCDebug(dcZigbeeNetwork()) << " - " << cluster;
foreach (const ZigbeeClusterAttribute &attribute, cluster->attributes()) {
qCDebug(dcZigbeeNetwork()) << " - " << attribute;
}
}
qCDebug(dcZigbeeNetwork()) << " Output clusters:";
foreach (ZigbeeCluster *cluster, endpoint->outputClusters()) {
qCDebug(dcZigbeeNetwork()) << " - " << cluster;
foreach (const ZigbeeClusterAttribute &attribute, cluster->attributes()) {
qCDebug(dcZigbeeNetwork()) << " - " << attribute;
}
}
}
}
}
void ZigbeeNetwork::addNodeInternally(ZigbeeNode *node)
{
if (m_nodes.contains(node)) {
@ -370,6 +407,8 @@ void ZigbeeNetwork::clearSettings()
if (!m_database->wipeDatabase()) {
qCWarning(dcZigbeeNetwork()) << "Failed to wipe the network database" << m_database->databaseName();
}
m_database->deleteLater();
m_database = nullptr;
}
// Reset network configurations
@ -444,7 +483,7 @@ void ZigbeeNetwork::setState(ZigbeeNetwork::State state)
m_state = state;
if (state == StateRunning) {
qCDebug(dcZigbeeNetwork()) << this;
printNetwork();
}
emit stateChanged(m_state);
}
@ -543,32 +582,11 @@ void ZigbeeNetwork::onNodeClusterAttributeChanged(ZigbeeCluster *cluster, const
QDebug operator<<(QDebug debug, ZigbeeNetwork *network)
{
debug.nospace().noquote() << "ZigbeeNetwork (" << ZigbeeUtils::convertUint16ToHexString(network->panId())
<< ", Channel " << network->channel()
<< ")" << "\n";
foreach (ZigbeeNode *node, network->nodes()) {
debug.nospace().noquote() << " ---> " << node << "\n";
debug.nospace().noquote() << " " << node->nodeDescriptor();
debug.nospace().noquote() << " " << node->powerDescriptor();
debug.nospace().noquote() << " Endpoints: " << node->endpoints().count() << "\n";
foreach (ZigbeeNodeEndpoint *endpoint, node->endpoints()) {
debug.nospace().noquote() << " - " << endpoint << "\n";
debug.nospace().noquote() << " Input clusters:" << "\n";
foreach (ZigbeeCluster *cluster, endpoint->inputClusters()) {
debug.nospace().noquote() << " - " << cluster << "\n";
foreach (const ZigbeeClusterAttribute &attribute, cluster->attributes()) {
debug.nospace().noquote() << " - " << attribute << "\n";
}
}
debug.nospace().noquote() << " Output clusters:" << "\n";
foreach (ZigbeeCluster *cluster, endpoint->outputClusters()) {
debug.nospace().noquote() << " - " << cluster << "\n";
foreach (const ZigbeeClusterAttribute &attribute, cluster->attributes()) {
debug.nospace().noquote() << " - " << attribute << "\n";
}
}
}
}
debug.nospace().noquote() << "ZigbeeNetwork(" << network->macAddress() << ", "
<< network->networkUuid().toString() << ", "
<< network->backendType() << ", "
<< "Channel: " << network->channel() << ", "
<< network->state()
<< ")";
return debug.space();
}

View File

@ -90,6 +90,8 @@ public:
void setMacAddress(const ZigbeeAddress &zigbeeAddress);
// Network configurations
QString firmwareVersion() const;
quint16 panId();
void setPanId(quint16 panId);
@ -108,6 +110,7 @@ public:
bool permitJoining() const;
void setPermitJoining(bool permitJoining);
quint8 generateSequenceNumber();
// Network nodes
@ -149,6 +152,8 @@ private:
QList<ZigbeeNode *> m_nodes;
QList<ZigbeeNode *> m_uninitializedNodes;
void printNetwork();
private:
void addNodeInternally(ZigbeeNode *node);
void removeNodeInternally(ZigbeeNode *node);
@ -192,6 +197,7 @@ signals:
void serialPortNameChanged(const QString &serialPortName);
void serialBaudrateChanged(qint32 serialBaudrate);
void macAddressChanged(const ZigbeeAddress &macAddress);
void firmwareVersionChanged(const QString &firmwareVersion);
void panIdChanged(quint16 panId);
void extendedPanIdChanged(quint64 extendedPanId);

View File

@ -39,9 +39,9 @@ ZigbeeNetworkDatabase::ZigbeeNetworkDatabase(ZigbeeNetwork *network, const QStri
m_network(network),
m_databaseName(databaseName)
{
m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), "zigbee");
m_connectionName = QFileInfo(m_databaseName).baseName();
m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connectionName);
m_db.setDatabaseName(m_databaseName);
qCDebug(dcZigbeeNetworkDatabase()) << "Opening zigbee network database" << m_db.databaseName();
if (!m_db.isValid()) {
qCWarning(dcZigbeeNetworkDatabase()) << "The zigbee network database is not valid" << m_db.databaseName();
@ -49,6 +49,7 @@ ZigbeeNetworkDatabase::ZigbeeNetworkDatabase(ZigbeeNetwork *network, const QStri
return;
}
qCDebug(dcZigbeeNetworkDatabase()) << "Opening zigbee network database" << m_db.databaseName();
if (!initDatabase()) {
qCWarning(dcZigbeeNetworkDatabase()) << "Failed to initialize the database" << m_db.databaseName();
// FIXME: rotate database
@ -58,10 +59,9 @@ ZigbeeNetworkDatabase::ZigbeeNetworkDatabase(ZigbeeNetwork *network, const QStri
ZigbeeNetworkDatabase::~ZigbeeNetworkDatabase()
{
if (m_db.isOpen()) {
qCDebug(dcZigbeeNetworkDatabase()) << "Closing database" << m_db.databaseName();
m_db.close();
}
m_db.close();
m_db = QSqlDatabase();
QSqlDatabase::removeDatabase(m_connectionName);
}
QString ZigbeeNetworkDatabase::databaseName() const
@ -184,6 +184,8 @@ bool ZigbeeNetworkDatabase::wipeDatabase()
return false;
}
m_db.close();
m_db = QSqlDatabase();
QSqlDatabase::removeDatabase(m_connectionName);
// Delete database file
QFile databaseFile(m_databaseName);

View File

@ -55,6 +55,7 @@ public:
private:
ZigbeeNetwork *m_network = nullptr;
QString m_databaseName;
QString m_connectionName;
QSqlDatabase m_db;
bool initDatabase();

View File

@ -62,43 +62,43 @@ void ZigbeeUartAdapter::setSystemLocation(const QString &systemLocation)
m_systemLocation = systemLocation;
}
bool ZigbeeUartAdapter::backendSuggestionAvailable() const
bool ZigbeeUartAdapter::hardwareRecognized() const
{
return m_backendSuggestionAvailable;
return m_hardwareRecognized;
}
void ZigbeeUartAdapter::setBackendSuggestionAvailable(bool backendSuggestionAvailable)
void ZigbeeUartAdapter::setHardwareRecognized(bool hardwareRecognized)
{
m_backendSuggestionAvailable = backendSuggestionAvailable;
m_hardwareRecognized = hardwareRecognized;
}
Zigbee::ZigbeeBackendType ZigbeeUartAdapter::suggestedZigbeeBackendType() const
Zigbee::ZigbeeBackendType ZigbeeUartAdapter::zigbeeBackend() const
{
return m_suggestedZigbeeBackendType;
return m_zigbeeBackend;
}
void ZigbeeUartAdapter::setSuggestedZigbeeBackendType(Zigbee::ZigbeeBackendType backendType)
void ZigbeeUartAdapter::setBackendType(Zigbee::ZigbeeBackendType backendType)
{
m_suggestedZigbeeBackendType = backendType;
m_zigbeeBackend = backendType;
}
qint32 ZigbeeUartAdapter::suggestedBaudRate() const
qint32 ZigbeeUartAdapter::baudRate() const
{
return m_suggestedBaudRate;
return m_baudRate;
}
void ZigbeeUartAdapter::setSuggestedBaudRate(qint32 baudRate)
void ZigbeeUartAdapter::setBaudRate(qint32 baudRate)
{
m_suggestedBaudRate = baudRate;
m_baudRate = baudRate;
}
QDebug operator<<(QDebug debug, const ZigbeeUartAdapter &adapter)
{
debug.nospace() << "ZigbeeUartAdapter(" << adapter.name() << " - " << adapter.description();
debug.nospace() << ", " << adapter.systemLocation();
if (adapter.backendSuggestionAvailable()) {
debug.nospace() << "Suggested backend: " << adapter.suggestedZigbeeBackendType();
debug.nospace() << ", " << adapter.suggestedBaudRate();
if (adapter.hardwareRecognized()) {
debug.nospace() << "Suggested backend: " << adapter.zigbeeBackend();
debug.nospace() << ", " << adapter.baudRate();
}
debug.nospace() << ")";

View File

@ -47,23 +47,23 @@ public:
QString systemLocation() const;
void setSystemLocation(const QString &systemLocation);
bool backendSuggestionAvailable() const;
void setBackendSuggestionAvailable(bool backendSuggestionAvailable);
bool hardwareRecognized() const;
void setHardwareRecognized(bool hardwareRecognized);
Zigbee::ZigbeeBackendType suggestedZigbeeBackendType() const;
void setSuggestedZigbeeBackendType(Zigbee::ZigbeeBackendType backendType);
Zigbee::ZigbeeBackendType zigbeeBackend() const;
void setBackendType(Zigbee::ZigbeeBackendType backendType);
qint32 suggestedBaudRate() const;
void setSuggestedBaudRate(qint32 baudRate);
qint32 baudRate() const;
void setBaudRate(qint32 baudRate);
private:
QString m_name;
QString m_description;
QString m_systemLocation;
bool m_backendSuggestionAvailable = false;
Zigbee::ZigbeeBackendType m_suggestedZigbeeBackendType = Zigbee::ZigbeeBackendTypeDeconz;
qint32 m_suggestedBaudRate = 38400;
bool m_hardwareRecognized = false;
Zigbee::ZigbeeBackendType m_zigbeeBackend = Zigbee::ZigbeeBackendTypeDeconz;
qint32 m_baudRate = 38400;
};
Q_DECLARE_METATYPE(ZigbeeUartAdapter)

View File

@ -224,13 +224,13 @@ void ZigbeeUartAdapterMonitor::addAdapterInternally(const QString &systemLocatio
// Check if we recognize this adapter from USB information
if (serialPortInfo.manufacturer().toLower().contains("dresden elektronik")) {
adapter.setBackendSuggestionAvailable(true);
adapter.setSuggestedZigbeeBackendType(Zigbee::ZigbeeBackendTypeDeconz);
adapter.setSuggestedBaudRate(38400);
adapter.setHardwareRecognized(true);
adapter.setBackendType(Zigbee::ZigbeeBackendTypeDeconz);
adapter.setBaudRate(38400);
} else if (serialPortInfo.manufacturer().toLower().contains("nxp")) {
adapter.setBackendSuggestionAvailable(true);
adapter.setSuggestedZigbeeBackendType(Zigbee::ZigbeeBackendTypeNxp);
adapter.setSuggestedBaudRate(115200);
adapter.setHardwareRecognized(true);
adapter.setBackendType(Zigbee::ZigbeeBackendTypeNxp);
adapter.setBaudRate(115200);
}
qCDebug(dcZigbeeAdapterMonitor()) << "Added" << adapter;