diff options
-rw-r--r-- | _tools/gpon-omci-vlan-parser.md | 79 | ||||
-rw-r--r-- | assets/js/omci-vlan.js | 60 |
2 files changed, 139 insertions, 0 deletions
diff --git a/_tools/gpon-omci-vlan-parser.md b/_tools/gpon-omci-vlan-parser.md new file mode 100644 index 0000000..77c5b6b --- /dev/null +++ b/_tools/gpon-omci-vlan-parser.md @@ -0,0 +1,79 @@ +--- +title: GPON OMCI VLAN Table parser +has_children: false +description: Tool for parse the GPON OMCI VLAN Table (ME 171) +layout: default +--- + +<h1>VLAN TABLE PARSER</h1> +<form id="vlan-table-parser" novalidate> + <div class="form-floating mb-3"> + <input type="text" class="form-control" placeholder="VLAN TABLE in HEX" name="vlan-table-hex" id="vlan-table-hex" required> + <label for="vlan-table-hex">VLAN TABLE in HEX</label> + <div class="invalid-feedback"> + Please provide a valid input text. + </div> + </div> + <div class="mb-3"> + <input type="submit" class="btn btn-primary" value="Decode!"> + </div> + <div class="form-floating mb-3" id="to-place-table"></div> +</form> + +<script type="text/javascript" src="/assets/js/omci-vlan.js"></script> +<script> + function fillVlanTable(table, vlanTableRule) { + const tableNames = ["Filter outer priority", + "Filter outer VID", + "Filter outer TPID", + "Filter inner priority", + "Filter inner VID", + "Filter inner TPID", + "Filter ether type", + "Treatment tags to remove", + "Treatment outer priority", + "Treatment outer VID", + "Treatment outer TPID", + "Treatment inner priority", + "Treatment inner VID", + "Treatment inner TPID"]; + + + for (var j = 0; j < 14; j++) { + var row = table.insertRow(j); + var cell1 = row.insertCell(0); + var cell2 = row.insertCell(1); + + cell1.innerHTML = tableNames[j]; + cell2.innerHTML = vlanTableRule[j]; + } + } + + function makeVlanTables(formToPlaceTable, vlanTable) { + for (const vlanRule of vlanTable) { + var div = document.createElement('div'); + div.classList = "table-wrapper"; + var table = document.createElement('table'); + div.appendChild(table); + fillVlanTable(table, vlanRule); + formToPlaceTable.appendChild(div); + } + } + + var vlanTableForm = document.getElementById('vlan-table-parser'); + vlanTableForm.addEventListener('submit',(event) => { + if (!vlanTableForm.checkValidity()) { + event.preventDefault(); + [...vlanTableForm.elements].map(e => e.parentNode).forEach(e => e.classList.toggle('was-validated', true)); + } else { + event.preventDefault(); + [...vlanTableForm.elements].map(e => e.parentNode).forEach(e => e.classList.toggle('was-validated', false)); + var fomrdata = new FormData(vlanTableForm); + const hexString = fomrdata.get('vlan-table-hex'); + const vlanTable = vlanTableParse(hexString); + const formToPlaceTable = document.getElementById('to-place-table'); + formToPlaceTable.innerHTML = ''; + makeVlanTables(formToPlaceTable, vlanTable); + } + }); +</script> 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; +} |