diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 23f243824..403516236 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -80,6 +80,33 @@ struct Event uint8_t mData[OT_EVENT_DATA_MAX_SIZE]; } OT_TOOL_PACKED_END; +/** + * This enumeration represents exit codes used when OpenThread exits. + * + */ +enum +{ + /** + * Success. + */ + OT_EXIT_SUCCESS = 0, + + /** + * Generic failure. + */ + OT_EXIT_FAILURE = 1, + + /** + * Invalid arguments. + */ + OT_EXIT_INVALID_ARGUMENTS = 2, + + /** + * Incompatible radio spinel. + */ + OT_EXIT_INCOMPATIBLE_RADIO_SPINEL = 3, +}; + /** * Unique node ID. * diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index 5a68e7997..398253782 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -146,7 +146,7 @@ static inline void SuccessOrDie(otError aError) if (aError != OT_ERROR_NONE) { // fprintf(stderr, "Operation failed: %s\r\n", otThreadErrorToString(aError)); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } } @@ -287,7 +287,7 @@ static int ForkPty(const char *aCommand, const char *aArguments) execl(getenv("SHELL"), getenv("SHELL"), "-c", cmd, NULL); perror("open pty failed"); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); } else { @@ -353,7 +353,7 @@ static int OpenFile(const char *aFile, const char *aConfig) default: // not supported assert(false); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -367,7 +367,7 @@ static int OpenFile(const char *aFile, const char *aConfig) break; default: assert(false); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -455,7 +455,7 @@ static int OpenFile(const char *aFile, const char *aConfig) #endif default: assert(false); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -467,7 +467,7 @@ static int OpenFile(const char *aFile, const char *aConfig) exit: if (rval != 0) { - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } return fd; @@ -563,7 +563,7 @@ void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig) otLogCritPlat(mInstance, "Spinel version mismatch - PosixApp:%d.%d, RCP:%d.%d", SPINEL_PROTOCOL_VERSION_THREAD_MAJOR, SPINEL_PROTOCOL_VERSION_THREAD_MINOR, versionMajor, versionMinor); - ExitNow(error = OT_ERROR_FAILED); + exit(OT_EXIT_INCOMPATIBLE_RADIO_SPINEL); } } @@ -1248,12 +1248,12 @@ otError RadioSpinel::WaitResponse(void) else if (FD_ISSET(mSockFd, &error_fds)) { fprintf(stderr, "NCP error\r\n"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } else { assert(false); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } } else if (rval == 0) @@ -1265,7 +1265,7 @@ otError RadioSpinel::WaitResponse(void) else if (errno != EINTR) { perror("wait response"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } #endif // OPENTHREAD_POSIX_VIRTUAL_TIME diff --git a/src/posix/platform/settings.cpp b/src/posix/platform/settings.cpp index c0754b6dd..f0de65719 100644 --- a/src/posix/platform/settings.cpp +++ b/src/posix/platform/settings.cpp @@ -44,14 +44,14 @@ #include "common/code_utils.hpp" -#define VerifyOrDie(aCondition) \ - do \ - { \ - if (!(aCondition)) \ - { \ - perror(__func__); \ - exit(EXIT_FAILURE); \ - } \ +#define VerifyOrDie(aCondition) \ + do \ + { \ + if (!(aCondition)) \ + { \ + perror(__func__); \ + exit(OT_EXIT_FAILURE); \ + } \ } while (false) static const size_t kMaxFileNameSize = sizeof(OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH) + 32; diff --git a/src/posix/platform/sim.c b/src/posix/platform/sim.c index f496f0819..a5301e110 100644 --- a/src/posix/platform/sim.c +++ b/src/posix/platform/sim.c @@ -67,7 +67,7 @@ void otSimInit(void) if (*endptr != '\0') { fprintf(stderr, "Invalid PORT_OFFSET: %s\n", offset); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); } sPortOffset *= kWellKnownNodeId; @@ -84,13 +84,13 @@ void otSimInit(void) if (sSockFd == -1) { perror("socket"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } if (bind(sSockFd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) { perror("bind"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } // fprintf(stderr, "[%s] sim fd is %d\r\n", getenv("NODE_ID"), sSockFd); } @@ -119,7 +119,7 @@ static void otSimSendEvent(struct Event *aEvent, size_t aLength) if (rval < 0) { perror("sendto"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } } @@ -130,7 +130,7 @@ void otSimReceiveEvent(struct Event *aEvent) if (rval < 0 || (uint16_t)rval < offsetof(struct Event, mData)) { perror("recvfrom"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } sNow += aEvent->mDelay; diff --git a/src/posix/platform/spi-stubs.c b/src/posix/platform/spi-stubs.c index a56184bbc..36a65510b 100644 --- a/src/posix/platform/spi-stubs.c +++ b/src/posix/platform/spi-stubs.c @@ -54,7 +54,7 @@ otError otPlatSpiSlaveEnable(otPlatSpiSlaveTransactionCompleteCallback aComplete (void)aContext; fprintf(stderr, "\nNo SPI support for posix platform."); - exit(0); + exit(OT_EXIT_FAILURE); return OT_ERROR_NOT_IMPLEMENTED; } diff --git a/src/posix/platform/system.c b/src/posix/platform/system.c index 7af1e327c..acfe5ae0b 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.c @@ -52,7 +52,6 @@ extern bool gPlatformPseudoResetWasRequested; static void PrintUsage(const char *aArg0) { fprintf(stderr, "Syntax:\n %s [-s TimeSpeedUpFactor] {NodeId|Device DeviceConfig|Command CommandArgs}\n", aArg0); - exit(EXIT_FAILURE); } void otSysInit(int aArgCount, char *aArgVector[]) @@ -72,6 +71,7 @@ void otSysInit(int aArgCount, char *aArgVector[]) if (aArgCount < 2) { PrintUsage(aArgVector[0]); + exit(OT_EXIT_INVALID_ARGUMENTS); } i = 1; @@ -83,7 +83,7 @@ void otSysInit(int aArgCount, char *aArgVector[]) if (*endptr != '\0' || speedUpFactor == 0) { fprintf(stderr, "Invalid value for TimerSpeedUpFactor: %s\n", aArgVector[i]); - exit(EXIT_FAILURE); + exit(OT_EXIT_INVALID_ARGUMENTS); } ++i; } @@ -91,6 +91,7 @@ void otSysInit(int aArgCount, char *aArgVector[]) if (i >= aArgCount) { PrintUsage(aArgVector[0]); + exit(OT_EXIT_INVALID_ARGUMENTS); } radioFile = aArgVector[i]; @@ -226,7 +227,7 @@ void otSysProcessDrivers(otInstance *aInstance) if ((rval < 0) && (errno != EINTR)) { perror("select"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } #if OPENTHREAD_POSIX_VIRTUAL_TIME diff --git a/src/posix/platform/uart.c b/src/posix/platform/uart.c index 69ba8ebb8..897286108 100644 --- a/src/posix/platform/uart.c +++ b/src/posix/platform/uart.c @@ -225,13 +225,13 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co if (FD_ISSET(s_in_fd, aErrorFdSet)) { perror("s_in_fd"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } if (FD_ISSET(s_out_fd, aErrorFdSet)) { perror("s_out_fd"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } if (FD_ISSET(s_in_fd, aReadFdSet)) @@ -245,12 +245,12 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co else if (rval < 0) { perror("UART read"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } else { fprintf(stderr, "UART ended\r\n"); - exit(0); + exit(OT_EXIT_SUCCESS); } } @@ -261,7 +261,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co if (rval <= 0) { perror("UART write"); - exit(EXIT_FAILURE); + exit(OT_EXIT_FAILURE); } s_write_buffer += (uint16_t)rval;