Appl new code style on whole code base

Use a similar code style to bemanitools though reduce indentation
to 2 spaces. Also some other tweaks to be more similar to google
style.
This commit is contained in:
icex2
2021-03-19 15:52:34 +01:00
parent b493b4983a
commit eafb2909d1
322 changed files with 30291 additions and 25084 deletions
+8 -8
View File
@@ -1,16 +1,16 @@
#ifndef PIUBTN_DRV_DEFS_H
#define PIUBTN_DRV_DEFS_H
#define PIUBTN_DRV_VID 0x0D2F
#define PIUBTN_DRV_PID 0x1010
#define PIUBTN_DRV_VID 0x0D2F
#define PIUBTN_DRV_PID 0x1010
#define PIUBTN_DRV_CONFIG 0x01
#define PIUBTN_DRV_IFACE 0x00
#define PIUBTN_DRV_CONFIG 0x01
#define PIUBTN_DRV_IFACE 0x00
#define PIUBTN_DRV_USB_CTRL_TYPE_IN 0xC0
#define PIUBTN_DRV_USB_CTRL_TYPE_OUT 0x40
#define PIUBTN_DRV_USB_CTRL_REQUEST 0xAE
#define PIUBTN_DRV_USB_REQ_TIMEOUT 10000
#define PIUBTN_DRV_USB_CTRL_TYPE_IN 0xC0
#define PIUBTN_DRV_USB_CTRL_TYPE_OUT 0x40
#define PIUBTN_DRV_USB_CTRL_REQUEST 0xAE
#define PIUBTN_DRV_USB_REQ_TIMEOUT 10000
#define PIUBTN_DRV_BUFFER_SIZE 8
+60 -48
View File
@@ -7,78 +7,90 @@
#include "defs.h"
#include "device.h"
static void* piubtn_drv_device_handle;
static void *piubtn_drv_device_handle;
bool piubtn_drv_device_open(void)
{
if (piubtn_drv_device_handle) {
log_warn("Device already opened");
return true;
}
if (piubtn_drv_device_handle) {
log_warn("Device already opened");
return true;
}
piubtn_drv_device_handle = io_usb_open(PIUBTN_DRV_VID, PIUBTN_DRV_PID,
PIUBTN_DRV_CONFIG, PIUBTN_DRV_IFACE);
piubtn_drv_device_handle = io_usb_open(
PIUBTN_DRV_VID, PIUBTN_DRV_PID, PIUBTN_DRV_CONFIG, PIUBTN_DRV_IFACE);
return piubtn_drv_device_handle;
return piubtn_drv_device_handle;
}
bool piubtn_drv_device_read(uint8_t* buffer, uint8_t len)
bool piubtn_drv_device_read(uint8_t *buffer, uint8_t len)
{
int32_t res;
int32_t res;
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed, buffer (%d) too small", len);
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_IN, PIUBTN_DRV_USB_CTRL_REQUEST, 0, 0, buffer,
PIUBTN_DRV_BUFFER_SIZE, PIUBTN_DRV_USB_REQ_TIMEOUT);
res = io_usb_control_transfer(
piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_IN,
PIUBTN_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUBTN_DRV_BUFFER_SIZE,
PIUBTN_DRV_USB_REQ_TIMEOUT);
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed: %d", res);
return false;
}
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed: %d", res);
return false;
}
return true;
return true;
}
bool piubtn_drv_device_write(uint8_t* buffer, uint8_t len)
bool piubtn_drv_device_write(uint8_t *buffer, uint8_t len)
{
int32_t res;
int32_t res;
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed, buffer (%d) too small", len);
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_OUT, PIUBTN_DRV_USB_CTRL_REQUEST, 0, 0, buffer,
PIUBTN_DRV_BUFFER_SIZE, PIUBTN_DRV_USB_REQ_TIMEOUT);
res = io_usb_control_transfer(
piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_OUT,
PIUBTN_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUBTN_DRV_BUFFER_SIZE,
PIUBTN_DRV_USB_REQ_TIMEOUT);
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed: %d", res);
return false;
}
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed: %d", res);
return false;
}
return true;
return true;
}
void piubtn_drv_device_close(void)
{
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
}
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
}
io_usb_close(piubtn_drv_device_handle);
io_usb_close(piubtn_drv_device_handle);
}
+2 -2
View File
@@ -22,7 +22,7 @@ bool piubtn_drv_device_open(void);
* @param len Number of bytes to read
* @return True if reading data to device was successful, false otherwise
*/
bool piubtn_drv_device_read(uint8_t* buffer, uint8_t len);
bool piubtn_drv_device_read(uint8_t *buffer, uint8_t len);
/**
* Write raw data to the piubtn device
@@ -31,7 +31,7 @@ bool piubtn_drv_device_read(uint8_t* buffer, uint8_t len);
* @param len Number of bytes to write
* @return True if writing data to device was successful, false otherwise
*/
bool piubtn_drv_device_write(uint8_t* buffer, uint8_t len);
bool piubtn_drv_device_write(uint8_t *buffer, uint8_t len);
/**
* Close the opened piubtn device
+8 -8
View File
@@ -1,16 +1,16 @@
#ifndef PIUIO_DRV_DEFS_H
#define PIUIO_DRV_DEFS_H
#define PIUIO_DRV_VID 0x0547
#define PIUIO_DRV_PID 0x1002
#define PIUIO_DRV_VID 0x0547
#define PIUIO_DRV_PID 0x1002
#define PIUIO_DRV_CONFIG 0x01
#define PIUIO_DRV_IFACE 0x00
#define PIUIO_DRV_CONFIG 0x01
#define PIUIO_DRV_IFACE 0x00
#define PIUIO_DRV_USB_CTRL_TYPE_IN 0xC0
#define PIUIO_DRV_USB_CTRL_TYPE_OUT 0x40
#define PIUIO_DRV_USB_CTRL_REQUEST 0xAE
#define PIUIO_DRV_USB_REQ_TIMEOUT 10000
#define PIUIO_DRV_USB_CTRL_TYPE_IN 0xC0
#define PIUIO_DRV_USB_CTRL_TYPE_OUT 0x40
#define PIUIO_DRV_USB_CTRL_REQUEST 0xAE
#define PIUIO_DRV_USB_REQ_TIMEOUT 10000
#define PIUIO_DRV_BUFFER_SIZE 8
+60 -48
View File
@@ -7,78 +7,90 @@
#include "defs.h"
#include "device.h"
static void* piuio_drv_device_handle;
static void *piuio_drv_device_handle;
bool piuio_drv_device_open(void)
{
if (piuio_drv_device_handle) {
log_warn("Device already opened");
return true;
}
if (piuio_drv_device_handle) {
log_warn("Device already opened");
return true;
}
piuio_drv_device_handle = io_usb_open(PIUIO_DRV_VID, PIUIO_DRV_PID,
PIUIO_DRV_CONFIG, PIUIO_DRV_IFACE);
piuio_drv_device_handle = io_usb_open(
PIUIO_DRV_VID, PIUIO_DRV_PID, PIUIO_DRV_CONFIG, PIUIO_DRV_IFACE);
return piuio_drv_device_handle;
return piuio_drv_device_handle;
}
bool piuio_drv_device_read(uint8_t* buffer, uint8_t len)
bool piuio_drv_device_read(uint8_t *buffer, uint8_t len)
{
int32_t res;
int32_t res;
if (!piuio_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (!piuio_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUIO_DRV_BUFFER_SIZE) {
log_error("Read failed, buffer (%d) too small", len);
return false;
}
if (len < PIUIO_DRV_BUFFER_SIZE) {
log_error("Read failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(piuio_drv_device_handle,
PIUIO_DRV_USB_CTRL_TYPE_IN, PIUIO_DRV_USB_CTRL_REQUEST, 0, 0, buffer,
PIUIO_DRV_BUFFER_SIZE, PIUIO_DRV_USB_REQ_TIMEOUT);
res = io_usb_control_transfer(
piuio_drv_device_handle,
PIUIO_DRV_USB_CTRL_TYPE_IN,
PIUIO_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUIO_DRV_BUFFER_SIZE,
PIUIO_DRV_USB_REQ_TIMEOUT);
if (res != PIUIO_DRV_BUFFER_SIZE) {
log_error("Read failed: %d", res);
return false;
}
if (res != PIUIO_DRV_BUFFER_SIZE) {
log_error("Read failed: %d", res);
return false;
}
return true;
return true;
}
bool piuio_drv_device_write(uint8_t* buffer, uint8_t len)
bool piuio_drv_device_write(uint8_t *buffer, uint8_t len)
{
int32_t res;
int32_t res;
if (!piuio_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (!piuio_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUIO_DRV_BUFFER_SIZE) {
log_error("Write failed, buffer (%d) too small", len);
return false;
}
if (len < PIUIO_DRV_BUFFER_SIZE) {
log_error("Write failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(piuio_drv_device_handle,
PIUIO_DRV_USB_CTRL_TYPE_OUT, PIUIO_DRV_USB_CTRL_REQUEST, 0, 0, buffer,
PIUIO_DRV_BUFFER_SIZE, PIUIO_DRV_USB_REQ_TIMEOUT);
res = io_usb_control_transfer(
piuio_drv_device_handle,
PIUIO_DRV_USB_CTRL_TYPE_OUT,
PIUIO_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUIO_DRV_BUFFER_SIZE,
PIUIO_DRV_USB_REQ_TIMEOUT);
if (res != PIUIO_DRV_BUFFER_SIZE) {
log_error("Write failed: %d", res);
return false;
}
if (res != PIUIO_DRV_BUFFER_SIZE) {
log_error("Write failed: %d", res);
return false;
}
return true;
return true;
}
void piuio_drv_device_close(void)
{
if (!piuio_drv_device_handle) {
log_error("Device not opened");
}
if (!piuio_drv_device_handle) {
log_error("Device not opened");
}
io_usb_close(piuio_drv_device_handle);
io_usb_close(piuio_drv_device_handle);
}
+2 -2
View File
@@ -22,7 +22,7 @@ bool piuio_drv_device_open(void);
* @param len Number of bytes to read
* @return True if reading data to device was successful, false otherwise
*/
bool piuio_drv_device_read(uint8_t* buffer, uint8_t len);
bool piuio_drv_device_read(uint8_t *buffer, uint8_t len);
/**
* Write raw data to the piuio device
@@ -31,7 +31,7 @@ bool piuio_drv_device_read(uint8_t* buffer, uint8_t len);
* @param len Number of bytes to write
* @return True if writing data to device was successful, false otherwise
*/
bool piuio_drv_device_write(uint8_t* buffer, uint8_t len);
bool piuio_drv_device_write(uint8_t *buffer, uint8_t len);
/**
* Close the opened piuio device
+29 -24
View File
@@ -11,41 +11,46 @@
#include "util/fs.h"
#include "util/log.h"
size_t io_util_joystick_util_scan(struct io_util_joystick_util_device_info* devices, size_t len)
size_t io_util_joystick_util_scan(
struct io_util_joystick_util_device_info *devices, size_t len)
{
log_assert(devices);
log_assert(devices);
char dev_path[PATH_MAX];
int handle;
size_t num_joysticks;
char dev_path[PATH_MAX];
int handle;
size_t num_joysticks;
num_joysticks = 0;
num_joysticks = 0;
for (size_t i = 0; i < len; i++) {
sprintf(dev_path, "/dev/input/js%d", i);
for (size_t i = 0; i < len; i++) {
sprintf(dev_path, "/dev/input/js%d", i);
memset(&devices[num_joysticks], 0, sizeof(struct io_util_joystick_util_device_info));
memset(
&devices[num_joysticks],
0,
sizeof(struct io_util_joystick_util_device_info));
if (util_fs_path_exists(dev_path)) {
// temporarily have to open the device
handle = open(dev_path, O_RDONLY | O_NONBLOCK);
if (util_fs_path_exists(dev_path)) {
// temporarily have to open the device
handle = open(dev_path, O_RDONLY | O_NONBLOCK);
if (handle == -1) {
log_error("Opening joystick device %s failed: %s", dev_path, strerror(errno));
continue;
}
if (handle == -1) {
log_error(
"Opening joystick device %s failed: %s", dev_path, strerror(errno));
continue;
}
strcpy(devices[num_joysticks].dev_path, dev_path);
strcpy(devices[num_joysticks].dev_path, dev_path);
ioctl(handle, JSIOCGAXES, &(devices[num_joysticks].num_axes));
ioctl(handle, JSIOCGBUTTONS, &(devices[num_joysticks].num_buttons));
ioctl(handle, JSIOCGNAME(JOYSTICK_NAME_LEN), devices[num_joysticks].name);
ioctl(handle, JSIOCGAXES, &(devices[num_joysticks].num_axes));
ioctl(handle, JSIOCGBUTTONS, &(devices[num_joysticks].num_buttons));
ioctl(handle, JSIOCGNAME(JOYSTICK_NAME_LEN), devices[num_joysticks].name);
close(handle);
close(handle);
num_joysticks++;
}
num_joysticks++;
}
}
return num_joysticks;
return num_joysticks;
}
+7 -6
View File
@@ -1,20 +1,21 @@
#ifndef IO_UTIL_JOYSTICK_UTIL_H
#define IO_UTIL_JOYSTICK_UTIL_H
#include <linux/limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <linux/limits.h>
#define JOYSTICK_NAME_LEN 128
struct io_util_joystick_util_device_info {
char dev_path[PATH_MAX];
uint16_t num_buttons;
uint16_t num_axes;
char name[JOYSTICK_NAME_LEN];
char dev_path[PATH_MAX];
uint16_t num_buttons;
uint16_t num_axes;
char name[JOYSTICK_NAME_LEN];
};
size_t io_util_joystick_util_scan(struct io_util_joystick_util_device_info* devices, size_t len);
size_t io_util_joystick_util_scan(
struct io_util_joystick_util_device_info *devices, size_t len);
#endif
+104 -90
View File
@@ -13,109 +13,123 @@
#define MAX_NUM_EVENTS 64
bool io_util_joystick_open(const char* dev_path, struct io_util_joystick** ctx)
bool io_util_joystick_open(const char *dev_path, struct io_util_joystick **ctx)
{
log_assert(dev_path);
log_assert(ctx);
log_assert(dev_path);
log_assert(ctx);
int handle;
int handle;
handle = open(dev_path, O_RDONLY | O_NONBLOCK);
handle = open(dev_path, O_RDONLY | O_NONBLOCK);
if (handle == -1) {
log_error("Opening joystick device %s failed: %s", dev_path, strerror(errno));
if (handle == -1) {
log_error(
"Opening joystick device %s failed: %s", dev_path, strerror(errno));
return false;
}
*ctx = util_xmalloc(sizeof(struct io_util_joystick));
strcpy((*ctx)->dev_path, dev_path);
(*ctx)->handle = handle;
// Init values to avoid having ioctl return garbage
(*ctx)->num_buttons = 0;
(*ctx)->num_axes = 0;
memset((*ctx)->name, 0, sizeof(char) * JOYSTICK_NAME_LEN);
// grab data
ioctl(handle, JSIOCGAXES, &((*ctx)->num_axes));
ioctl(handle, JSIOCGBUTTONS, &((*ctx)->num_buttons));
ioctl(handle, JSIOCGNAME(JOYSTICK_NAME_LEN), (*ctx)->name);
(*ctx)->button_state = util_xmalloc(sizeof(uint8_t) * (*ctx)->num_buttons);
log_debug(
"Opened joystick device %s, handle %p, num buttons %d, num axes %d, name "
"%s",
(*ctx)->dev_path,
(*ctx)->handle,
(*ctx)->num_buttons,
(*ctx)->num_axes,
(*ctx)->name);
return true;
}
bool io_util_joystick_close(struct io_util_joystick *ctx)
{
log_assert(ctx);
bool res;
log_debug(
"Closing joystick device %s, handle %p", ctx->dev_path, ctx->handle);
if (close(ctx->handle) != 0) {
log_error(
"Closing joystick device %s failed: %s",
ctx->dev_path,
strerror(errno));
res = false;
} else {
res = true;
}
free(ctx->button_state);
free(ctx);
return res;
}
bool io_util_joystick_update(struct io_util_joystick *ctx)
{
log_assert(ctx);
struct js_event jse[MAX_NUM_EVENTS];
int bytes = read(ctx->handle, jse, sizeof(struct js_event) * MAX_NUM_EVENTS);
if (bytes < 0) {
switch (errno) {
// try again on next poll
case EAGAIN:
return true;
default:
log_error(
"Reading from joystick device failed: %s",
ctx->dev_path,
strerror(errno));
return false;
}
}
*ctx = util_xmalloc(sizeof(struct io_util_joystick));
// determine how many whole events we read
bytes /= sizeof(struct js_event);
strcpy((*ctx)->dev_path, dev_path);
(*ctx)->handle = handle;
// now iterate all events
for (int i = 0; i < bytes; i++) {
switch (jse[i].type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
ctx->button_state[jse[i].number] = (bool) jse[i].value;
break;
// Init values to avoid having ioctl return garbage
(*ctx)->num_buttons = 0;
(*ctx)->num_axes = 0;
memset((*ctx)->name, 0, sizeof(char) * JOYSTICK_NAME_LEN);
default:
break;
}
}
// grab data
ioctl(handle, JSIOCGAXES, &((*ctx)->num_axes));
ioctl(handle, JSIOCGBUTTONS, &((*ctx)->num_buttons));
ioctl(handle, JSIOCGNAME(JOYSTICK_NAME_LEN), (*ctx)->name);
(*ctx)->button_state = util_xmalloc(sizeof(uint8_t) * (*ctx)->num_buttons);
log_debug("Opened joystick device %s, handle %p, num buttons %d, num axes %d, name %s", (*ctx)->dev_path,
(*ctx)->handle, (*ctx)->num_buttons, (*ctx)->num_axes, (*ctx)->name);
return true;
return true;
}
bool io_util_joystick_close(struct io_util_joystick* ctx)
bool io_util_joystick_is_button_pressed(
struct io_util_joystick *ctx, uint16_t button)
{
log_assert(ctx);
log_assert(ctx);
bool res;
if (button >= ctx->num_buttons) {
return false;
}
log_debug("Closing joystick device %s, handle %p", ctx->dev_path, ctx->handle);
if (close(ctx->handle) != 0) {
log_error("Closing joystick device %s failed: %s", ctx->dev_path, strerror(errno));
res = false;
} else {
res = true;
}
free(ctx->button_state);
free(ctx);
return res;
}
bool io_util_joystick_update(struct io_util_joystick* ctx)
{
log_assert(ctx);
struct js_event jse[MAX_NUM_EVENTS];
int bytes = read(ctx->handle, jse, sizeof(struct js_event) * MAX_NUM_EVENTS);
if (bytes < 0) {
switch (errno) {
// try again on next poll
case EAGAIN:
return true;
default:
log_error("Reading from joystick device failed: %s", ctx->dev_path, strerror(errno));
return false;
}
}
// determine how many whole events we read
bytes /= sizeof(struct js_event);
// now iterate all events
for (int i = 0; i < bytes; i++) {
switch (jse[i].type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
ctx->button_state[jse[i].number] = (bool) jse[i].value;
break;
default:
break;
}
}
return true;
}
bool io_util_joystick_is_button_pressed(struct io_util_joystick* ctx, uint16_t button)
{
log_assert(ctx);
if (button >= ctx->num_buttons) {
return false;
}
return ctx->button_state[button];
return ctx->button_state[button];
}
+12 -11
View File
@@ -1,27 +1,28 @@
#ifndef IO_UTIL_JOYSTICK_H
#define IO_UTIL_JOYSTICK_H
#include <linux/limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <linux/limits.h>
#define JOYSTICK_NAME_LEN 128
struct io_util_joystick {
char dev_path[PATH_MAX];
int handle;
uint16_t num_buttons;
uint16_t num_axes;
char name[JOYSTICK_NAME_LEN];
bool* button_state;
char dev_path[PATH_MAX];
int handle;
uint16_t num_buttons;
uint16_t num_axes;
char name[JOYSTICK_NAME_LEN];
bool *button_state;
};
bool io_util_joystick_open(const char* dev_path, struct io_util_joystick** ctx);
bool io_util_joystick_open(const char *dev_path, struct io_util_joystick **ctx);
bool io_util_joystick_close(struct io_util_joystick* ctx);
bool io_util_joystick_close(struct io_util_joystick *ctx);
bool io_util_joystick_update(struct io_util_joystick* ctx);
bool io_util_joystick_update(struct io_util_joystick *ctx);
bool io_util_joystick_is_button_pressed(struct io_util_joystick* ctx, uint16_t button);
bool io_util_joystick_is_button_pressed(
struct io_util_joystick *ctx, uint16_t button);
#endif
+133 -110
View File
@@ -7,165 +7,188 @@
#include "util/mem.h"
struct usb_io_ctx {
struct libusb_context* ctx;
struct libusb_device_handle* dev;
struct libusb_context *ctx;
struct libusb_device_handle *dev;
};
static struct libusb_device_handle* io_usb_get_device_handle(
struct libusb_context* ctx, uint16_t vid, uint16_t pid)
static struct libusb_device_handle *
io_usb_get_device_handle(struct libusb_context *ctx, uint16_t vid, uint16_t pid)
{
int ret;
struct libusb_device** dev_list;
struct libusb_device* device;
struct libusb_device_handle* handle;
int ret;
struct libusb_device **dev_list;
struct libusb_device *device;
struct libusb_device_handle *handle;
dev_list = NULL;
device = NULL;
dev_list = NULL;
device = NULL;
ssize_t num_dev = libusb_get_device_list(ctx, &dev_list);
ssize_t num_dev = libusb_get_device_list(ctx, &dev_list);
/* find device */
for (ssize_t i = 0; i < num_dev; i++) {
struct libusb_device_descriptor dev_desc_tmp;
struct libusb_device* dev_tmp;
/* find device */
for (ssize_t i = 0; i < num_dev; i++) {
struct libusb_device_descriptor dev_desc_tmp;
struct libusb_device *dev_tmp;
dev_tmp = dev_list[i];
dev_tmp = dev_list[i];
libusb_get_device_descriptor(dev_tmp, &dev_desc_tmp);
libusb_get_device_descriptor(dev_tmp, &dev_desc_tmp);
/* for handling multiple devices with the same pid and vid */
/*
/* for handling multiple devices with the same pid and vid */
/*
device_id_tmp = ((libusb_get_bus_number(dev_tmp) << 8) |
libusb_get_port_number(dev_tmp));
*/
if (dev_desc_tmp.idVendor == vid && dev_desc_tmp.idProduct == pid) {
device = dev_tmp;
break;
}
}
if (dev_desc_tmp.idVendor == vid && dev_desc_tmp.idProduct == pid) {
device = dev_tmp;
break;
}
}
/* not found */
if (device == NULL) {
libusb_free_device_list(dev_list, 1);
log_error("Could not find device %04X:%04X", vid, pid);
return NULL;
}
/* not found */
if (device == NULL) {
libusb_free_device_list(dev_list, 1);
log_error("Could not find device %04X:%04X", vid, pid);
return NULL;
}
ret = libusb_open(device, &handle);
if (ret != LIBUSB_SUCCESS) {
libusb_free_device_list(dev_list, 1);
ret = libusb_open(device, &handle);
if (ret != LIBUSB_SUCCESS) {
libusb_free_device_list(dev_list, 1);
log_error("Could not open device %04X:%04X: %s", vid, pid,
libusb_error_name(ret));
return NULL;
}
log_error(
"Could not open device %04X:%04X: %s",
vid,
pid,
libusb_error_name(ret));
return NULL;
}
libusb_free_device_list(dev_list, 1);
libusb_free_device_list(dev_list, 1);
return handle;
return handle;
}
void* io_usb_open(uint16_t vid, uint16_t pid, uint16_t config, uint16_t iface)
void *io_usb_open(uint16_t vid, uint16_t pid, uint16_t config, uint16_t iface)
{
int ret;
struct libusb_context* ctx;
struct libusb_device_handle* dev;
struct usb_io_ctx* handle;
int ret;
struct libusb_context *ctx;
struct libusb_device_handle *dev;
struct usb_io_ctx *handle;
log_debug("Opening %04X:%04X, config %d, iface %d", vid, pid, config,
iface);
log_debug("Opening %04X:%04X, config %d, iface %d", vid, pid, config, iface);
ret = libusb_init(&ctx);
ret = libusb_init(&ctx);
if (ret != LIBUSB_SUCCESS) {
log_error("(%04X:%04X) Initializing libusb context failed: %s",
vid, pid, libusb_error_name(ret));
return NULL;
}
if (ret != LIBUSB_SUCCESS) {
log_error(
"(%04X:%04X) Initializing libusb context failed: %s",
vid,
pid,
libusb_error_name(ret));
return NULL;
}
/* dont print any error messages to stderr, change this for debugging */
// libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_NONE);
/* dont print any error messages to stderr, change this for debugging */
// libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_NONE);
dev = io_usb_get_device_handle(ctx, vid, pid);
dev = io_usb_get_device_handle(ctx, vid, pid);
if (!dev) {
libusb_exit(ctx);
return NULL;
}
if (!dev) {
libusb_exit(ctx);
return NULL;
}
ret = libusb_set_configuration(dev, config);
ret = libusb_set_configuration(dev, config);
if (ret != LIBUSB_SUCCESS) {
libusb_close(dev);
libusb_exit(ctx);
log_error(
"(%04X:%04X) Setting configuration failed",
vid,
pid,
libusb_error_name(ret));
return NULL;
}
/* check if the device is attached to the kernel, detach it first then */
if (libusb_kernel_driver_active(dev, iface) == 1) {
ret = libusb_detach_kernel_driver(dev, iface);
if (ret != LIBUSB_SUCCESS) {
libusb_close(dev);
libusb_exit(ctx);
libusb_close(dev);
libusb_exit(ctx);
log_error("(%04X:%04X) Setting configuration failed", vid, pid,
libusb_error_name(ret));
return NULL;
log_error(
"(%04X:%04X) Detaching interface %d from kernel failed: %s",
vid,
pid,
iface,
libusb_error_name(ret));
return NULL;
}
}
/* check if the device is attached to the kernel, detach it first then */
if (libusb_kernel_driver_active(dev, iface) == 1) {
ret = libusb_detach_kernel_driver(dev, iface);
if (ret != LIBUSB_SUCCESS) {
libusb_close(dev);
libusb_exit(ctx);
ret = libusb_claim_interface(dev, iface);
log_error(
"(%04X:%04X) Detaching interface %d from kernel failed: %s",
vid, pid, iface, libusb_error_name(ret));
return NULL;
}
}
if (ret != LIBUSB_SUCCESS) {
libusb_close(dev);
libusb_exit(ctx);
ret = libusb_claim_interface(dev, iface);
log_error(
"(%04X:%04X) Claiming interface %d failed: %s",
vid,
pid,
iface,
libusb_error_name(ret));
return NULL;
}
if (ret != LIBUSB_SUCCESS) {
libusb_close(dev);
libusb_exit(ctx);
handle = (struct usb_io_ctx *) util_xmalloc(sizeof(struct usb_io_ctx));
log_error("(%04X:%04X) Claiming interface %d failed: %s", vid, pid,
iface, libusb_error_name(ret));
return NULL;
}
handle->ctx = ctx;
handle->dev = dev;
handle = (struct usb_io_ctx*) util_xmalloc(sizeof(struct usb_io_ctx));
log_debug("Opened %04X:%04X, handle %p", vid, pid, handle);
handle->ctx = ctx;
handle->dev = dev;
log_debug("Opened %04X:%04X, handle %p", vid, pid, handle);
return handle;
return handle;
}
int32_t io_usb_control_transfer(void* handle, uint8_t request_type,
uint8_t request, uint16_t value, uint16_t index, uint8_t* data,
uint16_t len, uint32_t timeout)
int32_t io_usb_control_transfer(
void *handle,
uint8_t request_type,
uint8_t request,
uint16_t value,
uint16_t index,
uint8_t *data,
uint16_t len,
uint32_t timeout)
{
struct usb_io_ctx* dev = (struct usb_io_ctx*) handle;
int32_t ret;
struct usb_io_ctx *dev = (struct usb_io_ctx *) handle;
int32_t ret;
ret = libusb_control_transfer(dev->dev, request_type, request, value,
index, data, len, timeout);
ret = libusb_control_transfer(
dev->dev, request_type, request, value, index, data, len, timeout);
if (ret != len) {
log_error("Control transfer, handle %p failed: %s", handle,
libusb_error_name(ret));
}
if (ret != len) {
log_error(
"Control transfer, handle %p failed: %s",
handle,
libusb_error_name(ret));
}
return ret;
return ret;
}
void io_usb_close(void* handle)
void io_usb_close(void *handle)
{
struct usb_io_ctx* dev = (struct usb_io_ctx*) handle;
struct usb_io_ctx *dev = (struct usb_io_ctx *) handle;
log_debug("Closing handle %p", handle);
log_debug("Closing handle %p", handle);
libusb_close(dev->dev);
libusb_exit(dev->ctx);
libusb_close(dev->dev);
libusb_exit(dev->ctx);
free(dev);
free(dev);
}
+11 -5
View File
@@ -16,7 +16,7 @@
* @param iface Interface to claim
* @return On success, a handle to the opened device, NULL on failure
*/
void* io_usb_open(uint16_t vid, uint16_t pid, uint16_t config, uint16_t iface);
void *io_usb_open(uint16_t vid, uint16_t pid, uint16_t config, uint16_t iface);
/**
* Execute a control transfer
@@ -32,15 +32,21 @@ void* io_usb_open(uint16_t vid, uint16_t pid, uint16_t config, uint16_t iface);
* @param timeout Timeout for the operation in ms
* @return Number of bytes read/written or -1 on error
*/
int32_t io_usb_control_transfer(void* handle, uint8_t request_type,
uint8_t request, uint16_t value, uint16_t index, uint8_t* data,
uint16_t len, uint32_t timeout);
int32_t io_usb_control_transfer(
void *handle,
uint8_t request_type,
uint8_t request,
uint16_t value,
uint16_t index,
uint8_t *data,
uint16_t len,
uint32_t timeout);
/**
* Close a opened usb device
*
* @param handle Handle of the opened device
*/
void io_usb_close(void* handle);
void io_usb_close(void *handle);
#endif