Miscellaneous

Overview

// enums

enum libusb_capability;
enum libusb_error;
enum libusb_request_recipient;
enum libusb_request_type;
enum libusb_standard_request;

// global variables

static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru" };

// global functions

int
libusb_has_capability(uint32_t capability);

const char*
libusb_error_name(int error_code);

const struct libusb_version*
libusb_get_version(void);

static
uint16_t
libusb_cpu_to_le16(const uint16_t x);

int
libusb_setlocale(const char* locale);

const char*
libusb_strerror(enum libusb_error errcode);

// macros

#define LIBUSB_API_VERSION
#define LIBUSB_CALL
#define libusb_le16_to_cpu

Detailed Documentation

Global Variables

static const char* usbi_locale_supported[] = { "en", "nl", "fr", "ru" }

How to add a new libusb_strerror() translation:

  1. Download the latest strerror.c from:

    https://raw.github.com/libusb/libusb/master/libusb/sterror.c

  2. Open the file in an UTF-8 capable editor

  3. Add the 2 letter ISO 639-1 code for your locale at the end of usbi_locale_supported []

    Eg. for Chinese, you would add “zh” so that:

    ... usbi_locale_supported[] = { "en", "nl", "fr" };

    becomes:

    ... usbi_locale_supported[] = { "en", "nl", "fr", "zh" };
  4. Copy the { / * English (en) * / ... } section and add it at the end of usbi_localized_errors

    Eg. for Chinese, the last section of usbi_localized_errors could look like:

        }, { / * Chinese (zh) * /
            "Success",
            ...
            "Other error",
        }
    };
  5. Translate each of the English messages from the section you copied into your language

  6. Save the file (in UTF-8 format) and send it to libusb-devel@lists.sourceforge.net

Global Functions

int
libusb_has_capability(uint32_t capability)

Check at runtime if the loaded library has a given capability.

This call should be performed after libusb_init(), to ensure the backend has updated its capability set.

Parameters:

capability

the libusb_capability to check for

Returns:

nonzero if the running library has the capability, 0 otherwise

const char*
libusb_error_name(int error_code)

Returns a constant NULL-terminated string with the ASCII name of a libusb error or transfer status code.

The caller must not free() the returned string.

Parameters:

error_code

The libusb_error or libusb_transfer_status code to return the name of.

Returns:

The error name, or the string UNKNOWN if the value of error_code is not a known error / status code.

const struct libusb_version*
libusb_get_version(void)

Returns a pointer to const struct libusb_version with the version (major, minor, micro, nano and rc) of the running library.

static
uint16_t
libusb_cpu_to_le16(const uint16_t x)

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.

Parameters:

x

the host-endian value to convert

Returns:

the value in little-endian byte order

int
libusb_setlocale(const char* locale)

Set the language, and only the language, not the encoding! used for translatable libusb messages.

This takes a locale string in the default setlocale format: lang[-region] or lang[_country_region][.codeset]. Only the lang part of the string is used, and only 2 letter ISO 639-1 codes are accepted for it, such as “de”. The optional region, country_region or codeset parts are ignored. This means that functions which return translatable strings will NOT honor the specified encoding. All strings returned are encoded as UTF-8 strings.

If libusb_setlocale() is not called, all messages will be in English.

The following functions return translatable strings: libusb_strerror(). Note that the libusb log messages controlled through libusb_set_debug() are not translated, they are always in English.

For POSIX UTF-8 environments if you want libusb to follow the standard locale settings, call libusb_setlocale(setlocale(LC_MESSAGES, NULL)), after your app has done its locale setup.

Parameters:

locale

locale-string in the form of lang[_country_region][.codeset] or lang[-region], where lang is a 2 letter ISO 639-1 code

Returns:

LIBUSB_SUCCESS on success

LIBUSB_ERROR_INVALID_PARAM if the locale doesn’t meet the requirements

LIBUSB_ERROR_NOT_FOUND if the requested language is not supported

a LIBUSB_ERROR code on other errors

const char*
libusb_strerror(enum libusb_error errcode)

Returns a constant string with a short description of the given error code, this description is intended for displaying to the end user and will be in the language set by libusb_setlocale().

The returned string is encoded in UTF-8.

The messages always start with a capital letter and end without any dot. The caller must not free() the returned string.

Parameters:

errcode

the error code whose description is desired

Returns:

a short description of the error code in UTF-8 encoding

Macros

#define LIBUSB_API_VERSION

libusb’s API version.

Since version 1.0.13, to help with feature detection, libusb defines a LIBUSB_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 libusb applications may want to detect at compilation time.

The macro is typically used in an application as follows:

#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234)
// Use one of the newer features from the libusb API
#endif

Internally, LIBUSB_API_VERSION is defined as follows: (libusb major << 24) | (libusb minor << 16) | (16 bit incremental)

#define LIBUSB_CALL

libusb’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 functions in the generated assembly code).

Matching the Windows API itself, libusb 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 libusb 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.

#define libusb_le16_to_cpu

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.

Parameters:

x

the little-endian value to convert

Returns:

the value in host-endian byte order