UNPKG

3.23 kBJavaScriptView Raw
1"use strict";
2/**
3 * this is the low-level implementation of the E1.31 (sACN) protocol
4 */
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Packet = void 0;
7const assert = require("assert");
8/* eslint-disable lines-between-class-members, no-bitwise, no-control-regex */
9const ACN_PID = Buffer.from([
10 0x41,
11 0x53,
12 0x43,
13 0x2d,
14 0x45,
15 0x31,
16 0x2e,
17 0x31,
18 0x37,
19 0x00,
20 0x00,
21 0x00,
22]);
23var RootVector;
24(function (RootVector) {
25 RootVector[RootVector["DATA"] = 4] = "DATA";
26 RootVector[RootVector["EXTENDED"] = 8] = "EXTENDED";
27})(RootVector || (RootVector = {}));
28var FrameVector;
29(function (FrameVector) {
30 FrameVector[FrameVector["DATA"] = 2] = "DATA";
31})(FrameVector || (FrameVector = {}));
32var DmpVector;
33(function (DmpVector) {
34 DmpVector[DmpVector["DATA"] = 2] = "DATA";
35})(DmpVector || (DmpVector = {}));
36/* eslint-disable camelcase */
37// enum ExtendedFrameVector { SYNC = 1, DISCOVERY = 2 }
38class Packet {
39 constructor(buffer, sourceAddress) {
40 this.buffer = buffer;
41 this.sourceAddress = sourceAddress;
42 /* root layer */
43 this.root_vector = this.buffer.readUInt32BE(18);
44 this.root_fl = this.buffer.readUInt16BE(16);
45 this.acnPid = this.buffer.slice(4, 16);
46 this.preambleSize = this.buffer.readUInt16BE(0);
47 this.postambleSize = this.buffer.readUInt16BE(2);
48 this.cid = this.buffer.slice(22, 38);
49 /* frame layer */
50 this.frame_vector = this.buffer.readUInt32BE(40);
51 this.frame_fl = this.buffer.readUInt16BE(38);
52 this.options = this.buffer.readUInt8(112);
53 this.sequence = this.buffer.readUInt8(111);
54 this.sourceName = this.buffer
55 .toString('ascii', 44, 107)
56 .replace(/\x00/g, '');
57 this.priority = this.buffer.readUInt8(108);
58 this.syncUniverse = this.buffer.readUInt16BE(109);
59 this.universe = this.buffer.readUInt16BE(113);
60 /* DMP layer */
61 this.dmp_vector = this.buffer.readUInt8(117);
62 this.dmp_fl = this.buffer.readUInt16BE(115);
63 this.type = this.buffer.readUInt8(118);
64 this.firstAddress = this.buffer.readUInt16BE(119);
65 this.addressIncrement = this.buffer.readUInt16BE(121);
66 this.propertyValueCount = this.buffer.readUInt16BE(123);
67 this.startCode = this.buffer.readUInt8(125);
68 this.slotsData = this.buffer.slice(126);
69 this.validate();
70 }
71 validate() {
72 // ascertains that this packet implements ACN
73 assert.deepStrictEqual(this.acnPid, ACN_PID);
74 // ascertains that this packet is a DATA packet
75 assert.strictEqual(this.root_vector, RootVector.DATA);
76 assert.strictEqual(this.frame_vector, FrameVector.DATA);
77 assert.strictEqual(this.dmp_vector, DmpVector.DATA);
78 // constants within the UDP overhead
79 assert.strictEqual(this.type, 0xa1); // = 61
80 assert.strictEqual(this.firstAddress, 0);
81 assert.strictEqual(this.addressIncrement, 1);
82 assert.strictEqual(this.startCode, 0);
83 assert.strictEqual(this.preambleSize, 0x0010); // = 16
84 assert.strictEqual(this.postambleSize, 0);
85 }
86}
87exports.Packet = Packet;