Skip to content

Commit 2dabf65

Browse files
authored
Merge pull request #254 from athombv/fix/ignore-unmatched-responses
fix: Ignore a response that has no pending request
2 parents a90fb76 + 84b27e8 commit 2dabf65

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎lib/Cluster.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,13 @@ class Cluster extends EventEmitter {
893893
}
894894
}
895895

896+
// A response without a pending request, e.g. a device sending the same response twice, is
897+
// not an error: answering it with an error Default Response would be wrong.
898+
if (command && (command.isResponse || command.name === 'defaultResponse')) {
899+
debug(this.logId, 'ignoring response without pending request:', command.name, frame);
900+
return;
901+
}
902+
896903
debug(this.logId, 'unknown command received:', frame, meta);
897904

898905
// ZCL R8 2.5.12.2: UNSUP_COMMAND (0x81, UNSUP_CLUSTER_COMMAND here)

‎test/testClusterResponseTimeout.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Node = require('../lib/Node');
88
const { ZCLDataTypes } = require('../lib/zclTypes');
99
const BoundCluster = require('../lib/BoundCluster');
1010
const OnOffCluster = require('../lib/clusters/onOff');
11+
const BasicCluster = require('../lib/clusters/basic');
1112
const AlarmsCluster = require('../lib/clusters/alarms');
1213
const IasZoneCluster = require('../lib/clusters/iasZone');
1314

@@ -371,4 +372,43 @@ describe('Cluster Response Timeout', function () {
371372

372373
await assert.rejects(sendingNode.endpoints[1].clusters.onOff.toggle(), /device not responding/);
373374
});
375+
376+
it('should ignore a duplicate response without sending an error back', async function () {
377+
const sentFrames = [];
378+
const node = new Node({
379+
sendFrame: async (endpointId, clusterId, frame) => {
380+
sentFrames.push(frame);
381+
if (sentFrames.length > 1) return;
382+
// The device answers with a Read Attributes Response (attribute 0x0004, SUCCESS, "_TZ3000")
383+
// and sends that same response a second time, as some devices do
384+
const response = Buffer.from([
385+
0x18,
386+
frame[1],
387+
0x01,
388+
0x04,
389+
0x00,
390+
0x00,
391+
0x42,
392+
0x07,
393+
...Buffer.from('_TZ3000'),
394+
]);
395+
setImmediate(async () => {
396+
await node.handleFrame(endpointId, clusterId, response, { dstEndpoint: 1 });
397+
await node.handleFrame(endpointId, clusterId, response, { dstEndpoint: 1 });
398+
});
399+
},
400+
endpointDescriptors: [{ endpointId: 1, inputClusters: [BasicCluster.ID] }],
401+
});
402+
403+
const { basic } = node.endpoints[1].clusters;
404+
const response = await basic.readAttributesStructured(
405+
{ attributes: [{ attributeId: 4, indexPath: [] }] },
406+
{ timeout: 1000 },
407+
);
408+
await new Promise(setImmediate);
409+
410+
assert.strictEqual(response.attributes.subarray(5).toString(), '_TZ3000');
411+
// Only the request went out, no error Default Response for the second copy
412+
assert.strictEqual(sentFrames.length, 1);
413+
});
374414
});

‎test/testZCLError.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const BoundCluster = require('../lib/BoundCluster');
66
const Node = require('../lib/Node');
77
const OnOffCluster = require('../lib/clusters/onOff');
88
const LevelControlCluster = require('../lib/clusters/levelControl');
9+
const IasZoneCluster = require('../lib/clusters/iasZone');
910
const { ZCLStandardHeader } = require('../lib/zclFrames');
1011
const { ZCLError } = require('../lib/util');
1112
const { createMockNode } = require('./util');
@@ -236,6 +237,31 @@ describe('ZCLError handling', function () {
236237
assert.strictEqual(ZCLStandardHeader.fromBuffer(capturedFrames[0].data).data[1], 0x81);
237238
});
238239

240+
// A known request without a handler is still unsupported; only responses without a pending
241+
// request are ignored
242+
it('should send UNSUP_COMMAND (0x81) for a server-to-client request without a handler', async function () {
243+
const capturedFrames = [];
244+
const node = new Node({
245+
sendFrame: async (endpointId, clusterId, data) => {
246+
capturedFrames.push(data);
247+
},
248+
endpointDescriptors: [{ endpointId: 1, inputClusters: [IasZoneCluster.ID] }],
249+
});
250+
const header = new ZCLStandardHeader();
251+
header.frameControl.clusterSpecific = true;
252+
header.frameControl.directionToClient = true;
253+
header.trxSequenceNumber = 1;
254+
header.cmdId = 0x00; // zoneStatusChangeNotification
255+
header.data = Buffer.from([0x01, 0x00, 0x00, 0x01, 0x00, 0x00]);
256+
257+
await node.handleFrame(1, IasZoneCluster.ID, header.toBuffer(), {});
258+
await new Promise(setImmediate);
259+
260+
const defaultResponse = ZCLStandardHeader.fromBuffer(capturedFrames[0]);
261+
assert.strictEqual(defaultResponse.cmdId, 0x0b);
262+
assert.strictEqual(defaultResponse.data[1], 0x81);
263+
});
264+
239265
it('should send UNSUPPORTED_CLUSTER (0xc3) for a server-to-client frame on a cluster the endpoint lacks', async function () {
240266
const { deviceNode, capturedFrames } = createSpyNodePair();
241267
await deviceNode.handleFrame(1, LevelControlCluster.ID, makeToClientFrame(0x00), {});

0 commit comments

Comments
 (0)