-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecoder.py
217 lines (183 loc) · 9.1 KB
/
decoder.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
"""
This scripts decodes a candump log file, producing a text file like the input,
annotated with comments regarding the decoded payloads.
Usage: python3 decoder.py <path to candump log> [--retime] [--nospam]
"""
from TextMessage import TextMessage
from FiatProtocol import *
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument("candump_trace", help="path to the log file generated by candump")
parser.add_argument("--retime", help="shift timestamps starting from zero", action="store_true")
parser.add_argument("--nospam", help="does not print spam messages", action="store_true")
args = parser.parse_args()
p = re.compile('\(([0-9]+)\.([0-9]+)\) (.+) ([A-Z0-9]+)#([A-Z0-9]*)')
initial_timestamp = None
text_message = []
text_decoder = TextMessage()
known = 0
unknown = 0
with open(args.candump_trace) as trace:
for line in trace:
matches = p.match(line)
timestamp_int = int(matches[1])
timestamp_dec = matches[2]
can_interface = matches[3]
arb_id_hex = matches[4]
payload_hex = matches[5]
arb_id = int(arb_id_hex, 16)
topic_hex = arb_id_hex[0:4]
topic_id = int(topic_hex, 16)
device_hex = arb_id_hex[4:9]
device_id = int(device_hex, 16)
payload_ba = ba(hex='0x' + payload_hex)
description = ""
is_spam = False
if initial_timestamp is None:
initial_timestamp = timestamp_int
# BODY COMPUTER
if arb_id == CANID_BODY_STATUS:
if payload_hex == '001C00000001' or payload_hex == '001E00000001':
description = "body computer is waking up the devices on the network"
if payload_hex == '001E0400106B':
description = "body computer is asking for devices status"
is_spam = True
if payload_hex == '001A04000029':
description = "body computer is asking the devices to shut down"
elif arb_id == CANID_BODY_PROXI:
description = "body computer is verifying PROXI configuration"
elif arb_id == CANID_BODY_BUTTONS:
if payload_hex == '0000':
description = "no button is pressed"
is_spam = True
else:
if payload_ba & MASK_BUTTON_VOLUME_UP == MASK_BUTTON_VOLUME_UP:
description = "pressed Volume + key"
if payload_ba & MASK_BUTTON_VOLUME_DN == MASK_BUTTON_VOLUME_DN:
description = "pressed Volume - key"
if payload_ba & MASK_BUTTON_WINDOWS == MASK_BUTTON_WINDOWS:
description = "pressed Windows key"
if payload_ba & MASK_BUTTON_MUTE == MASK_BUTTON_MUTE:
description = "pressed Mute key"
if payload_ba & MASK_BUTTON_UP == MASK_BUTTON_UP:
description = "pressed Up key"
if payload_ba & MASK_BUTTON_DOWN == MASK_BUTTON_DOWN:
description = "pressed Down key"
if payload_ba & MASK_BUTTON_MENU == MASK_BUTTON_MENU:
description = "pressed Menu key"
if payload_ba & MASK_BUTTON_SOURCE == MASK_BUTTON_SOURCE:
description = "pressed Source key"
elif arb_id == CANID_BODY_UNKNOWN1:
if payload_ba & MASK_DOOR_DRIVER == MASK_DOOR_DRIVER:
description += "driver door opened, "
if payload_ba & MASK_DOOR_PASSENGER == MASK_DOOR_PASSENGER:
description += "passenger door opened, "
elif arb_id == CANID_BODY_UNKNOWN2:
if payload_ba & MASK_ARROW_LEFT == MASK_ARROW_LEFT:
description = "arrow left on, "
if payload_ba & MASK_ARROW_RIGHT == MASK_ARROW_RIGHT:
description = "arrow right on, "
# BLUE AND ME
elif arb_id == CANID_BM_WATCHDOG:
description = "B&M triggering watchdog"
is_spam = True
elif arb_id == CANID_BM_AUDIO_CHANNEL:
if payload_ba & MASK_AUDIOCH_MUTED == MASK_AUDIOCH_MUTED:
description = "B&M is muted"
if payload_ba & MASK_AUDIOCH_PHONE == MASK_AUDIOCH_PHONE:
description = "B&M selects phone channel"
elif payload_ba & MASK_AUDIOCH_VOICE == MASK_AUDIOCH_VOICE:
description = "B&M selects voice channel"
elif payload_ba & MASK_AUDIOCH_NAVIGATION == MASK_AUDIOCH_NAVIGATION:
description = "B&M selects navigation channel"
elif payload_ba & MASK_AUDIOCH_MEDIAPLAYER == MASK_AUDIOCH_MEDIAPLAYER:
description = "B&M selects media player channel"
if payload_ba & MASK_AUDIOCH_LOCKED == MASK_AUDIOCH_LOCKED:
description += " (locked)"
elif arb_id == CANID_BM_TRACK_TIME:
if payload_ba == MESSAGE_BM_START1_SECONDS or payload_ba == MESSAGE_BM_START2_SECONDS:
description = "song time not available on poweron/off"
else:
description = "song time is {}:{}".format(payload_hex[0:2], payload_hex[2:4])
elif arb_id == CANID_BM_TEXT_MESSAGE:
if payload_hex == '0028000000000000':
description = ""
text_message = []
elif payload_hex == '0000000000000000':
description = "clear text message"
text_message = []
else:
text_message.append(payload_hex)
if len(text_message) > int(payload_hex[0]):
description = text_decoder.decode(text_message)
text_message = []
else:
description = "text message, string follows"
is_spam = True
# 4003
elif arb_id == CANID_4003_CLOCK:
description = "clock is {}:{} on {}-{}-{}".format(
payload_hex[0:2], payload_hex[2:4], payload_hex[8:12], payload_hex[6:8], payload_hex[4:6]
)
# RADIO UNIT
elif arb_id == CANID_RADIO_STATION:
if payload_hex == '00000000000080':
description = "FM tuner is off"
else:
description = "FM radio tuned on station '{}'".format(text_decoder.decode_radio(payload_hex))
elif arb_id == CANID_RADIO_AUDIOCH:
if (payload_ba & MASK_RADIO_AUDIOCH_SHUTDOWN).int == 0:
description = "radio unit is powered down"
else:
if payload_ba & MASK_RADIO_AUDIOCH_RADIO_OFF == MASK_RADIO_AUDIOCH_RADIO_OFF:
description = "radio unit is off"
if payload_ba & MASK_RADIO_AUDIOCH_RADIO_ON == MASK_RADIO_AUDIOCH_RADIO_ON:
description = "radio unit is on"
if payload_ba & MASK_RADIO_AUDIOCH_MPMUTE_O == MASK_RADIO_AUDIOCH_MPMUTE_O:
description = "radio unit is on, mp: mute->play"
if payload_ba & MASK_RADIO_AUDIOCH_MPMUTE_I == MASK_RADIO_AUDIOCH_MPMUTE_I:
description = "radio unit is on, mp: play->mute"
if payload_ba & MASK_RADIO_AUDIOCH_MPPLAYING == MASK_RADIO_AUDIOCH_MPPLAYING:
description += ", mp is playing"
else:
description += ", mp is mute"
elif arb_id == CANID_RADIO_FREQUENCY:
if payload_ba == MESSAGE_RADIO_FREQU_OFF:
description = "radio unit is powered down"
if payload_ba == MESSAGE_RADIO_BM_PLAYING:
description = "radio unit tuned on B&M, playing"
if payload_ba == MESSAGE_RADIO_BM_MUTED:
description = "radio unit tuned on B&M, muted"
if payload_hex[2:6] != "0000":
description = "radio unit tuned on frequency {0:.2f} MHz".format(ba(hex=payload_hex[2:6]).int / 10)
else:
if topic_hex == '0E09':
is_spam = True
if device_id in KNOWN_DEVICES:
description = "status response from " + KNOWN_DEVICES[device_id]
else:
description = "status response from unknown device " + device_hex
if topic_hex == '1E11':
if device_id in KNOWN_DEVICES:
description = "PROXI response from " + KNOWN_DEVICES[device_id]
else:
description = "PROXI response from unknown device " + device_hex
if args.retime:
ts = "{}".format(timestamp_int - initial_timestamp).rjust(10) + "." + timestamp_dec
else:
ts = "{}.{}".format(timestamp_int, timestamp_dec)
if description is not "":
known += 1
out = "({}) {} {}#{}{}\t\t\t\t{}".format(ts, can_interface,
arb_id_hex, payload_hex, ' ' * (16 - len(payload_hex)), description.strip(", "))
else:
unknown += 1
out = "({}) {} {}#{}".format(ts, can_interface, arb_id_hex, payload_hex)
if args.nospam:
if not is_spam:
print(out)
else:
print(out)
print("File decoded, known messages={}; unknown={}; percentage={}%"
.format(known, unknown, 100 * known / (known + unknown)))