dlms_cosem_kit/xdlms/
error.rs1use crate::codec::{Decode, Encode, Reader, Result, Writer};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum StateError {
9 ServiceNotAllowed,
11 ServiceUnknown,
13 Other(u8),
15}
16
17impl StateError {
18 #[must_use]
20 pub const fn from_u8(v: u8) -> Self {
21 match v {
22 1 => Self::ServiceNotAllowed,
23 2 => Self::ServiceUnknown,
24 other => Self::Other(other),
25 }
26 }
27
28 #[must_use]
30 pub const fn as_u8(self) -> u8 {
31 match self {
32 Self::ServiceNotAllowed => 1,
33 Self::ServiceUnknown => 2,
34 Self::Other(v) => v,
35 }
36 }
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41#[non_exhaustive]
42pub enum ServiceError {
43 OperationNotPossible,
45 ServiceNotSupported,
47 OtherReason,
49 PduTooLong,
51 DecipheringError,
53 InvocationCounterError,
55 Other(u8),
57}
58
59impl ServiceError {
60 #[must_use]
62 pub const fn from_u8(v: u8) -> Self {
63 match v {
64 1 => Self::OperationNotPossible,
65 2 => Self::ServiceNotSupported,
66 3 => Self::OtherReason,
67 4 => Self::PduTooLong,
68 5 => Self::DecipheringError,
69 6 => Self::InvocationCounterError,
70 other => Self::Other(other),
71 }
72 }
73
74 #[must_use]
76 pub const fn as_u8(self) -> u8 {
77 match self {
78 Self::OperationNotPossible => 1,
79 Self::ServiceNotSupported => 2,
80 Self::OtherReason => 3,
81 Self::PduTooLong => 4,
82 Self::DecipheringError => 5,
83 Self::InvocationCounterError => 6,
84 Self::Other(v) => v,
85 }
86 }
87}
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub struct ExceptionResponse {
92 pub state_error: StateError,
94 pub service_error: ServiceError,
96 pub expected_invocation_counter: Option<u32>,
102}
103
104impl Encode for ExceptionResponse {
105 fn encode(&self, w: &mut dyn Writer) -> Result<()> {
106 w.write_u8(self.state_error.as_u8())?;
107 w.write_u8(self.service_error.as_u8())?;
108 if let Some(ic) = self.expected_invocation_counter {
109 w.write_u32(ic)?;
110 }
111 Ok(())
112 }
113}
114
115impl<'a> Decode<'a> for ExceptionResponse {
116 fn decode(r: &mut Reader<'a>) -> Result<Self> {
117 let state_error = StateError::from_u8(r.u8()?);
118 let service_error = ServiceError::from_u8(r.u8()?);
119 let expected_invocation_counter =
122 if service_error == ServiceError::InvocationCounterError && r.remaining() >= 4 {
123 Some(r.u32()?)
124 } else {
125 None
126 };
127 Ok(Self { state_error, service_error, expected_invocation_counter })
128 }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub struct ConfirmedServiceError {
134 pub service: u8,
136 pub error_class: u8,
138 pub error_code: u8,
140}
141
142impl Encode for ConfirmedServiceError {
143 fn encode(&self, w: &mut dyn Writer) -> Result<()> {
144 w.write_bytes(&[self.service, self.error_class, self.error_code])
145 }
146
147 fn encoded_len(&self) -> usize {
148 3
149 }
150}
151
152impl<'a> Decode<'a> for ConfirmedServiceError {
153 fn decode(r: &mut Reader<'a>) -> Result<Self> {
154 Ok(Self { service: r.u8()?, error_class: r.u8()?, error_code: r.u8()? })
155 }
156}