class Utility { /** * This method calculates the CRC-8-AUTOSAR checksum of the given * array of bytes. * * This code is based on the code examples from the following page: * http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html * * The specifics of the algorithm is coming from the AUTOSAR CRC specification: * https://www.autosar.org/fileadmin/user_upload/standards/classic/21-11/AUTOSAR_SWS_CRCLibrary.pdf * * > Utility.crc8([0x0, 0x0, 0x0, 0x0]).toString(16) * "12" * > Utility.crc8([0xF2, 0x01, 0x83]).toString(16) * "c2" * > Utility.crc8([0x0F, 0xAA, 0x00, 0x55]).toString(16) * "c6" * > Utility.crc8([0x00, 0xff, 0x55, 0x11]).toString(16) * "77" * > Utility.crc8([0x33, 0x22, 0x55, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]).toString(16) * "11" * > Utility.crc8([0x92, 0x6b, 0x55]).toString(16) * "33" * > Utility.crc8([0xff, 0xff, 0xff, 0xff]).toString(16) * "6c" * * @param {Number[]} bytes * @return {Number} */ static crc8Autosar(bytes) { const polynomial = 0x2f; const xorOut = 0xff; let crc = 0xff; for (const byte of bytes) { crc ^= byte; for (let i = 0; i < 8; i++) { if ((crc & 0x80) != 0) { crc = ((crc << 1) ^ polynomial) & 0xff; } else { crc <<= 1; } } } return crc ^ xorOut; } /** * Take a bit array and encode the contents using manchester encoding. * * @param {Number[]} bitArray * @return {Number[]} */ static manchesterEncode(bitArray) { const encodedBits = []; for (const bit of bitArray) { encodedBits.push(bit ^ 1); encodedBits.push(bit ^ 0); } return encodedBits; } /** * Set the information message on the page to the given string. * @param {String} text */ static setMessage(text) { const message = document.getElementById("message"); message.textContent = text; } }