Skip to content

Commit

Permalink
read_dtc_information_report_number_of_dtc
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Apr 2, 2024
1 parent 47dd748 commit ce4057f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/uds/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum SecurityAccessType {
SendKey = 0x02,
}

/// ROutine Control Sub-Function ID as defined in ISO 14229
/// Routine Control Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
Expand All @@ -131,3 +131,11 @@ pub enum RoutineControlType {
Stop = 0x02,
RequestResults = 0x03,
}

/// Read DTC Information Sub-Function ID as defined in ISO 14229
#[derive(Debug, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum ReportType {
ReportNumberOfDTCByStatusMask = 0x01,
}
29 changes: 29 additions & 0 deletions src/uds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::Result;
use crate::StreamExt;
pub use constants::*;
pub use error::{Error, NegativeResponseCode};
pub use types::*;

use tracing::info;

Expand Down Expand Up @@ -270,6 +271,34 @@ impl<'a> UDSClient<'a> {
Ok(())
}

pub async fn read_dtc_information_report_number_of_dtc(
&self,
mask: u8,
) -> Result<DTCReportNumberByStatusMask> {
let resp = self
.request(
ServiceIdentifier::ReadDTCInformation as u8,
Some(ReportType::ReportNumberOfDTCByStatusMask as u8),
Some(&[mask]),
)
.await?;

if resp.len() != 4 {
return Err(Error::InvalidResponseLength.into());
}

let mask = resp[0];
let format =
DTCFormatIdentifier::from_repr(resp[1]).expect("Unknown DTC Format Identifier");
let count = u16::from_be_bytes([resp[2], resp[3]]);

Ok(DTCReportNumberByStatusMask {
DTCStatusAvailabilityMask: mask,
DTCFormatIdentifier: format,
DTCCount: count,
})
}

/// 0x31 - Routine Control. The `routine_control_type` selects the operation such as Start and Stop, see [`constants::RoutineControlType`]. The `routine_identifier` is a 16-bit identifier for the routine. The `data` parameter is optional and can be used when starting or stopping a routine. The ECU can optionally return data for all routine operations.
pub async fn routine_control(
&self,
Expand Down
23 changes: 23 additions & 0 deletions src/uds/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Types used in the UDS protocol.
use strum_macros::FromRepr;

use std::time::Duration;

Expand All @@ -11,3 +12,25 @@ pub struct SessionParameterRecord {
/// Performance requirement for the server (i.e. the ECU) to start with the response message after the transmission of a "ResponsePending" message.
pub p2_star_server_max: Duration,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, FromRepr)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
#[allow(non_camel_case_types)]
pub enum DTCFormatIdentifier {
SAE_J2012_DA_DTCFormat_00 = 0x00,
ISO_14229_1_DTCFormat = 0x01,
SAE_J1939_73_DTCFormat = 0x02,
ISO_11992_4_DTCFormat = 0x03,
SAE_J2012_DA_DTCFormat_04 = 0x04,
}

/// Struct returned by ReadDTCInformation (0x19)
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(non_snake_case)]
pub struct DTCReportNumberByStatusMask {
pub DTCStatusAvailabilityMask: u8,
pub DTCFormatIdentifier: DTCFormatIdentifier,
pub DTCCount: u16,
}

0 comments on commit ce4057f

Please sign in to comment.