ergot 0.12.0

Eloquence in messaging
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Embassy USB Bulk packet interface
//!
//! This implementation uses USB Bulk endpoints to send data in a framed manner. This uses
//! the convention of non-max sized frames signalling the end of a frame, and using zero length
//! packets (ZLP) to signal the end of a frame when a message happens to be the max size of of the interface.
//!
//! For example on USB FS, where the max packet size is 64, sending a 32 byte message would take up
//! one frame, and we would know it is the "end" of the frame because 32 != 64. Similarly, we could
//! send a 96 byte message as two packets, 64 + 32 bytes, and we would know the second packet ends the
//! frame. Finally, we could send a 128 byte frame as three packets: 64 + 64 + 0, using a ZLP to mark
//! the end of the frame.

use core::{marker::PhantomData, sync::atomic::AtomicU8};

use bbq2::{
    queue::BBQueue,
    traits::{bbqhdl::BbqHandle, notifier::maitake::MaiNotSpsc, storage::Inline},
};
use static_cell::ConstStaticCell;

use crate::interface_manager::{Interface, utils::framed_stream};

/// An Embassy-USB interface implementation
pub struct EmbassyInterface<Q: BbqHandle + 'static> {
    _pd: PhantomData<Q>,
}

/// A small type for handling USB device name enumeration
struct ErgotHandler {}

/// A type alias for the outgoing packet queue typically used by the [`EmbassyInterface`]
pub type Queue<const N: usize, C> = BBQueue<Inline<N>, C, MaiNotSpsc>;
/// A type alias for the InterfaceSink typically used by the [`EmbassyInterface`]
pub type EmbassySink<Q> = framed_stream::Sink<Q>;

/// Interface Implementation
impl<Q: BbqHandle + 'static> Interface for EmbassyInterface<Q> {
    type Sink = EmbassySink<Q>;
}

// Helper bits

/// Errors observable by the sender
enum TransmitError {
    ConnectionClosed,
    Timeout,
}

// ---- Constants ----

/// Random device GUIDs generated for demos
pub const DEVICE_INTERFACE_GUIDS: &[&str] = &["{AFB9A6FB-30BA-44BC-9232-806CFC875321}"];
/// Default time in milliseconds to wait for the completion of sending
pub const DEFAULT_TIMEOUT_MS_PER_FRAME: usize = 2;
/// Default max packet size for USB Full Speed
pub const USB_FS_MAX_PACKET_SIZE: usize = 64;

// ---- Statics ----

static STINDX: AtomicU8 = AtomicU8::new(0xFF);
static HDLR: ConstStaticCell<ErgotHandler> = ConstStaticCell::new(ErgotHandler {});

// ---- Types ----

pub struct UsbDeviceBuffers<
    const CONFIG: usize = 256,
    const BOS: usize = 256,
    const CONTROL: usize = 64,
    const MSOS: usize = 256,
> {
    /// Config descriptor storage
    pub config_descriptor: [u8; CONFIG],
    /// BOS descriptor storage
    pub bos_descriptor: [u8; BOS],
    /// CONTROL endpoint buffer storage
    pub control_buf: [u8; CONTROL],
    /// MSOS descriptor buffer storage
    pub msos_descriptor: [u8; MSOS],
}

// ---- impls ----

// impl UsbDeviceBuffers

impl<const CONFIG: usize, const BOS: usize, const CONTROL: usize, const MSOS: usize>
    UsbDeviceBuffers<CONFIG, BOS, CONTROL, MSOS>
{
    /// Create a new, empty set of buffers
    pub const fn new() -> Self {
        Self {
            config_descriptor: [0u8; CONFIG],
            bos_descriptor: [0u8; BOS],
            msos_descriptor: [0u8; MSOS],
            control_buf: [0u8; CONTROL],
        }
    }
}

impl<const CONFIG: usize, const BOS: usize, const CONTROL: usize, const MSOS: usize> Default
    for UsbDeviceBuffers<CONFIG, BOS, CONTROL, MSOS>
{
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "embassy-usb-v0_5")]
pub mod eusb_0_5 {
    use core::sync::atomic::Ordering;

    use crate::logging::{info, warn};
    use bbq2::{
        prod_cons::framed::FramedConsumer,
        queue::BBQueue,
        traits::{coordination::Coord, notifier::maitake::MaiNotSpsc, storage::Inline},
    };
    use embassy_futures::select::{Either, select};
    use embassy_time::Timer;
    use embassy_usb_0_5::{
        Builder, UsbDevice,
        driver::{Driver, Endpoint, EndpointIn},
        msos::{self, windows_version},
    };
    use static_cell::ConstStaticCell;

    use crate::interface_manager::interface_impls::embassy_usb::TransmitError;

