libusb1-sys 0.3.7

FFI bindings for libusb.
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
use libc::{c_int, c_uchar};
use libusb1_sys as ffi;

use std::{mem, ptr, slice};

fn main() {
    let mut context = mem::MaybeUninit::<*mut ffi::libusb_context>::uninit();

    let context = match unsafe { ffi::libusb_init(context.as_mut_ptr()) } {
        0 => unsafe { context.assume_init() },
        e => panic!("libusb_init: {}", get_error(e)),
    };

    list_devices(context);

    unsafe { ffi::libusb_exit(context) };
}

fn list_devices(context: *mut ffi::libusb_context) {
    let mut device_list = mem::MaybeUninit::<*const *mut ffi::libusb_device>::uninit();

    let len = unsafe { ffi::libusb_get_device_list(context, device_list.as_mut_ptr()) };

    if len < 0 {
        println!("libusb_get_device_list: {}", get_error(len as c_int));
        return;
    }
    let device_list = unsafe { device_list.assume_init() };

    let devs = unsafe { slice::from_raw_parts(device_list, len as usize) };

    for dev in devs {
        display_device(dev);
    }

    unsafe { ffi::libusb_free_device_list(device_list, 1) };
}

fn display_device(dev: &*mut ffi::libusb_device) {
    let mut descriptor = mem::MaybeUninit::<ffi::libusb_device_descriptor>::uninit();
    let mut handle: *mut ffi::libusb_device_handle = ptr::null_mut();

    let bus = unsafe { ffi::libusb_get_bus_number(*dev) };
    let address = unsafe { ffi::libusb_get_device_address(*dev) };
    let speed = unsafe { ffi::libusb_get_device_speed(*dev) };

    let descriptor =
        match unsafe { ffi::libusb_get_device_descriptor(*dev, descriptor.as_mut_ptr()) } {
            0 => Some(unsafe { descriptor.assume_init() }),
            _ => None,
        };

    if unsafe { ffi::libusb_open(*dev, &mut handle) } < 0 {
        println!("Couldn't open device, some information will be missing");
        handle = ptr::null_mut();
    }

    print!("Bus {:03} Device {:03}", bus, address);

    if let Some(descriptor) = &descriptor {
        print!(
            " ID {:04x}:{:04x}",
            descriptor.idVendor, descriptor.idProduct
        );
    }

    print!(" {}", get_device_speed(speed));

    if let Some(descriptor) = &descriptor {
        if descriptor.iManufacturer > 0 {
            match get_string_descriptor(handle, descriptor.iManufacturer) {
                Some(s) => print!(" {}", s),
                None => (),
            }
        }

        if descriptor.iProduct > 0 {
            match get_string_descriptor(handle, descriptor.iProduct) {
                Some(s) => print!(" {}", s),
                None => (),
            }
        }

        if descriptor.iSerialNumber > 0 {
            match get_string_descriptor(handle, descriptor.iSerialNumber) {
                Some(s) => print!(" Serial No. {}", s),
                None => (),
            }
        }
    }

    println!();

    if let Some(descriptor) = &descriptor {
        print_device_descriptor(handle, descriptor);

        for i in 0..descriptor.bNumConfigurations {
            let mut descriptor = mem::MaybeUninit::<*const ffi::libusb_config_descriptor>::uninit();

            match unsafe { ffi::libusb_get_config_descriptor(*dev, i, descriptor.as_mut_ptr()) } {
                0 => {
                    let descriptor = unsafe { descriptor.assume_init() };
                    let config = unsafe { &*descriptor };
                    let interfaces = unsafe {
                        slice::from_raw_parts(config.interface, config.bNumInterfaces as usize)
                    };

                    print_config_descriptor(handle, config);

                    for iface in interfaces {
                        let iface_descriptors = unsafe {
                            slice::from_raw_parts(iface.altsetting, iface.num_altsetting as usize)
                        };

                        for iface_desc in iface_descriptors {
                            print_interface_descriptor(handle, iface_desc);

                            let endpoints = unsafe {
                                slice::from_raw_parts(
                                    iface_desc.endpoint,
                                    iface_desc.bNumEndpoints as usize,
                                )
                            };

                            for endpoint in endpoints {
                                print_endpoint_descriptor(endpoint);
                            }
                        }
                    }

                    unsafe { ffi::libusb_free_config_descriptor(descriptor) };
                }
                _ => (),
            }
        }
    }

    if !handle.is_null() {
        unsafe { ffi::libusb_close(handle) };
    }
}

