Files
harbour-core/extras/hbusb/core.c
vszakats 9687850865 2013-03-16 02:10 UTC+0100 Viktor Szakats (harbour syenar.net)
* (all files)
    * stripped svn header
    * minor cleanups
    ; use following command to find out the history of files:
       git log
       git log --follow
       git blame
       git annotate
2013-03-16 02:11:42 +01:00

327 lines
8.2 KiB
C

/* TODO: change raw pointers to GC collected ones? */
#include "hbapi.h"
#include "libusb.h"
/* ------------ library initialisation and deinitialisation -------------------- */
/* Initialises libusb.
Must be called before calling any other libusb functions.
Getting a context is optional. */
HB_FUNC( LIBUSB_INIT )
{
int success;
if( HB_ISBYREF( 1 ) )
{
libusb_context * context;
success = libusb_init( &context );
hb_storptr( success == 0 ? context : NULL, 1 );
}
else
success = libusb_init( NULL );
hb_retni( success );
}
/* Deinitialise libusb. */
HB_FUNC( LIBUSB_EXIT )
{
libusb_exit( ( libusb_context * ) hb_parptr( 1 ) );
}
/* Sets the message verbosity.
Refer to hbusb.ch for options */
HB_FUNC( LIBUSB_SET_DEBUG )
{
libusb_set_debug( ( libusb_context * ) hb_parptr( 1 ), hb_parni( 2 ) );
}
/* ------------------- device handling and enumeration ------------------------- */
/* 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_parptr( 1 ), &devicelist );
hb_storptr( devicelist, 2 );
hb_retns( ( HB_SIZE ) count );
}
/* Frees the list returned by LIBUSB_GetDeviceList(). */
HB_FUNC( LIBUSB_FREE_DEVICE_LIST )
{
libusb_free_device_list( ( libusb_device ** ) hb_parptr( 1 ), hb_parni( 2 ) );
}
/* 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_parptr( 1 );
busnumber = libusb_get_bus_number( devicelist[ hb_parni( 2 ) ] );
hb_retni( busnumber );
}
/* 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_parptr( 1 );
deviceaddress = libusb_get_device_address( devicelist[ hb_parni( 2 ) ] );
hb_retni( deviceaddress );
}
/* 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_parptr( 1 );
devicespeed = libusb_get_device_speed( devicelist[ hb_parni( 2 ) ] );
hb_retni( devicespeed );
}
#endif
/* 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_parptr( 1 );
maxpacketsize = libusb_get_max_packet_size( devicelist[ hb_parni( 2 ) ], ( unsigned char ) hb_parni( 3 ) );
hb_retni( maxpacketsize );
}
/* Increments the reference count of a device. */
HB_FUNC( LIBUSB_REF_DEVICE )
{
libusb_device * device;
libusb_device ** devicelist;
devicelist = ( libusb_device ** ) hb_parptr( 1 );
device = libusb_ref_device( devicelist[ hb_parni( 2 ) ] );
HB_SYMBOL_UNUSED( device );
}
/* Decrements the reference count of a device */
HB_FUNC( LIBUSB_UNREF_DEVICE )
{
libusb_device ** devicelist;
devicelist = ( libusb_device ** ) hb_parptr( 1 );
libusb_unref_device( devicelist[ hb_parni( 2 ) ] );
}
/* 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_parptr( 1 );
success = libusb_open( devicelist[ hb_parni( 2 ) ], &handle );
hb_storptr( success == 0 ? handle : NULL, 3 );
hb_retni( success );
}
/* 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_parptr( 1 ), ( uint16_t ) hb_parni( 2 ), ( uint16_t ) hb_parni( 3 ) );
hb_retptr( handle );
}
/* Close a device handle. */
HB_FUNC( LIBUSB_CLOSE )
{
libusb_close( ( libusb_device_handle * ) hb_parptr( 1 ) );
}
/* Get the underlying device for a handle. */
HB_FUNC( LIBUSB_GET_DEVICE )
{
libusb_device * device;
device = libusb_get_device( ( libusb_device_handle * ) hb_parptr( 1 ) );
hb_retptr( device );
}
/* 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_parptr( 1 ), &configuration );
hb_storni( success == 0 ? configuration : 0, 2 );
hb_retni( success );
}
/* Set the active configuration for a device. */
HB_FUNC( LIBUSB_SET_CONFIGURATION )
{
int success;
success = libusb_set_configuration( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) );
hb_retni( success );
}
/* 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_parptr( 1 ), hb_parni( 2 ) );
hb_retni( success );
}
/* Release a previously claimed interface. */
HB_FUNC( LIBUSB_RELEASE_INTERFACE )
{
int success;
success = libusb_release_interface( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) );
hb_retni( success );
}
/* 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_parptr( 1 ), hb_parni( 2 ), hb_parni( 3 ) );
hb_retni( success );
}
/* Clear the halt / stall condition for an endpoint */
HB_FUNC( LIBUSB_CLEAR_HALT )
{
int success;
success = libusb_clear_halt( ( libusb_device_handle * ) hb_parptr( 1 ), ( unsigned char ) hb_parni( 2 ) );
hb_retni( success );
}
/* Perform a USB port reset to reinitialise a device. */
HB_FUNC( LIBUSB_RESET_DEVICE )
{
int success;
success = libusb_reset_device( ( libusb_device_handle * ) hb_parptr( 1 ) );
hb_retni( success );
}
/* 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_parptr( 1 ), hb_parni( 2 ) );
hb_retni( isactive );
}
/* Detach a kernel driver from an interface. */
HB_FUNC( LIBUSB_DETACH_KERNEL_DRIVER )
{
hb_retni( libusb_detach_kernel_driver( ( libusb_device_handle * ) hb_parptr( 1 ), hb_parni( 2 ) ) );
}
/* 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_parptr( 1 ), hb_parni( 2 ) );
hb_retni( success );
}
/* ------------------------------ USB descriptors ------------------------------ */
/* 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_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 -------------------------- */
/* 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_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 );
}
/* 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_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 );
}