It is currently Tue Oct 23, 2018 8:27 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: libudev usage for linux
PostPosted: Sun Sep 09, 2018 10:05 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 3979
Location: Germany
Hi,

since I needed an autodetection of an usb device ...
Code:
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  ImportC "/lib/i386-linux-gnu/libudev.so"
CompilerElse
  ImportC "/lib/x86_64-linux-gnu/libudev.so"
CompilerEndIf
  udev_new.i()
  udev_unref.i(*udev)
  udev_enumerate_new.i(*udev)
  udev_enumerate_add_match_subsystem.i(*enumerate, subsystem.p-UTF8)
  udev_enumerate_scan_devices.i(*enumerate)
  udev_enumerate_get_list_entry.i(*enumerate)
  udev_list_entry_get_next.i(*list_entry)
  udev_list_entry_get_name_.i(*list_entry) As "udev_list_entry_get_name"
  udev_device_new_from_syspath.i(*udev, path.p-UTF8)
  udev_device_get_devnode_.i(*dev) As "udev_device_get_devnode"
  udev_device_get_parent_with_subsystem_devtype.i(*dev, subsystem.p-UTF8, devtype.p-UTF8)
  udev_device_get_sysattr_value_.i(*dev, sysattr.p-UTF8) As "udev_device_get_sysattr_value"
  udev_device_unref.i(*dev)
  udev_enumerate_unref.i(*enumerate)
EndImport


Procedure.s udev_list_entry_get_name(*list_entry)
 
  Protected *path, Result$
 
 
  *path = udev_list_entry_get_name_(*list_entry)
  If *path
    Result$ = PeekS(*path, -1, #PB_UTF8)
  EndIf
 
  ProcedureReturn Result$
 
EndProcedure


Procedure.s udev_device_get_devnode(*dev)
 
  Protected *DevNode, Result$
 
 
  *DevNode = udev_device_get_devnode_(*dev)
  If *DevNode
    Result$ = PeekS(*DevNode, -1, #PB_UTF8)
  EndIf
 
  ProcedureReturn Result$
 
EndProcedure


Procedure.s udev_device_get_sysattr_value(*dev, sysattr$)
 
  Protected *Value, Result$
 
 
  *Value = udev_device_get_sysattr_value_(*dev, sysattr$)
  If *Value
    Result$ = PeekS(*Value, -1, #PB_UTF8)
  EndIf
 
  ProcedureReturn Result$
 
EndProcedure




CompilerIf #PB_Compiler_IsMainFile
     
  Define udev.i, enumerate.i, devices.i, list_entry.i, path$, dev.i
  Define usb_DevicePath.i, usb_DevicePath$, value$
 
 
  Define usb_handler.i, Count.i
  Dim usb_buffer.a(64)
 
 
  udev = udev_new()
  If udev
   
    ;Debug udev
   
    enumerate = udev_enumerate_new(udev)
   
    ;Debug enumerate
   
    udev_enumerate_add_match_subsystem(enumerate, "hidraw")
    udev_enumerate_scan_devices(enumerate)
    devices = udev_enumerate_get_list_entry(enumerate)
    ;Debug devices
   
    list_entry = devices
    While list_entry
      ;Debug list_entry
     
      path$ = udev_list_entry_get_name(list_entry)
      Debug path$
     
      dev = udev_device_new_from_syspath(udev, path$)
      usb_DevicePath$ = udev_device_get_devnode(dev)
      Debug usb_DevicePath$
     
      dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device")
      If dev
        value$ = udev_device_get_sysattr_value(dev, "product")
        If value$ <> ""
          Debug value$
        EndIf
        udev_device_unref(dev)
      EndIf
     
      list_entry = udev_list_entry_get_next(list_entry)
    Wend
   
    udev_enumerate_unref(enumerate)
   
    udev_unref(udev)
  EndIf
 
CompilerEndIf

Bernd


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye