Skip to content

Commit

Permalink
hidraw/libusb: fix -Wall -Wextra -pedantic -Werror compilation (#214)
Browse files Browse the repository at this point in the history
- minor code-style fixes;
  • Loading branch information
Youw authored Nov 24, 2020
1 parent ffb50af commit f6d0073
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
18 changes: 8 additions & 10 deletions hidtest/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
8/22/2009
Copyright 2009
This contents of this file may be used by anyone
for any reason without any conditions and may be
used as a starting point for your own applications
Expand All @@ -29,18 +29,16 @@

int main(int argc, char* argv[])
{
(void)argc;
(void)argv;

int res;
unsigned char buf[256];
#define MAX_STR 255
wchar_t wstr[MAX_STR];
hid_device *handle;
int i;

#ifdef WIN32
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
#endif

struct hid_device_info *devs, *cur_dev;

printf("hidapi test/example tool. Compiled with hidapi version %s, runtime version %s.\n", HID_API_VERSION_STR, hid_version_str());
Expand All @@ -55,7 +53,7 @@ int main(int argc, char* argv[])
return -1;

devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
Expand All @@ -73,7 +71,7 @@ int main(int argc, char* argv[])
memset(buf,0x00,sizeof(buf));
buf[0] = 0x01;
buf[1] = 0x81;


// Open the device using the VID, PID,
// and optionally the Serial number.
Expand Down Expand Up @@ -115,7 +113,7 @@ int main(int argc, char* argv[])

// Set the hid_read() function to be non-blocking.
hid_set_nonblocking(handle, 1);

// Try to read from the device. There should be no
// data here, but execution should not block.
res = hid_read(handle, buf, 17);
Expand Down Expand Up @@ -158,7 +156,7 @@ int main(int argc, char* argv[])
printf("Unable to write()\n");
printf("Error: %ls\n", hid_error(handle));
}


// Request state (cmd 0x81). The first byte is the report number (0x1).
buf[0] = 0x1;
Expand Down
8 changes: 6 additions & 2 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1098,18 +1098,21 @@ static void cleanup_mutex(void *param)

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

#if 0
int transferred;
int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
LOG("transferred: %d\n", transferred);
return transferred;
#endif
/* by initialising this variable right here, GCC gives a compilation warning/error: */
/* error: variable ‘bytes_read’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered] */
int bytes_read; /* = -1; */

pthread_mutex_lock(&dev->mutex);
pthread_cleanup_push(&cleanup_mutex, dev);

bytes_read = -1;

/* There's an input report queued up. Return it. */
if (dev->input_reports) {
/* Return the first one */
Expand Down Expand Up @@ -1359,6 +1362,7 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index

HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
{
(void)dev;
return L"hid_error is not implemented yet";
}

Expand Down
21 changes: 14 additions & 7 deletions linux/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ static int get_hid_report_descriptor_from_sysfs(const char *sysfs_path, struct h
* strings pointed to by serial_number_utf8 and product_name_utf8 after use.
*/
static int
parse_uevent_info(const char *uevent, int *bus_type,
parse_uevent_info(const char *uevent, unsigned *bus_type,
unsigned short *vendor_id, unsigned short *product_id,
char **serial_number_utf8, char **product_name_utf8)
{
Expand Down Expand Up @@ -471,8 +471,8 @@ static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t
struct udev_device *udev_dev, *parent, *hid_dev;
struct stat s;
int ret = -1;
char *serial_number_utf8 = NULL;
char *product_name_utf8 = NULL;
char *serial_number_utf8 = NULL;
char *product_name_utf8 = NULL;

/* Create the udev object */
udev = udev_new();
Expand All @@ -495,7 +495,7 @@ static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t
if (hid_dev) {
unsigned short dev_vid;
unsigned short dev_pid;
int bus_type;
unsigned bus_type;
size_t retm;

ret = parse_uevent_info(
Expand Down Expand Up @@ -567,8 +567,8 @@ static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t
}

end:
free(serial_number_utf8);
free(product_name_utf8);
free(serial_number_utf8);
free(product_name_utf8);

udev_device_unref(udev_dev);
/* parent and hid_dev don't need to be (and can't be) unref'd.
Expand Down Expand Up @@ -647,7 +647,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
unsigned short dev_pid;
char *serial_number_utf8 = NULL;
char *product_name_utf8 = NULL;
int bus_type;
unsigned bus_type;
int result;
struct hidraw_report_descriptor report_desc;

Expand Down Expand Up @@ -1046,6 +1046,9 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
// Not supported by Linux HidRaw yet
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
{
(void)dev;
(void)data;
(void)length;
return -1;
}

Expand Down Expand Up @@ -1082,6 +1085,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s

int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
{
(void)dev;
(void)string_index;
(void)string;
(void)maxlen;
return -1;
}

Expand Down

0 comments on commit f6d0073

Please sign in to comment.