enip-ts
Version:
Typescript implementation of the Ethernet/IP™ protocol.
87 lines • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EPathData = exports.EPathDataElementTypes = exports.EPathDataTypes = void 0;
var EPathDataTypes;
(function (EPathDataTypes) {
EPathDataTypes[EPathDataTypes["Simple"] = 128] = "Simple";
EPathDataTypes[EPathDataTypes["ANSI_EXTD"] = 145] = "ANSI_EXTD";
})(EPathDataTypes || (exports.EPathDataTypes = EPathDataTypes = {}));
;
var EPathDataElementTypes;
(function (EPathDataElementTypes) {
EPathDataElementTypes[EPathDataElementTypes["UINT8"] = 40] = "UINT8";
EPathDataElementTypes[EPathDataElementTypes["UINT16"] = 41] = "UINT16";
EPathDataElementTypes[EPathDataElementTypes["UINT32"] = 42] = "UINT32";
})(EPathDataElementTypes || (exports.EPathDataElementTypes = EPathDataElementTypes = {}));
;
class EPathData {
/**
* Builds EPATH Data Segment
* @param data
* @param ANSI Declare if ANSI Extended or Simple, default to true
* @returns
*/
static build(data, ANSI = true) {
// Build Element Segment If Int
if (typeof data === 'string')
return this.elementBuild(parseInt(data));
// Build symbolic segment by default
return this.symbolicBuild(data, ANSI);
}
;
/**
* Builds EPATH Symbolic Segment
* @param data
* @param [ANSI=true] Declare if ANSI Extended or Simple
* @returns build symbolic segment buffer
*/
static symbolicBuild(data, ANSI = true) {
// Initialize Buffer
let buf = Buffer.alloc(2);
// Write Appropriate Segment Byte
buf.writeUInt8(ANSI ? EPathDataTypes.ANSI_EXTD : EPathDataTypes.Simple, 0);
// Write Appropriate Length
buf.writeUInt8(ANSI ? data.length : Math.ceil(data.length / 2), 1);
// Append Data
buf = Buffer.concat([buf, Buffer.from(data)]);
// Add Pad Byte if Odd Length
if (buf.length % 2 === 1)
buf = Buffer.concat([buf, Buffer.alloc(1)]); // Pad Odd Length Strings
return buf;
}
;
/**
* Builds EPATH Element Segment
* @param data
* @returns build Element segment buffer
*/
static elementBuild(data) {
// Get Element Length - Data Access 2 - IOI Segments - Element Segments
let type;
let dataBuf;
if (data < 256) {
type = EPathDataElementTypes.UINT8; // UNIT8 x28 xx
dataBuf = Buffer.alloc(1);
dataBuf.writeUInt8(data);
}
else if (data < 65536) {
type = EPathDataElementTypes.UINT16; // UINT16 x29 00 xx xx
dataBuf = Buffer.alloc(3);
dataBuf.writeUInt16LE(data, 1);
}
else {
type = EPathDataElementTypes.UINT32; // UINT32 x2a 00 xx xx xx xx
dataBuf = Buffer.alloc(5);
dataBuf.writeUInt32LE(data, 1);
}
// Initialize Buffer
let buf = Buffer.alloc(1);
// Write Appropriate Segment Byte
buf.writeUInt8(type, 0);
// Append Data
buf = Buffer.concat([buf, dataBuf]);
return buf;
}
}
exports.EPathData = EPathData;
//# sourceMappingURL=data.js.map