@project-chip/matter.js 0.0.0 → 0.0.1
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.
- package/README.md +11 -0
- package/dist/matter.d.ts +22 -0
- package/dist/matter.js +22 -0
- package/dist/schema/BitmapSchema.d.ts +42 -16
- package/dist/schema/BitmapSchema.js +32 -12
- package/dist/schema/Schema.d.ts +10 -1
- package/dist/schema/Schema.js +11 -6
- package/dist/spec/Specifications.d.ts +14 -0
- package/dist/spec/Specifications.js +6 -0
- package/dist/tlv/TlvAny.d.ts +13 -0
- package/dist/tlv/TlvAny.js +67 -0
- package/dist/tlv/TlvArray.d.ts +29 -0
- package/dist/tlv/TlvArray.js +47 -0
- package/dist/tlv/TlvBoolean.d.ts +18 -0
- package/dist/tlv/TlvBoolean.js +23 -0
- package/dist/tlv/TlvCodec.d.ts +98 -0
- package/dist/tlv/TlvCodec.js +244 -0
- package/dist/tlv/TlvNullable.d.ts +20 -0
- package/dist/tlv/TlvNullable.js +32 -0
- package/dist/tlv/TlvNumber.d.ts +46 -0
- package/dist/tlv/TlvNumber.js +76 -0
- package/dist/tlv/TlvObject.d.ts +56 -0
- package/dist/tlv/TlvObject.js +73 -0
- package/dist/tlv/TlvSchema.d.ts +67 -0
- package/dist/tlv/TlvSchema.js +84 -0
- package/dist/tlv/TlvString.d.ts +38 -0
- package/dist/tlv/TlvString.js +52 -0
- package/dist/tlv/TlvVoid.d.ts +17 -0
- package/dist/tlv/TlvVoid.js +22 -0
- package/dist/tlv/TlvWrapper.d.ts +16 -0
- package/dist/tlv/TlvWrapper.js +23 -0
- package/dist/util/ByteArray.d.ts +30 -0
- package/dist/util/ByteArray.js +45 -0
- package/dist/util/DataReader.d.ts +29 -0
- package/dist/util/DataReader.js +65 -0
- package/dist/util/DataWriter.d.ts +26 -0
- package/dist/util/DataWriter.js +98 -0
- package/dist/util/Number.d.ts +23 -0
- package/dist/util/Number.js +39 -0
- package/dist/util/Type.d.ts +9 -0
- package/dist/util/Type.js +6 -0
- package/dist-cjs/matter.js +38 -0
- package/package.json +17 -12
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -1
package/README.md
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
1
|
# matter.js
|
|
2
|
+
|
|
3
|
+
 [](https://raw.githubusercontent.com/project-chip/matter.js/master/LICENSE)
|
|
4
|
+
|
|
2
5
|
Implementation of Matter protocol in typescript with no native dependencies (and very limited dependencies).
|
|
6
|
+
|
|
7
|
+
## Tests
|
|
8
|
+
|
|
9
|
+
Run tests in node.js: npm run test
|
|
10
|
+
|
|
11
|
+
Run tests in the browser: npm run test-web
|
|
12
|
+
|
|
13
|
+
test-web will create a /coverage directory conatining the test code coverage
|
package/dist/matter.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export * from "./schema/BitmapSchema.js";
|
|
7
|
+
export * from "./schema/Schema.js";
|
|
8
|
+
export * from "./spec/Specifications.js";
|
|
9
|
+
export * from "./util/Number.js";
|
|
10
|
+
export * from "./util/ByteArray.js";
|
|
11
|
+
export * from "./util/DataReader.js";
|
|
12
|
+
export * from "./util/DataWriter.js";
|
|
13
|
+
export * from "./tlv/TlvAny.js";
|
|
14
|
+
export * from "./tlv/TlvArray.js";
|
|
15
|
+
export * from "./tlv/TlvBoolean.js";
|
|
16
|
+
export * from "./tlv/TlvSchema.js";
|
|
17
|
+
export * from "./tlv/TlvNullable.js";
|
|
18
|
+
export * from "./tlv/TlvObject.js";
|
|
19
|
+
export * from "./tlv/TlvString.js";
|
|
20
|
+
export * from "./tlv/TlvNumber.js";
|
|
21
|
+
export * from "./tlv/TlvVoid.js";
|
|
22
|
+
export * from "./tlv/TlvWrapper.js";
|
package/dist/matter.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export * from "./schema/BitmapSchema.js";
|
|
7
|
+
export * from "./schema/Schema.js";
|
|
8
|
+
export * from "./spec/Specifications.js";
|
|
9
|
+
export * from "./util/Number.js";
|
|
10
|
+
export * from "./util/ByteArray.js";
|
|
11
|
+
export * from "./util/DataReader.js";
|
|
12
|
+
export * from "./util/DataWriter.js";
|
|
13
|
+
export * from "./tlv/TlvAny.js";
|
|
14
|
+
export * from "./tlv/TlvArray.js";
|
|
15
|
+
export * from "./tlv/TlvBoolean.js";
|
|
16
|
+
export * from "./tlv/TlvSchema.js";
|
|
17
|
+
export * from "./tlv/TlvNullable.js";
|
|
18
|
+
export * from "./tlv/TlvObject.js";
|
|
19
|
+
export * from "./tlv/TlvString.js";
|
|
20
|
+
export * from "./tlv/TlvNumber.js";
|
|
21
|
+
export * from "./tlv/TlvVoid.js";
|
|
22
|
+
export * from "./tlv/TlvWrapper.js";
|
|
@@ -1,21 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "./Schema.js";
|
|
7
|
+
declare const enum BitRangeType {
|
|
8
|
+
Flag = 0,
|
|
9
|
+
Number = 1,
|
|
10
|
+
Enum = 2
|
|
3
11
|
}
|
|
4
|
-
|
|
5
|
-
|
|
12
|
+
type BitRange<T> = {
|
|
13
|
+
type: BitRangeType;
|
|
14
|
+
offset: number;
|
|
15
|
+
mask: number;
|
|
6
16
|
};
|
|
7
|
-
declare type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
declare const BitRange: <T>(type: BitRangeType, offset: number, length: number) => BitRange<T>;
|
|
18
|
+
/** Defines the bit position of a boolean flag. */
|
|
19
|
+
export interface BitFlag extends BitRange<boolean> {
|
|
20
|
+
type: BitRangeType.Flag;
|
|
21
|
+
}
|
|
22
|
+
export declare const BitFlag: (offset: number) => BitFlag;
|
|
23
|
+
/** Defines the bit position and bit length of a numeric value. */
|
|
24
|
+
export interface BitField extends BitRange<number> {
|
|
25
|
+
type: BitRangeType.Number;
|
|
26
|
+
}
|
|
27
|
+
export declare const BitField: (offset: number, length: number) => BitField;
|
|
28
|
+
/** Defines the bit position and bit length of an enum flag. */
|
|
29
|
+
export interface BitFieldEnum<E extends number> extends BitRange<E> {
|
|
30
|
+
type: BitRangeType.Enum;
|
|
31
|
+
}
|
|
32
|
+
export declare const BitFieldEnum: <E extends number>(offset: number, length: number) => BitFieldEnum<E>;
|
|
33
|
+
export type BitSchema = {
|
|
34
|
+
[key: string]: BitFlag | BitField | BitFieldEnum<any>;
|
|
12
35
|
};
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
encodeInternal(value: TypeFromBitSchema<BitSchema>): number;
|
|
16
|
-
decodeInternal(bitmap: number): TypeFromBitSchema<BitSchema>;
|
|
17
|
-
encode(value: TypeFromBitSchema<BitSchema>): number;
|
|
18
|
-
decode(encoded: number): TypeFromBitSchema<BitSchema>;
|
|
19
|
-
validate(value: TypeFromBitSchema<BitSchema>): void;
|
|
36
|
+
export type TypeFromBitSchema<T extends BitSchema> = {
|
|
37
|
+
[K in keyof T]: T[K] extends BitFieldEnum<infer E> ? E : (T[K] extends BitField ? number : boolean);
|
|
20
38
|
};
|
|
39
|
+
declare class BitmapSchemaInternal<T extends BitSchema> extends Schema<TypeFromBitSchema<T>, number> {
|
|
40
|
+
readonly bitSchemas: T;
|
|
41
|
+
constructor(bitSchemas: T);
|
|
42
|
+
encodeInternal(value: TypeFromBitSchema<T>): number;
|
|
43
|
+
decodeInternal(bitmap: number): TypeFromBitSchema<T>;
|
|
44
|
+
}
|
|
45
|
+
/** Declares a bitmap schema by indicating the bit position and their names. */
|
|
46
|
+
export declare const BitmapSchema: <T extends BitSchema>(bitSchemas: T) => BitmapSchemaInternal<T>;
|
|
21
47
|
export {};
|
|
@@ -1,28 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { Schema } from "./Schema.js";
|
|
7
|
+
const BitRange = (type, offset, length) => ({ type, mask: ((1 << length) - 1) << offset, offset });
|
|
8
|
+
export const BitFlag = (offset) => BitRange(0 /* BitRangeType.Flag */, offset, 1);
|
|
9
|
+
export const BitField = (offset, length) => BitRange(1 /* BitRangeType.Number */, offset, length);
|
|
10
|
+
export const BitFieldEnum = (offset, length) => BitRange(2 /* BitRangeType.Enum */, offset, length);
|
|
11
|
+
class BitmapSchemaInternal extends Schema {
|
|
8
12
|
constructor(bitSchemas) {
|
|
9
13
|
super();
|
|
10
14
|
this.bitSchemas = bitSchemas;
|
|
15
|
+
// TODO: validate that bitSchemas is coherent
|
|
11
16
|
}
|
|
12
17
|
encodeInternal(value) {
|
|
13
18
|
let result = 0;
|
|
14
19
|
for (const name in this.bitSchemas) {
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
const { type, offset, mask } = this.bitSchemas[name];
|
|
21
|
+
switch (type) {
|
|
22
|
+
case 0 /* BitRangeType.Flag */:
|
|
23
|
+
if (value[name])
|
|
24
|
+
result |= mask;
|
|
25
|
+
break;
|
|
26
|
+
case 2 /* BitRangeType.Enum */:
|
|
27
|
+
case 1 /* BitRangeType.Number */:
|
|
28
|
+
result |= value[name] << offset;
|
|
29
|
+
}
|
|
17
30
|
}
|
|
18
31
|
return result;
|
|
19
32
|
}
|
|
20
33
|
decodeInternal(bitmap) {
|
|
21
34
|
const result = {};
|
|
22
35
|
for (const name in this.bitSchemas) {
|
|
23
|
-
|
|
36
|
+
const { type, offset, mask } = this.bitSchemas[name];
|
|
37
|
+
if (type === 0 /* BitRangeType.Flag */) {
|
|
38
|
+
result[name] = (bitmap & mask) !== 0;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
result[name] = (bitmap & mask) >> offset;
|
|
42
|
+
}
|
|
24
43
|
}
|
|
25
44
|
return result;
|
|
26
45
|
}
|
|
27
|
-
}
|
|
28
|
-
|
|
46
|
+
}
|
|
47
|
+
/** Declares a bitmap schema by indicating the bit position and their names. */
|
|
48
|
+
export const BitmapSchema = (bitSchemas) => new BitmapSchemaInternal(bitSchemas);
|
package/dist/schema/Schema.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/** Define a schema to encode / decode convert type T to type E. */
|
|
1
7
|
export declare abstract class Schema<T, E> {
|
|
8
|
+
/** Encodes the value using the schema. */
|
|
2
9
|
encode(value: T): E;
|
|
10
|
+
/** Decodes the encoded data using the schema. */
|
|
3
11
|
decode(encoded: E): T;
|
|
4
12
|
protected abstract encodeInternal(value: T): E;
|
|
5
13
|
protected abstract decodeInternal(encoded: E): T;
|
|
6
|
-
|
|
14
|
+
/** Optional validator that can be used to enforce constraints on the data before encoding / after decoding. */
|
|
15
|
+
validate(_value: T): void;
|
|
7
16
|
}
|
package/dist/schema/Schema.js
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/** Define a schema to encode / decode convert type T to type E. */
|
|
7
|
+
export class Schema {
|
|
8
|
+
/** Encodes the value using the schema. */
|
|
5
9
|
encode(value) {
|
|
6
10
|
this.validate(value);
|
|
7
11
|
return this.encodeInternal(value);
|
|
8
12
|
}
|
|
13
|
+
/** Decodes the encoded data using the schema. */
|
|
9
14
|
decode(encoded) {
|
|
10
15
|
const result = this.decodeInternal(encoded);
|
|
11
16
|
this.validate(result);
|
|
12
17
|
return result;
|
|
13
18
|
}
|
|
14
|
-
|
|
19
|
+
/** Optional validator that can be used to enforce constraints on the data before encoding / after decoding. */
|
|
20
|
+
validate(_value) {
|
|
15
21
|
}
|
|
16
22
|
}
|
|
17
|
-
exports.Schema = Schema;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Core Specification 1.0} */
|
|
7
|
+
export interface MatterCoreSpecificationV1_0 {
|
|
8
|
+
}
|
|
9
|
+
/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Application Cluster Specification 1.0} */
|
|
10
|
+
export interface MatterApplicationClusterSpecificationV1_0 {
|
|
11
|
+
}
|
|
12
|
+
/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Device Library Specification 1.0} */
|
|
13
|
+
export interface MatterDeviceLibrarySpecificationV1_0 {
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvTag, TlvTypeLength } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvStream, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
export declare class AnySchema extends TlvSchema<TlvStream> {
|
|
9
|
+
encodeTlvInternal(writer: TlvWriter, tlvStream: TlvStream, tagAssigned?: TlvTag | undefined): void;
|
|
10
|
+
decodeTlvInternalValue(reader: TlvReader, typeLength: TlvTypeLength): TlvStream;
|
|
11
|
+
decodeTlvValueRec(reader: TlvReader, typeLength: TlvTypeLength, tlvStream: TlvStream, tag?: TlvTag): TlvStream;
|
|
12
|
+
}
|
|
13
|
+
export declare const TlvAny: AnySchema;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
7
|
+
export class AnySchema extends TlvSchema {
|
|
8
|
+
encodeTlvInternal(writer, tlvStream, tagAssigned) {
|
|
9
|
+
tlvStream.forEach(({ tag, typeLength, value }) => {
|
|
10
|
+
if (tagAssigned !== undefined) {
|
|
11
|
+
// Assign the tag to the 1st TLV element in the stream
|
|
12
|
+
tag = tagAssigned;
|
|
13
|
+
tagAssigned = undefined;
|
|
14
|
+
}
|
|
15
|
+
switch (typeLength.type) {
|
|
16
|
+
case 20 /* TlvType.Null */:
|
|
17
|
+
case 8 /* TlvType.Boolean */:
|
|
18
|
+
case 22 /* TlvType.Array */:
|
|
19
|
+
case 21 /* TlvType.Structure */:
|
|
20
|
+
case 23 /* TlvType.List */:
|
|
21
|
+
case 24 /* TlvType.EndOfContainer */:
|
|
22
|
+
writer.writeTag(typeLength, tagAssigned !== null && tagAssigned !== void 0 ? tagAssigned : tag);
|
|
23
|
+
break;
|
|
24
|
+
case 4 /* TlvType.UnsignedInt */:
|
|
25
|
+
case 0 /* TlvType.SignedInt */:
|
|
26
|
+
case 10 /* TlvType.Float */:
|
|
27
|
+
case 12 /* TlvType.Utf8String */:
|
|
28
|
+
case 16 /* TlvType.ByteString */:
|
|
29
|
+
writer.writeTag(typeLength, tagAssigned !== null && tagAssigned !== void 0 ? tagAssigned : tag);
|
|
30
|
+
writer.writePrimitive(typeLength, value);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
decodeTlvInternalValue(reader, typeLength) {
|
|
36
|
+
return this.decodeTlvValueRec(reader, typeLength, new Array());
|
|
37
|
+
}
|
|
38
|
+
decodeTlvValueRec(reader, typeLength, tlvStream, tag) {
|
|
39
|
+
switch (typeLength.type) {
|
|
40
|
+
case 20 /* TlvType.Null */:
|
|
41
|
+
case 8 /* TlvType.Boolean */:
|
|
42
|
+
case 4 /* TlvType.UnsignedInt */:
|
|
43
|
+
case 0 /* TlvType.SignedInt */:
|
|
44
|
+
case 10 /* TlvType.Float */:
|
|
45
|
+
case 12 /* TlvType.Utf8String */:
|
|
46
|
+
case 16 /* TlvType.ByteString */:
|
|
47
|
+
tlvStream.push({ tag, typeLength, value: reader.readPrimitive(typeLength) });
|
|
48
|
+
break;
|
|
49
|
+
case 22 /* TlvType.Array */:
|
|
50
|
+
case 21 /* TlvType.Structure */:
|
|
51
|
+
case 23 /* TlvType.List */:
|
|
52
|
+
tlvStream.push({ tag, typeLength });
|
|
53
|
+
while (true) {
|
|
54
|
+
const { tag: elementTag, typeLength: typeLengthElement } = reader.readTagType();
|
|
55
|
+
this.decodeTlvValueRec(reader, typeLengthElement, tlvStream, elementTag);
|
|
56
|
+
if (typeLengthElement.type === 24 /* TlvType.EndOfContainer */)
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 24 /* TlvType.EndOfContainer */:
|
|
61
|
+
tlvStream.push({ tag, typeLength });
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return tlvStream;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export const TlvAny = new AnySchema();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvTag, TlvTypeLength } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
type LengthConstraints = {
|
|
9
|
+
minLength?: number;
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
length?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Schema to encode an array or string in TLV.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.2 and A.11.4
|
|
17
|
+
*/
|
|
18
|
+
export declare class ArraySchema<T> extends TlvSchema<T[]> {
|
|
19
|
+
private readonly elementSchema;
|
|
20
|
+
private readonly minLength;
|
|
21
|
+
private readonly maxLength;
|
|
22
|
+
constructor(elementSchema: TlvSchema<T>, minLength?: number, maxLength?: number);
|
|
23
|
+
encodeTlvInternal(writer: TlvWriter, value: T[], tag?: TlvTag): void;
|
|
24
|
+
decodeTlvInternalValue(reader: TlvReader, typeLength: TlvTypeLength): T[];
|
|
25
|
+
validate({ length }: T[]): void;
|
|
26
|
+
}
|
|
27
|
+
/** Array TLV schema. */
|
|
28
|
+
export declare const TlvArray: <T>(elementSchema: TlvSchema<T>, { minLength, maxLength, length }?: LengthConstraints) => ArraySchema<T>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
7
|
+
/**
|
|
8
|
+
* Schema to encode an array or string in TLV.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.2 and A.11.4
|
|
11
|
+
*/
|
|
12
|
+
export class ArraySchema extends TlvSchema {
|
|
13
|
+
constructor(elementSchema, minLength = 0, maxLength = 1024) {
|
|
14
|
+
super();
|
|
15
|
+
this.elementSchema = elementSchema;
|
|
16
|
+
this.minLength = minLength;
|
|
17
|
+
this.maxLength = maxLength;
|
|
18
|
+
}
|
|
19
|
+
encodeTlvInternal(writer, value, tag = {}) {
|
|
20
|
+
writer.writeTag({ type: 22 /* TlvType.Array */ }, tag);
|
|
21
|
+
value.forEach(element => this.elementSchema.encodeTlvInternal(writer, element));
|
|
22
|
+
writer.writeTag({ type: 24 /* TlvType.EndOfContainer */ });
|
|
23
|
+
}
|
|
24
|
+
decodeTlvInternalValue(reader, typeLength) {
|
|
25
|
+
if (typeLength.type !== 22 /* TlvType.Array */)
|
|
26
|
+
throw new Error(`Unexpected type ${typeLength.type}.`);
|
|
27
|
+
const result = new Array();
|
|
28
|
+
while (true) {
|
|
29
|
+
const { tag: elementTag, typeLength: elementTypeLength } = reader.readTagType();
|
|
30
|
+
if (elementTag !== undefined)
|
|
31
|
+
throw new Error("Array element tags should be anonymous.");
|
|
32
|
+
if (elementTypeLength.type === 24 /* TlvType.EndOfContainer */)
|
|
33
|
+
break;
|
|
34
|
+
result.push(this.elementSchema.decodeTlvInternalValue(reader, elementTypeLength));
|
|
35
|
+
}
|
|
36
|
+
this.validate(result);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
validate({ length }) {
|
|
40
|
+
if (length > this.maxLength)
|
|
41
|
+
throw new Error(`Array is too long: ${length}, max ${this.maxLength}.`);
|
|
42
|
+
if (length < this.minLength)
|
|
43
|
+
throw new Error(`Array is too short: ${length}, min ${this.minLength}.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Array TLV schema. */
|
|
47
|
+
export const TlvArray = (elementSchema, { minLength, maxLength, length } = {}) => new ArraySchema(elementSchema, length !== null && length !== void 0 ? length : minLength, length !== null && length !== void 0 ? length : maxLength);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvTag, TlvTypeLength } from "./TlvCodec.js";
|
|
7
|
+
import { TlvReader, TlvSchema, TlvWriter } from "./TlvSchema.js";
|
|
8
|
+
/**
|
|
9
|
+
* Schema to encode a boolean in TLV.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.3
|
|
12
|
+
*/
|
|
13
|
+
export declare class BooleanSchema extends TlvSchema<boolean> {
|
|
14
|
+
encodeTlvInternal(writer: TlvWriter, value: boolean, tag?: TlvTag): void;
|
|
15
|
+
decodeTlvInternalValue(_reader: TlvReader, typeLength: TlvTypeLength): boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Boolean TLV schema. */
|
|
18
|
+
export declare const TlvBoolean: BooleanSchema;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { TlvSchema } from "./TlvSchema.js";
|
|
7
|
+
/**
|
|
8
|
+
* Schema to encode a boolean in TLV.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.11.3
|
|
11
|
+
*/
|
|
12
|
+
export class BooleanSchema extends TlvSchema {
|
|
13
|
+
encodeTlvInternal(writer, value, tag = {}) {
|
|
14
|
+
writer.writeTag({ type: 8 /* TlvType.Boolean */, value }, tag);
|
|
15
|
+
}
|
|
16
|
+
decodeTlvInternalValue(_reader, typeLength) {
|
|
17
|
+
if (typeLength.type !== 8 /* TlvType.Boolean */)
|
|
18
|
+
throw new Error(`Unexpected type ${typeLength.type}.`);
|
|
19
|
+
return typeLength.value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Boolean TLV schema. */
|
|
23
|
+
export const TlvBoolean = new BooleanSchema();
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2022 Project CHIP Authors
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { ByteArray, Endian } from "../util/ByteArray.js";
|
|
7
|
+
import { DataReader } from "../util/DataReader.js";
|
|
8
|
+
import { DataWriter } from "../util/DataWriter.js";
|
|
9
|
+
/**
|
|
10
|
+
* TLV element types.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link MatterCoreSpecificationV1_0} § A.7.1
|
|
13
|
+
*/
|
|
14
|
+
export declare const enum TlvType {
|
|
15
|
+
SignedInt = 0,
|
|
16
|
+
UnsignedInt = 4,
|
|
17
|
+
Boolean = 8,
|
|
18
|
+
Float = 10,
|
|
19
|
+
Utf8String = 12,
|
|
20
|
+
ByteString = 16,
|
|
21
|
+
Null = 20,
|
|
22
|
+
Structure = 21,
|
|
23
|
+
Array = 22,
|
|
24
|
+
List = 23,
|
|
25
|
+
EndOfContainer = 24
|
|
26
|
+
}
|
|
27
|
+
/** Byte length of the encoded value or length. */
|
|
28
|
+
export declare const enum TlvLength {
|
|
29
|
+
OneByte = 0,
|
|
30
|
+
TwoBytes = 1,
|
|
31
|
+
FourBytes = 2,
|
|
32
|
+
EightBytes = 3
|
|
33
|
+
}
|
|
34
|
+
/** Type and length or value, when applicable. */
|
|
35
|
+
export type TlvTypeLength = {
|
|
36
|
+
type: TlvType.SignedInt;
|
|
37
|
+
length: TlvLength;
|
|
38
|
+
} | {
|
|
39
|
+
type: TlvType.UnsignedInt;
|
|
40
|
+
length: TlvLength;
|
|
41
|
+
} | {
|
|
42
|
+
type: TlvType.Boolean;
|
|
43
|
+
value: boolean;
|
|
44
|
+
} | {
|
|
45
|
+
type: TlvType.Float;
|
|
46
|
+
length: TlvLength.FourBytes | TlvLength.EightBytes;
|
|
47
|
+
} | {
|
|
48
|
+
type: TlvType.Utf8String;
|
|
49
|
+
length: TlvLength;
|
|
50
|
+
} | {
|
|
51
|
+
type: TlvType.ByteString;
|
|
52
|
+
length: TlvLength;
|
|
53
|
+
} | {
|
|
54
|
+
type: TlvType.Null;
|
|
55
|
+
} | {
|
|
56
|
+
type: TlvType.Structure;
|
|
57
|
+
} | {
|
|
58
|
+
type: TlvType.Array;
|
|
59
|
+
} | {
|
|
60
|
+
type: TlvType.List;
|
|
61
|
+
} | {
|
|
62
|
+
type: TlvType.EndOfContainer;
|
|
63
|
+
};
|
|
64
|
+
/** Converts {@link TlvType} to the js primitive type. */
|
|
65
|
+
export type TlvToPrimitive = {
|
|
66
|
+
[TlvType.SignedInt]: bigint | number;
|
|
67
|
+
[TlvType.UnsignedInt]: bigint | number;
|
|
68
|
+
[TlvType.Boolean]: never;
|
|
69
|
+
[TlvType.Float]: number;
|
|
70
|
+
[TlvType.Utf8String]: string;
|
|
71
|
+
[TlvType.ByteString]: ByteArray;
|
|
72
|
+
[TlvType.Null]: null;
|
|
73
|
+
[TlvType.Structure]: never;
|
|
74
|
+
[TlvType.Array]: never;
|
|
75
|
+
[TlvType.List]: never;
|
|
76
|
+
[TlvType.EndOfContainer]: never;
|
|
77
|
+
};
|
|
78
|
+
/** {@link MatterCoreSpecificationV1_0} § A.2 */
|
|
79
|
+
export type TlvTag = {
|
|
80
|
+
profile?: number;
|
|
81
|
+
id?: number;
|
|
82
|
+
};
|
|
83
|
+
export declare class TlvCodec {
|
|
84
|
+
static getUIntTlvLength(value: number | bigint): TlvLength;
|
|
85
|
+
static getIntTlvLength(value: number | bigint): TlvLength;
|
|
86
|
+
/** @see {@link MatterCoreSpecificationV1_0} § A.7 */
|
|
87
|
+
static readTagType(reader: DataReader<Endian.Little>): {
|
|
88
|
+
tag?: TlvTag;
|
|
89
|
+
typeLength: TlvTypeLength;
|
|
90
|
+
};
|
|
91
|
+
private static readTag;
|
|
92
|
+
private static parseTypeLength;
|
|
93
|
+
static readPrimitive<T extends TlvTypeLength, V = TlvToPrimitive[T["type"]]>(reader: DataReader<Endian.Little>, typeLength: T): V;
|
|
94
|
+
/** @see {@link MatterCoreSpecificationV1_0} § A.7 & A.8 */
|
|
95
|
+
static writeTag(writer: DataWriter<Endian.Little>, typeLengthValue: TlvTypeLength, { profile, id }?: TlvTag): void;
|
|
96
|
+
static writePrimitive<T extends TlvTypeLength>(writer: DataWriter<Endian.Little>, typeLength: T, value: TlvToPrimitive[T["type"]]): void;
|
|
97
|
+
private static writeUInt;
|
|
98
|
+
}
|