{
int ret;
libusb_device_handle *dev;
+ int current_configuration;
ret = libusb_init(NULL);
if (ret < 0) {
goto out_libusb_exit;
}
- ret = libusb_set_configuration(dev, VISOMAT_CONFIGURATION);
+ current_configuration = -1;
+ ret = libusb_get_configuration(dev, ¤t_configuration);
if (ret < 0) {
- fprintf(stderr, "libusb_set_configuration failed: %s\n",
+ fprintf(stderr, "libusb_get_configuration failed: %s\n",
libusb_error_name(ret));
- fprintf(stderr, "Cannot set configuration %hhu\n",
- VISOMAT_CONFIGURATION);
goto out_libusb_close;
}
+ if (current_configuration != VISOMAT_CONFIGURATION) {
+ ret = libusb_set_configuration(dev, VISOMAT_CONFIGURATION);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_set_configuration failed: %s\n",
+ libusb_error_name(ret));
+ fprintf(stderr, "Cannot set configuration %hhu\n",
+ VISOMAT_CONFIGURATION);
+ goto out_libusb_close;
+ }
+ }
+
libusb_set_auto_detach_kernel_driver(dev, 1);
ret = libusb_claim_interface(dev, VISOMAT_INTERFACE);
goto out_libusb_close;
}
+ /* Checking that the configuration has not changed, as suggested in
+ * http://libusb.sourceforge.net/api-1.0/caveats.html
+ */
+ current_configuration = -1;
+ ret = libusb_get_configuration(dev, ¤t_configuration);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_get_configuration after claim failed: %s\n",
+ libusb_error_name(ret));
+ goto out_libusb_release_interface;
+ }
+
+ if (current_configuration != VISOMAT_CONFIGURATION) {
+ fprintf(stderr, "libusb configuration changed (expected: %hhu, current: %hhu\n",
+ VISOMAT_CONFIGURATION, current_configuration);
+ ret = -EINVAL;
+ goto out_libusb_release_interface;
+ }
+
ret = visomat_get_datetime(dev);
if (ret < 0)
goto out_libusb_release_interface;