Improve check if command is supported

This commit is contained in:
Maschell 2022-09-03 23:38:42 +02:00
parent 2bf277b992
commit 77c4c30d6f
2 changed files with 57 additions and 40 deletions

View File

@ -16,6 +16,7 @@ typedef enum SDUtilsStatus {
SDUTILS_RESULT_FAILED = -0x10, SDUTILS_RESULT_FAILED = -0x10,
SDUTILS_RESULT_LIB_UNINITIALIZED = -0x20, SDUTILS_RESULT_LIB_UNINITIALIZED = -0x20,
SDUTILS_RESULT_UNSUPPORTED_VERSION = -0x99, SDUTILS_RESULT_UNSUPPORTED_VERSION = -0x99,
SDUTILS_RESULT_UNSUPPORTED_COMMAND = -0x100,
SDUTILS_RESULT_UNKNOWN_ERROR = -0x1000, SDUTILS_RESULT_UNKNOWN_ERROR = -0x1000,
} SDUtilsStatus; } SDUtilsStatus;
@ -33,8 +34,8 @@ typedef void (*SDCleanUpHandlesHandlerFn)();
/** /**
* Initializes the SDUtils library. This must be call before any other function can be called * 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 * @return SDUTILS_RESULT_SUCCESS on success, the functions of this lib can be used <br>
* SDUTILS_RESULT_MODULE_NOT_FOUND when the SDHotSwapModule is not loaded * SDUTILS_RESULT_MODULE_NOT_FOUND when the SDHotSwapModule is not loaded <br>
* SDUTILS_RESULT_MODULE_MISSING_EXPORT when the SDHotSwapModule does not export the expected functions. * SDUTILS_RESULT_MODULE_MISSING_EXPORT when the SDHotSwapModule does not export the expected functions.
*/ */
SDUtilsStatus SDUtils_InitLibrary(); SDUtilsStatus SDUtils_InitLibrary();
@ -59,19 +60,20 @@ SDUtilsStatus SDUtils_DeInitLibrary();
SDUtilsStatus SDUtils_GetVersion(SDUtilsVersion *outVersion); SDUtilsStatus SDUtils_GetVersion(SDUtilsVersion *outVersion);
/** /**
* Registers a callback which will be called whenever a sd card will be inserted or ejected. * Registers a callback which will be called whenever a sd card will be inserted or ejected. <br>
* This is only true for future events, if the sd card is already inserted before registering * This is only true for future events, if the sd card is already inserted before registering <br>
* a callback, the callback is only called on the next ejecting but not for the initial * a callback, the callback is only called on the next ejecting but not for the initial <br>
* insertion that happened in the past. * insertion that happened in the past. <br>
* * <br>
* Any previously registered callbacks will be removed when the currently running application * Any previously registered callbacks will be removed when the currently running application <br>
* is closing. * is closing. <br>
* *
* @param fn callback that will be called * @param fn callback that will be called
* @return SDUTILS_RESULT_SUCCESS on success * @return SDUTILS_RESULT_SUCCESS on success <br>
* SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the <br>
* maximum amount of callback has been reached * maximum amount of callback has been reached <br>
* SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly <br>
* SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module
*/ */
SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn); SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn);
@ -79,27 +81,29 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn);
* Removed a previously registered callback * Removed a previously registered callback
* *
* @param fn * @param fn
* @return SDUTILS_RESULT_SUCCESS on success * @return SDUTILS_RESULT_SUCCESS on success <br>
* SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. <br>
* SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly <br>
* SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module
*/ */
SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn); SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn);
/** /**
* Registers a callback which will be called whenever a sd card will be ejected and before * Registers a callback which will be called whenever a sd card will be ejected and before <br>
* the SDUtils_AddAttachHandler. This callback is supposed to be used to clean up any open * the SDUtils_AddAttachHandler. This callback is supposed to be used to clean up any open <br>
* file handles before the sd card gets unmounted. * file handles before the sd card gets unmounted. <br>
* This is only true for future events, if the sd card is already ejected before registering * This is only true for future events, if the sd card is already ejected before registering <br>
* a callback, the callback is only called on the next ejecting. * a callback, the callback is only called on the next ejecting. <br>
* * <br>
* Any previously registered callbacks will be removed when the currently running application * Any previously registered callbacks will be removed when the currently running application <br>
* is closing. * is closing. <br>
* *
* @param fn callback that will be called * @param fn callback that will be called
* @return SDUTILS_RESULT_SUCCESS on success * @return SDUTILS_RESULT_SUCCESS on success <br>
* SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the * SDUTILS_RESULT_MAX_CALLBACKS when registering the callback has failed because the <br>
* maximum amount of callback has been reached * maximum amount of callback has been reached <br>
* SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly <br>
* SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module
*/ */
SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn);
@ -107,9 +111,10 @@ SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn);
* Removed a previously registered callback * Removed a previously registered callback
* *
* @param fn * @param fn
* @return SDUTILS_RESULT_SUCCESS on success * @return SDUTILS_RESULT_SUCCESS on success <br>
* SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. * SDUTILS_RESULT_NOT_FOUND when the given callback was not registered. <br>
* SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly * SDUTILS_RESULT_LIB_UNINITIALIZED if the lib was not initialized properly <br>
* SDUTILS_RESULT_UNSUPPORTED_COMMAND Command not supported by the currently running module
*/ */
SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn); 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` * Checks if a FAT32 formatted SD Card is inserted, mounted and available via `fs:/vol/external01`
* *
* @param outStatus stores the result of the function * @param outStatus stores the result of the function
* @return SDUTILS_RESULT_SUCCESS on success * @return SDUTILS_RESULT_SUCCESS on success <br>
* SDUTILS_RESULT_INVALID_ARGUMENT when an invalid ptr has been given * SDUTILS_RESULT_INVALID_ARGUMENT when an invalid ptr has been given
*/ */
SDUtilsStatus SDUtils_IsSdCardMounted(bool *outStatus); SDUtilsStatus SDUtils_IsSdCardMounted(bool *outStatus);

View File

@ -34,22 +34,22 @@ SDUtilsStatus SDUtils_InitLibrary() {
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) { if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddAttachHandler failed."); 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) { if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveAttachHandler", (void **) &sSDUtilsRemoveAttachHandler) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveAttachHandler failed."); 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) { if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddCleanUpHandlesHandler", (void **) &sSDUtilsAddCleanUpHandlesHandler) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddCleanUpHandlesHandler failed."); 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) { if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveCleanUpHandlesHandler", (void **) &sSDUtilsRemoveCleanUpHandlesHandler) != OS_DYNLOAD_OK) {
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveCleanUpHandlesHandler failed."); DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveCleanUpHandlesHandler failed.");
return SDUTILS_RESULT_MODULE_MISSING_EXPORT; sSDUtilsRemoveCleanUpHandlesHandler = nullptr;
} }
return SDUTILS_RESULT_SUCCESS; return SDUTILS_RESULT_SUCCESS;
@ -96,9 +96,12 @@ SDUtilsStatus SDUtils_IsSdCardMounted(bool *status) {
bool AddAttachHandler(SDAttachHandlerFn); bool AddAttachHandler(SDAttachHandlerFn);
SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) { SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) {
if (sSDUtilsAddAttachHandler == nullptr) { if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
return SDUTILS_RESULT_LIB_UNINITIALIZED; return SDUTILS_RESULT_LIB_UNINITIALIZED;
} }
if (sSDUtilsAddAttachHandler == nullptr || sSDUtilsVersion < 1) {
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
}
auto res = reinterpret_cast<decltype(&AddAttachHandler)>(sSDUtilsAddAttachHandler)(fn); auto res = reinterpret_cast<decltype(&AddAttachHandler)>(sSDUtilsAddAttachHandler)(fn);
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS;
} }
@ -106,9 +109,12 @@ SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) {
bool RemoveAttachHandler(SDAttachHandlerFn); bool RemoveAttachHandler(SDAttachHandlerFn);
SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) { SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) {
if (sSDUtilsRemoveAttachHandler == nullptr) { if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
return SDUTILS_RESULT_LIB_UNINITIALIZED; return SDUTILS_RESULT_LIB_UNINITIALIZED;
} }
if (sSDUtilsRemoveAttachHandler == nullptr || sSDUtilsVersion < 1) {
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
}
auto res = reinterpret_cast<decltype(&RemoveAttachHandler)>(sSDUtilsRemoveAttachHandler)(fn); auto res = reinterpret_cast<decltype(&RemoveAttachHandler)>(sSDUtilsRemoveAttachHandler)(fn);
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND;
} }
@ -116,9 +122,12 @@ SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) {
bool AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); bool AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn);
SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
if (sSDUtilsAddCleanUpHandlesHandler == nullptr) { if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
return SDUTILS_RESULT_LIB_UNINITIALIZED; return SDUTILS_RESULT_LIB_UNINITIALIZED;
} }
if (sSDUtilsAddCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) {
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
}
auto res = reinterpret_cast<decltype(&AddCleanUpHandlesHandler)>(sSDUtilsAddCleanUpHandlesHandler)(fn); auto res = reinterpret_cast<decltype(&AddCleanUpHandlesHandler)>(sSDUtilsAddCleanUpHandlesHandler)(fn);
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS; return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS;
} }
@ -126,9 +135,12 @@ SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
bool RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn); bool RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn);
SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) { SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr) { if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
return SDUTILS_RESULT_LIB_UNINITIALIZED; return SDUTILS_RESULT_LIB_UNINITIALIZED;
} }
if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) {
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
}
auto res = reinterpret_cast<decltype(&RemoveCleanUpHandlesHandler)>(sSDUtilsRemoveCleanUpHandlesHandler)(fn); auto res = reinterpret_cast<decltype(&RemoveCleanUpHandlesHandler)>(sSDUtilsRemoveCleanUpHandlesHandler)(fn);
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND; return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND;
} }