summaryrefslogtreecommitdiffstats
path: root/assets/js
diff options
context:
space:
mode:
authorErnesto Castellotti <mail@ernestocastellotti.it>2023-03-23 20:53:46 +0100
committerGitHub <noreply@github.com>2023-03-23 20:53:46 +0100
commit62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e (patch)
tree610a3368eb96ceedd2ded2ce609c862fc7c83b0a /assets/js
parentUpdate ont-cig-g-97cp.md (#166) (diff)
downloadhack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar.gz
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar.bz2
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar.lz
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar.xz
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.tar.zst
hack-gpon.github.io-62a3ffc3084d8aca2ec7f21f6c75ce13bca7129e.zip
Diffstat (limited to 'assets/js')
-rw-r--r--assets/js/omci-vlan.js60
1 files changed, 60 insertions, 0 deletions
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 <mail@ernestocastellotti.it>
+ * 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;
+}