From e68dfb514f8b979675c25640530cf3e99046ce97 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Schl=C3=BCter?= <jan.schluter@lis-lab.fr>
Date: Thu, 20 Dec 2018 17:34:58 +0100
Subject: [PATCH] Correctly claim and release USB interface

---
 CMakeLists.txt   | 3 +--
 src/recorder.cpp | 5 +++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 55bcc03..8d14cad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@ if(POLICY CMP0063)
 endif()
 
 project(jasonrec)
-set(JASONREC_VERSION "1.0")
+set(JASONREC_VERSION "1.1")
 add_definitions(-DJASONREC_VERSION="${JASONREC_VERSION}")
 
 find_path(LIBUSB_INCLUDE_DIR
@@ -15,4 +15,3 @@ find_library(LIBUSB_LIBRARY
 	PATH_SUFFIXES "lib" "lib32" "lib64")
 
 add_subdirectory(src)
-
diff --git a/src/recorder.cpp b/src/recorder.cpp
index ae1a834..8c5e33e 100644
--- a/src/recorder.cpp
+++ b/src/recorder.cpp
@@ -44,6 +44,7 @@ JasonRecorder::JasonRecorder() {
 JasonRecorder::~JasonRecorder() {
     // free handle
     if (handle) {
+        libusb_release_interface(handle, 0);
         libusb_close(handle);
     }
     // free devices
@@ -60,6 +61,7 @@ size_t JasonRecorder::get_device_count() {
 
 void JasonRecorder::set_device(size_t number) {
     if (handle) {
+        libusb_release_interface(handle, 0);
         libusb_close(handle);
         handle = NULL;
     }
@@ -69,6 +71,9 @@ void JasonRecorder::set_device(size_t number) {
     if (libusb_open(devices[number], &handle) < 0) {
         throw std::runtime_error("could not open USB device (try again as root)");
     }
+    if (libusb_claim_interface(handle, 0) < 0) {
+        throw std::runtime_error("could not claim USB interface");
+    }
 }
 
 void JasonRecorder::send_message(std::uint8_t cmd) {
-- 
GitLab