def test_find_generated_crc16(self): mb = MessageTypeBuilder("data") mb.add_label(FieldType.Function.PREAMBLE, 8) mb.add_label(FieldType.Function.SYNC, 16) mb.add_label(FieldType.Function.LENGTH, 8) mb.add_label(FieldType.Function.DATA, 32) mb.add_checksum_label(16, GenericCRC.from_standard_checksum("CRC16 CCITT")) mb2 = MessageTypeBuilder("data2") mb2.add_label(FieldType.Function.PREAMBLE, 8) mb2.add_label(FieldType.Function.SYNC, 16) mb2.add_label(FieldType.Function.LENGTH, 8) mb2.add_label(FieldType.Function.DATA, 16) mb2.add_checksum_label(16, GenericCRC.from_standard_checksum("CRC16 CCITT")) pg = ProtocolGenerator([mb.message_type, mb2.message_type], syncs_by_mt={mb.message_type: "0x1234", mb2.message_type: "0x1234"}) num_messages = 5 for i in range(num_messages): pg.generate_message(data="{0:032b}".format(i), message_type=mb.message_type) pg.generate_message(data="{0:016b}".format(i), message_type=mb2.message_type) #self.save_protocol("crc16_test", pg) self.clear_message_types(pg.protocol.messages) ff = FormatFinder(pg.protocol.messages) ff.run() self.assertEqual(len(ff.message_types), 2) for mt in ff.message_types: checksum_label = mt.get_first_label_with_type(FieldType.Function.CHECKSUM) self.assertEqual(checksum_label.length, 16) self.assertEqual(checksum_label.checksum.caption, "CRC16 CCITT")
def _prepare_protocol_3() -> ProtocolGenerator: alice = Participant("Alice", address_hex="1337") bob = Participant("Bob", address_hex="beef") checksum = GenericCRC.from_standard_checksum("CRC8 CCITT") mb = MessageTypeBuilder("data") mb.add_label(FieldType.Function.PREAMBLE, 16) mb.add_label(FieldType.Function.SYNC, 16) mb.add_label(FieldType.Function.LENGTH, 8) mb.add_label(FieldType.Function.SRC_ADDRESS, 16) mb.add_label(FieldType.Function.DST_ADDRESS, 16) mb.add_label(FieldType.Function.SEQUENCE_NUMBER, 8) mb.add_label(FieldType.Function.DATA, 10 * 8) mb.add_checksum_label(8, checksum) mb_ack = MessageTypeBuilder("ack") mb_ack.add_label(FieldType.Function.PREAMBLE, 16) mb_ack.add_label(FieldType.Function.SYNC, 16) mb_ack.add_label(FieldType.Function.LENGTH, 8) mb_ack.add_label(FieldType.Function.DST_ADDRESS, 16) mb_ack.add_checksum_label(8, checksum) pg = ProtocolGenerator([mb.message_type, mb_ack.message_type], syncs_by_mt={ mb.message_type: "0x9a7d", mb_ack.message_type: "0x9a7d" }, preambles_by_mt={ mb.message_type: "10" * 8, mb_ack.message_type: "10" * 8 }, participants=[alice, bob]) return pg
def _prepare_protocol_4() -> ProtocolGenerator: alice = Participant("Alice", address_hex="1337") bob = Participant("Bob", address_hex="beef") checksum = GenericCRC.from_standard_checksum("CRC16 CCITT") mb = MessageTypeBuilder("data1") mb.add_label(FieldType.Function.PREAMBLE, 16) mb.add_label(FieldType.Function.SYNC, 16) mb.add_label(FieldType.Function.LENGTH, 8) mb.add_label(FieldType.Function.SRC_ADDRESS, 16) mb.add_label(FieldType.Function.DST_ADDRESS, 16) mb.add_label(FieldType.Function.DATA, 8 * 8) mb.add_checksum_label(16, checksum) mb2 = MessageTypeBuilder("data2") mb2.add_label(FieldType.Function.PREAMBLE, 16) mb2.add_label(FieldType.Function.SYNC, 16) mb2.add_label(FieldType.Function.LENGTH, 8) mb2.add_label(FieldType.Function.SRC_ADDRESS, 16) mb2.add_label(FieldType.Function.DST_ADDRESS, 16) mb2.add_label(FieldType.Function.DATA, 64 * 8) mb2.add_checksum_label(16, checksum) mb_ack = MessageTypeBuilder("ack") mb_ack.add_label(FieldType.Function.PREAMBLE, 16) mb_ack.add_label(FieldType.Function.SYNC, 16) mb_ack.add_label(FieldType.Function.LENGTH, 8) mb_ack.add_label(FieldType.Function.DST_ADDRESS, 16) mb_ack.add_checksum_label(16, checksum) mt1, mt2, mt3 = mb.message_type, mb2.message_type, mb_ack.message_type preamble = "10001000" * 2 pg = ProtocolGenerator([mt1, mt2, mt3], syncs_by_mt={ mt1: "0x9a7d", mt2: "0x9a7d", mt3: "0x9a7d" }, preambles_by_mt={ mt1: preamble, mt2: preamble, mt3: preamble }, participants=[alice, bob]) return pg
def _prepare_protocol_7() -> ProtocolGenerator: alice = Participant("Alice", address_hex="313370") bob = Participant("Bob", address_hex="031337") charly = Participant("Charly", address_hex="110000") daniel = Participant("Daniel", address_hex="001100") # broadcast = Participant("Broadcast", address_hex="ff") #TODO: Sometimes messages to broadcast checksum = GenericCRC.from_standard_checksum("CRC16 CC1101") mb = MessageTypeBuilder("data") mb.add_label(FieldType.Function.PREAMBLE, 16) mb.add_label(FieldType.Function.SYNC, 16) mb.add_label(FieldType.Function.LENGTH, 8) mb.add_label(FieldType.Function.DST_ADDRESS, 24) mb.add_label(FieldType.Function.SRC_ADDRESS, 24) mb.add_label(FieldType.Function.DATA, 8 * 8) mb.add_checksum_label(16, checksum) mb_ack = MessageTypeBuilder("ack") mb_ack.add_label(FieldType.Function.PREAMBLE, 8) mb_ack.add_label(FieldType.Function.SYNC, 16) mb_ack.add_label(FieldType.Function.DST_ADDRESS, 24) mb_ack.add_checksum_label(16, checksum) mb_kex = MessageTypeBuilder("kex") mb_kex.add_label(FieldType.Function.PREAMBLE, 24) mb_kex.add_label(FieldType.Function.SYNC, 16) mb_kex.add_label(FieldType.Function.DST_ADDRESS, 24) mb_kex.add_label(FieldType.Function.SRC_ADDRESS, 24) mb_kex.add_label(FieldType.Function.DATA, 64 * 8) mb_kex.add_checksum_label(16, checksum) pg = ProtocolGenerator( [mb.message_type, mb_ack.message_type, mb_kex.message_type], syncs_by_mt={ mb.message_type: "0x0420", mb_ack.message_type: "0x2222", mb_kex.message_type: "0x6767" }, preambles_by_mt={ mb.message_type: "10" * 8, mb_ack.message_type: "10" * 4, mb_kex.message_type: "10" * 12 }, participants=[alice, bob, charly, daniel]) return pg
def generate_homematic(cls, num_messages: int, save_protocol=True): mb_m_frame = MessageTypeBuilder("mframe") mb_c_frame = MessageTypeBuilder("cframe") mb_r_frame = MessageTypeBuilder("rframe") mb_a_frame = MessageTypeBuilder("aframe") participants = [ Participant("CCU", address_hex="3927cc"), Participant("Switch", address_hex="3101cc") ] checksum = GenericCRC.from_standard_checksum("CRC16 CC1101") for mb_builder in [mb_m_frame, mb_c_frame, mb_r_frame, mb_a_frame]: mb_builder.add_label(FieldType.Function.PREAMBLE, 32) mb_builder.add_label(FieldType.Function.SYNC, 32) mb_builder.add_label(FieldType.Function.LENGTH, 8) mb_builder.add_label(FieldType.Function.SEQUENCE_NUMBER, 8) mb_builder.add_label(FieldType.Function.TYPE, 16) mb_builder.add_label(FieldType.Function.SRC_ADDRESS, 24) mb_builder.add_label(FieldType.Function.DST_ADDRESS, 24) if mb_builder.name == "mframe": mb_builder.add_label(FieldType.Function.DATA, 16, name="command") elif mb_builder.name == "cframe": mb_builder.add_label(FieldType.Function.DATA, 16 * 4, name="command+challenge+magic") elif mb_builder.name == "rframe": mb_builder.add_label(FieldType.Function.DATA, 32 * 4, name="cipher") elif mb_builder.name == "aframe": mb_builder.add_label(FieldType.Function.DATA, 10 * 4, name="command + auth") mb_builder.add_checksum_label(16, checksum) message_types = [ mb_m_frame.message_type, mb_c_frame.message_type, mb_r_frame.message_type, mb_a_frame.message_type ] preamble = "0xaaaaaaaa" sync = "0xe9cae9ca" initial_sequence_number = 36 pg = ProtocolGenerator( message_types, participants, preambles_by_mt={mt: preamble for mt in message_types}, syncs_by_mt={mt: sync for mt in message_types}, sequence_numbers={ mt: initial_sequence_number for mt in message_types }, message_type_codes={ mb_m_frame.message_type: 42560, mb_c_frame.message_type: 40962, mb_r_frame.message_type: 40963, mb_a_frame.message_type: 32770 }) for i in range(num_messages): mt = pg.message_types[i % 4] data_length = mt.get_first_label_with_type( FieldType.Function.DATA).length data = "".join( random.choice(["0", "1"]) for _ in range(data_length)) pg.generate_message(mt, data, source=pg.participants[i % 2], destination=pg.participants[(i + 1) % 2]) if save_protocol: cls.save_protocol("homematic", pg) cls.clear_message_types(pg.messages) return pg.protocol