Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node type parsing #61

Merged
merged 1 commit into from Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libnymea-zigbee/zdo/zigbeedeviceobject.cpp
Expand Up @@ -142,7 +142,7 @@ ZigbeeDeviceObjectReply *ZigbeeDeviceObject::requestIeeeAddress()

ZigbeeDeviceObjectReply *ZigbeeDeviceObject::requestNodeDescriptor()
{
qCDebug(dcZigbeeDeviceObject()) << "Request node descriptor from" << m_node;
qCDebug(dcZigbeeDeviceObject()) << "Requesting node descriptor from" << m_node;

// Build APS request
ZigbeeNetworkRequest request = buildZdoRequest(ZigbeeDeviceProfile::NodeDescriptorRequest);
Expand Down
34 changes: 13 additions & 21 deletions libnymea-zigbee/zdo/zigbeedeviceprofile.cpp
Expand Up @@ -38,36 +38,28 @@ ZigbeeDeviceProfile::NodeDescriptor ZigbeeDeviceProfile::parseNodeDescriptor(con
// Parse and set the node descriptor
QDataStream stream(payload);
stream.setByteOrder(QDataStream::LittleEndian);
quint8 typeDescriptorFlag = 0; quint8 frequencyFlag = 0; quint8 macCapabilitiesFlag = 0;
quint16 serverMaskFlag = 0; quint8 descriptorCapabilitiesFlag = 0;
quint8 typeAndDescriptorFlags = 0, frequencyAndApsFlags = 0, macCapabilitiesFlags = 0, descriptorCapabilities = 0;
quint16 serverMask = 0;

stream >> typeDescriptorFlag >> frequencyFlag >> macCapabilitiesFlag >> nodeDescriptor.manufacturerCode >> nodeDescriptor.maximumBufferSize;
stream >> nodeDescriptor.maximumRxSize >> serverMaskFlag >> nodeDescriptor.maximumTxSize >> descriptorCapabilitiesFlag;
stream >> typeAndDescriptorFlags >> frequencyAndApsFlags >> macCapabilitiesFlags >> nodeDescriptor.manufacturerCode >> nodeDescriptor.maximumBufferSize;
stream >> nodeDescriptor.maximumRxSize >> serverMask >> nodeDescriptor.maximumTxSize >> descriptorCapabilities;

// 0-2 Bit = logical type, 0 = coordinator, 1 = router, 2 = end device
if (!ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 0) && !ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 1)) {
nodeDescriptor.nodeType = NodeTypeCoordinator;
} else if (!ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 0) && ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 1)) {
nodeDescriptor.nodeType = NodeTypeRouter;
} else if (ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 0) && !ZigbeeUtils::checkBitUint8(typeDescriptorFlag, 1)) {
nodeDescriptor.nodeType = NodeTypeEndDevice;
}

nodeDescriptor.complexDescriptorAvailable = (typeDescriptorFlag >> 3) & 0x0001;
nodeDescriptor.userDescriptorAvailable = (typeDescriptorFlag >> 4) & 0x0001;
nodeDescriptor.nodeType = static_cast<NodeType>(typeAndDescriptorFlags & 0x07);
nodeDescriptor.complexDescriptorAvailable = typeAndDescriptorFlags & 0x08;
nodeDescriptor.userDescriptorAvailable = typeAndDescriptorFlags & 0x10;

// Frequency band, 5 bits
if (ZigbeeUtils::checkBitUint8(frequencyFlag, 3)) {
if (ZigbeeUtils::checkBitUint8(frequencyAndApsFlags, 3)) {
nodeDescriptor.frequencyBand = FrequencyBand868Mhz;
} else if (ZigbeeUtils::checkBitUint8(frequencyFlag, 5)) {
} else if (ZigbeeUtils::checkBitUint8(frequencyAndApsFlags, 5)) {
nodeDescriptor.frequencyBand = FrequencyBand902Mhz;
} else if (ZigbeeUtils::checkBitUint8(frequencyFlag, 6)) {
} else if (ZigbeeUtils::checkBitUint8(frequencyAndApsFlags, 6)) {
nodeDescriptor.frequencyBand = FrequencyBand2400Mhz;
}

nodeDescriptor.macCapabilities = parseMacCapabilities(macCapabilitiesFlag);
nodeDescriptor.serverMask = parseServerMask(serverMaskFlag);
nodeDescriptor.descriptorCapabilities = parseDescriptorCapabilities(descriptorCapabilitiesFlag);
nodeDescriptor.macCapabilities = parseMacCapabilities(macCapabilitiesFlags);
nodeDescriptor.serverMask = parseServerMask(serverMask);
nodeDescriptor.descriptorCapabilities = parseDescriptorCapabilities(descriptorCapabilities);

return nodeDescriptor;
}
Expand Down
4 changes: 2 additions & 2 deletions libnymea-zigbee/zigbeenode.cpp
Expand Up @@ -266,14 +266,14 @@ ZigbeeReply *ZigbeeNode::readBindingTableEntries()

void ZigbeeNode::initNodeDescriptor()
{
qCDebug(dcZigbeeNode()) << "Request node descriptor from" << this;
qCDebug(dcZigbeeNode()) << "Requesting node descriptor from" << this;
ZigbeeDeviceObjectReply *reply = deviceObject()->requestNodeDescriptor();
connect(reply, &ZigbeeDeviceObjectReply::finished, this, [this, reply](){
if (reply->error() != ZigbeeDeviceObjectReply::ErrorNoError) {
qCWarning(dcZigbeeNode()) << "Error occured during initialization of" << this << "Failed to read node descriptor" << reply->error();
m_requestRetry++;
if (m_requestRetry < m_requestRetriesMax) {
qCDebug(dcZigbeeNode()) << "Retry to request node descriptor" << m_requestRetry << "/" << m_requestRetriesMax;
qCDebug(dcZigbeeNode()) << "Retrying to request node descriptor" << m_requestRetry << "/" << m_requestRetriesMax;
QTimer::singleShot(500, this, [=](){ initNodeDescriptor(); });
} else {
qCWarning(dcZigbeeNode()) << "Failed to read node descriptor from" << this << "after" << m_requestRetriesMax << "attempts.";
Expand Down