/
AWRExperiments.py
586 lines (473 loc) · 25.1 KB
/
AWRExperiments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import array
import random
import time
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from tests.awre.AWRETestCase import AWRETestCase
from tests.utils_testing import get_path_for_data_file
from urh.awre.FormatFinder import FormatFinder
from urh.awre.MessageTypeBuilder import MessageTypeBuilder
from urh.awre.Preprocessor import Preprocessor
from urh.awre.ProtocolGenerator import ProtocolGenerator
from urh.awre.engines.Engine import Engine
from urh.signalprocessing.FieldType import FieldType
from urh.signalprocessing.Message import Message
from urh.signalprocessing.MessageType import MessageType
from urh.signalprocessing.Participant import Participant
from urh.signalprocessing.ProtocoLabel import ProtocolLabel
from urh.signalprocessing.ProtocolAnalyzer import ProtocolAnalyzer
from urh.util.GenericCRC import GenericCRC
class AWRExperiments(AWRETestCase):
@staticmethod
def _prepare_protocol_1() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="dead")
bob = Participant("Bob", address_hex="beef")
mb = MessageTypeBuilder("protocol_with_one_message_type")
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.SRC_ADDRESS, 16)
mb.add_label(FieldType.Function.DST_ADDRESS, 16)
mb.add_label(FieldType.Function.SEQUENCE_NUMBER, 8)
pg = ProtocolGenerator([mb.message_type],
syncs_by_mt={mb.message_type: "0x1337"},
participants=[alice, bob])
return pg
@staticmethod
def _prepare_protocol_2() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="dead01")
bob = Participant("Bob", address_hex="beef24")
mb = MessageTypeBuilder("protocol_with_one_message_type")
mb.add_label(FieldType.Function.PREAMBLE, 72)
mb.add_label(FieldType.Function.SYNC, 16)
mb.add_label(FieldType.Function.LENGTH, 8)
mb.add_label(FieldType.Function.SRC_ADDRESS, 24)
mb.add_label(FieldType.Function.DST_ADDRESS, 24)
mb.add_label(FieldType.Function.SEQUENCE_NUMBER, 16)
pg = ProtocolGenerator([mb.message_type],
syncs_by_mt={mb.message_type: "0x1337"},
preambles_by_mt={mb.message_type: "10" * 36},
participants=[alice, bob])
return pg
@staticmethod
def _prepare_protocol_3() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="1337")
bob = Participant("Bob", address_hex="beef")
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, 16)
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)
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
@staticmethod
def _prepare_protocol_4() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="1337")
bob = Participant("Bob", address_hex="beef")
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.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
pg = ProtocolGenerator([mt1, mt2, mt3],
syncs_by_mt={mt1: "0x9a7d", mt2: "0x9a7d", mt3: "0x9a7d"},
preambles_by_mt={mt1: "10" * 8, mt2: "10" * 8, mt3: "10" * 8},
participants=[alice, bob])
return pg
@staticmethod
def _prepare_protocol_5() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="1337")
bob = Participant("Bob", address_hex="beef")
carl = Participant("Carl", address_hex="cafe")
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, 16)
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)
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, carl])
return pg
@staticmethod
def _prepare_protocol_6() -> ProtocolGenerator:
alice = Participant("Alice", address_hex="24")
broadcast = Participant("Bob", address_hex="ff")
mb = MessageTypeBuilder("data")
mb.add_label(FieldType.Function.SYNC, 16)
mb.add_label(FieldType.Function.LENGTH, 8)
mb.add_label(FieldType.Function.SRC_ADDRESS, 8)
mb.add_label(FieldType.Function.SEQUENCE_NUMBER, 8)
pg = ProtocolGenerator([mb.message_type],
syncs_by_mt={mb.message_type: "0x8e88"},
preambles_by_mt={mb.message_type: "10" * 8},
participants=[alice, broadcast])
return pg
@staticmethod
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.SEQUENCE_NUMBER, 32)
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
@classmethod
def get_protocol(cls, protocol_number: int, num_messages, num_broken_messages=0, silent=False):
if protocol_number == 1:
pg = cls._prepare_protocol_1()
elif protocol_number == 2:
pg = cls._prepare_protocol_2()
elif protocol_number == 3:
pg = cls._prepare_protocol_3()
elif protocol_number == 4:
pg = cls._prepare_protocol_4()
elif protocol_number == 5:
pg = cls._prepare_protocol_5()
elif protocol_number == 6:
pg = cls._prepare_protocol_6()
elif protocol_number == 7:
pg = cls._prepare_protocol_7()
else:
raise ValueError("Unknown protocol number")
messages_types_with_data_field = [mt for mt in pg.protocol.message_types
if mt.get_first_label_with_type(FieldType.Function.DATA)]
i = -1
while len(pg.protocol.messages) < num_messages:
i += 1
source = pg.participants[i % len(pg.participants)]
destination = pg.participants[(i + 1) % len(pg.participants)]
if i % 2 == 0:
data_bytes = 8
else:
# data_bytes = 16
data_bytes = 64
data = "".join(random.choice(["0", "1"]) for _ in range(data_bytes * 8))
if len(messages_types_with_data_field) == 0:
# set data automatically
pg.generate_message(data=data, source=source, destination=destination)
else:
# search for message type with right data length
mt = next(mt for mt in messages_types_with_data_field
if mt.get_first_label_with_type(FieldType.Function.DATA).length == data_bytes * 8)
pg.generate_message(message_type=mt, data=data, source=source, destination=destination)
ack_message_type = next((mt for mt in pg.protocol.message_types if "ack" in mt.name), None)
if ack_message_type:
pg.generate_message(message_type=ack_message_type, data="", source=destination, destination=source)
for i in range(num_broken_messages):
msg = pg.protocol.messages[i]
pos = random.randint(0, len(msg.plain_bits) // 2)
msg.plain_bits[pos:] = array.array("B",
[random.randint(0, 1) for _ in range(len(msg.plain_bits) - pos)])
if num_broken_messages == 0:
cls.save_protocol("protocol{}_{}_messages".format(protocol_number, num_messages), pg, silent=silent)
else:
cls.save_protocol("protocol{}_{}_broken".format(protocol_number, num_broken_messages), pg, silent=silent)
expected_message_types = [msg.message_type for msg in pg.protocol.messages]
# Delete message type information -> no prior knowledge
cls.clear_message_types(pg.protocol.messages)
# Delete data labels if present
for mt in expected_message_types:
data_lbl = mt.get_first_label_with_type(FieldType.Function.DATA)
if data_lbl:
mt.remove(data_lbl)
return pg.protocol, expected_message_types
@staticmethod
def calculate_accuracy(messages, expected_labels, num_broken_messages=0):
"""
Calculate the accuracy of labels compared to expected labels
Accuracy is 100% when labels == expected labels
Accuracy drops by 1 / len(expected_labels) for every expected label not present in labels
:type messages: list of Message
:type expected_labels: list of MessageType
:return:
"""
accuracy = 0
def label_functions_matches(lbl1: ProtocolLabel, lbl2: ProtocolLabel) -> bool:
return lbl1.field_type.function == lbl2.field_type.function
for i, msg in enumerate(messages):
if i in range(num_broken_messages):
continue
expected = expected_labels[i] # type: MessageType
msg_accuracy = 1
for lbl in expected:
try:
next(l for l in msg.message_type if
l.start == lbl.start and l.end == lbl.end and label_functions_matches(l, lbl))
found = True
except StopIteration:
found = False
if not found:
msg_accuracy -= 1 / len(expected)
accuracy += msg_accuracy * (1 / (len(messages) - num_broken_messages))
return accuracy * 100
def test_against_num_messages(self):
num_messages = list(range(1, 24, 1))
accuracies = defaultdict(list)
protocols = [1, 2, 3, 4, 5, 6, 7]
random.seed(0)
np.random.seed(0)
for protocol_nr in protocols:
for n in num_messages:
protocol, expected_labels = self.get_protocol(protocol_nr, num_messages=n)
self.run_format_finder_for_protocol(protocol)
accuracy = self.calculate_accuracy(protocol.messages, expected_labels)
accuracies["protocol {}".format(protocol_nr)].append(accuracy)
self.__plot(num_messages, accuracies, xlabel="Number of messages", ylabel="Accuracy in %", grid=True)
self.__export_to_csv("/tmp/accuray-vs-messages", num_messages, accuracies)
def test_against_error(self):
Engine._DEBUG_ = False
Preprocessor._DEBUG_ = False
num_runs = 100
num_messages = 30
num_broken_messages = list(range(0, num_messages + 1))
accuracies = defaultdict(list)
accuracies_without_broken = defaultdict(list)
protocols = [1, 2, 3, 4, 5, 6, 7]
random.seed(0)
np.random.seed(0)
for protocol_nr in protocols:
for broken in num_broken_messages:
tmp_accuracies = np.empty(num_runs, dtype=np.float64)
tmp_accuracies_without_broken = np.empty(num_runs, dtype=np.float64)
for i in range(num_runs):
print("\rProtocol {0} with {1:02d} broken messages ({2}/{3} runs)".format(protocol_nr, broken, i+1,
num_runs), flush=True,
end="")
protocol, expected_labels = self.get_protocol(protocol_nr,
num_messages=num_messages,
num_broken_messages=broken,
silent=True)
self.run_format_finder_for_protocol(protocol)
accuracy = self.calculate_accuracy(protocol.messages, expected_labels)
accuracy_without_broken = self.calculate_accuracy(protocol.messages, expected_labels, broken)
tmp_accuracies[i] = accuracy
tmp_accuracies_without_broken[i] = accuracy_without_broken
avg_accuracy = np.mean(tmp_accuracies)
avg_accuracy_without_broken = np.mean(tmp_accuracies_without_broken)
accuracies["protocol {}".format(protocol_nr)].append(avg_accuracy)
accuracies_without_broken["protocol {}".format(protocol_nr)].append(avg_accuracy_without_broken)
print(" {:>3}% {:>3}%".format(int(avg_accuracy), int(avg_accuracy_without_broken)))
self.__plot(100 * np.array(num_broken_messages) / num_messages, accuracies,
title="Overall Accuracy vs percentage of broken messages",
xlabel="Broken messages in %",
ylabel="Accuracy in %", grid=True)
self.__plot(100 * np.array(num_broken_messages) / num_messages, accuracies_without_broken,
title=" Accuracy of unbroken vs percentage of broken messages",
xlabel="Broken messages in %",
ylabel="Accuracy in %", grid=True)
self.__export_to_csv("/tmp/accuray-vs-error", num_broken_messages, accuracies, relative=num_messages)
self.__export_to_csv("/tmp/accuray-vs-error-without-broken", num_broken_messages, accuracies_without_broken, relative=num_messages)
def test_performance(self):
Engine._DEBUG_ = False
Preprocessor._DEBUG_ = False
num_messages = list(range(200, 205, 5))
protocols = [1]
random.seed(0)
np.random.seed(0)
performances = defaultdict(list)
for protocol_nr in protocols:
print("Running for protocol", protocol_nr)
for messages in num_messages:
protocol, _ = self.get_protocol(protocol_nr, messages, silent=True)
t = time.time()
self.run_format_finder_for_protocol(protocol)
performances["protocol {}".format(protocol_nr)].append(time.time() - t)
# self.__plot(num_messages, performances, xlabel="Number of messages", ylabel="Time in seconds", grid=True)
def test_performance_real_protocols(self):
Engine._DEBUG_ = False
Preprocessor._DEBUG_ = False
num_messages = list(range(8, 256, 4))
protocol_names = ["enocean", "homematic", "rwe"]
random.seed(0)
np.random.seed(0)
performances = defaultdict(list)
for protocol_name in protocol_names:
print("Running for protocol", protocol_name)
for messages in num_messages:
if protocol_name == "homematic":
protocol = self.generate_homematic(messages, save_protocol=False)
elif protocol_name == "enocean":
protocol = self.generate_enocean(messages, save_protocol=False)
elif protocol_name == "rwe":
protocol = self.generate_rwe(messages, save_protocol=False)
else:
raise ValueError("Unknown protocol name")
t = time.time()
self.run_format_finder_for_protocol(protocol)
performances["{}".format(protocol_name)].append(time.time() - t)
self.__plot(num_messages, performances, xlabel="Number of messages", ylabel="Time in seconds", grid=True)
@staticmethod
def __export_to_csv(filename: str, x: list, y: dict, relative=None):
if not filename.endswith(".csv"):
filename += ".csv"
with open(filename, "w") as f:
f.write("N,")
if relative is not None:
f.write("NRel,")
for y_cap in sorted(y):
f.write(y_cap + ",")
f.write("\n")
for i, x_val in enumerate(x):
f.write("{},".format(x_val))
if relative is not None:
f.write("{},".format(100 * x_val / relative))
for y_cap in sorted(y):
f.write("{},".format(y[y_cap][i]))
f.write("\n")
@staticmethod
def __plot(x: list, y: dict, xlabel: str, ylabel: str, grid=False, title=None):
plt.xlabel(xlabel)
plt.ylabel(ylabel)
for y_cap, y_values in sorted(y.items()):
plt.plot(x, y_values, label=y_cap)
if grid:
plt.grid()
if title:
plt.title(title)
plt.legend()
plt.show()
@staticmethod
def run_format_finder_for_protocol(protocol: ProtocolAnalyzer):
ff = FormatFinder(protocol.messages)
ff.known_participant_addresses.clear()
ff.run()
for msg_type, indices in ff.existing_message_types.items():
for i in indices:
protocol.messages[i].message_type = msg_type
def generate_homematic(self, 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:
self.save_protocol("homematic", pg)
self.clear_message_types(pg.messages)
return pg.protocol
def generate_enocean(self, num_messages: int, save_protocol=True):
filename = get_path_for_data_file("enocean_bits.txt")
enocean_bits = []
with open(filename, "r") as f:
for line in map(str.strip, f):
enocean_bits.append(line)
protocol = ProtocolAnalyzer(None)
message_type = MessageType("empty")
for i in range(num_messages):
msg = Message.from_plain_bits_str(enocean_bits[i % len(enocean_bits)])
msg.message_type = message_type
protocol.messages.append(msg)
if save_protocol:
self.save_protocol("enocean", protocol)
return protocol
def generate_rwe(self, num_messages: int, save_protocol=True):
ff, messages = self.get_format_finder_from_protocol_file("rwe.proto.xml", return_messages=True)
protocol = ProtocolAnalyzer(None)
message_type = MessageType("empty")
for i in range(num_messages):
msg = messages[i % len(messages)] # type: Message
msg.message_type = message_type
protocol.messages.append(msg)
if save_protocol:
self.save_protocol("rwe", protocol)
return protocol