mirror of
https://github.com/espressif/openthread.git
synced 2026-08-21 09:59:52 +00:00
[examples] Introducing pseudo resets (#2621)
Pseudo resets allow for `otPlatReset()` to reset the OpenThread state without resetting the chip. This is important if the SoC is presenting itself as a USB device. The build option `OPENTHREAD_PLATFORM_USE_PSEUDO_RESET` can be used to force this feature to be enabled or disabled by setting it to `1` or `0` accordingly. Otherwise it will be set to a platform-specified default value.
This commit is contained in:
committed by
Jonathan Hui
parent
1545b0d658
commit
ccb354fb31
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
(void)argv;
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformProcessDrivers(otInstance *aInstance)
|
||||
{
|
||||
sInstance = aInstance;
|
||||
|
||||
@@ -57,6 +57,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
cc2650RadioInit();
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function documented in platform-cc2650.h
|
||||
*/
|
||||
|
||||
@@ -71,6 +71,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
cc2652RadioInit();
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function documented in platform-cc2652.h
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -66,6 +66,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
efr32RandomInit();
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformDeinit(void)
|
||||
{
|
||||
efr32RadioDeinit();
|
||||
|
||||
@@ -53,6 +53,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
(void)argv;
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformProcessDrivers(otInstance *aInstance)
|
||||
{
|
||||
emskUartProcess();
|
||||
|
||||
@@ -76,6 +76,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
qorvoRadioInit();
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformProcessDrivers(otInstance *aInstance)
|
||||
{
|
||||
if (localInstance == NULL)
|
||||
|
||||
@@ -100,6 +100,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
(void)argv;
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformDeinit(void)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
******************************************************************************/
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -175,6 +175,11 @@ void PlatformInit(int argc, char *argv[])
|
||||
samr21RadioInit();
|
||||
}
|
||||
|
||||
bool PlatformPseudoResetWasRequested(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlatformDeinit(void)
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user