Continue restructuring and reach compilable state

This commit is contained in:
Simon Stürz 2020-02-27 21:53:53 +01:00
parent f870c705af
commit 899ad4e3b9
17 changed files with 1113 additions and 1514 deletions

View File

@ -4,10 +4,10 @@ TARGET = nymea-zigbee1
TEMPLATE = lib
SOURCES += \
interface/zigbeeinterface.cpp \
interface/zigbeeinterfacemessage.cpp \
interface/zigbeeinterfacerequest.cpp \
interface/zigbeeinterfacereply.cpp \
nxp/interface/zigbeeinterface.cpp \
nxp/interface/zigbeeinterfacemessage.cpp \
nxp/interface/zigbeeinterfacerequest.cpp \
nxp/interface/zigbeeinterfacereply.cpp \
nxp/zigbeenetworknxp.cpp \
nxp/zigbeebridgecontrollernxp.cpp \
zigbeecluster.cpp \
@ -23,10 +23,10 @@ SOURCES += \
zigbeeaddress.cpp \
HEADERS += \
interface/zigbeeinterface.h \
interface/zigbeeinterfacemessage.h \
interface/zigbeeinterfacerequest.h \
interface/zigbeeinterfacereply.h \
nxp/interface/zigbeeinterface.h \
nxp/interface/zigbeeinterfacemessage.h \
nxp/interface/zigbeeinterfacerequest.h \
nxp/interface/zigbeeinterfacereply.h \
nxp/zigbeenetworknxp.h \
nxp/zigbeebridgecontrollernxp.h \
zigbeecluster.h \

View File

@ -32,7 +32,7 @@
#include <QTimer>
#include <QSerialPort>
#include "../zigbee.h"
#include "../../zigbee.h"
#include "zigbeeinterfacemessage.h"
class ZigbeeInterface : public QObject

View File

