diff --git a/fxlink/tui/tui-interactive.c b/fxlink/tui/tui-interactive.c index 64ffd61..041c94f 100644 --- a/fxlink/tui/tui-interactive.c +++ b/fxlink/tui/tui-interactive.c @@ -469,8 +469,8 @@ bool TUI_core_update(bool allow_console, bool auto_refresh, bool *has_command) struct fxlink_device *fdev = &TUI.devices.devices[i]; /* Check for devices ready to connect to */ - if(fdev->status == FXLINK_FDEV_STATUS_IDLE && fdev->comm - && fdev->comm->ep_bulk_IN != 0xff) { + if(fxlink_device_ready_to_connect(fdev) + && fxlink_device_has_fxlink_interface(fdev)) { if(fxlink_device_claim_fxlink(fdev)) fxlink_device_start_bulk_IN(fdev); } diff --git a/libfxlink/cmake/FindLibFxlink.cmake b/libfxlink/cmake/FindLibFxlink.cmake index e624fd2..daf6ca2 100644 --- a/libfxlink/cmake/FindLibFxlink.cmake +++ b/libfxlink/cmake/FindLibFxlink.cmake @@ -1,6 +1,5 @@ # Locate the library file and includes -message("test: $ENV{HOME}/.local/lib $ENV{FXSDK_PATH}/lib") find_library( LIBFXLINK_PATH "fxlink" HINTS "$ENV{HOME}/.local/lib" "$ENV{FXSDK_PATH}/lib" diff --git a/libfxlink/devices.c b/libfxlink/devices.c index 89fd095..cf6f144 100644 --- a/libfxlink/devices.c +++ b/libfxlink/devices.c @@ -280,10 +280,23 @@ void fxlink_device_analysis_2(struct fxlink_device *fdev) fdev->status = FXLINK_FDEV_STATUS_IDLE; } +bool fxlink_device_ready_to_connect(struct fxlink_device const *fdev) +{ + bool status = (fdev->status == FXLINK_FDEV_STATUS_IDLE) || + (fdev->status == FXLINK_FDEV_STATUS_CONNECTED); + return fdev->calc && fdev->comm && status; +} + +bool fxlink_device_has_fxlink_interface(struct fxlink_device const *fdev) +{ + return fdev->calc && fdev->comm && (fdev->comm->ep_bulk_IN != 0xff); +} + bool fxlink_device_claim_fxlink(struct fxlink_device *fdev) { - /* Only connect to calculators with an fxlink interface */ - if(!fdev->comm || fdev->status != FXLINK_FDEV_STATUS_IDLE) + if(!fxlink_device_ready_to_connect(fdev) || + !fxlink_device_has_fxlink_interface(fdev) || + fdev->comm->claimed) return false; /* Allocate transfer data */ diff --git a/libfxlink/include/fxlink/devices.h b/libfxlink/include/fxlink/devices.h index 5675f5e..a175f9d 100644 --- a/libfxlink/include/fxlink/devices.h +++ b/libfxlink/include/fxlink/devices.h @@ -195,6 +195,16 @@ struct fxlink_comm { bool cancelled_OUT; }; +/* Check whether the device is ready to have interfaces claimed. This function + only checks that the device is a calculator and could be opened; it doesn't + guarantee that claiming the interfaces will succeed. This function returns + true even after an interface has been claimed since multiple interfaces can + be claimed at the same time. */ +bool fxlink_device_ready_to_connect(struct fxlink_device const *fdev); + +/* Check whether the device exposes an fxlink interface. */ +bool fxlink_device_has_fxlink_interface(struct fxlink_device const *fdev); + /* Claim the fxlink interface (for a device that has one). Returns false and sets the status to ERROR on failure. */ bool fxlink_device_claim_fxlink(struct fxlink_device *fdev);