fn print_device_descriptor(
    handle: *mut ffi::libusb_device_handle,
    descriptor: &ffi::libusb_device_descriptor,
) {
    println!("Device Descriptor:");
    println!("  bLength: {:16}", descriptor.bLength);
    println!(
        "  bDescriptorType: {:8} {}",
        descriptor.bDescriptorType,
        get_descriptor_type(descriptor.bDescriptorType)
    );
    println!(
        "  bcdUSB:            {:#06x} {}",
        descriptor.bcdUSB,
        get_bcd_version(descriptor.bcdUSB)
    );
    println!(
        "  bDeviceClass:        {:#04x} {}",
        descriptor.bDeviceClass,
        get_class_type(descriptor.bDeviceClass)
    );
    println!("  bDeviceSubClass: {:8}", descriptor.bDeviceSubClass);
    println!("  bDeviceProtocol: {:8}", descriptor.bDeviceProtocol);
    println!("  bMaxPacketSize0: {:8}", descriptor.bMaxPacketSize0);
    println!("  idVendor:          {:#06x}", descriptor.idVendor);
    println!("  idProduct:         {:#06x}", descriptor.idProduct);
    println!("  bcdDevice:         {:#06x}", descriptor.bcdDevice);
    println!(
        "  iManufacturer: {:10} {}",
        descriptor.iManufacturer,
        get_string_descriptor(handle, descriptor.iManufacturer).unwrap_or(String::new())
    );
    println!(
        "  iProduct: {:15} {}",
        descriptor.iProduct,
        get_string_descriptor(handle, descriptor.iProduct).unwrap_or(String::new())
    );
    println!(
        "  iSerialNumber: {:10} {}",
        descriptor.iSerialNumber,
        get_string_descriptor(handle, descriptor.iSerialNumber).unwrap_or(String::new())
    );
    println!("  bNumConfigurations: {:5}", descriptor.bNumConfigurations);
}

fn print_config_descriptor(
    handle: *mut ffi::libusb_device_handle,
    descriptor: &ffi::libusb_config_descriptor,
) {
    println!("  Configuration Descriptor:");
    println!("    bLength: {:16}", descriptor.bLength);
    println!(
        "    bDescriptorType: {:8} {}",
        descriptor.bDescriptorType,
        get_descriptor_type(descriptor.bDescriptorType)
    );
    println!("    wTotalLength: {:11}", descriptor.wTotalLength);
    println!("    bNumInterfaces: {:9}", descriptor.bNumInterfaces);
    println!(
        "    bConfigurationValue: {:4}",
        descriptor.bConfigurationValue
    );
    println!(
        "    iConfiguration: {:9} {}",
        descriptor.iConfiguration,
        get_string_descriptor(handle, descriptor.iConfiguration).unwrap_or(String::new())
    );
    println!("    bmAttributes:        {:#04x}", descriptor.bmAttributes);
    println!(
        "    bMaxPower: {:14} {}",
        descriptor.bMaxPower,
        get_max_power(descriptor.bMaxPower)
    );

    if descriptor.extra_length > 0 {
        let extra =
            unsafe { slice::from_raw_parts(descriptor.extra, descriptor.extra_length as usize) };
        println!("    (extra: {:?})", extra);
    }
}

fn print_interface_descriptor(
    handle: *mut ffi::libusb_device_handle,
    descriptor: &ffi::libusb_interface_descriptor,
) {
    println!("    Interface Descriptor:");
    println!("      bLength: {:16}", descriptor.bLength);
    println!(
        "      bDescriptorType: {:8} {}",
        descriptor.bDescriptorType,
        get_descriptor_type(descriptor.bDescriptorType)
    );
    println!("      bInterfaceNumber: {:7}", descriptor.bInterfaceNumber);
    println!(
        "      bAlternateSetting: {:6}",
        descriptor.bAlternateSetting
    );
    println!("      bNumEndpoints: {:10}", descriptor.bNumEndpoints);
    println!(
        "      bInterfaceClass:     {:#04x} {}",
        descriptor.bInterfaceClass,
        get_class_type(descriptor.bInterfaceClass)
    );
    println!(
        "      bInterfaceSubClass: {:5}",
        descriptor.bInterfaceSubClass
    );
    println!(
        "      bInterfaceProtocol: {:5}",
        descriptor.bInterfaceProtocol
    );
    println!(
        "      iInterface: {:13} {}",
        descriptor.iInterface,
        get_string_descriptor(handle, descriptor.iInterface).unwrap_or(String::new())
    );

    if descriptor.extra_length > 0 {
        let extra =
            unsafe { slice::from_raw_parts(descriptor.extra, descriptor.extra_length as usize) };
        println!("    (extra: {:?})", extra);
    }
}

