diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 6b7fe1b21..49fd72732 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -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. * diff --git a/src/cli/README.md b/src/cli/README.md index 782cc47f9..fbaee2053 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -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. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 105f4a31f..a3a3b05c5 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -4002,6 +4002,29 @@ exit: return error; } +template <> otError Interpreter::Process(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 diff --git a/src/core/api/instance_api.cpp b/src/core/api/instance_api.cpp index 06db73ce3..ad2a89e16 100644 --- a/src/core/api/instance_api.cpp +++ b/src/core/api/instance_api.cpp @@ -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 diff --git a/src/core/common/instance.cpp b/src/core/common/instance.cpp index d0aebecd3..10d545d66 100644 --- a/src/core/common/instance.cpp +++ b/src/core/common/instance.cpp @@ -245,6 +245,7 @@ Instance::Instance(void) , mPowerCalibration(*this) #endif , mIsInitialized(false) + , mId(Random::NonCrypto::GetUint32()) { } diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index 8ce6eba09..3a5f4fa73 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -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); diff --git a/tests/scripts/expect/cli-misc.exp b/tests/scripts/expect/cli-misc.exp index 8b8b4d5ee..522a0faac 100755 --- a/tests/scripts/expect/cli-misc.exp +++ b/tests/scripts/expect/cli-misc.exp @@ -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"