Seite 1 von 2

libusb unter Windows mit PureBasic

Verfasst: 22.05.2008 18:20
von topsoft
Hallo,

ich versuche gerade folgenden Code zur Ansteuerung der "libusb" nach PureBasic zu konvertieren.

Code: Alles auswählen

static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName)
{
struct usb_bus      *bus;
struct usb_device   *dev;
usb_dev_handle      *handle = NULL;
int                 errorCode = USB_ERROR_NOTFOUND;
static int          didUsbInit = 0;

    if(!didUsbInit){
        didUsbInit = 1;
        usb_init();
    }
    usb_find_busses();
    usb_find_devices();
    for(bus=usb_get_busses(); bus; bus=bus->next){
        for(dev=bus->devices; dev; dev=dev->next){
            if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){
                char    string[256];
                int     len;
                handle = usb_open(dev); /* we need to open the device in order to query strings */
                if(!handle){
                    errorCode = USB_ERROR_ACCESS;
                    fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
                    continue;
                }
                if(vendorName == NULL && productName == NULL){  /* name does not matter */
                    break;
                }
                /* now check whether the names match: */
                len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
                if(len < 0){
                    errorCode = USB_ERROR_IO;
                    fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
                }else{
                    errorCode = USB_ERROR_NOTFOUND;
                    /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
                    if(strcmp(string, vendorName) == 0){
                        len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
                        if(len < 0){
                            errorCode = USB_ERROR_IO;
                            fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
                        }else{
                            errorCode = USB_ERROR_NOTFOUND;
                            /* fprintf(stderr, "seen product ->%s<-\n", string); */
                            if(strcmp(string, productName) == 0)
                                break;
                        }
                    }
                }
                usb_close(handle);
                handle = NULL;
            }
        }
        if(handle)
            break;
    }
    if(handle != NULL){
        errorCode = 0;
        *device = handle;
    }
    return errorCode;
}
Was ich bis jetzt habe ist:

Code: Alles auswählen

