1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Module for OBD (ISO-9141)

use crate::dynamic_diag::{DiagAction, DiagPayload, DiagProtocol, DiagSessionMode, EcuNRC};

pub use automotive_diag::obd2::*;
use automotive_diag::ByteWrapper;
use std::collections::HashMap;

mod data_pids;
mod enumerations;
mod service01;
mod service09;
mod units;

// Exports
pub use data_pids::*;
pub use enumerations::*;
pub use service01::*;
pub use service09::*;
pub use units::*;

/// Function to decode PID support response from ECU
pub(crate) fn decode_pid_response(x: &[u8]) -> Vec<bool> {
    let mut resp: Vec<bool> = Vec::new();
    for b in x {
        let mut mask: u8 = 0b10000000;
        for _ in 0..8 {
            resp.push(b & mask != 0x00);
            mask >>= 1;
        }
    }
    resp
}

impl EcuNRC for Obd2ErrorByte {
    fn desc(&self) -> String {
        format!("{self:02X?}")
    }

    fn is_ecu_busy(&self) -> bool {
        matches!(self, ByteWrapper::Standard(Obd2Error::BusyResponsePending))
    }

    fn is_wrong_diag_mode(&self) -> bool {
        self.is_not_supported()
    }

    fn is_repeat_request(&self) -> bool {
        matches!(self, ByteWrapper::Standard(Obd2Error::BusyRepeatRequest))
    }
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// OBD2 diagnostic protocol
pub struct OBD2Protocol {}

impl DiagProtocol<Obd2ErrorByte> for OBD2Protocol {
    fn get_basic_session_mode(&self) -> Option<DiagSessionMode> {
        None
    }

    fn get_protocol_name(&self) -> &'static str {
        "OBD2(CAN)"
    }

    fn process_req_payload(&self, payload: &[u8]) -> DiagAction {
        DiagAction::Other {
            sid: payload[0],
            data: payload[1..].to_vec(),
        }
    }

    fn create_tp_msg(_response_required: bool) -> DiagPayload {
        DiagPayload::new(0x00, &[]) // Ignored
    }

    fn make_session_control_msg(&self, _mode: &DiagSessionMode) -> Vec<u8> {
        vec![]
    }

    fn process_ecu_response(r: &[u8]) -> Result<Vec<u8>, (u8, Obd2ErrorByte)> {
        match r[0] {
            0x7F => Err((r[2], r[2].into())),
            _ => Ok(r.to_vec()),
        }
    }

    fn get_diagnostic_session_list(&self) -> HashMap<u8, DiagSessionMode> {
        HashMap::new()
    }

    fn register_session_type(&mut self, _session: DiagSessionMode) {}
}