forked from BiancoRoyal/node-bacstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcov-notify.js
More file actions
110 lines (107 loc) · 4.58 KB
/
Copy pathcov-notify.js
File metadata and controls
110 lines (107 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const baAsn1 = require('../asn1');
const baEnum = require('../enum');
module.exports.encode = (buffer, subscriberProcessId, initiatingDeviceId, monitoredObjectId, timeRemaining, values) => {
baAsn1.encodeContextUnsigned(buffer, 0, subscriberProcessId);
baAsn1.encodeContextObjectId(buffer, 1, baEnum.ObjectType.DEVICE, initiatingDeviceId);
baAsn1.encodeContextObjectId(buffer, 2, monitoredObjectId.type, monitoredObjectId.instance);
baAsn1.encodeContextUnsigned(buffer, 3, timeRemaining);
baAsn1.encodeOpeningTag(buffer, 4);
values.forEach((value) => {
baAsn1.encodeContextEnumerated(buffer, 0, value.property.id);
if (value.property.index === baEnum.ASN1_ARRAY_ALL) {
baAsn1.encodeContextUnsigned(buffer, 1, value.property.index);
}
baAsn1.encodeOpeningTag(buffer, 2);
value.value.forEach((v) => {
baAsn1.bacappEncodeApplicationData(buffer, v);
});
baAsn1.encodeClosingTag(buffer, 2);
if (value.priority === baEnum.ASN1_NO_PRIORITY) {
baAsn1.encodeContextUnsigned(buffer, 3, value.priority);
}
// TODO: Handle to too large telegrams -> APDU limit
});
baAsn1.encodeClosingTag(buffer, 4);
};
module.exports.decode = (buffer, offset, apduLen) => {
let len = 0;
let result;
let decodedValue;
if (!baAsn1.decodeIsContextTag(buffer, offset + len, 0)) return;
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeUnsigned(buffer, offset + len, result.value);
len += decodedValue.len;
const subscriberProcessId = decodedValue.value;
if (!baAsn1.decodeIsContextTag(buffer, offset + len, 1)) return;
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeObjectId(buffer, offset + len);
len += decodedValue.len;
const initiatingDeviceId = {type: decodedValue.objectType, instance: decodedValue.instance};
if (!baAsn1.decodeIsContextTag(buffer, offset + len, 2)) return;
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeObjectId(buffer, offset + len);
len += decodedValue.len;
const monitoredObjectId = {type: decodedValue.objectType, instance: decodedValue.instance};
if (!baAsn1.decodeIsContextTag(buffer, offset + len, 3)) return;
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeUnsigned(buffer, offset + len, result.value);
len += decodedValue.len;
const timeRemaining = decodedValue.value;
if (!baAsn1.decodeIsOpeningTagNumber(buffer, offset + len, 4)) return;
len++;
const values = [];
while ((apduLen - len) > 1 && !baAsn1.decodeIsClosingTagNumber(buffer, offset + len, 4)) {
let newEntry = {};
newEntry.property = {};
if (!baAsn1.decodeIsContextTag(buffer, offset + len, 0)) return;
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeEnumerated(buffer, offset + len, result.value);
len += decodedValue.len;
newEntry.property.id = decodedValue.value;
if (baAsn1.decodeIsContextTag(buffer, offset + len, 1)) {
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeUnsigned(buffer, offset + len, result.value);
len += decodedValue.len;
newEntry.property.index = decodedValue.value;
} else {
newEntry.property.index = baEnum.ASN1_ARRAY_ALL;
}
if (!baAsn1.decodeIsOpeningTagNumber(buffer, offset + len, 2)) return;
len++;
const properties = [];
while ((apduLen - len) > 1 && !baAsn1.decodeIsClosingTagNumber(buffer, offset + len, 2)) {
decodedValue = baAsn1.bacappDecodeApplicationData(buffer, offset + len, apduLen + offset, monitoredObjectId.type, newEntry.property.id);
if (!decodedValue) return;
len += decodedValue.len;
delete decodedValue.len;
properties.push(decodedValue);
}
newEntry.value = properties;
len++;
if (baAsn1.decodeIsContextTag(buffer, offset + len, 3)) {
result = baAsn1.decodeTagNumberAndValue(buffer, offset + len);
len += result.len;
decodedValue = baAsn1.decodeUnsigned(buffer, offset + len, result.value);
len += decodedValue.len;
newEntry.priority = decodedValue.value;
} else {
newEntry.priority = baEnum.ASN1_NO_PRIORITY;
}
values.push(newEntry);
}
return {
len: len,
subscriberProcessId: subscriberProcessId,
initiatingDeviceId: initiatingDeviceId,
monitoredObjectId: monitoredObjectId,
timeRemaining: timeRemaining,
values: values
};
};