Exemple #1
0
    def setUp(self):
        filename = os.path.join(tempfile.gettempdir(), "test.fuzz")
        mod1 = Modulator("mod 1")
        mod2 = Modulator("mod 2")
        mod2.param_for_one = 42

        decoders = [
            Encoder(["NRZ"]),
            Encoder(["NRZ-I", constants.DECODING_INVERT])
        ]

        pac = ProtocolAnalyzerContainer([mod1, mod2])
        pac.messages.append(
            Message([True, False, False, True, "A"],
                    100,
                    decoder=decoders[0],
                    message_type=pac.default_message_type))
        pac.messages.append(
            Message([False, False, False, False, "A"],
                    200,
                    decoder=decoders[1],
                    message_type=pac.default_message_type))
        pac.used_symbols.add(Symbol("A", 1, 1, 100))
        pac.create_fuzzing_label(1, 10, 0)
        pac.to_xml_file(filename)
Exemple #2
0
    def from_xml_tag(self, root: ET.Element, read_bits=False, participants=None, decodings=None):
        if not root:
            return None

        if root.find("modulators") and hasattr(self, "modulators"):
            self.modulators[:] = []
            for mod_tag in root.find("modulators").findall("modulator"):
                self.modulators.append(Modulator.from_xml(mod_tag))

        decoders = self.read_decoders_from_xml_tag(root) if decodings is None else decodings

        self.used_symbols.clear()
        try:
            for symbol_tag in root.find("symbols").findall("symbol"):
                s = Symbol(symbol_tag.get("name"), int(symbol_tag.get("nbits")),
                           int(symbol_tag.get("pulsetype")), int(symbol_tag.get("nsamples")))
                self.used_symbols.add(s)
        except AttributeError:
            pass

        if participants is None:
            participants = self.read_participants_from_xml_tag(root)

        if read_bits:
            self.messages[:] = []

        try:
            message_types = []
            for message_type_tag in root.find("message_types").findall("message_type"):
                message_types.append(MessageType.from_xml(message_type_tag))
        except AttributeError:
            message_types = []

        for message_type in message_types:
            if message_type not in self.message_types:
                self.message_types.append(message_type)

        try:
            message_tags = root.find("messages").findall("message")
            for i, message_tag in enumerate(message_tags):
                if read_bits:
                    message = Message.from_plain_bits_str(bits=message_tag.get("bits"),
                                                          symbols={s.name: s for s in self.used_symbols})
                    message.from_xml(tag=message_tag, participants=participants, decoders=decoders,
                                     message_types=self.message_types)
                    self.messages.append(message)
                else:
                    try:
                        self.messages[i].from_xml(tag=message_tag, participants=participants,
                                                  decoders=decoders, message_types=self.message_types)
                    except IndexError:
                        pass  # Part of signal was copied in last session but signal was not saved

        except AttributeError:
            pass
Exemple #3
0
    def __create_symbol(self, num_bits, ptype, num_samples,
                        avail_symbol_names):
        name_index = len(self.used_symbols)
        if name_index > len(avail_symbol_names) - 1:
            name_index = len(avail_symbol_names) - 1
            print("WARNING:"
                  "Needed more symbols than names were available."
                  "Symbols may be wrong labeled,"
                  "consider extending the symbol alphabet.")

        symbol = Symbol(avail_symbol_names[name_index], num_bits, ptype,
                        num_samples)

        self.used_symbols.add(symbol)
        return symbol
Exemple #4
0
    def from_plain_bits_str(bits, symbols: dict):
        plain_bits = []
        for b in bits:
            if b == "0":
                plain_bits.append(False)
            elif b == "1":
                plain_bits.append(True)
            else:
                try:
                    plain_bits.append(symbols[b])
                except KeyError:
                    print("[Warning] Did not find symbol name", file=sys.stderr)
                    plain_bits.append(Symbol(b, 0, 0, 1))

        return Message(plain_bits=plain_bits, pause=0, message_type=MessageType("none"))
Exemple #5
0
    def from_plain_bits_str(bits, symbols: dict):
        plain_bits = []
        for b in bits:
            if b == "0":
                plain_bits.append(False)
            elif b == "1":
                plain_bits.append(True)
            else:
                try:
                    plain_bits.append(symbols[b])
                except KeyError:
                    print("[Warning] Did not find symbol name",
                          file=sys.stderr)
                    plain_bits.append(Symbol(b, 0, 0, 1))

        return ProtocolBlock(plain_bits, 0, [])
Exemple #6
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())