[core] add the instance id (#9055)

It is difficult for testers to know whether the stack has been
automatically restarted in the background. This commit adds an
instance id to indicate whether the stack has been restarted.
The instance id is set to a random value when the OpenThread
instance is constructed and its value will not change after
initialization.
This commit is contained in:
Zhanglong Xia
2023-05-18 15:40:45 -07:00
committed by GitHub
parent b420a860ab
commit 853f6eab0b
7 changed files with 67 additions and 1 deletions
+12 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (324)
#define OPENTHREAD_API_VERSION (325)
/**
* @addtogroup api-instance
@@ -102,6 +102,17 @@ otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize);
*/
otInstance *otInstanceInitSingle(void);
/**
* Gets the instance identifier.
*
* The instance identifier is set to a random value when the instance is constructed, and then its value will not
* change after initialization.
*
* @returns The instance identifier.
*
*/
uint32_t otInstanceGetId(otInstance *aInstance);
/**
* This function indicates whether or not the instance is valid/initialized.
*
+11
View File
@@ -56,6 +56,7 @@ Done
- [fem](#fem)
- [history](README_HISTORY.md)
- [ifconfig](#ifconfig)
- [instanceid](#instanceid)
- [ipaddr](#ipaddr)
- [ipmaddr](#ipmaddr)
- [joiner](README_JOINER.md)
@@ -1491,6 +1492,16 @@ Bring down the IPv6 interface.
Done
```
### instanceid
Show OpenThread instance identifier.
```bash
> instanceid
468697314
Done
```
### ipaddr
List all IPv6 addresses assigned to the Thread interface.
+24
View File
@@ -4002,6 +4002,29 @@ exit:
return error;
}
template <> otError Interpreter::Process<Cmd("instanceid")>(Arg aArgs[])
{
otError error = OT_ERROR_INVALID_ARGS;
/**
* @cli instanceid
* @code
* instanceid
* 468697314
* Done
* @endcode
* @par api_copy
* #otInstanceGetId
*/
if (aArgs[0].IsEmpty())
{
OutputLine("%lu", ToUlong(otInstanceGetId(GetInstancePtr())));
error = OT_ERROR_NONE;
}
return error;
}
const char *Interpreter::AddressOriginToString(uint8_t aOrigin)
{
static const char *const kOriginStrings[4] = {
@@ -8605,6 +8628,7 @@ otError Interpreter::ProcessCommand(Arg aArgs[])
CmdEntry("history"),
#endif
CmdEntry("ifconfig"),
CmdEntry("instanceid"),
CmdEntry("ipaddr"),
CmdEntry("ipmaddr"),
#if OPENTHREAD_CONFIG_JOINER_ENABLE
+2
View File
@@ -70,6 +70,8 @@ otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize)
otInstance *otInstanceInitSingle(void) { return &Instance::InitSingle(); }
#endif // #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
uint32_t otInstanceGetId(otInstance *aInstance) { return AsCoreType(aInstance).GetId(); }
bool otInstanceIsInitialized(otInstance *aInstance)
{
#if OPENTHREAD_MTD || OPENTHREAD_FTD
+1
View File
@@ -245,6 +245,7 @@ Instance::Instance(void)
, mPowerCalibration(*this)
#endif
, mIsInitialized(false)
, mId(Random::NonCrypto::GetUint32())
{
}
+13
View File
@@ -207,6 +207,17 @@ public:
static Instance &Get(void);
#endif
/**
* Gets the instance identifier.
*
* The instance identifier is set to a random value when the instance is constructed, and then its value will not
* change after initialization.
*
* @returns The instance identifier.
*
*/
uint32_t GetId(void) const { return mId; }
/**
* This method indicates whether or not the instance is valid/initialized and not yet finalized.
*
@@ -645,6 +656,8 @@ private:
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE && (OPENTHREAD_FTD || OPENTHREAD_MTD)
static bool sDnsNameCompressionEnabled;
#endif
uint32_t mId;
};
DefineCoreType(otInstance, Instance);
+4
View File
@@ -86,6 +86,10 @@ send "ifconfig\n"
expect "up"
expect_line "Done"
send "instanceid\n"
expect -re {\d+}
expect_line "Done"
send "ipaddr add ::\n"
expect_line "Done"
send "ipaddr del ::\n"