Skip to main content

Session

Struct Session 

Source
pub struct Session<A: Adapter> { /* private fields */ }
Expand description

The primary entry point for all OBD-II operations.

A Session wraps an Adapter and provides high-level methods for reading PIDs, DTCs, identifying vehicles, and more.

§Example

use obd2_core::adapter::mock::MockAdapter;
use obd2_core::session::Session;
use obd2_core::protocol::pid::Pid;

let adapter = MockAdapter::new();
let mut session = Session::new(adapter);
let profile = session.identify_vehicle().await?;
let rpm = session.read_pid(Pid::ENGINE_RPM).await?;

Implementations§

Source§

impl<A: Adapter> Session<A>

Source

pub fn new(adapter: A) -> Self

Create a new Session with default embedded specs.

Source

pub fn load_spec(&mut self, path: &Path) -> Result<(), Obd2Error>

Load a vehicle spec from a YAML file.

Source

pub fn load_spec_dir(&mut self, dir: &Path) -> Result<usize, Obd2Error>

Load all specs from a directory.

Source

pub fn specs(&self) -> &SpecRegistry

Access the spec registry.

Source

pub async fn read_pid(&mut self, pid: Pid) -> Result<Reading, Obd2Error>

Read a single standard PID.

Source

pub async fn read_pids( &mut self, pids: &[Pid], ) -> Result<Vec<(Pid, Reading)>, Obd2Error>

Read multiple standard PIDs in sequence.

Source

pub async fn supported_pids(&mut self) -> Result<HashSet<Pid>, Obd2Error>

Query which standard PIDs this vehicle supports.

Source

pub async fn read_dtcs(&mut self) -> Result<Vec<Dtc>, Obd2Error>

Read stored (confirmed) DTCs via broadcast.

Source

pub async fn read_pending_dtcs(&mut self) -> Result<Vec<Dtc>, Obd2Error>

Read pending DTCs (Mode 07).

Source

pub async fn read_permanent_dtcs(&mut self) -> Result<Vec<Dtc>, Obd2Error>

Read permanent DTCs (Mode 0A).

Source

pub async fn clear_dtcs(&mut self) -> Result<(), Obd2Error>

Clear all DTCs and reset monitors (broadcast).

Source

pub async fn read_vin(&mut self) -> Result<String, Obd2Error>

Read VIN (17 characters).

Source

pub async fn identify_vehicle(&mut self) -> Result<VehicleProfile, Obd2Error>

Identify vehicle: read VIN, decode offline, match spec.

Populates the VehicleProfile with:

  • Offline VIN decode (manufacturer, year, vehicle class)
  • Matched vehicle spec (if any)
  • Supported standard PIDs
Source

pub async fn read_enhanced( &mut self, did: u16, module: ModuleId, ) -> Result<Reading, Obd2Error>

Read an enhanced PID from a specific module.

Source

pub fn module_pids(&self, module: ModuleId) -> Vec<&EnhancedPid>

List enhanced PIDs available for a module (from matched spec).

Source

pub async fn read_o2_monitoring( &mut self, test_id: u8, ) -> Result<Vec<O2TestResult>, Obd2Error>

Read O2 sensor monitoring test results for a specific TID.

Source

pub async fn read_all_o2_monitoring( &mut self, ) -> Result<Vec<O2TestResult>, Obd2Error>

Read all O2 sensor monitoring tests (TIDs 0x01-0x09).

Source

pub async fn read_j1939_pgn(&mut self, pgn: Pgn) -> Result<Vec<u8>, Obd2Error>

Read a J1939 Parameter Group from a heavy-duty vehicle.

Sends a CAN 29-bit request for the specified PGN and returns the raw response bytes. Use the decoder functions in crate::protocol::j1939 to parse the response.

Requires an ELM327/STN adapter on a J1939-capable vehicle (CAN 29-bit 250 kbps).

§Example
let data = session.read_j1939_pgn(Pgn::EEC1).await.unwrap();
if let Some(eec1) = decode_eec1(&data) {
    if let (Some(rpm), Some(torque)) = (eec1.engine_rpm, eec1.actual_torque_pct) {
        println!("RPM: {rpm:.0}, Torque: {torque:.0}%");
    }
}
Source

pub async fn read_j1939_dtcs(&mut self) -> Result<Vec<J1939Dtc>, Obd2Error>

Read and decode J1939 active DTCs (DM1 — PGN 65226).

Returns J1939-format DTCs (SPN + FMI), distinct from OBD-II P-codes.

Source

pub fn evaluate_threshold( &self, pid: Pid, value: f64, ) -> Option<ThresholdResult>

Evaluate a standard PID reading against the matched spec’s thresholds.

Returns None if the value is in normal range, no threshold is defined for this PID, or no spec is matched.

§Example
if let Some(result) = session.evaluate_threshold(Pid::COOLANT_TEMP, 110.0) {
    match result.level {
        AlertLevel::Warning => eprintln!("Warning: {}", result.message),
        AlertLevel::Critical => eprintln!("CRITICAL: {}", result.message),
        AlertLevel::Normal => {}
    }
}
Source

pub fn evaluate_enhanced_threshold( &self, did: u16, value: f64, ) -> Option<ThresholdResult>

Evaluate an enhanced PID (DID) reading against the matched spec’s thresholds.

Source

pub fn vehicle(&self) -> Option<&VehicleProfile>

Current vehicle profile (after identify_vehicle()).

Source

pub fn spec(&self) -> Option<&VehicleSpec>

Matched spec (shorthand).

Source

pub fn adapter_info(&self) -> &AdapterInfo

Adapter info.

Source

pub async fn battery_voltage(&mut self) -> Result<Option<f64>, Obd2Error>

Battery voltage.

Source

pub async fn raw_request( &mut self, service: u8, data: &[u8], target: Target, ) -> Result<Vec<u8>, Obd2Error>

Raw service request (escape hatch).

Trait Implementations§

Source§

impl<A: Adapter> Debug for Session<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Session<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for Session<A>
where A: RefUnwindSafe,

§

impl<A> Send for Session<A>

§

impl<A> Sync for Session<A>
where A: Sync,

§

impl<A> Unpin for Session<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for Session<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for Session<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more