use dlms_cosem_kit::axdr::Data;
use dlms_cosem_kit::codec::Decode;
use dlms_cosem_kit::xdlms::Apdu;
const SAMPLES: &[(&str, &str)] = &[
("a GET request for 1-0:1.8.0*255 attribute 2", "C0 01 C1 00 03 01 00 01 08 00 FF 02 00"),
("the answer: a 32-bit register value", "C4 01 C1 00 06 00 BC 61 4E"),
("a refusal: the association may not read it", "C4 01 C1 01 03"),
("the same GET, protected with the global key set", "C8 0A 30 00 00 00 01 AA BB CC DD EE"),
("an exception response naming a counter error", "D8 01 06 00 00 00 2A"),
("a value with a tag nobody defines", "C4 01 C1 00 0B"),
];
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
if args.is_empty() {
for (what, hex) in SAMPLES {
println!("── {what}");
report(hex);
println!();
}
println!("pass your own hex as arguments to decode it.");
return;
}
for hex in &args {
report(hex);
}
}
fn report(hex: &str) {
let bytes = match unhex(hex) {
Some(b) => b,
None => {
println!(" not hexadecimal: {hex}");
return;
}
};
println!(" {} bytes: {}", bytes.len(), pretty(&bytes));
match Apdu::from_bytes(&bytes) {
Ok(apdu) => {
println!(" tag {:?} ({:#04x})", apdu.tag(), apdu.tag().as_u8());
match apdu {
Apdu::Ciphered { protection, service, body } => {
println!(" protected: {protection:?}, carrying {service:?}");
println!(" suite {}", body.security_control.suite());
println!(" authenticated {}", body.security_control.authenticated());
println!(" encrypted {}", body.security_control.encrypted());
println!(" counter {}", body.invocation_counter);
println!(" payload {} bytes, needs a key", body.payload.len());
}
Apdu::GetResponse(r) => println!(" {r:?}"),
Apdu::ExceptionResponse(e) => {
println!(" state: {:?}", e.state_error);
println!(" service: {:?}", e.service_error);
if let Some(c) = e.expected_invocation_counter {
println!(" the sender expects counter {c} next");
}
}
other => println!(" {other:?}"),
}
}
Err(apdu_error) => match Data::from_bytes(&bytes) {
Ok(value) => println!(" not an APDU, but a value: {value:?}"),
Err(_) => println!(" {apdu_error}"),
},
}
}
fn unhex(s: &str) -> Option<Vec<u8>> {
let digits: Vec<u8> = s.bytes().filter(|b| !b.is_ascii_whitespace() && *b != b':').collect();
if digits.is_empty() || digits.len() % 2 != 0 {
return None;
}
digits
.chunks_exact(2)
.map(|pair| {
let hi = (pair[0] as char).to_digit(16)?;
let lo = (pair[1] as char).to_digit(16)?;
Some((hi * 16 + lo) as u8)
})
.collect()
}
fn pretty(bytes: &[u8]) -> String {
let mut out = String::new();
for (i, b) in bytes.iter().enumerate() {
if i == 24 {
out.push('…');
break;
}
if i > 0 {
out.push(' ');
}
out.push_str(&format!("{b:02X}"));
}
out
}