Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here is the correct C Code:
- #include <iostream>
- #include <libusb.h>
- using namespace std;
- void printdev(libusb_device *dev); //prototype of the function
- int main() {
- libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
- libusb_context *ctx = NULL; //a libusb session
- int r; //for return values
- ssize_t cnt; //holding number of devices in list
- r = libusb_init(&ctx); //initialize a library session
- if(r < 0) {
- cout<<"Init Error "<<r<<endl; //there was an error
- return 1;
- }
- libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
- cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
- if(cnt < 0) {
- cout<<"Get Device Error"<<endl; //there was an error
- }
- cout<<cnt<<" Devices in list."<<endl; //print total number of usb devices
- ssize_t i; //for iterating through the list
- for(i = 0; i < cnt; i++) {
- printdev(devs[i]); //print specs of this device
- }
- libusb_free_device_list(devs, 1); //free the list, unref the devices in it
- libusb_exit(ctx); //close the session
- return 0;
- }
- void printdev(libusb_device *dev) {
- libusb_device_descriptor desc;
- int r = libusb_get_device_descriptor(dev, &desc);
- if (r < 0) {
- cout<<"failed to get device descriptor"<<endl;
- return;
- }
- cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<" ";
- cout<<"Device Class: "<<(int)desc.bDeviceClass<<" ";
- cout<<"VendorID: "<<desc.idVendor<<" ";
- cout<<"ProductID: "<<desc.idProduct<<endl;
- libusb_config_descriptor *config;
- libusb_get_config_descriptor(dev, 0, &config);
- cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| ";
- const libusb_interface *inter;
- const libusb_interface_descriptor *interdesc;
- const libusb_endpoint_descriptor *epdesc;
- for(int i=0; i<(int)config->bNumInterfaces; i++) {
- inter = &config->interface[i];
- cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";
- for(int j=0; j<inter->num_altsetting; j++) {
- interdesc = &inter->altsetting[j];
- cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
- cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
- for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
- epdesc = &interdesc->endpoint[k];
- cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
- cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";
- }
- }
- }
- cout<<endl<<endl<<endl;
- libusb_free_config_descriptor(config);
- }
- Here is my partial translation into CFFI. I stopped where I got stuck: In the 'init' function 'devices' does not seem to be initialized correctly to an array of type libusb_device. I am looking for the right way to allocate pointers, pass pointers, or something else entirely that I missed.
- (cffi:define-foreign-library libusb
- (:darwin (:or "libusb.dylib" "libusb-1.0.0.dylib"))
- (:unix (:or "libusb.so"))
- (t (error "unable to load libusb, unknown operating system")))
- (cffi:use-foreign-library libusb)
- (cffi:defcfun "libusb_init" :int (context :pointer))
- (cffi:defcfun "libusb_set_debug" :void (context :pointer) (level :int))
- (cffi:defcfun "libusb_get_device_list" :int (context :pointer) (devices :pointer))
- (cffi:defcfun "libusb_get_device_descriptor" :int (device :pointer) (device-descriptor :pointer))
- (cffi:defcstruct (libusb-device))
- (cffi:defcstruct (libusb-device-descriptor :size 144)
- (b-num-configurations :uint8 :offset 136)
- (b-device-class :uint8 :offset 32)
- (id-vendor :uint16 :offset 72)
- (id-product :uint16 :offset 88))
- (defun init ()
- (let ((devices (cffi:foreign-alloc :pointer))
- (context (cffi:foreign-alloc :pointer))
- (r 0)
- (cnt 0))
- (setf r (libusb-init context))
- (when (< r 0)
- (error "failed to initialize LIBUSB"))
- (setf cnt (libusb-get-device-list context devices))
- (when (< cnt 0)
- (error "Get Device Error"))
- (format t "~A Devices in list.~%" cnt)
- devices))
- (defun print-dev (device-pointer)
- (let ((device-descriptor (cffi:foreign-alloc '(:struct libusb-device-descriptor)))
- (r 0))
- (setf r (libusb-get-device-descriptor device-pointer device-descriptor))
- (when (< r 0)
- (error "failed to get device descriptor"))
- (format t "Number of possible configurations: ~A~%"
- (cffi:foreign-slot-value device-descriptor
- '(:struct libusb-device-descriptor)
- 'b-num-configurations))
- (format t "Device Class: ~A~%"
- (cffi:foreign-slot-value device-descriptor
- '(:struct libusb-device-descriptor)
- 'b-device-class))
- (format t "Vendor ID: ~A~%"
- (cffi:foreign-slot-value device-descriptor
- '(:struct libusb-device-descriptor)
- 'id-vendor))
- (format t "Product ID: ~A~%"
- (cffi:foreign-slot-value device-descriptor
- '(:struct libusb-device-descriptor)
- 'id-product))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement