Exemplo n.º 1
0
    def redo(self):
        len_pattern = len(self.amble_pattern)
        amble_sequence = self.amble_pattern * self.min_occurences
        len_sequence = len(amble_sequence)
        labels = []

        for group in self.groups:
            count = 0
            for k, block in enumerate(group.blocks):
                bit_str = block.decoded_bits_str
                len_bit_str = len(bit_str)
                indx = bit_str.find(amble_sequence)
                labels.append([])
                while indx != -1:
                    start = indx
                    end = indx + len_sequence
                    for i in range(start + len_sequence, len_bit_str,
                                   len_pattern):
                        if bit_str[i:i + len_pattern] != self.amble_pattern:
                            end = i
                            break

                    start = start if start > 1 else 0  # Erstes Bit mitnehmen, wenn Preamble beim zweiten Bit beginnt

                    if count == 0:
                        name = "Preamble"
                    else:
                        name = "Amble #{0:d}".format(count)
                    lbl = ProtocolLabel(name, start, end - 1, k, 0, -1, True)
                    lbl.reference_bits = bit_str[start:end]
                    lbl.apply_decoding = False
                    labels[k].append(lbl)
                    indx = bit_str.find(amble_sequence, end - 1)
                    count += 1
                    group.add_label(lbl)
Exemplo n.º 2
0
 def to_label(self, field_type: FieldType) -> ProtocolLabel:
     result = ProtocolLabel(name=self.name, start=self.start, end=self.end - 1, color_index=self.color_index, field_type=field_type,
                            auto_created=self.auto_created, fuzz_created=self.fuzz_created)
     result.apply_decoding = self.apply_decoding
     result.show = self.show
     result.fuzz_me = self.fuzz_me
     result.fuzz_values = self.fuzz_values
     result.display_format_index = self.display_format_index
     return result
Exemplo n.º 3
0
    def read_compare_frame_groups(self):
        if self.project_file is None:
            return

        tree = ET.parse(self.project_file)
        root = tree.getroot()

        proto_tree_model = self.maincontroller.compare_frame_controller.proto_tree_model
        tree_root = proto_tree_model.rootItem
        pfi = proto_tree_model.protocol_tree_items
        proto_frame_items = [item for item in pfi[0]]
        """:type: list of ProtocolTreeItem """

        for group_tag in root.iter("group"):
            name = group_tag.attrib["name"]
            id = group_tag.attrib["id"]

            if id == "0":
                tree_root.child(0).setData(name)
            else:
                tree_root.addGroup(name=name)

            group = tree_root.child(int(id))

            for proto_tag in group_tag.iter("cf_protocol"):
                filename = os.path.join(self.project_path, proto_tag.attrib["filename"])
                show = proto_tag.attrib["show"]
                try:
                    proto_frame_item = next((p for p in proto_frame_items if p.protocol.filename == filename))
                except StopIteration:
                    proto_frame_item = None

                if proto_frame_item is not None:
                    group.appendChild(proto_frame_item)
                    proto_frame_item.show_in_compare_frame = Qt.Checked if show == "1" else Qt.Unchecked

            group = proto_tree_model.groups[int(id)]

            for label_tag in group_tag.iter("label"):
                name = label_tag.attrib["name"]
                start = int(label_tag.attrib["start"])
                end = int(label_tag.attrib["end"])
                refblock = int(label_tag.attrib["refblock"])
                color_index = int(label_tag.attrib["color_index"])
                restrictive = int(label_tag.attrib["restrictive"]) == 1

                proto_label = ProtocolLabel(name, start, end, refblock, 0, color_index, restrictive)
                proto_label.reference_bits = label_tag.attrib["refbits"]
                proto_label.display_type_index = int(label_tag.attrib["display_type_index"])
                proto_label.apply_decoding = int(label_tag.attrib["apply_decoding"]) == 1

                group.add_label(proto_label)

            self.maincontroller.compare_frame_controller.expand_group_node(int(id))

        self.maincontroller.compare_frame_controller.refresh()