@ -31,7 +31,7 @@
#include <QObject>
#include <QDebug>
#include "../zigbee.h"
#include "../../zigbee.h"
class ZigbeeInterfaceMessage
{

View File

@ -33,12 +33,12 @@
#include "zigbeeinterfacerequest.h"
class ZigbeeBridgeController;
class ZigbeeBridgeControllerNxp;
class ZigbeeInterfaceReply : public QObject
{
Q_OBJECT
friend class ZigbeeBridgeController;
friend class ZigbeeBridgeControllerNxp;
public:
enum Status {
@ -80,7 +80,7 @@ private:
quint8 m_sequenceNumber;
QString m_statusErrorMessage;
// Called by ZigbeeBridgeController
// Called by ZigbeeBridgeControllerNxp
void setStatusMessage(const ZigbeeInterfaceMessage &statusMessage);
void setAdditionalMessage(const ZigbeeInterfaceMessage &additionalMessage);

View File

@ -25,31 +25,31 @@
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "zigbeebridgecontroller.h"
#include "zigbeebridgecontrollernxp.h"
#include "loggingcategory.h"
#include "zigbeeutils.h"
#include <QDataStream>
ZigbeeBridgeController::ZigbeeBridgeController(QObject *parent) :
ZigbeeBridgeControllerNxp::ZigbeeBridgeControllerNxp(QObject *parent) :
QObject(parent)
{
m_interface = new ZigbeeInterface(this);
connect(m_interface, &ZigbeeInterface::availableChanged, this, &ZigbeeBridgeController::availableChanged);
connect(m_interface, &ZigbeeInterface::messageReceived, this, &ZigbeeBridgeController::onMessageReceived);
connect(m_interface, &ZigbeeInterface::availableChanged, this, &ZigbeeBridgeControllerNxp::availableChanged);
connect(m_interface, &ZigbeeInterface::messageReceived, this, &ZigbeeBridgeControllerNxp::onMessageReceived);
}
ZigbeeBridgeController::~ZigbeeBridgeController()
ZigbeeBridgeControllerNxp::~ZigbeeBridgeControllerNxp()
{
qCDebug(dcZigbeeController()) << "Destroy controller";
}
bool ZigbeeBridgeController::available() const
bool ZigbeeBridgeControllerNxp::available() const
{
return m_interface->available();
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandResetController()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandResetController()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeReset, QByteArray()));
request.setDescription("Reset controller");
@ -58,7 +58,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandResetController()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSoftResetController()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSoftResetController()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeZllFactoryNew, QByteArray()));
request.setDescription("Soft reset controller");
@ -67,7 +67,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSoftResetController()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandErasePersistantData()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandErasePersistantData()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeErasePersistentData, QByteArray()));
request.setDescription("Erase persistent data");
@ -75,7 +75,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandErasePersistantData()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandGetVersion()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandGetVersion()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeGetVersion, QByteArray()));
request.setDescription("Get version");
@ -84,7 +84,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandGetVersion()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetExtendedPanId(quint64 extendedPanId)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSetExtendedPanId(quint64 extendedPanId)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -96,7 +96,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetExtendedPanId(quint64 ex
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetChannelMask(quint32 channelMask)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSetChannelMask(quint32 channelMask)
{
// Note: 10 < value < 27 -> using sinle channel value
// 0x07fff800 select from all channels 11 - 26
@ -116,7 +116,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetChannelMask(quint32 chan
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetNodeType(ZigbeeNode::NodeType nodeType)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSetNodeType(ZigbeeNode::NodeType nodeType)
{
quint8 deviceTypeValue = 0;
if (nodeType == ZigbeeNode::NodeTypeEndDevice) {
@ -146,7 +146,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetNodeType(ZigbeeNode::Nod
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandStartNetwork()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandStartNetwork()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeStartNetwork, QByteArray()));
request.setDescription("Start network");
@ -156,7 +156,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandStartNetwork()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandStartScan()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandStartScan()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeStartScan, QByteArray()));
request.setDescription("Start scan");
@ -166,7 +166,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandStartScan()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandPermitJoin(quint16 targetAddress, const quint8 advertisingIntervall, bool tcSignificance)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandPermitJoin(quint16 targetAddress, const quint8 advertisingIntervall, bool tcSignificance)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -180,7 +180,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandPermitJoin(quint16 targetAd
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandGetPermitJoinStatus()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandGetPermitJoinStatus()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeGetPermitJoining, QByteArray()));
request.setDescription("Get permit joining status");
@ -190,7 +190,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandGetPermitJoinStatus()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandRequestActiveEndpoints(quint16 shortAddress)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandRequestActiveEndpoints(quint16 shortAddress)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -204,7 +204,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandRequestActiveEndpoints(quin
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandRequestLinkQuality(quint16 shortAddress)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandRequestLinkQuality(quint16 shortAddress)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -219,7 +219,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandRequestLinkQuality(quint16
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandEnableWhiteList()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandEnableWhiteList()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeNetworkWhitelistEnable, QByteArray()));
request.setDescription("Enable whitelist");
@ -227,7 +227,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandEnableWhiteList()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandInitiateTouchLink()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandInitiateTouchLink()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeInitiateTouchlink, QByteArray()));
request.setDescription("Initiate touch link");
@ -235,7 +235,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandInitiateTouchLink()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandTouchLinkFactoryReset()
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandTouchLinkFactoryReset()
{
ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeTouchlinkFactoryReset, QByteArray()));
request.setDescription("Touch link factory reset");
@ -243,7 +243,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandTouchLinkFactoryReset()
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandNetworkAddressRequest(quint16 targetAddress, quint64 extendedAddress)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandNetworkAddressRequest(quint16 targetAddress, quint64 extendedAddress)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -260,7 +260,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandNetworkAddressRequest(quint
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetSecurityStateAndKey(quint8 keyState, quint8 keySequence, quint8 keyType, const QString &key)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSetSecurityStateAndKey(quint8 keyState, quint8 keySequence, quint8 keyType, const QString &key)
{
// Note: calls ZPS_vAplSecSetInitialSecurityState
@ -287,7 +287,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSetSecurityStateAndKey(quin
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandAuthenticateDevice(const ZigbeeAddress &ieeeAddress, const QString &key)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandAuthenticateDevice(const ZigbeeAddress &ieeeAddress, const QString &key)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -302,7 +302,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandAuthenticateDevice(const Zi
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandNodeDescriptorRequest(quint16 shortAddress)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandNodeDescriptorRequest(quint16 shortAddress)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -316,7 +316,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandNodeDescriptorRequest(quint
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandSimpleDescriptorRequest(quint16 shortAddress, quint8 endpoint)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandSimpleDescriptorRequest(quint16 shortAddress, quint8 endpoint)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -331,7 +331,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandSimpleDescriptorRequest(qui
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandPowerDescriptorRequest(quint16 shortAddress)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandPowerDescriptorRequest(quint16 shortAddress)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -345,7 +345,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandPowerDescriptorRequest(quin
return sendRequest(request);
}
ZigbeeInterfaceReply *ZigbeeBridgeController::commandUserDescriptorRequest(quint16 shortAddress, quint16 address)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::commandUserDescriptorRequest(quint16 shortAddress, quint16 address)
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
@ -360,7 +360,7 @@ ZigbeeInterfaceReply *ZigbeeBridgeController::commandUserDescriptorRequest(quint
return sendRequest(request);
}
void ZigbeeBridgeController::sendMessage(ZigbeeInterfaceReply *reply)
void ZigbeeBridgeControllerNxp::sendMessage(ZigbeeInterfaceReply *reply)
{
if (!reply)
return;
@ -372,7 +372,7 @@ void ZigbeeBridgeController::sendMessage(ZigbeeInterfaceReply *reply)
reply->startTimer(reply->request().timeoutIntervall());
}
void ZigbeeBridgeController::onMessageReceived(const ZigbeeInterfaceMessage &message)
void ZigbeeBridgeControllerNxp::onMessageReceived(const ZigbeeInterfaceMessage &message)
{
// Check if we have a current reply
if (m_currentReply) {
@ -412,7 +412,7 @@ void ZigbeeBridgeController::onMessageReceived(const ZigbeeInterfaceMessage &mes
}
void ZigbeeBridgeController::onReplyTimeout()
void ZigbeeBridgeControllerNxp::onReplyTimeout()
{
m_currentReply->setFinished();
m_currentReply = nullptr;
@ -422,21 +422,21 @@ void ZigbeeBridgeController::onReplyTimeout()
}
bool ZigbeeBridgeController::enable(const QString &serialPort, qint32 baudrate)
bool ZigbeeBridgeControllerNxp::enable(const QString &serialPort, qint32 baudrate)
{
return m_interface->enable(serialPort, baudrate);
}
void ZigbeeBridgeController::disable()
void ZigbeeBridgeControllerNxp::disable()
{
m_interface->disable();
}
ZigbeeInterfaceReply *ZigbeeBridgeController::sendRequest(const ZigbeeInterfaceRequest &request)
ZigbeeInterfaceReply *ZigbeeBridgeControllerNxp::sendRequest(const ZigbeeInterfaceRequest &request)
{
// Create Reply
ZigbeeInterfaceReply *reply = new ZigbeeInterfaceReply(request);
connect(reply, &ZigbeeInterfaceReply::timeout, this, &ZigbeeBridgeController::onReplyTimeout);
connect(reply, &ZigbeeInterfaceReply::timeout, this, &ZigbeeBridgeControllerNxp::onReplyTimeout);
// If reply running, enqueue, else send request
if (m_currentReply) {

View File

@ -38,12 +38,12 @@
#include "interface/zigbeeinterfacerequest.h"
#include "interface/zigbeeinterfacemessage.h"
class ZigbeeBridgeController : public QObject
class ZigbeeBridgeControllerNxp : public QObject
{
Q_OBJECT
public:
explicit ZigbeeBridgeController(QObject *parent = nullptr);
~ZigbeeBridgeController();
explicit ZigbeeBridgeControllerNxp(QObject *parent = nullptr);
~ZigbeeBridgeControllerNxp();
bool available() const;

View File

@ -1,7 +1,936 @@
#include "zigbeenetworknxp.h"
#include "../loggingcategory.h"
#include "../zigbeeutils.h"
#include <QDataStream>
ZigbeeNetworkNxp::ZigbeeNetworkNxp(QObject *parent) :
ZigbeeNetwork(parent)
{
}
void ZigbeeNetworkNxp::setStartingState(ZigbeeNetworkNxp::StartingState state)
{
if (m_startingState == state)
return;
m_startingState = state;
switch (m_startingState) {
case StartingStateNone:
break;
case StartingStateErase: {
m_networkRunning = false;
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Erase persistant data";
ZigbeeInterfaceReply *reply = m_controller->commandErasePersistantData();
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandErasePersistentDataFinished);
break;
}
case StartingStateReset: {
m_networkRunning = false;
// qCDebug(dcZigbeeNetwork()) << "";
// ZigbeeInterfaceReply *reply = m_controller->commandSoftResetController();
// connect(reply, &ZigbeeInterfaceReply::finished, this, [this, reply](){
// reply->deleteLater();
// });
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Reset controller";
ZigbeeInterfaceReply *reply = m_controller->commandResetController();
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandResetControllerFinished);
break;
}
case StartingStateGetVersion: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Get controller version";
readControllerVersion();
break;
}
case StartingStateSetPanId: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Set PAN ID";
if (extendedPanId() == 0) {
setExtendedPanId(ZigbeeUtils::generateRandomPanId());
}
ZigbeeInterfaceReply *reply = m_controller->commandSetExtendedPanId(extendedPanId());
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandSetExtendedPanIdFinished);
break;
}
case StartingStateSetChannel: {
// Create channel mask
// Note: normal number passed, that specific channel will be used || Bitfield: all channels would be 0x07FFF800
// 0x07fff800 select from all channels 11 - 26
// 0x02108800 primary zigbee light link channels 11, 15, 20, 25
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Set channel mask";
ZigbeeInterfaceReply *reply = nullptr;
if (channel() == 0) {
qCDebug(dcZigbeeNetwork()) << "Autoselect quitest channel for the zigbee network. FIXME: currently hardcoded to 13 due to firmware error.";
quint32 channelMask = 0;
channelMask |= 1 << (13);
reply = m_controller->commandSetChannelMask(channelMask);
} else {
quint32 channelMask = 0;
channelMask |= 1 << (channel());
qCDebug(dcZigbeeNetwork()) << "Using channel" << channel() << "for the zigbee network.";
reply = m_controller->commandSetChannelMask(channelMask);
}
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandSetChannelMaskFinished);
break;
}
case StartingStateSetSecurity: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Set security configuration";
ZigbeeInterfaceReply *reply = m_controller->commandSetSecurityStateAndKey(4, 0, 1, "5A6967426565416C6C69616E63653039");
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandSetSecurityFinished);
break;
}
case StartingStateSetNodeType: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Set node type";
ZigbeeInterfaceReply *reply = m_controller->commandSetNodeType(ZigbeeNode::NodeTypeCoordinator);
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandSetNodeTypeFinished);
break;
}
case StartingStateStartNetwork: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Starting network";
ZigbeeInterfaceReply *reply = m_controller->commandStartNetwork();
connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandStartNetworkFinished);
break;
}
case StartingStateGetPermitJoinStatus: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Get permit join status";
readPermitJoinStatus();
break;
}
case StartingStateReadeNodeDescriptor: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Read coordinator node descriptor";
//ZigbeeInterfaceReply *reply = m_controller->commandNodeDescriptorRequest(0x0000);
//connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandNodeDescriptorRequestFinished);
break;
}
case StartingStateReadPowerDescriptor: {
qCDebug(dcZigbeeNetwork()) << "Starting state changed: Read coordinator power descriptor";
//ZigbeeInterfaceReply *reply = m_controller->commandPowerDescriptorRequest(0x0000);
//connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandPowerDescriptorRequestFinished);
break;
}
}
}
void ZigbeeNetworkNxp::readControllerVersion()
{
ZigbeeInterfaceReply *reply = m_controller->commandGetVersion();
connect(reply, &ZigbeeInterfaceReply::finished, this, [this, reply](){
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
if (reply->additionalMessage().data().count() != 4) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << ":" << "Invalid payload size";
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
// Parse version
quint16 majorVersion = ZigbeeUtils::convertByteArrayToUint16(reply->additionalMessage().data().mid(0, 2));
quint16 minorVersion = ZigbeeUtils::convertByteArrayToUint16(reply->additionalMessage().data().mid(2, 2));
//m_controllerFirmwareVersion = QString("%1.%2").arg(majorVersion).arg(minorVersion);
qCDebug(dcZigbeeNetwork()) << "Controller version:" << QString("%1.%2").arg(majorVersion).arg(minorVersion);
if (m_startingState == StartingStateGetVersion) setStartingState(StartingStateSetPanId);
});
}
void ZigbeeNetworkNxp::readPermitJoinStatus()
{
ZigbeeInterfaceReply *reply = m_controller->commandGetPermitJoinStatus();
connect(reply, &ZigbeeInterfaceReply::finished, this, [this, reply](){
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
qCDebug(dcZigbeeController()) << reply->additionalMessage();
setPermitJoining(static_cast<bool>(reply->additionalMessage().data().at(0)));
if (m_startingState == StartingStateGetPermitJoinStatus) setStartingState(StartingStateReadeNodeDescriptor);
});
}
void ZigbeeNetworkNxp::onCommandInitiateTouchLinkFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
}
void ZigbeeNetworkNxp::onCommandTouchLinkFactoryResetFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
}
void ZigbeeNetworkNxp::onCommandRequestLinkQualityFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
qCDebug(dcZigbeeController()) << reply->additionalMessage();
// FIXME: parsing
quint8 sequenceNumber = static_cast<quint8>(reply->additionalMessage().data().at(0));
quint8 statusCode = static_cast<quint8>(reply->additionalMessage().data().at(1));
quint8 neighborTableEntries = static_cast<quint8>(reply->additionalMessage().data().at(2));
quint8 neighborTableListCount = static_cast<quint8>(reply->additionalMessage().data().at(3));
quint8 startIndex = static_cast<quint8>(reply->additionalMessage().data().at(4));
qCDebug(dcZigbeeNetwork()) << "LQI response:";
qCDebug(dcZigbeeNetwork()) << " SQN:" << ZigbeeUtils::convertByteToHexString(sequenceNumber);
qCDebug(dcZigbeeNetwork()) << " Status:" << ZigbeeUtils::convertByteToHexString(statusCode);
qCDebug(dcZigbeeNetwork()) << " Neighbor table entries:" << neighborTableEntries;
qCDebug(dcZigbeeNetwork()) << " Neighbor table list count:" << neighborTableListCount;
qCDebug(dcZigbeeNetwork()) << " Start index:" << startIndex;
int offset = 5;
// Note: according to docs, if the table has no neigbors the list will be empty
if (neighborTableEntries == 0) {
qCDebug(dcZigbeeNetwork()) << " There are no neigbors";
return;
}
for (int i = startIndex; i < neighborTableListCount; i++) {
quint16 shortAddress = ZigbeeUtils::convertByteArrayToUint16(reply->additionalMessage().data().mid(offset, 2));
quint64 panId = ZigbeeUtils::convertByteArrayToUint64(reply->additionalMessage().data().mid(offset + 2, 8));
quint64 ieeeAddress = ZigbeeUtils::convertByteArrayToUint64(reply->additionalMessage().data().mid(offset + 10, 8));
quint8 depth = static_cast<quint8>(reply->additionalMessage().data().at(offset + 18));
quint8 linkQuality = static_cast<quint8>(reply->additionalMessage().data().at(offset + 19));
quint8 bitMap = static_cast<quint8>(reply->additionalMessage().data().at(offset + 20));
offset += 21;
qCDebug(dcZigbeeNetwork()) << " Neighbor:" << i;
qCDebug(dcZigbeeNetwork()) << " Address:" << ZigbeeUtils::convertUint16ToHexString(shortAddress);
qCDebug(dcZigbeeNetwork()) << " PAN id:" << panId;
qCDebug(dcZigbeeNetwork()) << " Extended address:" << ZigbeeAddress(ieeeAddress);
qCDebug(dcZigbeeNetwork()) << " Depth:" << depth;
qCDebug(dcZigbeeNetwork()) << " Link quality:" << linkQuality;
qCDebug(dcZigbeeNetwork()) << " BitMap:" << ZigbeeUtils::convertByteToHexString(bitMap);
// foreach (ZigbeeNode *node, nodes()) {
// if (node->extendedAddress() == ZigbeeAddress(ieeeAddress)) {
// node->setShortAddress(shortAddress);
// }
// }
}
}
void ZigbeeNetworkNxp::processLoggingMessage(const ZigbeeInterfaceMessage &message)
{
quint8 logLevel = static_cast<quint8>(message.data().at(0));
QString logMessage = QString::fromUtf8(message.data().right(message.data().count() - 1));
QString logLevelString;
switch (logLevel) {
case 0:
logLevelString = "Emergency:";
break;
case 1:
logLevelString = "Alert:";
break;
case 2:
logLevelString = "Critical:";
break;
case 3:
logLevelString = "Error:";
break;
case 4:
logLevelString = "Warning:";
break;
case 5:
logLevelString = "Notice:";
break;
case 6:
logLevelString = "Information:";
break;
case 7:
logLevelString = "Debug:";
break;
default:
logLevelString = "Unknown:";
break;
}
if (logLevel < 5) {
qCWarning(dcZigbeeController()).noquote() << "ControllerLog:" << logLevelString << logMessage;
} else {
qCDebug(dcZigbeeController()).noquote() << "ControllerLog:" << logLevelString << logMessage;
}
}
void ZigbeeNetworkNxp::processFactoryNewRestart(const ZigbeeInterfaceMessage &message)
{
quint8 controllerStatus = static_cast<quint8>(message.data().at(0));
QString controllerStatusString;
switch (controllerStatus) {
case 0:
controllerStatusString = "startup";
break;
case 1:
controllerStatusString = "wait start";
break;
case 2:
controllerStatusString = "NRF start";
break;
case 3:
controllerStatusString = "discovery";
break;
case 4:
controllerStatusString = "network init";
break;
case 5:
controllerStatusString = "rescan";
break;
case 6:
controllerStatusString = "running";
break;
default:
qCWarning(dcZigbeeNetwork()) << "Unhandled controller status" << controllerStatus;
break;
}
qCDebug(dcZigbeeNetwork()) << "Restart finished. Current controller state:" << controllerStatusString;
if (m_startingState == StartingStateReset) setStartingState(StartingStateGetVersion);
}
void ZigbeeNetworkNxp::processNodeClusterList(const ZigbeeInterfaceMessage &message)
{
quint8 sourceEndpoint = static_cast<quint8>(message.data().at(0));
quint16 profileId = static_cast<quint8>(message.data().at(1));
profileId <<= 8;
profileId |= static_cast<quint8>(message.data().at(2));
qCDebug(dcZigbeeController()) << "Node cluster list received:";
qCDebug(dcZigbeeController()) << " Souce endpoint:" << sourceEndpoint;
qCDebug(dcZigbeeController()) << " Profile:" << ZigbeeUtils::profileIdToString(static_cast<Zigbee::ZigbeeProfile>(profileId));
QByteArray clusterListData = message.data().right(message.data().count() - 3);
for (int i = 0; i < clusterListData.count(); i += 2) {
quint16 clusterId = static_cast<quint16>(clusterListData.at(i));
clusterId <<= 8;
clusterId |= clusterListData .at(i + 1);
qCDebug(dcZigbeeController()) << " Cluster ID:" << ZigbeeUtils::clusterIdToString(static_cast<Zigbee::ClusterId>(clusterId));
}
}
void ZigbeeNetworkNxp::processNodeAttributeList(const ZigbeeInterfaceMessage &message)
{
quint8 sourceEndpoint = static_cast<quint8>(message.data().at(0));
quint16 profileId = ZigbeeUtils::convertByteArrayToUint16(message.data().mid(1, 2));
quint16 clusterId = ZigbeeUtils::convertByteArrayToUint16(message.data().mid(3, 2));
qCDebug(dcZigbeeController()) << "Node attribute list received:";
qCDebug(dcZigbeeController()) << " Souce endpoint:" << sourceEndpoint;
qCDebug(dcZigbeeController()) << " Profile:" << ZigbeeUtils::profileIdToString(static_cast<Zigbee::ZigbeeProfile>(profileId));
qCDebug(dcZigbeeController()) << " Cluster ID:" << ZigbeeUtils::clusterIdToString(static_cast<Zigbee::ClusterId>(clusterId));
QByteArray attributeListData = message.data().right(message.data().count() - 5);
if (attributeListData.count() % 2 != 0) {
qCWarning(dcZigbeeController()) << "Attribute list is incomplete. Truncate last byte";
attributeListData = attributeListData.left(attributeListData.count() - 1);
}
qCWarning(dcZigbeeController()) << "Attributes" << ZigbeeUtils::convertByteArrayToHexString(attributeListData);
for (int i = 0; i < attributeListData.count(); i += 2) {
quint16 attribute = ZigbeeUtils::convertByteArrayToUint16(attributeListData.mid(i, 2));
qCDebug(dcZigbeeController()) << " Attribute:" << ZigbeeUtils::convertUint16ToHexString(attribute);
}
}
void ZigbeeNetworkNxp::processNodeCommandIdList(const ZigbeeInterfaceMessage &message)
{
quint8 sourceEndpoint = static_cast<quint8>(message.data().at(0));
quint16 profileId = ZigbeeUtils::convertByteArrayToUint16(message.data().mid(1, 2));
quint16 clusterId = ZigbeeUtils::convertByteArrayToUint16(message.data().mid(3, 2));
qCDebug(dcZigbeeController()) << "Node command list received:";
qCDebug(dcZigbeeController()) << " Souce endpoint:" << sourceEndpoint;
qCDebug(dcZigbeeController()) << " Profile:" << ZigbeeUtils::profileIdToString(static_cast<Zigbee::ZigbeeProfile>(profileId));
qCDebug(dcZigbeeController()) << " Cluster ID:" << ZigbeeUtils::clusterIdToString(static_cast<Zigbee::ClusterId>(clusterId));
QByteArray commandListData = message.data().right(message.data().count() - 5);
for (int i = 0; i < commandListData.count(); i++) {
quint8 attribute = static_cast<quint8>(commandListData.at(i));
qCDebug(dcZigbeeController()) << " Command:" << ZigbeeUtils::convertByteToHexString(attribute);
}
}
void ZigbeeNetworkNxp::processDeviceAnnounce(const ZigbeeInterfaceMessage &message)
{
QByteArray data = message.data();
quint16 shortAddress = 0;
quint64 ieeeAddress = 0;
quint8 macCapabilitiesFlag = 0;
QDataStream stream(&data, QIODevice::ReadOnly);
stream >> shortAddress >> ieeeAddress >> macCapabilitiesFlag;
qCDebug(dcZigbeeNetwork()) << "Device announced:";
qCDebug(dcZigbeeNetwork()) << " Address:" << ZigbeeUtils::convertUint16ToHexString(shortAddress);
qCDebug(dcZigbeeNetwork()) << " Extended address:" << ZigbeeAddress(ieeeAddress);
qCDebug(dcZigbeeNetwork()) << " Mac capabilities:" << ZigbeeUtils::convertByteToHexString(macCapabilitiesFlag);
// Check if we already have a node with this
// ZigbeeNode *node = createNode();
// node->setShortAddress(shortAddress);
// node->setExtendedAddress(ZigbeeAddress(ieeeAddress));
// node->setMacCapabilitiesFlag(macCapabilitiesFlag);
// qCDebug(dcZigbeeNetwork()) << " Node:" << node;
// // FIXME: check if node already added, and if we have to update it
// addUnitializedNode(node);
// node->setState(StateInitializing);
// ZigbeeInterfaceReply *reply = nullptr;
// reply = m_controller->commandAuthenticateDevice(node->extendedAddress(), securityConfiguration().globalTrustCenterLinkKey());
// connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandAuthenticateDeviceFinished);
// reply = m_controller->commandNodeDescriptorRequest(node->shortAddress());
// connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandNodeDescriptorRequestFinished);
// reply = m_controller->commandSimpleDescriptorRequest(node->shortAddress(), endPoint());
// connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandSimpleDescriptorRequestFinished);
// reply = m_controller->commandPowerDescriptorRequest(node->shortAddress());
// connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onCommandPowerDescriptorRequestFinished);
}
void ZigbeeNetworkNxp::processAttributeReport(const ZigbeeInterfaceMessage &message)
{
QByteArray data = message.data();
quint8 sequenceNumber = 0;
quint16 sourceAddress = 0;
quint8 endPoint = 0;
quint16 clusterId = 0;
quint16 attributeId = 0;
quint8 attributeStatus = 0;
quint8 attributDataType = 0;
quint16 dataSize = 0;
QDataStream stream(&data, QIODevice::ReadOnly);
stream >> sequenceNumber >> sourceAddress >> endPoint >> clusterId >> attributeId >> attributeStatus >> attributDataType >> dataSize;
Zigbee::DataType dataType = static_cast<Zigbee::DataType>(attributDataType);
QByteArray attributeData = data.right(dataSize);
if (attributeData.length() != dataSize) {
qCCritical(dcZigbeeNetwork()) << "HACK" << attributeData.length() << "!=" << dataSize;
// Note: the NXP firmware for JN5169 has a bug here and does not send the attributeStatus.
// Repars data without attribute status
sequenceNumber = 0;
sourceAddress = 0;
endPoint = 0;
clusterId = 0;
attributeId = 0;
attributeStatus = 0;
attributDataType = 0;
dataSize = 0;
QDataStream alternativeStream(&data, QIODevice::ReadOnly);
alternativeStream >> sequenceNumber >> sourceAddress >> endPoint >> clusterId >> attributeId >> attributDataType >> dataSize;
dataType = static_cast<Zigbee::DataType>(attributDataType);
attributeData = data.right(dataSize);
}
qCDebug(dcZigbeeNetwork()) << "Attribute report:";
qCDebug(dcZigbeeNetwork()) << " SQN:" << ZigbeeUtils::convertByteToHexString(sequenceNumber);
qCDebug(dcZigbeeNetwork()) << " Source address:" << ZigbeeUtils::convertUint16ToHexString(sourceAddress);
qCDebug(dcZigbeeNetwork()) << " End point:" << ZigbeeUtils::convertByteToHexString(endPoint);
qCDebug(dcZigbeeNetwork()) << " Cluster:" << ZigbeeUtils::clusterIdToString(static_cast<Zigbee::ClusterId>(clusterId));
qCDebug(dcZigbeeNetwork()) << " Attribut id:" << ZigbeeUtils::convertUint16ToHexString(attributeId);
qCDebug(dcZigbeeNetwork()) << " Attribut data type:" << dataType;
qCDebug(dcZigbeeNetwork()) << " Attribut size:" << dataSize;
qCDebug(dcZigbeeNetwork()) << " Data:" << ZigbeeUtils::convertByteArrayToHexString(attributeData);
switch (dataType) {
case Zigbee::CharString:
qCDebug(dcZigbeeNetwork()) << " Data(converted)" << QString::fromUtf8(attributeData);
break;
case Zigbee::Bool:
qCDebug(dcZigbeeNetwork()) << " Data(converted)" << static_cast<bool>(attributeData.at(0));
break;
default:
break;
}
// FIXME
// ZigbeeNode *node = getZigbeeNode(sourceAddress);
// if (!node) {
// qCWarning(dcZigbeeNode()) << "Received an attribute report from an unknown node. Ignoring data.";
// return;
// }
// node->setClusterAttribute(static_cast<Zigbee::ClusterId>(clusterId), ZigbeeClusterAttribute(attributeId, dataType, attributeData));
}
void ZigbeeNetworkNxp::processLeaveIndication(const ZigbeeInterfaceMessage &message)
{
QByteArray data = message.data();
quint64 extendedAddress = 0;
bool rejoining = 0;
QDataStream stream(&data, QIODevice::ReadOnly);
stream >> extendedAddress;
stream >> rejoining;
ZigbeeAddress address(extendedAddress);
qCDebug(dcZigbeeNetwork()) << "Node leaving indication:" << address.toString() << "rejoining:" << rejoining;
ZigbeeNode *node = getZigbeeNode(address);
if (node) removeNode(node);
}
void ZigbeeNetworkNxp::processRestartProvisioned(const ZigbeeInterfaceMessage &message)
{
if (message.data().isEmpty())
return;
quint8 status = static_cast<quint8>(message.data().at(0));
switch (status) {
case 0:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: start up";
break;
case 1:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: wait start";
break;
case 2:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: NFN start";
break;
case 3:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: discovery";
break;
case 4:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: network init";
break;
case 5:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: rescan";
break;
case 6:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: running";
break;
default:
qCDebug(dcZigbeeNetwork()) << "Restart provisioned: unknown";
break;
}
if (m_startingState == StartingStateReset) {
if (m_networkRunning) {
qCDebug(dcZigbeeNetwork()) << "Reset finished. Network already running. No need to set it up";
setStartingState(StartingStateGetPermitJoinStatus);
} else {
qCDebug(dcZigbeeNetwork()) << "Reset finished. Set up network";
setStartingState(StartingStateGetVersion);
}
}
}
void ZigbeeNetworkNxp::processRouterDiscoveryConfirm(const ZigbeeInterfaceMessage &message)
{
if (message.data().isEmpty())
return;
quint8 status = static_cast<quint8>(message.data().at(0));
Zigbee::ZigbeeNwkLayerStatus networkStatus = static_cast<Zigbee::ZigbeeNwkLayerStatus>(message.data().at(0));
qCDebug(dcZigbeeNetwork()) << "Router discovery confirm received" << status << networkStatus;
}
void ZigbeeNetworkNxp::onMessageReceived(const ZigbeeInterfaceMessage &message)
{
switch (message.messageType()) {
case Zigbee::MessageTypeLogging:
processLoggingMessage(message);
break;
case Zigbee::MessageTypeFactoryNewRestart:
processFactoryNewRestart(message);
break;
case Zigbee::MessageTypeNodeClusterList:
processNodeClusterList(message);
break;
case Zigbee::MessageTypeNodeAttributeList:
processNodeAttributeList(message);
break;
case Zigbee::MessageTypeNodeCommandIdList:
processNodeCommandIdList(message);
break;
case Zigbee::MessageTypeDeviceAnnounce:
processDeviceAnnounce(message);
break;
case Zigbee::MessageTypeAttributeReport:
processAttributeReport(message);
break;
case Zigbee::MessageTypeLeaveIndication:
processLeaveIndication(message);
break;
case Zigbee::MessageTypeNetworkJoinedFormed:
processNetworkFormed(message);
break;
case Zigbee::MessageTypeRestartProvisioned:
processRestartProvisioned(message);
break;
case Zigbee::MessageTypeRouterDiscoveryConfirm:
processRouterDiscoveryConfirm(message);
break;
default:
qCWarning(dcZigbeeController()) << "Unhandled message received:" << message;
break;
}
}
void ZigbeeNetworkNxp::onControllerAvailableChanged(bool available)
{
qCDebug(dcZigbeeNetwork()) << "Hardware controller is" << (available ? "now available" : "not available");
if (m_factoryResetting && !available) {
setStartingState(StartingStateReset);
return;
}
if (!available) {
// FIXME
// foreach (ZigbeeNode *node, nodes()) {
// node->setConnected(false);
// }
setError(ErrorHardwareUnavailable);
setPermitJoining(false);
setState(StateOffline);
setStartingState(StartingStateNone);
} else {
setError(ErrorNoError);
setState(StateStarting);
setStartingState(StartingStateReset);
}
}
//void ZigbeeNetworkNxp::requestMatchDescriptor(const quint16 &shortAddress, const Zigbee::ZigbeeProfile &profile)
//{
// // TargetAddress profile InputClusterCount InputClusterList OutputClusterCount OutputClusterList
// Q_UNUSED(profile)
// QByteArray data;
// QDataStream stream(&data, QIODevice::WriteOnly);
// stream << shortAddress;
// stream << static_cast<quint16>(0xFFFF);
// stream << static_cast<quint8>(0);
// stream << static_cast<quint16>(0);
// stream << static_cast<quint8>(0);
// stream << static_cast<quint16>(0);
// ZigbeeInterfaceRequest request(ZigbeeInterfaceMessage(Zigbee::MessageTypeMatchDescriptorRequest, data));
// request.setExpectedAdditionalMessageType(Zigbee::MessageTypeMatchDescriptorResponse);
// request.setDescription("Request match descriptors " + ZigbeeUtils::convertUint16ToHexString(shortAddress));
// request.setTimoutIntervall(5000);
// ZigbeeInterfaceReply *reply = controller()->sendRequest(request);
// connect(reply, &ZigbeeInterfaceReply::finished, this, &ZigbeeNetworkNxp::onRequestMatchDescriptorFinished);
//}
void ZigbeeNetworkNxp::onCommandResetControllerFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
}
void ZigbeeNetworkNxp::onCommandSoftResetControllerFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
}
void ZigbeeNetworkNxp::onCommandErasePersistentDataFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
m_factoryResetting = false;
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
if (m_startingState == StartingStateErase) {
setStartingState(StartingStateReset);
}
}
void ZigbeeNetworkNxp::onCommandSetExtendedPanIdFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
if (m_startingState == StartingStateSetPanId) setStartingState(StartingStateSetChannel);
}
void ZigbeeNetworkNxp::onCommandSetChannelMaskFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeNetwork()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
if (m_startingState == StartingStateSetChannel) setStartingState(StartingStateSetSecurity);
}
void ZigbeeNetworkNxp::onCommandSetNodeTypeFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
if (m_startingState == StartingStateSetNodeType) setStartingState(StartingStateStartNetwork);
}
void ZigbeeNetworkNxp::onCommandStartNetworkFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeNetwork()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
qCDebug(dcZigbeeController()) << reply->additionalMessage();
processNetworkFormed(reply->additionalMessage());
if (m_startingState == StartingStateStartNetwork) setStartingState(StartingStateGetPermitJoinStatus);
}
void ZigbeeNetworkNxp::onCommandStartScanFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
qCDebug(dcZigbeeController()) << reply->additionalMessage();
processNetworkFormed(reply->additionalMessage());
}
void ZigbeeNetworkNxp::onCommandRequestMatchDescriptorFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
qCDebug(dcZigbeeController()) << reply->additionalMessage();
}
void ZigbeeNetworkNxp::onCommandSetSecurityFinished()
{
ZigbeeInterfaceReply *reply = static_cast<ZigbeeInterfaceReply *>(sender());
reply->deleteLater();
if (reply->status() != ZigbeeInterfaceReply::Success) {
qCWarning(dcZigbeeController()) << "Could not" << reply->request().description() << reply->status() << reply->statusErrorMessage();
return;
}
qCDebug(dcZigbeeController()) << reply->request().description() << "finished successfully";
if (m_startingState == StartingStateSetSecurity) setStartingState(StartingStateSetNodeType);
}
void ZigbeeNetworkNxp::processNetworkFormed(const ZigbeeInterfaceMessage &message)
{
// Parse network status
QByteArray data = message.data();
quint8 networkStatus = static_cast<quint8>(data.at(0));
QString networkStatusString;
bool success = false;
if (networkStatus == 0) {
networkStatusString = "joined";
success = true;
} else if (networkStatus == 1) {
networkStatusString = "created";
success = true;
} else if (networkStatus >= 128 && networkStatus <= 244) {
networkStatusString = "failed: Zigbee event code: " + QString::number(networkStatus);
} else {
networkStatusString = "unknown";
}
if (!success) {
qCWarning(dcZigbeeNetwork()) << "Forming network failed" << networkStatusString;
setPermitJoining(false);
setStartingState(StartingStateNone);
setState(StateOffline);
setError(ErrorZigbeeError);
m_networkRunning = false;
return;
}
quint16 shortAddress = ZigbeeUtils::convertByteArrayToUint16(data.mid(1, 2));
quint64 extendedAddress = ZigbeeUtils::convertByteArrayToUint64(data.mid(3, 8));
// Parse network channel
quint8 channel = static_cast<quint8>(data.at(11));
qCDebug(dcZigbeeNetwork()).noquote() << "Network" << networkStatusString;
qCDebug(dcZigbeeNetwork()) << " Extended PAN ID:" << extendedPanId();
qCDebug(dcZigbeeNetwork()) << " Address:" << ZigbeeUtils::convertUint16ToHexString(shortAddress);
qCDebug(dcZigbeeNetwork()) << " Extended address:" << ZigbeeAddress(extendedAddress);
qCDebug(dcZigbeeNetwork()) << " Channel:" << channel;
qCDebug(dcZigbeeNetwork()) << " Permit joining:" << permitJoining();
m_networkRunning = true;
// FIXME: create coordinator node
// // Set the node information
// setShortAddress(shortAddress);
// setExtendedAddress(ZigbeeAddress(extendedAddress));
// setChannel(channel);
// if (!hasNode(this->shortAddress()))
// addUnitializedNode(this);
}
void ZigbeeNetworkNxp::startNetwork()
{
qCDebug(dcZigbeeNetwork()) << "Start network...";
if (m_controller) {
qCDebug(dcZigbeeNetwork()) << "Clean up old controller...";
delete m_controller;
m_controller = nullptr;
}
qCDebug(dcZigbeeNetwork()) << "Create new controller...";
m_controller = new ZigbeeBridgeControllerNxp(this);
connect(m_controller, &ZigbeeBridgeControllerNxp::messageReceived, this, &ZigbeeNetworkNxp::onMessageReceived);
connect(m_controller, &ZigbeeBridgeControllerNxp::availableChanged, this, &ZigbeeNetworkNxp::onControllerAvailableChanged);
if (state() == StateUninitialized)
loadNetwork();
if (!m_controller->enable(serialPortName(), serialBaudrate())) {
setPermitJoining(false);
setState(StateOffline);
setStartingState(StartingStateNone);
setError(ErrorHardwareUnavailable);
} else {
// Reset
setStartingState(StartingStateReset);
setState(StateStarting);
}
}
void ZigbeeNetworkNxp::stopNetwork()
{
qCDebug(dcZigbeeNetwork()) << "Stopping network...";
setState(StateStopping);
if (m_controller) {
delete m_controller;
m_controller = nullptr;
}
setStartingState(StartingStateNone);
setPermitJoining(false);
setState(StateOffline);
setError(ErrorNoError);
}
void ZigbeeNetworkNxp::factoryResetNetwork()
{
qCDebug(dcZigbeeNetwork()) << "Factory reset network and forget all information. This cannot be undone.";
clearSettings();
setState(StateStarting);
setStartingState(StartingStateErase);
}

