scsir/command/
get_stream_status.rs

1#![allow(dead_code)]
2
3use std::mem::size_of;
4
5use modular_bitfield_msb::prelude::*;
6
7use crate::{
8    data_wrapper::{AnyType, FlexibleStruct},
9    result_data::ResultData,
10    Command, DataDirection, Scsi,
11};
12
13#[derive(Clone, Debug)]
14pub struct GetStreamStatusCommand<'a> {
15    interface: &'a Scsi,
16    command_buffer: CommandBuffer,
17    descriptor_length: u32,
18}
19
20#[derive(Debug)]
21pub struct CommandResult {
22    pub total_descripter_length: usize,
23    pub number_of_open_streams: u16,
24    pub stream_identifiers: Vec<u16>,
25}
26
27impl<'a> GetStreamStatusCommand<'a> {
28    fn new(interface: &'a Scsi) -> Self {
29        Self {
30            interface,
31            descriptor_length: 0,
32            command_buffer: CommandBuffer::new()
33                .with_operation_code(OPERATION_CODE)
34                .with_service_action(SERVICE_ACTION),
35        }
36    }
37
38    pub fn starting_stream_identifier(&mut self, value: u16) -> &mut Self {
39        self.command_buffer.set_starting_stream_identifier(value);
40        self
41    }
42
43    pub fn control(&mut self, value: u8) -> &mut Self {
44        self.command_buffer.set_control(value);
45        self
46    }
47
48    // descriptor length must be less than 268435455(0xFFF_FFFF), which is (0xFFFF_FFFF - 8) / 16
49    pub fn descriptor_length(&mut self, value: u32) -> &mut Self {
50        self.descriptor_length = value;
51        self
52    }
53
54    pub fn issue(&mut self) -> crate::Result<CommandResult> {
55        const MAX_DESCRIPTOR_LENGTH: usize =
56            (u32::MAX as usize - size_of::<ParameterHeader>()) / size_of::<Descriptor>();
57        if self.descriptor_length > MAX_DESCRIPTOR_LENGTH as u32 {
58            return Err(
59                crate::Error::ArgumentOutOfBounds(
60                    format!(
61                        "descriptor length is out of bounds. The maximum possible value is {}, but {} was provided.",
62                        MAX_DESCRIPTOR_LENGTH,
63                        self.descriptor_length)));
64        }
65
66        let temp = ThisCommand {
67            command_buffer: self.command_buffer.with_allocation_length(
68                size_of::<ParameterHeader>() as u32
69                    + self.descriptor_length * size_of::<Descriptor>() as u32,
70            ),
71            max_descriptor_length: self.descriptor_length,
72        };
73
74        self.interface.issue(&temp)
75    }
76}
77
78impl Scsi {
79    pub fn get_stream_status(&self) -> GetStreamStatusCommand {
80        GetStreamStatusCommand::new(self)
81    }
82}
83
84const OPERATION_CODE: u8 = 0x9E;
85const SERVICE_ACTION: u8 = 0x16;
86
87#[bitfield]
88#[derive(Clone, Copy, Debug)]
89struct CommandBuffer {
90    operation_code: B8,
91    reserved_0: B3,
92    service_action: B5,
93    reserved_1: B16,
94    starting_stream_identifier: B16,
95    reserved_2: B32,
96    allocation_length: B32,
97    reserved_3: B8,
98    control: B8,
99}
100
101#[bitfield]
102#[derive(Clone, Copy)]
103struct ParameterHeader {
104    parameter_data_length: B32,
105    reserved: B16,
106    number_of_open_streams: B16,
107}
108
109#[bitfield]
110#[derive(Clone, Copy)]
111struct Descriptor {
112    reserved_0: B16,
113    stream_identifier: B16,
114    reserved_1: B32,
115}
116
117struct ThisCommand {
118    command_buffer: CommandBuffer,
119    max_descriptor_length: u32,
120}
121
122impl Command for ThisCommand {
123    type CommandBuffer = CommandBuffer;
124
125    type DataBuffer = AnyType;
126
127    type DataBufferWrapper = FlexibleStruct<ParameterHeader, Descriptor>;
128
129    type ReturnType = crate::Result<CommandResult>;
130
131    fn direction(&self) -> DataDirection {
132        DataDirection::FromDevice
133    }
134
135    fn command(&self) -> Self::CommandBuffer {
136        self.command_buffer
137    }
138
139    fn data(&self) -> Self::DataBufferWrapper {
140        unsafe { FlexibleStruct::with_length(self.max_descriptor_length as usize) }
141    }
142
143    fn data_size(&self) -> u32 {
144        self.max_descriptor_length * size_of::<Descriptor>() as u32
145            + size_of::<ParameterHeader>() as u32
146    }
147
148    fn process_result(&self, result: ResultData<Self::DataBufferWrapper>) -> Self::ReturnType {
149        result.check_ioctl_error()?;
150        result.check_common_error()?;
151
152        let data = result.data;
153        let length = unsafe { data.body_as_ref() }.parameter_data_length();
154        let length = (length as usize - size_of::<u64>()) / size_of::<Descriptor>();
155
156        let mut stream_identifiers = vec![];
157        for item in unsafe { &data.elements_as_slice()[..usize::min(length, data.length())] } {
158            stream_identifiers.push(item.stream_identifier());
159        }
160
161        Ok(CommandResult {
162            total_descripter_length: length,
163            number_of_open_streams: unsafe { data.body_as_ref() }.number_of_open_streams(),
164            stream_identifiers,
165        })
166    }
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172    use std::mem::size_of;
173
174    const COMMAND_LENGTH: usize = 16;
175    const PARAMETER_HEADER_LENGTH: usize = 8;
176    const DESCRIPTOR_LENGTH: usize = 8;
177
178    #[test]
179    fn layout_test() {
180        assert_eq!(
181            size_of::<CommandBuffer>(),
182            COMMAND_LENGTH,
183            concat!("Size of: ", stringify!(CommandBuffer))
184        );
185
186        assert_eq!(
187            size_of::<ParameterHeader>(),
188            PARAMETER_HEADER_LENGTH,
189            concat!("Size of: ", stringify!(ParameterHeader))
190        );
191
192        assert_eq!(
193            size_of::<Descriptor>(),
194            DESCRIPTOR_LENGTH,
195            concat!("Size of: ", stringify!(Descriptor))
196        );
197    }
198}