obd2_core/adapter/mod.rs
1//! Adapter trait and built-in implementations.
2//!
3//! An adapter interprets OBD-II diagnostic requests over a transport.
4//! For example, the ELM327 adapter translates service requests into
5//! AT commands and hex strings, while a raw CAN adapter would frame
6//! differently.
7
8use std::collections::HashSet;
9use async_trait::async_trait;
10use crate::error::Obd2Error;
11use crate::protocol::pid::Pid;
12use crate::protocol::service::ServiceRequest;
13
14pub mod detect;
15pub mod elm327;
16pub mod mock;
17
18/// Protocol interpreter for OBD-II communication.
19///
20/// Translates high-level diagnostic requests (read PID, read DTCs)
21/// into adapter-specific commands and parses the responses.
22#[async_trait]
23pub trait Adapter: Send {
24 /// Initialize the adapter: reset, detect chipset, configure protocol.
25 /// Returns information about the detected adapter hardware.
26 async fn initialize(&mut self) -> Result<AdapterInfo, Obd2Error>;
27
28 /// Send a diagnostic service request and return the raw response data bytes.
29 /// Response should NOT include the service ID echo or padding — just data.
30 async fn request(&mut self, req: &ServiceRequest) -> Result<Vec<u8>, Obd2Error>;
31
32 /// Query which standard PIDs are supported (Mode 01 PID 00/20/40/60 bitmaps).
33 async fn supported_pids(&mut self) -> Result<HashSet<Pid>, Obd2Error>;
34
35 /// Read the adapter's battery voltage measurement (if supported).
36 async fn battery_voltage(&mut self) -> Result<Option<f64>, Obd2Error>;
37
38 /// Return adapter information detected during initialization.
39 fn info(&self) -> &AdapterInfo;
40}
41
42/// Information about the connected OBD-II adapter hardware.
43#[derive(Debug, Clone)]
44pub struct AdapterInfo {
45 /// Detected chipset type.
46 pub chipset: Chipset,
47 /// Firmware version string from adapter.
48 pub firmware: String,
49 /// Detected OBD-II protocol.
50 pub protocol: crate::vehicle::Protocol,
51 /// Adapter capabilities.
52 pub capabilities: Capabilities,
53}
54
55/// Known OBD-II adapter chipset types.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum Chipset {
58 /// ELM327 clone (v1.0-v1.5) — limited features.
59 Elm327Clone,
60 /// Genuine ELM327 (v2.0+) — full feature set.
61 Elm327Genuine,
62 /// STN chip (STN1110, STN2120) — enhanced features.
63 Stn,
64 /// Unknown or undetected chipset.
65 Unknown,
66}
67
68/// Adapter hardware capabilities.
69#[derive(Debug, Clone, Default)]
70pub struct Capabilities {
71 /// Can clear DTCs (Mode 04).
72 pub can_clear_dtcs: bool,
73 /// Supports dual CAN buses.
74 pub dual_can: bool,
75 /// Supports enhanced diagnostics (Mode 22, etc.).
76 pub enhanced_diag: bool,
77 /// Can read battery voltage (AT RV).
78 pub battery_voltage: bool,
79 /// Supports adaptive timing (AT AT).
80 pub adaptive_timing: bool,
81}
82
83