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