use crate::obis::ObisCode;
use crate::types::BerError;
pub mod acse;
pub mod action;
pub mod ciphering;
pub mod error;
pub mod gbt;
pub mod general_ciphering;
pub mod get;
pub mod initiate;
pub mod notification;
pub mod set;
pub mod tag {
pub const DATA_NOTIFICATION: u8 = 0x0F;
pub const GET_REQUEST: u8 = 0xC0;
pub const SET_REQUEST: u8 = 0xC1;
pub const EVENT_NOTIFICATION_REQUEST: u8 = 0xC2;
pub const ACTION_REQUEST: u8 = 0xC3;
pub const GET_RESPONSE: u8 = 0xC4;
pub const SET_RESPONSE: u8 = 0xC5;
pub const ACTION_RESPONSE: u8 = 0xC7;
pub const EXCEPTION_RESPONSE: u8 = 0xD8;
pub const CONFIRMED_SERVICE_ERROR: u8 = 0x0E;
}
pub mod data_access_result {
pub const SUCCESS: u8 = 0;
pub const HARDWARE_FAULT: u8 = 1;
pub const TEMPORARY_FAILURE: u8 = 2;
pub const READ_WRITE_DENIED: u8 = 3;
pub const OBJECT_UNDEFINED: u8 = 4;
pub const OBJECT_CLASS_INCONSISTENT: u8 = 9;
pub const OBJECT_UNAVAILABLE: u8 = 11;
pub const TYPE_UNMATCHED: u8 = 12;
pub const SCOPE_OF_ACCESS_VIOLATED: u8 = 13;
pub const DATA_BLOCK_UNAVAILABLE: u8 = 14;
pub const LONG_GET_ABORTED: u8 = 15;
pub const NO_LONG_GET_IN_PROGRESS: u8 = 16;
pub const LONG_SET_ABORTED: u8 = 17;
pub const NO_LONG_SET_IN_PROGRESS: u8 = 18;
pub const DATA_BLOCK_NUMBER_INVALID: u8 = 19;
pub const OTHER_REASON: u8 = 250;
}
#[derive(Debug, PartialEq, Eq)]
pub enum ServiceError {
UnexpectedTag(u8),
UnexpectedType(u8),
Truncated,
InvalidData,
}
impl std::fmt::Display for ServiceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
impl std::error::Error for ServiceError {}
impl From<ServiceError> for std::io::Error {
fn from(e: ServiceError) -> Self {
std::io::Error::new(std::io::ErrorKind::InvalidData, e)
}
}
impl From<BerError> for ServiceError {
fn from(_: BerError) -> Self {
ServiceError::InvalidData
}
}
pub fn invoke_id_and_priority(invoke_id: u8, confirmed: bool, high_priority: bool) -> u8 {
(invoke_id & 0x0F) | if confirmed { 0x40 } else { 0x00 } | if high_priority { 0x80 } else { 0x00 }
}
#[derive(Debug, Clone, PartialEq)]
pub struct AttributeDescriptor {
pub class_id: u16,
pub instance_id: ObisCode,
pub attribute_id: i8,
}
impl AttributeDescriptor {
pub fn new(class_id: u16, instance_id: ObisCode, attribute_id: i8) -> Self {
AttributeDescriptor { class_id, instance_id, attribute_id }
}
pub fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.class_id.to_be_bytes());
buf.extend_from_slice(&self.instance_id.to_bytes());
#[allow(clippy::cast_sign_loss)]
buf.push(self.attribute_id as u8);
}
pub fn decode(bytes: &[u8]) -> Result<(AttributeDescriptor, usize), ServiceError> {
if bytes.len() < 9 {
return Err(ServiceError::Truncated);
}
let class_id = u16::from_be_bytes([bytes[0], bytes[1]]);
let o = &bytes[2..8];
let instance_id = ObisCode::new(o[0], o[1], o[2], o[3], o[4], o[5]);
#[allow(clippy::cast_possible_wrap)]
let attribute_id = bytes[8] as i8;
Ok((AttributeDescriptor { class_id, instance_id, attribute_id }, 9))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct MethodDescriptor {
pub class_id: u16,
pub instance_id: ObisCode,
pub method_id: i8,
}
impl MethodDescriptor {
pub fn new(class_id: u16, instance_id: ObisCode, method_id: i8) -> Self {
MethodDescriptor { class_id, instance_id, method_id }
}
pub fn encode(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.class_id.to_be_bytes());
buf.extend_from_slice(&self.instance_id.to_bytes());
#[allow(clippy::cast_sign_loss)]
buf.push(self.method_id as u8);
}
pub fn decode(bytes: &[u8]) -> Result<(MethodDescriptor, usize), ServiceError> {
if bytes.len() < 9 {
return Err(ServiceError::Truncated);
}
let class_id = u16::from_be_bytes([bytes[0], bytes[1]]);
let o = &bytes[2..8];
let instance_id = ObisCode::new(o[0], o[1], o[2], o[3], o[4], o[5]);
#[allow(clippy::cast_possible_wrap)]
let method_id = bytes[8] as i8;
Ok((MethodDescriptor { class_id, instance_id, method_id }, 9))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataBlockSa {
pub last_block: bool,
pub block_number: u32,
pub raw_data: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawApdu {
pub tag: u8,
pub body: Vec<u8>,
}
impl RawApdu {
pub fn new(tag: u8, body: Vec<u8>) -> Self {
RawApdu { tag, body }
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ServiceError> {
if bytes.is_empty() {
return Err(ServiceError::Truncated);
}
let tag = bytes[0];
let (len, header) = read_length(&bytes[1..])?;
let start = 1 + header;
let body = bytes.get(start..start + len).ok_or(ServiceError::Truncated)?.to_vec();
Ok(RawApdu { tag, body })
}
pub fn encode(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(2 + self.body.len());
buf.push(self.tag);
push_length(self.body.len(), &mut buf);
buf.extend_from_slice(&self.body);
buf
}
pub fn tag(&self) -> u8 {
self.tag
}
pub fn body(&self) -> &[u8] {
&self.body
}
pub fn body_mut(&mut self) -> &mut Vec<u8> {
&mut self.body
}
pub fn into_parts(self) -> (u8, Vec<u8>) {
(self.tag, self.body)
}
}
impl DataBlockSa {
pub fn encode(&self, buf: &mut Vec<u8>) {
buf.push(u8::from(self.last_block));
buf.extend_from_slice(&self.block_number.to_be_bytes());
push_length(self.raw_data.len(), buf);
buf.extend_from_slice(&self.raw_data);
}
pub fn decode(bytes: &[u8]) -> Result<(DataBlockSa, usize), ServiceError> {
let last_block = *bytes.first().ok_or(ServiceError::Truncated)? != 0;
let b = bytes.get(1..5).ok_or(ServiceError::Truncated)?;
let block_number = u32::from_be_bytes([b[0], b[1], b[2], b[3]]);
let (len, header) = read_length(&bytes[5..])?;
let start = 5 + header;
let raw_data = bytes.get(start..start + len).ok_or(ServiceError::Truncated)?.to_vec();
Ok((DataBlockSa { last_block, block_number, raw_data }, start + len))
}
}
#[allow(clippy::cast_possible_truncation)] pub(crate) fn push_length(length: usize, buf: &mut Vec<u8>) {
if length < 128 {
buf.push(length as u8);
} else {
let bytes = (length as u64).to_be_bytes();
let first = bytes.iter().position(|&b| b != 0).unwrap_or(7);
let n = 8 - first;
buf.push(0x80 | n as u8);
buf.extend_from_slice(&bytes[first..]);
}
}
pub(crate) fn read_length(bytes: &[u8]) -> Result<(usize, usize), ServiceError> {
let first = *bytes.first().ok_or(ServiceError::Truncated)?;
if first < 128 {
Ok((first as usize, 1))
} else {
let n = (first & 0x7F) as usize;
let slice = bytes.get(1..1 + n).ok_or(ServiceError::Truncated)?;
let mut len = 0usize;
for &b in slice {
len = (len << 8) | b as usize;
}
Ok((len, 1 + n))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invoke_id_and_priority_bits() {
assert_eq!(invoke_id_and_priority(1, true, true), 0xC1);
assert_eq!(invoke_id_and_priority(0, false, false), 0x00);
}
#[test]
fn attribute_descriptor_round_trips() {
let d = AttributeDescriptor::new(1, ObisCode::new(0, 0, 0x80, 0, 0, 0xFF), 2);
let mut buf = Vec::new();
d.encode(&mut buf);
assert_eq!(buf, vec![0x00, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, 0xFF, 0x02]);
let (decoded, n) = AttributeDescriptor::decode(&buf).unwrap();
assert_eq!(n, 9);
assert_eq!(decoded, d);
}
#[test]
fn method_descriptor_round_trips() {
let d = MethodDescriptor::new(70, ObisCode::new(0, 0, 96, 3, 10, 255), 1);
let mut buf = Vec::new();
d.encode(&mut buf);
let (decoded, n) = MethodDescriptor::decode(&buf).unwrap();
assert_eq!(n, 9);
assert_eq!(decoded, d);
}
#[test]
fn data_block_sa_round_trips() {
let block = DataBlockSa { last_block: true, block_number: 3, raw_data: vec![0x0A; 17] };
let mut buf = Vec::new();
block.encode(&mut buf);
assert_eq!(buf[..6], [0x01, 0x00, 0x00, 0x00, 0x03, 0x11]);
let (decoded, n) = DataBlockSa::decode(&buf).unwrap();
assert_eq!(n, buf.len());
assert_eq!(decoded, block);
}
#[test]
fn raw_apdu_encode_decode_round_trip() {
let raw = RawApdu::new(0xC0, vec![0x01, 0x02, 0x03]);
let encoded = raw.encode();
let decoded = RawApdu::from_bytes(&encoded).unwrap();
assert_eq!(decoded.tag(), 0xC0);
assert_eq!(decoded.body(), &[0x01, 0x02, 0x03]);
}
#[test]
fn raw_apdu_from_bytes_with_length() {
let bytes = vec![0xC1, 0x05, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA];
let raw = RawApdu::from_bytes(&bytes).unwrap();
assert_eq!(raw.tag(), 0xC1);
assert_eq!(raw.body(), &[0xAA; 5]);
}
#[test]
fn raw_apdu_empty_body() {
let raw = RawApdu::new(0xC2, vec![]);
let encoded = raw.encode();
assert_eq!(encoded, vec![0xC2, 0x00]);
let decoded = RawApdu::from_bytes(&encoded).unwrap();
assert!(decoded.body().is_empty());
}
#[test]
fn raw_apdu_truncated_returns_error() {
let bytes = vec![0xC0]; assert!(RawApdu::from_bytes(&bytes).is_err());
}
#[test]
fn raw_apdu_mutable_body() {
let mut raw = RawApdu::new(0xC3, vec![0x01]);
raw.body_mut().push(0x02);
assert_eq!(raw.body(), &[0x01, 0x02]);
}
}