@project-chip/matter.js 0.0.1 → 0.1.0

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 (135) hide show
  1. package/README.md +14 -0
  2. package/dist/cjs/cluster/AccessControlCluster.js +105 -0
  3. package/dist/cjs/cluster/BasicInformationCluster.js +92 -0
  4. package/dist/cjs/cluster/BindingCluster.js +51 -0
  5. package/dist/cjs/cluster/BridgedDeviceBasicInformationCluster.js +75 -0
  6. package/dist/cjs/cluster/Cluster.js +49 -0
  7. package/dist/cjs/cluster/DescriptorCluster.js +53 -0
  8. package/dist/cjs/cluster/GeneralCommissioningCluster.js +89 -0
  9. package/dist/cjs/cluster/GroupsCluster.js +140 -0
  10. package/dist/cjs/cluster/IdentifyCluster.js +55 -0
  11. package/dist/cjs/cluster/LabelCluster.js +55 -0
  12. package/dist/cjs/cluster/OnOffCluster.js +105 -0
  13. package/dist/cjs/cluster/OperationalCredentialsCluster.js +205 -0
  14. package/dist/cjs/cluster/ScenesCluster.js +236 -0
  15. package/dist/cjs/common/AttributeId.js +23 -0
  16. package/dist/cjs/common/ClusterId.js +24 -0
  17. package/dist/cjs/common/DeviceTypeId.js +23 -0
  18. package/dist/cjs/common/EndpointNumber.js +23 -0
  19. package/dist/cjs/common/FabricId.js +24 -0
  20. package/dist/cjs/common/FabricIndex.js +28 -0
  21. package/dist/cjs/common/GroupId.js +30 -0
  22. package/dist/cjs/common/NodeId.js +31 -0
  23. package/dist/cjs/common/SubjectId.js +11 -0
  24. package/dist/cjs/common/VendorId.js +25 -0
  25. package/{dist-cjs → dist/cjs}/matter.js +12 -0
  26. package/dist/cjs/protocol/InteractionProtocol.js +136 -0
  27. package/dist/cjs/schema/BitmapSchema.js +133 -0
  28. package/dist/cjs/schema/Schema.js +26 -0
  29. package/dist/cjs/spec/Specifications.js +7 -0
  30. package/dist/cjs/tlv/TlvAny.js +71 -0
  31. package/dist/cjs/tlv/TlvArray.js +52 -0
  32. package/dist/cjs/tlv/TlvBoolean.js +27 -0
  33. package/dist/cjs/tlv/TlvCodec.js +248 -0
  34. package/dist/cjs/tlv/TlvNullable.js +37 -0
  35. package/dist/cjs/tlv/TlvNumber.js +83 -0
  36. package/dist/cjs/tlv/TlvObject.js +85 -0
  37. package/dist/cjs/tlv/TlvSchema.js +92 -0
  38. package/dist/cjs/tlv/TlvString.js +56 -0
  39. package/dist/cjs/tlv/TlvVoid.js +26 -0
  40. package/dist/cjs/tlv/TlvWrapper.js +27 -0
  41. package/dist/cjs/util/ByteArray.js +50 -0
  42. package/dist/cjs/util/DataReader.js +69 -0
  43. package/dist/cjs/util/DataWriter.js +102 -0
  44. package/dist/cjs/util/Number.js +46 -0
  45. package/dist/cjs/util/Type.js +10 -0
  46. package/dist/dts/cluster/AccessControlCluster.d.ts +108 -0
  47. package/dist/dts/cluster/BasicInformationCluster.d.ts +57 -0
  48. package/dist/dts/cluster/BindingCluster.d.ts +23 -0
  49. package/dist/dts/cluster/BridgedDeviceBasicInformationCluster.d.ts +53 -0
  50. package/dist/dts/cluster/Cluster.d.ts +109 -0
  51. package/dist/dts/cluster/DescriptorCluster.d.ts +25 -0
  52. package/dist/dts/cluster/GeneralCommissioningCluster.d.ts +94 -0
  53. package/dist/dts/cluster/GroupsCluster.d.ts +92 -0
  54. package/dist/dts/cluster/IdentifyCluster.d.ts +60 -0
  55. package/dist/dts/cluster/LabelCluster.d.ts +34 -0
  56. package/dist/dts/cluster/OnOffCluster.d.ts +85 -0
  57. package/dist/dts/cluster/OperationalCredentialsCluster.d.ts +156 -0
  58. package/dist/dts/cluster/ScenesCluster.d.ts +200 -0
  59. package/dist/dts/common/AttributeId.d.ts +17 -0
  60. package/dist/dts/common/ClusterId.d.ts +18 -0
  61. package/dist/dts/common/DeviceTypeId.d.ts +17 -0
  62. package/dist/dts/common/EndpointNumber.d.ts +17 -0
  63. package/dist/dts/common/FabricId.d.ts +18 -0
  64. package/dist/dts/common/FabricIndex.d.ts +22 -0
  65. package/dist/dts/common/GroupId.d.ts +24 -0
  66. package/dist/dts/common/NodeId.d.ts +19 -0
  67. package/dist/dts/common/SubjectId.d.ts +15 -0
  68. package/dist/dts/common/VendorId.d.ts +19 -0
  69. package/dist/{matter.d.ts → dts/matter.d.ts} +12 -0
  70. package/dist/dts/protocol/InteractionProtocol.d.ts +159 -0
  71. package/dist/{schema → dts/schema}/BitmapSchema.d.ts +15 -3
  72. package/dist/{schema → dts/schema}/Schema.d.ts +0 -0
  73. package/dist/{spec → dts/spec}/Specifications.d.ts +0 -0
  74. package/dist/{tlv → dts/tlv}/TlvAny.d.ts +0 -0
  75. package/dist/{tlv → dts/tlv}/TlvArray.d.ts +0 -0
  76. package/dist/{tlv → dts/tlv}/TlvBoolean.d.ts +0 -0
  77. package/dist/{tlv → dts/tlv}/TlvCodec.d.ts +0 -0
  78. package/dist/{tlv → dts/tlv}/TlvNullable.d.ts +0 -0
  79. package/dist/{tlv → dts/tlv}/TlvNumber.d.ts +0 -0
  80. package/dist/{tlv → dts/tlv}/TlvObject.d.ts +0 -0
  81. package/dist/{tlv → dts/tlv}/TlvSchema.d.ts +0 -0
  82. package/dist/{tlv → dts/tlv}/TlvString.d.ts +0 -0
  83. package/dist/{tlv → dts/tlv}/TlvVoid.d.ts +0 -0
  84. package/dist/{tlv → dts/tlv}/TlvWrapper.d.ts +0 -0
  85. package/dist/{util → dts/util}/ByteArray.d.ts +0 -0
  86. package/dist/{util → dts/util}/DataReader.d.ts +0 -0
  87. package/dist/{util → dts/util}/DataWriter.d.ts +0 -0
  88. package/dist/{util → dts/util}/Number.d.ts +0 -0
  89. package/dist/{util → dts/util}/Type.d.ts +1 -0
  90. package/dist/es/cluster/AccessControlCluster.js +102 -0
  91. package/dist/es/cluster/BasicInformationCluster.js +89 -0
  92. package/dist/es/cluster/BindingCluster.js +48 -0
  93. package/dist/es/cluster/BridgedDeviceBasicInformationCluster.js +72 -0
  94. package/dist/es/cluster/Cluster.js +36 -0
  95. package/dist/es/cluster/DescriptorCluster.js +50 -0
  96. package/dist/es/cluster/GeneralCommissioningCluster.js +86 -0
  97. package/dist/es/cluster/GroupsCluster.js +137 -0
  98. package/dist/es/cluster/IdentifyCluster.js +52 -0
  99. package/dist/es/cluster/LabelCluster.js +52 -0
  100. package/dist/es/cluster/OnOffCluster.js +102 -0
  101. package/dist/es/cluster/OperationalCredentialsCluster.js +202 -0
  102. package/dist/es/cluster/ScenesCluster.js +233 -0
  103. package/dist/es/common/AttributeId.js +19 -0
  104. package/dist/es/common/ClusterId.js +20 -0
  105. package/dist/es/common/DeviceTypeId.js +19 -0
  106. package/dist/es/common/EndpointNumber.js +19 -0
  107. package/dist/es/common/FabricId.js +20 -0
  108. package/dist/es/common/FabricIndex.js +24 -0
  109. package/dist/es/common/GroupId.js +26 -0
  110. package/dist/es/common/NodeId.js +27 -0
  111. package/dist/es/common/SubjectId.js +8 -0
  112. package/dist/es/common/VendorId.js +21 -0
  113. package/dist/{matter.js → es/matter.js} +12 -0
  114. package/dist/es/protocol/InteractionProtocol.js +133 -0
  115. package/dist/es/schema/BitmapSchema.js +125 -0
  116. package/dist/{schema → es/schema}/Schema.js +0 -0
  117. package/dist/{spec → es/spec}/Specifications.js +0 -0
  118. package/dist/{tlv → es/tlv}/TlvAny.js +0 -0
  119. package/dist/{tlv → es/tlv}/TlvArray.js +0 -0
  120. package/dist/{tlv → es/tlv}/TlvBoolean.js +0 -0
  121. package/dist/{tlv → es/tlv}/TlvCodec.js +0 -0
  122. package/dist/{tlv → es/tlv}/TlvNullable.js +0 -0
  123. package/dist/{tlv → es/tlv}/TlvNumber.js +0 -0
  124. package/dist/{tlv → es/tlv}/TlvObject.js +6 -2
  125. package/dist/{tlv → es/tlv}/TlvSchema.js +0 -0
  126. package/dist/{tlv → es/tlv}/TlvString.js +0 -0
  127. package/dist/{tlv → es/tlv}/TlvVoid.js +0 -0
  128. package/dist/{tlv → es/tlv}/TlvWrapper.js +0 -0
  129. package/dist/{util → es/util}/ByteArray.js +2 -0
  130. package/dist/{util → es/util}/DataReader.js +0 -0
  131. package/dist/{util → es/util}/DataWriter.js +0 -0
  132. package/dist/{util → es/util}/Number.js +0 -0
  133. package/dist/{util → es/util}/Type.js +1 -1
  134. package/package.json +6 -6
  135. package/dist/schema/BitmapSchema.js +0 -48
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TlvBitmap = exports.TlvEnum = exports.TlvUInt64 = exports.TlvUInt32 = exports.TlvUInt16 = exports.TlvUInt8 = exports.TlvInt64 = exports.TlvInt32 = exports.TlvInt16 = exports.TlvInt8 = exports.TlvDouble = exports.TlvFloat = exports.TlvLongNumberSchema = exports.TlvNumberSchema = exports.TlvNumericSchema = void 0;
4
+ /**
5
+ * @license
6
+ * Copyright 2022 Project CHIP Authors
7
+ * SPDX-License-Identifier: Apache-2.0
8
+ */
9
+ const Number_js_1 = require("../util/Number.js");
10
+ const TlvCodec_js_1 = require("./TlvCodec.js");
11
+ const TlvSchema_js_1 = require("./TlvSchema.js");
12
+ const TlvWrapper_js_1 = require("./TlvWrapper.js");
13
+ const BitmapSchema_js_1 = require("../schema/BitmapSchema.js");
14
+ /**
15
+ * Schema to encode an unsigned integer in TLV.
16
+ *
17
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.1
18
+ */
19
+ class TlvNumericSchema extends TlvSchema_js_1.TlvSchema {
20
+ constructor(type, lengthProvider, min, max) {
21
+ super();
22
+ this.type = type;
23
+ this.lengthProvider = lengthProvider;
24
+ this.min = min;
25
+ this.max = max;
26
+ }
27
+ encodeTlvInternal(writer, value, tag = {}) {
28
+ const typeLength = { type: this.type, length: this.lengthProvider(value) };
29
+ writer.writeTag(typeLength, tag);
30
+ writer.writePrimitive(typeLength, value);
31
+ }
32
+ decodeTlvInternalValue(reader, typeLength) {
33
+ if (typeLength.type !== this.type)
34
+ throw new Error(`Unexpected type ${typeLength.type}, was expecting ${this.type}.`);
35
+ const value = reader.readPrimitive(typeLength);
36
+ this.validate(value);
37
+ return value;
38
+ }
39
+ validate(value) {
40
+ super.validate(value);
41
+ if (this.min !== undefined && value < this.min)
42
+ throw new Error(`Invalid value: ${value} is below the minimum, ${this.min}.`);
43
+ if (this.max !== undefined && value > this.max)
44
+ throw new Error(`Invalid value: ${value} is above the maximum, ${this.max}.`);
45
+ }
46
+ /** Restrict value range. */
47
+ bound({ min, max }) {
48
+ return new TlvNumericSchema(this.type, this.lengthProvider, (0, Number_js_1.maxValue)(min, this.min), (0, Number_js_1.minValue)(max, this.max));
49
+ }
50
+ }
51
+ exports.TlvNumericSchema = TlvNumericSchema;
52
+ class TlvNumberSchema extends TlvNumericSchema {
53
+ constructor(type, lengthProvider, min, max) {
54
+ super(type, lengthProvider, min, max);
55
+ }
56
+ decodeTlvInternalValue(reader, typeLength) {
57
+ const value = super.decodeTlvInternalValue(reader, typeLength);
58
+ return typeof value === "bigint" ? Number(value) : value;
59
+ }
60
+ bound({ min, max }) {
61
+ return new TlvNumberSchema(this.type, this.lengthProvider, (0, Number_js_1.maxValue)(min, this.min), (0, Number_js_1.minValue)(max, this.max));
62
+ }
63
+ }
64
+ exports.TlvNumberSchema = TlvNumberSchema;
65
+ exports.TlvLongNumberSchema = (TlvNumericSchema);
66
+ /** Unsigned integer TLV schema. */
67
+ exports.TlvFloat = new TlvNumberSchema(10 /* TlvType.Float */, _value => 2 /* TlvLength.FourBytes */, Number_js_1.FLOAT32_MIN, Number_js_1.FLOAT32_MAX);
68
+ exports.TlvDouble = new TlvNumberSchema(10 /* TlvType.Float */, _value => 3 /* TlvLength.EightBytes */);
69
+ exports.TlvInt8 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT8_MIN, Number_js_1.INT8_MAX);
70
+ exports.TlvInt16 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT16_MIN, Number_js_1.INT16_MAX);
71
+ exports.TlvInt32 = new TlvNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT32_MIN, Number_js_1.INT32_MAX);
72
+ exports.TlvInt64 = new exports.TlvLongNumberSchema(0 /* TlvType.SignedInt */, value => TlvCodec_js_1.TlvCodec.getIntTlvLength(value), Number_js_1.INT64_MIN, Number_js_1.INT64_MAX);
73
+ exports.TlvUInt8 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT8_MAX);
74
+ exports.TlvUInt16 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT16_MAX);
75
+ exports.TlvUInt32 = new TlvNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT32_MAX);
76
+ exports.TlvUInt64 = new exports.TlvLongNumberSchema(4 /* TlvType.UnsignedInt */, value => TlvCodec_js_1.TlvCodec.getUIntTlvLength(value), 0, Number_js_1.UINT64_MAX);
77
+ const TlvEnum = () => exports.TlvUInt32;
78
+ exports.TlvEnum = TlvEnum;
79
+ const TlvBitmap = (underlyingSchema, bitSchema) => {
80
+ const bitmapSchema = (0, BitmapSchema_js_1.BitmapSchema)(bitSchema);
81
+ return new TlvWrapper_js_1.TlvWrapper(underlyingSchema, (bitmapData) => bitmapSchema.encode(bitmapData), value => bitmapSchema.decode(value));
82
+ };
83
+ exports.TlvBitmap = TlvBitmap;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvOptionalField = exports.TlvField = exports.TlvList = exports.TlvObject = exports.ObjectSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ const TlvAny_js_1 = require("./TlvAny.js");
11
+ ;
12
+ /**
13
+ * Schema to encode an object in TLV.
14
+ *
15
+ * @see {@link MatterCoreSpecificationV1_0} § A.5.1 and § A.11.4
16
+ */
17
+ class ObjectSchema extends TlvSchema_js_1.TlvSchema {
18
+ constructor(fieldDefinitions, type = 21 /* TlvType.Structure */) {
19
+ super();
20
+ this.fieldDefinitions = fieldDefinitions;
21
+ this.type = type;
22
+ this.fieldById = new Array();
23
+ for (const name in this.fieldDefinitions) {
24
+ const field = this.fieldDefinitions[name];
25
+ this.fieldById[field.id] = { name, field };
26
+ }
27
+ }
28
+ encodeTlvInternal(writer, value, tag = {}) {
29
+ writer.writeTag({ type: this.type }, tag);
30
+ for (const name in this.fieldDefinitions) {
31
+ const { id, schema, optional: isOptional } = this.fieldDefinitions[name];
32
+ const fieldValue = value[name];
33
+ if (fieldValue === undefined) {
34
+ if (!isOptional)
35
+ throw new Error(`Missing mandatory field ${name}`);
36
+ continue;
37
+ }
38
+ schema.encodeTlvInternal(writer, fieldValue, { id });
39
+ }
40
+ writer.writeTag({ type: 24 /* TlvType.EndOfContainer */ });
41
+ }
42
+ decodeTlvInternalValue(reader, typeLength) {
43
+ if (typeLength.type !== this.type)
44
+ throw new Error(`Unexpected type ${typeLength.type}.`);
45
+ const result = {};
46
+ while (true) {
47
+ const { tag: { profile, id } = {}, typeLength: elementTypeLength } = reader.readTagType();
48
+ if (elementTypeLength.type === 24 /* TlvType.EndOfContainer */)
49
+ break;
50
+ if (profile !== undefined)
51
+ throw new Error("Structure element tags should be context-specific.");
52
+ if (id === undefined)
53
+ throw new Error("Structure element tags should have an id.");
54
+ const fieldName = this.fieldById[id];
55
+ if (fieldName === undefined) {
56
+ // Ignore unknown field by decoding it as raw TLV so we skip forward the proper length.
57
+ TlvAny_js_1.TlvAny.decodeTlvInternalValue(reader, elementTypeLength);
58
+ continue;
59
+ }
60
+ const { field, name } = fieldName;
61
+ result[name] = field.schema.decodeTlvInternalValue(reader, elementTypeLength);
62
+ }
63
+ this.validate(result);
64
+ return result;
65
+ }
66
+ validate(value) {
67
+ for (const name in this.fieldDefinitions) {
68
+ if (!this.fieldDefinitions[name].optional && value[name] === undefined)
69
+ throw new Error(`Missing mandatory field ${name}`);
70
+ }
71
+ }
72
+ }
73
+ exports.ObjectSchema = ObjectSchema;
74
+ /** Object TLV schema. */
75
+ const TlvObject = (fields) => new ObjectSchema(fields, 21 /* TlvType.Structure */);
76
+ exports.TlvObject = TlvObject;
77
+ /** List TLV schema. */
78
+ const TlvList = (fields) => new ObjectSchema(fields, 23 /* TlvType.List */);
79
+ exports.TlvList = TlvList;
80
+ /** Object TLV mandatory field. */
81
+ const TlvField = (id, schema) => ({ id, schema, optional: false });
82
+ exports.TlvField = TlvField;
83
+ /** Object TLV optional field. */
84
+ const TlvOptionalField = (id, schema) => ({ id, schema, optional: true });
85
+ exports.TlvOptionalField = TlvOptionalField;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvByteArrayReader = exports.TlvByteArrayWriter = exports.TlvArrayReader = exports.TlvArrayWriter = exports.TlvSchema = void 0;
9
+ const ByteArray_js_1 = require("../util/ByteArray.js");
10
+ const DataReader_js_1 = require("../util/DataReader.js");
11
+ const DataWriter_js_1 = require("../util/DataWriter.js");
12
+ const Schema_js_1 = require("../schema/Schema.js");
13
+ const TlvCodec_js_1 = require("./TlvCodec.js");
14
+ class TlvSchema extends Schema_js_1.Schema {
15
+ decodeInternal(encoded) {
16
+ return this.decodeTlvInternal(new TlvByteArrayReader(encoded)).value;
17
+ }
18
+ encodeInternal(value) {
19
+ const writer = new TlvByteArrayWriter();
20
+ this.encodeTlvInternal(writer, value);
21
+ return writer.toByteArray();
22
+ }
23
+ encodeTlv(value) {
24
+ const writer = new TlvArrayWriter();
25
+ this.encodeTlvInternal(writer, value);
26
+ return writer.toTlvArray();
27
+ }
28
+ decodeTlv(encoded) {
29
+ return this.decodeTlvInternal(new TlvArrayReader(encoded)).value;
30
+ }
31
+ decodeTlvInternal(reader) {
32
+ const { tag, typeLength } = reader.readTagType();
33
+ return { tag, value: this.decodeTlvInternalValue(reader, typeLength) };
34
+ }
35
+ }
36
+ exports.TlvSchema = TlvSchema;
37
+ class TlvArrayWriter {
38
+ constructor() {
39
+ this.tlvArray = new Array();
40
+ }
41
+ writeTag(typeLength, tag) {
42
+ this.tlvArray.push({ tag, typeLength });
43
+ }
44
+ writePrimitive(_typeLength, value) {
45
+ this.tlvArray[this.tlvArray.length - 1].value = value;
46
+ }
47
+ toTlvArray() {
48
+ return this.tlvArray;
49
+ }
50
+ }
51
+ exports.TlvArrayWriter = TlvArrayWriter;
52
+ class TlvArrayReader {
53
+ constructor(tlvElements) {
54
+ this.tlvElements = tlvElements;
55
+ this.index = -1;
56
+ }
57
+ readTagType() {
58
+ this.index++;
59
+ return this.tlvElements[this.index];
60
+ }
61
+ readPrimitive(_typeLength) {
62
+ return this.tlvElements[this.index].value;
63
+ }
64
+ }
65
+ exports.TlvArrayReader = TlvArrayReader;
66
+ class TlvByteArrayWriter {
67
+ constructor() {
68
+ this.writer = new DataWriter_js_1.DataWriter(ByteArray_js_1.Endian.Little);
69
+ }
70
+ writeTag(typeLength, tag) {
71
+ TlvCodec_js_1.TlvCodec.writeTag(this.writer, typeLength, tag);
72
+ }
73
+ writePrimitive(typeLength, value) {
74
+ TlvCodec_js_1.TlvCodec.writePrimitive(this.writer, typeLength, value);
75
+ }
76
+ toByteArray() {
77
+ return this.writer.toByteArray();
78
+ }
79
+ }
80
+ exports.TlvByteArrayWriter = TlvByteArrayWriter;
81
+ class TlvByteArrayReader {
82
+ constructor(byteArray) {
83
+ this.reader = new DataReader_js_1.DataReader(byteArray, ByteArray_js_1.Endian.Little);
84
+ }
85
+ readTagType() {
86
+ return TlvCodec_js_1.TlvCodec.readTagType(this.reader);
87
+ }
88
+ readPrimitive(typeLength) {
89
+ return TlvCodec_js_1.TlvCodec.readPrimitive(this.reader, typeLength);
90
+ }
91
+ }
92
+ exports.TlvByteArrayReader = TlvByteArrayReader;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvString256max = exports.TlvString64max = exports.TlvString32max = exports.TlvString = exports.TlvByteString = exports.StringSchema = void 0;
9
+ const TlvCodec_js_1 = require("./TlvCodec.js");
10
+ const TlvSchema_js_1 = require("./TlvSchema.js");
11
+ const Number_js_1 = require("../util/Number.js");
12
+ /**
13
+ * Schema to encode an byte string or an Utf8 string in TLV.
14
+ *
15
+ * @see {@link MatterCoreSpecificationV1_0} § A.11.2
16
+ */
17
+ class StringSchema extends TlvSchema_js_1.TlvSchema {
18
+ constructor(type, minLength = 0, maxLength = 1024) {
19
+ super();
20
+ this.type = type;
21
+ this.minLength = minLength;
22
+ this.maxLength = maxLength;
23
+ if (minLength < 0)
24
+ throw new Error("Minimum length should be a positive number.");
25
+ }
26
+ encodeTlvInternal(writer, value, tag = {}) {
27
+ const typeLength = { type: this.type, length: TlvCodec_js_1.TlvCodec.getUIntTlvLength(value.length) };
28
+ writer.writeTag(typeLength, tag);
29
+ writer.writePrimitive(typeLength, value);
30
+ }
31
+ decodeTlvInternalValue(reader, typeLength) {
32
+ if (typeLength.type !== this.type)
33
+ throw new Error(`Unexpected type ${typeLength.type}.`);
34
+ return reader.readPrimitive(typeLength);
35
+ }
36
+ validate({ length }) {
37
+ if (length > this.maxLength)
38
+ throw new Error(`Array is too long: ${length}, max ${this.maxLength}.`);
39
+ if (length < this.minLength)
40
+ throw new Error(`Array is too short: ${length}, min ${this.minLength}.`);
41
+ }
42
+ bound({ minLength, maxLength, length }) {
43
+ return new StringSchema(this.type, length !== null && length !== void 0 ? length : (0, Number_js_1.maxValue)(this.minLength, minLength), length !== null && length !== void 0 ? length : (0, Number_js_1.minValue)(this.maxLength, maxLength));
44
+ }
45
+ }
46
+ exports.StringSchema = StringSchema;
47
+ /** ByteString TLV schema. */
48
+ exports.TlvByteString = new StringSchema(16 /* TlvType.ByteString */);
49
+ /** String TLV schema. */
50
+ exports.TlvString = new StringSchema(12 /* TlvType.Utf8String */);
51
+ /** String TLV schema. */
52
+ exports.TlvString32max = exports.TlvString.bound({ maxLength: 32 });
53
+ /** String TLV schema. */
54
+ exports.TlvString64max = exports.TlvString.bound({ maxLength: 64 });
55
+ /** String TLV schema. */
56
+ exports.TlvString256max = exports.TlvString.bound({ maxLength: 256 });
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvVoid = exports.VoidSchema = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ /**
11
+ * Schema to encode void.
12
+ */
13
+ class VoidSchema extends TlvSchema_js_1.TlvSchema {
14
+ encodeTlvInternal(_writer, _value, _tag) {
15
+ // Nothing to do
16
+ }
17
+ decodeTlv(_encoded) {
18
+ // Nothing to do
19
+ }
20
+ decodeTlvInternalValue(_reader, _typeLength) {
21
+ throw new Error("decodeTlvInternalValue should never be called");
22
+ }
23
+ }
24
+ exports.VoidSchema = VoidSchema;
25
+ /** Void TLV schema. */
26
+ exports.TlvVoid = new VoidSchema();
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.TlvWrapper = void 0;
9
+ const TlvSchema_js_1 = require("./TlvSchema.js");
10
+ class TlvWrapper extends TlvSchema_js_1.TlvSchema {
11
+ constructor(underlyingSchema, wrap, unwrap) {
12
+ super();
13
+ this.underlyingSchema = underlyingSchema;
14
+ this.wrap = wrap;
15
+ this.unwrap = unwrap;
16
+ }
17
+ decodeTlvInternalValue(reader, typeLength) {
18
+ return this.unwrap(this.underlyingSchema.decodeTlvInternalValue(reader, typeLength));
19
+ }
20
+ encodeTlvInternal(writer, value, tag) {
21
+ this.underlyingSchema.encodeTlvInternal(writer, this.wrap(value), tag);
22
+ }
23
+ validate(value) {
24
+ this.underlyingSchema.validate(this.wrap(value));
25
+ }
26
+ }
27
+ exports.TlvWrapper = TlvWrapper;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Endian = exports.ByteArray = void 0;
9
+ Uint8Array.prototype.toHex = function () {
10
+ return this.reduce((result, byte) => result + byte.toString(16).padStart(2, "0"), "");
11
+ };
12
+ Uint8Array.prototype.getDataView = function () {
13
+ return new DataView(this.buffer, this.byteOffset, this.byteLength);
14
+ };
15
+ Uint8Array.prototype.equals = function (other) {
16
+ if (other.length !== this.length)
17
+ return false;
18
+ return this.every((value, index) => other[index] === value);
19
+ };
20
+ Uint8Array.fromHex = function (hexString) {
21
+ var _a;
22
+ if (hexString.length === 0)
23
+ return new exports.ByteArray(0);
24
+ if (hexString.length % 2 !== 0)
25
+ throw new Error("Hex string should have an even length.");
26
+ const bytes = (_a = hexString.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map(byteHex => parseInt(byteHex, 16));
27
+ if (bytes === undefined)
28
+ throw new Error("Failed to parse the hex string.");
29
+ return exports.ByteArray.from(bytes);
30
+ };
31
+ Uint8Array.fromString = function (string) {
32
+ return new TextEncoder().encode(string);
33
+ };
34
+ Uint8Array.concat = function (...arrays) {
35
+ let length = 0;
36
+ arrays.forEach(array => length += array.length);
37
+ const result = new Uint8Array(length);
38
+ let offset = 0;
39
+ arrays.forEach(array => {
40
+ result.set(array, offset);
41
+ offset += array.length;
42
+ });
43
+ return result;
44
+ };
45
+ exports.ByteArray = Uint8Array;
46
+ var Endian;
47
+ (function (Endian) {
48
+ Endian[Endian["Little"] = 0] = "Little";
49
+ Endian[Endian["Big"] = 1] = "Big";
50
+ })(Endian = exports.Endian || (exports.Endian = {}));
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.DataReader = void 0;
9
+ const ByteArray_js_1 = require("./ByteArray.js");
10
+ /** Reader that auto-increments its offset after each read. */
11
+ class DataReader {
12
+ constructor(buffer, endian) {
13
+ this.buffer = buffer;
14
+ this.offset = 0;
15
+ this.dataView = buffer.getDataView();
16
+ this.littleEndian = endian === ByteArray_js_1.Endian.Little;
17
+ }
18
+ readUInt8() {
19
+ return this.dataView.getUint8(this.getOffsetAndAdvance(1));
20
+ }
21
+ readUInt16() {
22
+ return this.dataView.getUint16(this.getOffsetAndAdvance(2), this.littleEndian);
23
+ }
24
+ readUInt32() {
25
+ return this.dataView.getUint32(this.getOffsetAndAdvance(4), this.littleEndian);
26
+ }
27
+ readUInt64() {
28
+ return this.dataView.getBigUint64(this.getOffsetAndAdvance(8), this.littleEndian);
29
+ }
30
+ readInt8() {
31
+ return this.dataView.getInt8(this.getOffsetAndAdvance(1));
32
+ }
33
+ readInt16() {
34
+ return this.dataView.getInt16(this.getOffsetAndAdvance(2), this.littleEndian);
35
+ }
36
+ readInt32() {
37
+ return this.dataView.getInt32(this.getOffsetAndAdvance(4), this.littleEndian);
38
+ }
39
+ readInt64() {
40
+ return this.dataView.getBigInt64(this.getOffsetAndAdvance(8), this.littleEndian);
41
+ }
42
+ readFloat() {
43
+ return this.dataView.getFloat32(this.getOffsetAndAdvance(4), this.littleEndian);
44
+ }
45
+ readDouble() {
46
+ return this.dataView.getFloat64(this.getOffsetAndAdvance(8), this.littleEndian);
47
+ }
48
+ readUtf8String(length) {
49
+ const offset = this.getOffsetAndAdvance(length);
50
+ return new TextDecoder().decode(this.buffer.subarray(offset, offset + length));
51
+ }
52
+ readByteArray(length) {
53
+ const offset = this.getOffsetAndAdvance(length);
54
+ const result = this.buffer.subarray(offset, offset + length);
55
+ return result;
56
+ }
57
+ getRemainingBytesCount() {
58
+ return this.dataView.byteLength - this.offset;
59
+ }
60
+ getRemainingBytes() {
61
+ return this.buffer.subarray(this.offset);
62
+ }
63
+ getOffsetAndAdvance(size) {
64
+ const result = this.offset;
65
+ this.offset += size;
66
+ return result;
67
+ }
68
+ }
69
+ exports.DataReader = DataReader;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.DataWriter = void 0;
9
+ const ByteArray_js_1 = require("./ByteArray.js");
10
+ const Number_js_1 = require("./Number.js");
11
+ /** Writer that auto-increments its offset after each write. */
12
+ // TODO: some research should be done to make sure this is most performant implementation.
13
+ class DataWriter {
14
+ constructor(endian) {
15
+ this.length = 0;
16
+ this.chunks = new Array();
17
+ this.littleEndian = endian === ByteArray_js_1.Endian.Little;
18
+ }
19
+ writeUInt8(value) {
20
+ this.chunks.push(new ByteArray_js_1.ByteArray([(0, Number_js_1.toNumber)(value)]));
21
+ this.length += 1;
22
+ }
23
+ writeUInt16(value) {
24
+ const chunk = new ByteArray_js_1.ByteArray(2);
25
+ new DataView(chunk.buffer, 0, 2).setUint16(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
26
+ this.chunks.push(chunk);
27
+ this.length += 2;
28
+ }
29
+ writeUInt32(value) {
30
+ const chunk = new ByteArray_js_1.ByteArray(4);
31
+ new DataView(chunk.buffer, 0, 4).setUint32(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
32
+ this.chunks.push(chunk);
33
+ this.length += 4;
34
+ }
35
+ writeUInt64(value) {
36
+ const chunk = new ByteArray_js_1.ByteArray(8);
37
+ new DataView(chunk.buffer, 0, 8).setBigUint64(0, (0, Number_js_1.toBigInt)(value), this.littleEndian);
38
+ this.chunks.push(chunk);
39
+ this.length += 8;
40
+ }
41
+ writeInt8(value) {
42
+ const chunk = new ByteArray_js_1.ByteArray(1);
43
+ new DataView(chunk.buffer, 0, 1).setInt8(0, (0, Number_js_1.toNumber)(value));
44
+ this.chunks.push(chunk);
45
+ this.length += 1;
46
+ }
47
+ writeInt16(value) {
48
+ const chunk = new ByteArray_js_1.ByteArray(2);
49
+ new DataView(chunk.buffer, 0, 2).setInt16(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
50
+ this.chunks.push(chunk);
51
+ this.length += 2;
52
+ }
53
+ writeInt32(value) {
54
+ const chunk = new ByteArray_js_1.ByteArray(4);
55
+ new DataView(chunk.buffer, 0, 4).setInt32(0, (0, Number_js_1.toNumber)(value), this.littleEndian);
56
+ this.chunks.push(chunk);
57
+ this.length += 4;
58
+ }
59
+ writeInt64(value) {
60
+ const chunk = new ByteArray_js_1.ByteArray(8);
61
+ new DataView(chunk.buffer, 0, 8).setBigInt64(0, (0, Number_js_1.toBigInt)(value), this.littleEndian);
62
+ this.chunks.push(chunk);
63
+ this.length += 8;
64
+ }
65
+ writeFloat(value) {
66
+ const chunk = new ByteArray_js_1.ByteArray(4);
67
+ new DataView(chunk.buffer, 0, 4).setFloat32(0, value, this.littleEndian);
68
+ this.chunks.push(chunk);
69
+ this.length += 4;
70
+ }
71
+ writeDouble(value) {
72
+ const chunk = new ByteArray_js_1.ByteArray(8);
73
+ new DataView(chunk.buffer, 0, 8).setFloat64(0, value, this.littleEndian);
74
+ this.chunks.push(chunk);
75
+ this.length += 8;
76
+ }
77
+ writeUtf8String(value) {
78
+ const bytes = ByteArray_js_1.ByteArray.fromString(value);
79
+ this.chunks.push(bytes);
80
+ this.length += bytes.byteLength;
81
+ }
82
+ writeByteArray(value) {
83
+ this.chunks.push(value);
84
+ this.length += value.byteLength;
85
+ }
86
+ toByteArray() {
87
+ if (this.chunks.length === 0)
88
+ return new ByteArray_js_1.ByteArray(0);
89
+ if (this.chunks.length === 1)
90
+ return this.chunks[0];
91
+ const result = new ByteArray_js_1.ByteArray(this.length);
92
+ let offset = 0;
93
+ this.chunks.forEach(chunck => {
94
+ result.set(chunck, offset);
95
+ offset += chunck.byteLength;
96
+ });
97
+ this.chunks.length = 0;
98
+ this.chunks.push(result);
99
+ return result;
100
+ }
101
+ }
102
+ exports.DataWriter = DataWriter;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.maxValue = exports.minValue = exports.toBigInt = exports.toNumber = exports.FLOAT32_MAX = exports.FLOAT32_MIN = exports.INT64_MAX = exports.INT32_MAX = exports.INT16_MAX = exports.INT8_MAX = exports.INT64_MIN = exports.INT32_MIN = exports.INT16_MIN = exports.INT8_MIN = exports.UINT64_MAX = exports.UINT32_MAX = exports.UINT16_MAX = exports.UINT8_MAX = void 0;
9
+ exports.UINT8_MAX = 0xFF;
10
+ exports.UINT16_MAX = 0xFFFF;
11
+ exports.UINT32_MAX = 0xFFFFFFFF;
12
+ exports.UINT64_MAX = BigInt("18446744073709551615");
13
+ exports.INT8_MIN = -128;
14
+ exports.INT16_MIN = -32768;
15
+ exports.INT32_MIN = -2147483648;
16
+ exports.INT64_MIN = BigInt("-9223372036854775808");
17
+ exports.INT8_MAX = 127;
18
+ exports.INT16_MAX = 32767;
19
+ exports.INT32_MAX = 2147483647;
20
+ exports.INT64_MAX = BigInt("9223372036854775807");
21
+ exports.FLOAT32_MIN = -340282346638528859811704183484516925440.0;
22
+ exports.FLOAT32_MAX = 340282346638528859811704183484516925440.0;
23
+ function toNumber(value) {
24
+ return typeof value === "bigint" ? Number(value) : value;
25
+ }
26
+ exports.toNumber = toNumber;
27
+ function toBigInt(value) {
28
+ return typeof value === "number" ? BigInt(value) : value;
29
+ }
30
+ exports.toBigInt = toBigInt;
31
+ function minValue(a, b) {
32
+ if (a === undefined)
33
+ return b;
34
+ if (b === undefined)
35
+ return a;
36
+ return a < b ? a : b;
37
+ }
38
+ exports.minValue = minValue;
39
+ function maxValue(a, b) {
40
+ if (a === undefined)
41
+ return b;
42
+ if (b === undefined)
43
+ return a;
44
+ return a > b ? a : b;
45
+ }
46
+ exports.maxValue = maxValue;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ /**
3
+ * @license
4
+ * Copyright 2022 Project CHIP Authors
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.Merge = void 0;
9
+ const Merge = (a, b) => (Object.assign(Object.assign({}, a), b));
10
+ exports.Merge = Merge;