Example #1
0
    def __create_label(self, name: str, start: int, end: int, color_index: int,
                       auto_created: bool, field_type: FieldType):
        if field_type is not None:
            if field_type.function == FieldType.Function.CHECKSUM:
                # If we have sync or preamble labels start behind last one:
                pre_sync_label_ends = [
                    lbl.end for lbl in self if lbl.is_preamble or lbl.is_sync
                ]
                if len(pre_sync_label_ends) > 0:
                    range_start = max(pre_sync_label_ends)
                else:
                    range_start = 0

                if range_start >= start:
                    range_start = 0

                return ChecksumLabel(name=name,
                                     start=start,
                                     end=end,
                                     color_index=color_index,
                                     field_type=field_type,
                                     auto_created=auto_created,
                                     data_range_start=range_start)

        return ProtocolLabel(name=name,
                             start=start,
                             end=end,
                             color_index=color_index,
                             field_type=field_type,
                             auto_created=auto_created)
Example #2
0
    def from_xml(tag: ET.Element):
        field_types_by_caption = {
            ft.caption: ft
            for ft in FieldType.load_from_xml()
        }

        name = tag.get("name", "blank")
        id = tag.get("id", None)
        assigned_by_ruleset = bool(int(tag.get("assigned_by_ruleset", 0)))
        assigned_by_logic_analyzer = bool(
            int(tag.get("assigned_by_logic_analyzer", 0)))
        labels = []
        for lbl_tag in tag.findall("label"):
            labels.append(
                ProtocolLabel.from_xml(
                    lbl_tag, field_types_by_caption=field_types_by_caption))
        for lbl_tag in tag.findall("checksum_label"):
            labels.append(
                ChecksumLabel.from_xml(
                    lbl_tag, field_types_by_caption=field_types_by_caption))
        result = MessageType(name=name,
                             iterable=labels,
                             id=id,
                             ruleset=Ruleset.from_xml(tag.find("ruleset")))
        result.assigned_by_ruleset = assigned_by_ruleset
        result.assigned_by_logic_analyzer = assigned_by_logic_analyzer

        return result
Example #3
0
    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")
Example #4
0
    def test_default_crcs(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)

        default_crc_polynomials = GenericCRC.DEFAULT_POLYNOMIALS
        special_crcs = ChecksumWidgetController.SPECIAL_CRCS

        self.assertEqual(
            len(default_crc_polynomials) + len(special_crcs),
            crc_widget_controller.ui.comboBoxCRCFunction.count())
        for i, default_polynomial_name in enumerate(default_crc_polynomials):
            self.assertEqual(
                default_polynomial_name,
                crc_widget_controller.ui.comboBoxCRCFunction.itemText(i))

        for i, special_crc in enumerate(special_crcs):
            self.assertEqual(
                special_crc,
                crc_widget_controller.ui.comboBoxCRCFunction.itemText(
                    i + len(default_crc_polynomials)))

        crc_widget_controller.ui.comboBoxCRCFunction.setCurrentIndex(1)
        self.assertNotEqual(
            crc_widget_controller.ui.comboBoxCRCFunction.currentText(),
            "8_standard")
        crc_widget_controller.ui.comboBoxCRCFunction.setCurrentText(
            "8_standard")
        self.assertEqual(
            crc_widget_controller.ui.comboBoxCRCFunction.currentText(),
            "8_standard")
        self.assertEqual(crc_widget_controller.ui.lineEditCRCPolynomial.text(),
                         "d5")
Example #5
0
    def test_crc_widget_in_protocol_label_dialog(self):
        mt = MessageType("test")
        mt.append(
            ChecksumLabel("test_crc", 8, 16, 0,
                          FieldType("test_crc", FieldType.Function.CHECKSUM)))

        self.dialog = ProtocolLabelController(0, Message([0] * 100, 0, mt), 0)
        self.assertEqual(self.dialog.ui.tabWidgetAdvancedSettings.count(), 1)
        self.assertEqual(self.dialog.ui.tabWidgetAdvancedSettings.tabText(0),
                         "test_crc")
Example #6
0
    def field_type(self, val: FieldType):
        if val is None:
            return

        if self.is_checksum_label and val.function != FieldType.Function.CHECKSUM:
            assert isinstance(self.label, ChecksumLabel)
            self.label = self.label.to_label(val)
        elif not self.is_checksum_label and val.function == FieldType.Function.CHECKSUM:
            self.label = ChecksumLabel.from_label(self.label)
            self.value_type_index = 0
        self.label.field_type = val
Example #7
0
    def field_type(self, val: FieldType):
        if val is None:
            return

        if self.is_checksum_label and val.function != FieldType.Function.CHECKSUM:
            assert isinstance(self.label, ChecksumLabel)
            self.label = self.label.to_label(val)
        elif not self.is_checksum_label and val.function == FieldType.Function.CHECKSUM:
            self.label = ChecksumLabel.from_label(self.label)
            self.value_type_index = 0
        self.label.field_type = val
