firewire_dice_protocols/tcat/extension/
stream_format_section.rs

1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (c) 2020 Takashi Sakamoto
3
4//! Stream format section in protocol extension defined by TCAT for ASICs of DICE.
5//!
6//! The module includes structure, enumeration, and trait and its implementation for stream format
7//! section in protocol extension defined by TCAT for ASICs of DICE.
8
9use super::{stream_format_entry::*, *};
10
11/// Parameters of entries in stream format section.
12#[derive(Default, Debug, Clone, PartialEq, Eq)]
13pub struct StreamFormatParams {
14    pub tx_entries: Vec<FormatEntry>,
15    pub rx_entries: Vec<FormatEntry>,
16}
17
18impl<O: TcatExtensionOperation> TcatExtensionSectionParamsOperation<StreamFormatParams> for O {
19    fn cache_extension_whole_params(
20        req: &FwReq,
21        node: &FwNode,
22        sections: &ExtensionSections,
23        caps: &ExtensionCaps,
24        params: &mut StreamFormatParams,
25        timeout_ms: u32,
26    ) -> Result<(), Error> {
27        let size = calculate_stream_format_entries_size(
28            caps.general.max_tx_streams as usize,
29            caps.general.max_rx_streams as usize,
30        );
31        let size = std::cmp::min(sections.stream_format.size, size);
32        let mut raw = vec![0u8; size];
33        Self::read_extension(req, node, &sections.stream_format, 0, &mut raw, timeout_ms)?;
34
35        let mut tx_entry_count = 0usize;
36        deserialize_usize(&mut tx_entry_count, &raw[..4]);
37
38        let mut rx_entry_count = 0usize;
39        deserialize_usize(&mut rx_entry_count, &raw[4..8]);
40
41        params
42            .tx_entries
43            .resize_with(tx_entry_count, Default::default);
44        params
45            .rx_entries
46            .resize_with(rx_entry_count, Default::default);
47        deserialize_stream_format_entries((&mut params.tx_entries, &mut params.rx_entries), &raw)
48            .map_err(|e| Error::new(ProtocolExtensionError::StreamFormat, &e.to_string()))
49    }
50}
51
52impl<O: TcatExtensionOperation> TcatExtensionSectionWholeMutableParamsOperation<StreamFormatParams>
53    for O
54{
55    fn update_extension_whole_params(
56        req: &FwReq,
57        node: &FwNode,
58        sections: &ExtensionSections,
59        caps: &ExtensionCaps,
60        params: &StreamFormatParams,
61        timeout_ms: u32,
62    ) -> Result<(), Error> {
63        if caps.general.dynamic_stream_format {
64            let msg = "Stream format configuration is not mutable.";
65            Err(Error::new(ProtocolExtensionError::StreamFormat, &msg))?;
66        }
67
68        let size =
69            calculate_stream_format_entries_size(params.tx_entries.len(), params.rx_entries.len());
70        let size = std::cmp::min(sections.stream_format.size, size);
71        let mut raw = vec![0u8; size];
72        serialize_stream_format_entries((&params.tx_entries, &params.rx_entries), &mut raw)
73            .map_err(|e| Error::new(ProtocolExtensionError::StreamFormat, &e.to_string()))?;
74
75        Self::write_extension(req, node, &sections.stream_format, 0, &mut raw, timeout_ms)
76            .map_err(|e| Error::new(ProtocolExtensionError::StreamFormat, &e.to_string()))
77    }
78}