Skip to content

Commit 87701ff

Browse files
committed
Added more error handling to bail out more gracefully when things go wrong. Could
be more common with bluetooth connections.
1 parent 754ea84 commit 87701ff

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/msf/core/post/hardware/automotive/uds.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def get_current_data(bus, srcId, dstId, pid, opt={})
104104
def get_current_data_pids(bus, srcId, dstId)
105105
pids = []
106106
packets = get_current_data(bus, srcId, dstId, 0, {"MAXPKTS" => 1})
107+
return pids if packets == nil
107108
if packets.has_key? "Packets" and packets["Packets"].size > 0
108109
hexpids = packets["Packets"][0]["DATA"][3,6]
109110
hexpids = hexpids.join.hex.to_s(2).rjust(32, '0').split('') # Array of 1s and 0s
@@ -184,6 +185,7 @@ def get_current_data_pids(bus, srcId, dstId)
184185
# @return [Hash] Packet Hash with { "MIL" => true|false "DTC_COUNT" => 0 }
185186
def get_monitor_status(bus, srcId, dstId)
186187
packets = get_current_data(bus, srcId, dstId, 0x01, {"MAXPKTS" => 1})
188+
return {} if packets == nil
187189
return packets if packets.has_key? "error"
188190
return packets if not packets.has_key? "Packets"
189191
packets["MIL"] = packets["Packets"][0]["DATA"][3].hex & 0xB0 == 1 ? true : false
@@ -201,6 +203,7 @@ def get_monitor_status(bus, srcId, dstId)
201203
# @return [Hash] Packet Hash with { "TEMP_C" => <Celcious Temp>, "TEMP_F" => <Fahrenheit TEmp> }
202204
def get_engine_coolant_temp(bus, srcId, dstId)
203205
packets = get_current_data(bus, srcId, dstId, 0x05, {"MAXPKTS" => 1})
206+
return {} if packets == nil
204207
return packets if packets.has_key? "error"
205208
return packets if not packets.has_key? "Packets"
206209
celcious = packets["Packets"][0]["DATA"][3].hex - 40
@@ -220,6 +223,7 @@ def get_engine_coolant_temp(bus, srcId, dstId)
220223
# @return [Hash] Packet Hash with { "RPM" => <RPMs> }
221224
def get_rpms(bus, srcId, dstId)
222225
packets = get_current_data(bus, srcId, dstId, 0x0C, {"MAXPKTS" => 1})
226+
return {} if packets == nil
223227
return packets if packets.has_key? "error"
224228
return packets if not packets.has_key? "Packets"
225229
packets["RPM"] = (256 * packets["Packets"][0]["DATA"][3].hex + packets["Packets"][0]["DATA"][4].hex) / 4
@@ -236,6 +240,7 @@ def get_rpms(bus, srcId, dstId)
236240
# @return [Hash] Packet Hash with { "SPEED_K" => <km/h>, "SPEED_M" => <mph> }
237241
def get_vehicle_speed(bus, srcId, dstId)
238242
packets = get_current_data(bus, srcId, dstId, 0x0D, {"MAXPKTS" => 1})
243+
return {} if packets == nil
239244
return packets if packets.has_key? "error"
240245
return packets if not packets.has_key? "Packets"
241246
packets["SPEED_K"] = packets["Packets"][0]["DATA"][3].hex
@@ -254,6 +259,7 @@ def get_vehicle_speed(bus, srcId, dstId)
254259
# @return [String] Description of standard
255260
def get_obd_standards(bus, srcId, dstId)
256261
packets = get_current_data(bus, srcId, dstId, 0x1C, {"MAXPKTS" => 1})
262+
return "" if packets == nil
257263
if packets.has_key? "error"
258264
print_error("OBD ERR: #{packets["error"]}")
259265
return ""
@@ -379,6 +385,7 @@ def get_dtcs(bus, srcId, dstId, opt={})
379385
return {}
380386
end
381387
data = client.automotive.send_isotp_and_wait_for_response(bus,srcId, dstId, [0x03], opt)
388+
return [] if data == nil
382389
if data.has_key? "error"
383390
print_error("UDS ERR: #{data["error"]}")
384391
return []
@@ -474,6 +481,7 @@ def get_vehicle_info(bus, srcId, dstId, mode, opt={})
474481
def get_vinfo_supported_pids(bus, srcId, dstId)
475482
pids = []
476483
packets = get_vehicle_info(bus, srcId, dstId, 0, {"MAXPKTS" => 1})
484+
return pids if packets == nil
477485
if packets.has_key? "Packets" and packets["Packets"].size > 0
478486
hexpids = packets["Packets"][0]["DATA"][3,6]
479487
hexpids = hexpids.join.hex.to_s(2).rjust(32, '0').split('') # Array of 1s and 0s
@@ -494,6 +502,7 @@ def get_vinfo_supported_pids(bus, srcId, dstId)
494502
# @return [String] VIN as ASCII
495503
def get_vin(bus, srcId, dstId)
496504
packets = get_vehicle_info(bus, srcId, dstId, 0x02)
505+
return "" if packets == nil
497506
return "UDS ERR: #{packets["error"]}" if packets.has_key? "error"
498507
data = response_hash_to_data_array(dstId.to_s(16), packets)
499508
return "" if data == nil
@@ -510,6 +519,7 @@ def get_vin(bus, srcId, dstId)
510519
# @return [String] Calibration ID as ASCII
511520
def get_calibration_id(bus, srcId, dstId)
512521
packets = get_vehicle_info(bus, srcId, dstId, 0x04)
522+
return "" if packets == nil
513523
return "UDS ERR: #{packets["error"]}" if packets.has_key? "error"
514524
data = response_hash_to_data_array(dstId.to_s(16), packets)
515525
return "" if data == nil
@@ -526,6 +536,7 @@ def get_calibration_id(bus, srcId, dstId)
526536
# @return [String] ECU Name as ASCII
527537
def get_ecu_name(bus, srcId, dstId)
528538
packets = get_vehicle_info(bus, srcId, dstId, 0x0A)
539+
return "" if packets == nil
529540
return "UDS ERR: #{packets["error"]}" if packets.has_key? "error"
530541
data = response_hash_to_data_array(dstId.to_s(16), packets)
531542
return "" if data == nil
@@ -634,6 +645,7 @@ def read_data_by_id(bus, srcId, dstId, id, show_error=false)
634645
opt = {}
635646
opt["MAXPKTS"] = 15
636647
packets = client.automotive.send_isotp_and_wait_for_response(bus,srcId, dstId, [0x22] + id, opt)
648+
return [] if packets == nil
637649
if packets.has_key? "error"
638650
return packets if show_error
639651
else
@@ -669,6 +681,7 @@ def get_security_token(bus, srcId, dstId, level=1)
669681
opt={}
670682
opt["MAXPKTS"]=1
671683
packets = client.automotive.send_isotp_and_wait_for_response(bus,srcId, dstId, [0x27, level], opt)
684+
return {} if packets == nil
672685
if not packets.has_key? "error"
673686
packets["SEED"] = response_hash_to_data_array(dstId, packets)
674687
end

tools/hardware/elm327_relay.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def send_cmd(cmd)
134134
def connect_to_device()
135135
@ser = SerialPort.new(self.serial_port, self.serial_baud, self.serial_bits, self.serial_stop_bits, SerialPort::NONE)
136136
resp = send_cmd("ATZ") # Turn off ECHO
137+
#if resp =~ /ELM327/
137138
if resp =~ /ELM327/
138139
send_cmd("ATE0") # Turn off ECHO
139140
send_cmd("ATL0") # Disble linefeeds
@@ -144,6 +145,9 @@ def connect_to_device()
144145
else
145146
$stdout.puts("Connected but invalid ELM response: #{resp.inspect}")
146147
@operational_status = 2
148+
# Down the road we may make a way to re-init via the hwbridge but for now just exit
149+
$stdout.puts("The device may not have been fully initialized, try reconnecting")
150+
exit(-1)
147151
end
148152
@ser
149153
end

0 commit comments

Comments
 (0)