1use crate::{error::Error, Service, utils};
5
6#[repr(u16)]
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
9pub enum DataIdentifier {
10 VehicleManufacturerSpecific(u16),
11 NetworkConfigurationDataForTractorTrailerApplication(u16),
12 IdentificationOptionVehicleManufacturerSpecific(u16),
13 BootSoftwareIdentification = 0xF180,
14 ApplicationSoftwareIdentification = 0xF181,
15 ApplicationDataIdentification = 0xF182,
16 BootSoftwareFingerprint = 0xF183,
17 ApplicationSoftwareFingerprint = 0xF184,
18 ApplicationDataFingerprint = 0xF185,
19 ActiveDiagnosticSession = 0xF186,
20 VehicleManufacturerSparePartNumber = 0xF187,
21 VehicleManufacturerECUSoftwareNumber = 0xF188,
22 VehicleManufacturerECUSoftwareVersionNumber = 0xF189,
23 SystemSupplierIdentifier = 0xF18A,
24 ECUManufacturingDate = 0xF18B,
25 ECUSerialNumber = 0xF18C,
26 SupportedFunctionalUnits = 0xF18D,
27 VehicleManufacturerKitAssemblyPartNumber = 0xF18E,
28 ISOSAEReservedStandardized = 0xF18F,
29 VIN = 0xF190,
30 VehicleManufacturerECUHardwareNumber = 0xF191,
31 SystemSupplierECUHardwareNumber = 0xF192,
32 SystemSupplierECUHardwareVersionNumber = 0xF193,
33 SystemSupplierECUSoftwareNumber = 0xF194,
34 SystemSupplierECUSoftwareVersionNumber = 0xF195,
35 ExhaustRegulationOrTypeApprovalNumber = 0xF196,
36 SystemNameOrEngineType = 0xF197,
37 RepairShopCodeOrTesterSerialNumber = 0xF198,
38 ProgrammingDate = 0xF199,
39 CalibrationRepairShopCodeOrCalibrationEquipmentSerialNumber = 0xF19A,
40 CalibrationDate = 0xF19B,
41 CalibrationEquipmentSoftwareNumber = 0xF19C,
42 ECUInstallationDate = 0xF19D,
43 ODXFile = 0xF19E,
44 Entity = 0xF19F,
45 IdentificationOptionSystemSupplierSpecific(u16),
46 Periodic(u16),
47 DynamicallyDefined(u16),
48 OBD(u16),
49 OBDMonitor(u16),
50 OBDInfoType(u16),
51 Tachograph(u16),
52 AirbagDeployment(u16),
53 NumberOfEDRDevices = 0xFA10,
54 EDRIdentification = 0xFA11,
55 EDRDeviceAddressInformation = 0xFA12,
56 EDREntries(u16),
57 SafetySystem(u16),
58 SystemSupplierSpecific(u16),
59 UDSVersion = 0xFF00,
60 Reserved(u16),
61 }
63
64impl From<u16> for DataIdentifier {
65 fn from(value: u16) -> Self {
66 match value {
67 0x0100..=0xA5FF |
68 0xA800..=0xACFF |
69 0xB000..=0xB1FF |
70 0xC000..=0xC2FF |
71 0xCF00..=0xEFFF |
72 0xF010..=0xF0FF => Self::VehicleManufacturerSpecific(value),
73 0xF000..=0xF00F => Self::NetworkConfigurationDataForTractorTrailerApplication(value),
74 0xF100..=0xF17F |
75 0xF1A0..=0xF1EF => Self::IdentificationOptionVehicleManufacturerSpecific(value),
76 0xF180 => Self::BootSoftwareIdentification,
77 0xF181 => Self::ApplicationSoftwareIdentification,
78 0xF182 => Self::ApplicationDataIdentification,
79 0xF183 => Self::BootSoftwareFingerprint,
80 0xF184 => Self::ApplicationSoftwareFingerprint,
81 0xF185 => Self::ApplicationDataFingerprint,
82 0xF186 => Self::ActiveDiagnosticSession,
83 0xF187 => Self::VehicleManufacturerSparePartNumber,
84 0xF188 => Self::VehicleManufacturerECUSoftwareNumber,
85 0xF189 => Self::VehicleManufacturerECUSoftwareVersionNumber,
86 0xF18A => Self::SystemSupplierIdentifier,
87 0xF18B => Self::ECUManufacturingDate,
88 0xF18C => Self::ECUSerialNumber,
89 0xF18D => Self::SupportedFunctionalUnits,
90 0xF18E => Self::VehicleManufacturerKitAssemblyPartNumber,
91 0xF18F => Self::ISOSAEReservedStandardized,
92 0xF190 => Self::VIN,
93 0xF191 => Self::VehicleManufacturerECUHardwareNumber,
94 0xF192 => Self::SystemSupplierECUHardwareNumber,
95 0xF193 => Self::SystemSupplierECUHardwareVersionNumber,
96 0xF194 => Self::SystemSupplierECUSoftwareNumber,
97 0xF195 => Self::SystemSupplierECUSoftwareVersionNumber,
98 0xF196 => Self::ExhaustRegulationOrTypeApprovalNumber,
99 0xF197 => Self::SystemNameOrEngineType,
100 0xF198 => Self::RepairShopCodeOrTesterSerialNumber,
101 0xF199 => Self::ProgrammingDate,
102 0xF19A => Self::CalibrationRepairShopCodeOrCalibrationEquipmentSerialNumber,
103 0xF19B => Self::CalibrationDate,
104 0xF19C => Self::CalibrationEquipmentSoftwareNumber,
105 0xF19D => Self::ECUInstallationDate,
106 0xF19E => Self::ODXFile,
107 0xF19F => Self::Entity,
108 0xF1F0..=0xF1FF => Self::IdentificationOptionSystemSupplierSpecific(value),
109 0xF200..=0xF2FF => Self::Periodic(value),
110 0xF300..=0xF3FF => Self::DynamicallyDefined(value),
111 0xF400..=0xF5FF |
112 0xF700..=0xF7FF => Self::OBD(value),
113 0xF600..=0xF6FF => Self::OBDMonitor(value),
114 0xF800..=0xF8FF => Self::OBDInfoType(value),
115 0xF900..=0xF9FF => Self::Tachograph(value),
116 0xFA00..=0xFA0F => Self::AirbagDeployment(value),
117 0xFA10 => Self::NumberOfEDRDevices,
118 0xFA11 => Self::EDRIdentification,
119 0xFA12 => Self::EDRDeviceAddressInformation,
120 0xFA13..=0xFA18 => Self::EDREntries(value),
121 0xFA19..=0xFAFF => Self::SafetySystem(value),
122 0xFD00..=0xFEFF => Self::SystemSupplierSpecific(value),
123 0xFF00 => Self::UDSVersion,
124 _ => Self::Reserved(value),
125 }
126 }
127}
128
129impl Into<u16> for DataIdentifier {
130 fn into(self) -> u16 {
131 match self {
132 Self::BootSoftwareIdentification => 0xF180,
133 Self::ApplicationSoftwareIdentification => 0xF181,
134 Self::ApplicationDataIdentification => 0xF182,
135 Self::BootSoftwareFingerprint => 0xF183,
136 Self::ApplicationSoftwareFingerprint => 0xF184,
137 Self::ApplicationDataFingerprint => 0xF185,
138 Self::ActiveDiagnosticSession => 0xF186,
139 Self::VehicleManufacturerSparePartNumber => 0xF187,
140 Self::VehicleManufacturerECUSoftwareNumber => 0xF188,
141 Self::VehicleManufacturerECUSoftwareVersionNumber => 0xF189,
142 Self::SystemSupplierIdentifier => 0xF18A,
143 Self::ECUManufacturingDate => 0xF18B,
144 Self::ECUSerialNumber => 0xF18C,
145 Self::SupportedFunctionalUnits => 0xF18D,
146 Self::VehicleManufacturerKitAssemblyPartNumber => 0xF18E,
147 Self::ISOSAEReservedStandardized => 0xF18F,
148 Self::VIN => 0xF190,
149 Self::VehicleManufacturerECUHardwareNumber => 0xF191,
150 Self::SystemSupplierECUHardwareNumber => 0xF192,
151 Self::SystemSupplierECUHardwareVersionNumber => 0xF193,
152 Self::SystemSupplierECUSoftwareNumber => 0xF194,
153 Self::SystemSupplierECUSoftwareVersionNumber => 0xF195,
154 Self::ExhaustRegulationOrTypeApprovalNumber => 0xF196,
155 Self::SystemNameOrEngineType => 0xF197,
156 Self::RepairShopCodeOrTesterSerialNumber => 0xF198,
157 Self::ProgrammingDate => 0xF199,
158 Self::CalibrationRepairShopCodeOrCalibrationEquipmentSerialNumber => 0xF19A,
159 Self::CalibrationDate => 0xF19B,
160 Self::CalibrationEquipmentSoftwareNumber => 0xF19C,
161 Self::ECUInstallationDate => 0xF19D,
162 Self::ODXFile => 0xF19E,
163 Self::Entity => 0xF19F,
164 Self::VehicleManufacturerSpecific(v) |
165 Self::NetworkConfigurationDataForTractorTrailerApplication(v) |
166 Self::IdentificationOptionVehicleManufacturerSpecific(v) |
167 Self::IdentificationOptionSystemSupplierSpecific(v) |
168 Self::Periodic(v) |
169 Self::DynamicallyDefined(v) |
170 Self::OBD(v) |
171 Self::OBDMonitor(v) |
172 Self::OBDInfoType(v) |
173 Self::Tachograph(v) |
174 Self::AirbagDeployment(v) |
175 Self::EDREntries(v) |
176 Self::SafetySystem(v) |
177 Self::SystemSupplierSpecific(v) => v,
178 Self::NumberOfEDRDevices => 0xFA10,
179 Self::EDRIdentification => 0xFA11,
180 Self::EDRDeviceAddressInformation => 0xFA12,
181 Self::UDSVersion => 0xFF00,
182 Self::Reserved(v) => v,
183 }
184 }
185}
186
187#[derive(Debug, Clone, Eq, PartialEq)]
188pub struct DIDData {
189 pub did: DataIdentifier,
190 pub data: Vec<u8>,
191}
192
193impl<'a> TryFrom<&'a [u8]> for DIDData {
194 type Error = Error;
195 fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
196 let data_len = data.len();
197 utils::data_length_check(data_len, 2, false)?;
198
199 let mut offset = 0;
200 let did = DataIdentifier::from(
201 u16::from_be_bytes([data[offset], data[offset + 1]])
202 );
203 offset += 2;
204
205 Ok(Self { did, data: data[offset..].to_vec() })
206 }
207}
208
209impl Into<Vec<u8>> for DIDData {
210 fn into(mut self) -> Vec<u8> {
211 let did: u16 = self.did.into();
212 let mut result = did.to_be_bytes().to_vec();
213 result.append(&mut self.data);
214
215 result
216 }
217}
218
219