fn print_endpoint_descriptor(descriptor: &ffi::libusb_endpoint_descriptor) {
    println!("      Endpoint Descriptor:");
    println!("        bLength: {:16}", descriptor.bLength);
    println!(
        "        bDescriptorType: {:8} {}",
        descriptor.bDescriptorType,
        get_descriptor_type(descriptor.bDescriptorType)
    );
    println!(
        "        bEndpointAddress:    {:#04x} {}",
        descriptor.bEndpointAddress,
        get_endpoint(descriptor.bEndpointAddress)
    );
    println!(
        "        bmAttributes:        {:#04x}",
        descriptor.bmAttributes
    );
    println!(
        "          Transfer Type:           {}",
        get_transfer_type(descriptor.bmAttributes)
    );
    println!(
        "          Synch Type:              {}",
        get_synch_type(descriptor.bmAttributes)
    );
    println!(
        "          Usage Type:              {}",
        get_usage_type(descriptor.bmAttributes)
    );
    println!(
        "        wMaxPacketSize:    {:#06x}",
        descriptor.wMaxPacketSize
    );
    println!("        bInterval: {:14}", descriptor.bInterval);
    println!("        bRefresh: {:15}", descriptor.bRefresh);
    println!("        bSynchAddress: {:10}", descriptor.bSynchAddress);

    if descriptor.extra_length > 0 {
        let extra =
            unsafe { slice::from_raw_parts(descriptor.extra, descriptor.extra_length as usize) };
        println!("    (extra: {:?})", extra);
    }
}

fn get_error(err: c_int) -> &'static str {
    match err {
        ffi::constants::LIBUSB_SUCCESS => "success",
        ffi::constants::LIBUSB_ERROR_IO => "I/O error",
        ffi::constants::LIBUSB_ERROR_INVALID_PARAM => "invalid parameter",
        ffi::constants::LIBUSB_ERROR_ACCESS => "access denied",
        ffi::constants::LIBUSB_ERROR_NO_DEVICE => "no such device",
        ffi::constants::LIBUSB_ERROR_NOT_FOUND => "entity not found",
        ffi::constants::LIBUSB_ERROR_BUSY => "resource busy",
        ffi::constants::LIBUSB_ERROR_TIMEOUT => "opteration timed out",
        ffi::constants::LIBUSB_ERROR_OVERFLOW => "overflow error",
        ffi::constants::LIBUSB_ERROR_PIPE => "pipe error",
        ffi::constants::LIBUSB_ERROR_INTERRUPTED => "system call interrupted",
        ffi::constants::LIBUSB_ERROR_NO_MEM => "insufficient memory",
        ffi::constants::LIBUSB_ERROR_NOT_SUPPORTED => "operation not supported",
        ffi::constants::LIBUSB_ERROR_OTHER | _ => "other error",
    }
}

fn get_device_speed(speed: c_int) -> &'static str {
    match speed {
        ffi::constants::LIBUSB_SPEED_SUPER => "5000 Mbps",
        ffi::constants::LIBUSB_SPEED_HIGH => " 480 Mbps",
        ffi::constants::LIBUSB_SPEED_FULL => "  12 Mbps",
        ffi::constants::LIBUSB_SPEED_LOW => " 1.5 Mbps",
        ffi::constants::LIBUSB_SPEED_UNKNOWN | _ => "(unknown)",
    }
}

fn get_max_power(power: u8) -> String {
    if power > 0 {
        format!("{}mW", power as usize * 2)
    } else {
        String::new()
    }
}

fn get_descriptor_type(desc_type: u8) -> &'static str {
    match desc_type {
        ffi::constants::LIBUSB_DT_DEVICE => "Device",
        ffi::constants::LIBUSB_DT_CONFIG => "Configuration",
        ffi::constants::LIBUSB_DT_STRING => "String",
        ffi::constants::LIBUSB_DT_INTERFACE => "Interface",
        ffi::constants::LIBUSB_DT_ENDPOINT => "Endpoint",
        ffi::constants::LIBUSB_DT_BOS => "BOS",
        ffi::constants::LIBUSB_DT_DEVICE_CAPABILITY => "Device Capability",
        ffi::constants::LIBUSB_DT_HID => "HID",
        ffi::constants::LIBUSB_DT_REPORT => "Report",
        ffi::constants::LIBUSB_DT_PHYSICAL => "Physical",
        ffi::constants::LIBUSB_DT_HUB => "HUB",
        ffi::constants::LIBUSB_DT_SUPERSPEED_HUB => "Superspeed Hub",
        ffi::constants::LIBUSB_DT_SS_ENDPOINT_COMPANION => "Superspeed Endpoint Companion",
        _ => "",
    }
}

fn get_bcd_version(bcd_version: u16) -> String {
    let digit1 = (bcd_version & 0xF000) >> 12;
    let digit2 = (bcd_version & 0x0F00) >> 8;
    let digit3 = (bcd_version & 0x00F0) >> 4;
    let digit4 = (bcd_version & 0x000F) >> 0;

    if digit1 > 0 {
        format!("{}{}.{}{}", digit1, digit2, digit3, digit4)
    } else {
        format!("{}.{}{}", digit2, digit3, digit4)
    }
}

