From 77c4c30d6fa5a47f94c2b8db1616e12037804ce3 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 3 Sep 2022 23:38:42 +0200 Subject: [PATCH] Improve check if command is supported --- include/sdutils/sdutils.h | 69 +++++++++++++++++++++------------------ source/utils.cpp | 28 +++++++++++----- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/include/sdutils/sdutils.h b/include/sdutils/sdutils.h index f24f6c9..db8cf03 100644 --- a/include/sdutils/sdutils.h +++ b/include/sdutils/sdutils.h @@ -16,6 +16,7 @@ typedef enum SDUtilsStatus { SDUTILS_RESULT_FAILED = -0x10, SDUTILS_RESULT_LIB_UNINITIALIZED = -0x20, SDUTILS_RESULT_UNSUPPORTED_VERSION = -0x99, + SDUTILS_RESULT_UNSUPPORTED_COMMAND = -0x100, SDUTILS_RESULT_UNKNOWN_ERROR = -0x1000, } SDUtilsStatus; @@ -33,8 +34,8 @@ typedef void (*SDCleanUpHandlesHandlerFn)(); /** * Initializes the SDUtils library. This must be call before any other function can be called - * @return SDUTILS_RESULT_SUCCESS on success, the functions of this lib can be used - * SDUTILS_RESULT_MODULE_NOT_FOUND when the SDHotSwapModule is not loaded + * @return SDUTILS_RESULT_SUCCESS on success, the functions of this lib can be used
+ * SDUTILS_RESULT_MODULE_NOT_FOUND when the SDHotSwapModule is not loaded
* SDUTILS_RESULT_MODULE_MISSING_EXPORT when the SDHotSwapModule does not export the expected functions. */ SDUtilsStatus SDUtils_InitLibrary(); @@ -59,19 +60,20 @@ SDUtilsStatus SDUtils_DeInitLibrary(); SDUtilsStatus SDUtils_GetVersion(SDUtilsVersion *outVersion); /** - * Registers a callback which will be called whenever a sd card will be inserted or ejected. - * This is only true for future events, if the sd card is already inserted before registering - * a callback, the callback is only called on the next ejecting but not for the initial - * insertion that happened in the past. - * - * Any previously registered callbacks will be removed when the currently running application - * is closing. + * Registers a callback which will be called whenever a sd card will be inserted or ejected.
+ * This is only true for future events, if the sd card is already inserted before registering
+ * a callback, the callback is only called on the next ejecting but not for the initial
+ * insertion that happened in the past.
+ *
+ * Any previously registered callbacks will be removed when the currently running application
+ * is closing.
* * @param fn callback that will be called - * @return SDUTILS_RESULT_SUCCESS on success - * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the - * maximum amount of callback has been reached - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + * @return SDUTILS_RESULT_SUCCESS on success
+ * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the
+ * maximum amount of callback has been reached
+ * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly
+ * SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module */ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); @@ -79,27 +81,29 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); * Removed a previously registered callback * * @param fn - * @return SDUTILS_RESULT_SUCCESS on success - * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + * @return SDUTILS_RESULT_SUCCESS on success
+ * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered.
+ * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly
+ * SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module */ SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn); /** - * Registers a callback which will be called whenever a sd card will be ejected and before - * the SDUtils_AddAttachHandler. This callback is supposed to be used to clean up any open - * file handles before the sd card gets unmounted. - * This is only true for future events, if the sd card is already ejected before registering - * a callback, the callback is only called on the next ejecting. - * - * Any previously registered callbacks will be removed when the currently running application - * is closing. + * Registers a callback which will be called whenever a sd card will be ejected and before
+ * the SDUtils_AddAttachHandler. This callback is supposed to be used to clean up any open
+ * file handles before the sd card gets unmounted.
+ * This is only true for future events, if the sd card is already ejected before registering
+ * a callback, the callback is only called on the next ejecting.
+ *
+ * Any previously registered callbacks will be removed when the currently running application
+ * is closing.
* * @param fn callback that will be called - * @return SDUTILS_RESULT_SUCCESS on success - * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the - * maximum amount of callback has been reached - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + * @return SDUTILS_RESULT_SUCCESS on success
+ * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the
+ * maximum amount of callback has been reached
+ * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly
+ * SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module */ SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); @@ -107,9 +111,10 @@ SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); * Removed a previously registered callback * * @param fn - * @return SDUTILS_RESULT_SUCCESS on success - * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. - * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly + * @return SDUTILS_RESULT_SUCCESS on success
+ * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered.
+ * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly
+ * SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module */ SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); @@ -117,7 +122,7 @@ SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); * Checks if a FAT32 formatted SD Card is inserted, mounted and available via `fs:/vol/external01` * * @param outStatus stores the result of the function - * @return SDUTILS_RESULT_SUCCESS on success + * @return SDUTILS_RESULT_SUCCESS on success
* SDUTILS_RESULT_INVALID_ARGUMENT when an invalid ptr has been given */ SDUtilsStatus SDUtils_IsSdCardMounted(bool *outStatus); diff --git a/source/utils.cpp b/source/utils.cpp index e112214..71dda17 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -34,22 +34,22 @@ SDUtilsStatus SDUtils_InitLibrary() { if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) { DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddAttachHandler failed."); - return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + sSDUtilsAddAttachHandler = nullptr; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveAttachHandler", (void **) &sSDUtilsRemoveAttachHandler) != OS_DYNLOAD_OK) { DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveAttachHandler failed."); - return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + sSDUtilsRemoveAttachHandler = nullptr; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddCleanUpHandlesHandler", (void **) &sSDUtilsAddCleanUpHandlesHandler) != OS_DYNLOAD_OK) { DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddCleanUpHandlesHandler failed."); - return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + sSDUtilsAddCleanUpHandlesHandler = nullptr; } if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveCleanUpHandlesHandler", (void **) &sSDUtilsRemoveCleanUpHandlesHandler) != OS_DYNLOAD_OK) { DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveCleanUpHandlesHandler failed."); - return SDUTILS_RESULT_MODULE_MISSING_EXPORT; + sSDUtilsRemoveCleanUpHandlesHandler = nullptr; } return SDUTILS_RESULT_SUCCESS; @@ -96,9 +96,12 @@ SDUtilsStatus SDUtils_IsSdCardMounted(bool *status) { bool AddAttachHandler(SDAttachHandlerFn); SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) { - if (sSDUtilsAddAttachHandler == nullptr) { + if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) { return SDUTILS_RESULT_LIB_UNINITIALIZED; } + if (sSDUtilsAddAttachHandler == nullptr || sSDUtilsVersion < 1) { + return SDUTILS_RESULT_UNSUPPORTED_COMMAND; + } auto res = reinterpret_cast(sSDUtilsAddAttachHandler)(fn); return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; } @@ -106,9 +109,12 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) { bool RemoveAttachHandler(SDAttachHandlerFn); SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { - if (sSDUtilsRemoveAttachHandler == nullptr) { + if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) { return SDUTILS_RESULT_LIB_UNINITIALIZED; } + if (sSDUtilsRemoveAttachHandler == nullptr || sSDUtilsVersion < 1) { + return SDUTILS_RESULT_UNSUPPORTED_COMMAND; + } auto res = reinterpret_cast(sSDUtilsRemoveAttachHandler)(fn); return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; } @@ -116,9 +122,12 @@ SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { bool AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { - if (sSDUtilsAddCleanUpHandlesHandler == nullptr) { + if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) { return SDUTILS_RESULT_LIB_UNINITIALIZED; } + if (sSDUtilsAddCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) { + return SDUTILS_RESULT_UNSUPPORTED_COMMAND; + } auto res = reinterpret_cast(sSDUtilsAddCleanUpHandlesHandler)(fn); return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; } @@ -126,9 +135,12 @@ SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { bool RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { - if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr) { + if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) { return SDUTILS_RESULT_LIB_UNINITIALIZED; } + if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) { + return SDUTILS_RESULT_UNSUPPORTED_COMMAND; + } auto res = reinterpret_cast(sSDUtilsRemoveCleanUpHandlesHandler)(fn); return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; } \ No newline at end of file