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 Oct 3, 2024
1 parent 9fab199 commit df2d4d0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 35 deletions.
45 changes: 34 additions & 11 deletions linux/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ static void register_global_error_format(const char *format, ...)
* Use register_device_error(dev, NULL) to indicate "no error". */
static void register_device_error(hid_device *dev, const char *msg)
{
if (!dev)
return;

register_error_str(&dev->last_error_str, msg);
}

Expand Down Expand Up @@ -1125,8 +1122,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;

Expand All @@ -1146,8 +1145,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);
Expand Down Expand Up @@ -1201,13 +1202,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
Expand All @@ -1220,8 +1228,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;

Expand All @@ -1236,8 +1246,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;

Expand All @@ -1254,6 +1266,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);
Expand All @@ -1265,8 +1282,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;

Expand Down Expand Up @@ -1369,8 +1388,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
Expand All @@ -1383,8 +1404,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;
Expand Down
56 changes: 44 additions & 12 deletions mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,6 @@ static void register_global_error_format(const char *format, ...)
* Use register_device_error(dev, NULL) to indicate "no error". */
static void register_device_error(hid_device *dev, const char *msg)
{
if (!dev)
return;

register_error_str(&dev->last_error_str, msg);
}

Expand Down Expand Up @@ -1111,8 +1108,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;
Expand Down Expand Up @@ -1156,8 +1155,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_device_error(dev, "Data is NULL");
return -1;
}

unsigned char *report = data;
CFIndex report_length = length;
Expand Down Expand Up @@ -1266,15 +1272,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);
Expand Down Expand Up @@ -1353,13 +1360,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;
Expand Down Expand Up @@ -1501,8 +1515,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);
Expand All @@ -1527,8 +1543,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_device_error(dev, "Location ID is NULL");
return -1;
}

int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey));
if (res != 0) {
Expand All @@ -1552,16 +1575,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_device_error(dev, "Buffer is NULL");
return -1;
}

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

0 comments on commit df2d4d0

Please sign in to comment.