diff --git a/src/posix/platform/misc.c b/src/posix/platform/misc.c index 61f5f0c3b..7a7f8c540 100644 --- a/src/posix/platform/misc.c +++ b/src/posix/platform/misc.c @@ -35,6 +35,7 @@ #include #include "openthread-system.h" +#include "common/logging.hpp" extern jmp_buf gResetJump; @@ -89,3 +90,33 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance) (void)aInstance; return gPlatMcuPowerState; } + +void SuccessOrDie(otError aError) +{ + int exitCode; + + switch (aError) + { + case OT_ERROR_NONE: + return; + + case OT_ERROR_INVALID_ARGS: + exitCode = OT_EXIT_INVALID_ARGUMENTS; + break; + + default: + exitCode = OT_EXIT_FAILURE; + break; + } + + otLogCritPlat("Error: %s", otThreadErrorToString(aError)); + exit(exitCode); +} + +void VerifyOrDie(bool aCondition, int aExitCode) +{ + if (!aCondition) + { + exit(aExitCode); + } +} diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index f9907214a..29b506bc6 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -91,7 +91,12 @@ enum /** * Incompatible radio spinel. */ - OT_EXIT_INCOMPATIBLE_RADIO_SPINEL = 3, + OT_EXIT_RADIO_SPINEL_INCOMPATIBLE = 3, + + /** + * Unexpected radio spinel reset. + */ + OT_EXIT_RADIO_SPINEL_RESET = 4, }; /** @@ -353,6 +358,24 @@ void platformUdpProcess(otInstance *aInstance, const fd_set *aReadSet); */ void platformUdpUpdateFdSet(otInstance *aInstance, fd_set *aReadFdSet, int *aMaxFd); +/** + * This function ends the current process with exit code @p aExitCode if @p aCondition is false. + * + * @param[in] aCondition The condition to verify + * @param[in] aExitCode The exit code if exits. + * + */ +void VerifyOrDie(bool aCondition, int aExitCode); + +/** + * This function ends the current process if @p aError is not OT_ERROR_NONE. + * The error code will be mapped from @p aError. + * + * @param[in] aError The OpenThread error code. + * + */ +void SuccessOrDie(otError aError); + #ifdef __cplusplus } #endif diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index b1072d841..0738d8b1c 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -142,23 +142,6 @@ static inline bool isAckRequested(const uint8_t *frame) return (frame[0] & IEEE802154_ACK_REQUEST) != 0; } -static inline void SuccessOrDie(otError aError) -{ - if (aError != OT_ERROR_NONE) - { - // fprintf(stderr, "Operation failed: %s\r\n", otThreadErrorToString(aError)); - exit(OT_EXIT_FAILURE); - } -} - -static inline void VerifyOrDie(bool aCondition) -{ - if (!aCondition) - { - exit(OT_EXIT_FAILURE); - } -} - namespace ot { static otError SpinelStatusToOtError(spinel_status_t aError) @@ -543,7 +526,7 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig) else { otLogCritPlat("Radio file '%s' not supported", aRadioFile); - exit(OT_EXIT_INVALID_ARGUMENTS); + ExitNow(error = OT_ERROR_INVALID_ARGS); } SuccessOrExit(error = SendReset()); @@ -581,7 +564,7 @@ otError RadioSpinel::CheckSpinelVersion(void) { otLogCritPlat("Spinel version mismatch - PosixApp:%d.%d, RCP:%d.%d", SPINEL_PROTOCOL_VERSION_THREAD_MAJOR, SPINEL_PROTOCOL_VERSION_THREAD_MINOR, versionMajor, versionMinor); - exit(OT_EXIT_INCOMPATIBLE_RADIO_SPINEL); + exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -623,7 +606,7 @@ otError RadioSpinel::CheckCapabilities(void) if (!supportsRawRadio) { otLogCritPlat("RCP capability list does not include support for radio/raw mode"); - exit(OT_EXIT_INCOMPATIBLE_RADIO_SPINEL); + exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -648,7 +631,7 @@ otError RadioSpinel::CheckRadioCapabilities(void) (mRadioCaps & OT_RADIO_CAPS_TRANSMIT_RETRIES) ? "yes" : "no", (mRadioCaps & OT_RADIO_CAPS_CSMA_BACKOFF) ? "yes" : "no"); - exit(OT_EXIT_INCOMPATIBLE_RADIO_SPINEL); + exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -858,7 +841,7 @@ void RadioSpinel::HandleValueIs(spinel_prop_key_t aKey, const uint8_t *aBuffer, mIsReady = true; // If RCP crashes/resets while radio was enabled, posix app exits. - VerifyOrDie(!IsEnabled()); + VerifyOrDie(!IsEnabled(), OT_EXIT_RADIO_SPINEL_RESET); } else {