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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::{{DiagServerResult, DiagnosticServer}, DiagError, dtc::{DTCFormatType, DTCStatus, DTC}};
use super::{KWP2000Command, KWP2000Error, Kwp2000DiagnosticServer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DTCRange {
Powertrain,
Chassis,
Body,
Network,
All,
}
impl DTCRange {
pub(crate) fn to_args(&self, pid: u8) -> [u8; 3] {
match self {
DTCRange::Powertrain => [pid, 0x00, 0x00],
DTCRange::Chassis => [pid, 0x40, 0x00],
DTCRange::Body => [pid, 0x80, 0x00],
DTCRange::Network => [pid, 0xC0, 0x00],
DTCRange::All => [pid, 0xFF, 0x00],
}
}
}
const KWP_DTC_FMT: DTCFormatType = crate::dtc::DTCFormatType::ISO15031_6;
pub fn read_stored_dtcs_iso15031(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<Vec<DTC>> {
let mut res = server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0x00),
)?;
if res.len() < 5 {
return Ok(Vec::new());
}
let num_dtcs = res[2];
res.drain(0..3);
if res.len() % 3 != 0 {
return Err(DiagError::InvalidResponseLength);
}
let mut ret: Vec<DTC> = Vec::with_capacity(num_dtcs as usize);
for x in (0..res.len()).step_by(3) {
let status = res[x + 2];
ret.push(DTC {
format: KWP_DTC_FMT,
raw: (res[x] as u32) << 8 | res[x + 1] as u32,
status: DTCStatus::from_kwp_status(status),
mil_on: status & 0b10000000 != 0,
readiness_flag: status & 0b00010000 != 0,
})
}
Ok(ret)
}
pub fn read_supported_dtcs_iso15031(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<Vec<DTC>> {
let mut res: Vec<DTC> = Vec::new();
loop {
let res_bytes = server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0x01),
)?;
println!("RES: {:02X?}", res_bytes);
match read_extended_supported_dtcs(server, range) {
Ok(x) => {
if x == 0 {
break;
}
}
Err(_) => break,
}
}
Ok(res)
}
pub fn read_stored_dtcs(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<Vec<DTC>> {
let mut res = server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0x02),
)?;
if res.len() < 5 {
return Ok(Vec::new());
}
let num_dtcs = res[1];
res.drain(0..2);
if res.len() % 3 != 0 {
return Err(DiagError::InvalidResponseLength);
}
let mut ret: Vec<DTC> = Vec::with_capacity(num_dtcs as usize);
for x in (0..res.len()).step_by(3) {
let status = res[x + 2];
ret.push(DTC {
format: KWP_DTC_FMT,
raw: (res[x] as u32) << 8 | res[x + 1] as u32,
status: DTCStatus::from_kwp_status(status),
mil_on: status & 0b10000000 != 0,
readiness_flag: status & 0b00010000 != 0,
})
}
Ok(ret)
}
pub fn read_supported_dtcs(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<Vec<DTC>> {
let mut res: Vec<DTC> = Vec::new();
loop {
let mut res_bytes = server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0x03),
)?;
if res_bytes.len() < 5 {
return Ok(Vec::new());
}
res_bytes.drain(0..2);
if res_bytes.len() % 3 != 0 {
return Err(DiagError::InvalidResponseLength);
}
for x in (0..res_bytes.len()).step_by(3) {
let status = res_bytes[x + 2];
res.push(DTC {
format: KWP_DTC_FMT,
raw: (res_bytes[x] as u32) << 8 | res_bytes[x + 1] as u32,
status: DTCStatus::from_kwp_status(status),
mil_on: status & 0b10000000 != 0,
readiness_flag: status & 0b00010000 != 0,
})
}
match read_extended_supported_dtcs(server, range) {
Ok(x) => {
if x == 0 || x as usize == res.len() {
return Ok(res);
}
}
Err(_) => return Ok(res),
}
}
}
pub fn get_most_recent_dtc(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<Option<DTC>> {
let req = server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0x04),
)?;
todo!("ECU Response: {:02X?}", req)
}
pub fn read_extended_supported_dtcs(
server: &mut Kwp2000DiagnosticServer,
range: DTCRange,
) -> DiagServerResult<u16> {
match server.execute_command_with_response(
KWP2000Command::ReadDiagnosticTroubleCodesByStatus,
&range.to_args(0xE0),
) {
Ok(x) => {
if x.len() == 3 {
Ok((x[1] as u16) << 8 | x[2] as u16)
} else {
Ok(0)
}
}
Err(e) => {
if let DiagError::ECUError(err) = e {
if KWP2000Error::from(err) == KWP2000Error::SubFunctionNotSupportedInvalidFormat {
Ok(0)
} else {
Err(e)
}
} else {
return Err(e);
}
}
}
}