*lib = OpenLibrary(#PB_Any, "libusb0.dll")
If *lib
  Debug "open lib"
  Debug CallFunction(*lib, "usb_init")
  Anz_Bus = CallFunction(*lib, "usb_find_busses")
  Debug Anz_Bus
  Anz_Dev = CallFunction(*lib, "usb_find_devices")
  Debug Anz_Dev
    
  
  
  
  CloseLibrary(*lib)
  Debug "close lib"
EndIf
End
Leider habe ich keine Ahnung wie folgende Zeilen nach PB portiert werden können.

Code: Alles auswählen

    for(bus=usb_get_busses(); bus; bus=bus->next){ 
        for(dev=bus->devices; dev; dev=dev->next){ 
            if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){
Kann mir hier jemand helfen und hat das schon mal jemand gemacht?

Die Suchfunktion hat mit "libusb" leider nichts brauchbares ausgespuckt.

Gruß Rene

Verfasst: 22.05.2008 19:18
von edel
Du solltest erst mal die Strukturen nach pb portieren.
Die Schleifen koenntest du so umsetzen :

Code: Alles auswählen

*busses.usb_bus = usb_get_busses()

Repeat
  *dev.usb_device = *busses\devices
  
  Repeat

    *dev = *dev\next
  Until *dev = 0
  
  *busses = *busses\next
Until *busses = 0

Verfasst: 22.05.2008 19:41
von topsoft
Hi,
danke für die schnelle Antwort. Die Strukturen lauten:

Code: Alles auswählen

struct usb_device {
  struct usb_device *next, *prev;

  char filename[PATH_MAX + 1];

  struct usb_bus *bus;

  struct usb_device_descriptor descriptor;
  struct usb_config_descriptor *config;
};

struct usb_bus {
  struct usb_bus *next, *prev;

  char dirname[PATH_MAX + 1];

  struct usb_device *devices;
};

struct usb_device_descriptor {
	u_int8_t  bLength;
	u_int8_t  bDescriptorType;
	u_int16_t bcdUSB;
	u_int8_t  bDeviceClass;
	u_int8_t  bDeviceSubClass;
	u_int8_t  bDeviceProtocol;
	u_int8_t  bMaxPacketSize0;
	u_int16_t idVendor;
	u_int16_t idProduct;
	u_int16_t bcdDevice;
	u_int8_t  iManufacturer;
	u_int8_t  iProduct;
	u_int8_t  iSerialNumber;
	u_int8_t  bNumConfigurations;
} __attribute__ ((packed));

struct usb_config_descriptor {
	u_int8_t  bLength;
	u_int8_t  bDescriptorType;
	u_int16_t wTotalLength;
	u_int8_t  bNumInterfaces;
	u_int8_t  bConfigurationValue;
	u_int8_t  iConfiguration;
	u_int8_t  bmAttributes;
	u_int8_t  MaxPower;
} __attribute__ ((packed));
Oh man, Zeiger auf Zeiger und keine Ahnung was "__attribute__ ((packed));" zu bedeuten hat. Hilfe .....

Gruß Rene

Verfasst: 22.05.2008 19:54
von edel
NicTheQuick hatte das Problem auch schon, vielleicht hat er den
Code ja noch.

http://www.purebasic.fr/german/viewtopic.php?t=14436

Verfasst: 25.05.2008 20:47
von topsoft
Hallo,

habe das Problem jetzt auf eine sehr unelegante Weise selber gelöst.

Code: Alles auswählen

#PATH_MAX = 512

Structure usb_device_descriptor
	bLength.c
	bDescriptorType.c
	bcdUSB.w
	bDeviceClass.c
	bDeviceSubClass.c
	bDeviceProtocol.c
	bMaxPacketSize0.c
	idVendor.w
	idProduct.w
	bcdDevice.w
	iManufacturer.c
	iProduct.c
	iSerialNumber.c
	bNumConfigurations.c
EndStructure

Structure usb_config_descriptor
	bLength.c
	bDescriptorType.c
	wTotalLength.w
	bNumInterfaces.c
	bConfigurationValue.c
	iConfiguration.c
	bmAttributes.c
	MaxPower.c
EndStructure

Structure usb_bus
  *next                                                                                      ;Zeiger auf eine Structure 'usb_bus'
  *prev                                                                                      ;Zeiger auf eine Structure 'usb_bus'
  dirname.c[#PATH_MAX + 1]
  *devices                                                                                   ;Zeiger auf eine Structure 'usb_device'
EndStructure

Structure usb_device
  *next                                                                                      ;Zeiger auf eine Structure 'usb_device'
  *prev                                                                                      ;Zeiger auf eine Structure 'usb_device'
  filename.c[#PATH_MAX + 1]
  *bus                                                                                       ;Zeiger auf eine Structure 'usb_bus'
  descriptor.usb_device_descriptor
  *config                                                                                    ;Zeiger auf eine Structure 'usb_config_descriptor'
EndStructure

Procedure.s bcd_w (wert.l)
  ret.s = Chr((wert >> 12) & $F + 48)
  ret + Chr((wert >> 8) & $F + 48)
  ret + "."
  ret + Chr((wert >> 4) & $F + 48)
  ret + Chr(wert & $F + 48)
  ProcedureReturn ret
EndProcedure

hLib.l = OpenLibrary(#PB_Any, "libusb0.dll")
;Debug hLib
If hLib
  Debug "libusb0.dll open"
  
  status_usb_init = CallFunction(hLib, "usb_init")
  Debug  "usb_init: " + Str(status_usb_init)
  If status_usb_init
    anz_bus = CallFunction(hLib, "usb_find_busses")
    Debug "Anzahl Bus: " + Str(anz_bus)
    anz_dev = CallFunction(hLib, "usb_find_devices")
    Debug "Anzahl Devices: " + Str(anz_dev)
    Debug ""

    *busses = CallFunction(hLib, "usb_get_busses")
    While *busses
      *nex_t = PeekL(*busses)
      ;Debug *busses
      Debug "Busname: " + PeekS(*busses + 8)                                                  ;dirname
      *devices = PeekL(*busses + 8 + #PATH_MAX)
    
      While *devices
        *next_device = PeekL(*devices)
      
        ;usb_device
        Debug ""
        Debug "usb_device"
        Debug "Filename: " + PeekS(*devices + 8)                                              ;filename
      
        ;usb_config_descriptor
        Debug "usb_config_descriptor"
        *Addr = PeekL(*devices + 8 + #PATH_MAX + 4 + 18)
        Debug "bLength: " + StrU(PeekC(*Addr + 0), #Byte)                                     ;bLength
        Debug "bDescriptorType: " + StrU(PeekC(*Addr + 1), #Byte)                             ;bDescriptorType
        Debug "wTotalLength: " + StrU(PeekW(*Addr + 2), #Word)                                ;wTotalLength
        Debug "bNumInterfaces: " + StrU(PeekC(*Addr + 4), #Byte)                              ;bNumInterfaces
        Debug "bConfigurationValue: " + StrU(PeekC(*Addr + 5), #Byte)                         ;bConfigurationValue
        Debug "iConfiguration: " + StrU(PeekC(*Addr + 6), #Byte)                              ;iConfiguration
        Debug "bmAttributes: " + StrU(PeekC(*Addr + 7), #Byte)                                ;bmAttributes
        Debug "MaxPower: " + StrU(PeekC(*Addr + 8), #Byte)                                    ;MaxPower
      
        ;descriptor.usb_device_descriptor
        Debug "usb_device_descriptor"
        Debug "bLength: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 0), #Byte)              ;bLength
        Debug "bDescriptorType: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 1), #Byte)      ;bDescriptorType
        Debug "bcd: " + bcd_w(PeekW(*devices + 8 + #PATH_MAX + 4 + 2))                        ;bcdUSB
        Debug "bDeviceClass: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 4), #Byte)         ;bDeviceClass
        Debug "bDeviceSubClass: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 5), #Byte)      ;bDeviceSubClass
        Debug "bDeviceProtocol: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 6), #Byte)      ;bDeviceProtocol      ;
        Debug "bMaxPacketSize0: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 7), #Byte)      ;bMaxPacketSize0
        Debug "idVendor: " + Hex(PeekW(*devices + 8 + #PATH_MAX + 4 + 8))                     ;idVendor
        Debug "idProduct: " + Hex(PeekW(*devices + 8 + #PATH_MAX + 4 + 10))                   ;idProduct
        Debug "bcdDevice: " + bcd_w(PeekW(*devices + 8 + #PATH_MAX + 4 + 12))                 ;bcdDevice
        Debug "iManufacturer: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 14), #Byte)       ;iManufacturer
        Debug "iProduct: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 15), #Byte)            ;iProduct
        Debug "iSerialNumber: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 16), #Byte)       ;iSerialNumber
        Debug "bNumConfigurations: " + StrU(PeekC(*devices + 8 + #PATH_MAX + 4 + 17), #Byte)  ;bNumConfigurations
        *devices = *next_device
      Wend
    
      *busses = *nex_t
      Debug ""
    Wend
  EndIf
  
  CloseLibrary(hLib)
  hLib = 0
  Debug "libusb0.dll close"
Else
  Debug "Error, open libusb0.dll"
EndIf
End
Hier mal einen Ausgabe des Programmes auf meinem Rechner:

Code: Alles auswählen

libusb0.dll open
usb_init: 1
Anzahl Bus: 7
Anzahl Devices: 1

Busname: bus-0

usb_device
Filename: \\.\libusb0-0000--0x16c0-0x05dc
usb_config_descriptor
bLength: 9
bDescriptorType: 2
wTotalLength: 18
bNumInterfaces: 1
bConfigurationValue: 1
iConfiguration: 0
bmAttributes: 64
MaxPower: 10
usb_device_descriptor
bLength: 18
bDescriptorType: 1
bcd: 01.10
bDeviceClass: 255
bDeviceSubClass: 0
bDeviceProtocol: 0
bMaxPacketSize0: 8
idVendor: 16C0
idProduct: 5DC
bcdDevice: 01.00
iManufacturer: 1
iProduct: 2
iSerialNumber: 0
bNumConfigurations: 1

Busname: bus-1

Busname: bus-2

Busname: bus-3

Busname: bus-4

Busname: bus-5

Busname: bus-6

libusb0.dll close
Es funktioniert soweit aber ich würde schon gern dem Compiler mitteilen das der Rückgabewert der Funktion usb_get_busses ein Zeiger auf eine Structur usb_bus ist. Dann kann ich die unübersichtliche Offsetberechnung dem Compiler überlassen. Leider habe ich keine Ahnung wie das in PB geht.

Gruß Rene

Verfasst: 25.05.2008 21:15
von edel
In dem du die Struktur an den Pointer haengst

Code: Alles auswählen

*busses.usb_bus = function()

Re: libusb unter Windows mit PureBasic

Verfasst: 13.10.2011 13:53
von DNA
Da keiner anscheinend vollständige die libusb in pb portiert hat und ich das grad brauche, hier zum einen die lusb0_usb.h vollständig und dann noch einmal angefangen die dll befehle zu mappen, aber da hänge ich grad fest.

lusb0_usb.h

Code: Alles auswählen

#LIBUSB_PATH_MAX = 512

; Device And/Or Interface Class codes

#USB_CLASS_PER_INTERFACE =   0  ; For DeviceClass
#USB_CLASS_AUDIO         =   1
#USB_CLASS_COMM          =   2
#USB_CLASS_HID           =   3
#USB_CLASS_PRINTER       =   7
#USB_CLASS_MASS_STORAGE  =   8
#USB_CLASS_HUB           =   9
#USB_CLASS_DATA          =  10
#USB_CLASS_VENDOR_SPEC   = $ff

; Descriptor types
#USB_DT_DEVICE    = $01
#USB_DT_CONFIG    = $02
#USB_DT_STRING    = $03
#USB_DT_INTERFACE = $04
#USB_DT_ENDPOINT  = $05

#USB_DT_HID      = $21
#USB_DT_REPORT   = $22
#USB_DT_PHYSICAL = $23
#USB_DT_HUB      = $29

; Descriptor sizes per descriptor type
#USB_DT_DEVICE_SIZE         =  18
#USB_DT_CONFIG_SIZE         =   9
#USB_DT_INTERFACE_SIZE      =   9
#USB_DT_ENDPOINT_SIZE       =   7
#USB_DT_ENDPOINT_AUDIO_SIZE =   9  ; Audio extension
#USB_DT_HUB_NONVAR_SIZE     =   7


; All standard descriptors have these 2 fields in common
Structure usb_descriptor_header
  bLength.c
  bDescriptorType.c
EndStructure

; String descriptor
Structure usb_string_descriptor
  bLength.c
  bDescriptorType.c
  wData.u[1]
EndStructure

; HID descriptor
Structure usb_hid_descriptor
  bLength.c
  bDescriptorType.c
  bcdHID.u
  bCountryCode.c
  bNumDescriptors.c
EndStructure

; Endpoint descriptor
#USB_MAXENDPOINTS  = 32
Structure usb_endpoint_descriptor
  bLength.c
  bDescriptorType.c
  bEndpointAddress.c
  bmAttributes.c
  wMaxPacketSize.u
  bInterval.c
  bRefresh.c
  bSynchAddress.c

  *extra.c ; Extra descriptors
  extralen.l
EndStructure

#USB_ENDPOINT_ADDRESS_MASK     = $0f    ; in bEndpointAddress
#USB_ENDPOINT_DIR_MASK         = $80

#USB_ENDPOINT_TYPE_MASK        = $03    ; in bmAttributes
#USB_ENDPOINT_TYPE_CONTROL     =   0
#USB_ENDPOINT_TYPE_ISOCHRONOUS =   1
#USB_ENDPOINT_TYPE_BULK        =   2
#USB_ENDPOINT_TYPE_INTERRUPT   =   3

; Interface descriptor
#USB_MAXINTERFACES = 32
Structure usb_interface_descriptor
  bLength.c
  bDescriptorType.c
  bInterfaceNumber.c
  bAlternateSetting.c
  bNumEndpoints.c
  bInterfaceClass.c
  bInterfaceSubClass.c
  bInterfaceProtocol.c
  iInterface.c

  *endpoint.usb_endpoint_descriptor

  *extra.c ; Extra descriptors
  extralen.l
EndStructure

#USB_MAXALTSETTING = 128  ; Hard limit

Structure usb_interface
  *altsetting.usb_interface_descriptor

  num_altsetting.l
EndStructure

; Configuration descriptor information..
#USB_MAXCONFIG = 8
Structure usb_config_descriptor
  bLength.c
  bDescriptorType.c
  wTotalLength.u
  bNumInterfaces.c
  bConfigurationValue.c
  iConfiguration.c
  bmAttributes.c
  MaxPower.c

  *Interface.usb_interface

  *extra.c  ; Extra descriptors
  extralen.l
EndStructure

; Device descriptor
Structure usb_device_descriptor
  bLength.c
  bDescriptorType.c
  bcdUSB.u
  bDeviceClass.c
  bDeviceSubClass.c
  bDeviceProtocol.c
  bMaxPacketSize0.c
  idVendor.u
  idProduct.u
  bcdDevice.u
  iManufacturer.c
  iProduct.c
  iSerialNumber.c
  bNumConfigurations.c
EndStructure

Structure usb_ctrl_setup
  bRequestType.c
  bRequest.c
  wValue.u
  wIndex.u
  wLength.u
EndStructure

; Standard requests

#USB_REQ_GET_STATUS         = $00
#USB_REQ_CLEAR_FEATURE      = $01
; $02 is reserved
#USB_REQ_SET_FEATURE        = $03
; $04 is reserved
#USB_REQ_SET_ADDRESS        = $05
#USB_REQ_GET_DESCRIPTOR     = $06
#USB_REQ_SET_DESCRIPTOR     = $07
#USB_REQ_GET_CONFIGURATION  = $08
#USB_REQ_SET_CONFIGURATION  = $09
#USB_REQ_GET_INTERFACE      = $0A
#USB_REQ_SET_INTERFACE      = $0B
#USB_REQ_SYNCH_FRAME        = $0C

#USB_TYPE_STANDARD          = ($00 << 5)
#USB_TYPE_CLASS             = ($01 << 5)
#USB_TYPE_VENDOR            = ($02 << 5)
#USB_TYPE_RESERVED          = ($03 << 5)

#USB_RECIP_DEVICE           = $00
#USB_RECIP_INTERFACE        = $01
#USB_RECIP_ENDPOINT         = $02
#USB_RECIP_OTHER            = $03

; Various libusb API related stuff

#USB_ENDPOINT_IN    = $80
#USB_ENDPOINT_OUT   = $00

; Error codes
#USB_ERROR_BEGIN    = 500000

; This is supposed To look weird. This file is generated from autoconf
; And I didn't want to make this too complicated.

; #USB_LE16_TO_CPU(x)

; Device reset types For usb_reset_ex.
; http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx
; http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx

#USB_RESET_TYPE_RESET_PORT = (1 << 0)
#USB_RESET_TYPE_CYCLE_PORT = (1 << 1)
#USB_RESET_TYPE_FULL_RESET = (#USB_RESET_TYPE_CYCLE_PORT | #USB_RESET_TYPE_RESET_PORT)


; Data types
; struct usb_device
; struct usb_bus

Structure usb_device
  *next.usb_device
  *prev.usb_device

  filename.c[#LIBUSB_PATH_MAX]

  *bus.usb_bus

  descriptor.usb_device_descriptor
  *config.usb_config_descriptor

  *dev.l  ; Darwin support

  devnum.c

  num_children.c
  *children.usb_device
EndStructure

Structure usb_bus
  *next.usb_bus
  *prev.usb_bus

  dirname.c[#LIBUSB_PATH_MAX]

  *devices.usb_device
  location.l

  *root_dev.usb_device
EndStructure

; Version information, Windows specific
Structure dll
  major.l
  minor.l
  micro.l
  nano.l
EndStructure

Structure driver
  major.l
  minor.l
  micro.l
  nano.l
EndStructure
  
Structure usb_version
  dll.dll
  driver.driver
EndStructure

Structure usb_dev_handle
  fd.l

  *bus.usb_bus
  *device.usb_device

  config.l
  hinterface.l
  altsetting.l

  ; Added by RMT so implementations can store other per-open-device Data
  *impl_info.l
EndStructure

libusb0.dll (Mapper) not complete tested!!!

Code: Alles auswählen

EnableExplicit

IncludeFile "usbHeader.pb" ;constants and structures we need for libusb0.dll

Global libUsbID = OpenLibrary(#PB_Any, "libusb0.dll")

;Define Function-Pointers for CallFunctionFast
If IsLibrary(libUsbID)

  Define _usb_get_version                = GetFunction(libUsbID, "usb_get_version")

  ;-Core Functions
  Define _usb_init                       = GetFunction(libUsbID, "usb_init")
  Define _usb_find_busses                = GetFunction(libUsbID, "usb_find_busses")
  Define _usb_find_devices               = GetFunction(libUsbID, "usb_find_devices")
  Define _usb_get_busses                 = GetFunction(libUsbID, "usb_get_busses")

  ;-Device operations
  Define _usb_open                       = GetFunction(libUsbID, "usb_open")
  Define _usb_close                      = GetFunction(libUsbID, "usb_close")
  Define _usb_set_configuration          = GetFunction(libUsbID, "usb_set_configuration")
  Define _usb_set_altinterface           = GetFunction(libUsbID, "usb_set_altinterface")
  Define _usb_resetep                    = GetFunction(libUsbID, "usb_resetep")
  Define _usb_clear_halt                 = GetFunction(libUsbID, "usb_clear_halt")
  Define _usb_reset                      = GetFunction(libUsbID, "usb_reset")
  Define _usb_claim_interface            = GetFunction(libUsbID, "usb_claim_interface")
  Define _usb_release_interface          = GetFunction(libUsbID, "usb_release_interface")

  ;-Control Transfer
  Define _usb_control_msg                = GetFunction(libUsbID, "usb_control_msg")
  Define _usb_get_string                 = GetFunction(libUsbID, "usb_get_string")
  Define _usb_get_string_simple          = GetFunction(libUsbID, "usb_get_string_simple")
  Define _usb_get_descriptor             = GetFunction(libUsbID, "usb_get_descriptor")
  Define _usb_get_descriptor_by_endpoint = GetFunction(libUsbID, "usb_get_descriptor_by_endpoint")

  ;-Bulk Transfer
  Define _usb_bulk_write                 = GetFunction(libUsbID, "usb_bulk_write")
  Define _usb_bulk_read                  = GetFunction(libUsbID, "usb_bulk_read")

  ;-Interrupt Transfer
  Define _usb_interrupt_write            = GetFunction(libUsbID, "usb_interrupt_write")
  Define _usb_interrupt_read             = GetFunction(libUsbID, "usb_interrupt_read")

  ;-Non Portable
  Define _usb_get_driver_np              = GetFunction(libUsbID, "usb_get_driver_np")
  Define _usb_detach_kernel_driver_np    = GetFunction(libUsbID, "usb_detach_kernel_driver_np")

EndIf

; ----------------------------------------------------------------------

; allready tested and works fine

Procedure usb_init()
  Shared _usb_init
  
  ProcedureReturn CallFunctionFast(_usb_init)
EndProcedure

Procedure usb_find_busses()
  Shared _usb_find_busses
  
  ProcedureReturn CallFunctionFast(_usb_find_busses)
EndProcedure

Procedure usb_find_devices()
  Shared _usb_find_devices
  
  ProcedureReturn CallFunctionFast(_usb_find_devices)
EndProcedure

Procedure usb_get_busses()
  ProcedureReturn CallFunction(libUsbID, "usb_get_busses")
EndProcedure

Procedure usb_get_version()
  ProcedureReturn CallFunction(libUsbID, "usb_get_version")
EndProcedure

; ----------------------------------------------------------------------

; Don't work for me, but don't know why :(

Procedure usb_open(*dev.usb_device) ;returns *usb_dev_handle
  Shared _usb_open

  If *dev = 0
    ProcedureReturn 0
  EndIf

  ProcedureReturn CallFunctionFast(_usb_open, *dev)
EndProcedure

; Not Tested

Procedure usb_close(*dev.usb_dev_handle) ;returns 0 if success
  Shared _usb_close

  If *dev = 0
    ProcedureReturn -1
  EndIf

  ProcedureReturn CallFunctionFast(_usb_close, *dev)
EndProcedure

Procedure usb_set_configuration(*dev.usb_dev_handle, configuration.l) ;returns 0 if success
  Shared _usb_set_configuration

  If *dev = 0
    ProcedureReturn -1
  EndIf

  ProcedureReturn CallFunctionFast(_usb_set_configuration, *dev, configuration)
EndProcedure

Procedure usb_set_altinterface(*dev.usb_dev_handle, alternate.l)
  Shared _usb_set_altinterface

  If *dev = 0
    ProcedureReturn -1
  EndIf

  ProcedureReturn CallFunctionFast(_usb_set_altinterface, *dev, alternate)
EndProcedure

; usb_resetep is deprecated. Use usb_clear_halt
Procedure usb_resetep(*dev.usb_dev_handle, ep.l)
  Shared _usb_resetep

  If *dev = 0
    ProcedureReturn -1
  EndIf

  ProcedureReturn CallFunctionFast(_usb_resetep, *dev, ep)
EndProcedure

Procedure usb_clear_halt(*dev.usb_dev_handle, ep.l)
  Shared _usb_clear_halt
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_clear_halt, *dev, ep)
EndProcedure

Procedure usb_reset(*dev.usb_dev_handle)
  Shared _usb_reset
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_reset, *dev)
EndProcedure

Procedure usb_claim_interface(*dev.usb_dev_handle, hInterface.l)
  Shared _usb_claim_interface
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_claim_interface, *dev, hInterface)
EndProcedure

Procedure usb_release_interface(*dev.usb_dev_handle, hInterface)
  Shared _usb_release_interface
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_release_interface, *dev, hInterface)
EndProcedure

Procedure usb_control_msg(*dev.usb_dev_handle, requesttype.l, request.l, value.l, index.l, *bytes.c, size.l, timeout.l)
  Shared _usb_control_msg
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_control_msg, *dev, requesttype, request, value, index, *bytes, size, timeout)
EndProcedure

Procedure usb_get_string(*dev.usb_dev_handle, index.l, langid.l, *buf.c, buflen.i)
  Shared _usb_get_string
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_get_string, *dev, index, langid, *buf, buflen)
EndProcedure

Procedure usb_get_string_simple(*dev.usb_dev_handle, index.l, *buf.c, buflen.i)
  Shared _usb_get_string_simple
  
  If *dev = 0
    ProcedureReturn -1
  EndIf

  ProcedureReturn CallFunctionFast(_usb_get_string_simple, *dev, index, *buf, buflen)
EndProcedure

Procedure usb_get_descriptor(*dev.usb_dev_handle, type.c, index.c, *buf.l, size.l)
  Shared _usb_get_descriptor
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_get_descriptor, *dev, type, index, *buf, size)
EndProcedure

Procedure usb_get_descriptor_by_endpoint(*dev.usb_dev_handle, ep.l, type.c, index.c, *buf.l, size.l)
  Shared _usb_get_descriptor_by_endpoint
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_get_descriptor_by_endpoint, *dev, ep, type, index, *buf, size)
EndProcedure

Procedure usb_bulk_write(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l)
  Shared _usb_bulk_write
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_bulk_write, *dev, ep, *bytes, size, timeout)
EndProcedure

Procedure usb_bulk_read(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l)
  Shared _usb_bulk_read
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_bulk_read, *dev, ep, *bytes, size, timeout)
EndProcedure

Procedure usb_interrupt_write(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l)
  Shared _usb_interrupt_write
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_interrupt_write, *dev, ep, *bytes, size, timeout)
EndProcedure

Procedure usb_interrupt_read(*dev.usb_dev_handle, ep.l, *bytes.c, size.l, timeout.l)
  Shared _usb_interrupt_read
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_interrupt_read, *dev, ep, *bytes, size, timeout)
EndProcedure

Procedure usb_get_driver_np(*dev.usb_dev_handle, hInterface.l, *name.c, namelen.l)
  Shared _usb_get_driver_np
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_get_driver_np, *dev, hInterface, *name, namelen)
EndProcedure

Procedure usb_detach_kernel_driver_np(*dev.usb_dev_handle, hInterface.l)
  Shared _usb_detach_kernel_driver_np
  
  If *dev = 0
    ProcedureReturn -1
  EndIf
  
  ProcedureReturn CallFunctionFast(_usb_detach_kernel_driver_np, *dev, hInterface)
EndProcedure

DisableExplicit
und hier noch ein kleines beispiel, dass alle Daten von vorhandenen Busse und Devices ausgibt.

Code: Alles auswählen

;first of all, we have to call usb_init(). If result is > 0 than we can use the libusb0.dll

IncludeFile "usbLIB.pb"     ;Functions we can call from libusb0.dll

; Variables
Global *usb_busses.usb_bus
Global *usb_device.usb_device
Global *usb_dev_handle.usb_dev_handle

Procedure showBussesAndDevicesWithInformation()
  Debug "Anzahl Busse: " + Str(usb_find_busses())
  Debug "Anzahl Devices: " + Str(usb_find_devices())

  *usb_busses = usb_get_busses()

  While *usb_busses
    Debug ""
    Debug "Busname: " + PeekS(@*usb_busses\dirname)

    *devices.usb_device = *usb_busses\devices
    *usb_device = *usb_busses\devices
    
    While *devices
      Debug "Filename: " + PeekS(@*devices\filename)
      Debug "devnum: " + Str(*devices\devnum)
      Debug "dev: " + Str(*devices\dev)
      Debug "num_children: " + Str(*devices\num_children)
      Debug "children: " + Str(*devices\children)

      If *devices\descriptor
        Debug ""
        Debug "Device Descriptor:"
        Debug "    bcdDevice: " + Hex(*devices\descriptor\bcdDevice)
        Debug "    bcdUSB: " + Hex(*devices\descriptor\bcdUSB)
        Debug "    bDescriptorType: " + Hex(*devices\descriptor\bDescriptorType)
        Debug "    bDeviceClass: " + Hex(*devices\descriptor\bDeviceClass)
        Debug "    bDeviceProtocol: " + Hex(*devices\descriptor\bDeviceProtocol)
        Debug "    bDeviceSubClass: " + Hex(*devices\descriptor\bDeviceSubClass)
        Debug "    bLength: " + Hex(*devices\descriptor\bLength)
        Debug "    bMaxPacketSize0: " + Hex(*devices\descriptor\bMaxPacketSize0)
        Debug "    bNumConfigurations: " + Hex(*devices\descriptor\bNumConfigurations)
        Debug "    idProduct: " + RSet(Hex(*devices\descriptor\idProduct), 4, "0")
        Debug "    idVendor: " + RSet(Hex(*devices\descriptor\idVendor), 4, "0")
        Debug "    iManufacturer: " + Hex(*devices\descriptor\iManufacturer)
        Debug "    iProduct: " + Hex(*devices\descriptor\iProduct)
        Debug "    iSerialNumber: " + Hex(*devices\descriptor\iSerialNumber)
      EndIf

      If *devices\config
        Debug ""
        Debug "Device Config:"
        Debug "    bConfigurationValue: " + Hex(*devices\config\bConfigurationValue)
        Debug "    bDescriptorType: " + Hex(*devices\config\bDescriptorType)
        Debug "    bLength: " + Hex(*devices\config\bLength)
        Debug "    bmAttributes: " + Hex(*devices\config\bmAttributes)
        Debug "    bNumInterfaces: " + Hex(*devices\config\bNumInterfaces)
        Debug "    extra: " + Hex(*devices\config\extra)
        Debug "    extralen: " + Hex(*devices\config\extralen)
        Debug "    iConfiguration: " + Hex(*devices\config\iConfiguration)
        Debug "    MaxPower: " + Hex(*devices\config\MaxPower)
        Debug "    wTotalLength: " + Hex(*devices\config\wTotalLength)

        If *devices\config\Interface
          Debug ""
          Debug "    Interface:"
          Debug "        num_altsetting: " + Hex(*devices\config\Interface\num_altsetting)

          If *devices\config\Interface\altsetting
            Debug ""
            Debug "        altsetting:"
            Debug "            bAlternateSetting: " + Hex(*devices\config\Interface\altsetting\bAlternateSetting)
            Debug "            bDescriptorType: " + Hex(*devices\config\Interface\altsetting\bDescriptorType)
            Debug "            bInterfaceClass: " + Hex(*devices\config\Interface\altsetting\bInterfaceClass)
            Debug "            bInterfaceNumber: " + Hex(*devices\config\Interface\altsetting\bInterfaceNumber)  
            Debug "            bInterfaceProtocol: " + Hex(*devices\config\Interface\altsetting\bInterfaceProtocol)
            Debug "            bInterfaceSubClass: " + Hex(*devices\config\Interface\altsetting\bInterfaceSubClass)
            Debug "            bLength: " + Hex(*devices\config\Interface\altsetting\bLength)
            Debug "            extra: " + Hex(*devices\config\Interface\altsetting\extra)
            Debug "            extralen: " + Hex(*devices\config\Interface\altsetting\extralen)
            Debug "            iInterface: " + Hex(*devices\config\Interface\altsetting\iInterface)
            Debug "            bNumEndpoints: " + Hex(*devices\config\Interface\altsetting\bNumEndpoints)

            If *devices\config\Interface\altsetting\endpoint
              Debug ""
              Debug "            endpoint: "
              Debug "                bDescriptorType: " + Hex(*devices\config\Interface\altsetting\endpoint\bDescriptorType)
              Debug "                bEndpointAddress: " + Hex(*devices\config\Interface\altsetting\endpoint\bEndpointAddress)
              Debug "                bInterval: " + Hex(*devices\config\Interface\altsetting\endpoint\bInterval)
              Debug "                bLength: " + Hex(*devices\config\Interface\altsetting\endpoint\bLength)
              Debug "                bmAttributes: " + Hex(*devices\config\Interface\altsetting\endpoint\bmAttributes)
              Debug "                bRefresh: " + Hex(*devices\config\Interface\altsetting\endpoint\bRefresh)
              Debug "                bSynchAddress: " + Hex(*devices\config\Interface\altsetting\endpoint\bSynchAddress)
              Debug "                extra: " + Hex(*devices\config\Interface\altsetting\endpoint\extra)
              Debug "                extralen: " + Hex(*devices\config\Interface\altsetting\endpoint\extralen)
              Debug "                wMaxPacketSize: " + Hex(*devices\config\Interface\altsetting\endpoint\wMaxPacketSize)
            EndIf
          EndIf

        EndIf

      EndIf

      *devices = *devices\next
    Wend

    *usb_busses = *usb_busses\Next
  Wend  
EndProcedure

Procedure OpenAndCloseDevice()
  If *usb_device = 0
    ProcedureReturn 0
  EndIf
  
  Debug ""
  Debug "idProduct: " + RSet(Hex(*usb_device\descriptor\idProduct), 4, "0")
  Debug "idVendor: " + RSet(Hex(*usb_device\descriptor\idVendor), 4, "0")
  Debug ""
  
  *usb_dev_handle = usb_open(*usb_device)
  
  Debug ""
  Debug "Open and Close Device"
  Debug *usb_dev_handle
  Debug usb_close(*usb_dev_handle)
EndProcedure

Procedure libUSB0()
  If usb_init()
    showBussesAndDevicesWithInformation()

    *usb_version.usb_version = usb_get_version()

    Debug ""
    Debug "DLL Version: " + Str(*usb_version\dll\major) + "." + Str(*usb_version\dll\minor) + "." + Str(*usb_version\dll\micro) + "." + Str(*usb_version\dll\nano)
    Debug "Driver Version: " + Str(*usb_version\driver\major) + "." + Str(*usb_version\driver\minor) + "." + Str(*usb_version\driver\micro) + "." + Str(*usb_version\driver\nano)
    
    ;this method won't work, but why?
    ;OpenAndCloseDevice()
    
    CloseLibrary(libUsbID)
  EndIf
EndProcedure

libUSB0()

Noch ein kleiner Hinweis:
usb_init() muss immer vor irgendwelchen anderen usblib Zugriffen aufgerufen werden. Die dll wird autom. geladen, muss mal also nicht selbst machen.
Ein Gerät wird nur erkannt, wenn das Gerät auch für libusb oder libusbk installiert wurde. Dabei spielt es keine Rolle ob es eine Filter Installation ist oder nicht.

Falls jemand weiß, warum das mit dem usb_open() nicht funktioniert, nicht schüchtern sein und bitte bescheid geben. Dann kann ich das ganze vervollständigen.

Werd mal schauen, ob vielleicht ein paar Privilegien benötigt werden. Ich bin mir ziemlich sicher, das hängt mit dem CreateFile_() zusammen, dass innerhalb von usb_open in der dll aufgerufen wird. wenn ich diese schritte selbst über pb mache, scheitert es an CreateFile_(). Result ist immer -1.

Gruß
DNA

Re: libusb unter Windows mit PureBasic

Verfasst: 14.10.2011 01:13
von DNA
Hab nun den Mapper vollständig. Dazu habe ich die libusb_dyn.c als vorlage genommen.
Bin mir nur nicht sicher, wie man sowas in PB umsetzt:

Code: Alles auswählen

void **context
Das ist doch eigentlich ein Pointer auf ein Pointer oder wo für sind die 2 Sternchen?
Hab das jetzt in PB einfach so geschrieben:

Code: Alles auswählen

*context.l
Ich wollte die portierte Header und Mapping Datei hier reinposten, aber das ist sehr viel geworden.
Das wird hier als Post viel zu unübersichtlich. :(

Re: libusb unter Windows mit PureBasic

Verfasst: 14.10.2011 10:47
von NicTheQuick
Beispiel für verschachtelte Pointer:

Code: Alles auswählen

Structure Pointer
	StructureUnion
		*p.Pointer
		i.i
	EndStructureUnion
EndStructure

; in C:
; void ** context;				// Pointer auf Pointer

; in PB:
Define.Pointer *context			; Pointer auf Pointer auf Pointer ... (auf Integer)


; Beispielzugriff in C:
; int ** ppInt;
; int a = 123;
; int * pInt = &a;
; ppInt = &pInt;
; **ppInt = 456;
; // a = 456

; Beispielzugriff in PB:
Define *ppInt.Pointer
Define a.i = 123
Define *pInt.Integer = @a
*ppInt = @*pInt
*ppInt\p\i = 456
; a = 456
Debug a
ich hoffe ich habe den C-Code jetzt so aus dem Kopf richtig geschrieben. :wink:

Re: libusb unter Windows mit PureBasic

Verfasst: 14.10.2011 22:52
von DNA
hmmmm, ich habs ein bisschen anders gelöst, weiß aber nicht, ob das nicht das gleiche ist:

Code: Alles auswählen

Structure pp
  *p.LONG
EndStructure

*pcontext.pp

a = 123

*pcontext = @a
*pcontext\p = 456

; a = 456
Debug a