From 8d5124bf7f4e7bf1703a62d714fc73b5ccb1a41d Mon Sep 17 00:00:00 2001 From: jopohl Date: Fri, 16 Sep 2016 21:00:49 +0200 Subject: [PATCH] prepare method for message type assigning --- src/urh/awre/FormatFinder.py | 10 +++++++--- src/urh/awre/components/Address.py | 5 +++-- src/urh/awre/components/Component.py | 28 ++++++++++++++++++++++++++-- src/urh/awre/components/Length.py | 4 ++-- src/urh/awre/components/Preamble.py | 4 ++-- tests/test_awre.py | 20 +++++++++++++------- 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/urh/awre/FormatFinder.py b/src/urh/awre/FormatFinder.py index 325482f7..01d2b624 100644 --- a/src/urh/awre/FormatFinder.py +++ b/src/urh/awre/FormatFinder.py @@ -29,10 +29,14 @@ class FormatFinder(object): self.len_cluster = self.cluster_lengths() self.xor_matrix = self.build_xor_matrix() - self.preamble_component = Preamble(priority=0) + + dm = self.protocol.default_message_type + + self.preamble_component = Preamble(priority=0, default_messagetype=dm) self.length_component = Length(length_cluster=self.len_cluster, priority=1, - predecessors=[self.preamble_component]) - self.address_component = Address(xor_matrix=self.xor_matrix, priority=2, predecessors=[self.preamble_component]) + predecessors=[self.preamble_component], default_messagetype=dm) + self.address_component = Address(xor_matrix=self.xor_matrix, priority=2, predecessors=[self.preamble_component], + default_messagetype=dm) self.sequence_number_component = SequenceNumber(priority=3, predecessors=[self.preamble_component]) self.type_component = Type(priority=4, predecessors=[self.preamble_component]) self.flags_component = Flags(priority=5, predecessors=[self.preamble_component]) diff --git a/src/urh/awre/components/Address.py b/src/urh/awre/components/Address.py index 937854bd..4ead35a6 100644 --- a/src/urh/awre/components/Address.py +++ b/src/urh/awre/components/Address.py @@ -11,8 +11,8 @@ from urh.signalprocessing.MessageType import MessageType class Address(Component): MIN_ADDRESS_LENGTH = 8 # Address should be at least one byte - def __init__(self, xor_matrix, priority=2, predecessors=None, enabled=True, backend=None): - super().__init__(priority, predecessors, enabled, backend) + def __init__(self, xor_matrix, priority=2, predecessors=None, enabled=True, backend=None, default_messagetype=None): + super().__init__(priority, predecessors, enabled, backend, default_messagetype) self.xor_matrix = xor_matrix def _py_find_field(self, messages): @@ -138,6 +138,7 @@ class Address(Component): clusters["isolated"][(candidate.start, candidate.end)].update(candidate.messages) # Merge clusters and create labels + print(clusters) for participant, ranges in scored_candidates_per_participant.items(): for rng in ranges: diff --git a/src/urh/awre/components/Component.py b/src/urh/awre/components/Component.py index f60c0adf..a1b456bc 100644 --- a/src/urh/awre/components/Component.py +++ b/src/urh/awre/components/Component.py @@ -23,13 +23,16 @@ class Component(metaclass=ABCMeta): cython = 2 plainc = 3 - def __init__(self, priority=0, predecessors=None, enabled=True, backend=None): + def __init__(self, priority=0, predecessors=None, enabled=True, backend=None, default_messagetype=None): """ :param priority: Priority for this Component. 0 is highest priority :type priority: int :param predecessors: List of preceding components, that need to be run before this one :type predecessors: list of Component or None + :param default_messagetype: Default message type of the examined protocol. + This is important when assigning new message types, + to prevent overwriting already customized message types """ self.enabled = enabled self.backend = backend if backend is not None else self.Backend.python @@ -37,6 +40,8 @@ class Component(metaclass=ABCMeta): self.predecessors = predecessors if isinstance(predecessors, list) else [] """:type: list of Component """ + self.default_messagetype = default_messagetype + def find_field(self, messages): """ Wrapper method selecting the backend to assign the protocol field. @@ -67,4 +72,23 @@ class Component(metaclass=ABCMeta): raise NotImplementedError() def _c_find_field(self, messages): - raise NotImplementedError() \ No newline at end of file + raise NotImplementedError() + + + def assign_messagetypes(self, messages, clusters): + """ + Assign message types based on the clusters. Following rules: + 1) Messages from different clusters will get different message types + 2) Messages from same clusters will get same message type + 3) The new message type will copy over the existing labels + 4) No new message type will be set for messages, that already have a custom message type assigned + + For messages with clustername "default" no new message type will be created + + :param messages: Messages, that messagetype needs to be clustered + :param clusters: clusters for the messages + :type messages: list[Message] + :type clusters: dict[str, set[int]] + :return: + """ + pass diff --git a/src/urh/awre/components/Length.py b/src/urh/awre/components/Length.py index 54f21483..21dcf264 100644 --- a/src/urh/awre/components/Length.py +++ b/src/urh/awre/components/Length.py @@ -14,8 +14,8 @@ class Length(Component): """ - def __init__(self, length_cluster, priority=2, predecessors=None, enabled=True, backend=None): - super().__init__(priority, predecessors, enabled, backend ) + def __init__(self, length_cluster, priority=2, predecessors=None, enabled=True, backend=None, default_messagetype=None): + super().__init__(priority, predecessors, enabled, backend, default_messagetype) self.length_cluster = length_cluster """ diff --git a/src/urh/awre/components/Preamble.py b/src/urh/awre/components/Preamble.py index ac400cfb..6e037d1a 100644 --- a/src/urh/awre/components/Preamble.py +++ b/src/urh/awre/components/Preamble.py @@ -8,8 +8,8 @@ class Preamble(Component): Assign Preamble and SoF. """ - def __init__(self, priority=0, predecessors=None, enabled=True, backend=None): - super().__init__(priority, predecessors, enabled, backend) + def __init__(self, priority=0, predecessors=None, enabled=True, backend=None, default_messagetype=None): + super().__init__(priority, predecessors, enabled, backend, default_messagetype) def _py_find_field(self, messages): """ diff --git a/tests/test_awre.py b/tests/test_awre.py index 17689532..6e25d766 100644 --- a/tests/test_awre.py +++ b/tests/test_awre.py @@ -5,6 +5,7 @@ from collections import defaultdict from urh.awre.CommonRange import CommonRange from urh.awre.FormatFinder import FormatFinder from urh.awre.components.Address import Address +from urh.awre.components.Component import Component from urh.awre.components.Flags import Flags from urh.awre.components.Length import Length from urh.awre.components.Preamble import Preamble @@ -31,8 +32,8 @@ class TestAWRE(unittest.TestCase): alice = Participant("Alice", "A") bob = Participant("Bob", "B") alice_indices = {1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 20, 22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 41} - for i, block in enumerate(self.protocol.messages): - block.participant = alice if i in alice_indices else bob + for i, message in enumerate(self.protocol.messages): + message.participant = alice if i in alice_indices else bob self.participants = [alice, bob] @@ -130,8 +131,13 @@ class TestAWRE(unittest.TestCase): self.assertIn(expected_address1, highscored) self.assertIn(expected_address2, highscored) - # Next Steps: - # - find the most probable canidates (x) - # - see where these canidates are (ranges) and look for matches - # - if there are enough matches consider this range as address range - # - if there are different address ranges create a message type for each \ No newline at end of file + def test_message_type_assign(self): + clusters = {"ack": {1, 17, 3, 20, 5, 7, 9, 11, 13, 15}, "default": {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 19}} + com = Component() + com.assign_messagetypes(self.protocol.messages, clusters) + + for clustername, msg_indices in clusters.items(): + for msg in msg_indices: + self.assertEqual(self.protocol.messages[msg].message_type.name, clustername, msg=str(msg)) + +