device_driver/
command.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use core::marker::PhantomData;

use crate::FieldSet;

/// A trait to represent the interface to the device.
///
/// This is called to dispatch commands.
pub trait CommandInterface {
    /// The error type
    type Error;
    /// The address type used by this interface. Should likely be an integer.
    type AddressType: Copy;

    /// Dispatch a command on the device by sending the command.
    ///
    /// The input is the content that needs to be sent to the device.
    /// The output is the buffer where the response needs to be written to.
    ///
    /// The slices are empty if the respective in or out fields are not specified.
    fn dispatch_command(
        &mut self,
        address: Self::AddressType,
        size_bits_in: u32,
        input: &[u8],
        size_bits_out: u32,
        output: &mut [u8],
    ) -> Result<(), Self::Error>;
}

/// A trait to represent the interface to the device.
///
/// This is called to asynchronously dispatch commands.
pub trait AsyncCommandInterface {
    /// The error type
    type Error;
    /// The address type used by this interface. Should likely be an integer.
    type AddressType: Copy;

    /// Dispatch a command on the device by sending the command.
    ///
    /// The input is the content that needs to be sent to the device.
    /// The output is the buffer where the response needs to be written to.
    ///
    /// The slices are empty if the respective in or out fields are not specified.
    async fn dispatch_command(
        &mut self,
        address: Self::AddressType,
        size_bits_in: u32,
        input: &[u8],
        size_bits_out: u32,
        output: &mut [u8],
    ) -> Result<(), Self::Error>;
}

/// Intermediate type for doing command operations
pub struct CommandOperation<'i, Interface, AddressType: Copy, InFieldSet, OutFieldSet> {
    interface: &'i mut Interface,
    address: AddressType,
    _phantom: PhantomData<(InFieldSet, OutFieldSet)>,
}

impl<'i, Interface, AddressType: Copy, InFieldSet, OutFieldSet>
    CommandOperation<'i, Interface, AddressType, InFieldSet, OutFieldSet>
{
    #[doc(hidden)]
    pub fn new(interface: &'i mut Interface, address: AddressType) -> Self {
        Self {
            interface,
            address,
            _phantom: PhantomData,
        }
    }
}

/// Simple command
impl<Interface, AddressType: Copy> CommandOperation<'_, Interface, AddressType, (), ()>
where
    Interface: CommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub fn dispatch(self) -> Result<(), Interface::Error> {
        self.interface
            .dispatch_command(self.address, 0, &[], 0, &mut [])
    }
}

/// Only input
impl<Interface, AddressType: Copy, InFieldSet: FieldSet>
    CommandOperation<'_, Interface, AddressType, InFieldSet, ()>
where
    Interface: CommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub fn dispatch(self, f: impl FnOnce(&mut InFieldSet)) -> Result<(), Interface::Error> {
        let mut in_fields = InFieldSet::new_with_zero();
        f(&mut in_fields);

        self.interface.dispatch_command(
            self.address,
            InFieldSet::SIZE_BITS,
            InFieldSet::BUFFER::from(in_fields).as_ref(),
            0,
            &mut [],
        )
    }
}

/// Only output
impl<Interface, AddressType: Copy, OutFieldSet: FieldSet>
    CommandOperation<'_, Interface, AddressType, (), OutFieldSet>
where
    Interface: CommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub fn dispatch(self) -> Result<OutFieldSet, Interface::Error> {
        let mut buffer = OutFieldSet::BUFFER::from(OutFieldSet::new_with_zero());

        self.interface.dispatch_command(
            self.address,
            0,
            &[],
            OutFieldSet::SIZE_BITS,
            buffer.as_mut(),
        )?;

        Ok(buffer.into())
    }
}

/// Input and output
impl<Interface, AddressType: Copy, InFieldSet: FieldSet, OutFieldSet: FieldSet>
    CommandOperation<'_, Interface, AddressType, InFieldSet, OutFieldSet>
where
    Interface: CommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub fn dispatch(
        self,
        f: impl FnOnce(&mut InFieldSet),
    ) -> Result<OutFieldSet, Interface::Error> {
        let mut in_fields = InFieldSet::new_with_zero();
        f(&mut in_fields);

        let mut buffer = OutFieldSet::BUFFER::from(OutFieldSet::new_with_zero());

        self.interface.dispatch_command(
            self.address,
            InFieldSet::SIZE_BITS,
            InFieldSet::BUFFER::from(in_fields).as_ref(),
            OutFieldSet::SIZE_BITS,
            buffer.as_mut(),
        )?;

        Ok(buffer.into())
    }
}

/// Simple command async
impl<'i, Interface, AddressType: Copy> CommandOperation<'i, Interface, AddressType, (), ()>
where
    Interface: AsyncCommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub async fn dispatch_async(self) -> Result<(), Interface::Error> {
        self.interface
            .dispatch_command(self.address, 0, &[], 0, &mut [])
            .await
    }
}

/// Only input async
impl<'i, Interface, AddressType: Copy, InFieldSet: FieldSet>
    CommandOperation<'i, Interface, AddressType, InFieldSet, ()>
where
    Interface: AsyncCommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub async fn dispatch_async(
        self,
        f: impl FnOnce(&mut InFieldSet),
    ) -> Result<(), Interface::Error> {
        let mut in_fields = InFieldSet::new_with_zero();
        f(&mut in_fields);

        self.interface
            .dispatch_command(
                self.address,
                InFieldSet::SIZE_BITS,
                InFieldSet::BUFFER::from(in_fields).as_ref(),
                0,
                &mut [],
            )
            .await
    }
}

/// Only output async
impl<'i, Interface, AddressType: Copy, OutFieldSet: FieldSet>
    CommandOperation<'i, Interface, AddressType, (), OutFieldSet>
where
    Interface: AsyncCommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub async fn dispatch_async(self) -> Result<OutFieldSet, Interface::Error> {
        let mut buffer = OutFieldSet::BUFFER::from(OutFieldSet::new_with_zero());

        self.interface
            .dispatch_command(
                self.address,
                0,
                &[],
                OutFieldSet::SIZE_BITS,
                buffer.as_mut(),
            )
            .await?;

        Ok(buffer.into())
    }
}

/// Input and output async
impl<'i, Interface, AddressType: Copy, InFieldSet: FieldSet, OutFieldSet: FieldSet>
    CommandOperation<'i, Interface, AddressType, InFieldSet, OutFieldSet>
where
    Interface: AsyncCommandInterface<AddressType = AddressType>,
{
    /// Dispatch the command to the device
    pub async fn dispatch_async(
        self,
        f: impl FnOnce(&mut InFieldSet),
    ) -> Result<OutFieldSet, Interface::Error> {
        let mut in_fields = InFieldSet::new_with_zero();
        f(&mut in_fields);

        let mut buffer = OutFieldSet::BUFFER::from(OutFieldSet::new_with_zero());

        self.interface
            .dispatch_command(
                self.address,
                InFieldSet::SIZE_BITS,
                InFieldSet::BUFFER::from(in_fields).as_ref(),
                OutFieldSet::SIZE_BITS,
                buffer.as_mut(),
            )
            .await?;

        Ok(buffer.into())
    }
}