Skip to content

Instantly share code, notes, and snippets.

@jeamland
Created August 7, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeamland/d8ce7967a7fe5e561178460392c6bb64 to your computer and use it in GitHub Desktop.
Save jeamland/d8ce7967a7fe5e561178460392c6bb64 to your computer and use it in GitHub Desktop.
Benno's Misadventures in libusb
$ uname -a
Darwin benno-mac.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64$ cc -o thing -I/usr/local/include/libusb-1.0 -L/usr/local/lib thing.c -lusb-1.0
$ ./thing
thing: set config failed: No such device (it may have been disconnected): No such file or directory
thing: claim interface failed: Access denied (insufficient permissions)
#include <err.h>
#include <stdio.h>
#include <libusb.h>
int
main(void)
{
libusb_context *ctx;
libusb_device_handle *handle;
libusb_device *device;
struct libusb_device_descriptor desc;
int err;
if (libusb_init(&ctx) != 0) {
errx(1, "failed to init");
}
handle = libusb_open_device_with_vid_pid(NULL, 0x1050, 0x0406);
if (handle == NULL) {
errx(1, "open failed");
}
device = libusb_get_device(handle);
libusb_get_device_descriptor(device, &desc);
if ((err = libusb_set_configuration(handle, 1)) != 0) {
warn("set config failed: %s", libusb_strerror(err));
}
if ((err = libusb_claim_interface(handle, 1)) != 0) {
errx(1, "claim interface failed: %s", libusb_strerror(err));
}
libusb_exit(ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment