1use 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#[derive(Debug, Clone, Copy, PartialEq)]
46pub enum GetRequest<'a> {
47 Normal {
49 invoke_id: InvokeId,
51 descriptor: AttributeDescriptor,
53 access: Option<SelectiveAccess<'a>>,
55 },
56 Next {
58 invoke_id: InvokeId,
60 block_number: u32,
62 },
63 WithList {
65 invoke_id: InvokeId,
67 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#[derive(Debug, Clone, Copy, PartialEq)]
114pub enum GetResponse<'a> {
115 Normal {
117 invoke_id: InvokeId,
119 result: GetDataResult<'a>,
121 },
122 WithDataBlock {
124 invoke_id: InvokeId,
126 block: DataBlockG<'a>,
128 },
129 WithList {
131 invoke_id: InvokeId,
133 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#[derive(Debug, Clone, Copy, PartialEq)]
175pub enum SetRequest<'a> {
176 Normal {
178 invoke_id: InvokeId,
180 descriptor: AttributeDescriptor,
182 access: Option<SelectiveAccess<'a>>,
184 value: Data<'a>,
186 },
187 WithFirstDataBlock {
189 invoke_id: InvokeId,
191 descriptor: AttributeDescriptor,
193 access: Option<SelectiveAccess<'a>>,
195 block: DataBlockSA<'a>,
197 },
198 WithDataBlock {
200 invoke_id: InvokeId,
202 block: DataBlockSA<'a>,
204 },
205 WithList {
207 invoke_id: InvokeId,
209 descriptors: List<'a, AttributeDescriptorWithSelection<'a>>,
211 values: List<'a, Data<'a>>,
213 },
214 WithListAndFirstDataBlock {
216 invoke_id: InvokeId,
218 descriptors: List<'a, AttributeDescriptorWithSelection<'a>>,
220 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#[derive(Debug, Clone, Copy, PartialEq)]
294pub enum SetResponse<'a> {
295 Normal {
297 invoke_id: InvokeId,
299 result: DataAccessResult,
301 },
302 DataBlock {
304 invoke_id: InvokeId,
306 block_number: u32,
308 },
309 LastDataBlock {
311 invoke_id: InvokeId,
313 result: DataAccessResult,
315 block_number: u32,
317 },
318 LastDataBlockWithList {
320 invoke_id: InvokeId,
322 results: List<'a, DataAccessResult>,
324 block_number: u32,
326 },
327 WithList {
329 invoke_id: InvokeId,
331 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#[derive(Debug, Clone, Copy, PartialEq)]
391pub struct ActionResponseWithOptionalData<'a> {
392 pub result: ActionResult,
394 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#[derive(Debug, Clone, Copy, PartialEq)]
413pub enum ActionRequest<'a> {
414 Normal {
416 invoke_id: InvokeId,
418 descriptor: MethodDescriptor,
420 parameters: Option<Data<'a>>,
422 },
423 NextPblock {
425 invoke_id: InvokeId,
427 block_number: u32,
429 },
430 WithList {
432 invoke_id: InvokeId,
434 descriptors: List<'a, MethodDescriptor>,
436 parameters: List<'a, Data<'a>>,
438 },
439 WithFirstPblock {
441 invoke_id: InvokeId,
443 descriptor: MethodDescriptor,
445 block: DataBlockSA<'a>,
447 },
448 WithListAndFirstPblock {
450 invoke_id: InvokeId,
452 descriptors: List<'a, MethodDescriptor>,
454 block: DataBlockSA<'a>,
456 },
457 WithPblock {
459 invoke_id: InvokeId,
461 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#[derive(Debug, Clone, Copy, PartialEq)]
537pub enum ActionResponse<'a> {
538 Normal {
540 invoke_id: InvokeId,
542 response: ActionResponseWithOptionalData<'a>,
544 },
545 WithPblock {
547 invoke_id: InvokeId,
549 block: DataBlockSA<'a>,
551 },
552 WithList {
554 invoke_id: InvokeId,
556 responses: List<'a, ActionResponseWithOptionalData<'a>>,
558 },
559 NextPblock {
561 invoke_id: InvokeId,
563 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}