UNPKG

knxultimate

Version:

KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.

59 lines 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const buffer_1 = require("buffer"); const KNXConstants_1 = require("./KNXConstants"); const KnxLog_1 = require("../KnxLog"); const logger = (0, KnxLog_1.module)('KNXHeader'); class KNXHeader { constructor(type, length) { this._headerLength = KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10; this._version = KNXConstants_1.KNX_CONSTANTS.KNXNETIP_VERSION_10; this.service_type = type; this.length = KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10 + length; } get headerLength() { return this._headerLength; } get version() { return this._version; } static createFromBuffer(buffer, offset = 0) { if (buffer.length < KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10) { logger.error(`createFromBuffer: incomplete buffer. Buffer length: ${buffer.length} expected HEADER_SIZE_10 equals to ${KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10}`); throw new Error('Incomplete buffer'); } const header_length = buffer.readUInt8(offset); if (header_length !== KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10) { logger.error(`createFromBuffer: invalid header_length. header_length: ${header_length} expected HEADER_SIZE_10 equals to ${KNXConstants_1.KNX_CONSTANTS.HEADER_SIZE_10}`); throw new Error(`Invalid buffer length ${header_length}`); } offset += 1; const version = buffer.readUInt8(offset); if (version !== KNXConstants_1.KNX_CONSTANTS.KNXNETIP_VERSION_10) { logger.error(`createFromBuffer: Unknown header version. Version: ${version} expected KNXNETIP_VERSION_10 to ${KNXConstants_1.KNX_CONSTANTS.KNXNETIP_VERSION_10}`); throw new Error(`Unknown version ${version}`); } offset += 1; const type = buffer.readUInt16BE(offset); offset += 2; const length = buffer.readUInt16BE(offset); if (length !== buffer.length) { logger.error(`Received KNX packet: createFromBuffer: Message length mismatch ${length}/${buffer.length} Data processed: ${buffer.toString('hex') || '??'}`); } return new KNXHeader(type, length - header_length); } toBuffer() { const buffer = buffer_1.Buffer.alloc(this._headerLength); let offset = 0; buffer.writeUInt8(this._headerLength, offset); offset += 1; buffer.writeUInt8(this._version, offset); offset += 1; buffer.writeUInt16BE(this.service_type, offset); offset += 2; buffer.writeUInt16BE(this.length, offset); return buffer; } } exports.default = KNXHeader; //# sourceMappingURL=KNXHeader.js.map