Use symbolic constants, check return values, auto detach the kernel
driver, release the interface.
#include <unistd.h>
#include <libusb.h>
#include <unistd.h>
#include <libusb.h>
+#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 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 {
#define BASE_YEAR 2000
typedef enum {
request[4] = ETX;
transferred = 0;
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));
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));
unsigned int j;
transferred = 0;
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));
if (ret != 0) {
fprintf(stderr, "Error getting response: %d (%s)\ttransferred: %d (expected %zu)\n",
ret, libusb_error_name(ret), transferred, sizeof(response));
int ret;
libusb_device_handle *dev;
int ret;
libusb_device_handle *dev;
+ 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);
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);
- fprintf(stderr, "Couldn't open device.\n");
- ret = -ENODEV;
+ fprintf(stderr, "error (%d): cannot open device.\n", errno);
+ ret = -errno;
- 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)
ret = visomat_get_datetime(dev);
if (ret < 0)
+ goto out_libusb_release_interface;
ret = visomat_dump_eeprom(dev, 0x01 | 0x02);
ret = visomat_dump_eeprom(dev, 0x01 | 0x02);
+ if (ret < 0)
+ goto out_libusb_release_interface;
+out_libusb_release_interface:
+ libusb_release_interface(dev, VISOMAT_INTERFACE);
+out_libusb_close:
out_libusb_exit:
libusb_exit(NULL);
out_libusb_exit:
libusb_exit(NULL);