Example #8
0
    def add_checksum_label(self,
                           length,
                           checksum,
                           data_start=None,
                           data_end=None,
                           name: str = None):
        label_type = FieldType.Function.CHECKSUM
        try:
            start = self.message_type[-1].end
            color_index = self.message_type[-1].color_index + 1
        except IndexError:
            start, color_index = 0, 0

        if name is None:
            name = label_type.value

        if data_start is None:
            # End of sync or preamble
            sync_label = self.message_type.get_first_label_with_type(
                FieldType.Function.SYNC)
            if sync_label:
                data_start = sync_label.end
            else:
                preamble_label = self.message_type.get_first_label_with_type(
                    FieldType.Function.PREAMBLE)
                if preamble_label:
                    data_start = preamble_label.end
                else:
                    data_start = 0

        if data_end is None:
            data_end = start

        lbl = ChecksumLabel(name,
                            start,
                            start + length - 1,
                            color_index,
                            field_type=FieldType(label_type.name, label_type))
        lbl.data_ranges = [(data_start, data_end)]
        lbl.checksum = checksum
        self.message_type.append(lbl)
Example #9
0
    def from_xml(tag: ET.Element):
        field_types_by_caption = {ft.caption: ft for ft in FieldType.load_from_xml()}

        name = tag.get("name", "blank")
        id = tag.get("id", None)
        assigned_by_ruleset = bool(int(tag.get("assigned_by_ruleset", 0)))
        assigned_by_logic_analyzer = bool(int(tag.get("assigned_by_logic_analyzer", 0)))
        labels = []
        for lbl_tag in tag.findall("label"):
            labels.append(ProtocolLabel.from_xml(lbl_tag, field_types_by_caption=field_types_by_caption))
        for lbl_tag in tag.findall("checksum_label"):
            labels.append(ChecksumLabel.from_xml(lbl_tag, field_types_by_caption=field_types_by_caption))
        result = MessageType(name=name, iterable=labels, id=id, ruleset=Ruleset.from_xml(tag.find("ruleset")))
        result.assigned_by_ruleset = assigned_by_ruleset
        result.assigned_by_logic_analyzer = assigned_by_logic_analyzer

        return result
Example #10
0
    def test_enocean_checksum(self):
        checksum_label = ChecksumLabel(
            "checksum_label", 50, 100, 0,
            FieldType("crc", FieldType.Function.CHECKSUM))
        crc_widget_controller = ChecksumWidgetController(
            checksum_label, Message([0] * 100, 0, MessageType("test")), 0)

        crc_widget_controller.ui.comboBoxCategory.setCurrentIndex(1)
        self.assertEqual(
            crc_widget_controller.ui.stackedWidget.currentWidget(),
            crc_widget_controller.ui.page_wsp)

        self.assertTrue(
            crc_widget_controller.ui.radioButtonWSPAuto.isChecked())

        crc_widget_controller.ui.radioButtonWSPChecksum8.click()

        self.assertEqual(checksum_label.checksum.mode,
                         WSPChecksum.ChecksumMode.checksum8)
Example #11
0
    def from_xml(cls, tag: ET.Element, field_types_by_caption=None):
        """

        :param tag:
        :type field_types_by_caption: dict[str, FieldType]
        :return:
        """
        label_tag = tag.find("label")
        if label_tag is not None:
            label = ProtocolLabel.from_xml(label_tag, field_types_by_caption)
        else:
            label = ChecksumLabel.from_xml(tag.find("checksum_label"), field_types_by_caption)
        result = SimulatorProtocolLabel(label)
        result.value_type_index = Formatter.str2val(tag.get("value_type_index", "0"), int)
        result.external_program = tag.get("external_program", "")
        result.formula = tag.get("formula", "")
        result.random_min = Formatter.str2val(tag.get("random_min", "0"), int)
        result.random_max = Formatter.str2val(tag.get("random_max", str(label.fuzz_maximum-1)), int)
        return result
Example #12
0
    def from_xml(cls, tag: ET.Element, field_types_by_caption=None):
        """

        :param tag:
        :type field_types_by_caption: dict[str, FieldType]
        :return:
        """
        label_tag = tag.find("label")
        if label_tag is not None:
            label = ProtocolLabel.from_xml(label_tag, field_types_by_caption)
        else:
            label = ChecksumLabel.from_xml(tag.find("checksum_label"),
                                           field_types_by_caption)
        result = SimulatorProtocolLabel(label)
        result.value_type_index = Formatter.str2val(
            tag.get("value_type_index", "0"), int)
        result.external_program = tag.get("external_program", "")
        result.formula = tag.get("formula", "")
        result.random_min = Formatter.str2val(tag.get("random_min", "0"), int)
        result.random_max = Formatter.str2val(
            tag.get("random_max", str(label.fuzz_maximum - 1)), int)
        return result
Example #13
0
    def test_configure_crc_ranges(self):
        checksum_label = ChecksumLabel(
            "checksum_label", 50, 100, 0,
            FieldType("crc", FieldType.Function.CHECKSUM))

        crc_widget_controller = ChecksumWidgetController(
            checksum_label, Message([0] * 100, 0, MessageType("test")), 0)
        model = crc_widget_controller.data_range_table_model
        self.assertEqual(model.data(model.index(0, 0)), 1)
        self.assertEqual(model.data(model.index(0, 1)), 50)
        self.assertEqual(model.rowCount(), 1)

        crc_widget_controller.ui.btnAddRange.click()
        self.assertEqual(model.rowCount(), 2)
        crc_widget_controller.ui.btnAddRange.click()
        self.assertEqual(model.rowCount(), 3)

        crc_widget_controller.ui.btnRemoveRange.click()
        self.assertEqual(model.rowCount(), 2)
        crc_widget_controller.ui.btnRemoveRange.click()
        self.assertEqual(model.rowCount(), 1)
        crc_widget_controller.ui.btnRemoveRange.click()
        self.assertEqual(model.rowCount(), 1)