Skip to main content

dlms_cosem_kit/xdlms/
access.rs

1//! The ACCESS service: several operations in one exchange.
2//!
3//! ACCESS batches GET, SET and ACTION into a single request and a single response. It
4//! is the service a battery-powered meter on a low-power network uses, because the cost
5//! there is round trips rather than bytes, and it is what the Green Book's LPWAN
6//! examples are built on.
7
8use crate::axdr::Data;
9use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};
10
11use super::descriptor::{
12    AttributeDescriptor, AttributeDescriptorWithSelection, LongInvokeId, MethodDescriptor, decode_optional,
13    encode_optional,
14};
15use super::notify::OptionalDateTime;
16use super::result::{ActionResult, DataAccessResult, List};
17
18/// The most operations one ACCESS request may batch.
19///
20/// A bound rather than a buffer: a server's outcome list is two bytes an entry, so this
21/// decides a 128-byte array instead of one sized by the PDU. It also decides how much
22/// work a single request can demand, which is worth deciding rather than inheriting from
23/// whatever the PDU size happens to be. Sixty-four operations in one exchange is already
24/// far beyond what a companion profile's use cases ask for.
25pub const MAX_ACCESS_ITEMS: usize = 64;
26
27/// One operation inside an ACCESS request.
28#[derive(Debug, Clone, Copy, PartialEq)]
29pub enum AccessRequestSpecification<'a> {
30    /// Read an attribute.
31    Get(AttributeDescriptor),
32    /// Write an attribute; the value is in the data list.
33    Set(AttributeDescriptor),
34    /// Invoke a method; the parameter is in the data list.
35    Action(MethodDescriptor),
36    /// Read part of an attribute.
37    GetWithSelection(AttributeDescriptorWithSelection<'a>),
38    /// Write part of an attribute.
39    SetWithSelection(AttributeDescriptorWithSelection<'a>),
40}
41
42impl Encode for AccessRequestSpecification<'_> {
43    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
44        match self {
45            Self::Get(d) => {
46                w.write_u8(1)?;
47                d.encode(w)
48            }
49            Self::Set(d) => {
50                w.write_u8(2)?;
51                d.encode(w)
52            }
53            Self::Action(d) => {
54                w.write_u8(3)?;
55                d.encode(w)
56            }
57            Self::GetWithSelection(d) => {
58                w.write_u8(4)?;
59                d.encode(w)
60            }
61            Self::SetWithSelection(d) => {
62                w.write_u8(5)?;
63                d.encode(w)
64            }
65        }
66    }
67}
68
69impl<'a> Decode<'a> for AccessRequestSpecification<'a> {
70    fn decode(r: &mut Reader<'a>) -> Result<Self> {
71        Ok(match r.u8()? {
72            1 => Self::Get(AttributeDescriptor::decode(r)?),
73            2 => Self::Set(AttributeDescriptor::decode(r)?),
74            3 => Self::Action(MethodDescriptor::decode(r)?),
75            4 => Self::GetWithSelection(AttributeDescriptorWithSelection::decode(r)?),
76            5 => Self::SetWithSelection(AttributeDescriptorWithSelection::decode(r)?),
77            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
78        })
79    }
80}
81
82/// The outcome of one operation inside an ACCESS response.
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum AccessResponseSpecification {
85    /// Result of a read.
86    Get(DataAccessResult),
87    /// Result of a write.
88    Set(DataAccessResult),
89    /// Result of a method invocation.
90    Action(ActionResult),
91}
92
93impl Encode for AccessResponseSpecification {
94    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
95        match self {
96            Self::Get(v) => w.write_bytes(&[1, v.as_u8()]),
97            Self::Set(v) => w.write_bytes(&[2, v.as_u8()]),
98            Self::Action(v) => w.write_bytes(&[3, v.as_u8()]),
99        }
100    }
101
102    fn encoded_len(&self) -> usize {
103        2
104    }
105}
106
107impl<'a> Decode<'a> for AccessResponseSpecification {
108    fn decode(r: &mut Reader<'a>) -> Result<Self> {
109        Ok(match r.u8()? {
110            1 => Self::Get(DataAccessResult::from_u8(r.u8()?)),
111            2 => Self::Set(DataAccessResult::from_u8(r.u8()?)),
112            3 => Self::Action(ActionResult::from_u8(r.u8()?)),
113            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
114        })
115    }
116}
117
118/// Several operations in one request.
119#[derive(Debug, Clone, Copy, PartialEq)]
120pub struct AccessRequest<'a> {
121    /// Identifies the exchange.
122    pub long_invoke_id: LongInvokeId,
123    /// When the client sent it, if it said.
124    pub date_time: OptionalDateTime,
125    /// What to do, in order.
126    pub specification: List<'a, AccessRequestSpecification<'a>>,
127    /// The values the SET and ACTION entries consume, in the same order.
128    pub data: List<'a, Data<'a>>,
129}
130
131impl Encode for AccessRequest<'_> {
132    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
133        w.write_u32(self.long_invoke_id.0)?;
134        self.date_time.encode(w)?;
135        self.specification.encode(w)?;
136        self.data.encode(w)
137    }
138}
139
140impl<'a> Decode<'a> for AccessRequest<'a> {
141    fn decode(r: &mut Reader<'a>) -> Result<Self> {
142        Ok(Self {
143            long_invoke_id: LongInvokeId(r.u32()?),
144            date_time: OptionalDateTime::decode(r)?,
145            specification: List::decode(r)?,
146            data: List::decode(r)?,
147        })
148    }
149}
150
151/// The answer to an [`AccessRequest`].
152///
153/// The response echoes the request's specification list before its own results, so a
154/// client that lost track of what it asked can still line the answers up.
155#[derive(Debug, Clone, Copy, PartialEq)]
156pub struct AccessResponse<'a> {
157    /// Echoes the request.
158    pub long_invoke_id: LongInvokeId,
159    /// When the server answered, if it said.
160    pub date_time: OptionalDateTime,
161    /// The request's specification list, echoed.
162    ///
163    /// `OPTIONAL` in the ASN.1 — the only optional field in either ACCESS body — so it
164    /// is preceded by a usage flag on the wire. A codec that writes the list
165    /// unconditionally emits a response every other stack reads one byte out of step.
166    pub request_specification: Option<List<'a, AccessRequestSpecification<'a>>>,
167    /// The values the GET and ACTION entries produced.
168    pub data: List<'a, Data<'a>>,
169    /// One outcome per operation, in order.
170    pub response_specification: List<'a, AccessResponseSpecification>,
171}
172
173impl Encode for AccessResponse<'_> {
174    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
175        w.write_u32(self.long_invoke_id.0)?;
176        self.date_time.encode(w)?;
177        encode_optional(w, self.request_specification.as_ref())?;
178        self.data.encode(w)?;
179        self.response_specification.encode(w)
180    }
181}
182
183impl<'a> Decode<'a> for AccessResponse<'a> {
184    fn decode(r: &mut Reader<'a>) -> Result<Self> {
185        Ok(Self {
186            long_invoke_id: LongInvokeId(r.u32()?),
187            date_time: OptionalDateTime::decode(r)?,
188            request_specification: decode_optional(r)?,
189            data: List::decode(r)?,
190            response_specification: List::decode(r)?,
191        })
192    }
193}