-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
97 lines (97 loc) · 4.22 KB
/
index.js
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
"use strict";
exports.__esModule = true;
var crc = require("crc");
var EventEmitter = require("events").EventEmitter;
var FRAME_BOUNDARY_OCTET = 0x7E;
var CONTROL_ESCAPE_OCTET = 0x7D;
var INVERT_OCTET = 0x20;
var MINIHDLC_MAX_FRAME_LENGTH = 4096;
var HDLC = /** @class */ (function () {
function HDLC() {
this.pendingFrame = {};
this.eventEmitter = new EventEmitter();
this.byteSendingFunction = function (byte) { };
}
HDLC.prototype.HDLC = function () { };
HDLC.prototype.init = function (byteSendingFunction) {
this.byteSendingFunction = byteSendingFunction;
this.pendingFrame.framePosition = 0;
this.pendingFrame.frameChecksum = undefined;
this.pendingFrame.escapeCharacter = false;
this.pendingFrame.receivedFrameBuffer = [];
this.pendingFrame.isStared = false;
};
HDLC.prototype.sendchar = function (data) {
this.byteSendingFunction(data & 0xff);
};
HDLC.prototype.byteReceiver = function (bytes) {
for (var i = 0; i < bytes.length; i++) {
/* FRAME FLAG */
var data = bytes[i];
/* FRAME FLAG */
if (data === FRAME_BOUNDARY_OCTET) {
if (this.pendingFrame.escapeCharacter === true) {
this.pendingFrame.escapeCharacter = false;
}
else if ((this.pendingFrame.framePosition >= 2) &&
(this.pendingFrame.frameChecksum === ((this.pendingFrame.receivedFrameBuffer[this.pendingFrame.framePosition - 1] << 8) | (this.pendingFrame.receivedFrameBuffer[this.pendingFrame.framePosition - 2] & 0xff)))) {
/* Call the user defined function and pass frame to it */
this.eventEmitter.emit("newFrame", this.pendingFrame.receivedFrameBuffer.slice(0, this.pendingFrame.receivedFrameBuffer.length - 2));
}
this.pendingFrame.framePosition = 0;
this.pendingFrame.frameChecksum = undefined;
this.pendingFrame.escapeCharacter = false;
this.pendingFrame.receivedFrameBuffer = [];
continue;
}
if (this.pendingFrame.escapeCharacter) {
this.pendingFrame.escapeCharacter = false;
data ^= INVERT_OCTET;
}
else if (data === CONTROL_ESCAPE_OCTET) {
this.pendingFrame.escapeCharacter = true;
continue;
}
this.pendingFrame.receivedFrameBuffer[this.pendingFrame.framePosition] = data;
if (this.pendingFrame.framePosition - 2 >= 0) {
this.pendingFrame.frameChecksum = crc.crc16ccitt([this.pendingFrame.receivedFrameBuffer[this.pendingFrame.framePosition - 2]], this.pendingFrame.frameChecksum);
}
this.pendingFrame.framePosition++;
if (this.pendingFrame.framePosition === MINIHDLC_MAX_FRAME_LENGTH) {
this.pendingFrame.framePosition = 0;
this.pendingFrame.frameChecksum = undefined;
this.pendingFrame.escapeCharacter = false;
this.pendingFrame.receivedFrameBuffer = [];
}
}
};
HDLC.prototype.sendFrame = function (rawFrame) {
var byte;
var fcs;
this.sendchar(FRAME_BOUNDARY_OCTET);
for (var i = 0; i < rawFrame.length; i++) {
byte = rawFrame[i];
fcs = crc.crc16ccitt([byte], fcs);
if ((byte === CONTROL_ESCAPE_OCTET) || (byte === FRAME_BOUNDARY_OCTET)) {
this.sendchar(CONTROL_ESCAPE_OCTET);
byte ^= INVERT_OCTET;
}
this.sendchar(byte);
}
byte = Buffer.from([fcs]).readInt8(0);
if ((byte === CONTROL_ESCAPE_OCTET) || (byte === FRAME_BOUNDARY_OCTET)) {
this.sendchar(CONTROL_ESCAPE_OCTET);
byte ^= INVERT_OCTET;
}
this.sendchar(byte);
byte = Buffer.from([fcs >> 8]).readInt8(0);
if ((byte === CONTROL_ESCAPE_OCTET) || (byte === FRAME_BOUNDARY_OCTET)) {
this.sendchar(CONTROL_ESCAPE_OCTET);
byte ^= INVERT_OCTET;
}
this.sendchar(byte);
this.sendchar(FRAME_BOUNDARY_OCTET);
};
return HDLC;
}());
exports.HDLC = HDLC;