-
Notifications
You must be signed in to change notification settings - Fork 6
/
distribution.py
44 lines (33 loc) · 1.3 KB
/
distribution.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
"""
This script prints the payloads distribution on a candump log file.
Usage: python3 distribution.py <path to candump log>
"""
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument("candump_trace", help="path to the log file generated by candump")
args = parser.parse_args()
p = re.compile('\(([ 0-9]+)\.([0-9]+)\) (.+) ([A-Z0-9]+)#([A-Z0-9]*).*')
messages = {}
with open(args.candump_trace) as trace:
for line in trace:
matches = p.match(line)
if matches is None:
continue
arb_id_hex = matches[4]
payload_hex = matches[5]
topic_hex = arb_id_hex[0:4]
device_hex = arb_id_hex[4:9]
if device_hex not in messages:
messages[device_hex] = {}
if topic_hex not in messages[device_hex]:
messages[device_hex][topic_hex] = {}
if payload_hex not in messages[device_hex][topic_hex]:
messages[device_hex][topic_hex][payload_hex] = 0
messages[device_hex][topic_hex][payload_hex] += 1
for dev_id, topics in sorted(messages.items()):
for topic, values in sorted(messages[dev_id].items()):
print("{}{}".format(topic, dev_id))
for value, count in sorted(messages[dev_id][topic].items()):
print(" {} -> {}".format(value, count))
print("")