View File

@ -3,6 +3,7 @@
#include <QObject>
#include "../zigbeenetwork.h"
#include "zigbeebridgecontrollernxp.h"
class ZigbeeNetworkNxp : public ZigbeeNetwork
{
@ -10,7 +11,71 @@ class ZigbeeNetworkNxp : public ZigbeeNetwork
public:
explicit ZigbeeNetworkNxp(QObject *parent = nullptr);
signals:
private:
enum StartingState {
StartingStateNone,
StartingStateErase,
StartingStateReset,
StartingStateGetVersion,
StartingStateSetPanId,
StartingStateSetChannel,
StartingStateSetSecurity,
StartingStateSetNodeType,
StartingStateStartNetwork,
StartingStateGetPermitJoinStatus,
StartingStateReadeNodeDescriptor,
StartingStateReadPowerDescriptor
};
ZigbeeBridgeControllerNxp *m_controller = nullptr;
bool m_networkRunning = false;
bool m_factoryResetting = false;
StartingState m_startingState = StartingStateNone;
void setStartingState(StartingState state);
void readControllerVersion();
void readPermitJoinStatus();
private slots:
void onMessageReceived(const ZigbeeInterfaceMessage &message);
void onControllerAvailableChanged(bool available);
// Controller command finished slots
void onCommandResetControllerFinished();
void onCommandSoftResetControllerFinished();
void onCommandErasePersistentDataFinished();
void onCommandSetExtendedPanIdFinished();
void onCommandSetChannelMaskFinished();
void onCommandSetNodeTypeFinished();
void onCommandStartNetworkFinished();
void onCommandStartScanFinished();
void onCommandEnableWhitelistFinished();
// void onCommandNodeDescriptorRequestFinished();
// void onCommandSimpleDescriptorRequestFinished();
// void onCommandPowerDescriptorRequestFinished();
void onCommandInitiateTouchLinkFinished();
void onCommandTouchLinkFactoryResetFinished();
void onCommandRequestLinkQualityFinished();
void onCommandRequestMatchDescriptorFinished();
void onCommandSetSecurityFinished();
void onCommandNetworkAddressRequestFinished();
void onCommandAuthenticateDeviceFinished();
// Process controller notifications/messages
void processNetworkFormed(const ZigbeeInterfaceMessage &message);
void processLoggingMessage(const ZigbeeInterfaceMessage &message);
void processFactoryNewRestart(const ZigbeeInterfaceMessage &message);
void processNodeClusterList(const ZigbeeInterfaceMessage &message);
void processNodeAttributeList(const ZigbeeInterfaceMessage &message);
void processNodeCommandIdList(const ZigbeeInterfaceMessage &message);
void processDeviceAnnounce(const ZigbeeInterfaceMessage &message);
void processAttributeReport(const ZigbeeInterfaceMessage &message);
void processLeaveIndication(const ZigbeeInterfaceMessage &message);
void processRestartProvisioned(const ZigbeeInterfaceMessage &message);
void processRouterDiscoveryConfirm(const ZigbeeInterfaceMessage &message);
public slots:
void startNetwork() override;

View File

@ -129,6 +129,21 @@ void ZigbeeNetwork::setSecurityConfiguration(const ZigbeeSecurityConfiguration &
emit securityConfigurationChanged(m_securityConfiguration);
}
bool ZigbeeNetwork::permitJoining() const
{
return m_permitJoining;
}
void ZigbeeNetwork::setPermitJoining(bool permitJoining)
{
if (m_permitJoining == permitJoining)
return;
qCDebug(dcZigbeeNetwork()) << "Permit joining changed to" << permitJoining;
m_permitJoining = permitJoining;
emit permitJoiningChanged(m_permitJoining);
}
QList<ZigbeeNode *> ZigbeeNetwork::nodes() const
{
return m_nodes;
@ -242,7 +257,7 @@ void ZigbeeNetwork::loadNetwork()
foreach (const QString ieeeAddressString, settings.childGroups()) {
settings.beginGroup(ieeeAddressString);
ZigbeeNode *node = createNode();
ZigbeeNode *node = new ZigbeeNode(this);
node->setExtendedAddress(ZigbeeAddress(ieeeAddressString));
node->setShortAddress(static_cast<quint16>(settings.value("nwkAddress", 0).toUInt()));
node->setMacCapabilitiesFlag(static_cast<quint8>(settings.value("macCapabilitiesFlag", 0).toUInt()));
@ -285,8 +300,8 @@ void ZigbeeNetwork::loadNetwork()
// settings.endGroup(); // clusterId
// }
// settings.endGroup(); // outputCluster
node->setState(StateInitialized);
//FIXME
//node->setState(StateInitialized);
addNodeInternally(node);
settings.endGroup(); // ieeeAddress

View File

@ -41,7 +41,7 @@ class ZigbeeNetwork : public QObject
public:
enum State {
StateUninitialized,
StateDisconnected,
StateOffline,
StateStarting,
StateRunning,
StateStopping
@ -81,6 +81,9 @@ public:
ZigbeeSecurityConfiguration securityConfiguration() const;
void setSecurityConfiguration(const ZigbeeSecurityConfiguration &securityConfiguration);
bool permitJoining() const;
void setPermitJoining(bool permitJoining);
QList<ZigbeeNode *> nodes() const;
ZigbeeNode *coordinatorNode() const;
@ -105,6 +108,7 @@ private:
uint m_channel = 0;
ZigbeeSecurityConfiguration m_securityConfiguration;
ZigbeeNode::NodeType m_nodeType = ZigbeeNode::NodeTypeCoordinator;
bool m_permitJoining = false;
QString m_settingsFileName = "/etc/nymea/nymea-zigbee.conf";
QList<ZigbeeNode *> m_nodes;
@ -125,8 +129,6 @@ protected:
void addUnitializedNode(ZigbeeNode *node);
void removeNode(ZigbeeNode *node);
ZigbeeNode *createNode();
void setState(State state);
void setError(Error error);

File diff suppressed because it is too large Load Diff

View File

@ -29,109 +29,38 @@
#define ZIGBEEMANAGER_H
#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include "zigbeenetwork.h"
#include "zigbeeaddress.h"
#include "zigbeebridgecontroller.h"
#include "zigbeesecurityconfiguration.h"
class ZigbeeNetworkManager : public QObject
{
Q_OBJECT
public:
explicit ZigbeeNetworkManager(QObject *parent = nullptr);
enum BackendType {
BackendTypeNxp
};
Q_ENUM(BackendType)
QString controllerFirmwareVersion() const;
explicit ZigbeeNetworkManager(const QSerialPortInfo &serialPortInfo, QSerialPort::BaudRate baudrate, BackendType backendType, QObject *parent = nullptr);
// Note: temporary public available for debugging
ZigbeeBridgeController *controller() const;
QSerialPortInfo serialPortInfo() const;
QSerialPort::BaudRate baudrate() const;
BackendType backendType() const;
bool networkRunning() const;
ZigbeeNetwork *network() const;
// Note: Follwoing methods should be abstract
bool permitJoining() const;
void setPermitJoining(bool permitJoining);
void resetController();
private:
enum StartingState {
StartingStateNone,
StartingStateErase,
StartingStateReset,
StartingStateGetVersion,
StartingStateSetPanId,
StartingStateSetChannel,
StartingStateSetSecurity,
StartingStateSetNodeType,
StartingStateStartNetwork,
StartingStateGetPermitJoinStatus,
StartingStateReadeNodeDescriptor,
StartingStateReadPowerDescriptor
};
ZigbeeBridgeController *m_controller = nullptr;
QString m_controllerFirmwareVersion;
StartingState m_startingState = StartingStateNone;
void setStartingState(StartingState state);
QList<ZigbeeNode *> m_uninitializedNodes;
bool m_permitJoining = false;
bool m_networkRunning = false;
bool m_factoryResetting = false;
void readControllerVersion();
void readPermitJoinStatus();
signals:
void permitJoiningChanged(bool permitJoining);
QSerialPortInfo m_serialPortInfo;
QSerialPort::BaudRate m_baudrate;
BackendType m_backendType = BackendTypeNxp;
ZigbeeNetwork *m_network = nullptr;
private slots:
void onMessageReceived(const ZigbeeInterfaceMessage &message);
void onControllerAvailableChanged(bool available);
// Controller command finished slots
void onCommandResetControllerFinished();
void onCommandSoftResetControllerFinished();
void onCommandErasePersistentDataFinished();
void onCommandSetExtendedPanIdFinished();
void onCommandSetChannelMaskFinished();
void onCommandSetNodeTypeFinished();
void onCommandStartNetworkFinished();
void onCommandStartScanFinished();
void onCommandEnableWhitelistFinished();
void onCommandNodeDescriptorRequestFinished();
void onCommandSimpleDescriptorRequestFinished();
void onCommandPowerDescriptorRequestFinished();
void onCommandInitiateTouchLinkFinished();
void onCommandTouchLinkFactoryResetFinished();
void onCommandRequestLinkQualityFinished();
void onCommandRequestMatchDescriptorFinished();
void onCommandSetSecurityFinished();
void onCommandNetworkAddressRequestFinished();
void onCommandAuthenticateDeviceFinished();
// Process controller notifications/messages
void processNetworkFormed(const ZigbeeInterfaceMessage &message);
void processLoggingMessage(const ZigbeeInterfaceMessage &message);
void processFactoryNewRestart(const ZigbeeInterfaceMessage &message);
void processNodeClusterList(const ZigbeeInterfaceMessage &message);
void processNodeAttributeList(const ZigbeeInterfaceMessage &message);
void processNodeCommandIdList(const ZigbeeInterfaceMessage &message);
void processDeviceAnnounce(const ZigbeeInterfaceMessage &message);
void processAttributeReport(const ZigbeeInterfaceMessage &message);
void processLeaveIndication(const ZigbeeInterfaceMessage &message);
void processRestartProvisioned(const ZigbeeInterfaceMessage &message);
void processRouterDiscoveryConfirm(const ZigbeeInterfaceMessage &message);
public slots:
void startNetwork() override;
void stopNetwork() override;
void factoryResetNetwork() override;
};