Skip to main content

dlms_cosem_kit/xdlms/
service.rs

1//! GET, SET and ACTION — the confirmed services of logical-name referencing.
2
3use crate::axdr::Data;
4use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};
5
6use super::descriptor::{
7    AttributeDescriptor, AttributeDescriptorWithSelection, InvokeId, MethodDescriptor, SelectiveAccess,
8    decode_optional, encode_optional,
9};
10use super::result::{ActionResult, DataAccessResult, DataBlockG, DataBlockSA, GetDataResult, List};
11
12impl Encode for DataAccessResult {
13    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
14        w.write_u8(self.as_u8())
15    }
16
17    fn encoded_len(&self) -> usize {
18        1
19    }
20}
21
22impl<'a> Decode<'a> for DataAccessResult {
23    fn decode(r: &mut Reader<'a>) -> Result<Self> {
24        Ok(Self::from_u8(r.u8()?))
25    }
26}
27
28impl Encode for ActionResult {
29    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
30        w.write_u8(self.as_u8())
31    }
32
33    fn encoded_len(&self) -> usize {
34        1
35    }
36}
37
38impl<'a> Decode<'a> for ActionResult {
39    fn decode(r: &mut Reader<'a>) -> Result<Self> {
40        Ok(Self::from_u8(r.u8()?))
41    }
42}
43
44/// Read an attribute.
45#[derive(Debug, Clone, Copy, PartialEq)]
46pub enum GetRequest<'a> {
47    /// One attribute.
48    Normal {
49        /// Correlates the response.
50        invoke_id: InvokeId,
51        /// Which attribute.
52        descriptor: AttributeDescriptor,
53        /// Which part of it.
54        access: Option<SelectiveAccess<'a>>,
55    },
56    /// The next block of a long response.
57    Next {
58        /// Correlates the response.
59        invoke_id: InvokeId,
60        /// The block just received, which the server continues after.
61        block_number: u32,
62    },
63    /// Several attributes in one request.
64    WithList {
65        /// Correlates the response.
66        invoke_id: InvokeId,
67        /// Which attributes.
68        list: List<'a, AttributeDescriptorWithSelection<'a>>,
69    },
70}
71
72impl Encode for GetRequest<'_> {
73    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
74        match self {
75            Self::Normal { invoke_id, descriptor, access } => {
76                w.write_u8(1)?;
77                w.write_u8(invoke_id.0)?;
78                descriptor.encode(w)?;
79                encode_optional(w, access.as_ref())
80            }
81            Self::Next { invoke_id, block_number } => {
82                w.write_u8(2)?;
83                w.write_u8(invoke_id.0)?;
84                w.write_u32(*block_number)
85            }
86            Self::WithList { invoke_id, list } => {
87                w.write_u8(3)?;
88                w.write_u8(invoke_id.0)?;
89                list.encode(w)
90            }
91        }
92    }
93}
94
95impl<'a> Decode<'a> for GetRequest<'a> {
96    fn decode(r: &mut Reader<'a>) -> Result<Self> {
97        let choice = r.u8()?;
98        let invoke_id = InvokeId(r.u8()?);
99        Ok(match choice {
100            1 => Self::Normal {
101                invoke_id,
102                descriptor: AttributeDescriptor::decode(r)?,
103                access: decode_optional(r)?,
104            },
105            2 => Self::Next { invoke_id, block_number: r.u32()? },
106            3 => Self::WithList { invoke_id, list: List::decode(r)? },
107            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
108        })
109    }
110}
111
112/// The answer to a [`GetRequest`].
113#[derive(Debug, Clone, Copy, PartialEq)]
114pub enum GetResponse<'a> {
115    /// One value.
116    Normal {
117        /// Echoes the request.
118        invoke_id: InvokeId,
119        /// The value, or why there is none.
120        result: GetDataResult<'a>,
121    },
122    /// One block of a value too long for a single APDU.
123    WithDataBlock {
124        /// Echoes the request.
125        invoke_id: InvokeId,
126        /// The block.
127        block: DataBlockG<'a>,
128    },
129    /// Several values.
130    WithList {
131        /// Echoes the request.
132        invoke_id: InvokeId,
133        /// One result per requested attribute, in order.
134        results: List<'a, GetDataResult<'a>>,
135    },
136}
137
138impl Encode for GetResponse<'_> {
139    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
140        match self {
141            Self::Normal { invoke_id, result } => {
142                w.write_u8(1)?;
143                w.write_u8(invoke_id.0)?;
144                result.encode(w)
145            }
146            Self::WithDataBlock { invoke_id, block } => {
147                w.write_u8(2)?;
148                w.write_u8(invoke_id.0)?;
149                block.encode(w)
150            }
151            Self::WithList { invoke_id, results } => {
152                w.write_u8(3)?;
153                w.write_u8(invoke_id.0)?;
154                results.encode(w)
155            }
156        }
157    }
158}
159
160impl<'a> Decode<'a> for GetResponse<'a> {
161    fn decode(r: &mut Reader<'a>) -> Result<Self> {
162        let choice = r.u8()?;
163        let invoke_id = InvokeId(r.u8()?);
164        Ok(match choice {
165            1 => Self::Normal { invoke_id, result: GetDataResult::decode(r)? },
166            2 => Self::WithDataBlock { invoke_id, block: DataBlockG::decode(r)? },
167            3 => Self::WithList { invoke_id, results: List::decode(r)? },
168            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
169        })
170    }
171}
172
173/// Write an attribute.
174#[derive(Debug, Clone, Copy, PartialEq)]
175pub enum SetRequest<'a> {
176    /// One attribute, one value.
177    Normal {
178        /// Correlates the response.
179        invoke_id: InvokeId,
180        /// Which attribute.
181        descriptor: AttributeDescriptor,
182        /// Which part of it.
183        access: Option<SelectiveAccess<'a>>,
184        /// The new value.
185        value: Data<'a>,
186    },
187    /// The first block of a value too long for one APDU.
188    WithFirstDataBlock {
189        /// Correlates the response.
190        invoke_id: InvokeId,
191        /// Which attribute.
192        descriptor: AttributeDescriptor,
193        /// Which part of it.
194        access: Option<SelectiveAccess<'a>>,
195        /// The first block.
196        block: DataBlockSA<'a>,
197    },
198    /// A further block.
199    WithDataBlock {
200        /// Correlates the response.
201        invoke_id: InvokeId,
202        /// The block.
203        block: DataBlockSA<'a>,
204    },
205    /// Several attributes, several values.
206    WithList {
207        /// Correlates the response.
208        invoke_id: InvokeId,
209        /// Which attributes.
210        descriptors: List<'a, AttributeDescriptorWithSelection<'a>>,
211        /// The new values, in the same order.
212        values: List<'a, Data<'a>>,
213    },
214    /// Several attributes whose values start in this block.
215    WithListAndFirstDataBlock {
216        /// Correlates the response.
217        invoke_id: InvokeId,
218        /// Which attributes.
219        descriptors: List<'a, AttributeDescriptorWithSelection<'a>>,
220        /// The first block of the concatenated values.
221        block: DataBlockSA<'a>,
222    },
223}
224
225impl Encode for SetRequest<'_> {
226    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
227        match self {
228            Self::Normal { invoke_id, descriptor, access, value } => {
229                w.write_u8(1)?;
230                w.write_u8(invoke_id.0)?;
231                descriptor.encode(w)?;
232                encode_optional(w, access.as_ref())?;
233                value.encode(w)
234            }
235            Self::WithFirstDataBlock { invoke_id, descriptor, access, block } => {
236                w.write_u8(2)?;
237                w.write_u8(invoke_id.0)?;
238                descriptor.encode(w)?;
239                encode_optional(w, access.as_ref())?;
240                block.encode(w)
241            }
242            Self::WithDataBlock { invoke_id, block } => {
243                w.write_u8(3)?;
244                w.write_u8(invoke_id.0)?;
245                block.encode(w)
246            }
247            Self::WithList { invoke_id, descriptors, values } => {
248                w.write_u8(4)?;
249                w.write_u8(invoke_id.0)?;
250                descriptors.encode(w)?;
251                values.encode(w)
252            }
253            Self::WithListAndFirstDataBlock { invoke_id, descriptors, block } => {
254                w.write_u8(5)?;
255                w.write_u8(invoke_id.0)?;
256                descriptors.encode(w)?;
257                block.encode(w)
258            }
259        }
260    }
261}
262
263impl<'a> Decode<'a> for SetRequest<'a> {
264    fn decode(r: &mut Reader<'a>) -> Result<Self> {
265        let choice = r.u8()?;
266        let invoke_id = InvokeId(r.u8()?);
267        Ok(match choice {
268            1 => Self::Normal {
269                invoke_id,
270                descriptor: AttributeDescriptor::decode(r)?,
271                access: decode_optional(r)?,
272                value: Data::decode(r)?,
273            },
274            2 => Self::WithFirstDataBlock {
275                invoke_id,
276                descriptor: AttributeDescriptor::decode(r)?,
277                access: decode_optional(r)?,
278                block: DataBlockSA::decode(r)?,
279            },
280            3 => Self::WithDataBlock { invoke_id, block: DataBlockSA::decode(r)? },
281            4 => Self::WithList { invoke_id, descriptors: List::decode(r)?, values: List::decode(r)? },
282            5 => Self::WithListAndFirstDataBlock {
283                invoke_id,
284                descriptors: List::decode(r)?,
285                block: DataBlockSA::decode(r)?,
286            },
287            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
288        })
289    }
290}
291
292/// The answer to a [`SetRequest`].
293#[derive(Debug, Clone, Copy, PartialEq)]
294pub enum SetResponse<'a> {
295    /// One result.
296    Normal {
297        /// Echoes the request.
298        invoke_id: InvokeId,
299        /// Whether the write took effect.
300        result: DataAccessResult,
301    },
302    /// A block was accepted; send the next one.
303    DataBlock {
304        /// Echoes the request.
305        invoke_id: InvokeId,
306        /// The block just accepted.
307        block_number: u32,
308    },
309    /// The last block was accepted, and here is the outcome.
310    LastDataBlock {
311        /// Echoes the request.
312        invoke_id: InvokeId,
313        /// Whether the write took effect.
314        result: DataAccessResult,
315        /// The last block number.
316        block_number: u32,
317    },
318    /// The last block of a list write.
319    LastDataBlockWithList {
320        /// Echoes the request.
321        invoke_id: InvokeId,
322        /// One result per attribute.
323        results: List<'a, DataAccessResult>,
324        /// The last block number.
325        block_number: u32,
326    },
327    /// A list write with no blocking.
328    WithList {
329        /// Echoes the request.
330        invoke_id: InvokeId,
331        /// One result per attribute.
332        results: List<'a, DataAccessResult>,
333    },
334}
335
336impl Encode for SetResponse<'_> {
337    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
338        match self {
339            Self::Normal { invoke_id, result } => {
340                w.write_u8(1)?;
341                w.write_u8(invoke_id.0)?;
342                w.write_u8(result.as_u8())
343            }
344            Self::DataBlock { invoke_id, block_number } => {
345                w.write_u8(2)?;
346                w.write_u8(invoke_id.0)?;
347                w.write_u32(*block_number)
348            }
349            Self::LastDataBlock { invoke_id, result, block_number } => {
350                w.write_u8(3)?;
351                w.write_u8(invoke_id.0)?;
352                w.write_u8(result.as_u8())?;
353                w.write_u32(*block_number)
354            }
355            Self::LastDataBlockWithList { invoke_id, results, block_number } => {
356                w.write_u8(4)?;
357                w.write_u8(invoke_id.0)?;
358                results.encode(w)?;
359                w.write_u32(*block_number)
360            }
361            Self::WithList { invoke_id, results } => {
362                w.write_u8(5)?;
363                w.write_u8(invoke_id.0)?;
364                results.encode(w)
365            }
366        }
367    }
368}
369
370impl<'a> Decode<'a> for SetResponse<'a> {
371    fn decode(r: &mut Reader<'a>) -> Result<Self> {
372        let choice = r.u8()?;
373        let invoke_id = InvokeId(r.u8()?);
374        Ok(match choice {
375            1 => Self::Normal { invoke_id, result: DataAccessResult::from_u8(r.u8()?) },
376            2 => Self::DataBlock { invoke_id, block_number: r.u32()? },
377            3 => Self::LastDataBlock {
378                invoke_id,
379                result: DataAccessResult::from_u8(r.u8()?),
380                block_number: r.u32()?,
381            },
382            4 => Self::LastDataBlockWithList { invoke_id, results: List::decode(r)?, block_number: r.u32()? },
383            5 => Self::WithList { invoke_id, results: List::decode(r)? },
384            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
385        })
386    }
387}
388
389/// A method result with its optional return value.
390#[derive(Debug, Clone, Copy, PartialEq)]
391pub struct ActionResponseWithOptionalData<'a> {
392    /// Whether the method ran.
393    pub result: ActionResult,
394    /// What it returned, if anything.
395    pub return_parameters: Option<GetDataResult<'a>>,
396}
397
398impl Encode for ActionResponseWithOptionalData<'_> {
399    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
400        w.write_u8(self.result.as_u8())?;
401        encode_optional(w, self.return_parameters.as_ref())
402    }
403}
404
405impl<'a> Decode<'a> for ActionResponseWithOptionalData<'a> {
406    fn decode(r: &mut Reader<'a>) -> Result<Self> {
407        Ok(Self { result: ActionResult::from_u8(r.u8()?), return_parameters: decode_optional(r)? })
408    }
409}
410
411/// Invoke a method.
412#[derive(Debug, Clone, Copy, PartialEq)]
413pub enum ActionRequest<'a> {
414    /// One method.
415    Normal {
416        /// Correlates the response.
417        invoke_id: InvokeId,
418        /// Which method.
419        descriptor: MethodDescriptor,
420        /// Its parameter, if it takes one.
421        parameters: Option<Data<'a>>,
422    },
423    /// Ask for the next block of a long return value.
424    NextPblock {
425        /// Correlates the response.
426        invoke_id: InvokeId,
427        /// The block just received.
428        block_number: u32,
429    },
430    /// Several methods.
431    WithList {
432        /// Correlates the response.
433        invoke_id: InvokeId,
434        /// Which methods.
435        descriptors: List<'a, MethodDescriptor>,
436        /// Their parameters, in the same order.
437        parameters: List<'a, Data<'a>>,
438    },
439    /// One method whose parameter starts in this block.
440    WithFirstPblock {
441        /// Correlates the response.
442        invoke_id: InvokeId,
443        /// Which method.
444        descriptor: MethodDescriptor,
445        /// The first block of the parameter.
446        block: DataBlockSA<'a>,
447    },
448    /// Several methods whose parameters start in this block.
449    WithListAndFirstPblock {
450        /// Correlates the response.
451        invoke_id: InvokeId,
452        /// Which methods.
453        descriptors: List<'a, MethodDescriptor>,
454        /// The first block of the concatenated parameters.
455        block: DataBlockSA<'a>,
456    },
457    /// A further parameter block.
458    WithPblock {
459        /// Correlates the response.
460        invoke_id: InvokeId,
461        /// The block.
462        block: DataBlockSA<'a>,
463    },
464}
465
466impl Encode for ActionRequest<'_> {
467    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
468        match self {
469            Self::Normal { invoke_id, descriptor, parameters } => {
470                w.write_u8(1)?;
471                w.write_u8(invoke_id.0)?;
472                descriptor.encode(w)?;
473                encode_optional(w, parameters.as_ref())
474            }
475            Self::NextPblock { invoke_id, block_number } => {
476                w.write_u8(2)?;
477                w.write_u8(invoke_id.0)?;
478                w.write_u32(*block_number)
479            }
480            Self::WithList { invoke_id, descriptors, parameters } => {
481                w.write_u8(3)?;
482                w.write_u8(invoke_id.0)?;
483                descriptors.encode(w)?;
484                parameters.encode(w)
485            }
486            Self::WithFirstPblock { invoke_id, descriptor, block } => {
487                w.write_u8(4)?;
488                w.write_u8(invoke_id.0)?;
489                descriptor.encode(w)?;
490                block.encode(w)
491            }
492            Self::WithListAndFirstPblock { invoke_id, descriptors, block } => {
493                w.write_u8(5)?;
494                w.write_u8(invoke_id.0)?;
495                descriptors.encode(w)?;
496                block.encode(w)
497            }
498            Self::WithPblock { invoke_id, block } => {
499                w.write_u8(6)?;
500                w.write_u8(invoke_id.0)?;
501                block.encode(w)
502            }
503        }
504    }
505}
506
507impl<'a> Decode<'a> for ActionRequest<'a> {
508    fn decode(r: &mut Reader<'a>) -> Result<Self> {
509        let choice = r.u8()?;
510        let invoke_id = InvokeId(r.u8()?);
511        Ok(match choice {
512            1 => Self::Normal {
513                invoke_id,
514                descriptor: MethodDescriptor::decode(r)?,
515                parameters: decode_optional(r)?,
516            },
517            2 => Self::NextPblock { invoke_id, block_number: r.u32()? },
518            3 => Self::WithList { invoke_id, descriptors: List::decode(r)?, parameters: List::decode(r)? },
519            4 => Self::WithFirstPblock {
520                invoke_id,
521                descriptor: MethodDescriptor::decode(r)?,
522                block: DataBlockSA::decode(r)?,
523            },
524            5 => Self::WithListAndFirstPblock {
525                invoke_id,
526                descriptors: List::decode(r)?,
527                block: DataBlockSA::decode(r)?,
528            },
529            6 => Self::WithPblock { invoke_id, block: DataBlockSA::decode(r)? },
530            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
531        })
532    }
533}
534
535/// The answer to an [`ActionRequest`].
536#[derive(Debug, Clone, Copy, PartialEq)]
537pub enum ActionResponse<'a> {
538    /// One result.
539    Normal {
540        /// Echoes the request.
541        invoke_id: InvokeId,
542        /// The outcome and any return value.
543        response: ActionResponseWithOptionalData<'a>,
544    },
545    /// One block of a long return value.
546    WithPblock {
547        /// Echoes the request.
548        invoke_id: InvokeId,
549        /// The block.
550        block: DataBlockSA<'a>,
551    },
552    /// Several results.
553    WithList {
554        /// Echoes the request.
555        invoke_id: InvokeId,
556        /// One per method, in order.
557        responses: List<'a, ActionResponseWithOptionalData<'a>>,
558    },
559    /// The next block was requested and here it is.
560    NextPblock {
561        /// Echoes the request.
562        invoke_id: InvokeId,
563        /// The block just sent.
564        block_number: u32,
565    },
566}
567
568impl Encode for ActionResponse<'_> {
569    fn encode(&self, w: &mut dyn Writer) -> Result<()> {
570        match self {
571            Self::Normal { invoke_id, response } => {
572                w.write_u8(1)?;
573                w.write_u8(invoke_id.0)?;
574                response.encode(w)
575            }
576            Self::WithPblock { invoke_id, block } => {
577                w.write_u8(2)?;
578                w.write_u8(invoke_id.0)?;
579                block.encode(w)
580            }
581            Self::WithList { invoke_id, responses } => {
582                w.write_u8(3)?;
583                w.write_u8(invoke_id.0)?;
584                responses.encode(w)
585            }
586            Self::NextPblock { invoke_id, block_number } => {
587                w.write_u8(4)?;
588                w.write_u8(invoke_id.0)?;
589                w.write_u32(*block_number)
590            }
591        }
592    }
593}
594
595impl<'a> Decode<'a> for ActionResponse<'a> {
596    fn decode(r: &mut Reader<'a>) -> Result<Self> {
597        let choice = r.u8()?;
598        let invoke_id = InvokeId(r.u8()?);
599        Ok(match choice {
600            1 => Self::Normal { invoke_id, response: ActionResponseWithOptionalData::decode(r)? },
601            2 => Self::WithPblock { invoke_id, block: DataBlockSA::decode(r)? },
602            3 => Self::WithList { invoke_id, responses: List::decode(r)? },
603            4 => Self::NextPblock { invoke_id, block_number: r.u32()? },
604            other => return Err(r.err_back(ErrorKind::InvalidTag(other), 2)),
605        })
606    }
607}