Advertisement
Guest User

C to CFFI 2

a guest
Mar 6th, 2018
120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Here is the correct C Code:
  2. #include <iostream>
  3. #include <libusb.h>
  4. using namespace std;
  5.  
  6. void printdev(libusb_device *dev); //prototype of the function
  7.  
  8. int main() {
  9. libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
  10. libusb_context *ctx = NULL; //a libusb session
  11. int r; //for return values
  12. ssize_t cnt; //holding number of devices in list
  13. r = libusb_init(&ctx); //initialize a library session
  14. if(r < 0) {
  15. cout<<"Init Error "<<r<<endl; //there was an error
  16. return 1;
  17. }
  18. libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
  19. cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
  20. if(cnt < 0) {
  21. cout<<"Get Device Error"<<endl; //there was an error
  22. }
  23. cout<<cnt<<" Devices in list."<<endl; //print total number of usb devices
  24. ssize_t i; //for iterating through the list
  25. for(i = 0; i < cnt; i++) {
  26. printdev(devs[i]); //print specs of this device
  27. }
  28. libusb_free_device_list(devs, 1); //free the list, unref the devices in it
  29. libusb_exit(ctx); //close the session
  30. return 0;
  31. }
  32.  
  33. void printdev(libusb_device *dev) {
  34. libusb_device_descriptor desc;
  35. int r = libusb_get_device_descriptor(dev, &desc);
  36. if (r < 0) {
  37. cout<<"failed to get device descriptor"<<endl;
  38. return;
  39. }
  40. cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<" ";
  41. cout<<"Device Class: "<<(int)desc.bDeviceClass<<" ";
  42. cout<<"VendorID: "<<desc.idVendor<<" ";
  43. cout<<"ProductID: "<<desc.idProduct<<endl;
  44. libusb_config_descriptor *config;
  45. libusb_get_config_descriptor(dev, 0, &config);
  46. cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| ";
  47. const libusb_interface *inter;
  48. const libusb_interface_descriptor *interdesc;
  49. const libusb_endpoint_descriptor *epdesc;
  50. for(int i=0; i<(int)config->bNumInterfaces; i++) {
  51. inter = &config->interface[i];
  52. cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";
  53. for(int j=0; j<inter->num_altsetting; j++) {
  54. interdesc = &inter->altsetting[j];
  55. cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
  56. cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
  57. for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
  58. epdesc = &interdesc->endpoint[k];
  59. cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
  60. cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";
  61. }
  62. }
  63. }
  64. cout<<endl<<endl<<endl;
  65. libusb_free_config_descriptor(config);
  66. }
  67.  
  68. 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.
  69.  
  70. (cffi:define-foreign-library libusb
  71. (:darwin (:or "libusb.dylib" "libusb-1.0.0.dylib"))
  72. (:unix (:or "libusb.so"))
  73. (t (error "unable to load libusb, unknown operating system")))
  74.  
  75. (cffi:use-foreign-library libusb)
  76.  
  77. (cffi:defcfun "libusb_init" :int (context :pointer))
  78. (cffi:defcfun "libusb_set_debug" :void (context :pointer) (level :int))
  79. (cffi:defcfun "libusb_get_device_list" :int (context :pointer) (devices :pointer))
  80. (cffi:defcfun "libusb_get_device_descriptor" :int (device :pointer) (device-descriptor :pointer))
  81.  
  82. (cffi:defcstruct (libusb-device))
  83. (cffi:defcstruct (libusb-device-descriptor :size 144)
  84. (b-num-configurations :uint8 :offset 136)
  85. (b-device-class :uint8 :offset 32)
  86. (id-vendor :uint16 :offset 72)
  87. (id-product :uint16 :offset 88))
  88.  
  89. (defun init ()
  90. (let ((devices (cffi:foreign-alloc :pointer))
  91. (context (cffi:foreign-alloc :pointer))
  92. (r 0)
  93. (cnt 0))
  94. (setf r (libusb-init context))
  95. (when (< r 0)
  96. (error "failed to initialize LIBUSB"))
  97. (setf cnt (libusb-get-device-list context devices))
  98. (when (< cnt 0)
  99. (error "Get Device Error"))
  100. (format t "~A Devices in list.~%" cnt)
  101. devices))
  102.  
  103. (defun print-dev (device-pointer)
  104. (let ((device-descriptor (cffi:foreign-alloc '(:struct libusb-device-descriptor)))
  105. (r 0))
  106. (setf r (libusb-get-device-descriptor device-pointer device-descriptor))
  107. (when (< r 0)
  108. (error "failed to get device descriptor"))
  109. (format t "Number of possible configurations: ~A~%"
  110. (cffi:foreign-slot-value device-descriptor
  111. '(:struct libusb-device-descriptor)
  112. 'b-num-configurations))
  113. (format t "Device Class: ~A~%"
  114. (cffi:foreign-slot-value device-descriptor
  115. '(:struct libusb-device-descriptor)
  116. 'b-device-class))
  117. (format t "Vendor ID: ~A~%"
  118. (cffi:foreign-slot-value device-descriptor
  119. '(:struct libusb-device-descriptor)
  120. 'id-vendor))
  121. (format t "Product ID: ~A~%"
  122. (cffi:foreign-slot-value device-descriptor
  123. '(:struct libusb-device-descriptor)
  124. 'id-product))))
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement