diff --git a/lib/Cluster.js b/lib/Cluster.js index 371c65d..d59953f 100644 --- a/lib/Cluster.js +++ b/lib/Cluster.js @@ -893,6 +893,13 @@ class Cluster extends EventEmitter { } } + // A response without a pending request, e.g. a device sending the same response twice, is + // not an error: answering it with an error Default Response would be wrong. + if (command && (command.isResponse || command.name === 'defaultResponse')) { + debug(this.logId, 'ignoring response without pending request:', command.name, frame); + return; + } + debug(this.logId, 'unknown command received:', frame, meta); // ZCL R8 2.5.12.2: UNSUP_COMMAND (0x81, UNSUP_CLUSTER_COMMAND here) diff --git a/test/testClusterResponseTimeout.js b/test/testClusterResponseTimeout.js index add1098..5d3ccd3 100644 --- a/test/testClusterResponseTimeout.js +++ b/test/testClusterResponseTimeout.js @@ -8,6 +8,7 @@ const Node = require('../lib/Node'); const { ZCLDataTypes } = require('../lib/zclTypes'); const BoundCluster = require('../lib/BoundCluster'); const OnOffCluster = require('../lib/clusters/onOff'); +const BasicCluster = require('../lib/clusters/basic'); const AlarmsCluster = require('../lib/clusters/alarms'); const IasZoneCluster = require('../lib/clusters/iasZone'); @@ -371,4 +372,43 @@ describe('Cluster Response Timeout', function () { await assert.rejects(sendingNode.endpoints[1].clusters.onOff.toggle(), /device not responding/); }); + + it('should ignore a duplicate response without sending an error back', async function () { + const sentFrames = []; + const node = new Node({ + sendFrame: async (endpointId, clusterId, frame) => { + sentFrames.push(frame); + if (sentFrames.length > 1) return; + // The device answers with a Read Attributes Response (attribute 0x0004, SUCCESS, "_TZ3000") + // and sends that same response a second time, as some devices do + const response = Buffer.from([ + 0x18, + frame[1], + 0x01, + 0x04, + 0x00, + 0x00, + 0x42, + 0x07, + ...Buffer.from('_TZ3000'), + ]); + setImmediate(async () => { + await node.handleFrame(endpointId, clusterId, response, { dstEndpoint: 1 }); + await node.handleFrame(endpointId, clusterId, response, { dstEndpoint: 1 }); + }); + }, + endpointDescriptors: [{ endpointId: 1, inputClusters: [BasicCluster.ID] }], + }); + + const { basic } = node.endpoints[1].clusters; + const response = await basic.readAttributesStructured( + { attributes: [{ attributeId: 4, indexPath: [] }] }, + { timeout: 1000 }, + ); + await new Promise(setImmediate); + + assert.strictEqual(response.attributes.subarray(5).toString(), '_TZ3000'); + // Only the request went out, no error Default Response for the second copy + assert.strictEqual(sentFrames.length, 1); + }); }); diff --git a/test/testZCLError.js b/test/testZCLError.js index 42a36a1..108aea2 100644 --- a/test/testZCLError.js +++ b/test/testZCLError.js @@ -6,6 +6,7 @@ const BoundCluster = require('../lib/BoundCluster'); const Node = require('../lib/Node'); const OnOffCluster = require('../lib/clusters/onOff'); const LevelControlCluster = require('../lib/clusters/levelControl'); +const IasZoneCluster = require('../lib/clusters/iasZone'); const { ZCLStandardHeader } = require('../lib/zclFrames'); const { ZCLError } = require('../lib/util'); const { createMockNode } = require('./util'); @@ -236,6 +237,31 @@ describe('ZCLError handling', function () { assert.strictEqual(ZCLStandardHeader.fromBuffer(capturedFrames[0].data).data[1], 0x81); }); + // A known request without a handler is still unsupported; only responses without a pending + // request are ignored + it('should send UNSUP_COMMAND (0x81) for a server-to-client request without a handler', async function () { + const capturedFrames = []; + const node = new Node({ + sendFrame: async (endpointId, clusterId, data) => { + capturedFrames.push(data); + }, + endpointDescriptors: [{ endpointId: 1, inputClusters: [IasZoneCluster.ID] }], + }); + const header = new ZCLStandardHeader(); + header.frameControl.clusterSpecific = true; + header.frameControl.directionToClient = true; + header.trxSequenceNumber = 1; + header.cmdId = 0x00; // zoneStatusChangeNotification + header.data = Buffer.from([0x01, 0x00, 0x00, 0x01, 0x00, 0x00]); + + await node.handleFrame(1, IasZoneCluster.ID, header.toBuffer(), {}); + await new Promise(setImmediate); + + const defaultResponse = ZCLStandardHeader.fromBuffer(capturedFrames[0]); + assert.strictEqual(defaultResponse.cmdId, 0x0b); + assert.strictEqual(defaultResponse.data[1], 0x81); + }); + it('should send UNSUPPORTED_CLUSTER (0xc3) for a server-to-client frame on a cluster the endpoint lacks', async function () { const { deviceNode, capturedFrames } = createSpyNodePair(); await deviceNode.handleFrame(1, LevelControlCluster.ID, makeToClientFrame(0x00), {});