diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 580d4157bf..08ae7380bc 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,50 @@ The license applies to all entries newer than 2009-04-28. */ +2012-07-27 01:20 UTC+0200 Viktor Szakats (harbour syenar.net) + + extras/hbusb/hbusb.hbc + + extras/hbusb/hbusb.hbp + + extras/hbusb/hbusb.hbx + + extras/hbusb/tests/hbmk.hbm + * extras/hbusb/core.c + * extras/hbusb/hbusb.ch + * extras/hbusb/tests/test.prg + + added hbmk2 make files including detection logic for libusb + (might need more tweaks, I don't know which is the preferred + libusb binary on Windows yet) + Use HB_WITH_LIBUSB envvar to set header location + + added hbx file + * renamed file references internally + + reformatted sources with uncrustify and hbformat then manually + ! removed non-ANSI comments + ! fixed #include + + added SVN headers + * renamed wrapper functions to match the name in libusb + * renamed macros to have LIBUSB_ prefix + ! fixed to use pointer type for pointers instead of numeric, + it fixes the code for non-32-bit builds + ! test fixed to compile w/o warnings + * test modified to output to stdout, plus some cleanups + * LIBUSB_INIT() changed to look for 1st param passed by + ref, instead of logical value to indicate that + ! C code fixed to fill params passed by ref even in error cases + ! some type corrections in C + ! fixed (mingw, msvc) warnings in C code + * other minor cleanups + ; TOFIX: try to use GC collected pointers + ; TOFIX: C code allows .prg code to create GPFs, f.e. + in 'devicelist[ hb_parni( 2 ) ]' the pointer is not + checked for NULL, nor the index is validated to be + inside limits. + ; TODO: RTE for invalid parameters + ; TODO: testing on various platforms + ; NOTE: bcc is not supported for multiple reasons + ; NOTE: C++ mode causes warnings in libusb header + ; NOTE: Links: + url: http://www.libusb.org/ + src: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.9/libusb-1.0.9.tar.bz2 + bin/win: http://sourceforge.net/projects/libusbx/files/releases/1.0.12/Windows/libusbx-1.0.12-win.7z/download + 2012-07-27 00:55 UTC+0200 Viktor Szakats (harbour syenar.net) + extras/hbusb + extras/hbusb/core.c diff --git a/harbour/extras/hbusb/core.c b/harbour/extras/hbusb/core.c index 869a8eff82..9cc6284ed3 100644 --- a/harbour/extras/hbusb/core.c +++ b/harbour/extras/hbusb/core.c @@ -1,476 +1,330 @@ -// usb4h.c - /* + * $Id$ + */ -UU UU SSSSSSS BBBBBBBBB 4444 HH HH -UU UU SSS SSS BB BBB 44 44 HH HH -UU UU SS SS BB BB 44 44 HH HH -UU UU SS BB BB 44 44 HH HH -UU UU SSS BB BBB 44 44 HH HH -UU UU SSSSSS BBBBBBBBB 444444444444 HHHHHHHHHHHH -UU UU SSS BB BBB 44 HH HH - UU UU SS BB BB 44 HH HH - UU UU SS SS BB BB 44 HH HH - UUU UUU SSS SSS BB BBB 44 HH HH - UUUUUU SSSSSS BBBBBBBBB 44 HH HH +/* TODO: change raw pointers to GC collected ones? */ -*/ +#include "hbapi.h" -#include -#include +#include "libusb.h" +/* ------------ library initialisation and deinitialisation -------------------- */ - -/*=============================================================================== - | | - | library initialisation and deinitialisation | - | | - ===============================================================================*/ - -/* USB4H_Init() - - Initialises USB4H (libusb). - - Must be called before calling any other USB4H functions. - - Getting a context is optional. */ - - -HB_FUNC( USB4H_INIT ) +/* Initialises libusb. + Must be called before calling any other libusb functions. + Getting a context is optional. */ +HB_FUNC( LIBUSB_INIT ) { - int success; - libusb_context * context; - if ( hb_parl( 1 ) ) - { + int success; + + if( HB_ISBYREF( 1 ) ) + { + libusb_context * context; success = libusb_init( &context ); - if ( success == 0 ) - { - hb_stornl( ( ULONG ) context, 2 ); - } - } + hb_storptr( success == 0 ? context : NULL, 1 ); + } else - { - success = libusb_init( NULL ); - } - hb_retni( success ); + success = libusb_init( NULL ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_Exit() - - Deinitialise USB4H (libusb). */ - -HB_FUNC( USB4H_EXIT ) +/* Deinitialise libusb. */ +HB_FUNC( LIBUSB_EXIT ) { - libusb_exit( ( libusb_context * ) hb_parnl( 1 ) ); - hb_ret(); + libusb_exit( ( libusb_context * ) hb_parptr( 1 ) ); } -//------------------------------------------------------------------------------- - -/* USB4H_SetDebug() - - Sets the message verbosity. - - Refer to usb4h.ch for options */ - - -HB_FUNC( USB4H_SETDEBUG ) +/* Sets the message verbosity. + Refer to hbusb.ch for options */ +HB_FUNC( LIBUSB_SET_DEBUG ) { - libusb_set_debug( ( libusb_context * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_ret(); + libusb_set_debug( ( libusb_context * ) hb_parptr( 1 ), hb_parni( 2 ) ); } -//------------------------------------------------------------------------------- +/* ------------------- device handling and enumeration ------------------------- */ -/*=============================================================================== - | | - | device handling and enumeration | - | | - ===============================================================================*/ - -/* USB4H_GetDeviceList() - - Returns a list of USB devcices attached to your system. */ - -HB_FUNC( USB4H_GETDEVICELIST ) +/* Returns a list of USB devcices attached to your system. */ +HB_FUNC( LIBUSB_GET_DEVICE_LIST ) { - libusb_device ** devicelist; - ssize_t count; - count = libusb_get_device_list( ( libusb_context * ) hb_parnl( 1 ), &devicelist ); - hb_stornl( ( ULONG ) devicelist, 2 ); - hb_retni( count ); + libusb_device ** devicelist; + ssize_t count; + + count = libusb_get_device_list( ( libusb_context * ) hb_parptr( 1 ), &devicelist ); + + hb_storptr( devicelist, 2 ); + + hb_retns( ( HB_SIZE ) count ); } -//------------------------------------------------------------------------------- - -/* USB4H_FreeDeviceList() - - Frees the list returned by USB4H_GetDeviceList. */ - -HB_FUNC( USB4H_FREEDEVICELIST ) +/* Frees the list returned by LIBUSB_GetDeviceList(). */ +HB_FUNC( LIBUSB_FREE_DEVICE_LIST ) { - libusb_free_device_list( ( libusb_device ** ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_ret(); + libusb_free_device_list( ( libusb_device ** ) hb_parptr( 1 ), hb_parni( 2 ) ); } -//------------------------------------------------------------------------------- - -/* USB4H_GetBusNumber() - - Gets the number of the bus that a device is attached to. */ - -HB_FUNC( USB4H_GETBUSNUMBER ) +/* Gets the number of the bus that a device is attached to. */ +HB_FUNC( LIBUSB_GET_BUS_NUMBER ) { - uint8_t busnumber; - libusb_device ** devicelist; - devicelist = ( ( libusb_device ** ) hb_parnl( 1 ) ); - busnumber = libusb_get_bus_number( devicelist[ hb_parni( 2 ) ] ); - hb_retni( busnumber ); + uint8_t busnumber; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + busnumber = libusb_get_bus_number( devicelist[ hb_parni( 2 ) ] ); + + hb_retni( busnumber ); } -//------------------------------------------------------------------------------- - -/* USB4H_GetDeviceAddress() - - Gets the address on the bus that a device is attached to. */ - -HB_FUNC( USB4H_GETDEVICEADDRESS ) +/* Gets the address on the bus that a device is attached to. */ +HB_FUNC( LIBUSB_GET_DEVICE_ADDRESS ) { - uint8_t deviceaddress; - libusb_device ** devicelist; - devicelist = ( ( libusb_device ** ) hb_parnl( 1 ) ); - deviceaddress = libusb_get_device_address( devicelist[ hb_parni( 2 ) ] ); - hb_retni( deviceaddress ); + uint8_t deviceaddress; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + deviceaddress = libusb_get_device_address( devicelist[ hb_parni( 2 ) ] ); + + hb_retni( deviceaddress ); } -//------------------------------------------------------------------------------- - -/* USB4H_GetDeviceSpeed() - - Gets the negotiated connection speed for a device. - - Refer to usb4h.ch for speeds. */ - -/* -HB_FUNC( USB4H_GETDEVICESPEED ) +/* Gets the negotiated connection speed for a device. + Refer to hbusb.ch for speeds. */ +#if 0 +HB_FUNC( LIBUSB_GET_DEVICE_SPEED ) { - int devicespeed; - libusb_device ** devicelist; - devicelist = ( ( libusb_device ** ) hb_parnl( 1 ) ); - devicespeed = libusb_get_device_speed( devicelist[ hb_parni( 2 ) ] ); - hb_retni( devicespeed ); + int devicespeed; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + devicespeed = libusb_get_device_speed( devicelist[ hb_parni( 2 ) ] ); + + hb_retni( devicespeed ); } -*/ +#endif -//------------------------------------------------------------------------------- - -/* -USB4H_GetMaxPacketSize() - -Gets the maximum packet size for a particular endpoint in the active device configuration. */ - -HB_FUNC( USB4H_GETMAXPACKETSIZE ) +/* Gets the maximum packet size for a particular endpoint in the active device configuration. */ +HB_FUNC( LIBUSB_GET_MAX_PACKET_SIZE ) { - int maxpacketsize; - libusb_device ** devicelist; - devicelist = ( libusb_device ** ) hb_parnl( 1 ); - maxpacketsize = libusb_get_max_packet_size( devicelist[ hb_parni( 2 ) ], ( unsigned char ) hb_parni( 3 ) ); - hb_retni( maxpacketsize ); + int maxpacketsize; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + maxpacketsize = libusb_get_max_packet_size( devicelist[ hb_parni( 2 ) ], ( unsigned char ) hb_parni( 3 ) ); + + hb_retni( maxpacketsize ); } -//------------------------------------------------------------------------------- - -/* USB4H_RefDevice() - - Increments the reference count of a device. */ - -HB_FUNC( USB4H_REFDEVICE ) +/* Increments the reference count of a device. */ +HB_FUNC( LIBUSB_REF_DEVICE ) { - libusb_device *device; - libusb_device ** devicelist; - devicelist = ( libusb_device ** ) hb_parnl( 1 ); - device = libusb_ref_device( devicelist[ hb_parni( 2 ) ] ); - hb_ret(); + libusb_device * device; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + device = libusb_ref_device( devicelist[ hb_parni( 2 ) ] ); + + HB_SYMBOL_UNUSED( device ); } -//------------------------------------------------------------------------------- - -/* USB4H_UnrefDevice() - - Decrements the reference count of a device */ - -HB_FUNC( USB4H_UNREFDEVICE ) +/* Decrements the reference count of a device */ +HB_FUNC( LIBUSB_UNREF_DEVICE ) { - libusb_device ** devicelist; - devicelist = ( libusb_device ** ) hb_parnl( 1 ); - libusb_unref_device( devicelist[ hb_parni( 2 ) ] ); - hb_ret(); + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + libusb_unref_device( devicelist[ hb_parni( 2 ) ] ); } -//------------------------------------------------------------------------------- - -/* USB4H_Open() - - Open a device and obtain a device handle */ - -HB_FUNC( USB4H_OPEN ) +/* Open a device and obtain a device handle */ +HB_FUNC( LIBUSB_OPEN ) { - int success; - libusb_device_handle * handle; - libusb_device ** devicelist; - devicelist = ( libusb_device ** ) hb_parnl( 1 ); - success = libusb_open( devicelist[ hb_parni( 2 ) ], &handle ); - if ( success == 0 ) - { - hb_stornl( ( ULONG ) handle, 3 ); - } - hb_retni( success ); + int success; + libusb_device_handle * handle; + libusb_device ** devicelist; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + success = libusb_open( devicelist[ hb_parni( 2 ) ], &handle ); + + hb_storptr( success == 0 ? handle : NULL, 3 ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_OpenDeviceWithVIDPID() - - Find a device with known Vendor IID and Product ID. */ - - -HB_FUNC( USB4H_OPENDEVICEWITHVIDPID ) +/* Find a device with known Vendor IID and Product ID. */ +HB_FUNC( LIBUSB_OPEN_DEVICE_WITH_VID_PID ) { - libusb_device_handle * handle; - handle = libusb_open_device_with_vid_pid( ( libusb_context * ) hb_parnl( 1 ), hb_parni( 2 ), hb_parni( 3 ) ); - hb_retnl( ( ULONG ) handle ); + libusb_device_handle * handle; + + handle = libusb_open_device_with_vid_pid( ( libusb_context * ) hb_parptr( 1 ), ( uint16_t ) hb_parni( 2 ), ( uint16_t ) hb_parni( 3 ) ); + + hb_retptr( handle ); } -//------------------------------------------------------------------------------- - -/* USB4H_Close() - - Close a device handle. */ - -HB_FUNC( USB4H_CLOSE ) +/* Close a device handle. */ +HB_FUNC( LIBUSB_CLOSE ) { - libusb_close( ( libusb_device_handle * ) hb_parnl( 1 ) ); - hb_ret(); + libusb_close( ( libusb_device_handle * ) hb_parptr( 1 ) ); } -//------------------------------------------------------------------------------- - -/* USB4H_GetDevice() - - Get the underlying device for a handle. */ - -HB_FUNC( USB4H_GETDEVICE ) +/* Get the underlying device for a handle. */ +HB_FUNC( LIBUSB_GET_DEVICE ) { - libusb_device * device; - device = libusb_get_device( ( libusb_device_handle * ) hb_parnl( 1 ) ); - hb_retnl( ( ULONG ) device ); + libusb_device * device; + + device = libusb_get_device( ( libusb_device_handle * ) hb_parptr( 1 ) ); + + hb_retptr( device ); } -//------------------------------------------------------------------------------- - -/* USB4H_GetConfiguration() - - Get the configuration value of the currently active configuration. */ - -HB_FUNC( USB4H_GETCONFIGURATION ) +/* Get the configuration value of the currently active configuration. */ +HB_FUNC( LIBUSB_GET_CONFIGURATION ) { - int configuration; - int success; - configuration = 0; - success = libusb_get_configuration( ( libusb_device_handle * ) hb_parnl( 1 ), &configuration ); - if ( success == 0 ) - { - hb_storni( configuration, 2 ); - } - hb_retni( success ); + int configuration; + int success; + + configuration = 0; + success = libusb_get_configuration( ( libusb_device_handle * ) hb_parptr( 1 ), &configuration ); + + hb_storni( success == 0 ? configuration : 0, 2 ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_SetConfiguration() - - Set the active configuration for a device. */ - -HB_FUNC( USB4H_SETCONFIGURATION ) +/* Set the active configuration for a device. */ +HB_FUNC( LIBUSB_SET_CONFIGURATION ) { - int configuration; - int success; - success = libusb_set_configuration( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( success ); + int success; + + success = libusb_set_configuration( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_ClaimInterface() - - Claim an interface on a given device handle. - - Required before you can perform I/O on any of its endpoints. */ - -HB_FUNC( USB4H_CLAIMINTERFACE ) +/* Claim an interface on a given device handle. + Required before you can perform I/O on any of its endpoints. */ +HB_FUNC( LIBUSB_CLAIM_INTERFACE ) { - int success; - success = libusb_claim_interface( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( success ); + int success; + + success = libusb_claim_interface( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_ReleaseInterface() - - Release a previously claimed interface. */ - -HB_FUNC( USB4H_RELEASEINTERFACE ) +/* Release a previously claimed interface. */ +HB_FUNC( LIBUSB_RELEASE_INTERFACE ) { - int success; - success = libusb_release_interface( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( success ); + int success; + + success = libusb_release_interface( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_SetInterfaceAltSetting() - - Activate an alternate setting for an interface. */ - -HB_FUNC( USB4H_SETINTERFACEALTSETTING ) +/* Activate an alternate setting for an interface. */ +HB_FUNC( LIBUSB_SET_INTERFACE_ALT_SETTING ) { - int success; - success = libusb_set_interface_alt_setting( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ), hb_parni( 3 ) ); - hb_retni( success ); + int success; + + success = libusb_set_interface_alt_setting( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ), hb_parni( 3 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_ClearHalt() - - Clear the halt / stall condition for an endpoint */ - -HB_FUNC( USB4H_CLEARHALT ) +/* Clear the halt / stall condition for an endpoint */ +HB_FUNC( LIBUSB_CLEAR_HALT ) { - int success; - success = libusb_clear_halt( ( libusb_device_handle * ) hb_parnl( 1 ), ( unsigned char ) hb_parni( 2 ) ); - hb_retni( success ); + int success; + + success = libusb_clear_halt( ( libusb_device_handle * ) hb_parptr( 1 ), ( unsigned char ) hb_parni( 2 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_ResetDevice() - - Perform a USB port reset to reinitialise a device. */ - -HB_FUNC( USB4H_RESETDEVICE ) +/* Perform a USB port reset to reinitialise a device. */ +HB_FUNC( LIBUSB_RESET_DEVICE ) { - int success; - success = libusb_reset_device( ( libusb_device_handle * ) hb_parnl( 1 ) ); - hb_retni( success ); + int success; + + success = libusb_reset_device( ( libusb_device_handle * ) hb_parptr( 1 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_KernelDriverActive() - - Determine if a kernel driver is active on an interfacc. */ - -HB_FUNC( USB4H_KERNELDRIVERACTIVE ) +/* Determine if a kernel driver is active on an interfacc. */ +HB_FUNC( LIBUSB_KERNEL_DRIVER_ACTIVE ) { - int isactive; - isactive = libusb_kernel_driver_active( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( isactive ); + int isactive; + + isactive = libusb_kernel_driver_active( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ); + + hb_retni( isactive ); } -//------------------------------------------------------------------------------- - -/* USB4H_DetachKernelDriver() - - Detach a kernel driver from an interface. */ - -HB_FUNC( USB4H_DETACHKERNELDRIVER ) +/* Detach a kernel driver from an interface. */ +HB_FUNC( LIBUSB_DETACH_KERNEL_DRIVER ) { - int success; - success = libusb_detach_kernel_driver( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( success ); + hb_retni( libusb_detach_kernel_driver( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ) ); } -//------------------------------------------------------------------------------- - -/* USB4H_AttachKernelDriver() - - Reattach a kernel driver which was previously detached. */ - -HB_FUNC( USB4H_ATTACHKERNELDRIVER ) +/* Reattach a kernel driver which was previously detached. */ +HB_FUNC( LIBUSB_ATTACH_KERNEL_DRIVER ) { - int success; - success = libusb_attach_kernel_driver( ( libusb_device_handle * ) hb_parnl( 1 ), hb_parni( 2 ) ); - hb_retni( success ); + int success; + + success = libusb_attach_kernel_driver( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- +/* ------------------------------ USB descriptors ------------------------------ */ -/*=============================================================================== - | | - | USB descriptors | - | | - ===============================================================================*/ - -/* USB4H_GetDeviceDescriptor() - - Get the USB descriptor for a given device. */ - -HB_FUNC( USB4H_GETDEVICEDESCRIPTOR ) +/* Get the USB descriptor for a given device. */ +HB_FUNC( LIBUSB_GET_DEVICE_DESCRIPTOR ) { - struct libusb_device_descriptor desc; - libusb_device ** devicelist; - int success; - devicelist = ( libusb_device ** ) hb_parnl( 1 ); - success = libusb_get_device_descriptor( devicelist[ hb_parni( 2 ) ], &desc ); - hb_stornl( ( ULONG ) &desc, 3 ); - hb_storni( desc.idVendor, 4 ); - hb_storni( desc.idProduct, 5 ); - hb_storni( (int ) desc.bNumConfigurations, 6 ); - hb_retni( success ); + struct libusb_device_descriptor desc; + libusb_device ** devicelist; + int success; + + devicelist = ( libusb_device ** ) hb_parptr( 1 ); + success = libusb_get_device_descriptor( devicelist[ hb_parni( 2 ) ], &desc ); + + hb_storptr( &desc, 3 ); + hb_storni( desc.idVendor, 4 ); + hb_storni( desc.idProduct, 5 ); + hb_storni( ( int ) desc.bNumConfigurations, 6 ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- +/* --------------------------- synchronous device I/O -------------------------- */ -/*=============================================================================== - | | - | synchronous device I/O | - | | - ===============================================================================*/ - -/* USB4H_BulkTransfer() - - Perform a USB bulk transfer. */ - -HB_FUNC( USB4H_BULKTRANSFER ) +/* Perform a USB bulk transfer. */ +HB_FUNC( LIBUSB_BULK_TRANSFER ) { - int success; - unsigned char data[512]; - int transferred; - success = libusb_bulk_transfer( ( libusb_device_handle * ) hb_parnl( 1 ), ( unsigned char ) hb_parni( 2 ), data, 512, &transferred, hb_parni( 3 ) ); - hb_storclen( data, ( long ) transferred, 4 ); - hb_storni( transferred, 5 ); - hb_retni( success ); + int success; + unsigned char data[ 512 ]; + int transferred; + + success = libusb_bulk_transfer( ( libusb_device_handle * ) hb_parptr( 1 ), ( unsigned char ) hb_parni( 2 ), data, sizeof( data ), &transferred, hb_parni( 3 ) ); + + hb_storclen( ( char * ) data, ( HB_ISIZ ) transferred, 4 ); + hb_storni( transferred, 5 ); + + hb_retni( success ); } -//------------------------------------------------------------------------------- - -/* USB4H_InterruptTransfer() - - Perform a USB interrupt transfer. */ - -HB_FUNC( USB4H_INTERRUPTTRANSFER ) +/* Perform a USB interrupt transfer. */ +HB_FUNC( LIBUSB_INTERRUPT_TRANSFER ) { - int success; - unsigned char data[512]; - int transferred; - success = libusb_interrupt_transfer( ( libusb_device_handle * ) hb_parnl( 1 ), ( unsigned char ) hb_parni( 2 ), data, 512, &transferred, hb_parni( 3 ) ); - hb_storclen( data, ( long ) transferred, 4 ); - hb_storni( transferred, 5 ); - hb_retni( success ); + int success; + unsigned char data[ 512 ]; + int transferred; + + success = libusb_interrupt_transfer( ( libusb_device_handle * ) hb_parptr( 1 ), ( unsigned char ) hb_parni( 2 ), data, sizeof( data ), &transferred, hb_parni( 3 ) ); + + hb_storclen( ( char * ) data, ( HB_ISIZ ) transferred, 4 ); + hb_storni( transferred, 5 ); + + hb_retni( success ); } - - diff --git a/harbour/extras/hbusb/hbusb.ch b/harbour/extras/hbusb/hbusb.ch index b2234e5743..02d47484c7 100644 --- a/harbour/extras/hbusb/hbusb.ch +++ b/harbour/extras/hbusb/hbusb.ch @@ -1,10 +1,10 @@ -// usb4h.ch +/* + * $Id$ + */ -// Header file for the USB4H library +#define LIBUSB_UNREF_DEVICES 1 -#define USB4H_UNREF_DEVICES 1 +#define LIBUSB_KERNEL_HAS_INTERFACE 1 -#define USB4H_KERNEL_HAS_INTERFACE 1 - -#define USB4H_ENDPOINT_IN 129 // should be 128 and then && 1 when required -#define USB4H_ENDPOINT_OUT 0 +#define LIBUSB_ENDPOINT_IN 129 /* Should be 128 and then && 1 when required */ +#define LIBUSB_ENDPOINT_OUT 0 diff --git a/harbour/extras/hbusb/hbusb.hbc b/harbour/extras/hbusb/hbusb.hbc new file mode 100644 index 0000000000..1e5dde3cd4 --- /dev/null +++ b/harbour/extras/hbusb/hbusb.hbc @@ -0,0 +1,11 @@ +# +# $Id$ +# + +incpaths=. +libpaths=lib/${hb_plat}/${hb_comp} + +libs=${_HB_DYNPREF}${hb_name}${_HB_DYNSUFF} + +{win}libs=libusb-1 +{!win}libs=libusb diff --git a/harbour/extras/hbusb/hbusb.hbp b/harbour/extras/hbusb/hbusb.hbp new file mode 100644 index 0000000000..d59b7c4985 --- /dev/null +++ b/harbour/extras/hbusb/hbusb.hbp @@ -0,0 +1,23 @@ +# +# $Id$ +# + +-hblib +-inc + +-olib/${hb_plat}/${hb_comp}/${hb_name} + +-w3 -es2 + +-depkeyhead=libusb:libusb.h +-depcontrol=libusb:${HB_WITH_LIBUSB} +-depincpath=libusb:/usr/include +-depincpath=libusb:/boot/common/include +-depimplibs=libusb:../../MS32/dll/libusb-1.0.dll{win&x86} +-depimplibs=libusb:../../MS64/dll/libusb-1.0.dll{win&x86_64} + +-hbx=hbusb.hbx + +hbusb.hbx + +core.c diff --git a/harbour/extras/hbusb/hbusb.hbx b/harbour/extras/hbusb/hbusb.hbx new file mode 100644 index 0000000000..b3bbe9deb2 --- /dev/null +++ b/harbour/extras/hbusb/hbusb.hbx @@ -0,0 +1,60 @@ +/* + * $Id$ + */ + +/* -------------------------------------------------------------------- */ +/* NOTE: You can add manual override which functions to include or */ +/* exclude from automatically generated EXTERNAL/DYNAMIC list. */ +/* Syntax: // HB_FUNC_INCLUDE */ +/* // HB_FUNC_EXCLUDE */ +/* -------------------------------------------------------------------- */ + +/* -------------------------------------------------------------------- */ +/* WARNING: Automatically generated code below. DO NOT EDIT! */ +/* Regenerate using hbmk2 '-hbx=' option. */ +/* -------------------------------------------------------------------- */ + +#ifndef __HBEXTERN_CH__HBUSB__ +#define __HBEXTERN_CH__HBUSB__ + +#if defined( __HBEXTREQ__ ) .OR. defined( __HBEXTERN__HBUSB__ANNOUNCE ) + ANNOUNCE __HBEXTERN__HBUSB__ +#endif + +#if defined( __HBEXTREQ__ ) .OR. defined( __HBEXTERN__HBUSB__REQUEST ) + #command DYNAMIC => EXTERNAL +#endif + +DYNAMIC libusb_attach_kernel_driver +DYNAMIC libusb_bulk_transfer +DYNAMIC libusb_claim_interface +DYNAMIC libusb_clear_halt +DYNAMIC libusb_close +DYNAMIC libusb_detach_kernel_driver +DYNAMIC libusb_exit +DYNAMIC libusb_free_device_list +DYNAMIC libusb_get_bus_number +DYNAMIC libusb_get_configuration +DYNAMIC libusb_get_device +DYNAMIC libusb_get_device_address +DYNAMIC libusb_get_device_descriptor +DYNAMIC libusb_get_device_list +DYNAMIC libusb_get_max_packet_size +DYNAMIC libusb_init +DYNAMIC libusb_interrupt_transfer +DYNAMIC libusb_kernel_driver_active +DYNAMIC libusb_open +DYNAMIC libusb_open_device_with_vid_pid +DYNAMIC libusb_ref_device +DYNAMIC libusb_release_interface +DYNAMIC libusb_reset_device +DYNAMIC libusb_set_configuration +DYNAMIC libusb_set_debug +DYNAMIC libusb_set_interface_alt_setting +DYNAMIC libusb_unref_device + +#if defined( __HBEXTREQ__ ) .OR. defined( __HBEXTERN__HBUSB__REQUEST ) + #uncommand DYNAMIC => EXTERNAL +#endif + +#endif diff --git a/harbour/extras/hbusb/tests/hbmk.hbm b/harbour/extras/hbusb/tests/hbmk.hbm new file mode 100644 index 0000000000..5b7cf0dc89 --- /dev/null +++ b/harbour/extras/hbusb/tests/hbmk.hbm @@ -0,0 +1,7 @@ +# +# $Id$ +# + +../hbusb.hbc + +-w3 -es2 diff --git a/harbour/extras/hbusb/tests/test.prg b/harbour/extras/hbusb/tests/test.prg index e8840351d8..d36efbf794 100644 --- a/harbour/extras/hbusb/tests/test.prg +++ b/harbour/extras/hbusb/tests/test.prg @@ -1,110 +1,126 @@ -// testusb4h.prg - -#include "usb4h.ch" - -FUNCTION Main() - - QOut( "Initialising USB4H" ) - int_RetVal := USB4H_Init( 0 ) - QQOut( " returns", int_RetVal ) - - int_DeviceList := 0 - QOut( "Getting Device List" ) - int_DeviceCount := USB4H_GetDeviceList( 0, @int_DeviceList ) - QQOut( " returns", int_DeviceCount, "which is the number of USB devices found" ) - QOut( "Device list address is", int_DeviceList ) - - FOR int_DeviceNumber = 0 TO int_DeviceCount - 1 - int_BusNumber := USB4H_GetBusNumber( int_DeviceList, int_DeviceNumber ) - QOut( "Bus Number:", LTrim( Str( int_BusNumber ) ) ) - int_DeviceAddress := USB4H_GetDeviceAddress( int_DeviceList, int_DeviceNumber ) - QQOut( " Address:", LTrim( Str( int_DeviceAddress ) ) ) - int_Descriptor := 0 - int_VendorID := 0 - int_ProductID := 0 - int_NumConfigurations := 0 - int_RetVal := USB4H_GetDeviceDescriptor( int_DeviceList, int_DeviceNumber, @int_Descriptor, @int_VendorID, @int_ProductID, @int_NumConfigurations ) - // QOut( int_Descriptor ) - QQOut( " Vendor:", LTrim( Str( int_VendorID ) ) ) - QQOut( " Product:", LTrim( Str( int_ProductID ) ) ) - QQOut( " Config Count:", LTrim( Str( int_NumConfigurations ) ) ) - NEXT - - QOut( "Freeing Device List" ) - USB4H_FreeDeviceList( 0 ) - - QOut() - QOut( "Opening Device" ) - int_DeviceHandle := USB4H_OpenDeviceWithVIDPID( 0, 1523, 255 ) - QOut( "returns", int_DeviceHandle ) - - IF int_DeviceHandle < 1 - QOut( "Cannot open the device" ) - ELSE - QOut( "Testing for kernel having claimed interface" ) - int_RetVal := USB4H_KernelDriverActive( int_DeviceHandle, 0 ) - QOut( "returns", int_RetVal ) - - IF int_RetVal == USB4H_KERNEL_HAS_INTERFACE - QOut( "Kernel has interface" ) - QOut( "Detaching Kernel from interface" ) - int_RetVal := USB4H_DetachKernelDriver( int_DeviceHandle, 0 ) - QOut( "returns", int_RetVal ) - ENDIF - QOut( "Claiming Interface" ) - int_RetVal := USB4H_ClaimInterface( int_DeviceHandle, 0 ) - QOut( "returns", int_RetVal ) - - str_Data := Space( 512 ) - int_Length := 0 - QOut( "Querying device" ) - FOR jj = 1 TO 500 - int_RetVal := USB4H_BulkTransfer( int_DeviceHandle, USB4H_ENDPOINT_IN, 100, @str_Data, @int_Length ) - IF Len( str_Data ) > 0 - SWITCH Asc( SubStr( str_Data, -2, 1 ) ) - CASE 0 - QOut( "Clear" ) - EXIT - CASE 1 - QOut( "Left Pedal" ) - EXIT - CASE 2 - QOut( "Middle Pedal" ) - EXIT - CASE 3 - QOut( "Left and Middle Pedals" ) - EXIT - CASE 4 - QOut( "Right Pedal" ) - EXIT - CASE 5 - QOut( "Left and Right Pedals" ) - EXIT - CASE 6 - QOut( "Middle and Right Pedals" ) - EXIT - CASE 7 - QOut( "All Three Pedals" ) - EXIT - OTHERWISE - QOut( "Error" ) - ENDSWITCH - ENDIF - NEXT - - QOut( "Releasing Interface" ) - int_RetVal := USB4H_ReleaseInterface( int_DeviceHandle, 0 ) - QOut( "returns", int_RetVal ) - - QOut( "Reattaching Kernel to interface" ) - int_RetVal := USB4H_AttachKernelDriver( int_DeviceHandle, 0 ) - QOut( "returns", int_RetVal ) - - ENDIF - QOut( "Closing Device" ) - USB4H_Close( int_DeviceHandle ) - - QOut( "Deinitialising USB4H" ) - USB4H_Exit() - QQOut( " done" ) - QUIT +/* + * $Id: array.prg 17867 2012-07-21 16:48:00Z vszakats $ + */ + +#include "simpleio.ch" + +#include "hbusb.ch" + +PROCEDURE Main() + + LOCAL nRetVal + LOCAL pDeviceList + LOCAL nDeviceCount + LOCAL nDeviceNumber + LOCAL nBusNumber + LOCAL nDeviceAddress + LOCAL pDescriptor + LOCAL nVendorID + LOCAL nProductID + LOCAL nNumConfigurations + LOCAL pDeviceHandle + LOCAL cData + LOCAL nLength + LOCAL tmp + + ? "Initialising libusb" + nRetVal := libusb_init() + ?? " returns", nRetVal + + ? "Getting Device List" + nDeviceCount := libusb_get_device_list( NIL, @pDeviceList ) + ?? " returns", nDeviceCount, "which is the number of USB devices found" + ? "Device list address is", pDeviceList + + FOR nDeviceNumber := 0 TO nDeviceCount - 1 + nBusNumber := libusb_get_bus_number( pDeviceList, nDeviceNumber ) + ? "Bus Number:", hb_ntos( nBusNumber ) + nDeviceAddress := libusb_get_device_address( pDeviceList, nDeviceNumber ) + ?? " Address:", hb_ntos( nDeviceAddress ) + nRetVal := libusb_get_device_descriptor( pDeviceList, nDeviceNumber, @pDescriptor, @nVendorID, @nProductID, @nNumConfigurations ) + ?? " return:", hb_ntos( nRetVal ) + ?? " Vendor:", hb_ntos( nVendorID ) + ?? " Product:", hb_ntos( nProductID ) + ?? " Config Count:", hb_ntos( nNumConfigurations ) + NEXT + + ? "Freeing Device List" + libusb_free_device_list( NIL ) + + ? + ? "Opening Device" + pDeviceHandle := libusb_open_device_with_vid_pid( NIL, 1523, 255 ) + ? "returns", pDeviceHandle + + IF Empty( pDeviceHandle ) + ? "Cannot open the device" + ELSE + ? "Testing for kernel having claimed interface" + nRetVal := libusb_kernel_driver_active( pDeviceHandle, 0 ) + ? "returns", nRetVal + + IF nRetVal == LIBUSB_KERNEL_HAS_INTERFACE + ? "Kernel has interface" + ? "Detaching Kernel from interface" + nRetVal := libusb_detach_kernel_driver( pDeviceHandle, 0 ) + ? "returns", nRetVal + ENDIF + ? "Claiming Interface" + nRetVal := libusb_claim_interface( pDeviceHandle, 0 ) + ? "returns", nRetVal + + cData := Space( 512 ) + nLength := 0 + ? "Querying device" + FOR tmp := 1 TO 500 + nRetVal := libusb_bulk_transfer( pDeviceHandle, LIBUSB_ENDPOINT_IN, 100, @cData, @nLength ) + HB_SYMBOL_UNUSED( nRetVal ) + IF hb_BLen( cData ) > 0 + SWITCH hb_BCode( hb_BSubStr( cData, -2, 1 ) ) + CASE 0 + ? "Clear" + EXIT + CASE 1 + ? "Left Pedal" + EXIT + CASE 2 + ? "Middle Pedal" + EXIT + CASE 3 + ? "Left and Middle Pedals" + EXIT + CASE 4 + ? "Right Pedal" + EXIT + CASE 5 + ? "Left and Right Pedals" + EXIT + CASE 6 + ? "Middle and Right Pedals" + EXIT + CASE 7 + ? "All Three Pedals" + EXIT + OTHERWISE + ? "Error" + ENDSWITCH + ENDIF + NEXT + + ? "Releasing Interface" + nRetVal := libusb_release_interface( pDeviceHandle, 0 ) + ? "returns", nRetVal + + ? "Reattaching Kernel to interface" + nRetVal := libusb_attach_kernel_driver( pDeviceHandle, 0 ) + ? "returns", nRetVal + ENDIF + + ? "Closing Device" + libusb_close( pDeviceHandle ) + + ? "Deinitialising libusb" + libusb_exit() + ?? " done" + + RETURN