Skip to content

Commit

Permalink
0.96.1 - Fix panic when payload len is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
rnd-ash committed Aug 27, 2024
1 parent ba4b56d commit e73bd74
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ecu_diagnostics"
version = "0.96.0"
version = "0.96.1"
authors = ["Ashcon Mohseninia <[email protected]>"]
edition = "2021"
description = "A rust crate for ECU diagnostic servers and communication APIs"
Expand Down
6 changes: 3 additions & 3 deletions src/dynamic_diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ where
/// Name of the diagnostic protocol
fn get_protocol_name(&self) -> &'static str;
/// Process a byte array into a command
fn process_req_payload(&self, payload: &[u8]) -> DiagAction;
fn process_req_payload(&self, payload: &[u8]) -> Option<DiagAction>;
/// Creates a session mod req message
fn make_session_control_msg(&self, mode: &DiagSessionMode) -> Vec<u8>;
/// Generate the tester present message (If required)
Expand Down Expand Up @@ -321,7 +321,7 @@ impl DynamicDiagSession {
do_cmd = true;
let mut tx_addr = basic_opts.send_id;
match protocol.process_req_payload(&req.payload) {
DiagAction::SetSessionMode(mode) => {
Some(DiagAction::SetSessionMode(mode)) => {
let mut needs_response = true;
let mut ext_id = None;
let res = send_recv_ecu_req::<P, NRC, L>(
Expand All @@ -347,7 +347,7 @@ impl DynamicDiagSession {
}
tx_resp.send(res);
}
DiagAction::EcuReset => {
Some(DiagAction::EcuReset) => {
let res = send_recv_ecu_req::<P, NRC, L>(
tx_addr,
rx_addr,
Expand Down
49 changes: 27 additions & 22 deletions src/kwp2000/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pub use automotive_diag::kwp2000::*;
use std::collections::HashMap;

use crate::dynamic_diag::{self, DiagAction, DiagPayload, DiagSessionMode};
use crate::{dynamic_diag::{self, DiagAction, DiagPayload, DiagSessionMode}, DiagServerResult};

mod clear_diagnostic_information;
mod ecu_reset;
Expand Down Expand Up @@ -102,28 +102,33 @@ impl Default for Kwp2000Protocol {
}

impl dynamic_diag::DiagProtocol<KwpErrorByte> for Kwp2000Protocol {
fn process_req_payload(&self, payload: &[u8]) -> DiagAction {
if matches!(
KwpCommand::try_from(payload[0]),
Ok(KwpCommand::StartDiagnosticSession)
) {
DiagAction::SetSessionMode(
self.session_modes
.get(&payload[1])
.unwrap_or(&DiagSessionMode {
id: payload[1],
tp_require: true,
name: format!("Unkown(0x{:02X?})", payload[1]),
})
.clone(),
)
} else if matches!(KwpCommand::try_from(payload[0]), Ok(KwpCommand::ECUReset)) {
DiagAction::EcuReset
} else {
DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
fn process_req_payload(&self, payload: &[u8]) -> Option<DiagAction> {
if payload.len() > 0 {
if matches!(
KwpCommand::try_from(payload[0]),
Ok(KwpCommand::StartDiagnosticSession)
) {
Some(DiagAction::SetSessionMode(
self.session_modes
.get(&payload[1])
.unwrap_or(&DiagSessionMode {
id: payload[1],
tp_require: true,
name: format!("Unkown(0x{:02X?})", payload[1]),
})
.clone(),
))
} else if matches!(KwpCommand::try_from(payload[0]), Ok(KwpCommand::ECUReset)) {
Some(DiagAction::EcuReset)
} else {
Some(DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
})
}
} else {
log::warn!("UDS Tx payload is empty");
None
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/obd2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ impl DiagProtocol<Obd2ErrorByte> for OBD2Protocol {
"OBD2(CAN)"
}

fn process_req_payload(&self, payload: &[u8]) -> DiagAction {
DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
fn process_req_payload(&self, payload: &[u8]) -> Option<DiagAction> {
if payload.len() > 0 {
Some(DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
})
} else {
log::warn!("OBD2 Tx payload is empty");
None
}
}

Expand Down
49 changes: 27 additions & 22 deletions src/uds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Theoretically, this module should be compliant with any ECU which implements
//! UDS (Typically any ECU produced after 2006 supports this)

use crate::dynamic_diag::{DiagAction, DiagPayload, DiagProtocol, DiagSessionMode, EcuNRC};
use crate::{dynamic_diag::{DiagAction, DiagPayload, DiagProtocol, DiagSessionMode, EcuNRC}, DiagServerResult};
use std::collections::HashMap;

mod access_timing_parameter;
Expand Down Expand Up @@ -112,29 +112,34 @@ impl DiagProtocol<ByteWrapper<UdsError>> for UDSProtocol {
"UDS"
}

fn process_req_payload(&self, payload: &[u8]) -> DiagAction {
let pid = UdsCommandByte::from(payload[0]);
if matches!(
pid,
UdsCommandByte::Standard(UdsCommand::DiagnosticSessionControl)
) {
let mode = self
.session_modes
.get(&payload[1])
.unwrap_or(&DiagSessionMode {
id: payload[1],
tp_require: true,
name: format!("Unknown (0x{:02X?})", payload[1]),
fn process_req_payload(&self, payload: &[u8]) -> Option<DiagAction> {
if payload.len() > 0 {
let pid = UdsCommandByte::from(payload[0]);
if matches!(
pid,
UdsCommandByte::Standard(UdsCommand::DiagnosticSessionControl)
) {
let mode = self
.session_modes
.get(&payload[1])
.unwrap_or(&DiagSessionMode {
id: payload[1],
tp_require: true,
name: format!("Unknown (0x{:02X?})", payload[1]),
})
.clone();
Some(DiagAction::SetSessionMode(mode))
} else if matches!(pid, UdsCommandByte::Standard(UdsCommand::ECUReset)) {
Some(DiagAction::EcuReset)
} else {
Some(DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
})
.clone();
DiagAction::SetSessionMode(mode)
} else if matches!(pid, UdsCommandByte::Standard(UdsCommand::ECUReset)) {
DiagAction::EcuReset
} else {
DiagAction::Other {
sid: payload[0],
data: payload[1..].to_vec(),
}
} else {
log::warn!("UDS Tx payload is empty");
None
}
}

Expand Down

0 comments on commit e73bd74

Please sign in to comment.