def test_configure_crc_parameters(self): crc_label = ChecksumLabel( "crc_label", 25, 120, 0, FieldType("crc", FieldType.Function.CHECKSUM)) crc_widget_controller = ChecksumWidgetController( crc_label, Message([0] * 150, 0, MessageType("test")), 0) crc = GenericCRC( polynomial=list(GenericCRC.DEFAULT_POLYNOMIALS.keys())[0]) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) self.assertEqual(crc_widget_controller.ui.lineEditStartValue.text(), util.bit2hex(crc.start_value)) self.assertEqual(crc_widget_controller.ui.lineEditFinalXOR.text(), util.bit2hex(crc.final_xor)) crc_widget_controller.ui.comboBoxCRCFunction.setCurrentIndex(2) crc.polynomial = crc.choose_polynomial(2) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) crc_widget_controller.ui.lineEditCRCPolynomial.setText("abcde") crc_widget_controller.ui.lineEditCRCPolynomial.editingFinished.emit() self.assertEqual(crc_label.checksum.polynomial, array.array("B", [1]) + util.hex2bit("abcde")) crc_widget_controller.ui.lineEditStartValue.setText("12345") crc_widget_controller.ui.lineEditStartValue.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.start_value), "12345") crc_widget_controller.ui.lineEditFinalXOR.setText("cccaa") crc_widget_controller.ui.lineEditFinalXOR.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.final_xor), "cccaa")
def test_configure_crc_parameters(self): crc_label = ChecksumLabel("crc_label", 25, 120, 0, FieldType("crc", FieldType.Function.CHECKSUM)) crc_widget_controller = ChecksumWidgetController(crc_label, Message([0] * 150, 0, MessageType("test")), 0) crc = GenericCRC(polynomial=list(GenericCRC.DEFAULT_POLYNOMIALS.keys())[0]) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) self.assertEqual(crc_widget_controller.ui.lineEditStartValue.text(), util.bit2hex(crc.start_value)) self.assertEqual(crc_widget_controller.ui.lineEditFinalXOR.text(), util.bit2hex(crc.final_xor)) crc_widget_controller.ui.comboBoxCRCFunction.setCurrentIndex(2) crc.polynomial = crc.choose_polynomial(2) self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(), crc.polynomial_as_hex_str) crc_widget_controller.ui.lineEditCRCPolynomial.setText("abcde") crc_widget_controller.ui.lineEditCRCPolynomial.editingFinished.emit() self.assertEqual(crc_label.checksum.polynomial, array.array("B", [1]) + util.hex2bit("abcde")) crc_widget_controller.ui.lineEditStartValue.setText("12345") crc_widget_controller.ui.lineEditStartValue.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.start_value), "12345") crc_widget_controller.ui.lineEditFinalXOR.setText("cccaa") crc_widget_controller.ui.lineEditFinalXOR.editingFinished.emit() self.assertEqual(util.bit2hex(crc_label.checksum.final_xor), "cccaa")
def export_message_type_to_latex(message_type, f): f.write(" \\begin{itemize}\n") for lbl in message_type: # type: ProtocolLabel if lbl.field_type.function == FieldType.Function.SYNC: sync = array( "B", map(int, self.syncs_by_message_type[message_type])) f.write(" \\item {}: \\texttt{{0x{}}}\n".format( lbl.name, util.bit2hex(sync))) elif lbl.field_type.function == FieldType.Function.PREAMBLE: preamble = array( "B", map(int, self.preambles_by_message_type[message_type])) f.write(" \\item {}: \\texttt{{0x{}}}\n".format( lbl.name, util.bit2hex(preamble))) elif lbl.field_type.function == FieldType.Function.CHECKSUM: f.write(" \\item {}: {}\n".format( lbl.name, lbl.checksum.caption)) elif lbl.field_type.function in ( FieldType.Function.LENGTH, FieldType.Function.SEQUENCE_NUMBER) and lbl.length > 8: f.write(" \\item {}: {} bit (\\textbf{{{} endian}})\n". format(lbl.name, lbl.length, "little" if self.little_endian else "big")) elif lbl.field_type.function == FieldType.Function.DATA: f.write(" \\item payload: {} byte\n".format( lbl.length // 8)) else: f.write(" \\item {}: {} bit\n".format( lbl.name, lbl.length)) f.write(" \\end{itemize}\n")
def test_reverse_engineering(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "1110001111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110011011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010" ] bitset = [] crcset = [] for i in bitstring_set: tmp = c.str2bit(i) bitset.append(tmp) crcset.append(c.crc(tmp)) polynomial = c.reverse_engineer_polynomial(bitset, crcset) if polynomial: self.assertEqual(c.bit2str(polynomial), "1000000000000101") self.assertEqual(util.bit2hex(polynomial), "8005")
def test_crc(self): # http://depa.usst.edu.cn/chenjq/www2/software/crc/CRC_Javascript/CRCcalculation.htm # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # CRC-16-CCITT: polynomial="16_ccitt", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # http://www.lammertbies.nl/comm/info/crc-calculation.html <- Fehler # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False c = GenericCRC(polynomial=WSPChecksum.CRC_8_POLYNOMIAL) e = Encoding() bitstr = [ "010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101" ] expected = ["78", "c9", "f2"] for value, expect in zip(bitstr, expected): nv = "" for i in range(0, len(value)): if value[i] == "1": nv += "0" else: nv += "1" self.assertEqual(util.bit2hex(c.crc(e.str2bit(value[4:-8]))), expect)
def on_combobox_crc_function_current_index_changed(self, index: int): poly_str = self.ui.comboBoxCRCFunction.itemText(index) if poly_str in GenericCRC.DEFAULT_POLYNOMIALS: self.checksum_label.checksum.polynomial = self.checksum_label.checksum.choose_polynomial(poly_str) self.checksum_label.checksum.start_value = array.array("B", [0] * (self.checksum_label.checksum.poly_order - 1)) self.checksum_label.checksum.final_xor = array.array("B", [0] * (self.checksum_label.checksum.poly_order - 1)) elif poly_str in self.SPECIAL_CRCS: self.checksum_label.checksum = copy.deepcopy(self.SPECIAL_CRCS[poly_str]) else: logger.error("Unknown CRC") return self.ui.lineEditCRCPolynomial.setText(self.checksum_label.checksum.polynomial_as_hex_str) self.ui.lineEditStartValue.setText(util.bit2hex(self.checksum_label.checksum.start_value)) self.ui.lineEditFinalXOR.setText(util.bit2hex(self.checksum_label.checksum.final_xor)) self.ui.lineEditCRCPolynomial.editingFinished.emit()
def on_line_edit_final_xor_editing_finished(self): crc = self.checksum_label.checksum final_xor = util.hex2bit(self.ui.lineEditFinalXOR.text()) final_xor = array.array("B", [0] * (crc.poly_order - 1 - len(final_xor))) + final_xor crc.final_xor = final_xor[0:crc.poly_order-1] self.ui.lineEditFinalXOR.setText(util.bit2hex(crc.final_xor)) self.__set_crc_info_label()
def set_checksum_ui_elements(self): if self.checksum_label.is_generic_crc: self.ui.lineEditCRCPolynomial.setText(self.checksum_label.checksum.polynomial_as_hex_str) self.ui.lineEditStartValue.setText(util.bit2hex(self.checksum_label.checksum.start_value)) self.ui.lineEditFinalXOR.setText(util.bit2hex(self.checksum_label.checksum.final_xor)) self.__ensure_same_length() self.__set_crc_info_label() elif self.checksum_label.category == self.checksum_label.Category.wsp: if self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.auto: self.ui.radioButtonWSPAuto.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.checksum4: self.ui.radioButtonWSPChecksum4.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.checksum8: self.ui.radioButtonWSPChecksum8.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.crc8: self.ui.radioButtonWSPCRC8.setChecked(True)
def on_combobox_crc_function_current_index_changed(self, index: int): poly_str = self.ui.comboBoxCRCFunction.itemText(index) if poly_str in GenericCRC.DEFAULT_POLYNOMIALS: self.checksum_label.checksum.polynomial = self.checksum_label.checksum.choose_polynomial(poly_str) self.checksum_label.checksum.start_value = array.array("B", [0] * (self.checksum_label.checksum.poly_order - 1)) self.checksum_label.checksum.final_xor = array.array("B", [0] * (self.checksum_label.checksum.poly_order - 1)) elif poly_str in self.SPECIAL_CRCS: self.checksum_label.checksum = copy.deepcopy(self.SPECIAL_CRCS[poly_str]) else: logger.error("Unknown CRC") return self.ui.lineEditCRCPolynomial.setText(self.checksum_label.checksum.polynomial_as_hex_str) self.ui.lineEditStartValue.setText(util.bit2hex(self.checksum_label.checksum.start_value)) self.ui.lineEditFinalXOR.setText(util.bit2hex(self.checksum_label.checksum.final_xor)) self.ui.lineEditCRCPolynomial.editingFinished.emit()
def test_crc(self): # http://depa.usst.edu.cn/chenjq/www2/software/crc/CRC_Javascript/CRCcalculation.htm # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # CRC-16-CCITT: polynomial="16_ccitt", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # http://www.lammertbies.nl/comm/info/crc-calculation.html <- Fehler # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False c = GenericCRC(polynomial=WSPChecksum.CRC_8_POLYNOMIAL) e = Encoding() bitstr = ["010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101"] expected = ["78", "c9", "f2"] for value, expect in zip(bitstr, expected): nv = "" for i in range(0, len(value)): if value[i] == "1": nv += "0" else: nv += "1" self.assertEqual(util.bit2hex(c.crc(e.str2bit(value[4:-8]))), expect)
def on_line_edit_final_xor_editing_finished(self): crc = self.checksum_label.checksum final_xor = util.hex2bit(self.ui.lineEditFinalXOR.text()) final_xor = array.array( "B", [0] * (crc.poly_order - 1 - len(final_xor))) + final_xor crc.final_xor = final_xor[0:crc.poly_order - 1] self.ui.lineEditFinalXOR.setText(util.bit2hex(crc.final_xor)) self.__set_crc_info_label()
def set_checksum_ui_elements(self): if self.checksum_label.is_generic_crc: self.ui.lineEditCRCPolynomial.setText(self.checksum_label.checksum.polynomial_as_hex_str) self.ui.lineEditStartValue.setText(util.bit2hex(self.checksum_label.checksum.start_value)) self.ui.lineEditFinalXOR.setText(util.bit2hex(self.checksum_label.checksum.final_xor)) self.__set_crc_function_index() self.__ensure_same_length() self.__set_crc_info_label() elif self.checksum_label.category == self.checksum_label.Category.wsp: if self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.auto: self.ui.radioButtonWSPAuto.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.checksum4: self.ui.radioButtonWSPChecksum4.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.checksum8: self.ui.radioButtonWSPChecksum8.setChecked(True) elif self.checksum_label.checksum.mode == WSPChecksum.ChecksumMode.crc8: self.ui.radioButtonWSPCRC8.setChecked(True)
def on_line_edit_start_value_editing_finished(self): crc = self.checksum_label.checksum start_value = util.hex2bit(self.ui.lineEditStartValue.text()) # pad with zeros at front start_value = array.array("B", [0]*(crc.poly_order - 1 - len(start_value))) + start_value crc.start_value = start_value[0:crc.poly_order-1] self.ui.lineEditStartValue.setText(util.bit2hex(crc.start_value)) self.__set_crc_info_label()
def test_crc8(self): messages = ["aabbcc", "abcdee", "dacafe"] expected = ["7d", "24", "33"] crc = GenericCRC(polynomial=GenericCRC.DEFAULT_POLYNOMIALS["8_ccitt"]) for msg, expect in zip(messages, expected): bits = util.hex2bit(msg) self.assertEqual(util.bit2hex(crc.crc(bits)), expect)
def on_line_edit_start_value_editing_finished(self): crc = self.checksum_label.checksum start_value = util.hex2bit(self.ui.lineEditStartValue.text()) # pad with zeros at front start_value = array.array( "B", [0] * (crc.poly_order - 1 - len(start_value))) + start_value crc.start_value = start_value[0:crc.poly_order - 1] self.ui.lineEditStartValue.setText(util.bit2hex(crc.start_value)) self.__set_crc_info_label()
def test_enocean_crc8_message(self): e = Encoding() received = util.hex2bit("aacbac4cddd5ddd3bddd5ddcc5ddcddd4c2d5d5c2cdddab200000") preamble, sof, eof = "aa", "9", "b" decoded, err, state = e.code_enocean(decoding=True, inpt=received) self.assertEqual(err, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertIn(preamble, util.bit2hex(decoded)) self.assertIn(sof, util.bit2hex(decoded)) self.assertIn(eof, util.bit2hex(decoded)) reencoded, errors, state = e.code_enocean(decoding=False, inpt=decoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) redecoded, errors, state = e.code_enocean(decoding=True, inpt=reencoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertEqual(decoded, redecoded)
def test_enocean_crc8_message(self): e = Encoding() received = util.hex2bit("aacbac4cddd5ddd3bddd5ddcc5ddcddd4c2d5d5c2cdddab200000") preamble, sof, eof = "aa", "9", "b" decoded, err, state = e.code_enocean(decoding=True, inpt=received) self.assertEqual(err, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertIn(preamble, util.bit2hex(decoded)) self.assertIn(sof, util.bit2hex(decoded)) self.assertIn(eof, util.bit2hex(decoded)) reencoded, errors, state = e.code_enocean(decoding=False, inpt=decoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) redecoded, errors, state = e.code_enocean(decoding=True, inpt=reencoded) self.assertEqual(errors, 0) self.assertEqual(state, e.ErrorState.SUCCESS) self.assertEqual(decoded, redecoded)
def test_reverse_engineering(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "1110001111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110011011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010"] bitset = [] crcset = [] for i in bitstring_set: tmp = c.str2bit(i) bitset.append(tmp) crcset.append(c.crc(tmp)) # print(c.guess_standard_parameters(bitset[0], crcset[0])) polynomial = c.reverse_engineer_polynomial(bitset, crcset) if polynomial: self.assertEqual(c.bit2str(polynomial), "1000000000000101") self.assertEqual(util.bit2hex(polynomial), "8005")
def polynomial_as_hex_str(self) -> str: return util.bit2hex(self.polynomial[1:]) # do not show leading one
def polynomial_as_hex_str(self) -> str: return util.bit2hex(self.polynomial[1:]) # do not show leading one