Ottenere mediante le funzioni esterne della libreria Libudev un elenco degli oggetti hidraw collegati al sistema

Da Gambas-it.org - Wikipedia.
Versione del 16 ott 2024 alle 14:15 di Vuott (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

I dispositivi HID (human interface device) sono quei dispositivi esterni che si relazionano direttamente con gli utenti (ad esempio il mouse).

Nella presente pagina sarà mostrato un codice per ottenere mediante le funzioni esterne della libreria Libudev un elenco degli oggetti hidraw collegati al sistema. E' necessario avere installata e richiamare in Gambas la libreria condivisa: "libudev.so.1.7.9 ".

Library "libudev:1.7.9"

' struct udev *udev_new(void)
' Create udev library context.
Private Extern udev_new() As Pointer

' struct udev_enumerate *udev_enumerate_new(struct udev *udev)
' Create, acquire and release a udev enumerate object.
Private Extern udev_enumerate_new(udev As Pointer) As Pointer

' int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
' Filter for a subsystem of the device to include in the list.
Private Extern udev_enumerate_add_match_subsystem(udev_enumerate As Pointer, subsystem As String) As Integer

' int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
' Query or modify a udev enumerate object.
Private Extern udev_enumerate_scan_devices(udev_enumerate As Pointer) As Integer

' struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
' Query or modify a udev enumerate object.
Private Extern udev_enumerate_get_list_entry(udev_enumerate As Pointer) As Pointer

' struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
' Iterate and access udev lists.
Private Extern udev_list_entry_get_next(list_entry As Pointer) As Pointer

' const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
' Iterate and access udev lists.
Private Extern udev_list_entry_get_name(list_entry As Pointer) As String

' struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
' Create, acquire and release a udev device object.
Private Extern udev_device_new_from_syspath(udev As Pointer, syspath As String) As Pointer

' const char *udev_device_get_devnode(struct udev_device *udev_device)
' Returns the path to the device node itself in /dev.
Private Extern udev_device_get_devnode(udev_device As Pointer) As String

' struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
' Find the next parent device, with a matching subsystem and devtype value, and fill in information from the sys device and the udev database entry.
Private Extern udev_device_get_parent_with_subsystem_devtype(udev_device As Pointer, subsystem As String, devtype As String) As Pointer

' const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
' The retrieved value is cached in the device.
Private Extern udev_device_get_sysattr_value(udev_device As Pointer, sysattr As String) As String

' struct udev_device *udev_device_unref(struct udev_device *udev_device)
' Drop a reference of a udev device.
Private Extern udev_device_unref(udev_device As Pointer) As Pointer

' struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
' Drop a reference of an enumeration context.
Private Extern udev_enumerate_unref(udev_enumerate As Pointer) As Pointer

' struct udev *udev_unref(struct udev *udev)
' Drop a reference of the udev library context.
Private Extern udev_unref(udev As Pointer) As Pointer


Public Sub Main()

 Dim ud, en, ls, dev As Pointer
 Dim d, t As Pointer
 Dim path, node_path As String
 
' Crea un contesto "udev":
 ud = udev_new()
 If ud == 0 Then Error.Raise("Impossibile creare un contesto 'udev' !")
   
' Crea una lista di dispositivi nel sottosistema "scsi":
 en = udev_enumerate_new(ud)
 If en == 0 Then Error.Raise("Errore !")
  
 udev_enumerate_add_match_subsystem(en, "hidraw")
 udev_enumerate_scan_devices(en)
 ls = udev_enumerate_get_list_entry(en)
 If ls == 0 Then Error.Raise("Errore !")
 
' Ottiene il file-descriptor per il monitor:
 dev = ls
  
 Repeat    
' Ottiene il nome del file della voce "/sys" per il dispositivo:
   path = udev_list_entry_get_name(dev)
   d = udev_device_new_from_syspath(ud, path)
     
   node_path = udev_device_get_devnode(d)
   If Not IsNull(node_path) Then Print "Device Node Path: "; node_path
   
   t = udev_device_get_parent_with_subsystem_devtype(d, "usb", "usb_device")
   If t == 0 Then Break
   
   Print "  VID/PID:        "; udev_device_get_sysattr_value(t, "idVendor"); ":"; udev_device_get_sysattr_value(t, "idProduct")
   
   Print "                  "; udev_device_get_sysattr_value(t, "manufacturer"), udev_device_get_sysattr_value(t, "product")
   
   Print "  serial:         "; udev_device_get_sysattr_value(t, "serial")
   Print
    
   udev_device_unref(d)    
   dev = udev_list_entry_get_next(dev)
    
 Until dev == 0
  
' Va in chiusura:
 udev_enumerate_unref(en)
 udev_unref(ud)
  
End


Riferimenti