@matter/protocol 0.13.0 → 0.13.1-alpha.0-20250501-80c86b03e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist/cjs/action/response/ReadResult.d.ts +4 -0
  2. package/dist/cjs/action/response/ReadResult.d.ts.map +1 -1
  3. package/dist/cjs/action/server/AttributeResponse.d.ts +1 -1
  4. package/dist/cjs/action/server/AttributeResponse.d.ts.map +1 -1
  5. package/dist/cjs/action/server/AttributeResponse.js +37 -17
  6. package/dist/cjs/action/server/AttributeResponse.js.map +2 -2
  7. package/dist/cjs/interaction/AttributeDataEncoder.d.ts +1 -1
  8. package/dist/cjs/interaction/AttributeDataEncoder.d.ts.map +1 -1
  9. package/dist/cjs/interaction/InteractionEndpointStructure.d.ts +37 -1
  10. package/dist/cjs/interaction/InteractionEndpointStructure.d.ts.map +1 -1
  11. package/dist/cjs/interaction/InteractionEndpointStructure.js +34 -15
  12. package/dist/cjs/interaction/InteractionEndpointStructure.js.map +1 -1
  13. package/dist/cjs/interaction/InteractionMessenger.js +1 -1
  14. package/dist/cjs/interaction/InteractionMessenger.js.map +1 -1
  15. package/dist/cjs/interaction/ServerSubscription.d.ts +1 -2
  16. package/dist/cjs/interaction/ServerSubscription.d.ts.map +1 -1
  17. package/dist/cjs/interaction/ServerSubscription.js +20 -20
  18. package/dist/cjs/interaction/ServerSubscription.js.map +1 -1
  19. package/dist/cjs/interaction/SubscriptionClient.d.ts +2 -1
  20. package/dist/cjs/interaction/SubscriptionClient.d.ts.map +1 -1
  21. package/dist/cjs/interaction/SubscriptionClient.js +2 -1
  22. package/dist/cjs/interaction/SubscriptionClient.js.map +1 -1
  23. package/dist/cjs/interaction/index.d.ts +0 -1
  24. package/dist/cjs/interaction/index.d.ts.map +1 -1
  25. package/dist/cjs/interaction/index.js +0 -1
  26. package/dist/cjs/interaction/index.js.map +1 -1
  27. package/dist/cjs/protocol/ExchangeManager.d.ts.map +1 -1
  28. package/dist/cjs/protocol/ExchangeManager.js +7 -2
  29. package/dist/cjs/protocol/ExchangeManager.js.map +1 -1
  30. package/dist/cjs/protocol/ProtocolHandler.d.ts +1 -0
  31. package/dist/cjs/protocol/ProtocolHandler.d.ts.map +1 -1
  32. package/dist/cjs/securechannel/SecureChannelProtocol.d.ts +1 -0
  33. package/dist/cjs/securechannel/SecureChannelProtocol.d.ts.map +1 -1
  34. package/dist/cjs/securechannel/SecureChannelProtocol.js +1 -0
  35. package/dist/cjs/securechannel/SecureChannelProtocol.js.map +1 -1
  36. package/dist/cjs/session/case/CaseServer.d.ts +1 -0
  37. package/dist/cjs/session/case/CaseServer.d.ts.map +1 -1
  38. package/dist/cjs/session/case/CaseServer.js +1 -0
  39. package/dist/cjs/session/case/CaseServer.js.map +1 -1
  40. package/dist/cjs/session/pase/PaseServer.d.ts +1 -0
  41. package/dist/cjs/session/pase/PaseServer.d.ts.map +1 -1
  42. package/dist/cjs/session/pase/PaseServer.js +1 -0
  43. package/dist/cjs/session/pase/PaseServer.js.map +1 -1
  44. package/dist/esm/action/response/ReadResult.d.ts +4 -0
  45. package/dist/esm/action/response/ReadResult.d.ts.map +1 -1
  46. package/dist/esm/action/server/AttributeResponse.d.ts +1 -1
  47. package/dist/esm/action/server/AttributeResponse.d.ts.map +1 -1
  48. package/dist/esm/action/server/AttributeResponse.js +37 -17
  49. package/dist/esm/action/server/AttributeResponse.js.map +1 -1
  50. package/dist/esm/interaction/AttributeDataEncoder.d.ts +1 -1
  51. package/dist/esm/interaction/AttributeDataEncoder.d.ts.map +1 -1
  52. package/dist/esm/interaction/InteractionEndpointStructure.d.ts +37 -1
  53. package/dist/esm/interaction/InteractionEndpointStructure.d.ts.map +1 -1
  54. package/dist/esm/interaction/InteractionEndpointStructure.js +21 -7
  55. package/dist/esm/interaction/InteractionEndpointStructure.js.map +1 -1
  56. package/dist/esm/interaction/InteractionMessenger.js +1 -1
  57. package/dist/esm/interaction/InteractionMessenger.js.map +1 -1
  58. package/dist/esm/interaction/ServerSubscription.d.ts +1 -2
  59. package/dist/esm/interaction/ServerSubscription.d.ts.map +1 -1
  60. package/dist/esm/interaction/ServerSubscription.js +4 -4
  61. package/dist/esm/interaction/ServerSubscription.js.map +1 -1
  62. package/dist/esm/interaction/SubscriptionClient.d.ts +2 -1
  63. package/dist/esm/interaction/SubscriptionClient.d.ts.map +1 -1
  64. package/dist/esm/interaction/SubscriptionClient.js +2 -1
  65. package/dist/esm/interaction/SubscriptionClient.js.map +1 -1
  66. package/dist/esm/interaction/index.d.ts +0 -1
  67. package/dist/esm/interaction/index.d.ts.map +1 -1
  68. package/dist/esm/interaction/index.js +0 -1
  69. package/dist/esm/interaction/index.js.map +1 -1
  70. package/dist/esm/protocol/ExchangeManager.d.ts.map +1 -1
  71. package/dist/esm/protocol/ExchangeManager.js +7 -2
  72. package/dist/esm/protocol/ExchangeManager.js.map +1 -1
  73. package/dist/esm/protocol/ProtocolHandler.d.ts +1 -0
  74. package/dist/esm/protocol/ProtocolHandler.d.ts.map +1 -1
  75. package/dist/esm/securechannel/SecureChannelProtocol.d.ts +1 -0
  76. package/dist/esm/securechannel/SecureChannelProtocol.d.ts.map +1 -1
  77. package/dist/esm/securechannel/SecureChannelProtocol.js +1 -0
  78. package/dist/esm/securechannel/SecureChannelProtocol.js.map +1 -1
  79. package/dist/esm/session/case/CaseServer.d.ts +1 -0
  80. package/dist/esm/session/case/CaseServer.d.ts.map +1 -1
  81. package/dist/esm/session/case/CaseServer.js +1 -0
  82. package/dist/esm/session/case/CaseServer.js.map +1 -1
  83. package/dist/esm/session/pase/PaseServer.d.ts +1 -0
  84. package/dist/esm/session/pase/PaseServer.d.ts.map +1 -1
  85. package/dist/esm/session/pase/PaseServer.js +1 -0
  86. package/dist/esm/session/pase/PaseServer.js.map +1 -1
  87. package/package.json +6 -6
  88. package/src/action/response/ReadResult.ts +4 -0
  89. package/src/action/server/AttributeResponse.ts +40 -17
  90. package/src/interaction/AttributeDataEncoder.ts +1 -1
  91. package/src/interaction/InteractionEndpointStructure.ts +62 -12
  92. package/src/interaction/InteractionMessenger.ts +1 -1
  93. package/src/interaction/ServerSubscription.ts +5 -5
  94. package/src/interaction/SubscriptionClient.ts +2 -2
  95. package/src/interaction/index.ts +0 -1
  96. package/src/protocol/ExchangeManager.ts +15 -2
  97. package/src/protocol/ProtocolHandler.ts +1 -0
  98. package/src/securechannel/SecureChannelProtocol.ts +1 -0
  99. package/src/session/case/CaseServer.ts +1 -0
  100. package/src/session/pase/PaseServer.ts +1 -0
  101. package/dist/cjs/interaction/InteractionServer.d.ts +0 -132
  102. package/dist/cjs/interaction/InteractionServer.d.ts.map +0 -1
  103. package/dist/cjs/interaction/InteractionServer.js +0 -1166
  104. package/dist/cjs/interaction/InteractionServer.js.map +0 -6
  105. package/dist/esm/interaction/InteractionServer.d.ts +0 -132
  106. package/dist/esm/interaction/InteractionServer.d.ts.map +0 -1
  107. package/dist/esm/interaction/InteractionServer.js +0 -1177
  108. package/dist/esm/interaction/InteractionServer.js.map +0 -6
  109. package/src/interaction/InteractionServer.ts +0 -1649
@@ -1,1166 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var InteractionServer_exports = {};
20
- __export(InteractionServer_exports, {
21
- InteractionServer: () => InteractionServer,
22
- attributePathToId: () => attributePathToId,
23
- clusterPathToId: () => clusterPathToId,
24
- commandPathToId: () => commandPathToId,
25
- eventPathToId: () => eventPathToId,
26
- genericElementPathToId: () => genericElementPathToId,
27
- validateReadAttributesPath: () => validateReadAttributesPath,
28
- validateReadEventPath: () => validateReadEventPath
29
- });
30
- module.exports = __toCommonJS(InteractionServer_exports);
31
- var import_general = require("#general");
32
- var import_model = require("#model");
33
- var import_types = require("#types");
34
- var import_AttributeServer = require("../cluster/server/AttributeServer.js");
35
- var import_MessageCodec = require("../codec/MessageCodec.js");
36
- var import_SecureSession = require("../session/SecureSession.js");
37
- var import_AttributeDataDecoder = require("./AttributeDataDecoder.js");
38
- var import_InteractionMessenger = require("./InteractionMessenger.js");
39
- var import_ServerSubscription = require("./ServerSubscription.js");
40
- var import_SubscriptionOptions = require("./SubscriptionOptions.js");
41
- /**
42
- * @license
43
- * Copyright 2022-2025 Matter.js Authors
44
- * SPDX-License-Identifier: Apache-2.0
45
- */
46
- const logger = import_general.Logger.get("InteractionServer");
47
- function genericElementPathToId(endpointId, clusterId, elementId) {
48
- return `${endpointId}/${clusterId}/${elementId}`;
49
- }
50
- function commandPathToId({ endpointId, clusterId, commandId }) {
51
- return genericElementPathToId(endpointId, clusterId, commandId);
52
- }
53
- function attributePathToId({ endpointId, clusterId, attributeId }) {
54
- return genericElementPathToId(endpointId, clusterId, attributeId);
55
- }
56
- function eventPathToId({ endpointId, clusterId, eventId }) {
57
- return genericElementPathToId(endpointId, clusterId, eventId);
58
- }
59
- function clusterPathToId({ nodeId, endpointId, clusterId }) {
60
- return `${nodeId}/${endpointId}/${clusterId}`;
61
- }
62
- function isConcreteAttributePath(path) {
63
- const { endpointId, clusterId, attributeId } = path;
64
- return endpointId !== void 0 && clusterId !== void 0 && attributeId !== void 0;
65
- }
66
- function validateReadAttributesPath(path, isGroupSession = false) {
67
- if (isGroupSession) {
68
- throw new import_types.StatusResponseError("Illegal read request with group session", import_types.StatusCode.InvalidAction);
69
- }
70
- const { clusterId, attributeId } = path;
71
- if (clusterId === void 0 && attributeId !== void 0) {
72
- if (!import_model.GLOBAL_IDS.has(attributeId)) {
73
- throw new import_types.StatusResponseError(
74
- `Illegal read request for wildcard cluster and non global attribute ${attributeId}`,
75
- import_types.StatusCode.InvalidAction
76
- );
77
- }
78
- }
79
- }
80
- function validateWriteAttributesPath(path, isGroupSession = false) {
81
- const { endpointId, clusterId, attributeId } = path;
82
- if (clusterId === void 0 || attributeId === void 0) {
83
- throw new import_types.StatusResponseError(
84
- "Illegal write request with wildcard cluster or attribute ID",
85
- import_types.StatusCode.InvalidAction
86
- );
87
- }
88
- if (isGroupSession && endpointId !== void 0) {
89
- throw new import_types.StatusResponseError("Illegal write request with group ID and endpoint ID", import_types.StatusCode.InvalidAction);
90
- }
91
- }
92
- function isConcreteEventPath(path) {
93
- const { endpointId, clusterId, eventId } = path;
94
- return endpointId !== void 0 && clusterId !== void 0 && eventId !== void 0;
95
- }
96
- function validateReadEventPath(path, isGroupSession = false) {
97
- const { clusterId, eventId } = path;
98
- if (clusterId === void 0 && eventId !== void 0) {
99
- throw new import_types.StatusResponseError("Illegal read request with wildcard cluster ID", import_types.StatusCode.InvalidAction);
100
- }
101
- if (isGroupSession) {
102
- throw new import_types.StatusResponseError("Illegal read request with group session", import_types.StatusCode.InvalidAction);
103
- }
104
- }
105
- function isConcreteCommandPath(path) {
106
- const { endpointId, clusterId, commandId } = path;
107
- return endpointId !== void 0 && clusterId !== void 0 && commandId !== void 0;
108
- }
109
- function validateCommandPath(path, isGroupSession = false) {
110
- const { endpointId, clusterId, commandId } = path;
111
- if (clusterId === void 0 || commandId === void 0) {
112
- throw new import_types.StatusResponseError(
113
- "Illegal write request with wildcard cluster or attribute ID",
114
- import_types.StatusCode.InvalidAction
115
- );
116
- }
117
- if (isGroupSession && endpointId !== void 0) {
118
- throw new import_types.StatusResponseError("Illegal write request with group ID and endpoint ID", import_types.StatusCode.InvalidAction);
119
- }
120
- }
121
- function getMatterModelCluster(clusterId) {
122
- return import_model.MatterModel.standard.get(import_model.ClusterModel, clusterId);
123
- }
124
- function getMatterModelClusterAttribute(clusterId, attributeId) {
125
- return getMatterModelCluster(clusterId)?.get(import_model.AttributeModel, attributeId);
126
- }
127
- function getMatterModelClusterCommand(clusterId, commandId) {
128
- return getMatterModelCluster(clusterId)?.get(import_model.CommandModel, commandId);
129
- }
130
- class InteractionServer {
131
- id = import_types.INTERACTION_PROTOCOL_ID;
132
- #context;
133
- #nextSubscriptionId = import_general.Crypto.getRandomUInt32();
134
- #isClosing = false;
135
- #clientHandler;
136
- #subscriptionConfig;
137
- #maxPathsPerInvoke;
138
- #subscriptionEstablishmentStarted = (0, import_general.Observable)();
139
- constructor(context) {
140
- this.#context = context;
141
- this.#subscriptionConfig = import_SubscriptionOptions.ServerSubscriptionConfig.of(context.subscriptionOptions);
142
- this.#maxPathsPerInvoke = context.maxPathsPerInvoke ?? import_types.DEFAULT_MAX_PATHS_PER_INVOKE;
143
- this.#context.structure.change.on(async () => {
144
- this.#context.sessions.updateAllSubscriptions();
145
- });
146
- }
147
- get isClosing() {
148
- return this.#isClosing;
149
- }
150
- get maxPathsPerInvoke() {
151
- return this.#maxPathsPerInvoke;
152
- }
153
- get subscriptionEstablishmentStarted() {
154
- return this.#subscriptionEstablishmentStarted;
155
- }
156
- async onNewExchange(exchange, message) {
157
- if (this.#isClosing) return;
158
- if (message.payloadHeader.messageType === import_InteractionMessenger.MessageType.SubscribeRequest && this.#clientHandler) {
159
- return this.#clientHandler.onNewExchange(exchange, message);
160
- }
161
- await new import_InteractionMessenger.InteractionServerMessenger(exchange).handleRequest(this);
162
- }
163
- get clientHandler() {
164
- return this.#clientHandler;
165
- }
166
- set clientHandler(clientHandler) {
167
- this.#clientHandler = clientHandler;
168
- }
169
- async #collectEventDataForRead({ eventRequests, eventFilters, isFabricFiltered }, exchange, message) {
170
- let eventReportsPayload;
171
- if (eventRequests) {
172
- eventReportsPayload = [];
173
- for (const requestPath of eventRequests) {
174
- validateReadEventPath(requestPath);
175
- const events = this.#endpointStructure.getEvents([requestPath]);
176
- if (events.length === 0) {
177
- if (isConcreteEventPath(requestPath)) {
178
- const { endpointId, clusterId, eventId } = requestPath;
179
- try {
180
- this.#endpointStructure.validateConcreteEventPath(endpointId, clusterId, eventId);
181
- throw new import_general.InternalError(
182
- "validateConcreteEventPath should throw StatusResponseError but did not."
183
- );
184
- } catch (e) {
185
- import_types.StatusResponseError.accept(e);
186
- logger.debug(
187
- `Read event from ${exchange.channel.name}: ${this.#endpointStructure.resolveEventName(requestPath)}: unsupported path: Status=${e.code}`
188
- );
189
- eventReportsPayload?.push({
190
- hasFabricSensitiveData: false,
191
- eventStatus: { path: requestPath, status: { status: e.code } }
192
- });
193
- }
194
- }
195
- logger.debug(
196
- `Read event from ${exchange.channel.name}: ${this.#endpointStructure.resolveEventName(
197
- requestPath
198
- )}: ignore non-existing event`
199
- );
200
- continue;
201
- }
202
- const reportsForPath = new Array();
203
- for (const { path, event } of events) {
204
- try {
205
- const matchingEvents = await this.readEvent(
206
- path,
207
- eventFilters,
208
- event,
209
- exchange,
210
- isFabricFiltered,
211
- message
212
- );
213
- logger.debug(
214
- `Read event from ${exchange.channel.name}: ${this.#endpointStructure.resolveEventName(
215
- path
216
- )}=${import_general.Diagnostic.json(matchingEvents)}`
217
- );
218
- const { schema } = event;
219
- reportsForPath.push(
220
- ...matchingEvents.map(({ number, priority, epochTimestamp, payload }) => ({
221
- hasFabricSensitiveData: event.hasFabricSensitiveData,
222
- eventData: {
223
- path,
224
- eventNumber: number,
225
- priority,
226
- epochTimestamp,
227
- payload,
228
- schema
229
- }
230
- }))
231
- );
232
- } catch (error) {
233
- logger.error(
234
- `Error while reading event from ${exchange.channel.name} to ${this.#endpointStructure.resolveEventName(path)}:`,
235
- error
236
- );
237
- import_types.StatusResponseError.accept(error);
238
- if (isConcreteEventPath(requestPath)) {
239
- eventReportsPayload?.push({
240
- hasFabricSensitiveData: false,
241
- eventStatus: { path, status: { status: error.code } }
242
- });
243
- }
244
- }
245
- }
246
- eventReportsPayload.push(
247
- ...reportsForPath.sort((a, b) => {
248
- const eventNumberA = a.eventData?.eventNumber ?? (0, import_types.EventNumber)(0);
249
- const eventNumberB = b.eventData?.eventNumber ?? (0, import_types.EventNumber)(0);
250
- if (eventNumberA > eventNumberB) {
251
- return 1;
252
- } else if (eventNumberA < eventNumberB) {
253
- return -1;
254
- } else {
255
- return 0;
256
- }
257
- })
258
- );
259
- }
260
- }
261
- return eventReportsPayload;
262
- }
263
- /**
264
- * Returns an iterator that yields the data reports and events data for the given read request.
265
- */
266
- *#iterateReadAttributesPaths({ attributeRequests, dataVersionFilters, isFabricFiltered }, eventReportsPayload, exchange, message) {
267
- const dataVersionFilterMap = new Map(
268
- dataVersionFilters?.map(({ path, dataVersion }) => [clusterPathToId(path), dataVersion]) ?? []
269
- );
270
- if (dataVersionFilterMap.size > 0) {
271
- logger.debug(
272
- `DataVersionFilters: ${Array.from(dataVersionFilterMap.entries()).map(([path, version]) => `${path}=${version}`).join(", ")}`
273
- );
274
- }
275
- for (const requestPath of attributeRequests ?? []) {
276
- validateReadAttributesPath(requestPath);
277
- const attributes = this.#endpointStructure.getAttributes([requestPath]);
278
- if (attributes.length === 0) {
279
- if (isConcreteAttributePath(requestPath)) {
280
- const { endpointId, clusterId, attributeId } = requestPath;
281
- try {
282
- this.#endpointStructure.validateConcreteAttributePath(endpointId, clusterId, attributeId);
283
- throw new import_general.InternalError(
284
- "validateConcreteAttributePath should throw StatusResponseError but did not."
285
- );
286
- } catch (e) {
287
- import_types.StatusResponseError.accept(e);
288
- logger.debug(
289
- `Error reading attribute from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(requestPath)}: unsupported path: Status=${e.code}`
290
- );
291
- yield {
292
- hasFabricSensitiveData: false,
293
- attributeStatus: { path: requestPath, status: { status: e.code } }
294
- };
295
- }
296
- }
297
- logger.debug(
298
- `Read from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(
299
- requestPath
300
- )}: ${this.#endpointStructure.resolveAttributeName(requestPath)}: ignore non-existing attribute`
301
- );
302
- continue;
303
- }
304
- for (const { path, attribute } of attributes) {
305
- const { nodeId, endpointId, clusterId } = path;
306
- try {
307
- if (getMatterModelClusterAttribute(clusterId, attribute.id)?.readable === false) {
308
- throw new import_types.StatusResponseError(
309
- `Attribute ${attribute.id} is not readable.`,
310
- import_types.StatusCode.UnsupportedRead
311
- );
312
- }
313
- let value, version;
314
- try {
315
- ({ value, version } = this.readAttribute(path, attribute, exchange, isFabricFiltered, message));
316
- } catch (e) {
317
- import_SecureSession.NoAssociatedFabricError.accept(e);
318
- if (endpointId === void 0 || clusterId === void 0) {
319
- throw new import_general.MatterFlowError("Should never happen");
320
- }
321
- const cluster = this.#endpointStructure.getClusterServer(endpointId, clusterId);
322
- if (cluster === void 0 || cluster.datasource == void 0) {
323
- throw new import_general.MatterFlowError("Should never happen");
324
- }
325
- version = cluster.datasource.version;
326
- value = [];
327
- }
328
- const versionFilterValue = endpointId !== void 0 && clusterId !== void 0 ? dataVersionFilterMap.get(clusterPathToId({ nodeId, endpointId, clusterId })) : void 0;
329
- if (versionFilterValue !== void 0 && versionFilterValue === version) {
330
- logger.debug(
331
- `Read attribute from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(
332
- path
333
- )}=${import_general.Diagnostic.json(value)} (version=${version}) ignored because of dataVersionFilter`
334
- );
335
- continue;
336
- }
337
- logger.debug(
338
- `Read attribute from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(
339
- path
340
- )}=${import_general.Diagnostic.json(value)} (version=${version})`
341
- );
342
- const { schema } = attribute;
343
- yield {
344
- hasFabricSensitiveData: attribute.hasFabricSensitiveData,
345
- attributeData: { path, dataVersion: version, payload: value, schema }
346
- };
347
- } catch (error) {
348
- const what = `reading ${this.#endpointStructure.resolveAttributeName(path)} from ${exchange.channel.name}`;
349
- if (!(error instanceof import_types.StatusResponseError)) {
350
- const wrappedError = new import_general.ImplementationError(`Unhandled error ${what}`);
351
- wrappedError.cause = error;
352
- throw wrappedError;
353
- }
354
- logger.error(`Error ${what}:`, error.message);
355
- if (isConcreteAttributePath(requestPath)) {
356
- yield {
357
- hasFabricSensitiveData: false,
358
- attributeStatus: { path, status: { status: error.code } }
359
- };
360
- }
361
- }
362
- }
363
- }
364
- if (eventReportsPayload !== void 0) {
365
- for (const eventReport of eventReportsPayload) {
366
- yield eventReport;
367
- }
368
- }
369
- }
370
- async handleReadRequest(exchange, readRequest, message) {
371
- const { attributeRequests, eventRequests, isFabricFiltered, interactionModelRevision } = readRequest;
372
- logger.debug(
373
- `Received read request from ${exchange.channel.name}: attributes:${attributeRequests?.map((path) => this.#endpointStructure.resolveAttributeName(path)).join(", ") ?? "none"}, events:${eventRequests?.map((path) => this.#endpointStructure.resolveEventName(path)).join(", ") ?? "none"} isFabricFiltered=${isFabricFiltered}`
374
- );
375
- if (interactionModelRevision > import_model.Specification.INTERACTION_MODEL_REVISION) {
376
- logger.debug(
377
- `Interaction model revision of sender ${interactionModelRevision} is higher than supported ${import_model.Specification.INTERACTION_MODEL_REVISION}.`
378
- );
379
- }
380
- if (attributeRequests === void 0 && eventRequests === void 0) {
381
- return {
382
- dataReport: {
383
- interactionModelRevision: import_model.Specification.INTERACTION_MODEL_REVISION,
384
- suppressResponse: true
385
- }
386
- };
387
- }
388
- if (message.packetHeader.sessionType !== import_MessageCodec.SessionType.Unicast) {
389
- throw new import_types.StatusResponseError(
390
- "Subscriptions are only allowed on unicast sessions",
391
- import_types.StatusCode.InvalidAction
392
- );
393
- }
394
- return {
395
- dataReport: {
396
- interactionModelRevision: import_model.Specification.INTERACTION_MODEL_REVISION,
397
- suppressResponse: true
398
- },
399
- payload: this.#iterateReadAttributesPaths(
400
- readRequest,
401
- await this.#collectEventDataForRead(readRequest, exchange, message),
402
- exchange,
403
- message
404
- )
405
- };
406
- }
407
- readAttribute(_path, attribute, exchange, isFabricFiltered, message, offline = false) {
408
- return attribute.getWithVersion(exchange.session, isFabricFiltered, offline ? void 0 : message);
409
- }
410
- /**
411
- * Reads the attributes for the given endpoint.
412
- * This can currently only be used for subscriptions because errors are ignored!
413
- */
414
- readEndpointAttributesForSubscription(attributes, exchange, isFabricFiltered, message, offline = false) {
415
- const result = new Array();
416
- for (const { path, attribute } of attributes) {
417
- try {
418
- const { version, value } = this.readAttribute(
419
- path,
420
- attribute,
421
- exchange,
422
- isFabricFiltered,
423
- message,
424
- offline
425
- );
426
- result.push({ path, value, version, attribute });
427
- } catch (error) {
428
- if (import_types.StatusResponseError.is(error, import_types.StatusCode.UnsupportedAccess)) {
429
- logger.warn(
430
- `Permission denied reading attribute ${this.#endpointStructure.resolveAttributeName(path)}`
431
- );
432
- } else {
433
- logger.warn(
434
- `Error reading attribute ${this.#endpointStructure.resolveAttributeName(path)}:`,
435
- error
436
- );
437
- }
438
- }
439
- }
440
- return result;
441
- }
442
- async readEvent(_path, eventFilters, event, exchange, isFabricFiltered, message) {
443
- return event.get(exchange.session, isFabricFiltered, message, eventFilters);
444
- }
445
- async handleWriteRequest(exchange, { suppressResponse, timedRequest, writeRequests, interactionModelRevision, moreChunkedMessages }, message) {
446
- const sessionType = message.packetHeader.sessionType;
447
- logger.debug(
448
- `Received write request from ${exchange.channel.name}: ${writeRequests.map((req) => this.#endpointStructure.resolveAttributeName(req.path)).join(", ")}, suppressResponse=${suppressResponse}, moreChunkedMessages=${moreChunkedMessages}`
449
- );
450
- if (moreChunkedMessages && suppressResponse) {
451
- throw new import_types.StatusResponseError(
452
- "MoreChunkedMessages and SuppressResponse cannot be used together in write messages",
453
- import_types.StatusCode.InvalidAction
454
- );
455
- }
456
- if (interactionModelRevision > import_model.Specification.INTERACTION_MODEL_REVISION) {
457
- logger.debug(
458
- `Interaction model revision of sender ${interactionModelRevision} is higher than supported ${import_model.Specification.INTERACTION_MODEL_REVISION}.`
459
- );
460
- }
461
- const receivedWithinTimedInteraction = exchange.hasActiveTimedInteraction();
462
- if (receivedWithinTimedInteraction && moreChunkedMessages) {
463
- throw new import_types.StatusResponseError(
464
- "Write Request action that is part of a Timed Write Interaction SHALL NOT be chunked.",
465
- import_types.StatusCode.InvalidAction
466
- );
467
- }
468
- if (exchange.hasExpiredTimedInteraction()) {
469
- exchange.clearTimedInteraction();
470
- throw new import_types.StatusResponseError(`Timed request window expired. Decline write request.`, import_types.StatusCode.Timeout);
471
- }
472
- if (timedRequest !== exchange.hasTimedInteraction()) {
473
- throw new import_types.StatusResponseError(
474
- `timedRequest flag of write interaction (${timedRequest}) mismatch with expected timed interaction (${receivedWithinTimedInteraction}).`,
475
- import_types.StatusCode.TimedRequestMismatch
476
- );
477
- }
478
- if (receivedWithinTimedInteraction) {
479
- logger.debug(
480
- `Write request from ${exchange.channel.name} successfully received while timed interaction is running.`
481
- );
482
- exchange.clearTimedInteraction();
483
- if (sessionType !== import_MessageCodec.SessionType.Unicast) {
484
- throw new import_types.StatusResponseError(
485
- "Write requests are only allowed on unicast sessions when a timed interaction is running.",
486
- import_types.StatusCode.InvalidAction
487
- );
488
- }
489
- }
490
- if (sessionType === import_MessageCodec.SessionType.Group && !suppressResponse) {
491
- throw new import_types.StatusResponseError(
492
- "Write requests are only allowed as group casts when suppressResponse=true.",
493
- import_types.StatusCode.InvalidAction
494
- );
495
- }
496
- const writeData = (0, import_AttributeDataDecoder.expandPathsInAttributeData)(writeRequests, true);
497
- const writeResults = new Array();
498
- const attributeListWrites = /* @__PURE__ */ new Set();
499
- const clusterDataVersionInfo = /* @__PURE__ */ new Map();
500
- const inaccessiblePaths = /* @__PURE__ */ new Set();
501
- for (const writeRequest of writeData) {
502
- const { path: writePath, dataVersion } = writeRequest;
503
- validateWriteAttributesPath(writePath);
504
- const attributes = this.#endpointStructure.getAttributes([writePath], true);
505
- if (attributes.length === 0) {
506
- if (isConcreteAttributePath(writePath)) {
507
- const { endpointId, clusterId, attributeId } = writePath;
508
- try {
509
- this.#endpointStructure.validateConcreteAttributePath(endpointId, clusterId, attributeId);
510
- throw new import_types.StatusResponseError(
511
- `Attribute ${attributeId} is not writable.`,
512
- import_types.StatusCode.UnsupportedWrite
513
- );
514
- } catch (e) {
515
- import_types.StatusResponseError.accept(e);
516
- logger.debug(
517
- `Write from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(
518
- writePath
519
- )} not allowed: Status=${e.code}`
520
- );
521
- writeResults.push({ path: writePath, statusCode: e.code });
522
- }
523
- } else {
524
- logger.debug(
525
- `Write from ${exchange.channel.name}: ${this.#endpointStructure.resolveAttributeName(
526
- writePath
527
- )}: ignore non-existing (wildcard) attribute`
528
- );
529
- }
530
- continue;
531
- }
532
- if (attributes.length === 1 && isConcreteAttributePath(writePath)) {
533
- const { endpointId, clusterId } = writePath;
534
- const { attribute } = attributes[0];
535
- if (attribute.requiresTimedInteraction && !receivedWithinTimedInteraction) {
536
- logger.debug(`This write requires a timed interaction which is not initialized.`);
537
- writeResults.push({ path: writePath, statusCode: import_types.StatusCode.NeedsTimedInteraction });
538
- continue;
539
- }
540
- if (attribute instanceof import_AttributeServer.FabricScopedAttributeServer && (!exchange.session.isSecure || !exchange.session.fabric)) {
541
- logger.debug(`This write requires a secure session with a fabric assigned which is missing.`);
542
- writeResults.push({ path: writePath, statusCode: import_types.StatusCode.UnsupportedAccess });
543
- continue;
544
- }
545
- if (dataVersion !== void 0) {
546
- const datasource = this.#endpointStructure.getClusterServer(endpointId, clusterId)?.datasource;
547
- const { nodeId } = writePath;
548
- const clusterKey = clusterPathToId({ nodeId, endpointId, clusterId });
549
- const currentDataVersion = clusterDataVersionInfo.get(clusterKey) ?? datasource?.version;
550
- if (currentDataVersion !== void 0) {
551
- if (dataVersion !== currentDataVersion) {
552
- logger.debug(
553
- `This write requires a specific data version (${dataVersion}) which do not match the current cluster data version (${currentDataVersion}).`
554
- );
555
- writeResults.push({ path: writePath, statusCode: import_types.StatusCode.DataVersionMismatch });
556
- continue;
557
- }
558
- clusterDataVersionInfo.set(clusterKey, currentDataVersion);
559
- }
560
- }
561
- }
562
- for (const { path, attribute } of attributes) {
563
- const { schema, defaultValue } = attribute;
564
- const pathId = attributePathToId(path);
565
- try {
566
- if (!(attribute instanceof import_AttributeServer.AttributeServer) && !(attribute instanceof import_AttributeServer.FabricScopedAttributeServer)) {
567
- throw new import_types.StatusResponseError(
568
- "Fixed attributes cannot be written",
569
- import_types.StatusCode.UnsupportedWrite
570
- );
571
- }
572
- if (inaccessiblePaths.has(pathId)) {
573
- logger.debug(`This write is not allowed due to previous access denied.`);
574
- continue;
575
- }
576
- const { endpointId } = path;
577
- const { listIndex } = writePath;
578
- const value = listIndex === void 0 ? (0, import_AttributeDataDecoder.decodeAttributeValueWithSchema)(schema, [writeRequest], defaultValue) : (0, import_AttributeDataDecoder.decodeListAttributeValueWithSchema)(
579
- schema,
580
- [writeRequest],
581
- this.readAttribute(path, attribute, exchange, true, message).value ?? defaultValue
582
- );
583
- logger.debug(
584
- `Handle write request from ${exchange.channel.name} resolved to: ${this.#endpointStructure.resolveAttributeName(path)}=${import_general.Diagnostic.json(
585
- value
586
- )} (listIndex=${listIndex}, for-version=${dataVersion})`
587
- );
588
- if (attribute.requiresTimedInteraction && !receivedWithinTimedInteraction) {
589
- logger.debug(`This write requires a timed interaction which is not initialized.`);
590
- throw new import_types.StatusResponseError(
591
- "This write requires a timed interaction which is not initialized.",
592
- import_types.StatusCode.NeedsTimedInteraction
593
- );
594
- }
595
- await this.writeAttribute(
596
- path,
597
- attribute,
598
- value,
599
- exchange,
600
- message,
601
- this.#endpointStructure.getEndpoint(endpointId),
602
- receivedWithinTimedInteraction,
603
- schema instanceof import_types.ArraySchema
604
- );
605
- if (schema instanceof import_types.ArraySchema && !attributeListWrites.has(attribute)) {
606
- attributeListWrites.add(attribute);
607
- }
608
- } catch (error) {
609
- if (import_types.StatusResponseError.is(error, import_types.StatusCode.UnsupportedAccess)) {
610
- inaccessiblePaths.add(pathId);
611
- }
612
- if (attributes.length === 1 && isConcreteAttributePath(writePath)) {
613
- logger.error(
614
- `Error while handling write request from ${exchange.channel.name} to ${this.#endpointStructure.resolveAttributeName(path)}:`,
615
- error instanceof import_types.StatusResponseError ? error.message : error
616
- );
617
- if (error instanceof import_types.StatusResponseError) {
618
- writeResults.push({ path, statusCode: error.code, clusterStatusCode: error.clusterCode });
619
- continue;
620
- }
621
- writeResults.push({ path, statusCode: import_types.StatusCode.ConstraintError });
622
- continue;
623
- } else {
624
- logger.debug(
625
- `While handling write request from ${exchange.channel.name} to ${this.#endpointStructure.resolveAttributeName(path)} ignored: ${error.message}`
626
- );
627
- }
628
- }
629
- writeResults.push({ path, statusCode: import_types.StatusCode.Success });
630
- }
631
- }
632
- const errorResults = writeResults.filter(({ statusCode }) => statusCode !== import_types.StatusCode.Success);
633
- logger.debug(
634
- `Write request from ${exchange.channel.name} done ${errorResults.length ? `with following errors: ${errorResults.map(
635
- ({ path, statusCode }) => `${this.#endpointStructure.resolveAttributeName(path)}=${import_general.Diagnostic.json(statusCode)}`
636
- ).join(", ")}` : "without errors"}`
637
- );
638
- const response = {
639
- interactionModelRevision: import_model.Specification.INTERACTION_MODEL_REVISION,
640
- writeResponses: writeResults.map(({ path, statusCode, clusterStatusCode }) => ({
641
- path,
642
- status: { status: statusCode, clusterStatus: clusterStatusCode }
643
- }))
644
- };
645
- for (const attribute of attributeListWrites.values()) {
646
- try {
647
- attribute.triggerDelayedChangeEvents();
648
- } catch (error) {
649
- logger.error(
650
- `Ignored Error while writing attribute from ${exchange.channel.name} to ${attribute.name}:`,
651
- error
652
- );
653
- }
654
- }
655
- return response;
656
- }
657
- async writeAttribute(_path, attribute, value, exchange, message, _endpoint, _receivedWithinTimedInteraction, isListWrite = false) {
658
- attribute.set(value, exchange.session, message, isListWrite);
659
- }
660
- async handleSubscribeRequest(exchange, request, messenger, message) {
661
- const {
662
- minIntervalFloorSeconds,
663
- maxIntervalCeilingSeconds,
664
- attributeRequests,
665
- dataVersionFilters,
666
- eventRequests,
667
- eventFilters,
668
- keepSubscriptions,
669
- isFabricFiltered,
670
- interactionModelRevision
671
- } = request;
672
- logger.debug(
673
- `Received subscribe request from ${exchange.channel.name} (keepSubscriptions=${keepSubscriptions}, isFabricFiltered=${isFabricFiltered})`
674
- );
675
- if (interactionModelRevision > import_model.Specification.INTERACTION_MODEL_REVISION) {
676
- logger.debug(
677
- `Interaction model revision of sender ${interactionModelRevision} is higher than supported ${import_model.Specification.INTERACTION_MODEL_REVISION}.`
678
- );
679
- }
680
- if (message.packetHeader.sessionType !== import_MessageCodec.SessionType.Unicast) {
681
- throw new import_types.StatusResponseError(
682
- "Subscriptions are only allowed on unicast sessions",
683
- import_types.StatusCode.InvalidAction
684
- );
685
- }
686
- (0, import_SecureSession.assertSecureSession)(exchange.session, "Subscriptions are only implemented on secure sessions");
687
- const session = exchange.session;
688
- const fabric = session.fabric;
689
- if (fabric === void 0)
690
- throw new import_types.StatusResponseError(
691
- "Subscriptions are only implemented after a fabric has been assigned",
692
- import_types.StatusCode.InvalidAction
693
- );
694
- if (!keepSubscriptions) {
695
- const clearedCount = await this.#context.sessions.clearSubscriptionsForNode(
696
- fabric.addressOf(session.peerNodeId),
697
- true
698
- );
699
- if (clearedCount > 0) {
700
- logger.debug(
701
- `Cleared ${clearedCount} subscriptions for Subscriber node ${session.peerNodeId} because keepSubscriptions=false`
702
- );
703
- }
704
- }
705
- if ((!Array.isArray(attributeRequests) || attributeRequests.length === 0) && (!Array.isArray(eventRequests) || eventRequests.length === 0)) {
706
- throw new import_types.StatusResponseError("No attributes or events requested", import_types.StatusCode.InvalidAction);
707
- }
708
- logger.debug(
709
- `Subscribe to attributes:${attributeRequests?.map((path) => this.#endpointStructure.resolveAttributeName(path)).join(", ") ?? "none"}, events:${eventRequests?.map((path) => this.#endpointStructure.resolveEventName(path)).join(", ") ?? "none"}`
710
- );
711
- if (dataVersionFilters !== void 0 && dataVersionFilters.length > 0) {
712
- logger.debug(
713
- `DataVersionFilters: ${dataVersionFilters.map(
714
- ({ path: { nodeId, endpointId, clusterId }, dataVersion }) => `${clusterPathToId({ nodeId, endpointId, clusterId })}=${dataVersion}`
715
- ).join(", ")}`
716
- );
717
- }
718
- if (eventFilters !== void 0 && eventFilters.length > 0)
719
- logger.debug(
720
- `Event filters: ${eventFilters.map((filter) => `${filter.nodeId}/${filter.eventMin}`).join(", ")}`
721
- );
722
- attributeRequests?.forEach((path) => validateReadAttributesPath(path));
723
- eventRequests?.forEach((path) => validateReadEventPath(path));
724
- if (minIntervalFloorSeconds < 0) {
725
- throw new import_types.StatusResponseError(
726
- "minIntervalFloorSeconds should be greater or equal to 0",
727
- import_types.StatusCode.InvalidAction
728
- );
729
- }
730
- if (maxIntervalCeilingSeconds < 0) {
731
- throw new import_types.StatusResponseError(
732
- "maxIntervalCeilingSeconds should be greater or equal to 1",
733
- import_types.StatusCode.InvalidAction
734
- );
735
- }
736
- if (maxIntervalCeilingSeconds < minIntervalFloorSeconds) {
737
- throw new import_types.StatusResponseError(
738
- "maxIntervalCeilingSeconds should be greater or equal to minIntervalFloorSeconds",
739
- import_types.StatusCode.InvalidAction
740
- );
741
- }
742
- if (this.#nextSubscriptionId === 4294967295) this.#nextSubscriptionId = 0;
743
- const subscriptionId = this.#nextSubscriptionId++;
744
- this.#subscriptionEstablishmentStarted.emit(session.peerAddress);
745
- let subscription;
746
- try {
747
- subscription = await this.#establishSubscription(
748
- subscriptionId,
749
- request,
750
- messenger,
751
- session,
752
- exchange,
753
- message
754
- );
755
- } catch (error) {
756
- logger.error(
757
- `Subscription ${subscriptionId} for Session ${session.id}: Error while sending initial data reports`,
758
- error instanceof import_general.MatterError ? error.message : error
759
- );
760
- if (error instanceof import_types.StatusResponseError && !(error instanceof import_types.ReceivedStatusResponseError)) {
761
- logger.info(`Sending status response ${error.code} for interaction error: ${error.message}`);
762
- await messenger.sendStatus(error.code, {
763
- logContext: {
764
- for: "I/SubscriptionSeed-Status"
765
- }
766
- });
767
- }
768
- await messenger.close();
769
- return;
770
- }
771
- const maxInterval = subscription.maxInterval;
772
- await messenger.send(
773
- import_InteractionMessenger.MessageType.SubscribeResponse,
774
- import_types.TlvSubscribeResponse.encode({
775
- subscriptionId,
776
- maxInterval,
777
- interactionModelRevision: import_model.Specification.INTERACTION_MODEL_REVISION
778
- }),
779
- {
780
- logContext: {
781
- subId: subscriptionId,
782
- maxInterval
783
- }
784
- }
785
- );
786
- subscription.activate();
787
- }
788
- async #establishSubscription(id, {
789
- minIntervalFloorSeconds,
790
- maxIntervalCeilingSeconds,
791
- attributeRequests,
792
- dataVersionFilters,
793
- eventRequests,
794
- eventFilters,
795
- isFabricFiltered
796
- }, messenger, session, exchange, message) {
797
- const context = {
798
- session,
799
- structure: this.#endpointStructure,
800
- readAttribute: (path, attribute, offline) => this.readAttribute(path, attribute, exchange, isFabricFiltered, message, offline),
801
- readEndpointAttributesForSubscription: (attributes) => this.readEndpointAttributesForSubscription(attributes, exchange, isFabricFiltered, message),
802
- readEvent: (path, event, eventFilters2) => this.readEvent(path, eventFilters2, event, exchange, isFabricFiltered, message),
803
- initiateExchange: (address, protocolId) => this.#context.initiateExchange(address, protocolId)
804
- };
805
- const subscription = new import_ServerSubscription.ServerSubscription({
806
- id,
807
- context,
808
- criteria: {
809
- attributeRequests,
810
- dataVersionFilters,
811
- eventRequests,
812
- eventFilters,
813
- isFabricFiltered
814
- },
815
- minIntervalFloorSeconds,
816
- maxIntervalCeilingSeconds,
817
- subscriptionOptions: this.#subscriptionConfig
818
- });
819
- try {
820
- await subscription.sendInitialReport(messenger);
821
- } catch (error) {
822
- await subscription.close();
823
- throw error;
824
- }
825
- logger.info(
826
- `Successfully created subscription ${id} for Session ${session.id} to ${session.peerAddress}. Updates: ${minIntervalFloorSeconds} - ${maxIntervalCeilingSeconds} => ${subscription.maxInterval} seconds (sendInterval = ${subscription.sendInterval} seconds)`
827
- );
828
- return subscription;
829
- }
830
- async establishFormerSubscription({
831
- subscriptionId,
832
- attributeRequests,
833
- eventRequests,
834
- isFabricFiltered,
835
- minIntervalFloorSeconds,
836
- maxIntervalCeilingSeconds,
837
- maxInterval,
838
- sendInterval
839
- }, session) {
840
- const exchange = this.#context.initiateExchange(session.peerAddress, import_types.INTERACTION_PROTOCOL_ID);
841
- const message = {};
842
- logger.debug(
843
- `Send DataReports to re-establish subscription ${subscriptionId} to `,
844
- import_general.Diagnostic.dict({ isFabricFiltered, maxInterval, sendInterval })
845
- );
846
- const context = {
847
- session,
848
- structure: this.#endpointStructure,
849
- readAttribute: (path, attribute, offline) => this.readAttribute(path, attribute, exchange, isFabricFiltered, message, offline),
850
- readEndpointAttributesForSubscription: (attributes) => this.readEndpointAttributesForSubscription(attributes, exchange, isFabricFiltered, message),
851
- readEvent: (path, event, eventFilters) => this.readEvent(path, eventFilters, event, exchange, isFabricFiltered, message),
852
- initiateExchange: (address, protocolId) => this.#context.initiateExchange(address, protocolId)
853
- };
854
- const subscription = new import_ServerSubscription.ServerSubscription({
855
- id: subscriptionId,
856
- context,
857
- minIntervalFloorSeconds,
858
- maxIntervalCeilingSeconds,
859
- criteria: {
860
- attributeRequests,
861
- eventRequests,
862
- isFabricFiltered
863
- },
864
- subscriptionOptions: this.#subscriptionConfig,
865
- useAsMaxInterval: maxInterval,
866
- useAsSendInterval: sendInterval
867
- });
868
- try {
869
- await subscription.sendInitialReport(new import_InteractionMessenger.InteractionServerMessenger(exchange));
870
- subscription.activate();
871
- logger.info(
872
- `Successfully re-established subscription ${subscriptionId} for Session ${session.id} to ${session.peerAddress}. Updates: ${minIntervalFloorSeconds} - ${maxIntervalCeilingSeconds} => ${subscription.maxInterval} seconds (sendInterval = ${subscription.sendInterval} seconds)`
873
- );
874
- } catch (error) {
875
- await subscription.close();
876
- throw error;
877
- }
878
- return subscription;
879
- }
880
- async handleInvokeRequest(exchange, { invokeRequests, timedRequest, suppressResponse, interactionModelRevision }, messenger, message) {
881
- logger.debug(
882
- `Received invoke request from ${exchange.channel.name}${invokeRequests.length > 0 ? ` with ${invokeRequests.length} commands` : ""}: ${invokeRequests.map(
883
- ({ commandPath: { endpointId, clusterId, commandId } }) => this.#endpointStructure.resolveCommandName({ endpointId, clusterId, commandId })
884
- ).join(", ")}, suppressResponse=${suppressResponse}`
885
- );
886
- if (interactionModelRevision > import_model.Specification.INTERACTION_MODEL_REVISION) {
887
- logger.debug(
888
- `Interaction model revision of sender ${interactionModelRevision} is higher than supported ${import_model.Specification.INTERACTION_MODEL_REVISION}.`
889
- );
890
- }
891
- const receivedWithinTimedInteraction = exchange.hasActiveTimedInteraction();
892
- if (exchange.hasExpiredTimedInteraction()) {
893
- exchange.clearTimedInteraction();
894
- throw new import_types.StatusResponseError(`Timed request window expired. Decline invoke request.`, import_types.StatusCode.Timeout);
895
- }
896
- if (timedRequest !== exchange.hasTimedInteraction()) {
897
- throw new import_types.StatusResponseError(
898
- `timedRequest flag of invoke interaction (${timedRequest}) mismatch with expected timed interaction (${receivedWithinTimedInteraction}).`,
899
- import_types.StatusCode.TimedRequestMismatch
900
- );
901
- }
902
- if (receivedWithinTimedInteraction) {
903
- logger.debug(`Invoke request from ${exchange.channel.name} received while timed interaction is running.`);
904
- exchange.clearTimedInteraction();
905
- if (message.packetHeader.sessionType !== import_MessageCodec.SessionType.Unicast) {
906
- throw new import_types.StatusResponseError(
907
- "Invoke requests are only allowed on unicast sessions when a timed interaction is running.",
908
- import_types.StatusCode.InvalidAction
909
- );
910
- }
911
- }
912
- if (invokeRequests.length > this.#maxPathsPerInvoke) {
913
- throw new import_types.StatusResponseError(
914
- `Only ${this.#maxPathsPerInvoke} invoke requests are supported in one message. This message contains ${invokeRequests.length}`,
915
- import_types.StatusCode.InvalidAction
916
- );
917
- }
918
- invokeRequests.forEach(({ commandPath }) => validateCommandPath(commandPath));
919
- if (invokeRequests.length > 1) {
920
- const pathsUsed = /* @__PURE__ */ new Set();
921
- const commandRefsUsed = /* @__PURE__ */ new Set();
922
- invokeRequests.forEach(({ commandPath, commandRef }) => {
923
- if (!isConcreteCommandPath(commandPath)) {
924
- throw new import_types.StatusResponseError("Illegal wildcard path in batch invoke", import_types.StatusCode.InvalidAction);
925
- }
926
- const commandPathId = commandPathToId(commandPath);
927
- if (pathsUsed.has(commandPathId)) {
928
- throw new import_types.StatusResponseError(
929
- `Duplicate command path (${commandPathId}) in batch invoke`,
930
- import_types.StatusCode.InvalidAction
931
- );
932
- }
933
- if (commandRef === void 0) {
934
- throw new import_types.StatusResponseError(
935
- `Command reference missing in batch invoke of ${commandPathId}`,
936
- import_types.StatusCode.InvalidAction
937
- );
938
- }
939
- if (commandRefsUsed.has(commandRef)) {
940
- throw new import_types.StatusResponseError(
941
- `Duplicate command reference ${commandRef} in invoke of ${commandPathId}`,
942
- import_types.StatusCode.InvalidAction
943
- );
944
- }
945
- pathsUsed.add(commandPathId);
946
- commandRefsUsed.add(commandRef);
947
- });
948
- }
949
- const isGroupSession = message.packetHeader.sessionType === import_MessageCodec.SessionType.Group;
950
- const invokeResponseMessage = {
951
- suppressResponse: false,
952
- // Deprecated but must be present
953
- interactionModelRevision: import_model.Specification.INTERACTION_MODEL_REVISION,
954
- invokeResponses: [],
955
- moreChunkedMessages: invokeRequests.length > 1
956
- // Assume for now we have multiple responses when having multiple invokes
957
- };
958
- const emptyInvokeResponseBytes = import_types.TlvInvokeResponseForSend.encode(invokeResponseMessage);
959
- let messageSize = emptyInvokeResponseBytes.length;
960
- let invokeResultsProcessed = 0;
961
- const processResponseResult = async (invokeResponse) => {
962
- invokeResultsProcessed++;
963
- if (isGroupSession) {
964
- return;
965
- }
966
- const encodedInvokeResponse = import_types.TlvInvokeResponseData.encodeTlv(invokeResponse);
967
- const invokeResponseBytes = import_types.TlvAny.getEncodedByteLength(encodedInvokeResponse);
968
- if (messageSize + invokeResponseBytes > exchange.maxPayloadSize || invokeResultsProcessed === invokeRequests.length) {
969
- let lastMessageProcessed = false;
970
- if (messageSize + invokeResponseBytes <= exchange.maxPayloadSize) {
971
- invokeResponseMessage.invokeResponses.push(encodedInvokeResponse);
972
- lastMessageProcessed = true;
973
- }
974
- if (invokeResponseMessage.invokeResponses.length > 0) {
975
- if (invokeRequests.length > 1) {
976
- logger.debug(
977
- `Send ${lastMessageProcessed ? "final " : ""}invoke response for ${invokeResponseMessage.invokeResponses} commands`
978
- );
979
- }
980
- const moreChunkedMessages = lastMessageProcessed ? void 0 : true;
981
- await messenger.send(
982
- import_InteractionMessenger.MessageType.InvokeResponse,
983
- import_types.TlvInvokeResponseForSend.encode({
984
- ...invokeResponseMessage,
985
- moreChunkedMessages
986
- }),
987
- {
988
- logContext: {
989
- invokeMsgFlags: import_general.Diagnostic.asFlags({
990
- suppressResponse,
991
- moreChunkedMessages
992
- })
993
- }
994
- }
995
- );
996
- invokeResponseMessage.invokeResponses = [];
997
- messageSize = emptyInvokeResponseBytes.length;
998
- }
999
- if (!lastMessageProcessed) {
1000
- invokeResultsProcessed--;
1001
- return processResponseResult(invokeResponse);
1002
- }
1003
- } else {
1004
- invokeResponseMessage.invokeResponses.push(encodedInvokeResponse);
1005
- messageSize += invokeResponseBytes;
1006
- }
1007
- };
1008
- for (const { commandPath, commandFields, commandRef } of invokeRequests) {
1009
- const commands = this.#endpointStructure.getCommands([commandPath]);
1010
- if (commands.length === 0) {
1011
- if (isConcreteCommandPath(commandPath)) {
1012
- const { endpointId, clusterId, commandId } = commandPath;
1013
- let result;
1014
- try {
1015
- this.#endpointStructure.validateConcreteCommandPath(endpointId, clusterId, commandId);
1016
- throw new import_general.InternalError(
1017
- "validateConcreteCommandPath should throw StatusResponseError but did not."
1018
- );
1019
- } catch (e) {
1020
- import_types.StatusResponseError.accept(e);
1021
- logger.debug(
1022
- `Invoke from ${exchange.channel.name}: ${this.#endpointStructure.resolveCommandName(
1023
- commandPath
1024
- )} unsupported path: Status=${e.code}`
1025
- );
1026
- result = { status: { commandPath, status: { status: e.code }, commandRef } };
1027
- }
1028
- await processResponseResult(result);
1029
- } else {
1030
- logger.debug(
1031
- `Invoke from ${exchange.channel.name}: ${this.#endpointStructure.resolveCommandName(
1032
- commandPath
1033
- )} ignore non-existing command`
1034
- );
1035
- }
1036
- continue;
1037
- }
1038
- const isConcretePath = isConcreteCommandPath(commandPath);
1039
- for (const { command, path } of commands) {
1040
- const { endpointId, clusterId, commandId } = path;
1041
- if (endpointId === void 0) {
1042
- logger.error(
1043
- `Invoke from ${exchange.channel.name}: ${this.#endpointStructure.resolveCommandName(
1044
- path
1045
- )} invalid path because empty endpoint!`
1046
- );
1047
- if (isConcretePath) {
1048
- await processResponseResult({
1049
- status: {
1050
- commandPath: path,
1051
- status: { status: import_types.StatusCode.UnsupportedEndpoint },
1052
- commandRef
1053
- }
1054
- });
1055
- }
1056
- continue;
1057
- }
1058
- const endpoint = this.#endpointStructure.getEndpoint(endpointId);
1059
- if (endpoint === void 0) {
1060
- logger.error(
1061
- `Invoke from ${exchange.channel.name}: ${this.#endpointStructure.resolveCommandName(
1062
- path
1063
- )} invalid path because endpoint not found!`
1064
- );
1065
- if (isConcretePath) {
1066
- await processResponseResult({
1067
- status: {
1068
- commandPath: path,
1069
- status: { status: import_types.StatusCode.UnsupportedEndpoint },
1070
- commandRef
1071
- }
1072
- });
1073
- }
1074
- continue;
1075
- }
1076
- if (command.requiresTimedInteraction && !receivedWithinTimedInteraction) {
1077
- logger.debug(`This invoke requires a timed interaction which is not initialized.`);
1078
- if (isConcretePath) {
1079
- await processResponseResult({
1080
- status: {
1081
- commandPath: path,
1082
- status: { status: import_types.StatusCode.NeedsTimedInteraction },
1083
- commandRef
1084
- }
1085
- });
1086
- }
1087
- continue;
1088
- }
1089
- if (getMatterModelClusterCommand(clusterId, commandId)?.fabricScoped && (!exchange.session.isSecure || !exchange.session.fabric)) {
1090
- logger.debug(`This invoke requires a secure session with a fabric assigned which is missing.`);
1091
- if (isConcretePath) {
1092
- await processResponseResult({
1093
- status: { commandPath: path, status: { status: import_types.StatusCode.UnsupportedAccess }, commandRef }
1094
- });
1095
- }
1096
- continue;
1097
- }
1098
- let result;
1099
- try {
1100
- result = await this.invokeCommand(
1101
- path,
1102
- command,
1103
- exchange,
1104
- commandFields ?? import_types.TlvNoArguments.encodeTlv(commandFields),
1105
- message,
1106
- endpoint,
1107
- receivedWithinTimedInteraction
1108
- );
1109
- } catch (e) {
1110
- import_types.StatusResponseError.accept(e);
1111
- let errorCode = e.code;
1112
- const errorLogText = `Error ${import_general.Diagnostic.hex(errorCode)}${e.clusterCode !== void 0 ? `/${import_general.Diagnostic.hex(e.clusterCode)}` : ""} while invoking command: ${e.message}`;
1113
- if (e instanceof import_types.ValidationError) {
1114
- logger.info(
1115
- `Validation-${errorLogText}${e.fieldName !== void 0 ? ` in field ${e.fieldName}` : ""}`
1116
- );
1117
- if (errorCode === import_types.StatusCode.InvalidAction) {
1118
- errorCode = import_types.StatusCode.InvalidCommand;
1119
- }
1120
- } else {
1121
- logger.info(errorLogText);
1122
- }
1123
- result = {
1124
- code: errorCode,
1125
- clusterCode: e.clusterCode,
1126
- responseId: command.responseId,
1127
- response: import_types.TlvNoResponse.encodeTlv()
1128
- };
1129
- }
1130
- const { code, clusterCode, responseId, response } = result;
1131
- if (response.length === 0) {
1132
- await processResponseResult({
1133
- status: { commandPath: path, status: { status: code, clusterStatus: clusterCode }, commandRef }
1134
- });
1135
- } else {
1136
- await processResponseResult({
1137
- command: {
1138
- commandPath: { ...path, commandId: responseId },
1139
- commandFields: response,
1140
- commandRef
1141
- }
1142
- });
1143
- }
1144
- }
1145
- }
1146
- }
1147
- async invokeCommand(_path, command, exchange, commandFields, message, endpoint, _receivedWithinTimedInteraction = false) {
1148
- return command.invoke(exchange.session, commandFields, message, endpoint);
1149
- }
1150
- handleTimedRequest(exchange, { timeout, interactionModelRevision }) {
1151
- logger.debug(`Received timed request (${timeout}ms) from ${exchange.channel.name}`);
1152
- if (interactionModelRevision > import_model.Specification.INTERACTION_MODEL_REVISION) {
1153
- logger.debug(
1154
- `Interaction model revision of sender ${interactionModelRevision} is higher than supported ${import_model.Specification.INTERACTION_MODEL_REVISION}.`
1155
- );
1156
- }
1157
- exchange.startTimedInteraction(timeout);
1158
- }
1159
- async close() {
1160
- this.#isClosing = true;
1161
- }
1162
- get #endpointStructure() {
1163
- return this.#context.structure;
1164
- }
1165
- }
1166
- //# sourceMappingURL=InteractionServer.js.map