Skip to content

Commit 0c7f1ea

Browse files
hid: implement hid_error
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.
1 parent 02f7938 commit 0c7f1ea

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

libusb/hid.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,11 +1888,38 @@ int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char
18881888
return hid_get_report_descriptor_libusb(dev->device_handle, dev->interface, dev->report_descriptor_size, buf, buf_size);
18891889
}
18901890

1891-
18921891
HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
18931892
{
1894-
(void)dev;
1895-
return L"hid_error is not implemented yet";
1893+
static const char format_simple[] = "%s: %s";
1894+
static const char format_context[] = "%s: %s (%s)";
1895+
const char *name, *message, *context, *format;
1896+
char *buffer;
1897+
wchar_t *w;
1898+
size_t len;
1899+
1900+
if (dev->error == LIBUSB_SUCCESS) {
1901+
return NULL;
1902+
}
1903+
1904+
name = libusb_error_name(dev->error);
1905+
message = libusb_strerror(dev->error);
1906+
context = dev->error_context;
1907+
format = context? format_context : format_simple;
1908+
1909+
len = 1 + snprintf(NULL, 0, format, name, message, context);
1910+
1911+
buffer = malloc(len);
1912+
if (!buffer) {
1913+
return NULL;
1914+
}
1915+
1916+
snprintf(buffer, len, format, name, message, context);
1917+
1918+
w = utf8_to_wchar(buffer);
1919+
1920+
free(buffer);
1921+
1922+
return w;
18961923
}
18971924

18981925

0 commit comments

Comments
 (0)