usbfs 0.1.1

Port of the usbfs Linux userspace library in pure Rust
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
use std::{cmp, ffi::c_void, fmt};

use super::UsbfsIsoPacketDesc;

/// Convenience alias for types used as a `usercontext` argument in [`Urb`].
///
/// The `usercontext` is an argument to USB callback completion functions.
pub trait UrbUserContext {
    fn as_raw_ptr(&self) -> *const c_void;
    fn as_raw_ptr_mut(&mut self) -> *mut c_void;
}

impl UrbUserContext for () {
    fn as_raw_ptr(&self) -> *const c_void {
        self as *const () as *const _
    }

    fn as_raw_ptr_mut(&mut self) -> *mut c_void {
        self as *mut () as *mut _
    }
}

impl PartialEq for dyn UrbUserContext {
    fn eq(&self, rhs: &Self) -> bool {
        self.as_raw_ptr() == rhs.as_raw_ptr()
    }
}

impl fmt::Display for dyn UrbUserContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "")
    }
}

impl fmt::Debug for dyn UrbUserContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "")
    }
}

/// Represents USBFS transfer information.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TransferInfo(u32);

impl TransferInfo {
    /// Creates a new [TransferInfo].
    pub const fn new() -> Self {
        Self(0)
    }

    /// Creates a new [TransferInfo] for an Isochronous transfer.
    pub const fn create_isoc(number_of_packets: i32) -> Self {
        Self(number_of_packets as u32)
    }

    /// Creates a new [TransferInfo] for a Bulk transfer.
    pub const fn create_bulk(stream_id: u32) -> Self {
        Self(stream_id)
    }

    /// Gets the number of packets in an Isochronous transfer.
    pub const fn number_of_packets(&self) -> i32 {
        self.0 as i32
    }

    /// Gets the stream ID of a Bulk transfer.
    pub const fn stream_id(&self) -> u32 {
        self.0
    }
}

impl fmt::Display for TransferInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Represents a URB record on Linux.
#[repr(C)]
pub struct Urb<'a> {
    urb_type: u8,
    endpoint: u8,
    status: i32,
    flags: u32,
    buffer: Vec<u8>,
    actual_length: usize,
    start_frame: i32,
    info: TransferInfo,
    error_count: i32,
    signr: u32,
    usercontext: Option<&'a mut dyn UrbUserContext>,
    iso_frame_desc: Vec<UsbfsIsoPacketDesc>,
}

impl<'a> Urb<'a> {
    /// Creates a new [Urb].
    pub fn new() -> Self {
        Self {
            urb_type: 0,
            endpoint: 0,
            status: 0,
            flags: 0,
            buffer: Vec::new(),
            actual_length: 0,
            start_frame: 0,
            info: TransferInfo::new(),
            error_count: 0,
            signr: 0,
            usercontext: None,
            iso_frame_desc: Vec::new(),
        }
    }

    /// Gets the URB type.
    pub const fn urb_type(&self) -> u8 {
        self.urb_type
    }

    /// Sets the URB type.
    pub fn set_urb_type(&mut self, urb_type: u8) {
        self.urb_type = urb_type;
    }

    /// Builder function that sets the URB type.
    pub fn with_urb_type(mut self, urb_type: u8) -> Self {
        self.set_urb_type(urb_type);
        self
    }

    /// Gets the URB endpoint.
    pub const fn endpoint(&self) -> u8 {
        self.endpoint
    }

    /// Sets the URB endpoint.
    pub fn set_endpoint(&mut self, endpoint: u8) {
        self.endpoint = endpoint;
    }

    /// Builder function that sets the URB endpoint.
    pub fn with_endpoint(mut self, endpoint: u8) -> Self {
        self.set_endpoint(endpoint);
        self
    }

    /// Gets the URB status.
    pub const fn status(&self) -> i32 {
        self.status
    }

    /// Sets the URB status.
    pub fn set_status(&mut self, status: i32) {
        self.status = status;
    }

