From 2dbc14d512995a46e12e074993e4d946adbc3b7d Mon Sep 17 00:00:00 2001 From: Etienne van Delden de la Haije Date: Tue, 5 Mar 2024 20:30:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8Frubify=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libcombustd/communication/ambx.rb | 42 +++++++++++++------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/libcombustd/communication/ambx.rb b/libcombustd/communication/ambx.rb index a17376d..94dc97d 100644 --- a/libcombustd/communication/ambx.rb +++ b/libcombustd/communication/ambx.rb @@ -38,36 +38,28 @@ def self.connect # Open the device if it has been connected before. # If the device hasn't been opened yet, try to open it otherwise fail def self.open - if @device.nil? - if !Ambx.connect - return false - end - end + return false if @device.nil? && !Ambx.connect - unless @device.nil? - @handle = @device.open - # we retry a few times to open the device or kill it - unless @handle.nil? - retries = 0 + @handle = @device.open + # we retry a few times to open the device or kill it + unless @handle.nil? + retries = 0 + begin begin - begin - error_code = @handle.claim_interface(0) - rescue ArgumentError - end - - raise CannotClaimInterfaceError if error_code.nil? # TODO: libusb doesn't return anything on error - return true - rescue CannotClaimInterfaceError - @handle.auto_detach_kernel_driver = true - retries += 1 - retry - else - return false + error_code = @handle.claim_interface(0) + rescue ArgumentError end + + raise CannotClaimInterfaceError if error_code.nil? # TODO: libusb doesn't return anything on error + return true + rescue CannotClaimInterfaceError + @handle.auto_detach_kernel_driver = true + retries += 1 + retry + else + false end end - - false end # Close the device if it is open.