diff --git a/examples/apps/cli/main.c b/examples/apps/cli/main.c index a893226d2..2354e2791 100644 --- a/examples/apps/cli/main.c +++ b/examples/apps/cli/main.c @@ -63,6 +63,8 @@ int main(int argc, char *argv[]) uint8_t *otInstanceBuffer = NULL; #endif +pseudo_reset: + PlatformInit(argc, argv); #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES @@ -86,17 +88,19 @@ int main(int argc, char *argv[]) otDiagInit(sInstance); #endif - while (1) + while (!PlatformPseudoResetWasRequested()) { otTaskletsProcess(sInstance); PlatformProcessDrivers(sInstance); } - // otInstanceFinalize(sInstance); + otInstanceFinalize(sInstance); #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES - // free(otInstanceBuffer); + free(otInstanceBuffer); #endif + goto pseudo_reset; + return 0; } diff --git a/examples/apps/ncp/main.c b/examples/apps/ncp/main.c index 47a4b5239..6f33a3ad0 100644 --- a/examples/apps/ncp/main.c +++ b/examples/apps/ncp/main.c @@ -62,6 +62,8 @@ int main(int argc, char *argv[]) uint8_t *otInstanceBuffer = NULL; #endif +pseudo_reset: + PlatformInit(argc, argv); #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES @@ -85,17 +87,19 @@ int main(int argc, char *argv[]) otDiagInit(sInstance); #endif - while (1) + while (!PlatformPseudoResetWasRequested()) { otTaskletsProcess(sInstance); PlatformProcessDrivers(sInstance); } - // otInstanceFinalize(sInstance); + otInstanceFinalize(sInstance); #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES - // free(otInstanceBuffer); + free(otInstanceBuffer); #endif + goto pseudo_reset; + return 0; } diff --git a/examples/platforms/cc2538/platform.c b/examples/platforms/cc2538/platform.c index feaf4db9f..cf469be0e 100644 --- a/examples/platforms/cc2538/platform.c +++ b/examples/platforms/cc2538/platform.c @@ -49,6 +49,11 @@ void PlatformInit(int argc, char *argv[]) (void)argv; } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformProcessDrivers(otInstance *aInstance) { sInstance = aInstance; diff --git a/examples/platforms/cc2650/platform.c b/examples/platforms/cc2650/platform.c index f2aa8c05b..9ee6dd013 100644 --- a/examples/platforms/cc2650/platform.c +++ b/examples/platforms/cc2650/platform.c @@ -57,6 +57,11 @@ void PlatformInit(int argc, char *argv[]) cc2650RadioInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + /** * Function documented in platform-cc2650.h */ diff --git a/examples/platforms/cc2652/platform.c b/examples/platforms/cc2652/platform.c index 553ecf68a..b1ca9ff69 100644 --- a/examples/platforms/cc2652/platform.c +++ b/examples/platforms/cc2652/platform.c @@ -71,6 +71,11 @@ void PlatformInit(int argc, char *argv[]) cc2652RadioInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + /** * Function documented in platform-cc2652.h */ diff --git a/examples/platforms/da15000/platform.c b/examples/platforms/da15000/platform.c index e9da6890e..b093b7376 100644 --- a/examples/platforms/da15000/platform.c +++ b/examples/platforms/da15000/platform.c @@ -138,6 +138,11 @@ void PlatformInit(int argc, char *argv[]) (void)argv; } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + static sys_clk_t ClkGet(void) { sys_clk_t clk = sysclk_RC16; diff --git a/examples/platforms/efr32/platform.c b/examples/platforms/efr32/platform.c index 93625e627..34ab559d6 100644 --- a/examples/platforms/efr32/platform.c +++ b/examples/platforms/efr32/platform.c @@ -66,6 +66,11 @@ void PlatformInit(int argc, char *argv[]) efr32RandomInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformDeinit(void) { efr32RadioDeinit(); diff --git a/examples/platforms/emsk/platform.c b/examples/platforms/emsk/platform.c index c15cb1811..7f3e8d681 100644 --- a/examples/platforms/emsk/platform.c +++ b/examples/platforms/emsk/platform.c @@ -53,6 +53,11 @@ void PlatformInit(int argc, char *argv[]) (void)argv; } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformProcessDrivers(otInstance *aInstance) { emskUartProcess(); diff --git a/examples/platforms/gp712/platform.c b/examples/platforms/gp712/platform.c index 3695f372c..9bfa6e4f2 100644 --- a/examples/platforms/gp712/platform.c +++ b/examples/platforms/gp712/platform.c @@ -76,6 +76,11 @@ void PlatformInit(int argc, char *argv[]) qorvoRadioInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformProcessDrivers(otInstance *aInstance) { if (localInstance == NULL) diff --git a/examples/platforms/kw41z/platform.c b/examples/platforms/kw41z/platform.c index 75629ca62..a4b76e505 100644 --- a/examples/platforms/kw41z/platform.c +++ b/examples/platforms/kw41z/platform.c @@ -100,6 +100,11 @@ void PlatformInit(int argc, char *argv[]) (void)argv; } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformDeinit(void) { } diff --git a/examples/platforms/nrf52840/alarm.c b/examples/platforms/nrf52840/alarm.c index 7e96e7d1e..0abf9d2d7 100644 --- a/examples/platforms/nrf52840/alarm.c +++ b/examples/platforms/nrf52840/alarm.c @@ -339,6 +339,9 @@ static void AlarmStop(AlarmIndex aIndex) void nrf5AlarmInit(void) { memset(sTimerData, 0, sizeof(sTimerData)); + sOverflowCounter = 0; + sMutex = 0; + sTimeOffset = 0; // Setup low frequency clock. nrf_drv_clock_lfclk_request(NULL); diff --git a/examples/platforms/nrf52840/misc.c b/examples/platforms/nrf52840/misc.c index d8b404372..e19e4da41 100644 --- a/examples/platforms/nrf52840/misc.c +++ b/examples/platforms/nrf52840/misc.c @@ -40,6 +40,8 @@ static uint32_t sResetReason; +bool gPlatformPseudoResetWasRequested; + __WEAK void nrf5CryptoInit(void) { // This function is defined as weak so it could be overridden with external implementation. @@ -69,8 +71,12 @@ void nrf5MiscDeinit(void) void otPlatReset(otInstance *aInstance) { (void)aInstance; - +#if OPENTHREAD_PLATFORM_USE_PSEUDO_RESET + gPlatformPseudoResetWasRequested = true; + sResetReason = POWER_RESETREAS_SREQ_Msk; +#else // if OPENTHREAD_PLATFORM_USE_PSEUDO_RESET NVIC_SystemReset(); +#endif // else OPENTHREAD_PLATFORM_USE_PSEUDO_RESET } otPlatResetReason otPlatGetResetReason(otInstance *aInstance) diff --git a/examples/platforms/nrf52840/platform-config.h b/examples/platforms/nrf52840/platform-config.h index f619a5c9f..cb6bedf60 100644 --- a/examples/platforms/nrf52840/platform-config.h +++ b/examples/platforms/nrf52840/platform-config.h @@ -347,6 +347,16 @@ #define USB_CDC_AS_SERIAL_TRANSPORT 0 #endif +/** + * @def OPENTHREAD_PLATFORM_USE_PSEUDO_RESET + * + * Reset the application, not the chip, when a software reset is requested. + * via `otPlatReset()`. + */ +#ifndef OPENTHREAD_PLATFORM_USE_PSEUDO_RESET +#define OPENTHREAD_PLATFORM_USE_PSEUDO_RESET USB_CDC_AS_SERIAL_TRANSPORT +#endif + /******************************************************************************* * @section Radio driver configuration. ******************************************************************************/ diff --git a/examples/platforms/nrf52840/platform.c b/examples/platforms/nrf52840/platform.c index 004094725..7dfe60bd8 100644 --- a/examples/platforms/nrf52840/platform.c +++ b/examples/platforms/nrf52840/platform.c @@ -51,6 +51,18 @@ void __cxa_pure_virtual(void) void PlatformInit(int argc, char *argv[]) { + extern bool gPlatformPseudoResetWasRequested; + + if (gPlatformPseudoResetWasRequested) + { + nrf5AlarmDeinit(); + nrf5AlarmInit(); + + gPlatformPseudoResetWasRequested = false; + + return; + } + (void)argc; (void)argv; @@ -87,6 +99,12 @@ void PlatformDeinit(void) #endif } +bool PlatformPseudoResetWasRequested(void) +{ + extern bool gPlatformPseudoResetWasRequested; + return gPlatformPseudoResetWasRequested; +} + void PlatformProcessDrivers(otInstance *aInstance) { nrf5AlarmProcess(aInstance); diff --git a/examples/platforms/platform.h b/examples/platforms/platform.h index 6a955009f..c1378526b 100644 --- a/examples/platforms/platform.h +++ b/examples/platforms/platform.h @@ -53,6 +53,14 @@ void PlatformInit(int argc, char *argv[]); */ void PlatformDeinit(void); +/** + * This function returns true is a pseudo-reset was requested. + * In such a case, the main loop should shut down and re-initialize + * the OpenThread instance. + * + */ +bool PlatformPseudoResetWasRequested(void); + /** * This function performs all platform-specific processing. * diff --git a/examples/platforms/posix/misc.c b/examples/platforms/posix/misc.c index c076e95ac..ff54095d3 100644 --- a/examples/platforms/posix/misc.c +++ b/examples/platforms/posix/misc.c @@ -42,9 +42,20 @@ extern int gArgumentsCount; extern char **gArguments; #endif +static otPlatResetReason sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON; +bool gPlatformPseudoResetWasRequested; + void otPlatReset(otInstance *aInstance) { -#ifndef _WIN32 +#if _WIN32 +// This function does nothing on the Windows platform. + +#elif OPENTHREAD_PLATFORM_USE_PSEUDO_RESET // if _WIN32 + gPlatformPseudoResetWasRequested = true; + sPlatResetReason = OT_PLAT_RESET_REASON_SOFTWARE; + +#else // elif OPENTHREAD_PLATFORM_USE_PSEUDO_RESET + // Restart the process using execvp. char *argv[gArgumentsCount + 1]; for (int i = 0; i < gArgumentsCount; ++i) @@ -62,16 +73,16 @@ void otPlatReset(otInstance *aInstance) execvp(argv[0], argv); perror("reset failed"); exit(EXIT_FAILURE); -#else -// This function does nothing on the Windows platform. -#endif // _WIN32 + +#endif // else OPENTHREAD_PLATFORM_USE_PSEUDO_RESET + (void)aInstance; } otPlatResetReason otPlatGetResetReason(otInstance *aInstance) { (void)aInstance; - return OT_PLAT_RESET_REASON_POWER_ON; + return sPlatResetReason; } void otPlatWakeHost(void) diff --git a/examples/platforms/posix/platform.c b/examples/platforms/posix/platform.c index 72f3a9cde..b34e06abd 100644 --- a/examples/platforms/posix/platform.c +++ b/examples/platforms/posix/platform.c @@ -55,6 +55,8 @@ uint32_t NODE_ID = 1; uint32_t WELLKNOWN_NODE_ID = 34; +extern bool gPlatformPseudoResetWasRequested; + #ifndef _WIN32 int gArgumentsCount = 0; char **gArguments = NULL; @@ -64,6 +66,12 @@ void PlatformInit(int argc, char *argv[]) { char *endptr; + if (gPlatformPseudoResetWasRequested) + { + gPlatformPseudoResetWasRequested = false; + return; + } + if (argc != 2) { exit(EXIT_FAILURE); @@ -90,6 +98,11 @@ void PlatformInit(int argc, char *argv[]) platformRandomInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return gPlatformPseudoResetWasRequested; +} + void PlatformDeinit(void) { platformRadioDeinit(); diff --git a/examples/platforms/posix/sim/platform-sim.c b/examples/platforms/posix/sim/platform-sim.c index 049062db2..99337ed01 100644 --- a/examples/platforms/posix/sim/platform-sim.c +++ b/examples/platforms/posix/sim/platform-sim.c @@ -52,6 +52,8 @@ uint32_t NODE_ID = 1; uint32_t WELLKNOWN_NODE_ID = 34; +extern bool gPlatformPseudoResetWasRequested; + int gArgumentsCount = 0; char **gArguments = NULL; @@ -170,6 +172,12 @@ void PlatformInit(int argc, char *argv[]) { char *endptr; + if (gPlatformPseudoResetWasRequested) + { + gPlatformPseudoResetWasRequested = false; + return; + } + if (argc != 2) { exit(EXIT_FAILURE); @@ -196,6 +204,11 @@ void PlatformInit(int argc, char *argv[]) platformRandomInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return gPlatformPseudoResetWasRequested; +} + void PlatformDeinit(void) { close(sSockFd); diff --git a/examples/platforms/samr21/platform.c b/examples/platforms/samr21/platform.c index c9d143df7..4890990e7 100644 --- a/examples/platforms/samr21/platform.c +++ b/examples/platforms/samr21/platform.c @@ -175,6 +175,11 @@ void PlatformInit(int argc, char *argv[]) samr21RadioInit(); } +bool PlatformPseudoResetWasRequested(void) +{ + return false; +} + void PlatformDeinit(void) { }