Skip to content

Commit

Permalink
Merge pull request #3830 from manup/zgp_proxy2
Browse files Browse the repository at this point in the history
Fix ZGP proxy pairing
  • Loading branch information
manup committed Dec 8, 2020
2 parents cb4f8c1 + 876df45 commit d460950
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
16 changes: 10 additions & 6 deletions de_web_plugin.cpp
Expand Up @@ -1407,7 +1407,7 @@ void DeRestPluginPrivate::gpProcessButtonEvent(const deCONZ::GpDataIndication &i
updateSensorEtag(sensor);
sensor->updateStateTimestamp();
item->setValue(btn);
DBG_Printf(DBG_INFO, "[INFO] - Button %u %s\n", item->toNumber(), qPrintable(sensor->modelId()));
DBG_Printf(DBG_ZGP, "ZGP button %u %s\n", item->toNumber(), qPrintable(sensor->modelId()));
Event e(RSensors, RStateButtonEvent, sensor->id(), item);
enqueueEvent(e);
enqueueEvent(Event(RSensors, RStateLastUpdated, sensor->id()));
Expand Down Expand Up @@ -1606,14 +1606,18 @@ void DeRestPluginPrivate::gpDataIndication(const deCONZ::GpDataIndication &ind)

Sensor *sensor = getSensorNodeForFingerPrint(ind.gpdSrcId(), fp, QLatin1String("ZGPSwitch"));

if (searchSensorsState == SearchSensorsActive && extOptions.bits.gpdKeyEncryption)
#if DECONZ_LIB_VERSION >= 0x011000
if (searchSensorsState == SearchSensorsActive && extOptions.bits.gpdKeyEncryption && ind.gppShortAddress() != 0)
{
const QDateTime now = QDateTime::currentDateTime();
if (!sensor || !sensor->lastRx().isValid() || sensor->lastRx().secsTo(now) > 5)
if (searchSensorGppPairCounter < GP_MAX_PROXY_PAIRINGS && ind.gppLqi() >= deCONZ::GppGpdLqiModerate)
{
GP_SendPairing(ind.gpdSrcId(), 0xdd09, gpdDeviceId, gpdOutgoingCounter, gpdKey, apsCtrl, zclSeq++);
if (GP_SendPairing(ind.gpdSrcId(), GP_DEFAULT_PROXY_GROUP, gpdDeviceId, gpdOutgoingCounter, gpdKey, apsCtrl, zclSeq++, ind.gppShortAddress()))
{
searchSensorGppPairCounter++;
}
}
}
#endif

if (!sensor || sensor->deletedState() == Sensor::StateDeleted)
{
Expand Down Expand Up @@ -1708,7 +1712,7 @@ void DeRestPluginPrivate::gpDataIndication(const deCONZ::GpDataIndication &ind)

default:
{
DBG_Printf(DBG_INFO, "GP unhandled command gpdsrcid %u: gpdcmdid: 0x%02X\n", ind.gpdSrcId(), ind.gpdCommandId());
DBG_Printf(DBG_ZGP, "ZGP unhandled command gpdsrcid %u: gpdcmdid: 0x%02X\n", ind.gpdSrcId(), ind.gpdCommandId());
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions de_web_plugin_private.h
Expand Up @@ -1979,6 +1979,7 @@ public Q_SLOTS:
QString lastLightsScan;

SearchSensorsState searchSensorsState;
size_t searchSensorGppPairCounter = 0;
deCONZ::Address fastProbeAddr;
std::vector<deCONZ::ApsDataIndication> fastProbeIndications;
QVariantMap searchSensorsResult;
Expand Down
8 changes: 4 additions & 4 deletions green_power.cpp
Expand Up @@ -124,12 +124,12 @@ bool GP_SendProxyCommissioningMode(deCONZ::ApsController *apsCtrl, quint8 zclSeq

/*! Send Pair command to GP proxy device.
*/
bool GP_SendPairing(quint32 gpdSrcId, quint16 sinkGroupId, quint8 deviceId, quint32 frameCounter, const GpKey_t &key, deCONZ::ApsController *apsCtrl, quint8 zclSeqNo)
bool GP_SendPairing(quint32 gpdSrcId, quint16 sinkGroupId, quint8 deviceId, quint32 frameCounter, const GpKey_t &key, deCONZ::ApsController *apsCtrl, quint8 zclSeqNo, quint16 gppShortAddress)
{
deCONZ::ApsDataRequest req;

req.setDstAddressMode(deCONZ::ApsNwkAddress);
req.dstAddress().setNwk(deCONZ::BroadcastRouters);
req.dstAddress().setNwk(gppShortAddress);
req.setProfileId(GP_PROFILE_ID);
req.setClusterId(GREEN_POWER_CLUSTER_ID);
req.setDstEndpoint(GREEN_POWER_ENDPOINT);
Expand Down Expand Up @@ -198,10 +198,10 @@ bool GP_SendPairing(quint32 gpdSrcId, quint16 sinkGroupId, quint8 deviceId, quin
// broadcast
if (apsCtrl->apsdeDataRequest(req) == deCONZ::Success)
{
DBG_Printf(DBG_INFO, "send GP pairing\n");
DBG_Printf(DBG_INFO, "send GP pairing to 0x%04X\n", gppShortAddress);
return true;
}

DBG_Printf(DBG_INFO, "send GP pairing\n");
DBG_Printf(DBG_INFO, "send GP pairing to 0x%04X failed\n", gppShortAddress);
return false;
}
8 changes: 7 additions & 1 deletion green_power.h
Expand Up @@ -18,11 +18,17 @@
#define GREEN_POWER_CLUSTER_ID 0x0021
#define GREEN_POWER_ENDPOINT 0xf2
#define GP_SECURITY_KEY_SIZE 16
#define GP_MAX_PROXY_PAIRINGS 3
#define GP_DEFAULT_PROXY_GROUP 0xdd09

#if DECONZ_LIB_VERSION < 0x011000
#define DBG_ZGP DBG_INFO // DBG_ZGP didn't exist before version v2.8.x
#endif

using GpKey_t = std::array<unsigned char, GP_SECURITY_KEY_SIZE>;

GpKey_t GP_DecryptSecurityKey(quint32 sourceID, const GpKey_t &securityKey);
bool GP_SendProxyCommissioningMode(deCONZ::ApsController *apsCtrl, quint8 zclSeqNo);
bool GP_SendPairing(quint32 gpdSrcId, quint16 sinkGroupId, quint8 deviceId, quint32 frameCounter, const GpKey_t &key, deCONZ::ApsController *apsCtrl, quint8 zclSeqNo);
bool GP_SendPairing(quint32 gpdSrcId, quint16 sinkGroupId, quint8 deviceId, quint32 frameCounter, const GpKey_t &key, deCONZ::ApsController *apsCtrl, quint8 zclSeqNo, quint16 gppShortAddress);

#endif // GREEN_POWER_H
5 changes: 2 additions & 3 deletions permitJoin.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2018 dresden elektronik ingenieurtechnik gmbh.
* Copyright (c) 2016-2020 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -164,15 +164,14 @@ void DeRestPluginPrivate::permitJoinTimerFired()

if (gwPermitJoinDuration > 0)
{
// sendGPProxyCommissioningMode(); TODO enable when GP security is implemented
GP_SendProxyCommissioningMode(apsCtrl, zclSeq++);
}
}
else
{
DBG_Printf(DBG_INFO, "send permit join failed\n");
}

GP_SendProxyCommissioningMode(apsCtrl, zclSeq++);
}
}

Expand Down
1 change: 1 addition & 0 deletions rest_sensors.cpp
Expand Up @@ -3051,6 +3051,7 @@ void DeRestPluginPrivate::startSearchSensors()
searchSensorsResult.clear();
lastSensorsScan = QDateTime::currentDateTimeUtc().toString(QLatin1String("yyyy-MM-ddTHH:mm:ss"));
QTimer::singleShot(1000, this, SLOT(searchSensorsTimerFired()));
searchSensorGppPairCounter = 0;
searchSensorsState = SearchSensorsActive;
}
else
Expand Down

0 comments on commit d460950

Please sign in to comment.