    use super::{DEVICE_INTERFACE_GUIDS, ErgotHandler, HDLR, STINDX, UsbDeviceBuffers};

    /// A helper type for `static` storage of buffers and driver components
    pub struct WireStorage<
        const CONFIG: usize = 256,
        const BOS: usize = 256,
        const CONTROL: usize = 64,
        const MSOS: usize = 256,
    > {
        /// Usb buffer storage
        pub bufs_usb: ConstStaticCell<UsbDeviceBuffers<CONFIG, BOS, CONTROL, MSOS>>,
    }

    /// Transmitter worker task
    ///
    /// Takes a bbqueue from the NetStack of packets to send. While sending,
    /// we will timeout if
    pub async fn tx_worker<'a, D: Driver<'a>, const N: usize, C: Coord>(
        ep_in: &mut D::EndpointIn,
        rx: FramedConsumer<&'static BBQueue<Inline<N>, C, MaiNotSpsc>>,
        timeout_ms_per_frame: usize,
        max_usb_frame_size: usize,
    ) {
        assert!(max_usb_frame_size.is_power_of_two());
        info!("Started tx_worker");
        let mut pending = false;
        loop {
            // Wait for the endpoint to be connected...
            ep_in.wait_enabled().await;

            info!("Endpoint marked connected");

            'connection: loop {
                // Wait for an outgoing frame
                let frame = rx.wait_read().await;

                defmt::debug!("Got frame to send len {=usize}", frame.len());

                // Attempt to send it
                let res = send_all::<D>(
                    ep_in,
                    &frame,
                    &mut pending,
                    timeout_ms_per_frame,
                    max_usb_frame_size,
                )
                .await;

                // Done with this frame, free the buffer space
                frame.release();

                match res {
                    Ok(()) => {}
                    Err(TransmitError::Timeout) => {
                        // TODO: treat enough timeouts like a connection closed?
                        warn!("Transmit timeout!");
                    }
                    Err(TransmitError::ConnectionClosed) => break 'connection,
                }
            }

            // Drain any pending frames - the connection was lost.
            while let Ok(frame) = rx.read() {
                frame.release();
            }
        }
    }

    #[inline]
    async fn send_all<'a, D>(
        ep_in: &mut D::EndpointIn,
        out: &[u8],
        pending_frame: &mut bool,
        timeout_ms_per_frame: usize,
        max_usb_frame_size: usize,
    ) -> Result<(), TransmitError>
    where
        D: Driver<'a>,
    {
        if out.is_empty() {
            return Ok(());
        }

        // Calculate an estimated timeout based on the number of frames we need to send
        // For now, we use 2ms/frame by default, rounded UP
        let frames = out.len().div_ceil(max_usb_frame_size);
        let timeout_ms = frames * timeout_ms_per_frame;

        let send_fut = async {
            // If we left off a pending frame, send one now so we don't leave an unterminated
            // message
            if *pending_frame && ep_in.write(&[]).await.is_err() {
                return Err(TransmitError::ConnectionClosed);
            }
            *pending_frame = true;

            // write in segments of max_usb_frame_size. The last chunk may
            // be 0 < len <= max_usb_frame_size.
            for ch in out.chunks(max_usb_frame_size) {
                if ep_in.write(ch).await.is_err() {
                    return Err(TransmitError::ConnectionClosed);
                }
            }
            // If the total we sent was a multiple of max_usb_frame_size, send an
            // empty message to "flush" the transaction. We already checked
            // above that the len != 0.
            if (out.len() & (max_usb_frame_size - 1)) == 0 && ep_in.write(&[]).await.is_err() {
                return Err(TransmitError::ConnectionClosed);
            }

            *pending_frame = false;
            Ok(())
        };

        match select(send_fut, Timer::after_millis(timeout_ms as u64)).await {
            Either::First(res) => res,
            Either::Second(()) => Err(TransmitError::Timeout),
        }
    }

    // impl WireStorage

    impl<const CONFIG: usize, const BOS: usize, const CONTROL: usize, const MSOS: usize>
        WireStorage<CONFIG, BOS, CONTROL, MSOS>
    {
        /// Create a new, uninitialized static set of buffers
        pub const fn new() -> Self {
            Self {
                bufs_usb: ConstStaticCell::new(UsbDeviceBuffers::new()),
            }
        }

        /// Initialize the static storage, reporting as ergot compatible
        ///
        /// This must only be called once.
        pub fn init_ergot<D: Driver<'static> + 'static>(
            &'static self,
            driver: D,
            config: embassy_usb_0_5::Config<'static>,
        ) -> (UsbDevice<'static, D>, D::EndpointIn, D::EndpointOut) {
            let bufs = self.bufs_usb.take();

            let mut builder = Builder::new(
                driver,
                config,
                &mut bufs.config_descriptor,
                &mut bufs.bos_descriptor,
                &mut bufs.msos_descriptor,
                &mut bufs.control_buf,
            );

            // Register a ergot-compatible string handler
            let hdlr = HDLR.take();
            builder.handler(hdlr);

            // Add the Microsoft OS Descriptor (MSOS/MOD) descriptor.
            // We tell Windows that this entire device is compatible with the "WINUSB" feature,
            // which causes it to use the built-in WinUSB driver automatically, which in turn
            // can be used by libusb/rusb software without needing a custom driver or INF file.
            // In principle you might want to call msos_feature() just on a specific function,
            // if your device also has other functions that still use standard class drivers.
            builder.msos_descriptor(windows_version::WIN8_1, 0);
            builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
            builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
                "DeviceInterfaceGUIDs",
                msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
            ));

            // Add a vendor-specific function (class 0xFF), and corresponding interface,
            // that uses our custom handler.
            let mut function = builder.function(0xFF, 0, 0);
            let mut interface = function.interface();
            let stindx = interface.string();
            STINDX.store(stindx.0, Ordering::Relaxed);
            let mut alt = interface.alt_setting(0xFF, 0xCA, 0x7D, Some(stindx));
            let ep_out = alt.endpoint_bulk_out(None, 64);
            let ep_in = alt.endpoint_bulk_in(None, 64);
            drop(function);

            // Build the builder.
            let usb = builder.build();

            (usb, ep_in, ep_out)
        }

        /// Initialize the static storage.
        ///
        /// This must only be called once.
        pub fn init<D: Driver<'static> + 'static>(
            &'static self,
            driver: D,
            config: embassy_usb_0_5::Config<'static>,
        ) -> (UsbDevice<'static, D>, D::EndpointIn, D::EndpointOut) {
            let (builder, wtx, wrx) = self.init_without_build(driver, config);
            let usb = builder.build();
            (usb, wtx, wrx)
        }
        /// Initialize the static storage, without building `Builder`
        ///
        /// This must only be called once.
        pub fn init_without_build<D: Driver<'static> + 'static>(
            &'static self,
            driver: D,
            config: embassy_usb_0_5::Config<'static>,
        ) -> (Builder<'static, D>, D::EndpointIn, D::EndpointOut) {
            let bufs = self.bufs_usb.take();

            let mut builder = Builder::new(
                driver,
                config,
                &mut bufs.config_descriptor,
                &mut bufs.bos_descriptor,
                &mut bufs.msos_descriptor,
                &mut bufs.control_buf,
            );

            // Register a ergot-compatible string handler
            let hdlr = HDLR.take();
            builder.handler(hdlr);

            // Add the Microsoft OS Descriptor (MSOS/MOD) descriptor.
            // We tell Windows that this entire device is compatible with the "WINUSB" feature,
            // which causes it to use the built-in WinUSB driver automatically, which in turn
            // can be used by libusb/rusb software without needing a custom driver or INF file.
            // In principle you might want to call msos_feature() just on a specific function,
            // if your device also has other functions that still use standard class drivers.
            builder.msos_descriptor(windows_version::WIN8_1, 0);
            builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
            builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
                "DeviceInterfaceGUIDs",
                msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
            ));

            // Add a vendor-specific function (class 0xFF), and corresponding interface,
            // that uses our custom handler.
            let mut function = builder.function(0xFF, 0, 0);
            let mut interface = function.interface();
            let stindx = interface.string();
            STINDX.store(stindx.0, Ordering::Relaxed);
            let mut alt = interface.alt_setting(0xFF, 0xCA, 0x7D, Some(stindx));
            let ep_out = alt.endpoint_bulk_out(None, 64);
            let ep_in = alt.endpoint_bulk_in(None, 64);
            drop(function);

            (builder, ep_in, ep_out)
        }
    }

    impl<const CONFIG: usize, const BOS: usize, const CONTROL: usize, const MSOS: usize> Default
        for WireStorage<CONFIG, BOS, CONTROL, MSOS>
    {
        fn default() -> Self {
            Self::new()
        }
    }

    // impl ErgotHandler

    impl embassy_usb_0_5::Handler for ErgotHandler {
        fn get_string(
            &mut self,
            index: embassy_usb_0_5::types::StringIndex,
            lang_id: u16,
        ) -> Option<&str> {
            use embassy_usb_0_5::descriptor::lang_id;

            let stindx = STINDX.load(Ordering::Relaxed);
            if stindx == 0xFF {
                return None;
            }
            if lang_id == lang_id::ENGLISH_US && index.0 == stindx {
                Some("ergot")
            } else {
                None
            }
        }
    }
}