use crate::acse::{Aare, Aarq, Rlre, Rlrq};
use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};
use super::access::{AccessRequest, AccessResponse};
use super::error::{ConfirmedServiceError, ExceptionResponse};
use super::gbt::GeneralBlockTransfer;
use super::initiate::{InitiateRequest, InitiateResponse};
use super::notify::{DataNotification, EventNotification};
use super::protection::{CipheredService, GeneralCiphering, GeneralGloCiphering, GeneralSigning};
use super::service::{ActionRequest, ActionResponse, GetRequest, GetResponse, SetRequest, SetResponse};
#[cfg(feature = "sn")]
use super::sn::{
InformationReportRequest, ReadRequest, ReadResponse, UnconfirmedWriteRequest, WriteRequest, WriteResponse,
};
use super::tag::{ApduTag, Protection};
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum Apdu<'a> {
Aarq(Aarq<'a>),
Aare(Aare<'a>),
Rlrq(Rlrq<'a>),
Rlre(Rlre<'a>),
InitiateRequest(InitiateRequest<'a>),
InitiateResponse(InitiateResponse),
GetRequest(GetRequest<'a>),
GetResponse(GetResponse<'a>),
SetRequest(SetRequest<'a>),
SetResponse(SetResponse<'a>),
ActionRequest(ActionRequest<'a>),
ActionResponse(ActionResponse<'a>),
AccessRequest(AccessRequest<'a>),
AccessResponse(AccessResponse<'a>),
DataNotification(DataNotification<'a>),
EventNotification(EventNotification<'a>),
#[cfg(feature = "sn")]
ReadRequest(ReadRequest<'a>),
#[cfg(feature = "sn")]
#[allow(missing_docs)]
ReadResponse(ReadResponse<'a>),
#[cfg(feature = "sn")]
#[allow(missing_docs)]
WriteRequest(WriteRequest<'a>),
#[cfg(feature = "sn")]
#[allow(missing_docs)]
WriteResponse(WriteResponse<'a>),
#[cfg(feature = "sn")]
#[allow(missing_docs)]
UnconfirmedWriteRequest(UnconfirmedWriteRequest<'a>),
#[cfg(feature = "sn")]
#[allow(missing_docs)]
InformationReportRequest(InformationReportRequest<'a>),
ExceptionResponse(ExceptionResponse),
ConfirmedServiceError(ConfirmedServiceError),
GeneralBlockTransfer(GeneralBlockTransfer<'a>),
Ciphered {
protection: Protection,
service: ApduTag,
body: CipheredService<'a>,
},
GeneralCiphered {
protection: Protection,
body: GeneralGloCiphering<'a>,
},
GeneralCiphering(GeneralCiphering<'a>),
GeneralSigning(GeneralSigning<'a>),
Gateway {
response: bool,
network_id: u8,
physical_device_address: &'a [u8],
payload: &'a [u8],
},
}
impl Apdu<'_> {
#[must_use]
pub const fn tag(&self) -> ApduTag {
match self {
Self::Aarq(_) => ApduTag::Aarq,
Self::Aare(_) => ApduTag::Aare,
Self::Rlrq(_) => ApduTag::ReleaseRequest,
Self::Rlre(_) => ApduTag::ReleaseResponse,
Self::InitiateRequest(_) => ApduTag::InitiateRequest,
Self::InitiateResponse(_) => ApduTag::InitiateResponse,
Self::GetRequest(_) => ApduTag::GetRequest,
Self::GetResponse(_) => ApduTag::GetResponse,
Self::SetRequest(_) => ApduTag::SetRequest,
Self::SetResponse(_) => ApduTag::SetResponse,
Self::ActionRequest(_) => ApduTag::ActionRequest,
Self::ActionResponse(_) => ApduTag::ActionResponse,
Self::AccessRequest(_) => ApduTag::AccessRequest,
Self::AccessResponse(_) => ApduTag::AccessResponse,
Self::DataNotification(_) => ApduTag::DataNotification,
Self::EventNotification(_) => ApduTag::EventNotificationRequest,
#[cfg(feature = "sn")]
Self::ReadRequest(_) => ApduTag::ReadRequest,
#[cfg(feature = "sn")]
Self::ReadResponse(_) => ApduTag::ReadResponse,
#[cfg(feature = "sn")]
Self::WriteRequest(_) => ApduTag::WriteRequest,
#[cfg(feature = "sn")]
Self::WriteResponse(_) => ApduTag::WriteResponse,
#[cfg(feature = "sn")]
Self::UnconfirmedWriteRequest(_) => ApduTag::UnconfirmedWriteRequest,
#[cfg(feature = "sn")]
Self::InformationReportRequest(_) => ApduTag::InformationReportRequest,
Self::ExceptionResponse(_) => ApduTag::ExceptionResponse,
Self::ConfirmedServiceError(_) => ApduTag::ConfirmedServiceError,
Self::GeneralBlockTransfer(_) => ApduTag::GeneralBlockTransfer,
Self::Ciphered { protection, service, .. } => match service.protected_as(*protection) {
Some(t) => t,
None => *service,
},
Self::GeneralCiphered { protection, .. } => match protection {
Protection::Dedicated => ApduTag::GeneralDedCiphering,
_ => ApduTag::GeneralGloCiphering,
},
Self::GeneralCiphering(_) => ApduTag::GeneralCiphering,
Self::GeneralSigning(_) => ApduTag::GeneralSigning,
Self::Gateway { response: true, .. } => ApduTag::GatewayResponse,
Self::Gateway { response: false, .. } => ApduTag::GatewayRequest,
}
}
#[must_use]
pub const fn is_protected(&self) -> bool {
matches!(
self,
Self::Ciphered { .. }
| Self::GeneralCiphered { .. }
| Self::GeneralCiphering(_)
| Self::GeneralSigning(_)
)
}
}
impl Encode for Apdu<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
match self {
Self::Aarq(v) => return v.encode(w),
Self::Aare(v) => return v.encode(w),
Self::Rlrq(v) => return v.encode(w),
Self::Rlre(v) => return v.encode(w),
Self::Ciphered { protection, service, .. } if service.protected_as(*protection).is_none() => {
return Err(crate::codec::Error::new(ErrorKind::InvalidValue, w.written()));
}
_ => {}
}
w.write_u8(self.tag().as_u8())?;
match self {
Self::Aarq(_) | Self::Aare(_) | Self::Rlrq(_) | Self::Rlre(_) => {
Err(crate::codec::Error::new(ErrorKind::InvalidValue, w.written()))
}
Self::InitiateRequest(v) => v.encode(w),
Self::InitiateResponse(v) => v.encode(w),
Self::GetRequest(v) => v.encode(w),
Self::GetResponse(v) => v.encode(w),
Self::SetRequest(v) => v.encode(w),
Self::SetResponse(v) => v.encode(w),
Self::ActionRequest(v) => v.encode(w),
Self::ActionResponse(v) => v.encode(w),
Self::AccessRequest(v) => v.encode(w),
Self::AccessResponse(v) => v.encode(w),
Self::DataNotification(v) => v.encode(w),
Self::EventNotification(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::ReadRequest(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::ReadResponse(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::WriteRequest(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::WriteResponse(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::UnconfirmedWriteRequest(v) => v.encode(w),
#[cfg(feature = "sn")]
Self::InformationReportRequest(v) => v.encode(w),
Self::ExceptionResponse(v) => v.encode(w),
Self::ConfirmedServiceError(v) => v.encode(w),
Self::GeneralBlockTransfer(v) => v.encode(w),
Self::Ciphered { body, .. } => body.encode(w),
Self::GeneralCiphered { body, .. } => body.encode(w),
Self::GeneralCiphering(v) => v.encode(w),
Self::GeneralSigning(v) => v.encode(w),
Self::Gateway { network_id, physical_device_address, payload, .. } => {
w.write_u8(*network_id)?;
w.write_length_prefixed(physical_device_address)?;
w.write_bytes(payload)
}
}
}
}
impl<'a> Decode<'a> for Apdu<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
let tag_byte = r.peek_u8()?;
let tag = ApduTag::from_u8(tag_byte).ok_or_else(|| r.err(ErrorKind::InvalidTag(tag_byte)))?;
match tag {
ApduTag::Aarq => return Ok(Self::Aarq(Aarq::decode(r)?)),
ApduTag::Aare => return Ok(Self::Aare(Aare::decode(r)?)),
ApduTag::ReleaseRequest => return Ok(Self::Rlrq(Rlrq::decode(r)?)),
ApduTag::ReleaseResponse => return Ok(Self::Rlre(Rlre::decode(r)?)),
_ => {}
}
r.skip(1)?;
if let Some((protection, service)) = tag.unprotect() {
return Ok(Self::Ciphered { protection, service, body: CipheredService::decode(r)? });
}
Ok(match tag {
ApduTag::InitiateRequest => Self::InitiateRequest(InitiateRequest::decode(r)?),
ApduTag::InitiateResponse => Self::InitiateResponse(InitiateResponse::decode(r)?),
ApduTag::GetRequest => Self::GetRequest(GetRequest::decode(r)?),
ApduTag::GetResponse => Self::GetResponse(GetResponse::decode(r)?),
ApduTag::SetRequest => Self::SetRequest(SetRequest::decode(r)?),
ApduTag::SetResponse => Self::SetResponse(SetResponse::decode(r)?),
ApduTag::ActionRequest => Self::ActionRequest(ActionRequest::decode(r)?),
ApduTag::ActionResponse => Self::ActionResponse(ActionResponse::decode(r)?),
ApduTag::AccessRequest => Self::AccessRequest(AccessRequest::decode(r)?),
ApduTag::AccessResponse => Self::AccessResponse(AccessResponse::decode(r)?),
ApduTag::DataNotification => Self::DataNotification(DataNotification::decode(r)?),
ApduTag::EventNotificationRequest => Self::EventNotification(EventNotification::decode(r)?),
#[cfg(feature = "sn")]
ApduTag::ReadRequest => Self::ReadRequest(ReadRequest::decode(r)?),
#[cfg(feature = "sn")]
ApduTag::ReadResponse => Self::ReadResponse(ReadResponse::decode(r)?),
#[cfg(feature = "sn")]
ApduTag::WriteRequest => Self::WriteRequest(WriteRequest::decode(r)?),
#[cfg(feature = "sn")]
ApduTag::WriteResponse => Self::WriteResponse(WriteResponse::decode(r)?),
#[cfg(feature = "sn")]
ApduTag::UnconfirmedWriteRequest => {
Self::UnconfirmedWriteRequest(UnconfirmedWriteRequest::decode(r)?)
}
#[cfg(feature = "sn")]
ApduTag::InformationReportRequest => {
Self::InformationReportRequest(InformationReportRequest::decode(r)?)
}
ApduTag::ExceptionResponse => Self::ExceptionResponse(ExceptionResponse::decode(r)?),
ApduTag::ConfirmedServiceError => Self::ConfirmedServiceError(ConfirmedServiceError::decode(r)?),
ApduTag::GeneralBlockTransfer => Self::GeneralBlockTransfer(GeneralBlockTransfer::decode(r)?),
ApduTag::GeneralGloCiphering => Self::GeneralCiphered {
protection: Protection::Global,
body: GeneralGloCiphering::decode(r)?,
},
ApduTag::GeneralDedCiphering => Self::GeneralCiphered {
protection: Protection::Dedicated,
body: GeneralGloCiphering::decode(r)?,
},
ApduTag::GeneralCiphering => Self::GeneralCiphering(GeneralCiphering::decode(r)?),
ApduTag::GeneralSigning => Self::GeneralSigning(GeneralSigning::decode(r)?),
ApduTag::GatewayRequest | ApduTag::GatewayResponse => Self::Gateway {
response: tag == ApduTag::GatewayResponse,
network_id: r.u8()?,
physical_device_address: r.length_prefixed()?,
payload: r.take_rest(),
},
_ => return Err(r.err_back(ErrorKind::InvalidTag(tag_byte), 1)),
})
}
}