Ottenere mediante le funzioni esterne del API di Libusb la lista di tutti i dispositivi USB attivi nel sistema

Da Gambas-it.org - Wikipedia.

Per ottenere la lista di tutti i dispositivi USB presenti nel sistema, si possono utilizzare le risorse della libreria esterna libusb-1.0.

In generale questa libreria consente di comunicare dallo spazio utente con i dispositivi USB.

Per poter fruire delle sue risorse in Gambas è necessario avere installata nel sistema e richiamare la libreria condivisa: "libusb-1.0.so.0.3.0 ".


Di seguito mostriamo un semplice esempio per ottenere alcune informazioni circa i dispositivi USB attivi nel BUS del sistema:

Library "libusb-1.0:0.3.0"

Public Struct Libusb_device_descriptor
  bLength As Byte
  bDescriptorType As Byte
  bcdUSB As Short
  bDeviceClass As Byte
  bDeviceSubClass As Byte
  bDeviceProtocol As Byte
  bMaxPacketSize0 As Byte
  idVendor As Short
  idProduct As Short
  bcdDevice As Short
  iManufacturer As Byte
  iProduct As Byte
  iSerialNumber As Byte
  bNumConfigurations As Byte
End Struct

' Speed codes. Indicates the speed at which the device is operating.
Private Enum LIBUSB_SPEED_UNKNOWN = 0, LIBUSB_SPEED_LOW, LIBUSB_SPEED_FULL, LIBUSB_SPEED_HIGH, LIBUSB_SPEED_SUPER

' int libusb_init(libusb_context **ctx)
' Initialize libusb.
Private Extern libusb_init(ctx As Pointer) As Integer

' ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
' Returns a list of USB devices currently attached to the system.
Private Extern libusb_get_device_list(ctx As Pointer, list As Pointer) As Long

' uint8_t libusb_get_bus_number(libusb_device *dev)
' Get the number of the bus that a device is connected to.
Private Extern libusb_get_bus_number(dev As Pointer) As Byte

' uint8_t libusb_get_port_number(libusb_device *dev)
' Returns the port number which the device given by dev is attached to.
Private Extern libusb_get_port_number(dev As Pointer) As Byte

' int libusb_get_device_speed(libusb_device *dev)
' Get the negotiated connection speed for a device.
Private Extern libusb_get_device_speed(dev As Pointer) As Integer

' int libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc)
' Get the USB device descriptor for a given device.
Private Extern libusb_get_device_descriptor(dev As Pointer, desc As Libusb_device_descriptor) As Integer

' uint8_t libusb_get_device_address(libusb_device *dev)
' Get the address of the device on the bus it is connected to.
Private Extern libusb_get_device_address(dev As Pointer) As Byte

' void libusb_free_device_list(libusb_device **list, int unref_devices)
' Frees a list of devices previously discovered using libusb_get_device_list().
Private Extern libusb_free_device_list(list As Pointer, unref_devices As Integer)

' void libusb_exit(libusb_context *ctx)
' Deinitialize libusb.
Private Extern libusb_exit(ctx As Pointer)


Public Sub Main()
 
 Dim ls, disp As Pointer
 Dim desc As New Libusb_device_descriptor
 Dim cnt As Long
 Dim i, vel As Integer
 Dim bus, porta, add As Byte
 Dim vl As String
 
' Inizializza la libreria "libusb-1.0":
 libusb_init(0)
  
' Ottiene un elenco dei dispositivi USB presenti nel bus di sistema:
 cnt = libusb_get_device_list(0, VarPtr(ls))
  
 For i = 0 To cnt - 1
   disp = Pointer@(ls + (i * SizeOf(gb.Pointer)))
   bus = libusb_get_bus_number(disp)
   porta = libusb_get_port_number(disp)
   vel = libusb_get_device_speed(disp)
   Select Case vel
     Case 0
       vl = "Velocità sconosciuta,"
      Case 1
        vl = "low speed    (1.5MBit/s)"
      Case 2
        vl = "full speed    (12MBit/s)"
      Case 3
        vl = "high speed   (480MBit/s)"
      Case 4
        vl = "super speed (5000MBit/s)"
   End Select
   add = libusb_get_device_address(disp)
   libusb_get_device_descriptor(disp, desc)
   
' Mostra le caratteristiche di ciascun dispositivo USB presente nel sistema:
   Print "\n\e[31mDispositivo USB "; Format(add, "#0"); "\e[0m:\nbus "; Format(bus, "000"); ",  porta " &
         Format(porta, "#0"); ",  "; "velocità "; vl; ",  Vendor:Device "; Hex(desc.idVendor, 4); ":"; Hex(desc.idProduct, 4)
 Next 

' Chiude la libreria:
 libusb_free_device_list(ls, 1)
 libusb_exit(0)
 
End

V'è da sottolineare che dai risultati ottenuti è possibile individuare il file-device nodo di ciascun dispositivo USB riportato.
Così, se ad esempio, se nei risultati si ha

Dispositivo USB  1:
bus 003, .......

allora il file-device inode sarà così individuato:

/dev/bus/usb/003/001


Riferimenti