    /// Builder function that sets the URB status.
    pub fn with_status(mut self, status: i32) -> Self {
        self.set_status(status);
        self
    }

    /// Gets the URB flags.
    pub const fn flags(&self) -> u32 {
        self.flags
    }

    /// Sets the URB flags.
    pub fn set_flags(&mut self, flags: u32) {
        self.flags = flags;
    }

    /// Builder function that sets the URB flags.
    pub fn with_flags(mut self, flags: u32) -> Self {
        self.set_flags(flags);
        self
    }

    /// Gets the URB buffer.
    pub fn buffer(&self) -> &[u8] {
        self.buffer.as_ref()
    }

    /// Sets the URB buffer.
    pub fn set_buffer<B: IntoIterator<Item = u8>>(&mut self, buffer: B) {
        self.buffer = buffer.into_iter().collect();
        self.actual_length = self.buffer.len();
    }

    /// Builder function that sets the URB buffer.
    pub fn with_buffer<B: IntoIterator<Item = u8>>(mut self, buffer: B) -> Self {
        self.set_buffer(buffer);
        self
    }

    /// Gets the URB buffer length.
    pub fn buffer_length(&self) -> usize {
        self.buffer.len()
    }

    /// Gets the URB actual length.
    pub const fn actual_length(&self) -> usize {
        self.actual_length
    }

    /// Sets the URB actual length.
    pub fn set_actual_length(&mut self, actual_length: usize) {
        self.actual_length = cmp::min(actual_length, self.buffer.len());
    }

    /// Builder function that sets the URB actual length.
    pub fn with_actual_length(mut self, actual_length: usize) -> Self {
        self.set_actual_length(actual_length);
        self
    }

    /// Gets the URB start frame.
    pub const fn start_frame(&self) -> i32 {
        self.start_frame
    }

    /// Sets the URB start frame.
    pub fn set_start_frame(&mut self, start_frame: i32) {
        self.start_frame = start_frame;
    }

    /// Builder function that sets the URB start frame.
    pub fn with_start_frame(mut self, start_frame: i32) -> Self {
        self.set_start_frame(start_frame);
        self
    }

    /// Gets the URB info.
    pub const fn info(&self) -> &TransferInfo {
        &self.info
    }

    /// Sets the URB info.
    pub fn set_info(&mut self, info: TransferInfo) {
        self.info = info;
    }

    /// Builder function that sets the URB info.
    pub fn with_info(mut self, info: TransferInfo) -> Self {
        self.set_info(info);
        self
    }

    /// Gets the URB error count.
    pub const fn error_count(&self) -> i32 {
        self.error_count
    }

    /// Sets the URB error count.
    pub fn set_error_count(&mut self, error_count: i32) {
        self.error_count = error_count;
    }

    /// Builder function that sets the URB error count.
    pub fn with_error_count(mut self, error_count: i32) -> Self {
        self.set_error_count(error_count);
        self
    }

    /// Gets the URB signr.
    pub const fn signr(&self) -> u32 {
        self.signr
    }

    /// Sets the URB signr.
    pub fn set_signr(&mut self, signr: u32) {
        self.signr = signr;
    }

    /// Builder function that sets the URB signr.
    pub fn with_signr(mut self, signr: u32) -> Self {
        self.set_signr(signr);
        self
    }

    /// Gets a void pointer to the [UrbUserContext].
    ///
    /// **WARNING** Pointer may be NULL for an unset [UrbUserContext].
    pub fn usercontext_ptr(&self) -> *const c_void {
        if let Some(ctx) = self.usercontext.as_ref() {
            ctx.as_raw_ptr()
        } else {
            std::ptr::null()
        }
    }

    /// Gets a mutable void pointer to the [UrbUserContext].
    ///
    /// **WARNING** Pointer may be NULL for an unset [UrbUserContext].
    pub fn usercontext_ptr_mut(&mut self) -> *mut c_void {
        if let Some(ctx) = self.usercontext.as_mut() {
            ctx.as_raw_ptr_mut()
        } else {
            std::ptr::null_mut()
        }
    }

    /// Sets the URB usercontext.
    pub fn set_usercontext(&mut self, usercontext: &'a mut dyn UrbUserContext) {
        self.usercontext.replace(usercontext);
    }

    /// Builder function that sets the URB usercontext.
    pub fn with_usercontext(mut self, usercontext: &'a mut dyn UrbUserContext) -> Self {
        self.set_usercontext(usercontext);
        self
    }

    /// Unsets the [UrbUserContext].
    pub fn unset_usercontext(&mut self) {
        self.usercontext.take();
    }

    /// Gets the URB [UsbfsIsoPacketDesc] list.
    pub fn iso_frame_desc(&self) -> &[UsbfsIsoPacketDesc] {
        self.iso_frame_desc.as_ref()
    }

    /// Sets the URB iso_frame_desc.
    pub fn set_iso_frame_desc<B: IntoIterator<Item = UsbfsIsoPacketDesc>>(
        &mut self,
        iso_frame_desc: B,
    ) {
        self.iso_frame_desc = iso_frame_desc.into_iter().collect();
    }

    /// Builder function that sets the URB iso_frame_desc.
    pub fn with_iso_frame_desc<B: IntoIterator<Item = UsbfsIsoPacketDesc>>(
        mut self,
        iso_frame_desc: B,
    ) -> Self {
        self.set_iso_frame_desc(iso_frame_desc);
        self
    }
}

impl<'a> PartialEq for Urb<'a> {
    fn eq(&self, rhs: &Self) -> bool {
        self.urb_type == rhs.urb_type
            && self.endpoint == rhs.endpoint
            && self.status == rhs.status
            && self.flags == rhs.flags
            && self.buffer == rhs.buffer
            && self.actual_length == rhs.actual_length
            && self.info == rhs.info
            && self.error_count == rhs.error_count
            && self.signr == rhs.signr
            && self.usercontext_ptr() == rhs.usercontext_ptr()
            && self.iso_frame_desc == rhs.iso_frame_desc
    }
}

