From: Antonio Ospite Date: Wed, 10 Jun 2015 09:13:04 +0000 (+0200) Subject: Misc libusb cleanups X-Git-Url: https://git.ao2.it/visomat-utils.git/commitdiff_plain/f7a9b92e7107a03a92e64a7091f0dea908210cda?ds=inline Misc libusb cleanups Use symbolic constants, check return values, auto detach the kernel driver, release the interface. --- diff --git a/src/visomat-data-downloader.c b/src/visomat-data-downloader.c index a91ee43..8cb99e2 100644 --- a/src/visomat-data-downloader.c +++ b/src/visomat-data-downloader.c @@ -25,15 +25,16 @@ #include #include +#define VISOMAT_DEVICE_VID 0x1247 +#define VISOMAT_DEVICE_PID 0x00f8 +#define VISOMAT_CONFIGURATION 1 +#define VISOMAT_INTERFACE 1 +#define VISOMAT_EP_IN 0x82 +#define VISOMAT_EP_OUT 0x03 + #define STX 0x02 #define ETX 0x03 -#define DEVICE_VID 0x1247 -#define DEVICE_PID 0x00f8 - -#define EP_IN 0x82 -#define EP_OUT 0x03 - #define BASE_YEAR 2000 typedef enum { @@ -224,7 +225,7 @@ static int send_command(libusb_device_handle *dev, visomat_command cmd) request[4] = ETX; transferred = 0; - ret = libusb_bulk_transfer(dev, EP_OUT, request, sizeof(request), &transferred, 0); + ret = libusb_bulk_transfer(dev, VISOMAT_EP_OUT, request, sizeof(request), &transferred, 0); if (ret != 0 || transferred != sizeof(request)) { fprintf(stderr, "Error: sending request: %d (%s)\ttransferred: %d (expected %zu)\n", ret, libusb_error_name(ret), transferred, sizeof(request)); @@ -247,7 +248,7 @@ static int get_response(libusb_device_handle *dev, unsigned int j; transferred = 0; - ret = libusb_bulk_transfer(dev, EP_IN, response, sizeof(response), &transferred, 5000); + ret = libusb_bulk_transfer(dev, VISOMAT_EP_IN, response, sizeof(response), &transferred, 5000); if (ret != 0) { fprintf(stderr, "Error getting response: %d (%s)\ttransferred: %d (expected %zu)\n", ret, libusb_error_name(ret), transferred, sizeof(response)); @@ -316,29 +317,54 @@ int main(void) int ret; libusb_device_handle *dev; - libusb_init(NULL); + ret = libusb_init(NULL); + if (ret < 0) { + fprintf(stderr, "error (%d): cannot initialize libusb.\n", ret); + goto out; + } + libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO); - dev = libusb_open_device_with_vid_pid(NULL, DEVICE_VID, DEVICE_PID); + dev = libusb_open_device_with_vid_pid(NULL, + VISOMAT_DEVICE_VID, + VISOMAT_DEVICE_PID); if (dev == NULL) { - fprintf(stderr, "Couldn't open device.\n"); - ret = -ENODEV; + fprintf(stderr, "error (%d): cannot open device.\n", errno); + ret = -errno; goto out_libusb_exit; } - libusb_set_configuration(dev, 1); - libusb_detach_kernel_driver(dev, 1); - libusb_claim_interface(dev, 1); + ret = libusb_set_configuration(dev, VISOMAT_CONFIGURATION); + if (ret < 0) { + fprintf(stderr, "error (%d): cannot set configuration %d.\n", + ret, VISOMAT_CONFIGURATION); + goto out_libusb_close; + } + + libusb_set_auto_detach_kernel_driver(dev, 1); + + ret = libusb_claim_interface(dev, VISOMAT_INTERFACE); + if (ret < 0) { + fprintf(stderr, "error (%d): cannot claim interface %d.\n", + ret, VISOMAT_INTERFACE); + goto out_libusb_close; + } ret = visomat_get_datetime(dev); if (ret < 0) - goto out; + goto out_libusb_release_interface; ret = visomat_dump_eeprom(dev, 0x01 | 0x02); + if (ret < 0) + goto out_libusb_release_interface; -out: +out_libusb_release_interface: + libusb_release_interface(dev, VISOMAT_INTERFACE); +out_libusb_close: libusb_close(dev); + dev = NULL; out_libusb_exit: libusb_exit(NULL); +out: return ret; }