Skip to content

Return correct status for unsupported Attributes #263

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@project-chip/matter.js": "^0.1.0",
"@project-chip/matter.js": "^0.2.1",
"bn.js": "^5.2.0",
"elliptic": "^6.5.4"
},
Expand Down
27 changes: 24 additions & 3 deletions src/matter/interaction/InteractionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ export class InteractionClient {
interactionModelRevision: 1,
isFabricFiltered: true,
});
if (!Array.isArray(response.values)) {
return []; // TODO handle Errors correctly
}

return response.values.map(({ value: { path: {endpointId, clusterId, attributeId}, version, value }}) => {
return response.values.flatMap(({ value: reportValue} ) => {
if (reportValue === undefined) return [];
const { path: { endpointId, clusterId, attributeId }, version, value } = reportValue;
if (endpointId === undefined || clusterId === undefined || attributeId === undefined ) throw new Error("Invalid response");
return { endpointId, clusterId, attributeId, version, value };
});
Expand All @@ -109,7 +114,16 @@ export class InteractionClient {
isFabricFiltered: true,
});

const value = response.values.map(({value}) => value).find(({ path }) => endpointId === path.endpointId && clusterId === path.clusterId && id === path.attributeId);
let value;
if (Array.isArray(response.values)) {
value = response.values.map(({ value }) => value).find((value) => {
if (value === undefined) return false;
const { path } = value;
return endpointId === path.endpointId && clusterId === path.clusterId && id === path.attributeId;
});
// Todo add proper handling for returned attributeStatus information
}

if (value === undefined) {
if (optional) return undefined;
if (conformanceValue === undefined) throw new Error(`Attribute ${endpointId}/${clusterId}/${id} not found`);
Expand Down Expand Up @@ -141,7 +155,14 @@ export class InteractionClient {
}); // TODO: also initialize all values

const subscriptionListener = (dataReport: DataReport) => {
const value = dataReport.values.map(({value}) => value).find(({ path }) => endpointId === path.endpointId && clusterId === path.clusterId && id === path.attributeId);
if (!Array.isArray(dataReport.values)) {
return;
}
const value = dataReport.values.map(({ value }) => value).find((value) => {
if (value === undefined) return false;
const { path } = value;
return endpointId === path.endpointId && clusterId === path.clusterId && id === path.attributeId;
});
if (value === undefined) return;
listener(schema.decodeTlv(value.value), value.version);
};
Expand Down
4 changes: 2 additions & 2 deletions src/matter/interaction/InteractionMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const TlvAttributeReportValue = TlvObject({ // TODO consolidate with TlvA
/** @see {@link MatterCoreSpecificationV1_0}, section 10.5.5 */
export const TlvAttributeReport = TlvObject({ // AttributeReportIB
attributeStatus: TlvOptionalField(0, TlvAttributeStatus),
value: TlvField(1, TlvAttributeReportValue), // AttributeDataIB, TODO rename to attributeData, formally Optional
value: TlvOptionalField(1, TlvAttributeReportValue), // AttributeDataIB, TODO rename to attributeData
});

/** @see {@link MatterCoreSpecificationV1_0}, section 10.5.15 */
Expand Down Expand Up @@ -200,7 +200,7 @@ export const TlvReadRequest = TlvObject({
/** @see {@link MatterCoreSpecificationV1_0}, section 10.6.3 */
export const TlvDataReport = TlvObject({
subscriptionId: TlvOptionalField(0, TlvUInt32),
values: TlvField(1, TlvArray(TlvAttributeReport)), // TODO: rename to attributeReports, formally optional
values: TlvOptionalField(1, TlvArray(TlvAttributeReport)), // TODO: rename to attributeReports
eventReports: TlvOptionalField(2, TlvArray(TlvEventReport)),
moreChunkedMessages: TlvOptionalField(3, TlvBoolean),
suppressResponse: TlvOptionalField(4, TlvBoolean),
Expand Down
3 changes: 3 additions & 0 deletions src/matter/interaction/InteractionMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export class InteractionServerMessenger extends InteractionMessenger<MatterDevic

async sendDataReport(dataReport: DataReport) {
const messageBytes = TlvDataReport.encode(dataReport);
if (!Array.isArray(dataReport.values)) {
throw new Error(`DataReport.values must be an array, got: ${dataReport.values}`);
}
if (messageBytes.length > MAX_SPDU_LENGTH) {
// DataReport is too long, it needs to be sent in chunks
const attributeReportsToSend = [...dataReport.values];
Expand Down
41 changes: 24 additions & 17 deletions src/matter/interaction/InteractionServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import { MatterDevice } from "../MatterDevice";
import { ProtocolHandler } from "../common/ProtocolHandler";
import { MessageExchange } from "../common/MessageExchange";
import {
DataReport,
InteractionServerMessenger,
InvokeRequest,
InvokeResponse,
ReadRequest,
DataReport,
SubscribeRequest,
SubscribeResponse,
TimedRequest,
WriteRequest,
WriteResponse,
Expand All @@ -24,16 +23,21 @@ import { CommandServer, ResultCode } from "../cluster/server/CommandServer";
import { DescriptorCluster } from "../cluster/DescriptorCluster";
import { AttributeGetterServer, AttributeServer } from "../cluster/server/AttributeServer";
import { Attributes, Cluster, Commands, Events } from "../cluster/Cluster";
import { AttributeServers, AttributeInitialValues, ClusterServerHandlers } from "../cluster/server/ClusterServer";
import { AttributeInitialValues, AttributeServers, ClusterServerHandlers } from "../cluster/server/ClusterServer";
import { SecureSession } from "../session/SecureSession";
import { SubscriptionHandler } from "./SubscriptionHandler";
import { Logger } from "../../log/Logger";
import { DeviceTypeId } from "../common/DeviceTypeId";
import { ClusterId } from "../common/ClusterId";
import { BitSchema, TlvStream, TypeFromBitSchema, TypeFromSchema} from "@project-chip/matter.js";
import { BitSchema, TlvStream, TypeFromBitSchema, TypeFromSchema } from "@project-chip/matter.js";
import { EndpointNumber } from "../common/EndpointNumber";
import { capitalize } from "../../util/String";
import {StatusCode, TlvAttributePath, TlvSubscribeResponse} from "./InteractionMessages";
import {
StatusCode,
TlvAttributePath,
TlvAttributeReport,
TlvSubscribeResponse
} from "./InteractionMessages";
import { Message } from "../../codec/MessageCodec";
import { Crypto } from "../../crypto/Crypto";

Expand Down Expand Up @@ -178,23 +182,26 @@ export class InteractionServer implements ProtocolHandler<MatterDevice> {
handleReadRequest(exchange: MessageExchange<MatterDevice>, {attributes: attributePaths, isFabricFiltered}: ReadRequest): DataReport {
logger.debug(`Received read request from ${exchange.channel.getName()}: ${attributePaths.map(path => this.resolveAttributeName(path)).join(", ")}, isFabricFiltered=${isFabricFiltered}`);

const values = this.getAttributes(attributePaths)
.map(({ path, attribute }) => {
const { value, version } = attribute.getWithVersion(exchange.session);
return { path, value, version, schema: attribute.schema };
// UnsupportedNode/UnsupportedEndpoint/UnsupportedCluster/UnsupportedAttribute/UnsupportedRead

const reportValues = attributePaths.flatMap((path: TypeFromSchema<typeof TlvAttributePath>): TypeFromSchema<typeof TlvAttributeReport>[] => {
const attributes = this.getAttributes([ path ]);
if (attributes.length === 0) {
logger.debug(`Read from ${exchange.channel.getName()}: ${this.resolveAttributeName(path)} unsupported path`);
return [{ attributeStatus: { path, status: {status: StatusCode.UnsupportedAttribute} } }]; // TODO: Find correct status code
}

return attributes.map(({ path, attribute }) => {
const { value, version } = attribute.getWithVersion(exchange.session); // TODO check ACL
logger.debug(`Read from ${exchange.channel.getName()}: ${this.resolveAttributeName(path)}=${Logger.toJSON(value)} (version=${version})`);
return { value: { path, value: attribute.schema.encodeTlv(value), version } };
});
});

logger.debug(`Read request from ${exchange.channel.getName()} resolved to: ${values.map(({ path, value, version }) => `${this.resolveAttributeName(path)}=${Logger.toJSON(value)} (${version})`).join(", ")}`);
return {
interactionModelRevision: 1,
suppressResponse: false,
values: values.map(({ path, value, version, schema }) => ({
value: {
path,
version,
value: schema.encodeTlv(value),
},
})),
values: reportValues,
};
}

Expand Down