diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ac8ed3a --- /dev/null +++ b/LICENSE @@ -0,0 +1,47 @@ + +License information for PS3EYEDriver +------------------------------------ + +The license of the PS3EYEDriver is MIT (for newly-written code) and GPLv2 for +all code derived from the Linux Kernel Driver (ov534) sources. + +In https://github.com/inspirit/PS3EYEDriver/pull/3, Eugene Zatepyakin writes: + + "all of my code is MIT licensed and to tell the truth i didnt check Linux + p3eye version license. as far as i know it was contributed to Linux by some + devs who decided to do it on their own..." + +The code is based on the Linux driver for the PSEye, which can be found here: + +http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/media/usb/gspca/ov534.c + +/* + * ov534-ov7xxx gspca driver + * + * Copyright (C) 2008 Antonio Ospite + * Copyright (C) 2008 Jim Paris + * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr + * + * Based on a prototype written by Mark Ferrell + * USB protocol reverse engineered by Jim Paris + * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/ + * + * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr + * PS3 Eye camera - brightness, contrast, awb, agc, aec controls + * added by Max Thrun + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + diff --git a/libusb/osx/libusb.a b/libusb/osx/libusb.a deleted file mode 100755 index a2bcbb7..0000000 Binary files a/libusb/osx/libusb.a and /dev/null differ diff --git a/libusb/vc10/libusb-1.0.lib b/libusb/vc10/libusb-1.0.lib deleted file mode 100755 index 57a48a6..0000000 Binary files a/libusb/vc10/libusb-1.0.lib and /dev/null differ diff --git a/sdl/main.cpp b/sdl/main.cpp index 9c3c879..06360b1 100644 --- a/sdl/main.cpp +++ b/sdl/main.cpp @@ -42,7 +42,7 @@ struct yuv422_buffers_t { ~yuv422_buffers_t() { - for (int i = 0; i < count; i++) { + for (int i=0; i - * Copyright © 2007-2008 Daniel Drake - * Copyright © 2012 Pete Batard - * For more information, please visit: http://libusbx.org - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef LIBUSB_H -#define LIBUSB_H - -#ifdef _MSC_VER -/* on MS environments, the inline keyword is available in C++ only */ -#if !defined(__cplusplus) -#define inline __inline -#endif -/* ssize_t is also not available (copy/paste from MinGW) */ -#ifndef _SSIZE_T_DEFINED -#define _SSIZE_T_DEFINED -#undef ssize_t -#ifdef _WIN64 - typedef __int64 ssize_t; -#else - typedef int ssize_t; -#endif /* _WIN64 */ -#endif /* _SSIZE_T_DEFINED */ -#endif /* _MSC_VER */ - -/* stdint.h is not available on older MSVC */ -#if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H)) -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; -typedef unsigned __int32 uint32_t; -#else -#include -#endif - -#if !defined(_WIN32_WCE) -#include -#endif - -#if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) -#include -#endif - -#include -#include - -/* 'interface' might be defined as a macro on Windows, so we need to - * undefine it so as not to break the current libusbx API, because - * libusb_config_descriptor has an 'interface' member - * As this can be problematic if you include windows.h after libusb.h - * in your sources, we force windows.h to be included first. */ -#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) -#include -#if defined(interface) -#undef interface -#endif -#if !defined(__CYGWIN__) -#include -#endif -#endif - -/** \def LIBUSB_CALL - * \ingroup misc - * libusbx's Windows calling convention. - * - * Under Windows, the selection of available compilers and configurations - * means that, unlike other platforms, there is not one true calling - * convention (calling convention: the manner in which parameters are - * passed to funcions in the generated assembly code). - * - * Matching the Windows API itself, libusbx uses the WINAPI convention (which - * translates to the stdcall convention) and guarantees that the - * library is compiled in this way. The public header file also includes - * appropriate annotations so that your own software will use the right - * convention, even if another convention is being used by default within - * your codebase. - * - * The one consideration that you must apply in your software is to mark - * all functions which you use as libusbx callbacks with this LIBUSB_CALL - * annotation, so that they too get compiled for the correct calling - * convention. - * - * On non-Windows operating systems, this macro is defined as nothing. This - * means that you can apply it to your code without worrying about - * cross-platform compatibility. - */ -/* LIBUSB_CALL must be defined on both definition and declaration of libusbx - * functions. You'd think that declaration would be enough, but cygwin will - * complain about conflicting types unless both are marked this way. - * The placement of this macro is important too; it must appear after the - * return type, before the function name. See internal documentation for - * API_EXPORTED. - */ -#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) -#define LIBUSB_CALL WINAPI -#else -#define LIBUSB_CALL -#endif - -/** \def LIBUSBX_API_VERSION - * \ingroup misc - * libusbx's API version. - * - * Since version 1.0.13, to help with feature detection, libusbx defines - * a LIBUSBX_API_VERSION macro that gets increased every time there is a - * significant change to the API, such as the introduction of a new call, - * the definition of a new macro/enum member, or any other element that - * libusbx applications may want to detect at compilation time. - * - * The macro is typically used in an application as follows: - * \code - * #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01001234) - * // Use one of the newer features from the libusbx API - * #endif - * \endcode - * - * Another feature of LIBUSBX_API_VERSION is that it can be used to detect - * whether you are compiling against the libusb or the libusbx library. - * - * Internally, LIBUSBX_API_VERSION is defined as follows: - * (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental) - */ -#define LIBUSBX_API_VERSION 0x010000FF - -#ifdef __cplusplus -extern "C" { -#endif - -/** \def libusb_cpu_to_le16 - * \ingroup misc - * Convert a 16-bit value from host-endian to little-endian format. On - * little endian systems, this function does nothing. On big endian systems, - * the bytes are swapped. - * \param x the host-endian value to convert - * \returns the value in little-endian byte order - */ -static inline uint16_t libusb_cpu_to_le16(const uint16_t x) -{ - union { - uint8_t b8[2]; - uint16_t b16; - } _tmp; - _tmp.b8[1] = x >> 8; - _tmp.b8[0] = x & 0xff; - return _tmp.b16; -} - -/** \def libusb_le16_to_cpu - * \ingroup misc - * Convert a 16-bit value from little-endian to host-endian format. On - * little endian systems, this function does nothing. On big endian systems, - * the bytes are swapped. - * \param x the little-endian value to convert - * \returns the value in host-endian byte order - */ -#define libusb_le16_to_cpu libusb_cpu_to_le16 - -/* standard USB stuff */ - -/** \ingroup desc - * Device and/or Interface Class codes */ -enum libusb_class_code { - /** In the context of a \ref libusb_device_descriptor "device descriptor", - * this bDeviceClass value indicates that each interface specifies its - * own class information and all interfaces operate independently. - */ - LIBUSB_CLASS_PER_INTERFACE = 0, - - /** Audio class */ - LIBUSB_CLASS_AUDIO = 1, - - /** Communications class */ - LIBUSB_CLASS_COMM = 2, - - /** Human Interface Device class */ - LIBUSB_CLASS_HID = 3, - - /** Physical */ - LIBUSB_CLASS_PHYSICAL = 5, - - /** Printer class */ - LIBUSB_CLASS_PRINTER = 7, - - /** Image class */ - LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */ - LIBUSB_CLASS_IMAGE = 6, - - /** Mass storage class */ - LIBUSB_CLASS_MASS_STORAGE = 8, - - /** Hub class */ - LIBUSB_CLASS_HUB = 9, - - /** Data class */ - LIBUSB_CLASS_DATA = 10, - - /** Smart Card */ - LIBUSB_CLASS_SMART_CARD = 0x0b, - - /** Content Security */ - LIBUSB_CLASS_CONTENT_SECURITY = 0x0d, - - /** Video */ - LIBUSB_CLASS_VIDEO = 0x0e, - - /** Personal Healthcare */ - LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f, - - /** Diagnostic Device */ - LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, - - /** Wireless class */ - LIBUSB_CLASS_WIRELESS = 0xe0, - - /** Application class */ - LIBUSB_CLASS_APPLICATION = 0xfe, - - /** Class is vendor-specific */ - LIBUSB_CLASS_VENDOR_SPEC = 0xff -}; - -/** \ingroup desc - * Descriptor types as defined by the USB specification. */ -enum libusb_descriptor_type { - /** Device descriptor. See libusb_device_descriptor. */ - LIBUSB_DT_DEVICE = 0x01, - - /** Configuration descriptor. See libusb_config_descriptor. */ - LIBUSB_DT_CONFIG = 0x02, - - /** String descriptor */ - LIBUSB_DT_STRING = 0x03, - - /** Interface descriptor. See libusb_interface_descriptor. */ - LIBUSB_DT_INTERFACE = 0x04, - - /** Endpoint descriptor. See libusb_endpoint_descriptor. */ - LIBUSB_DT_ENDPOINT = 0x05, - - /** HID descriptor */ - LIBUSB_DT_HID = 0x21, - - /** HID report descriptor */ - LIBUSB_DT_REPORT = 0x22, - - /** Physical descriptor */ - LIBUSB_DT_PHYSICAL = 0x23, - - /** Hub descriptor */ - LIBUSB_DT_HUB = 0x29, - - /** SuperSpeed Hub descriptor */ - LIBUSB_DT_SUPERSPEED_HUB = 0x2A, -}; - -/* Descriptor sizes per descriptor type */ -#define LIBUSB_DT_DEVICE_SIZE 18 -#define LIBUSB_DT_CONFIG_SIZE 9 -#define LIBUSB_DT_INTERFACE_SIZE 9 -#define LIBUSB_DT_ENDPOINT_SIZE 7 -#define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ -#define LIBUSB_DT_HUB_NONVAR_SIZE 7 - -#define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ -#define LIBUSB_ENDPOINT_DIR_MASK 0x80 - -/** \ingroup desc - * Endpoint direction. Values for bit 7 of the - * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. - */ -enum libusb_endpoint_direction { - /** In: device-to-host */ - LIBUSB_ENDPOINT_IN = 0x80, - - /** Out: host-to-device */ - LIBUSB_ENDPOINT_OUT = 0x00 -}; - -#define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ - -/** \ingroup desc - * Endpoint transfer type. Values for bits 0:1 of the - * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. - */ -enum libusb_transfer_type { - /** Control endpoint */ - LIBUSB_TRANSFER_TYPE_CONTROL = 0, - - /** Isochronous endpoint */ - LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, - - /** Bulk endpoint */ - LIBUSB_TRANSFER_TYPE_BULK = 2, - - /** Interrupt endpoint */ - LIBUSB_TRANSFER_TYPE_INTERRUPT = 3 -}; - -/** \ingroup misc - * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */ -enum libusb_standard_request { - /** Request status of the specific recipient */ - LIBUSB_REQUEST_GET_STATUS = 0x00, - - /** Clear or disable a specific feature */ - LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, - - /* 0x02 is reserved */ - - /** Set or enable a specific feature */ - LIBUSB_REQUEST_SET_FEATURE = 0x03, - - /* 0x04 is reserved */ - - /** Set device address for all future accesses */ - LIBUSB_REQUEST_SET_ADDRESS = 0x05, - - /** Get the specified descriptor */ - LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, - - /** Used to update existing descriptors or add new descriptors */ - LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, - - /** Get the current device configuration value */ - LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, - - /** Set device configuration */ - LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, - - /** Return the selected alternate setting for the specified interface */ - LIBUSB_REQUEST_GET_INTERFACE = 0x0A, - - /** Select an alternate interface for the specified interface */ - LIBUSB_REQUEST_SET_INTERFACE = 0x0B, - - /** Set then report an endpoint's synchronization frame */ - LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, - - /** Sets both the U1 and U2 Exit Latency */ - LIBUSB_REQUEST_SET_SEL = 0x30, - - /** Delay from the time a host transmits a packet to the time it is - * received by the device. */ - LIBUSB_SET_ISOCH_DELAY = 0x31, -}; - -/** \ingroup misc - * Request type bits of the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control - * transfers. */ -enum libusb_request_type { - /** Standard */ - LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), - - /** Class */ - LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), - - /** Vendor */ - LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), - - /** Reserved */ - LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) -}; - -/** \ingroup misc - * Recipient bits of the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control - * transfers. Values 4 through 31 are reserved. */ -enum libusb_request_recipient { - /** Device */ - LIBUSB_RECIPIENT_DEVICE = 0x00, - - /** Interface */ - LIBUSB_RECIPIENT_INTERFACE = 0x01, - - /** Endpoint */ - LIBUSB_RECIPIENT_ENDPOINT = 0x02, - - /** Other */ - LIBUSB_RECIPIENT_OTHER = 0x03, -}; - -#define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C - -/** \ingroup desc - * Synchronization type for isochronous endpoints. Values for bits 2:3 of the - * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in - * libusb_endpoint_descriptor. - */ -enum libusb_iso_sync_type { - /** No synchronization */ - LIBUSB_ISO_SYNC_TYPE_NONE = 0, - - /** Asynchronous */ - LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, - - /** Adaptive */ - LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, - - /** Synchronous */ - LIBUSB_ISO_SYNC_TYPE_SYNC = 3 -}; - -#define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 - -/** \ingroup desc - * Usage type for isochronous endpoints. Values for bits 4:5 of the - * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in - * libusb_endpoint_descriptor. - */ -enum libusb_iso_usage_type { - /** Data endpoint */ - LIBUSB_ISO_USAGE_TYPE_DATA = 0, - - /** Feedback endpoint */ - LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, - - /** Implicit feedback Data endpoint */ - LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, -}; - -/** \ingroup desc - * A structure representing the standard USB device descriptor. This - * descriptor is documented in section 9.6.1 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_device_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this - * context. */ - uint8_t bDescriptorType; - - /** USB specification release number in binary-coded decimal. A value of - * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ - uint16_t bcdUSB; - - /** USB-IF class code for the device. See \ref libusb_class_code. */ - uint8_t bDeviceClass; - - /** USB-IF subclass code for the device, qualified by the bDeviceClass - * value */ - uint8_t bDeviceSubClass; - - /** USB-IF protocol code for the device, qualified by the bDeviceClass and - * bDeviceSubClass values */ - uint8_t bDeviceProtocol; - - /** Maximum packet size for endpoint 0 */ - uint8_t bMaxPacketSize0; - - /** USB-IF vendor ID */ - uint16_t idVendor; - - /** USB-IF product ID */ - uint16_t idProduct; - - /** Device release number in binary-coded decimal */ - uint16_t bcdDevice; - - /** Index of string descriptor describing manufacturer */ - uint8_t iManufacturer; - - /** Index of string descriptor describing product */ - uint8_t iProduct; - - /** Index of string descriptor containing device serial number */ - uint8_t iSerialNumber; - - /** Number of possible configurations */ - uint8_t bNumConfigurations; -}; - -/** \ingroup desc - * A structure representing the standard USB endpoint descriptor. This - * descriptor is documented in section 9.6.6 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_endpoint_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in - * this context. */ - uint8_t bDescriptorType; - - /** The address of the endpoint described by this descriptor. Bits 0:3 are - * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, - * see \ref libusb_endpoint_direction. - */ - uint8_t bEndpointAddress; - - /** Attributes which apply to the endpoint when it is configured using - * the bConfigurationValue. Bits 0:1 determine the transfer type and - * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for - * isochronous endpoints and correspond to \ref libusb_iso_sync_type. - * Bits 4:5 are also only used for isochronous endpoints and correspond to - * \ref libusb_iso_usage_type. Bits 6:7 are reserved. - */ - uint8_t bmAttributes; - - /** Maximum packet size this endpoint is capable of sending/receiving. */ - uint16_t wMaxPacketSize; - - /** Interval for polling endpoint for data transfers. */ - uint8_t bInterval; - - /** For audio devices only: the rate at which synchronization feedback - * is provided. */ - uint8_t bRefresh; - - /** For audio devices only: the address if the synch endpoint */ - uint8_t bSynchAddress; - - /** Extra descriptors. If libusbx encounters unknown endpoint descriptors, - * it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup desc - * A structure representing the standard USB interface descriptor. This - * descriptor is documented in section 9.6.5 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_interface_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE - * in this context. */ - uint8_t bDescriptorType; - - /** Number of this interface */ - uint8_t bInterfaceNumber; - - /** Value used to select this alternate setting for this interface */ - uint8_t bAlternateSetting; - - /** Number of endpoints used by this interface (excluding the control - * endpoint). */ - uint8_t bNumEndpoints; - - /** USB-IF class code for this interface. See \ref libusb_class_code. */ - uint8_t bInterfaceClass; - - /** USB-IF subclass code for this interface, qualified by the - * bInterfaceClass value */ - uint8_t bInterfaceSubClass; - - /** USB-IF protocol code for this interface, qualified by the - * bInterfaceClass and bInterfaceSubClass values */ - uint8_t bInterfaceProtocol; - - /** Index of string descriptor describing this interface */ - uint8_t iInterface; - - /** Array of endpoint descriptors. This length of this array is determined - * by the bNumEndpoints field. */ - const struct libusb_endpoint_descriptor *endpoint; - - /** Extra descriptors. If libusbx encounters unknown interface descriptors, - * it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup desc - * A collection of alternate settings for a particular USB interface. - */ -struct libusb_interface { - /** Array of interface descriptors. The length of this array is determined - * by the num_altsetting field. */ - const struct libusb_interface_descriptor *altsetting; - - /** The number of alternate settings that belong to this interface */ - int num_altsetting; -}; - -/** \ingroup desc - * A structure representing the standard USB configuration descriptor. This - * descriptor is documented in section 9.6.3 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_config_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG - * in this context. */ - uint8_t bDescriptorType; - - /** Total length of data returned for this configuration */ - uint16_t wTotalLength; - - /** Number of interfaces supported by this configuration */ - uint8_t bNumInterfaces; - - /** Identifier value for this configuration */ - uint8_t bConfigurationValue; - - /** Index of string descriptor describing this configuration */ - uint8_t iConfiguration; - - /** Configuration characteristics */ - uint8_t bmAttributes; - - /** Maximum power consumption of the USB device from this bus in this - * configuration when the device is fully opreation. Expressed in units - * of 2 mA. */ - uint8_t MaxPower; - - /** Array of interfaces supported by this configuration. The length of - * this array is determined by the bNumInterfaces field. */ - const struct libusb_interface *interface; - - /** Extra descriptors. If libusbx encounters unknown configuration - * descriptors, it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup asyncio - * Setup packet for control transfers. */ -struct libusb_control_setup { - /** Request type. Bits 0:4 determine recipient, see - * \ref libusb_request_recipient. Bits 5:6 determine type, see - * \ref libusb_request_type. Bit 7 determines data transfer direction, see - * \ref libusb_endpoint_direction. - */ - uint8_t bmRequestType; - - /** Request. If the type bits of bmRequestType are equal to - * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD - * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to - * \ref libusb_standard_request. For other cases, use of this field is - * application-specific. */ - uint8_t bRequest; - - /** Value. Varies according to request */ - uint16_t wValue; - - /** Index. Varies according to request, typically used to pass an index - * or offset */ - uint16_t wIndex; - - /** Number of bytes to transfer */ - uint16_t wLength; -}; - -#define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) - -/* libusbx */ - -struct libusb_context; -struct libusb_device; -struct libusb_device_handle; - -/** \ingroup lib - * Structure providing the version of the libusbx runtime - */ -struct libusb_version { - /** Library major version. */ - const uint16_t major; - - /** Library minor version. */ - const uint16_t minor; - - /** Library micro version. */ - const uint16_t micro; - - /** Library nano version. */ - const uint16_t nano; - - /** Library release candidate suffix string, e.g. "-rc4". */ - const char *rc; - - /** For ABI compatibility only. */ - const char* describe; -}; - -/** \ingroup lib - * Structure representing a libusbx session. The concept of individual libusbx - * sessions allows for your program to use two libraries (or dynamically - * load two modules) which both independently use libusb. This will prevent - * interference between the individual libusbx users - for example - * libusb_set_debug() will not affect the other user of the library, and - * libusb_exit() will not destroy resources that the other user is still - * using. - * - * Sessions are created by libusb_init() and destroyed through libusb_exit(). - * If your application is guaranteed to only ever include a single libusbx - * user (i.e. you), you do not have to worry about contexts: pass NULL in - * every function call where a context is required. The default context - * will be used. - * - * For more information, see \ref contexts. - */ -typedef struct libusb_context libusb_context; - -/** \ingroup dev - * Structure representing a USB device detected on the system. This is an - * opaque type for which you are only ever provided with a pointer, usually - * originating from libusb_get_device_list(). - * - * Certain operations can be performed on a device, but in order to do any - * I/O you will have to first obtain a device handle using libusb_open(). - * - * Devices are reference counted with libusb_device_ref() and - * libusb_device_unref(), and are freed when the reference count reaches 0. - * New devices presented by libusb_get_device_list() have a reference count of - * 1, and libusb_free_device_list() can optionally decrease the reference count - * on all devices in the list. libusb_open() adds another reference which is - * later destroyed by libusb_close(). - */ -typedef struct libusb_device libusb_device; - - -/** \ingroup dev - * Structure representing a handle on a USB device. This is an opaque type for - * which you are only ever provided with a pointer, usually originating from - * libusb_open(). - * - * A device handle is used to perform I/O and other operations. When finished - * with a device handle, you should call libusb_close(). - */ -typedef struct libusb_device_handle libusb_device_handle; - -/** \ingroup dev - * Speed codes. Indicates the speed at which the device is operating. - */ -enum libusb_speed { - /** The OS doesn't report or know the device speed. */ - LIBUSB_SPEED_UNKNOWN = 0, - - /** The device is operating at low speed (1.5MBit/s). */ - LIBUSB_SPEED_LOW = 1, - - /** The device is operating at full speed (12MBit/s). */ - LIBUSB_SPEED_FULL = 2, - - /** The device is operating at high speed (480MBit/s). */ - LIBUSB_SPEED_HIGH = 3, - - /** The device is operating at super speed (5000MBit/s). */ - LIBUSB_SPEED_SUPER = 4, -}; - -/** \ingroup misc - * Error codes. Most libusbx functions return 0 on success or one of these - * codes on failure. - * You can call \ref libusb_error_name() to retrieve a string representation - * of an error code. - */ -enum libusb_error { - /** Success (no error) */ - LIBUSB_SUCCESS = 0, - - /** Input/output error */ - LIBUSB_ERROR_IO = -1, - - /** Invalid parameter */ - LIBUSB_ERROR_INVALID_PARAM = -2, - - /** Access denied (insufficient permissions) */ - LIBUSB_ERROR_ACCESS = -3, - - /** No such device (it may have been disconnected) */ - LIBUSB_ERROR_NO_DEVICE = -4, - - /** Entity not found */ - LIBUSB_ERROR_NOT_FOUND = -5, - - /** Resource busy */ - LIBUSB_ERROR_BUSY = -6, - - /** Operation timed out */ - LIBUSB_ERROR_TIMEOUT = -7, - - /** Overflow */ - LIBUSB_ERROR_OVERFLOW = -8, - - /** Pipe error */ - LIBUSB_ERROR_PIPE = -9, - - /** System call interrupted (perhaps due to signal) */ - LIBUSB_ERROR_INTERRUPTED = -10, - - /** Insufficient memory */ - LIBUSB_ERROR_NO_MEM = -11, - - /** Operation not supported or unimplemented on this platform */ - LIBUSB_ERROR_NOT_SUPPORTED = -12, - - /* NB! Remember to update libusb_error_name() - when adding new error codes here. */ - - /** Other error */ - LIBUSB_ERROR_OTHER = -99, -}; - -/** \ingroup asyncio - * Transfer status codes */ -enum libusb_transfer_status { - /** Transfer completed without error. Note that this does not indicate - * that the entire amount of requested data was transferred. */ - LIBUSB_TRANSFER_COMPLETED, - - /** Transfer failed */ - LIBUSB_TRANSFER_ERROR, - - /** Transfer timed out */ - LIBUSB_TRANSFER_TIMED_OUT, - - /** Transfer was cancelled */ - LIBUSB_TRANSFER_CANCELLED, - - /** For bulk/interrupt endpoints: halt condition detected (endpoint - * stalled). For control endpoints: control request not supported. */ - LIBUSB_TRANSFER_STALL, - - /** Device was disconnected */ - LIBUSB_TRANSFER_NO_DEVICE, - - /** Device sent more data than requested */ - LIBUSB_TRANSFER_OVERFLOW, - - /* NB! Remember to update libusb_error_name() - when adding new status codes here. */ -}; - -/** \ingroup asyncio - * libusb_transfer.flags values */ -enum libusb_transfer_flags { - /** Report short frames as errors */ - LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, - - /** Automatically free() transfer buffer during libusb_free_transfer() */ - LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, - - /** Automatically call libusb_free_transfer() after callback returns. - * If this flag is set, it is illegal to call libusb_free_transfer() - * from your transfer callback, as this will result in a double-free - * when this flag is acted upon. */ - LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, - - /** Terminate transfers that are a multiple of the endpoint's - * wMaxPacketSize with an extra zero length packet. This is useful - * when a device protocol mandates that each logical request is - * terminated by an incomplete packet (i.e. the logical requests are - * not separated by other means). - * - * This flag only affects host-to-device transfers to bulk and interrupt - * endpoints. In other situations, it is ignored. - * - * This flag only affects transfers with a length that is a multiple of - * the endpoint's wMaxPacketSize. On transfers of other lengths, this - * flag has no effect. Therefore, if you are working with a device that - * needs a ZLP whenever the end of the logical request falls on a packet - * boundary, then it is sensible to set this flag on every - * transfer (you do not have to worry about only setting it on transfers - * that end on the boundary). - * - * This flag is currently only supported on Linux. - * On other systems, libusb_submit_transfer() will return - * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set. - * - * Available since libusb-1.0.9. - */ - LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3, -}; - -/** \ingroup asyncio - * Isochronous packet descriptor. */ -struct libusb_iso_packet_descriptor { - /** Length of data to request in this packet */ - unsigned int length; - - /** Amount of data that was actually transferred */ - unsigned int actual_length; - - /** Status code for this packet */ - enum libusb_transfer_status status; -}; - -struct libusb_transfer; - -/** \ingroup asyncio - * Asynchronous transfer callback function type. When submitting asynchronous - * transfers, you pass a pointer to a callback function of this type via the - * \ref libusb_transfer::callback "callback" member of the libusb_transfer - * structure. libusbx will call this function later, when the transfer has - * completed or failed. See \ref asyncio for more information. - * \param transfer The libusb_transfer struct the callback function is being - * notified about. - */ -typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer); - -/** \ingroup asyncio - * The generic USB transfer structure. The user populates this structure and - * then submits it in order to request a transfer. After the transfer has - * completed, the library populates the transfer with the results and passes - * it back to the user. - */ -struct libusb_transfer { - /** Handle of the device that this transfer will be submitted to */ - libusb_device_handle *dev_handle; - - /** A bitwise OR combination of \ref libusb_transfer_flags. */ - uint8_t flags; - - /** Address of the endpoint where this transfer will be sent. */ - unsigned char endpoint; - - /** Type of the endpoint from \ref libusb_transfer_type */ - unsigned char type; - - /** Timeout for this transfer in millseconds. A value of 0 indicates no - * timeout. */ - unsigned int timeout; - - /** The status of the transfer. Read-only, and only for use within - * transfer callback function. - * - * If this is an isochronous transfer, this field may read COMPLETED even - * if there were errors in the frames. Use the - * \ref libusb_iso_packet_descriptor::status "status" field in each packet - * to determine if errors occurred. */ - enum libusb_transfer_status status; - - /** Length of the data buffer */ - int length; - - /** Actual length of data that was transferred. Read-only, and only for - * use within transfer callback function. Not valid for isochronous - * endpoint transfers. */ - int actual_length; - - /** Callback function. This will be invoked when the transfer completes, - * fails, or is cancelled. */ - libusb_transfer_cb_fn callback; - - /** User context data to pass to the callback function. */ - void *user_data; - - /** Data buffer */ - unsigned char *buffer; - - /** Number of isochronous packets. Only used for I/O with isochronous - * endpoints. */ - int num_iso_packets; - - /** Isochronous packet descriptors, for isochronous transfers only. */ - struct libusb_iso_packet_descriptor iso_packet_desc -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) - [] /* valid C99 code */ -#else - [0] /* non-standard, but usually working code */ -#endif - ; -}; - -/** \ingroup misc - * Capabilities supported by this instance of libusb. Test if the loaded - * library supports a given capability by calling - * \ref libusb_has_capability(). - */ -enum libusb_capability { - /** The libusb_has_capability() API is available. */ - LIBUSB_CAP_HAS_CAPABILITY = 0, -}; - -/** \ingroup lib - * Log message levels. - * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default) - * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning - * and error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout, - * warnings and errors to stderr - */ -enum libusb_log_level { - LIBUSB_LOG_LEVEL_NONE = 0, - LIBUSB_LOG_LEVEL_ERROR, - LIBUSB_LOG_LEVEL_WARNING, - LIBUSB_LOG_LEVEL_INFO, - LIBUSB_LOG_LEVEL_DEBUG, -}; - -int LIBUSB_CALL libusb_init(libusb_context **ctx); -void LIBUSB_CALL libusb_exit(libusb_context *ctx); -void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); -const struct libusb_version * LIBUSB_CALL libusb_get_version(void); -int LIBUSB_CALL libusb_has_capability(uint32_t capability); -const char * LIBUSB_CALL libusb_error_name(int errcode); - -ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx, - libusb_device ***list); -void LIBUSB_CALL libusb_free_device_list(libusb_device **list, - int unref_devices); -libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev); -void LIBUSB_CALL libusb_unref_device(libusb_device *dev); - -int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev, - int *config); -int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev, - struct libusb_device_descriptor *desc); -int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev, - struct libusb_config_descriptor **config); -int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev, - uint8_t config_index, struct libusb_config_descriptor **config); -int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev, - uint8_t bConfigurationValue, struct libusb_config_descriptor **config); -void LIBUSB_CALL libusb_free_config_descriptor( - struct libusb_config_descriptor *config); -uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev); -uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev); -libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev); -int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length); -uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev); -int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev); -int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev, - unsigned char endpoint); -int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev, - unsigned char endpoint); - -int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle); -void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle); -libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle); - -int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev, - int configuration); -int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev, - int interface_number); -int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev, - int interface_number); - -libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( - libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); - -int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev, - int interface_number, int alternate_setting); -int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev, - unsigned char endpoint); -int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev); - -int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev, - int interface_number); -int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev, - int interface_number); -int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev, - int interface_number); - -/* async I/O */ - -/** \ingroup asyncio - * Get the data section of a control transfer. This convenience function is here - * to remind you that the data does not start until 8 bytes into the actual - * buffer, as the setup packet comes first. - * - * Calling this function only makes sense from a transfer callback function, - * or situations where you have already allocated a suitably sized buffer at - * transfer->buffer. - * - * \param transfer a transfer - * \returns pointer to the first byte of the data section - */ -static inline unsigned char *libusb_control_transfer_get_data( - struct libusb_transfer *transfer) -{ - return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; -} - -/** \ingroup asyncio - * Get the control setup packet of a control transfer. This convenience - * function is here to remind you that the control setup occupies the first - * 8 bytes of the transfer data buffer. - * - * Calling this function only makes sense from a transfer callback function, - * or situations where you have already allocated a suitably sized buffer at - * transfer->buffer. - * - * \param transfer a transfer - * \returns a casted pointer to the start of the transfer data buffer - */ -static inline struct libusb_control_setup *libusb_control_transfer_get_setup( - struct libusb_transfer *transfer) -{ - return (struct libusb_control_setup *) transfer->buffer; -} - -/** \ingroup asyncio - * Helper function to populate the setup packet (first 8 bytes of the data - * buffer) for a control transfer. The wIndex, wValue and wLength values should - * be given in host-endian byte order. - * - * \param buffer buffer to output the setup packet into - * \param bmRequestType see the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field of - * \ref libusb_control_setup - * \param bRequest see the - * \ref libusb_control_setup::bRequest "bRequest" field of - * \ref libusb_control_setup - * \param wValue see the - * \ref libusb_control_setup::wValue "wValue" field of - * \ref libusb_control_setup - * \param wIndex see the - * \ref libusb_control_setup::wIndex "wIndex" field of - * \ref libusb_control_setup - * \param wLength see the - * \ref libusb_control_setup::wLength "wLength" field of - * \ref libusb_control_setup - */ -static inline void libusb_fill_control_setup(unsigned char *buffer, - uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, - uint16_t wLength) -{ - struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; - setup->bmRequestType = bmRequestType; - setup->bRequest = bRequest; - setup->wValue = libusb_cpu_to_le16(wValue); - setup->wIndex = libusb_cpu_to_le16(wIndex); - setup->wLength = libusb_cpu_to_le16(wLength); -} - -struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets); -int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer); -int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer); -void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer); - -/** \ingroup asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for a control transfer. - * - * If you pass a transfer buffer to this function, the first 8 bytes will - * be interpreted as a control setup packet, and the wLength field will be - * used to automatically populate the \ref libusb_transfer::length "length" - * field of the transfer. Therefore the recommended approach is: - * -# Allocate a suitably sized data buffer (including space for control setup) - * -# Call libusb_fill_control_setup() - * -# If this is a host-to-device transfer with a data stage, put the data - * in place after the setup packet - * -# Call this function - * -# Call libusb_submit_transfer() - * - * It is also legal to pass a NULL buffer to this function, in which case this - * function will not attempt to populate the length field. Remember that you - * must then populate the buffer and length fields later. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param buffer data buffer. If provided, this function will interpret the - * first 8 bytes as a setup packet and infer the transfer length from that. - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_control_transfer( - struct libusb_transfer *transfer, libusb_device_handle *dev_handle, - unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, - unsigned int timeout) -{ - struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; - transfer->dev_handle = dev_handle; - transfer->endpoint = 0; - transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; - transfer->timeout = timeout; - transfer->buffer = buffer; - if (setup) - transfer->length = LIBUSB_CONTROL_SETUP_SIZE - + libusb_le16_to_cpu(setup->wLength); - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for a bulk transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, - libusb_device_handle *dev_handle, unsigned char endpoint, - unsigned char *buffer, int length, libusb_transfer_cb_fn callback, - void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_BULK; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for an interrupt transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_interrupt_transfer( - struct libusb_transfer *transfer, libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *buffer, int length, - libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for an isochronous transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param num_iso_packets the number of isochronous packets - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, - libusb_device_handle *dev_handle, unsigned char endpoint, - unsigned char *buffer, int length, int num_iso_packets, - libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->num_iso_packets = num_iso_packets; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup asyncio - * Convenience function to set the length of all packets in an isochronous - * transfer, based on the num_iso_packets field in the transfer structure. - * - * \param transfer a transfer - * \param length the length to set in each isochronous packet descriptor - * \see libusb_get_max_packet_size() - */ -static inline void libusb_set_iso_packet_lengths( - struct libusb_transfer *transfer, unsigned int length) -{ - int i; - for (i = 0; i < transfer->num_iso_packets; i++) - transfer->iso_packet_desc[i].length = length; -} - -/** \ingroup asyncio - * Convenience function to locate the position of an isochronous packet - * within the buffer of an isochronous transfer. - * - * This is a thorough function which loops through all preceding packets, - * accumulating their lengths to find the position of the specified packet. - * Typically you will assign equal lengths to each packet in the transfer, - * and hence the above method is sub-optimal. You may wish to use - * libusb_get_iso_packet_buffer_simple() instead. - * - * \param transfer a transfer - * \param packet the packet to return the address of - * \returns the base address of the packet buffer inside the transfer buffer, - * or NULL if the packet does not exist. - * \see libusb_get_iso_packet_buffer_simple() - */ -static inline unsigned char *libusb_get_iso_packet_buffer( - struct libusb_transfer *transfer, unsigned int packet) -{ - int i; - size_t offset = 0; - int _packet; - - /* oops..slight bug in the API. packet is an unsigned int, but we use - * signed integers almost everywhere else. range-check and convert to - * signed to avoid compiler warnings. FIXME for libusb-2. */ - if (packet > INT_MAX) - return NULL; - _packet = packet; - - if (_packet >= transfer->num_iso_packets) - return NULL; - - for (i = 0; i < _packet; i++) - offset += transfer->iso_packet_desc[i].length; - - return transfer->buffer + offset; -} - -/** \ingroup asyncio - * Convenience function to locate the position of an isochronous packet - * within the buffer of an isochronous transfer, for transfers where each - * packet is of identical size. - * - * This function relies on the assumption that every packet within the transfer - * is of identical size to the first packet. Calculating the location of - * the packet buffer is then just a simple calculation: - * buffer + (packet_size * packet) - * - * Do not use this function on transfers other than those that have identical - * packet lengths for each packet. - * - * \param transfer a transfer - * \param packet the packet to return the address of - * \returns the base address of the packet buffer inside the transfer buffer, - * or NULL if the packet does not exist. - * \see libusb_get_iso_packet_buffer() - */ -static inline unsigned char *libusb_get_iso_packet_buffer_simple( - struct libusb_transfer *transfer, unsigned int packet) -{ - int _packet; - - /* oops..slight bug in the API. packet is an unsigned int, but we use - * signed integers almost everywhere else. range-check and convert to - * signed to avoid compiler warnings. FIXME for libusb-2. */ - if (packet > INT_MAX) - return NULL; - _packet = packet; - - if (_packet >= transfer->num_iso_packets) - return NULL; - - return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet); -} - -/* sync I/O */ - -int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle, - uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, - unsigned char *data, uint16_t wLength, unsigned int timeout); - -int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *data, int length, - int *actual_length, unsigned int timeout); - -int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *data, int length, - int *actual_length, unsigned int timeout); - -/** \ingroup desc - * Retrieve a descriptor from the default control pipe. - * This is a convenience function which formulates the appropriate control - * message to retrieve the descriptor. - * - * \param dev a device handle - * \param desc_type the descriptor type, see \ref libusb_descriptor_type - * \param desc_index the index of the descriptor to retrieve - * \param data output buffer for descriptor - * \param length size of data buffer - * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure - */ -static inline int libusb_get_descriptor(libusb_device_handle *dev, - uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) -{ - return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, - LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data, - (uint16_t) length, 1000); -} - -/** \ingroup desc - * Retrieve a descriptor from a device. - * This is a convenience function which formulates the appropriate control - * message to retrieve the descriptor. The string returned is Unicode, as - * detailed in the USB specifications. - * - * \param dev a device handle - * \param desc_index the index of the descriptor to retrieve - * \param langid the language ID for the string descriptor - * \param data output buffer for descriptor - * \param length size of data buffer - * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure - * \see libusb_get_string_descriptor_ascii() - */ -static inline int libusb_get_string_descriptor(libusb_device_handle *dev, - uint8_t desc_index, uint16_t langid, unsigned char *data, int length) -{ - return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, - LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index), - langid, data, (uint16_t) length, 1000); -} - -int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev, - uint8_t desc_index, unsigned char *data, int length); - -/* polling and timeouts */ - -int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx); -void LIBUSB_CALL libusb_lock_events(libusb_context *ctx); -void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx); -int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx); -int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx); -void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx); -void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx); -int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); - -int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx, - struct timeval *tv); -int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx, - struct timeval *tv, int *completed); -int LIBUSB_CALL libusb_handle_events(libusb_context *ctx); -int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed); -int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx, - struct timeval *tv); -int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx); -int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx, - struct timeval *tv); - -/** \ingroup poll - * File descriptor for polling - */ -struct libusb_pollfd { - /** Numeric file descriptor */ - int fd; - - /** Event flags to poll for from . POLLIN indicates that you - * should monitor this file descriptor for becoming ready to read from, - * and POLLOUT indicates that you should monitor this file descriptor for - * nonblocking write readiness. */ - short events; -}; - -/** \ingroup poll - * Callback function, invoked when a new file descriptor should be added - * to the set of file descriptors monitored for events. - * \param fd the new file descriptor - * \param events events to monitor for, see \ref libusb_pollfd for a - * description - * \param user_data User data pointer specified in - * libusb_set_pollfd_notifiers() call - * \see libusb_set_pollfd_notifiers() - */ -typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events, - void *user_data); - -/** \ingroup poll - * Callback function, invoked when a file descriptor should be removed from - * the set of file descriptors being monitored for events. After returning - * from this callback, do not use that file descriptor again. - * \param fd the file descriptor to stop monitoring - * \param user_data User data pointer specified in - * libusb_set_pollfd_notifiers() call - * \see libusb_set_pollfd_notifiers() - */ -typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data); - -const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds( - libusb_context *ctx); -void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx, - libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, - void *user_data); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/ps3eye.cpp b/src/ps3eye.cpp index e88fe72..70fbad0 100644 --- a/src/ps3eye.cpp +++ b/src/ps3eye.cpp @@ -1,20 +1,234 @@ - #include "ps3eye.h" +#define PLATFORM_WIN32 1 +#define PLATFORM_LINUX 2 +#define PLATFORM_OSX 3 + #if defined WIN32 || defined _WIN32 || defined WINCE + #define CURRENT_PLATFORM PLATFORM_WIN32 #include #include #else #include #include + #include + #include + #if defined __MACH__ && defined __APPLE__ + #define CURRENT_PLATFORM PLATFORM_OSX #include #include + #else + #define CURRENT_PLATFORM PLATFORM_LINUX #endif #endif +#define PLATFORM_IS_WIN32 (CURRENT_PLATFORM == PLATFORM_WIN32) +#define PLATFORM_IS_LINUX (CURRENT_PLATFORM == PLATFORM_LINUX) +#define PLATFORM_IS_OSX (CURRENT_PLATFORM == PLATFORM_OSX) + namespace ps3eye { +class Mutex +{ +public: + friend class ConditionVariable; + + struct AutoLock + { + AutoLock(Mutex& mutex) : + mutex(mutex) + { + mutex.Lock(); + } + + ~AutoLock() + { + mutex.Unlock(); + } + + Mutex& mutex; + }; + +#if PLATFORM_IS_WIN32 + Mutex() { InitializeCriticalSection(&mutex); } + ~Mutex() { DeleteCriticalSection(&mutex); } + + void Lock() { EnterCriticalSection(&mutex); } + void Unlock() { LeaveCriticalSection(&mutex); } +#else + Mutex() { pthread_mutex_init(&mutex, NULL); } + ~Mutex() { pthread_mutex_destroy(&mutex); } + + void Lock() { pthread_mutex_lock(&mutex); } + void Unlock() { pthread_mutex_unlock(&mutex); } +#endif + +private: + +#if PLATFORM_IS_WIN32 + CRITICAL_SECTION mutex; +#else + pthread_mutex_t mutex; +#endif +}; + +class ConditionVariable +{ +public: + enum EWaitResult + { + Signaled, + TimedOut + }; + +#if PLATFORM_IS_WIN32 + ConditionVariable(Mutex& mutex) : mutex(mutex) { InitializeConditionVariable(&cond); } + + EWaitResult Wait(int timeout_ms = 0) { return SleepConditionVariableCS(&cond, &mutex.mutex, timeout_ms == 0 ? INFINITE : timeout_ms) != 0 ? Signaled : TimedOut; } + void NotifyOne() { WakeConditionVariable(&cond); } +#else + ConditionVariable(Mutex& mutex) : mutex(mutex) { pthread_cond_init(&cond, NULL); } + + EWaitResult Wait(int timeout_ms = 0) + { + if (timeout_ms == 0) + { + pthread_cond_wait(&cond, &mutex.mutex); + return Signaled; + } + else + { + struct timeval tv; + gettimeofday(&tv, NULL); + + struct timespec ts; + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000; + ts.tv_nsec += timeout_ms * 1000000; + + return pthread_cond_timedwait(&cond, &mutex.mutex, &ts) == 0 ? Signaled : TimedOut; + } + } + void NotifyOne() { pthread_cond_signal(&cond); } +#endif + +private: + Mutex& mutex; + +#if PLATFORM_IS_WIN32 + CONDITION_VARIABLE cond; +#else + pthread_cond_t cond; +#endif +}; + +class Semaphore +{ +public: + Semaphore() : + sema(NULL) + { + } + + ~Semaphore() + { + Destroy(); + } + +#if PLATFORM_IS_WIN32 + void Init(int init_count, int max_count) { sema = CreateSemaphore(NULL, init_count, max_count, NULL); } + void Destroy() { CloseHandle(sema); } + + void Acquire() { WaitForSingleObject(sema, INFINITE); } + void Release() { ReleaseSemaphore(sema, 1, NULL); } +#else + void Init(int init_count, int max_count) { sem_init(&sema, 0, init_count); } + void Destroy() { sem_destroy(&sema); } + + void Acquire() { sem_wait(&sema); } + void Release() { sem_post(&sema); } +#endif + +private: +#if PLATFORM_IS_WIN32 + HANDLE sema; +#else + sem_t sema; +#endif +}; + +class Thread +{ +public: + typedef int (*ThreadFunc)(void* user_data); + +#if PLATFORM_IS_WIN32 + Thread() : + thread_func(NULL), + thread(NULL) + { + } + + void Run(ThreadFunc func, void* user_data) + { + thread_func = func; + this->user_data = user_data; + thread = CreateThread(NULL, 0, &ThreadStub, this, 0, NULL); + } + + void Join() + { + WaitForSingleObject(thread, INFINITE); + CloseHandle(thread); + thread = NULL; + } + + static DWORD __stdcall ThreadStub(void* user_data) + { + Thread* thread = (Thread*)user_data; + return thread->thread_func(thread->user_data); + } +#else + Thread() : + thread_func(NULL) + { + } + + void Run(ThreadFunc func, void* user_data) + { + thread_func = func; + this->user_data = user_data; + pthread_create(&thread, NULL, &ThreadStub, this); + } + + void Join() + { + pthread_join(thread, NULL); + } + + static void* ThreadStub(void* user_data) + { + Thread* thread = (Thread*)user_data; + return (void*)thread->thread_func(thread->user_data); + } +#endif + +private: + ThreadFunc thread_func; + void* user_data; + +#if PLATFORM_IS_WIN32 + HANDLE thread; +#else + pthread_t thread; + // TODO +#endif +}; + +#define TRANSFER_SIZE 16384 +#define NUM_TRANSFERS 8 + #define OV534_REG_ADDRESS 0xf1 /* sensor address */ #define OV534_REG_SUBADDR 0xf2 #define OV534_REG_WRITE 0xf3 @@ -279,7 +493,7 @@ enum gspca_packet_type { */ static uint8_t find_ep(struct libusb_device *device) { - const struct libusb_interface_descriptor *altsetting; + const struct libusb_interface_descriptor *altsetting = NULL; const struct libusb_endpoint_descriptor *ep; struct libusb_config_descriptor *config; int i; @@ -311,67 +525,36 @@ static uint8_t find_ep(struct libusb_device *device) return ep_addr; } -// timestapms -// WIN and MAC only -static int64_t getTickCount() -{ -#if defined WIN32 || defined _WIN32 || defined WINCE - LARGE_INTEGER counter; - QueryPerformanceCounter( &counter ); - return (int64_t)counter.QuadPart; -#else - return (int64_t)mach_absolute_time(); -#endif -} - -static double getTickFrequency() -{ -#if defined WIN32 || defined _WIN32 || defined WINCE - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - return (double)freq.QuadPart; -#else - static double freq = 0; - if( freq == 0 ) - { - mach_timebase_info_data_t sTimebaseInfo; - mach_timebase_info(&sTimebaseInfo); - freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer; - } - return freq; -#endif -} -// - - const uint16_t PS3EYECam::VENDOR_ID = 0x1415; const uint16_t PS3EYECam::PRODUCT_ID = 0x2000; class USBMgr { public: - USBMgr(); - ~USBMgr(); + ~USBMgr(); - static std::shared_ptr instance(); - static libusb_context* usbContext() { return instance()->usb_context; } - static int listDevices(std::vector& list); - static bool handleEvents(); + static std::shared_ptr instance(); + uint32_t getDeviceCount(bool force_refresh); + PS3EYECam::PS3EYERef createDevice(uint32_t index); static std::shared_ptr sInstance; - static int sTotalDevices; + int total_devices; + bool devices_enumerated; private: - libusb_context* usb_context; + USBMgr(); USBMgr(const USBMgr&); void operator=(const USBMgr&); + + libusb_context* usb_context; }; std::shared_ptr USBMgr::sInstance; -int USBMgr::sTotalDevices = 0; -USBMgr::USBMgr() +USBMgr::USBMgr() : + total_devices(0), + devices_enumerated(false) { libusb_init(&usb_context); libusb_set_debug(usb_context, 1); @@ -391,156 +574,331 @@ std::shared_ptr USBMgr::instance() return sInstance; } -bool USBMgr::handleEvents() +uint32_t USBMgr::getDeviceCount(bool force_refresh) { - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 50 * 1000; // ms - return (libusb_handle_events_timeout_completed(instance()->usb_context, &tv, NULL) == 0); + if (devices_enumerated && !force_refresh) + return total_devices; + + libusb_device *dev; + libusb_device **devs; + libusb_device_handle *devhandle; + + if (libusb_get_device_list(instance()->usb_context, &devs) < 0) { + debug("Error Device scan\n"); + } + + int device_count = 0; + int index = 0; + while ((dev = devs[index++]) != NULL) + { + struct libusb_device_descriptor desc; + libusb_get_device_descriptor(dev, &desc); + if (desc.idVendor == PS3EYECam::VENDOR_ID && desc.idProduct == PS3EYECam::PRODUCT_ID) + { + int err = libusb_open(dev, &devhandle); + if (err == 0) + { + libusb_close(devhandle); + device_count++; + } + } + } + + libusb_free_device_list(devs, 1); + + total_devices = device_count; + devices_enumerated = true; + + return device_count; } -int USBMgr::listDevices( std::vector& list ) +PS3EYECam::PS3EYERef USBMgr::createDevice(uint32_t index) { - libusb_device *dev; - libusb_device **devs; - libusb_device_handle *devhandle; - int i = 0; - int cnt; + PS3EYECam::PS3EYERef camera; + + // Create a context specific to the device we're about to open. + // This ensures that the libusb event loop will only affect this specific device + libusb_context* device_context; + libusb_init(&device_context); + libusb_set_debug(device_context, 0); - cnt = libusb_get_device_list(instance()->usb_context, &devs); + libusb_device *dev; + libusb_device **devs; + libusb_device_handle *devhandle; - if (cnt < 0) { + // Enumerate the devices. Note that we use the device specific context here, because libusb_device internally keeps a handle to the context it was enumerated from. + if (libusb_get_device_list(device_context, &devs) < 0) { debug("Error Device scan\n"); } - cnt = 0; - while ((dev = devs[i++]) != NULL) - { - struct libusb_device_descriptor desc; + uint32_t cur_device = 0; + int cur_index = 0; + while ((dev = devs[cur_index++]) != NULL) + { + struct libusb_device_descriptor desc; libusb_get_device_descriptor(dev, &desc); - if(desc.idVendor == PS3EYECam::VENDOR_ID && desc.idProduct == PS3EYECam::PRODUCT_ID) + if (desc.idVendor == PS3EYECam::VENDOR_ID && desc.idProduct == PS3EYECam::PRODUCT_ID) { - int err = libusb_open(dev, &devhandle); - if (err == 0) - { - libusb_close(devhandle); - list.push_back( PS3EYECam::PS3EYERef( new PS3EYECam(dev) ) ); - libusb_ref_device(dev); - cnt++; - - } + int err = libusb_open(dev, &devhandle); + if (err == 0) + { + libusb_close(devhandle); + + if (cur_device == index) + { + // Create the camera. Note that we transfer ownership of the device specific context to the camera here. + camera = PS3EYECam::PS3EYERef(new PS3EYECam(device_context, dev)); + libusb_ref_device(dev); + break; + } + else + { + cur_device++; + } + } } - } + } - libusb_free_device_list(devs, 1); + libusb_free_device_list(devs, 1); - return cnt; + // If no camera matching the specified index was found, we need to destroy the device specific context here. + if (camera == NULL) + { + libusb_exit(device_context); + } + + return camera; } -// URBDesc +static void LIBUSB_CALL transfer_completed_callback(struct libusb_transfer *xfr); + +class FrameQueue +{ +public: + FrameQueue(uint32_t frame_size, uint32_t num_frames) : + frame_size (frame_size), + num_frames (std::max(num_frames, 2u)), + frame_buffer ((uint8_t*)malloc(frame_size * num_frames)), + head (0), + tail (0), + available (0), + empty_condition (mutex) + { + } + + ~FrameQueue() + { + free(frame_buffer); + } + + uint8_t* GetFrameBufferStart() + { + return frame_buffer; + } + + uint8_t* Enqueue() + { + uint8_t* new_frame = NULL; + + Mutex::AutoLock lock(mutex); + + // Unlike traditional producer/consumer, we don't block the producer if the buffer is full (ie. the consumer is not reading data fast enough). + // Instead, if the buffer is full, we simply return the current frame pointer, causing the producer to overwrite the previous frame. + // This allows the service to degrade gracefully: if the consumer is not fast enough (< Camera FPS), it will miss frames, but if it is fast enough (>= Camera FPS), it will see everything. + // + // Note that because the the producer is writing directly to the ring buffer, we can only ever be a maximum of num_frames-1 ahead of the consumer, + // otherwise the producer could overwrite the frame the consumer is currently reading (in case of a slow consumer) + if (available >= num_frames - 1) + { + return frame_buffer + head * frame_size; + } + + // Note: we don't need to copy any data to the buffer since the URB packets are directly written to the frame buffer. + // We just need to update head and available count to signal to the consumer that a new frame is available + head = (head + 1) % num_frames; + available++; + + // Determine the next frame pointer that the producer should write to + new_frame = frame_buffer + head * frame_size; + + // Signal consumer that data became available + empty_condition.NotifyOne(); + + return new_frame; + } + + uint8_t* Dequeue() + { + uint8_t* new_frame = (uint8_t*)malloc(frame_size); + + Mutex::AutoLock lock(mutex); + + // If there is no data in the buffer, wait until data becomes available + while (available == 0) + { + empty_condition.Wait(); + } + + // Copy from internal buffer + uint8_t* source = frame_buffer + frame_size * tail; + memcpy(new_frame, source, frame_size); -static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr); + // Update tail and available count + tail = (tail + 1) % num_frames; + available--; + + return new_frame; + } + +private: + uint32_t frame_size; + uint32_t num_frames; + + uint8_t* frame_buffer; + uint32_t head; + uint32_t tail; + uint32_t available; + + Mutex mutex; + ConditionVariable empty_condition; +}; + +// URBDesc class URBDesc { public: - URBDesc() : num_transfers(0), last_packet_type(DISCARD_PACKET), last_pts(0), last_fid(0) + URBDesc(libusb_context* usb_context) : + usb_context (usb_context), + exit_signaled (false), + transfers_active (false), + last_packet_type (DISCARD_PACKET), + last_pts (0), + last_fid (0), + transfer_buffer (NULL), + cur_frame_start (NULL), + cur_frame_data_len (0), + frame_size (0), + frame_queue (NULL) { - // we allocate max possible size - // 16 frames - size_t stride = 640*2; - const size_t fsz = stride*480; - frame_buffer = (uint8_t*)malloc(fsz * 16 + 16384*2); - frame_buffer_end = frame_buffer + fsz * 16; - - frame_data_start = frame_buffer; - frame_data_len = 0; - frame_complete_ind = 0; - frame_work_ind = 0; - frame_size = fsz; } + ~URBDesc() { debug("URBDesc destructor\n"); - if(num_transfers) + if(transfers_active) { close_transfers(); } - if(frame_buffer != NULL) - free(frame_buffer); - frame_buffer = NULL; } - bool start_transfers(libusb_device_handle *handle, uint32_t curr_frame_size) + bool start_transfers(libusb_device_handle *handle, uint32_t curr_frame_size, uint32_t frame_queue_size) { - struct libusb_transfer *xfr0,*xfr1; - uint8_t* buff, *buff1; - uint8_t ep_addr; - int bsize = 16384; - + // Initialize the frame queue frame_size = curr_frame_size; + frame_queue = new FrameQueue(frame_size, frame_queue_size); - // bulk transfers - xfr0 = libusb_alloc_transfer(0); - xfr1 = libusb_alloc_transfer(0); - - buff = frame_buffer_end; - buff1 = buff + bsize; - memset(frame_buffer_end, 0, bsize*2); + // Initialize the current frame pointer to the start of the buffer; it will be updated as frames are completed and pushed onto the frame queue + cur_frame_start = frame_queue->GetFrameBufferStart(); + cur_frame_data_len = 0; - xfr[0] = xfr0; - xfr[1] = xfr1; + // Create the semaphore used to wait for all transfers to finish when shutting down + active_transfer_sema.Init(NUM_TRANSFERS, NUM_TRANSFERS); - ep_addr = find_ep(libusb_get_device(handle)); - //debug("found ep: %d\n", ep_addr); + // Find the bulk transfer endpoint + uint8_t bulk_endpoint = find_ep(libusb_get_device(handle)); + libusb_clear_halt(handle, bulk_endpoint); - libusb_clear_halt(handle, ep_addr); + // Allocate the transfer buffer + transfer_buffer = (uint8_t*)malloc(TRANSFER_SIZE * NUM_TRANSFERS); + memset(transfer_buffer, 0, TRANSFER_SIZE * NUM_TRANSFERS); - libusb_fill_bulk_transfer(xfr0, handle, ep_addr, buff, bsize, cb_xfr, reinterpret_cast(this), 0); - libusb_fill_bulk_transfer(xfr1, handle, ep_addr, buff1, bsize, cb_xfr, reinterpret_cast(this), 0); - - int res = libusb_submit_transfer(xfr0); - res |= libusb_submit_transfer(xfr1); + int res = 0; + for (int index = 0; index < NUM_TRANSFERS; ++index) + { + // Create & submit the transfer + xfr[index] = libusb_alloc_transfer(0); + libusb_fill_bulk_transfer(xfr[index], handle, bulk_endpoint, transfer_buffer + index * TRANSFER_SIZE, TRANSFER_SIZE, transfer_completed_callback, reinterpret_cast(this), 0); + + res |= libusb_submit_transfer(xfr[index]); + + // Acquire the semaphore (will return immediately since the semaphore is initialized with the max count) + active_transfer_sema.Acquire(); + } - num_transfers = 2; - frame_complete_ind = 0; - frame_work_ind = 0; + transfers_active = true; last_pts = 0; last_fid = 0; - last_frame_time = 0; + + start_transfer_thread(); return res == 0; } void close_transfers() { - libusb_cancel_transfer(xfr[0]); - libusb_cancel_transfer(xfr[1]); - while(num_transfers) - { - if( !USBMgr::instance()->handleEvents() ) - { - break; - } - } + if (!transfers_active) + return; + + // Cancel & wait for any pending transfers to finish + for (int index = 0; index < NUM_TRANSFERS; ++index) + { + libusb_cancel_transfer(xfr[index]); + active_transfer_sema.Acquire(); + } + + stop_transfer_thread(); + + free(transfer_buffer); + transfer_buffer = NULL; + + delete frame_queue; + frame_queue = NULL; + + active_transfer_sema.Destroy(); + transfers_active = false; + } + + void start_transfer_thread() + { + update_thread.Run(&transfer_thread_func, this); + } + + void stop_transfer_thread() + { + exit_signaled = true; + update_thread.Join(); + } + + static int transfer_thread_func(void* user_data) + { + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 50 * 1000; // ms + + URBDesc* desc = (URBDesc*)user_data; + while (!desc->exit_signaled) + { + libusb_handle_events_timeout_completed(desc->usb_context, &tv, NULL); + } + + return 0; } void frame_add(enum gspca_packet_type packet_type, const uint8_t *data, int len) { - int i; if (packet_type == FIRST_PACKET) { - frame_data_start = frame_buffer + frame_work_ind*frame_size; - frame_data_len = 0; + cur_frame_data_len = 0; } else { - switch(last_packet_type) + switch(last_packet_type) // ignore warning. { case DISCARD_PACKET: if (packet_type == LAST_PACKET) { last_packet_type = packet_type; - frame_data_len = 0; + cur_frame_data_len = 0; } return; case LAST_PACKET: @@ -553,24 +911,21 @@ class URBDesc /* append the packet to the frame buffer */ if (len > 0) { - if(frame_data_len + len > frame_size) + if(cur_frame_data_len + len > frame_size) { packet_type = DISCARD_PACKET; - frame_data_len = 0; + cur_frame_data_len = 0; } else { - memcpy(frame_data_start+frame_data_len, data, len); - frame_data_len += len; + memcpy(cur_frame_start+cur_frame_data_len, data, len); + cur_frame_data_len += len; } } last_packet_type = packet_type; if (packet_type == LAST_PACKET) { - last_frame_time = (double)getTickCount(); - frame_complete_ind = frame_work_ind; - i = (frame_work_ind + 1) & 15; - frame_work_ind = i; - frame_data_len = 0; + cur_frame_data_len = 0; + cur_frame_start = frame_queue->Enqueue(); //debug("frame completed %d\n", frame_complete_ind); } } @@ -625,7 +980,7 @@ class URBDesc else if (data[1] & UVC_STREAM_EOF) { last_pts = 0; - if(frame_data_len + len - 12 != frame_size) + if(cur_frame_data_len + len - 12 != frame_size) { goto discard; } @@ -648,24 +1003,25 @@ class URBDesc } while (remaining_len > 0); } - uint8_t num_transfers; - enum gspca_packet_type last_packet_type; - uint32_t last_pts; - uint16_t last_fid; - libusb_transfer *xfr[2]; - - uint8_t *frame_buffer; - uint8_t *frame_buffer_end; - uint8_t *frame_data_start; - uint32_t frame_data_len; - uint32_t frame_size; - uint8_t frame_complete_ind; - uint8_t frame_work_ind; - - double last_frame_time; + libusb_context* usb_context; + Thread update_thread; + bool exit_signaled; + + Semaphore active_transfer_sema; + bool transfers_active; + enum gspca_packet_type last_packet_type; + uint32_t last_pts; + uint16_t last_fid; + libusb_transfer* xfr[NUM_TRANSFERS]; + + uint8_t* transfer_buffer; + uint8_t* cur_frame_start; + uint32_t cur_frame_data_len; + uint32_t frame_size; + FrameQueue* frame_queue; }; -static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr) +static void LIBUSB_CALL transfer_completed_callback(struct libusb_transfer *xfr) { URBDesc *urb = reinterpret_cast(xfr->user_data); enum libusb_transfer_status status = xfr->status; @@ -675,7 +1031,7 @@ static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr) debug("transfer status %d\n", status); libusb_free_transfer(xfr); - urb->num_transfers--; + urb->active_transfer_sema.Release(); if(status != LIBUSB_TRANSFER_CANCELLED) { @@ -696,28 +1052,17 @@ static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr) // PS3EYECam -bool PS3EYECam::devicesEnumerated = false; -std::vector PS3EYECam::devices; - -const std::vector& PS3EYECam::getDevices( bool forceRefresh ) +uint32_t PS3EYECam::getDeviceCount(bool forceRefresh) { - if( devicesEnumerated && ( ! forceRefresh ) ) - return devices; - - devices.clear(); - - USBMgr::instance()->sTotalDevices = USBMgr::instance()->listDevices(devices); - - devicesEnumerated = true; - return devices; + return USBMgr::instance()->getDeviceCount(forceRefresh); } -bool PS3EYECam::updateDevices() +PS3EYECam::PS3EYERef PS3EYECam::createDevice(uint32_t index) { - return USBMgr::instance()->handleEvents(); + return USBMgr::instance()->createDevice(index); } -PS3EYECam::PS3EYECam(libusb_device *device) +PS3EYECam::PS3EYECam(libusb_context* context, libusb_device *device) { // default controls autogain = false; @@ -739,15 +1084,18 @@ PS3EYECam::PS3EYECam(libusb_device *device) is_streaming = false; + device_context = context; device_ = device; mgrPtr = USBMgr::instance(); - urb = std::shared_ptr( new URBDesc() ); + urb = std::shared_ptr( new URBDesc(device_context) ); } PS3EYECam::~PS3EYECam() { stop(); release(); + libusb_exit(device_context); + device_context = NULL; } void PS3EYECam::release() @@ -757,7 +1105,7 @@ void PS3EYECam::release() if(usb_buf) free(usb_buf); } -bool PS3EYECam::init(uint32_t width, uint32_t height, uint8_t desiredFrameRate) +bool PS3EYECam::init(uint32_t width, uint32_t height, uint8_t desiredFrameRate, uint32_t frame_buffer_count) { uint16_t sensor_id; @@ -785,6 +1133,8 @@ bool PS3EYECam::init(uint32_t width, uint32_t height, uint8_t desiredFrameRate) } frame_rate = ov534_set_frame_rate(desiredFrameRate, true); frame_stride = frame_width * 2; + frame_queue_size = frame_buffer_count; + // /* reset bridge */ @@ -802,7 +1152,7 @@ bool PS3EYECam::init(uint32_t width, uint32_t height, uint8_t desiredFrameRate) /* reset sensor */ sccb_reg_write(0x12, 0x80); -#ifdef _MSC_VER +#ifdef _MSC_VER Sleep(10); #else nanosleep((const struct timespec[]){{0, 10000000}}, NULL); @@ -856,8 +1206,7 @@ void PS3EYECam::start() ov534_reg_write(0xe0, 0x00); // start stream // init and start urb - urb->start_transfers(handle_, frame_stride*frame_height); - last_qued_frame_time = 0; + urb->start_transfers(handle_, frame_stride*frame_height, frame_queue_size); is_streaming = true; } @@ -875,20 +1224,9 @@ void PS3EYECam::stop() is_streaming = false; } -bool PS3EYECam::isNewFrame() const +uint8_t* PS3EYECam::getFrame() { - if(last_qued_frame_time < urb->last_frame_time) - { - return true; - } - return false; -} - -const uint8_t* PS3EYECam::getLastFramePointer() -{ - last_qued_frame_time = urb->last_frame_time; - const uint8_t* frame = const_cast(urb->frame_buffer + urb->frame_complete_ind * urb->frame_size); - return frame; + return urb->frame_queue->Dequeue(); } bool PS3EYECam::open_usb() @@ -993,11 +1331,11 @@ uint8_t PS3EYECam::ov534_set_frame_rate(uint8_t frame_rate, bool dry_run) break; r++; } - + if (!dry_run) { - sccb_reg_write(0x11, r->r11); - sccb_reg_write(0x0d, r->r0d); - ov534_reg_write(0xe5, r->re5); + sccb_reg_write(0x11, r->r11); + sccb_reg_write(0x0d, r->r0d); + ov534_reg_write(0xe5, r->re5); } debug("frame_rate: %d\n", r->fps); @@ -1070,7 +1408,7 @@ void PS3EYECam::sccb_reg_write(uint8_t reg, uint8_t val) if (!sccb_check_status()) { debug("sccb_reg_write failed\n"); - } +} } @@ -1081,12 +1419,12 @@ uint8_t PS3EYECam::sccb_reg_read(uint16_t reg) if (!sccb_check_status()) { debug("sccb_reg_read failed 1\n"); } - + ov534_reg_write(OV534_REG_OPERATION, OV534_OP_READ_2); if (!sccb_check_status()) { - debug("sccb_reg_read failed 2\n"); + debug( "sccb_reg_read failed 2\n"); } - + return ov534_reg_read(OV534_REG_READ); } /* output a bridge sequence (reg - val) */ diff --git a/src/ps3eye.h b/src/ps3eye.h old mode 100755 new mode 100644 index e4e4aba..e0e0b93 --- a/src/ps3eye.h +++ b/src/ps3eye.h @@ -6,21 +6,8 @@ #include #include -// define shared_ptr in std +#include -#if (defined( _MSC_VER ) && ( _MSC_VER >= 1600 )) || (__cplusplus >= 201103L) - #include -#else - #include - namespace std { - using std::tr1::shared_ptr; - using std::tr1::weak_ptr; - using std::tr1::static_pointer_cast; - using std::tr1::dynamic_pointer_cast; - using std::tr1::const_pointer_cast; - using std::tr1::enable_shared_from_this; - } -#endif #include "libusb.h" @@ -47,10 +34,10 @@ class PS3EYECam static const uint16_t VENDOR_ID; static const uint16_t PRODUCT_ID; - PS3EYECam(libusb_device *device); + PS3EYECam(libusb_context* context, libusb_device *device); ~PS3EYECam(); - bool init(uint32_t width = 0, uint32_t height = 0, uint8_t desiredFrameRate = 30); + bool init(uint32_t width = 0, uint32_t height = 0, uint8_t desiredFrameRate = 30, uint32_t frame_buffer_count = 2); void start(); void stop(); @@ -143,6 +130,8 @@ class PS3EYECam greenblc = val; sccb_reg_write(0x44, val); } + bool getFlipH() const { return flip_h; } + bool getFlipV() const { return flip_v; } void setFlip(bool horizontal = false, bool vertical = false) { flip_h = horizontal; flip_v = vertical; @@ -155,17 +144,19 @@ class PS3EYECam bool isStreaming() const { return is_streaming; } - bool isNewFrame() const; - const uint8_t* getLastFramePointer(); + + // Get a frame from the camera. Notes: + // - If there is no frame available, this function will block until one is + // - The returned frame is a malloc'd copy; you must free() it yourself when done with it + uint8_t* getFrame(); uint32_t getWidth() const { return frame_width; } uint32_t getHeight() const { return frame_height; } uint8_t getFrameRate() const { return frame_rate; } uint32_t getRowBytes() const { return frame_stride; } - // - static const std::vector& getDevices( bool forceRefresh = false ); - static bool updateDevices(); + static uint32_t getDeviceCount(bool forceRefresh = false); + static PS3EYERef createDevice(uint32_t index); private: PS3EYECam(const PS3EYECam&); @@ -203,17 +194,14 @@ class PS3EYECam std::shared_ptr mgrPtr; - static bool devicesEnumerated; - static std::vector devices; - uint32_t frame_width; uint32_t frame_height; uint32_t frame_stride; uint8_t frame_rate; - - double last_qued_frame_time; + uint32_t frame_queue_size; //usb stuff + libusb_context *device_context; libusb_device *device_; libusb_device_handle *handle_; uint8_t *usb_buf; diff --git a/src/ps3eyedriver.cpp b/src/ps3eyedriver.cpp new file mode 100644 index 0000000..8307b3c --- /dev/null +++ b/src/ps3eyedriver.cpp @@ -0,0 +1,281 @@ +/** + * PS3EYEDriver C API Interface for use with PS Move API + * Copyright (c) 2014 Thomas Perl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + **/ + + +#include "ps3eyedriver.h" + +#include "ps3eye.h" + +#include + +struct yuv422_buffer_t { + yuv422_buffer_t() : + pixels (NULL), + size (0), + stride (0), + width (0), + height (0) + { + } + + void update(unsigned char *pixels, int stride, int width, int height) + { + if (this->pixels != NULL) + { + free(this->pixels); + this->pixels = NULL; + } + + size_t size = stride * height; + this->size = size; + + this->pixels = pixels; + this->stride = stride; + this->width = width; + this->height = height; + } + + unsigned char *pixels; + size_t size; + + int stride; + int width; + int height; +}; + +struct ps3eye_context_t { + ps3eye_context_t() + { + } + + ~ps3eye_context_t() + { + } + + // Global context + std::list opened_devices; +}; + +static ps3eye_context_t * +ps3eye_context = NULL; + +struct ps3eye_t { + ps3eye_t(ps3eye::PS3EYECam::PS3EYERef eye, int width, int height, int fps) + : eye(eye) + , width(width) + , height(height) + , fps(fps) + { + eye->init(width, height, (uint8_t)fps); + eye->start(); + ps3eye_context->opened_devices.push_back(this); + } + + ~ps3eye_t() + { + eye->stop(); + ps3eye_context->opened_devices.remove(this); + } + + // Per-device context + ps3eye::PS3EYECam::PS3EYERef eye; + int width; + int height; + int fps; + yuv422_buffer_t frame_buffer; +}; + +void +ps3eye_init() +{ + if (!ps3eye_context) { + ps3eye_context = new ps3eye_context_t(); + } +} + +void +ps3eye_uninit() +{ + if (ps3eye_context) { + if (ps3eye_context->opened_devices.size() == 0) { + delete ps3eye_context; + ps3eye_context = NULL; + } else { + // ERROR: Cannot uninit, there are still handles open + } + } +} + +int +ps3eye_count_connected() +{ + if (!ps3eye_context) { + // Not init'ed + return 0; + } + + return (int)ps3eye::PS3EYECam::getDeviceCount(); +} + +ps3eye_t * +ps3eye_open(int id, int width, int height, int fps) +{ + if (!ps3eye_context) { + // Library not initialized + return NULL; + } + + if (id < 0 || id >= ps3eye_count_connected()) { + // No such device + return NULL; + } + + return new ps3eye_t(ps3eye::PS3EYECam::createDevice(id), width, height, fps); +} + +unsigned char * +ps3eye_grab_frame(ps3eye_t *eye, int *stride) +{ + if (!ps3eye_context) { + // No context available + return NULL; + } + + if (!eye) { + // Eye is not a valid handle + return NULL; + } + + eye->frame_buffer.update(eye->eye->getFrame(), + eye->eye->getRowBytes(), eye->eye->getWidth(), + eye->eye->getHeight()); + + if (stride) { + *stride = eye->frame_buffer.stride; + } + + return eye->frame_buffer.pixels; +} + +void +ps3eye_close(ps3eye_t *eye) +{ + delete eye; +} + +int +ps3eye_get_parameter(ps3eye_t *eye, ps3eye_parameter param) +{ + if (!eye) { + return -1; + } + switch (param) { + case PS3EYE_AUTO_GAIN: + return eye->eye->getAutogain(); + case PS3EYE_GAIN: + return eye->eye->getGain(); + case PS3EYE_AUTO_WHITEBALANCE: + return eye->eye->getAutoWhiteBalance(); + case PS3EYE_EXPOSURE: + return eye->eye->getExposure(); + case PS3EYE_SHARPNESS: + return eye->eye->getSharpness(); + case PS3EYE_CONTRAST: + return eye->eye->getContrast(); + case PS3EYE_BRIGHTNESS: + return eye->eye->getBrightness(); + case PS3EYE_HUE: + return eye->eye->getHue(); + case PS3EYE_REDBALANCE: + return eye->eye->getRedBalance(); + case PS3EYE_BLUEBALANCE: + return eye->eye->getBlueBalance(); + case PS3EYE_GREENBALANCE: + return eye->eye->getGreenBalance(); + case PS3EYE_HFLIP: + return eye->eye->getFlipH(); + case PS3EYE_VFLIP: + return eye->eye->getFlipV(); + default: + return -1; + } +} + +int +ps3eye_set_parameter(ps3eye_t *eye, ps3eye_parameter param, int value) +{ + if (!eye) { + return -1; + } + + switch (param) { + case PS3EYE_AUTO_GAIN: + eye->eye->setAutogain(value > 0); + break; + case PS3EYE_GAIN: + eye->eye->setGain((uint8_t)value); + break; + case PS3EYE_AUTO_WHITEBALANCE: + eye->eye->setAutoWhiteBalance(value > 0); + break; + case PS3EYE_EXPOSURE: + eye->eye->setExposure((uint8_t)value); + break; + case PS3EYE_SHARPNESS: + eye->eye->setSharpness((uint8_t)value); + break; + case PS3EYE_CONTRAST: + eye->eye->setContrast((uint8_t)value); + break; + case PS3EYE_BRIGHTNESS: + eye->eye->setBrightness((uint8_t)value); + break; + case PS3EYE_HUE: + eye->eye->setHue((uint8_t)value); + break; + case PS3EYE_REDBALANCE: + eye->eye->setRedBalance((uint8_t)value); + break; + case PS3EYE_BLUEBALANCE: + eye->eye->setBlueBalance((uint8_t)value); + break; + case PS3EYE_GREENBALANCE: + eye->eye->setGreenBalance((uint8_t)value); + break; + case PS3EYE_HFLIP: + eye->eye->setFlip(value > 0, eye->eye->getFlipV()); + break; + case PS3EYE_VFLIP: + eye->eye->setFlip(eye->eye->getFlipH(), value > 0); + break; + default: + break; + } + + return 0; +} \ No newline at end of file diff --git a/src/ps3eyedriver.h b/src/ps3eyedriver.h new file mode 100644 index 0000000..19c612b --- /dev/null +++ b/src/ps3eyedriver.h @@ -0,0 +1,119 @@ +/** + * PS3EYEDriver C API Interface for use with PS Move API + * Copyright (c) 2014 Thomas Perl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + **/ + +#ifndef PS3EYEDRIVER_H +#define PS3EYEDRIVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ps3eye_t ps3eye_t; + +typedef enum{ + PS3EYE_AUTO_GAIN, // [false, true] + PS3EYE_GAIN, // [0, 63] + PS3EYE_AUTO_WHITEBALANCE, // [false, true] + PS3EYE_EXPOSURE, // [0, 255] + PS3EYE_SHARPNESS, // [0 63] + PS3EYE_CONTRAST, // [0, 255] + PS3EYE_BRIGHTNESS, // [0, 255] + PS3EYE_HUE, // [0, 255] + PS3EYE_REDBALANCE, // [0, 255] + PS3EYE_BLUEBALANCE, // [0, 255] + PS3EYE_GREENBALANCE, // [0, 255] + PS3EYE_HFLIP, // [false, true] + PS3EYE_VFLIP // [false, true] +} ps3eye_parameter; + +/** + * Initialize and enumerate connected cameras. + * Needs to be called once before all other API functions. + **/ +void +ps3eye_init(); + +/** + * De-initialize the library and free resources. + * If a pseye_t * object is still opened, nothing happens. + **/ +void +ps3eye_uninit(); + +/** + * Return the number of PSEye cameras connected via USB. + **/ +int +ps3eye_count_connected(); + +/** + * Open a PSEye camera device by id. + * The id is zero-based, and must be smaller than the count. + * width and height should usually be 640x480 or 320x240 + * fps is the target frame rate, 60 usually works fine here + **/ +ps3eye_t * +ps3eye_open(int id, int width, int height, int fps); + +/** + * Grab the next frame as YUV422 blob. + * A pointer to the buffer will be passed back. The buffer + * will only be valid until the next call, or until the eye + * is closed again with ps3eye_close(). If stride is not NULL, + * the byte offset between two consecutive lines in the frame + * will be written to *stride. + **/ +unsigned char * +ps3eye_grab_frame(ps3eye_t *eye, int *stride); + +/** + * Close a PSEye camera device and free allocated resources. + * To really close the library, you should also call ps3eye_uninit(). + **/ +void +ps3eye_close(ps3eye_t *eye); + +/** + * Set a ps3eye_parameter to a value. + * Returns -1 if there is an error, otherwise 0. + **/ +int +ps3eye_set_parameter(ps3eye_t *eye, ps3eye_parameter param, int value); + +/** +* Get a ps3eye_parameter value. +* Returns -1 if there is an error, otherwise returns the parameter value int. +**/ +int +ps3eye_get_parameter(ps3eye_t *eye, ps3eye_parameter param); + +#ifdef __cplusplus +}; +#endif + +#endif /* PS3EYEDRIVER_H */