fn get_class_type(class: u8) -> &'static str {
    match class {
        ffi::constants::LIBUSB_CLASS_PER_INTERFACE => "(Defined at Interface level)",
        ffi::constants::LIBUSB_CLASS_AUDIO => "Audio",
        ffi::constants::LIBUSB_CLASS_COMM => "Comm",
        ffi::constants::LIBUSB_CLASS_HID => "HID",
        ffi::constants::LIBUSB_CLASS_PHYSICAL => "Physical",
        ffi::constants::LIBUSB_CLASS_PRINTER => "Printer",
        ffi::constants::LIBUSB_CLASS_IMAGE => "Image",
        ffi::constants::LIBUSB_CLASS_MASS_STORAGE => "Mass Storage",
        ffi::constants::LIBUSB_CLASS_HUB => "Hub",
        ffi::constants::LIBUSB_CLASS_DATA => "Data",
        ffi::constants::LIBUSB_CLASS_SMART_CARD => "Smart Card",
        ffi::constants::LIBUSB_CLASS_CONTENT_SECURITY => "Content Security",
        ffi::constants::LIBUSB_CLASS_VIDEO => "Video",
        ffi::constants::LIBUSB_CLASS_PERSONAL_HEALTHCARE => "Personal Healthcare",
        ffi::constants::LIBUSB_CLASS_DIAGNOSTIC_DEVICE => "Diagnostic Device",
        ffi::constants::LIBUSB_CLASS_WIRELESS => "Wireless",
        ffi::constants::LIBUSB_CLASS_APPLICATION => "Application",
        ffi::constants::LIBUSB_CLASS_VENDOR_SPEC => "Vendor Specific",
        _ => "",
    }
}

fn get_endpoint(address: u8) -> String {
    let number = address & ffi::constants::LIBUSB_ENDPOINT_ADDRESS_MASK;

    match address & ffi::constants::LIBUSB_ENDPOINT_DIR_MASK {
        ffi::constants::LIBUSB_ENDPOINT_IN => format!("EP {} IN", number),
        ffi::constants::LIBUSB_ENDPOINT_OUT | _ => format!("EP {} OUT", number),
    }
}

fn get_transfer_type(attributes: u8) -> &'static str {
    match attributes & ffi::constants::LIBUSB_TRANSFER_TYPE_MASK {
        ffi::constants::LIBUSB_TRANSFER_TYPE_CONTROL => "Control",
        ffi::constants::LIBUSB_TRANSFER_TYPE_ISOCHRONOUS => "Isochronous",
        ffi::constants::LIBUSB_TRANSFER_TYPE_BULK => "Bulk",
        ffi::constants::LIBUSB_TRANSFER_TYPE_INTERRUPT => "Interrupt",
        ffi::constants::LIBUSB_TRANSFER_TYPE_BULK_STREAM => "Bulk Stream",
        _ => "",
    }
}

fn get_synch_type(attributes: u8) -> &'static str {
    match (attributes & ffi::constants::LIBUSB_ISO_SYNC_TYPE_MASK) >> 2 {
        ffi::constants::LIBUSB_ISO_SYNC_TYPE_NONE => "None",
        ffi::constants::LIBUSB_ISO_SYNC_TYPE_ASYNC => "Async",
        ffi::constants::LIBUSB_ISO_SYNC_TYPE_ADAPTIVE => "Adaptive",
        ffi::constants::LIBUSB_ISO_SYNC_TYPE_SYNC => "Sync",
        _ => "",
    }
}

fn get_usage_type(attributes: u8) -> &'static str {
    match (attributes & ffi::constants::LIBUSB_ISO_USAGE_TYPE_MASK) >> 4 {
        ffi::constants::LIBUSB_ISO_USAGE_TYPE_DATA => "Data",
        ffi::constants::LIBUSB_ISO_USAGE_TYPE_FEEDBACK => "Feedback",
        ffi::constants::LIBUSB_ISO_USAGE_TYPE_IMPLICIT => "Implicit",
        _ => "",
    }
}

fn get_string_descriptor(handle: *mut ffi::libusb_device_handle, desc_index: u8) -> Option<String> {
    if handle.is_null() || desc_index == 0 {
        return None;
    }

    let mut vec = Vec::<u8>::with_capacity(256);
    let ptr = (&mut vec[..]).as_mut_ptr();

    let len = unsafe {
        ffi::libusb_get_string_descriptor_ascii(
            handle,
            desc_index,
            ptr as *mut c_uchar,
            vec.capacity() as c_int,
        )
    };

    if len > 0 {
        unsafe { vec.set_len(len as usize) };

        match String::from_utf8(vec) {
            Ok(s) => Some(s),
            Err(_) => None,
        }
    } else {
        None
    }
}