From 62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e Mon Sep 17 00:00:00 2001 From: Ernesto Castellotti Date: Thu, 23 Mar 2023 20:53:46 +0100 Subject: Tool: GPON OMCI VLAN Table parser (#164) * Tool: GPON OMCI VLAN Table parser --- assets/js/omci-vlan.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 assets/js/omci-vlan.js (limited to 'assets/js') diff --git a/assets/js/omci-vlan.js b/assets/js/omci-vlan.js new file mode 100644 index 0000000..e28642c --- /dev/null +++ b/assets/js/omci-vlan.js @@ -0,0 +1,60 @@ +/* Copyright (C) Ernesto Castellotti + * SPDX-License-Identifier: MPL-2.0-no-copyleft-exception +*/ + +// Convert hex string to binary representation +function hex2bin(hexString){ + var result = ""; + hexString = hexString.replace(/0x/g, "").replace(/ /g, '').toLowerCase(); + + for(const hexChr of hexString) { + const bin = parseInt(hexChr, 16).toString(2).padStart(4, '0'); + + if (!bin.isNaN) { + result += bin; + } else { + throw new Error("The hex string is not valid!"); + } + } + + return result; +} + +// Helper function to splice binary array and convert to decimal +function binSpliceToDec(binArray, len) { + const result = binArray.splice(0, len); + return parseInt(result.join(""), 2); +} + +// GPON OMCI VLAN Table parser +function vlanTableParse(hexString) { + const binaryArray = hex2bin(hexString).split(''); + var result = new Array(); + + while(binaryArray.length > 0) { + var vlanRule = new Array(); + + vlanRule.push(binSpliceToDec(binaryArray, 4)); + vlanRule.push(binSpliceToDec(binaryArray, 13)); + vlanRule.push(binSpliceToDec(binaryArray, 3)); + binaryArray.splice(0, 12); // Padding + vlanRule.push( binSpliceToDec(binaryArray, 4)); + vlanRule.push(binSpliceToDec(binaryArray, 13)); + vlanRule.push(binSpliceToDec(binaryArray, 3)); + binaryArray.splice(0, 8); // Padding + vlanRule.push(binSpliceToDec(binaryArray, 4)); + vlanRule.push(binSpliceToDec(binaryArray, 2)); + binaryArray.splice(0, 10); // Padding + vlanRule.push(binSpliceToDec(binaryArray, 4)); + vlanRule.push(binSpliceToDec(binaryArray, 13)); + vlanRule.push(binSpliceToDec(binaryArray, 3)); + binaryArray.splice(0, 12); // Padding + vlanRule.push(binSpliceToDec(binaryArray, 4)); + vlanRule.push(binSpliceToDec(binaryArray, 13)); + vlanRule.push(binSpliceToDec(binaryArray, 3)); + + result.push(vlanRule); + } + + return result; +} -- cgit v1.2.3