Skip to content

Commit

Permalink
hid: implement hid_error
Browse files Browse the repository at this point in the history
Compute a formatted error string containing the libusb error name,
the error message as well as any contextual information.
Return NULL if there are no errors or if memory allocation failed.
  • Loading branch information
matheusmoreira committed Sep 7, 2024
1 parent ec4d74a commit 9b6bef5
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1888,11 +1888,38 @@ int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char
return hid_get_report_descriptor_libusb(dev->device_handle, dev->interface, dev->report_descriptor_size, buf, buf_size);
}


HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
{
(void)dev;
return L"hid_error is not implemented yet";
static const char format_simple[] = "%s: %s";
static const char format_context[] = "%s: %s (%s)";
const char *name, *message, *context, *format;
char *buffer;
wchar_t *w;
size_t len;

if (dev->error == LIBUSB_SUCCESS) {
return NULL;
}

name = libusb_error_name(dev->error);
message = libusb_strerror(dev->error);
context = dev->error_context;
format = context? format_context : format_simple;

len = 1 + snprintf(NULL, 0, format, name, message, context);

buffer = malloc(len);
if (!buffer) {
return NULL;
}

snprintf(buffer, len, format, name, message, context);

w = utf8_to_wchar(buffer);

free(buffer);

return w;
}


Expand Down

0 comments on commit 9b6bef5

Please sign in to comment.