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

#![no_std]


pub use usb_device::{Result, UsbError};
pub mod hid_class;
pub mod descriptor;

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use crate::descriptor::gen_hid_descriptor;
    use crate::descriptor::SerializedDescriptor;

    // This should generate this descriptor:
    // 0x06, 0x00, 0xFF,  // Usage Page (Vendor Defined 0xFF00)
    // 0x09, 0x01,        // Usage (0x01)
    // 0xA1, 0x01,        // Collection (Application)
    // 0x15, 0x00,        //   Logical Minimum (0)
    // 0x26, 0xFF, 0x00,  //   Logical Maximum (255)
    // 0x75, 0x08,        //   Report Size (8)
    // 0x95, 0x01,        //   Report Count (1)
    // 0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
    // 0x27, 0xFF, 0xFF, 0x00, 0x00,  //   Logical Maximum (65534)
    // 0x75, 0x10,        //   Report Size (16)
    // 0x91, 0x02,        //   Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
    // 0xC1,              // End Collection
    #[gen_hid_descriptor(
        (collection = 0x01, usage = 0x01, usage_page = 0xff00) = {
            f1=input;
            f2=output;
        }
    )]
    #[allow(dead_code)]
    struct CustomUnaryUnsignedFrame {
        f1: u8,
        f2: u16,
    }


    #[test]
    fn test_custom_unsigned() {
        let expected = &[
            6u8, 0u8, 255u8, 9u8, 1u8, 161u8, 1u8, 21u8, 0u8, 38u8, 255u8, 0u8, 117u8, 8u8, 149u8,
            1u8, 129u8, 2u8, 39u8, 255u8, 255u8, 0u8, 0u8, 117u8, 16u8, 145u8, 2u8, 192u8,
        ];
        assert_eq!(CustomUnaryUnsignedFrame::desc(), expected);
    }

    // This should generate this descriptor:
    // 0x06, 0x00, 0xFF,                // Usage Page (Vendor Defined 0xFF00)
    // 0x09, 0x01,                      // Usage (0x01)
    // 0xA1, 0x01,                      // Collection (Application)
    // 0x17, 0x81, 0xFF, 0xFF, 0xFF,    //   Logical Minimum (-128)
    // 0x25, 0x7F,                      //   Logical Maximum (127)
    // 0x75, 0x08,                      //   Report Size (8)
    // 0x95, 0x01,                      //   Report Count (1)
    // 0x81, 0x02,                      //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
    // 0x17, 0x01, 0x80, 0xFF, 0xFF,    //   Logical Minimum (-32768)
    // 0x26, 0xFF, 0x7F,                //   Logical Maximum (32767)
    // 0x75, 0x10,                      //   Report Size (16)
    // 0x91, 0x02,                      //   Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
    // 0xC0,                            // End Collection
    #[gen_hid_descriptor(
        (collection = 0x01, usage = 0x01, usage_page = 0xff00) = {
            f1=input;
            f2=output;
        }
    )]
    #[allow(dead_code)]
    struct CustomUnarySignedFrame {
        f1: i8,
        f2: i16,
    }

    #[test]
    fn test_custom_signed() {
        let expected = &[
            6u8, 0u8, 255u8, 9u8, 1u8, 161u8, 1u8, 23u8, 129u8, 255u8, 255u8, 255u8, 37u8, 127u8, 117u8, 8u8,
            149u8, 1u8, 129u8, 2u8, 23u8, 1u8, 128u8, 255u8, 255u8, 38u8, 255u8, 127u8, 117u8, 16u8, 145u8, 2u8,
            192u8,
        ];
        assert_eq!(CustomUnarySignedFrame::desc()[0..32], expected[0..32]);
    }

    #[gen_hid_descriptor(
        (report_id = 0x01,) = {
            f1=input
        },
        (report_id = 0x02,) = {
            f2=input
        },
    )]
    #[allow(dead_code)]
    struct CustomMultiReport {
        f1: u8,
        f2: u8,
    }

    #[test]
    fn test_custom_reports() {
        let expected: &[u8] = &[
            133, 1, 21, 0, 38, 255, 0, 117, 8, 149, 1, 129, 2, 133, 2, 129, 2
        ];
        assert_eq!(CustomMultiReport::desc(), expected);
    }

    // This should generate the following descriptor:
    // 0x06, 0x00, 0xFF,  // Usage Page (Vendor Defined 0xFF00)
    // 0x09, 0x01,        // Usage (0x01)
    // 0xA1, 0x01,        // Collection (Application)
    // 0x15, 0x00,        //   Logical Minimum (0)
    // 0x26, 0xFF, 0x00,  //   Logical Maximum (255)
    // 0x75, 0x08,        //   Report Size (8)
    // 0x95, 0x20,        //   Report Count (32)
    // 0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
    // 0xC0,              // End Collection
    #[gen_hid_descriptor(
        (collection = 0x01, usage = 0x01, usage_page = 0xff00) = {
            buff=input
        },
    )]
    #[allow(dead_code)]
    struct CustomArray {
        buff: [u8; 32],
    }

    #[test]
    fn test_array() {
        let expected: &[u8] = &[
            6, 0, 255, 9, 1, 161, 1, 21, 0, 38, 255, 0, 117, 8, 149, 32, 129, 2, 192
        ];
        assert_eq!(CustomArray::desc(), expected);
    }

}