libfxlink: add status functions to avoid looking into fdev fields

This commit is contained in:
Lephenixnoir 2023-04-01 21:34:27 +02:00
parent 394d05726d
commit 7b77fb9c0b
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
4 changed files with 27 additions and 5 deletions

View file

@ -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]; struct fxlink_device *fdev = &TUI.devices.devices[i];
/* Check for devices ready to connect to */ /* Check for devices ready to connect to */
if(fdev->status == FXLINK_FDEV_STATUS_IDLE && fdev->comm if(fxlink_device_ready_to_connect(fdev)
&& fdev->comm->ep_bulk_IN != 0xff) { && fxlink_device_has_fxlink_interface(fdev)) {
if(fxlink_device_claim_fxlink(fdev)) if(fxlink_device_claim_fxlink(fdev))
fxlink_device_start_bulk_IN(fdev); fxlink_device_start_bulk_IN(fdev);
} }

View file

@ -1,6 +1,5 @@
# Locate the library file and includes # Locate the library file and includes
message("test: $ENV{HOME}/.local/lib $ENV{FXSDK_PATH}/lib")
find_library( find_library(
LIBFXLINK_PATH "fxlink" LIBFXLINK_PATH "fxlink"
HINTS "$ENV{HOME}/.local/lib" "$ENV{FXSDK_PATH}/lib" HINTS "$ENV{HOME}/.local/lib" "$ENV{FXSDK_PATH}/lib"

View file

@ -280,10 +280,23 @@ void fxlink_device_analysis_2(struct fxlink_device *fdev)
fdev->status = FXLINK_FDEV_STATUS_IDLE; 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) bool fxlink_device_claim_fxlink(struct fxlink_device *fdev)
{ {
/* Only connect to calculators with an fxlink interface */ if(!fxlink_device_ready_to_connect(fdev) ||
if(!fdev->comm || fdev->status != FXLINK_FDEV_STATUS_IDLE) !fxlink_device_has_fxlink_interface(fdev) ||
fdev->comm->claimed)
return false; return false;
/* Allocate transfer data */ /* Allocate transfer data */

View file

@ -195,6 +195,16 @@ struct fxlink_comm {
bool cancelled_OUT; 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 /* Claim the fxlink interface (for a device that has one). Returns false and
sets the status to ERROR on failure. */ sets the status to ERROR on failure. */
bool fxlink_device_claim_fxlink(struct fxlink_device *fdev); bool fxlink_device_claim_fxlink(struct fxlink_device *fdev);