Skip to content

Commit

Permalink
Register errors on early outs
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Sep 27, 2024
1 parent 0346fc6 commit 9b302c4
Showing 3 changed files with 119 additions and 29 deletions.
42 changes: 34 additions & 8 deletions linux/hid.c
Original file line number Diff line number Diff line change
@@ -1125,8 +1125,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)

int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

int bytes_written;

@@ -1146,8 +1148,10 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t

int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

/* Set device error to none */
register_device_error(dev, NULL);
@@ -1201,13 +1205,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t

int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
{
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
}

int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

/* Do all non-blocking in userspace using poll(), since it looks
like there's a bug in the kernel in some versions where
@@ -1220,8 +1231,10 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)

int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

int res;

@@ -1236,8 +1249,10 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char

int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

int res;

@@ -1254,6 +1269,11 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
{
int res;

if (!dev) {
register_global_error("Device is NULL");
return -1;
}

register_device_error(dev, NULL);

res = ioctl(dev->device_handle, HIDIOCSOUTPUT(length), data);
@@ -1265,8 +1285,10 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un

int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

int res;

@@ -1369,8 +1391,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s


HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return NULL;
}

if (!dev->device_info) {
// Lazy initialize device_info
@@ -1383,8 +1407,10 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi

int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

(void)string_index;
(void)string;
53 changes: 44 additions & 9 deletions mac/hid.c
Original file line number Diff line number Diff line change
@@ -1111,8 +1111,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)

static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

const unsigned char *data_to_send = data;
CFIndex length_to_send = length;
@@ -1156,8 +1158,15 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char

static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length)
{
if (!dev || !data)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

if (!data) {
register_global_error("Data is NULL");
return -1;
}

unsigned char *report = data;
CFIndex report_length = length;
@@ -1266,15 +1275,16 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t
}

return 0;

}

int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
{
int bytes_read = -1;

if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return bytes_read;
}

/* Lock the access to the report list. */
pthread_mutex_lock(&dev->mutex);
@@ -1353,13 +1363,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t

int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
{
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
}

int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

/* All Nonblocking operation is handled by the library. */
dev->blocking = !nonblock;
@@ -1501,8 +1518,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
}

HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return NULL;
}

if (!dev->device_info) {
dev->device_info = create_device_info(dev->device_handle);
@@ -1527,8 +1546,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index

int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id)
{
if (!dev || !location_id)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

if (!location_id) {
register_global_error("Location ID is NULL");
return -1;
}

int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey));
if (res != 0) {
@@ -1552,16 +1578,25 @@ int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive(void)

int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive(hid_device *dev)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

return (dev->open_options == kIOHIDOptionsTypeSeizeDevice) ? 1 : 0;
}

int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
{
if (!dev)
if (!dev) {
register_global_error("Device is NULL");
return -1;
}

if (!buf) {
register_global_error("Buffer is NULL");
return -1;
}

CFTypeRef ref = IOHIDDeviceGetProperty(dev->device_handle, CFSTR(kIOHIDReportDescriptorKey));
if (ref != NULL && CFGetTypeID(ref) == CFDataGetTypeID()) {
Loading

0 comments on commit 9b302c4

Please sign in to comment.