impl<'a> fmt::Display for Urb<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(f, r#""type": {}, "#, self.urb_type)?;
        write!(f, r#""endpoint": {}, "#, self.endpoint)?;
        write!(f, r#""status": {}, "#, self.status)?;
        write!(f, r#""flags": {}, "#, self.flags)?;

        write!(f, r#""buffer: ["#)?;
        for (i, b) in self.buffer.iter().enumerate() {
            if i != 0 {
                write!(f, ", ")?;
            }
            write!(f, "{b}")?;
        }
        write!(f, "], ")?;

        write!(f, r#""buffer_length": {}, "#, self.buffer.len())?;
        write!(f, r#""actual_length": {}, "#, self.actual_length)?;
        write!(f, r#""start_frame": {}, "#, self.start_frame)?;

        if self.iso_frame_desc.is_empty() {
            write!(f, r#""stream_id": {}, "#, self.info.stream_id())?;
        } else {
            write!(
                f,
                r#""number_of_packets": {}, "#,
                self.info.number_of_packets()
            )?;
        }

        write!(f, r#""error_count": {}, "#, self.error_count)?;
        write!(f, r#""signr": {}, "#, self.signr)?;

        write!(f, r#""iso_frame_desc": ["#)?;
        for (i, frame) in self.iso_frame_desc.iter().enumerate() {
            if i != 0 {
                write!(f, ", ")?;
            }
            write!(f, "{frame}")?;
        }
        write!(f, "]}}")
    }
}

impl<'a> Default for Urb<'a> {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Copy)]
pub union TransferInfoFfi {
    number_of_packets: i32,
    stream_id: u32,
}

impl TransferInfoFfi {
    /// Creates a new [TransferInfoFfi].
    pub const fn new() -> Self {
        Self {
            number_of_packets: 0,
        }
    }

    /// Creates a new [TransferInfoFfi] for isochronous transfers from the provided parameter.
    pub fn new_isoc(number_of_packets: i32) -> Self {
        Self { number_of_packets }
    }

    /// Creates a new [TransferInfoFfi] for bulk transfers from the provided parameter.
    pub fn new_bulk(stream_id: u32) -> Self {
        Self { stream_id }
    }
}

impl From<&TransferInfo> for TransferInfoFfi {
    fn from(val: &TransferInfo) -> Self {
        let number_of_packets = val.number_of_packets();
        let stream_id = val.stream_id();

        if number_of_packets != 0 {
            Self::new_isoc(number_of_packets)
        } else if stream_id != 0 {
            Self::new_bulk(stream_id)
        } else {
            Self::new()
        }
    }
}

impl From<TransferInfo> for TransferInfoFfi {
    fn from(val: TransferInfo) -> Self {
        (&val).into()
    }
}

impl Default for TransferInfoFfi {
    fn default() -> Self {
        Self::new()
    }
}

impl PartialEq for TransferInfoFfi {
    fn eq(&self, rhs: &Self) -> bool {
        unsafe {
            match (self, rhs) {
                (
                    Self {
                        number_of_packets: nl,
                    },
                    Self {
                        number_of_packets: nr,
                    },
                ) if nl == nr => true,
                (Self { stream_id: lid }, Self { stream_id: rid }) if lid == rid => true,
                _ => false,
            }
        }
    }
}

/// Represents a URB record on Linux passed to an `ioctl` FFI.
#[repr(C)]
#[derive(PartialEq)]
pub struct UrbFfi {
    urb_type: u8,
    endpoint: u8,
    status: i32,
    flags: u32,
    buffer: *mut c_void,
    buffer_length: i32,
    actual_length: i32,
    start_frame: i32,
    info: TransferInfoFfi,
    error_count: i32,
    signr: u32,
    usercontext: *mut c_void,
    iso_frame_desc: *mut UsbfsIsoPacketDesc,
}

impl UrbFfi {
    /// Creates a new [UrbFfi].
    pub const fn new() -> Self {
        Self {
            urb_type: 0,
            endpoint: 0,
            status: 0,
            flags: 0,
            buffer: std::ptr::null_mut(),
            buffer_length: 0,
            actual_length: 0,
            start_frame: 0,
            info: TransferInfoFfi::new(),
            error_count: 0,
            signr: 0,
            usercontext: std::ptr::null_mut(),
            iso_frame_desc: std::ptr::null_mut(),
        }
    }
}

impl<'a> From<&mut Urb<'a>> for UrbFfi {
    fn from(val: &mut Urb) -> Self {
        let info = if val.iso_frame_desc.is_empty() {
            val.info.into()
        } else {
            let len = val.iso_frame_desc.len();
            // check that we don't overflow the length
            if len > i32::MAX as usize {
                TransferInfoFfi::new_isoc(i32::MAX)
            } else {
                TransferInfoFfi::new_isoc(len as i32)
            }
        };

        Self {
            urb_type: val.urb_type,
            endpoint: val.endpoint,
            status: val.status,
            flags: val.flags,
            buffer: val.buffer.as_mut_ptr() as *mut _,
            buffer_length: val.buffer.len() as i32,
            actual_length: val.actual_length as i32,
            start_frame: val.start_frame,
            info,
            error_count: val.error_count,
            signr: val.signr,
            usercontext: if let Some(ctx) = val.usercontext.as_mut() {
                ctx.as_raw_ptr_mut()
            } else {
                std::ptr::null_mut()
            },
            iso_frame_desc: if val.iso_frame_desc.is_empty() {
                std::ptr::null_mut()
            } else {
                val.iso_frame_desc.as_mut_ptr() as *mut _
            },
        }
    }
}

impl Default for UrbFfi {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_urb() {
        let exp_urb_type = 1;
        let exp_endpoint = 2;
        let exp_status = 3;
        let exp_flags = 4;
        let exp_buffer = [1u8];
        let exp_start_frame = 5;
        let exp_info = TransferInfo::create_isoc(1);
        let exp_error_count = 6;
        let exp_signr = 7;
        let mut exp_context = ();
        let exp_context_ptr = UrbUserContext::as_raw_ptr(&exp_context) as usize;
        let exp_desc = [UsbfsIsoPacketDesc::new()];

        let exp_urb = Urb::new()
            .with_urb_type(exp_urb_type)
            .with_endpoint(exp_endpoint)
            .with_status(exp_status)
            .with_flags(exp_flags)
            .with_buffer(exp_buffer)
            .with_actual_length(exp_buffer.len())
            .with_start_frame(exp_start_frame)
            .with_info(exp_info)
            .with_error_count(exp_error_count)
            .with_signr(exp_signr)
            .with_usercontext(&mut exp_context)
            .with_iso_frame_desc(exp_desc);

        let mut null_urb = Urb::new();

        assert_eq!(exp_urb.urb_type(), exp_urb_type);
        assert_eq!(exp_urb.endpoint(), exp_endpoint);
        assert_eq!(exp_urb.status(), exp_status);
        assert_eq!(exp_urb.flags(), exp_flags);
        assert_eq!(exp_urb.buffer(), exp_buffer);
        assert_eq!(exp_urb.start_frame(), exp_start_frame);
        assert_eq!(exp_urb.info(), &exp_info);
        assert_eq!(exp_urb.error_count(), exp_error_count);
        assert_eq!(exp_urb.signr(), exp_signr);
        assert_eq!(exp_urb.usercontext_ptr() as usize, exp_context_ptr);
        assert_eq!(exp_urb.iso_frame_desc(), exp_desc);

        assert_eq!(null_urb.urb_type(), 0);
        assert_eq!(null_urb.endpoint(), 0);
        assert_eq!(null_urb.status(), 0);
        assert_eq!(null_urb.flags(), 0);
        assert_eq!(null_urb.buffer(), &[]);
        assert_eq!(null_urb.start_frame(), 0);
        assert_eq!(null_urb.info(), &TransferInfo::new());
        assert_eq!(null_urb.error_count(), 0);
        assert_eq!(null_urb.signr(), 0);
        assert_eq!(null_urb.usercontext_ptr(), std::ptr::null());
        assert_eq!(null_urb.iso_frame_desc(), &[]);

        null_urb.set_urb_type(exp_urb_type);
        assert_eq!(null_urb.urb_type(), exp_urb_type);

        null_urb.set_endpoint(exp_endpoint);
        assert_eq!(null_urb.endpoint(), exp_endpoint);

        null_urb.set_status(exp_status);
        assert_eq!(null_urb.status(), exp_status);

        null_urb.set_flags(exp_flags);
        assert_eq!(null_urb.flags(), exp_flags);

        null_urb.set_buffer(exp_buffer);
        assert_eq!(null_urb.buffer(), exp_buffer);

        null_urb.set_start_frame(exp_start_frame);
        assert_eq!(null_urb.start_frame(), exp_start_frame);

        null_urb.set_info(exp_info);
        assert_eq!(null_urb.info(), &exp_info);

        null_urb.set_error_count(exp_error_count);
        assert_eq!(null_urb.error_count(), exp_error_count);

        null_urb.set_signr(exp_signr);
        assert_eq!(null_urb.signr(), exp_signr);

        let mut null_context = ();
        let null_context_ptr = UrbUserContext::as_raw_ptr(&null_context) as usize;

        null_urb.set_usercontext(&mut null_context);
        assert_eq!(null_urb.usercontext_ptr() as usize, null_context_ptr);

        null_urb.set_iso_frame_desc(exp_desc);
        assert_eq!(null_urb.iso_frame_desc(), exp_desc.as_ref());
    }
}