os/hid: Add os_hid_get_feature_timeout.

This commit is contained in:
Lubosz Sarnecki 2019-09-25 12:52:25 +02:00
parent 6eb1086578
commit a1d729af8f
2 changed files with 44 additions and 0 deletions
src/xrt/auxiliary/os

View file

@ -38,6 +38,11 @@ struct os_hid_device
uint8_t *data,
size_t size);
int (*get_feature_timeout)(struct os_hid_device *hid_dev,
void *data,
size_t size,
uint32_t timeout);
int (*set_feature)(struct os_hid_device *hid_dev,
const uint8_t *data,
size_t size);
@ -84,6 +89,18 @@ os_hid_get_feature(struct os_hid_device *hid_dev,
return hid_dev->get_feature(hid_dev, report_num, data, size);
}
/*!
* Get a feature report with a timeout.
*/
XRT_MAYBE_UNUSED static inline int
os_hid_get_feature_timeout(struct os_hid_device *hid_dev,
void *data,
size_t size,
uint32_t timeout)
{
return hid_dev->get_feature_timeout(hid_dev, data, size, timeout);
}
/*!
* Set a feature report.
*

View file

@ -18,6 +18,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/ioctl.h>
@ -90,6 +91,31 @@ os_hidraw_get_feature(struct os_hid_device *ohdev,
return ioctl(hrdev->fd, HIDIOCGFEATURE(length), data);
}
static int
os_hidraw_get_feature_timeout(struct os_hid_device *ohdev,
void *data,
size_t length,
uint32_t timeout)
{
struct hid_hidraw *hrdev = (struct hid_hidraw *)ohdev;
struct timespec ts = {.tv_sec = 0, .tv_nsec = 1000000};
unsigned int i;
int ret = 0;
for (i = 0; i < timeout; i++) {
ret = ioctl(hrdev->fd, HIDIOCGFEATURE(length), data);
if (ret != -1 || errno != EPIPE)
break;
ts.tv_sec = 0;
ts.tv_nsec = 1000000;
nanosleep(&ts, NULL);
}
return ret;
}
static int
os_hidraw_set_feature(struct os_hid_device *ohdev,
const uint8_t *data,
@ -117,6 +143,7 @@ os_hid_open_hidraw(const char *path, struct os_hid_device **out_hid)
hrdev->base.read = os_hidraw_read;
hrdev->base.write = os_hidraw_write;
hrdev->base.get_feature = os_hidraw_get_feature;
hrdev->base.get_feature_timeout = os_hidraw_get_feature_timeout;
hrdev->base.set_feature = os_hidraw_set_feature;
hrdev->base.destroy = os_hidraw_destroy;
hrdev->fd = open(path, O_RDWR);