Exemplo n.º 4
0
def read_protocol(filename: str):
    if not os.path.isfile(filename):
        raise FileNotFoundError("{0} could not be found".format(filename))

    with open(filename, mode="r") as f:
        viewtype = 0
        reading_proto = False
        reading_labels = False
        reading_symbols = False
        label_name = None
        label_start = label_end = label_ref_block = -1
        label_restrictive = None
        label_blocks = None
        apply_decoding = None
        symbols = dict()
        cur_group = -1
        groups = []
        for line in f:
            line = line.strip()
            line = line.strip("\n")
            if line.startswith("#") or len(line) == 0:
                continue
            elif line.startswith("VIEWTYPE"):
                _, viewtype_str = line.split("=")
                viewtype_str = viewtype_str.strip()
                if viewtype_str not in VIEW_TYPES:
                    raise SyntaxError(
                        "Unknown Viewtype {0} in file {1}".format(
                            viewtype_str, filename))
                else:
                    viewtype = VIEW_TYPES.index(viewtype_str)
            elif line.startswith("GROUPNAME"):
                _, name = line.split("=")
                cur_group += 1
                groups.append({})
                groups[cur_group]["name"] = name.strip()
                groups[cur_group]["blocks"] = []
                groups[cur_group]["labels"] = []
            elif line.startswith("ENCODING"):
                _, encoding_str = line.split("=")
                encoding_str = encoding_str.strip()
                decoding = int(encoding_str)
                groups[cur_group]["decoding_index"] = decoding
            elif line.startswith("SYMBOLS:"):
                reading_symbols = True
                reading_labels = False
                reading_proto = False
            elif line.startswith("PROTOCOL:"):
                reading_proto = True
                reading_symbols = False
                reading_labels = False
            elif line.startswith("PROTOCOL-LABELS:"):
                reading_proto = False
                reading_symbols = False
                reading_labels = True
            elif reading_symbols and line.startswith("-"):
                try:
                    _, symbol_name, nbits, pulsetype, nsamples = line.split(
                        " ")
                    symbols[symbol_name] = Symbol(symbol_name, int(nbits),
                                                  int(pulsetype),
                                                  int(nsamples))
                except ValueError:
                    continue
            elif reading_proto and len(line) > 0:
                groups[cur_group]["blocks"].append(
                    ProtocolBlock.from_plain_bits_str(line, symbols))
            elif reading_labels and line.startswith("Name"):
                label_name = line.replace("Name: ", "")
            elif reading_labels and line.startswith("Bits"):
                label_start, label_end = map(
                    int,
                    line.replace("Bits: ", "").split("-"))
                label_start -= 1
                label_end -= 1
            elif reading_labels and line.startswith("Restrictive"):
                if line.replace("Restrictive: ", "") == "True":
                    label_restrictive = True
                else:
                    label_restrictive = False
            elif reading_labels and line.startswith("Reference Block"):
                label_ref_block = int(line.replace("Reference Block: ",
                                                   "")) - 1
            elif reading_labels and line.startswith("Applies for Blocks: "):
                label_blocks = list(
                    map(int,
                        line.replace("Applies for Blocks: ", "").split(",")))
            elif reading_labels and line.startswith("Apply Decoding: "):
                apply_decoding = False if line.replace("Apply Decoding: ",
                                                       "") == "False" else True


            if label_name is not None and label_start >= 0 and label_end >= 0 and label_restrictive is not None\
                    and label_ref_block >= 0 and label_blocks is not None and apply_decoding is not None:
                color_index = len(groups[cur_group]["labels"])
                proto_label = ProtocolLabel(label_name, label_start, label_end,
                                            label_ref_block, 0, color_index,
                                            label_restrictive)
                proto_label.block_numbers = label_blocks[:]
                proto_label.apply_decoding = apply_decoding
                groups[cur_group]["labels"].append(proto_label)

                label_name = None
                label_start = label_end = label_ref_block = -1
                label_restrictive = None
                label_blocks = None

        if len(groups) == 0:
            raise SyntaxError("Did not find a PROTOCOL in file " + filename)

        return viewtype, groups, set(symbols.values())