From 7b12b9fc989ec82bd3b65ca2cbbd5d27d9469ec9 Mon Sep 17 00:00:00 2001 From: Kangping Date: Thu, 9 May 2024 02:25:00 +0800 Subject: [PATCH] [api] remove the `XpanId` and `PanId` structs (#264) It's an overkill to define a dedicated class/struct for a simple `uint16_t` just for pretty print to json in HEX string. --- include/commissioner/error.hpp | 11 +- include/commissioner/network_data.hpp | 56 +------ src/app/cli/interpreter.cpp | 87 +++++------ src/app/cli/interpreter.hpp | 4 +- src/app/cli/interpreter_test.cpp | 155 +++++++++----------- src/app/cli/job.cpp | 6 +- src/app/cli/job.hpp | 2 +- src/app/cli/job_manager.cpp | 64 ++++---- src/app/cli/job_manager.hpp | 23 ++- src/app/cli/job_manager_test.cpp | 54 +++---- src/app/commissioner_app.cpp | 8 +- src/app/commissioner_app.hpp | 4 +- src/app/commissioner_app_dummy.cpp | 4 +- src/app/commissioner_app_mock.hpp | 4 +- src/app/json.cpp | 89 +++++------ src/app/ps/CMakeLists.txt | 6 + src/app/ps/persistent_storage_json.cpp | 10 +- src/app/ps/persistent_storage_json_test.cpp | 35 ++--- src/app/ps/registry.cpp | 66 +++++---- src/app/ps/registry.hpp | 16 +- src/app/ps/registry_entries.cpp | 29 ++-- src/app/ps/registry_entries.hpp | 14 +- src/app/ps/registry_test.cpp | 10 +- src/common/error.cpp | 2 +- src/common/utils.hpp | 28 ++++ src/common/utils_test.cpp | 23 +++ src/java/commissioner.i | 8 +- src/library/commissioner_impl.cpp | 6 +- src/library/network_data.cpp | 117 --------------- tests/integration/test_domain_syntax.sh | 26 ++-- 30 files changed, 415 insertions(+), 552 deletions(-) diff --git a/include/commissioner/error.hpp b/include/commissioner/error.hpp index 5771eed55..9b5dba768 100644 --- a/include/commissioner/error.hpp +++ b/include/commissioner/error.hpp @@ -264,14 +264,21 @@ inline bool operator!=(const ErrorCode &aErrorCode, const Error &aError) return !(aErrorCode == aError); } +std::string ErrorCodeToString(ErrorCode code); + /** * Allows pretty-print in unit tests. * * See https://google.github.io/googletest/advanced.html#teaching-googletest-how-to-print-your-values */ -inline void PrintTo(const Error &error, std::ostream *os) +inline void PrintTo(const Error &aError, std::ostream *os) +{ + *os << aError.ToString(); +} + +inline void PrintTo(ErrorCode aErrorCode, std::ostream *os) { - *os << error.ToString(); + *os << ErrorCodeToString(aErrorCode); } } // namespace commissioner diff --git a/include/commissioner/network_data.hpp b/include/commissioner/network_data.hpp index fb488b6a4..bd4962571 100644 --- a/include/commissioner/network_data.hpp +++ b/include/commissioner/network_data.hpp @@ -52,36 +52,6 @@ static constexpr uint8_t kMlrStatusNoResources = 4; static constexpr uint8_t kMlrStatusNotPrimary = 5; static constexpr uint8_t kMlrStatusFailure = 6; -/** - * Extended PAN Id wrapper - */ -struct XpanId -{ - static constexpr uint64_t kEmptyXpanId = 0; - - uint64_t mValue; - - XpanId(uint64_t val); - - XpanId(); - - std::string str() const; - - bool operator==(const XpanId &aOther) const; - - bool operator!=(const XpanId &aOther) const; - bool operator<(const XpanId &aOther) const; - - explicit operator std::string() const; - - /** - * Decodes hexadecimal string. - */ - Error FromHex(const std::string &aInput); -}; - -typedef std::vector XpanIdArray; - /** * @brief The Commissioner Dataset of the Thread Network Data. * @@ -249,26 +219,6 @@ enum SecurityPolicyFlags kSecurityPolicyMask_VR = 1 << 5 | 1 << 6 | 1 << 7, /// Protocol version }; -/** - * A PAN identifier. - */ -struct PanId -{ - static constexpr uint64_t kEmptyPanId = 0; - - uint16_t mValue; - explicit PanId(uint16_t aValue); - PanId(const PanId &aOther) = default; - PanId(); - - PanId &operator=(const PanId &aValue) = default; - PanId &operator=(uint16_t aValue); - explicit operator uint16_t() const; - explicit operator std::string() const; - - Error FromHex(const std::string &aInput); -}; - /** * @brief The Active Operational Dataset of the Thread Network Data. * @@ -283,13 +233,13 @@ struct PanId struct ActiveOperationalDataset { Timestamp mActiveTimestamp; + ByteArray mExtendedPanId; + uint16_t mPanId; + std::string mNetworkName; Channel mChannel; ChannelMask mChannelMask; - XpanId mExtendedPanId; ByteArray mMeshLocalPrefix; ByteArray mNetworkMasterKey; - std::string mNetworkName; - PanId mPanId; ByteArray mPSKc; SecurityPolicy mSecurityPolicy; diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index b81124879..e0549f81c 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -123,6 +123,8 @@ namespace ot { namespace commissioner { using ot::commissioner::persistent_storage::Network; +using ot::commissioner::utils::Hex; +using ot::commissioner::utils::ParseInteger; namespace { /** @@ -177,10 +179,9 @@ Interpreter::NetworkSelectionComparator::~NetworkSelectionComparator() Network nwk; RegistryStatus status = mInterpreter.mRegistry->GetCurrentNetwork(nwk); - if (status == RegistryStatus::kSuccess && mStartWith.mValue != nwk.mXpan.mValue) + if (status == RegistryStatus::kSuccess && mStartWith != nwk.mXpan) { - Console::Write(nwk.mXpan.mValue == XpanId::kEmptyXpanId ? WARN_NETWORK_SELECTION_DROPPED - : WARN_NETWORK_SELECTION_CHANGED, + Console::Write(nwk.mXpan == 0 ? WARN_NETWORK_SELECTION_DROPPED : WARN_NETWORK_SELECTION_CHANGED, Console::Color::kYellow); } } @@ -351,23 +352,6 @@ template static std::string ToHex(T aInteger) return "0x" + utils::Hex(utils::Encode(aInteger)); }; -template static Error ParseInteger(T &aInteger, const std::string &aStr) -{ - Error error; - uint64_t integer; - char *endPtr = nullptr; - - integer = strtoull(aStr.c_str(), &endPtr, 0); - - VerifyOrExit(endPtr != nullptr && endPtr > aStr.c_str(), - error = ERROR_INVALID_ARGS("{} is not a valid integer", aStr)); - - aInteger = integer; - -exit: - return error; -} - static inline std::string ToLower(const std::string &aStr) { return utils::ToLower(aStr); @@ -404,11 +388,11 @@ Error Interpreter::UpdateNetworkSelectionInfo(bool onStart /*=false*/) VerifyOrExit(mRegistry->GetCurrentNetwork(nwk) == Registry::Status::kSuccess, error = ERROR_REGISTRY_ERROR(RUNTIME_CUR_NETWORK_FAILED)); - if (onStart && nwk.mXpan.mValue != XpanId::kEmptyXpanId) + if (onStart && nwk.mXpan != 0) { Console::Write(fmt::format(FMT_STRING("Network selection recalled from previous session.\n" "Restored to [{}:'{}']"), - nwk.mXpan.str(), nwk.mName), + utils::Hex(nwk.mXpan), nwk.mName), Console::Color::kGreen); } exit: @@ -549,7 +533,7 @@ Interpreter::Value Interpreter::Eval(const Expression &aExpr) if (mContext.mNwkAliases.size() > 0 || mContext.mDomAliases.size() > 0) { - XpanIdArray nids; + std::vector nids; SuccessOrExit(value = ValidateMultiNetworkSyntax(retExpr, nids)); if (IsMultiJob(retExpr)) // asynchronous processing required @@ -574,19 +558,19 @@ Interpreter::Value Interpreter::Eval(const Expression &aExpr) // handle single command using selected network if (mContext.mImportFiles.size() > 0) { - XpanId xpan = XpanId(); + uint64_t xpan = 0; if (mContext.mNwkAliases.empty()) { auto result = mRegistry->GetCurrentNetworkXpan(xpan); if (result != Registry::Status::kSuccess) { - xpan = XpanId(); + xpan = 0; } } else { - XpanIdArray nwks; - StringArray unresolved; + std::vector nwks; + StringArray unresolved; VerifyOrExit(mRegistry->GetNetworkXpansByAliases(mContext.mNwkAliases, nwks, unresolved) == Registry::Status::kSuccess, value = ERROR_INVALID_ARGS("Failed to resolve network alias for import")); @@ -624,7 +608,7 @@ bool Interpreter::IsInactiveCommissionerAllowed(const Expression &aExpr) return IsFeatureSupported(mInactiveCommissionerExecution, aExpr); } -Interpreter::Value Interpreter::ValidateMultiNetworkSyntax(const Expression &aExpr, XpanIdArray &aNids) +Interpreter::Value Interpreter::ValidateMultiNetworkSyntax(const Expression &aExpr, std::vector &aNids) { Error error; bool supported; @@ -846,7 +830,7 @@ void Interpreter::PrintOrExport(const Value &aValue) void Interpreter::PrintNetworkMessage(uint64_t aNid, std::string aMessage, Console::Color aColor) { - std::string nidHex = std::string(XpanId(aNid)); + std::string nidHex = utils::Hex(aNid); PrintNetworkMessage(nidHex, aMessage, aColor); } @@ -970,7 +954,7 @@ Interpreter::Value Interpreter::ProcessStart(const Expression &aExpr) case 1: { // starting currently selected network - XpanId nid; + uint64_t nid; RegistryStatus status = mRegistry->GetCurrentNetworkXpan(nid); VerifyOrExit(status == RegistryStatus::kSuccess, value = ERROR_REGISTRY_ERROR("getting selected network failed")); @@ -1191,7 +1175,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) } else { - PrintNetworkMessage(nwk.mXpan.mValue, RUNTIME_LOOKUP_FAILED, COLOR_ALIAS_FAILED); + PrintNetworkMessage(nwk.mXpan, RUNTIME_LOOKUP_FAILED, COLOR_ALIAS_FAILED); } } } @@ -1547,8 +1531,8 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) ExitNow(value = ERROR_CANCELLED("Scan cancelled by user")); } - XpanIdArray xpans; - StringArray unresolved; + std::vector xpans; + StringArray unresolved; if (!mContext.mNwkAliases.empty()) { VerifyOrExit(mRegistry->GetNetworkXpansByAliases(mContext.mNwkAliases, xpans, unresolved) == @@ -1580,8 +1564,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr) (agentOrError.mBorderAgent.mDomainName == mContext.mDomAliases[0])) || (!mContext.mNwkAliases.empty() && (agentOrError.mBorderAgent.mPresentFlags & BorderAgent::kExtendedPanIdBit) && - (std::find(xpans.begin(), xpans.end(), XpanId(agentOrError.mBorderAgent.mExtendedPanId)) != - xpans.end()))) + (std::find(xpans.begin(), xpans.end(), agentOrError.mBorderAgent.mExtendedPanId) != xpans.end()))) { baJson.push_back(ba); } @@ -1686,9 +1669,9 @@ Interpreter::Value Interpreter::ProcessNetwork(const Expression &aExpr) } else { - StringArray aliases = {aExpr[2]}; - XpanIdArray xpans; - StringArray unresolved; + StringArray aliases = {aExpr[2]}; + std::vector xpans; + StringArray unresolved; VerifyOrExit(mRegistry->GetNetworkXpansByAliases(aliases, xpans, unresolved) == RegistryStatus::kSuccess, value = ERROR_REGISTRY_ERROR("Failed to resolve extended PAN Id for network '{}'", aExpr[2])); VerifyOrExit(xpans.size() == 1, value = ERROR_IO_ERROR("Detected {} networks instead of 1 for alias '{}'", @@ -1721,8 +1704,8 @@ Interpreter::Value Interpreter::ProcessNetwork(const Expression &aExpr) nwkData += '/'; } nwkData += nwk.mName; - json[nwk.mXpan.str()] = nwkData; - value = json.dump(JSON_INDENT_DEFAULT); + json[utils::Hex(nwk.mXpan)] = nwkData; + value = json.dump(JSON_INDENT_DEFAULT); } } else @@ -2115,7 +2098,7 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss } else if (CaseInsensitiveEqual(aExpr[2], "panid")) { - PanId panid; + uint16_t panid; if (isSet) { uint32_t delay; @@ -2127,7 +2110,7 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss else { SuccessOrExit(value = aCommissioner->GetPanId(panid)); - value = std::string(panid); + value = Hex(panid); } } else if (CaseInsensitiveEqual(aExpr[2], "pskc")) @@ -2166,7 +2149,7 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss ActiveOperationalDataset dataset; StringArray nwkAliases; StringArray unresolved; - XpanIdArray xpans; + std::vector xpans; if (isSet) { @@ -2186,13 +2169,13 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss // Get network object for the opdataset object if ((dataset.mPresentFlags & ActiveOperationalDataset::kExtendedPanIdBit) != 0) { - xpans.push_back(dataset.mExtendedPanId); + xpans.push_back(utils::Decode(dataset.mExtendedPanId)); } else { if ((dataset.mPresentFlags & ActiveOperationalDataset::kPanIdBit) != 0) { - nwkAliases.push_back(fmt::format(FMT_STRING("0x{:04X}"), dataset.mPanId.mValue)); + nwkAliases.push_back(Hex(dataset.mPanId)); } else if ((dataset.mPresentFlags & ActiveOperationalDataset::kNetworkNameBit) != 0) { @@ -2214,8 +2197,9 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss } if (mRegistry->GetNetworkByXpan(xpans[0], nwk) != RegistryStatus::kSuccess) { - Console::Write(fmt::format(FMT_STRING("Failed to find network record by XPAN '{}'"), xpans[0].str()), - Console::Color::kYellow); + Console::Write( + fmt::format(FMT_STRING("Failed to find network record by XPAN '{}'"), utils::Hex(xpans[0])), + Console::Color::kYellow); break; /// @todo It is possible that network XPAN ID /// might have change since the last sync /// so maybe it is worth to look up for @@ -2227,13 +2211,14 @@ Interpreter::Value Interpreter::ProcessOpDatasetJob(CommissionerAppPtr &aCommiss #define AODS_FIELD_IF_IS_SET(field, defaultValue) \ (((dataset.mPresentFlags & ActiveOperationalDataset::k##field##Bit) == 0) ? (defaultValue) : (dataset.m##field)) - nwk.mName = AODS_FIELD_IF_IS_SET(NetworkName, ""); - nwk.mXpan = AODS_FIELD_IF_IS_SET(ExtendedPanId, XpanId{}); + nwk.mName = AODS_FIELD_IF_IS_SET(NetworkName, ""); + nwk.mXpan = + utils::Decode(AODS_FIELD_IF_IS_SET(ExtendedPanId, ByteArray(kExtendedPanIdLength, 0))); nwk.mChannel = AODS_FIELD_IF_IS_SET(Channel, (Channel{0, 0})).mNumber; - nwk.mPan = AODS_FIELD_IF_IS_SET(PanId, PanId{}); + nwk.mPan = AODS_FIELD_IF_IS_SET(PanId, 0); if ((dataset.mPresentFlags & ActiveOperationalDataset::kPanIdBit) == 0) { - nwk.mPan = PanId::kEmptyPanId; + nwk.mPan = 0; } else { diff --git a/src/app/cli/interpreter.hpp b/src/app/cli/interpreter.hpp index 5cd2f8164..c4e0d6904 100644 --- a/src/app/cli/interpreter.hpp +++ b/src/app/cli/interpreter.hpp @@ -84,7 +84,7 @@ class Interpreter struct NetworkSelectionComparator { const Interpreter &mInterpreter; - XpanId mStartWith; + uint64_t mStartWith; bool mSuccess; NetworkSelectionComparator(const Interpreter &aInterpreter); @@ -179,7 +179,7 @@ class Interpreter * resolution of the provided network aliases is checked in the * course of execution. */ - Value ValidateMultiNetworkSyntax(const Expression &aExpr, XpanIdArray &aNids); + Value ValidateMultiNetworkSyntax(const Expression &aExpr, std::vector &aNids); /** * Resolves network aliases into a set of network ids. In the * course of resolution, duplicate network ids are compacted if diff --git a/src/app/cli/interpreter_test.cpp b/src/app/cli/interpreter_test.cpp index a01bc5722..39602721e 100644 --- a/src/app/cli/interpreter_test.cpp +++ b/src/app/cli/interpreter_test.cpp @@ -56,6 +56,7 @@ #include "commissioner/defines.hpp" #include "commissioner/error.hpp" #include "commissioner/network_data.hpp" +#include "common/utils.hpp" #include "fmt/core.h" #include "gmock/gmock-matchers.h" #include "gmock/gmock-spec-builders.h" @@ -63,6 +64,7 @@ #include "nlohmann/json.hpp" using namespace ot::commissioner; +using namespace ot::commissioner::utils; using namespace ot::commissioner::persistent_storage; using testing::_; @@ -152,7 +154,7 @@ TEST_F(InterpreterTestSuite, MNSV_ValidSyntaxPass) std::string command; Interpreter::Expression expr; Interpreter::Expression ret; - XpanIdArray nids; + std::vector nids; command = "start --nwk all"; expr = ctx.mInterpreter.ParseExpression(command); @@ -206,7 +208,7 @@ TEST_F(InterpreterTestSuite, MNSV_TwoGroupNwkAliasesFail) InitContext(ctx); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk all other"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_FALSE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -225,7 +227,7 @@ TEST_F(InterpreterTestSuite, MNSV_ThisResolvesWithCurrentSet) ASSERT_EQ(ctx.mRegistry->SetCurrentNetwork(br), RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk this"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -241,7 +243,7 @@ TEST_F(InterpreterTestSuite, MNSV_ThisUnresolvesWithCurrentUnset) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk this"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_FALSE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -265,12 +267,12 @@ TEST_F(InterpreterTestSuite, MNSV_AllOtherSameWithCurrentUnselected) // No current network selected Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk all"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{1}), nids.end()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{2}), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 1), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 2), nids.end()); ctx.mInterpreter.mContext.Cleanup(); ctx.mInterpreter.mJobManager->CleanupJobs(); ret.clear(); @@ -279,8 +281,8 @@ TEST_F(InterpreterTestSuite, MNSV_AllOtherSameWithCurrentUnselected) expr = ctx.mInterpreter.ParseExpression("start --nwk other"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{1}), nids.end()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{2}), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 1), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 2), nids.end()); ctx.mInterpreter.mContext.Cleanup(); ctx.mInterpreter.mJobManager->CleanupJobs(); ret.clear(); @@ -306,12 +308,12 @@ TEST_F(InterpreterTestSuite, MNSV_AllOtherDifferWithCurrentSelected) ASSERT_EQ(ctx.mRegistry->SetCurrentNetwork(br), RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk all"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{1}), nids.end()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{2}), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 1), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 2), nids.end()); ctx.mInterpreter.mContext.Cleanup(); ctx.mInterpreter.mJobManager->CleanupJobs(); ret.clear(); @@ -320,8 +322,8 @@ TEST_F(InterpreterTestSuite, MNSV_AllOtherDifferWithCurrentSelected) expr = ctx.mInterpreter.ParseExpression("start --nwk other"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); - EXPECT_EQ(std::find(nids.begin(), nids.end(), XpanId{1}), nids.end()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{2}), nids.end()); + EXPECT_EQ(std::find(nids.begin(), nids.end(), 1), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 2), nids.end()); ctx.mInterpreter.mContext.Cleanup(); ctx.mInterpreter.mJobManager->CleanupJobs(); ret.clear(); @@ -344,7 +346,7 @@ TEST_F(InterpreterTestSuite, MNSV_TwoDomSwitchesFail) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --dom domain1 --dom domain2"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_FALSE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -361,7 +363,7 @@ TEST_F(InterpreterTestSuite, MNSV_UnexistingDomainResolveFails) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --dom domain2"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_FALSE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -383,12 +385,12 @@ TEST_F(InterpreterTestSuite, MNSV_ExistingDomainResolves) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --dom domain1"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); - EXPECT_NE(std::find(nids.begin(), nids.end(), XpanId{1}), nids.end()); - EXPECT_EQ(std::find(nids.begin(), nids.end(), XpanId{2}), nids.end()); + EXPECT_NE(std::find(nids.begin(), nids.end(), 1), nids.end()); + EXPECT_EQ(std::find(nids.begin(), nids.end(), 2), nids.end()); } TEST_F(InterpreterTestSuite, MNSV_AmbiguousNwkResolutionFails) @@ -397,9 +399,9 @@ TEST_F(InterpreterTestSuite, MNSV_AmbiguousNwkResolutionFails) InitContext(ctx); NetworkId nid; - ASSERT_EQ(ctx.mRegistry->mStorage->Add( - Network{NetworkId{EMPTY_ID}, DomainId{EMPTY_ID}, "net1", XpanId{1}, 0, 0xAB, "", 0}, nid), - PersistentStorage::Status::kSuccess); + ASSERT_EQ( + ctx.mRegistry->mStorage->Add(Network{NetworkId{EMPTY_ID}, DomainId{EMPTY_ID}, "net1", 1, 0, 0xAB, "", 0}, nid), + PersistentStorage::Status::kSuccess); ASSERT_EQ( ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", BorderAgent::State{0, 0, 0, 0, 0}, "net1", 1, "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, "domain1", 0, 0, "", @@ -408,18 +410,18 @@ TEST_F(InterpreterTestSuite, MNSV_AmbiguousNwkResolutionFails) // lookup by PAN ID must succeed Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk ab"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_EQ(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).mError.GetCode(), ErrorCode::kNone); EXPECT_EQ(nids.size(), 1); - EXPECT_EQ(nids[0], XpanId{1}); + EXPECT_EQ(nids[0], 1); ctx.mInterpreter.mContext.Cleanup(); // create ambiguity by adding another network with the same PAN ID - ASSERT_EQ(ctx.mRegistry->mStorage->Add( - Network{NetworkId{EMPTY_ID}, DomainId{EMPTY_ID}, "net2", XpanId{2}, 0, 0xAB, "", 0}, nid), - PersistentStorage::Status::kSuccess); + ASSERT_EQ( + ctx.mRegistry->mStorage->Add(Network{NetworkId{EMPTY_ID}, DomainId{EMPTY_ID}, "net2", 2, 0, 0xAB, "", 0}, nid), + PersistentStorage::Status::kSuccess); ASSERT_EQ( ctx.mRegistry->Add(BorderAgent{"127.0.0.2", 20001, ByteArray{}, "1.1", BorderAgent::State{0, 0, 0, 0, 0}, "net2", 2, "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, "domain2", 0, 0, "", @@ -448,7 +450,7 @@ TEST_F(InterpreterTestSuite, MNSV_SameResolutionFromTwoAliasesCollapses) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk 1 net1"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -471,7 +473,7 @@ TEST_F(InterpreterTestSuite, MNSV_GroupAndIndividualNwkAliasesMustFail) RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --nwk 1 all"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_FALSE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); @@ -496,12 +498,12 @@ TEST_F(InterpreterTestSuite, MNSV_DomThisResolvesWithRespectToSelection) ASSERT_EQ(ctx.mRegistry->SetCurrentNetwork(br), RegistryStatus::kSuccess); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start --dom this"); EXPECT_EQ(ctx.mInterpreter.ReParseMultiNetworkSyntax(expr, ret).GetCode(), ErrorCode::kNone); EXPECT_TRUE(ctx.mInterpreter.ValidateMultiNetworkSyntax(ret, nids).HasNoError()); EXPECT_EQ(nids.size(), 1); - EXPECT_EQ(nids[0], XpanId{1}); + EXPECT_EQ(nids[0], 1); } TEST_F(InterpreterTestSuite, MNSV_NoAliasesResolvesToThisNwk) @@ -531,12 +533,12 @@ TEST_F(InterpreterTestSuite, MNSV_NoAliasesResolvesToThisNwk) BorderRouter br; br.mNetworkId = NetworkId{2}; ASSERT_EQ(ctx.mRegistry->SetCurrentNetwork(br), RegistryStatus::kSuccess); - XpanId nid; + uint64_t nid; ASSERT_EQ(ctx.mRegistry->GetCurrentNetworkXpan(nid), RegistryStatus::kSuccess); - ASSERT_EQ(nid, XpanId{3}); + ASSERT_EQ(nid, 3); Interpreter::Expression expr, ret; - XpanIdArray nids; + std::vector nids; expr = ctx.mInterpreter.ParseExpression("start"); CommissionerAppMockPtr pcaMock{new CommissionerAppMock()}; @@ -1133,17 +1135,17 @@ TEST_F(InterpreterTestSuite, PC_TokenWithCCM) InitContext(ctx); // prepare CCM network record - const std::string kDomainName = "domain1"; - const std::string kNetworkName = "net1"; - const std::string kSmPath = "./tmp/dom/" + kDomainName + "/"; - XpanId xpanCcm{1}; + const std::string kDomainName = "domain1"; + const std::string kNetworkName = "net1"; + const std::string kSmPath = "./tmp/dom/" + kDomainName + "/"; + uint64_t xpanCcm = 1; + BorderAgent::State baStateCcm{BorderAgent::State::ConnectionMode::kX509Connection, 0, 0, 0, 0}; - ASSERT_EQ( - ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", baStateCcm, kNetworkName, xpanCcm.mValue, - "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, kDomainName, 0, 0, "", 0, - 0x1F | BorderAgent::kDomainNameBit | BorderAgent::kExtendedPanIdBit}), - RegistryStatus::kSuccess); + ASSERT_EQ(ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", baStateCcm, kNetworkName, xpanCcm, + "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, kDomainName, 0, 0, "", 0, + 0x1F | BorderAgent::kDomainNameBit | BorderAgent::kExtendedPanIdBit}), + RegistryStatus::kSuccess); // prepare fake SM for the CCM network std::vector> smFiles = {{"ca", kSmPath + "ca.pem"}, @@ -1189,17 +1191,18 @@ TEST_F(InterpreterTestSuite, PC_TokenWithNonCCM) InitContext(ctx); // prepare non-CCM network record - const std::string kDomainName = "DefaultDomain"; - const std::string kNetworkName = "net2"; - const std::string kSmPath = "./tmp/nwk/" + kNetworkName + "/"; - XpanId xpanNonCcm{2}; + const std::string kDomainName = "DefaultDomain"; + const std::string kNetworkName = "net2"; + const std::string kSmPath = "./tmp/nwk/" + kNetworkName + "/"; + uint64_t xpanNonCcm = 2; + BorderAgent::State baStateNonCcm{BorderAgent::State::ConnectionMode::kPSKcConnection, 0, 0, 0, 0}; - ASSERT_EQ(ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", baStateNonCcm, kNetworkName, - xpanNonCcm.mValue, "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, - kDomainName, 0, 0, "", 0, - 0x1F | BorderAgent::kDomainNameBit | BorderAgent::kExtendedPanIdBit}), - RegistryStatus::kSuccess); + ASSERT_EQ( + ctx.mRegistry->Add(BorderAgent{"127.0.0.1", 20001, ByteArray{}, "1.1", baStateNonCcm, kNetworkName, xpanNonCcm, + "", "", Timestamp{0, 0, 0}, 0, "", ByteArray{}, kDomainName, 0, 0, "", 0, + 0x1F | BorderAgent::kDomainNameBit | BorderAgent::kExtendedPanIdBit}), + RegistryStatus::kSuccess); // prepare fake SM for the non-CCM network std::vector> smFiles = { {"ca", kSmPath + "ca.pem"}, {"key", kSmPath + "priv.pem"}, {"cert", kSmPath + "cert.pem"}}; @@ -1505,8 +1508,8 @@ TEST_F(InterpreterTestSuite, PC_NetworkIdentifyWithDomain) { EXPECT_TRUE(false) << "Failed to parse value: " << e.what(); } - EXPECT_TRUE(json.contains("0000000000000001")); - EXPECT_STREQ("domain1/net1", json.at("0000000000000001").get().c_str()); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_STREQ("domain1/net1", json.at("0x0000000000000001").get().c_str()); } TEST_F(InterpreterTestSuite, PC_NetworkIdentifyWithoutDomain) @@ -1546,8 +1549,8 @@ TEST_F(InterpreterTestSuite, PC_NetworkIdentifyWithoutDomain) { EXPECT_TRUE(false) << "Failed to parse value: " << e.what(); } - EXPECT_TRUE(json.contains("0000000000000002")); - EXPECT_STREQ("net2", json.at("0000000000000002").get().c_str()); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_STREQ("net2", json.at("0x0000000000000002").get().c_str()); } TEST_F(InterpreterTestSuite, PC_NetworkIdentifyUnset) @@ -1804,16 +1807,17 @@ TEST_F(InterpreterTestSuite, PC_OpdatasetGetActive) InitContext(ctx); NetworkId nwk_id; - EXPECT_EQ(ctx.mRegistry->mStorage->Add(Network{EMPTY_ID, EMPTY_ID, "", XpanId{1}, 0, 0, "", 0}, nwk_id), + EXPECT_EQ(ctx.mRegistry->mStorage->Add(Network{EMPTY_ID, EMPTY_ID, "", 1, 0, 0, "", 0}, nwk_id), PersistentStorage::Status::kSuccess); Network nwk; EXPECT_EQ(ctx.mRegistry->mStorage->Get(nwk_id, nwk), PersistentStorage::Status::kSuccess); - EXPECT_EQ((std::string)nwk.mPan, "0x0000"); + EXPECT_EQ(utils::Hex(nwk.mPan), "0x0000"); EXPECT_CALL(*ctx.mDefaultCommissionerObject, GetActiveDataset(_, _)) .Times(2) .WillRepeatedly(DoAll(WithArg<0>([](ActiveOperationalDataset &a) { - a.mExtendedPanId = XpanId{1}, a.mPanId = 1; + a.mExtendedPanId = ByteArray{0, 0, 0, 0, 0, 0, 0, 1}; + a.mPanId = 1; a.mPresentFlags = ActiveOperationalDataset::kPanIdBit | ActiveOperationalDataset::kExtendedPanIdBit; }), @@ -1827,7 +1831,7 @@ TEST_F(InterpreterTestSuite, PC_OpdatasetGetActive) EXPECT_TRUE(value.HasNoError()); EXPECT_EQ(ctx.mRegistry->mStorage->Get(nwk_id, nwk), PersistentStorage::Status::kSuccess); - EXPECT_EQ(nwk.mPan.mValue, 0x0001); + EXPECT_EQ(nwk.mPan, 0x0001); expr = ctx.mInterpreter.ParseExpression("opdataset get active --export ./tmp/aods.json"); value = ctx.mInterpreter.Eval(expr); @@ -2369,24 +2373,13 @@ TEST_F(InterpreterTestSuite, PC_BrAddInterObjectInconsistencyFail) TEST_F(InterpreterTestSuite, PC_BrAdd) { -#define XPAN_1234 "1234" #define XPAN_1235 "0x1235" -#define XPAN_1236 "0000000000001236" -#define XPAN_1237 "00000000000001237" // length > 16 +#define XPAN_1236 "0x0000000000001236" TestContext ctx; InitContext(ctx); std::string brJson = "[\n\ - {\n\ - \"Addr\": \"1234::5678\",\n\ - \"Port\": 2000,\n\ - \"ThreadVersion\": \"th1.2\",\n\ - \"State\": 0,\n\ - \"NetworkName\": \"net1\",\n\ - \"ExtendedPanId\": \"" XPAN_1234 "\",\n\ - \"DomainName\": \"dom1\"\n\ - },\n\ {\n\ \"Addr\": \"1234::5679\",\n\ \"Port\": 2001,\n\ @@ -2426,28 +2419,24 @@ TEST_F(InterpreterTestSuite, PC_BrAdd) BorderRouterArray brs; EXPECT_EQ(ctx.mInterpreter.mRegistry->GetAllBorderRouters(brs), RegistryStatus::kSuccess); - EXPECT_EQ(brs.size(), 4); + EXPECT_EQ(brs.size(), 3); NetworkArray nwks; EXPECT_EQ(ctx.mInterpreter.mRegistry->GetAllNetworks(nwks), RegistryStatus::kSuccess); - EXPECT_EQ(nwks.size(), 3); + EXPECT_EQ(nwks.size(), 2); DomainArray doms; EXPECT_EQ(ctx.mInterpreter.mRegistry->GetAllDomains(doms), RegistryStatus::kSuccess); EXPECT_EQ(doms.size(), 2); - Network nwk; - XpanId xpan; - EXPECT_EQ(xpan.FromHex(XPAN_1234).GetCode(), ErrorCode::kNone); - EXPECT_EQ(ctx.mInterpreter.mRegistry->GetNetworkByXpan(0x1234, nwk), RegistryStatus::kSuccess); - EXPECT_EQ(nwk.mXpan, xpan); - EXPECT_EQ(xpan.FromHex(XPAN_1235).GetCode(), ErrorCode::kNone); + Network nwk; + uint64_t xpan; + EXPECT_EQ(utils::ParseInteger(xpan, "0x1235").GetCode(), ErrorCode::kNone); EXPECT_EQ(ctx.mInterpreter.mRegistry->GetNetworkByXpan(0x1235, nwk), RegistryStatus::kSuccess); EXPECT_EQ(nwk.mXpan, xpan); - EXPECT_EQ(xpan.FromHex(XPAN_1236).GetCode(), ErrorCode::kNone); + EXPECT_EQ(utils::ParseInteger(xpan, "0x0000000000001236").GetCode(), ErrorCode::kNone); EXPECT_EQ(ctx.mInterpreter.mRegistry->GetNetworkByXpan(0x1236, nwk), RegistryStatus::kSuccess); EXPECT_EQ(nwk.mXpan, xpan); - EXPECT_EQ(xpan.FromHex(XPAN_1237).GetCode(), ErrorCode::kBadFormat); } TEST_F(InterpreterTestSuite, PC_BrListPositive) @@ -2626,7 +2615,7 @@ TEST_F(InterpreterTestSuite, PC_BrDeleteExplicitSelectedFails) "", 0, 0x3F | BorderAgent::kDomainNameBit}), RegistryStatus::kSuccess); - EXPECT_EQ(ctx.mRegistry->SetCurrentNetwork(XpanId{2}), RegistryStatus::kSuccess); + EXPECT_EQ(ctx.mRegistry->SetCurrentNetwork(2), RegistryStatus::kSuccess); Interpreter::Expression expr; Interpreter::Value value; diff --git a/src/app/cli/job.cpp b/src/app/cli/job.cpp index 7535e3ea7..6a38524a5 100644 --- a/src/app/cli/job.cpp +++ b/src/app/cli/job.cpp @@ -54,7 +54,7 @@ void Job::Wait() mJobThread.join(); if (!mValue.HasNoError()) { - LOG_DEBUG(LOG_REGION_JOB, "{}: job '{}' failed: {}", XpanId(mXpanId).str(), GetCommandString(), + LOG_DEBUG(LOG_REGION_JOB, "{}: job '{}' failed: {}", utils::Hex(mXpanId), GetCommandString(), mValue.ToString()); } } @@ -79,12 +79,12 @@ Job::Job(Interpreter &aInterpreter, CommissionerAppPtr &aCommApp, Interpreter::Expression aExpr, Interpreter::JobEvaluator aEval, - XpanId aXpanId) + uint64_t aXpanId) : mInterpreter(aInterpreter) , mCommissioner(aCommApp) , mExpr(aExpr) , mEval(aEval) - , mXpanId(aXpanId.mValue) + , mXpanId(aXpanId) { } diff --git a/src/app/cli/job.hpp b/src/app/cli/job.hpp index b23cfb29f..4ae1a2506 100644 --- a/src/app/cli/job.hpp +++ b/src/app/cli/job.hpp @@ -52,7 +52,7 @@ class Job CommissionerAppPtr &aCommApp, Interpreter::Expression aExpr, Interpreter::JobEvaluator aEval, - XpanId aXpanId); + uint64_t aXpanId); ~Job() = default; void Run(); diff --git a/src/app/cli/job_manager.cpp b/src/app/cli/job_manager.cpp index f0beadd01..4d2d7e3f6 100644 --- a/src/app/cli/job_manager.cpp +++ b/src/app/cli/job_manager.cpp @@ -117,7 +117,7 @@ void JobManager::SetImportFile(const std::string &importFile) mImportFile = importFile; } -Error JobManager::CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter::Expression &aExpr, XpanId aXpanId) +Error JobManager::CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter::Expression &aExpr, uint64_t aXpanId) { Interpreter::JobEvaluator eval; auto mapItem = Interpreter::mJobEvaluatorMap.find(utils::ToLower(aExpr[0])); @@ -133,7 +133,9 @@ Error JobManager::CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter return ERROR_NONE; } -Error JobManager::PrepareJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias) +Error JobManager::PrepareJobs(const Interpreter::Expression &aExpr, + const std::vector &aNids, + bool aGroupAlias) { Error error; @@ -191,7 +193,9 @@ Error JobManager::PrepareJobs(const Interpreter::Expression &aExpr, const XpanId return error; } -Error JobManager::PrepareStartJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias) +Error JobManager::PrepareStartJobs(const Interpreter::Expression &aExpr, + const std::vector &aNids, + bool aGroupAlias) { Error error = ERROR_NONE; @@ -248,7 +252,9 @@ Error JobManager::PrepareStartJobs(const Interpreter::Expression &aExpr, const X return error; } -Error JobManager::PrepareStopJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias) +Error JobManager::PrepareStopJobs(const Interpreter::Expression &aExpr, + const std::vector &aNids, + bool aGroupAlias) { Error error = ERROR_NONE; @@ -282,7 +288,7 @@ Error JobManager::PrepareStopJobs(const Interpreter::Expression &aExpr, const Xp return error; } -Error JobManager::PrepareDtlsConfig(const XpanId aNid, Config &aConfig) +Error JobManager::PrepareDtlsConfig(uint64_t aNid, Config &aConfig) { Error error; std::string domainName; @@ -321,12 +327,12 @@ Error JobManager::PrepareDtlsConfig(const XpanId aNid, Config &aConfig) status = mInterpreter.mRegistry->GetNetworkByXpan(aNid, nwk); VerifyOrExit(status == RegistryStatus::kSuccess, - error = ERROR_NOT_FOUND("network not found by XPAN '{}'", aNid.str())); + error = ERROR_NOT_FOUND("network not found by XPAN '{}'", utils::Hex(aNid))); isCCM = nwk.mCcm > 0; status = mInterpreter.mRegistry->GetDomainNameByXpan(aNid, domainName); if (status != RegistryStatus::kSuccess) { - LOG_DEBUG(LOG_REGION_JOB_MANAGER, "{}: domain resolution failed with status={}", XpanId(aNid).str(), + LOG_DEBUG(LOG_REGION_JOB_MANAGER, "{}: domain resolution failed with status={}", utils::Hex(aNid), static_cast(status)); } @@ -355,8 +361,8 @@ Error JobManager::PrepareDtlsConfig(const XpanId aNid, Config &aConfig) /// loading. Therefore, any other connection /// mode is considered a wrong configuration of /// border router, and respectively ignored. - LOG_DEBUG(LOG_REGION_JOB_MANAGER, "loading PSKc ignored for CCM network [{}:'{}']", aNid.str(), - nwk.mName); + LOG_DEBUG(LOG_REGION_JOB_MANAGER, "loading PSKc ignored for CCM network [{}:'{}']", + utils::Hex(aNid), nwk.mName); } goto update; } @@ -369,7 +375,7 @@ Error JobManager::PrepareDtlsConfig(const XpanId aNid, Config &aConfig) } } - error = security_material::GetNetworkSM(nwk.mXpan.str(), needCert, needPSKc, dtlsConfig); + error = security_material::GetNetworkSM(utils::Hex(nwk.mXpan), needCert, needPSKc, dtlsConfig); if (ERROR_NONE != error) { LOG_STR(DEBUG, LOG_REGION_JOB_MANAGER, error.GetMessage()); @@ -427,13 +433,13 @@ Error JobManager::PrepareDtlsConfig(const XpanId aNid, Config &aConfig) #undef UPDATE_IF_SET if (dtlsConfig.IsIncomplete(needCert, needPSKc, isCCM)) { - error = ERROR_SECURITY("incomplete DTLS configuration for the network [{}:'{}']", aNid.str(), nwk.mName); + error = ERROR_SECURITY("incomplete DTLS configuration for the network [{}:'{}']", utils::Hex(aNid), nwk.mName); } exit: return error; } -Error JobManager::MakeBorderRouterChoice(const XpanId aNid, BorderRouter &br) +Error JobManager::MakeBorderRouterChoice(uint64_t aNid, BorderRouter &br) { Error error; BRArray brs; @@ -451,7 +457,7 @@ Error JobManager::MakeBorderRouterChoice(const XpanId aNid, BorderRouter &br) } status = mInterpreter.mRegistry->GetNetworkByXpan(aNid, nwk); VerifyOrExit(status == RegistryStatus::kSuccess, - error = ERROR_NOT_FOUND("network not found by XPAN '{}'", aNid.str())); + error = ERROR_NOT_FOUND("network not found by XPAN '{}'", utils::Hex(aNid))); if (nwk.mCcm > 0) // Dealing with domain network { // - try to find active and connectable Primary BBR @@ -534,7 +540,7 @@ Error JobManager::MakeBorderRouterChoice(const XpanId aNid, BorderRouter &br) return error; } -Error JobManager::AppendImport(XpanId aXpanId, Interpreter::Expression &aExpr) +Error JobManager::AppendImport(uint64_t aXpanId, Interpreter::Expression &aExpr) { Error error; std::string jsonStr; @@ -543,13 +549,13 @@ Error JobManager::AppendImport(XpanId aXpanId, Interpreter::Expression &aExpr) SuccessOrExit(error = JsonFromFile(jsonStr, mImportFile)); jsonSrc = Json::parse(jsonStr); - if (aXpanId == XpanId()) // must be single command + if (aXpanId == 0) // must be single command { json = jsonSrc; } - else if (jsonSrc.count(aXpanId.str()) > 0) + else if (jsonSrc.count(utils::Hex(aXpanId)) > 0) { - json = jsonSrc[aXpanId.str()]; + json = jsonSrc[utils::Hex(aXpanId)]; } jsonStr = json.dump(JSON_INDENT_DEFAULT); if (utils::ToLower(aExpr[0]) == "opdataset") @@ -667,14 +673,14 @@ Interpreter::Value JobManager::CollectJobsValue() { Interpreter::Value value; nlohmann::json json; - XpanId xpan; + uint64_t xpan; for (const auto &job : mJobPool) { ASSERT(job->IsStopped()); if (job->GetValue().HasNoError()) { - xpan = XpanId{job->GetXpanId()}; + xpan = job->GetXpanId(); try { std::string valueStr = job->GetValue().ToString(); @@ -688,7 +694,7 @@ Interpreter::Value JobManager::CollectJobsValue() // nothing but [done] is printed; we need to see a // distinguished result per network } - json[xpan.str()] = nlohmann::json::parse(valueStr); + json[utils::Hex(xpan)] = nlohmann::json::parse(valueStr); } catch (std::exception &e) { ErrorMsg(xpan, e.what()); @@ -696,7 +702,7 @@ Interpreter::Value JobManager::CollectJobsValue() } else // produce error messages immediately before printing value { - ErrorMsg(XpanId{job->GetXpanId()}, job->GetValue().ToString()); + ErrorMsg(job->GetXpanId(), job->GetValue().ToString()); } } value = json.dump(JSON_INDENT_DEFAULT); @@ -719,13 +725,13 @@ void JobManager::StopCommissionerPool() Error JobManager::GetSelectedCommissioner(CommissionerAppPtr &aCommissioner) { Error error = ERROR_NONE; - XpanId nid; + uint64_t nid; RegistryStatus status; status = mInterpreter.mRegistry->GetCurrentNetworkXpan(nid); VerifyOrExit(RegistryStatus::kSuccess == status, error = ERROR_REGISTRY_ERROR("getting selected network failed")); - if (nid.mValue != XpanId::kEmptyXpanId) + if (nid != 0) { auto entry = mCommissionerPool.find(nid); if (entry != mCommissionerPool.end()) @@ -761,19 +767,19 @@ bool JobManager::IsClean() return mJobPool.size() == 0 && mImportFile.size() == 0; } -void JobManager::ErrorMsg(XpanId aNid, std::string aMessage) +void JobManager::ErrorMsg(uint64_t aNid, std::string aMessage) { - mInterpreter.PrintNetworkMessage(aNid.mValue, aMessage, Console::Color::kRed); + mInterpreter.PrintNetworkMessage(aNid, aMessage, Console::Color::kRed); } -void JobManager::WarningMsg(XpanId aNid, std::string aMessage) +void JobManager::WarningMsg(uint64_t aNid, std::string aMessage) { - mInterpreter.PrintNetworkMessage(aNid.mValue, aMessage, Console::Color::kMagenta); + mInterpreter.PrintNetworkMessage(aNid, aMessage, Console::Color::kMagenta); } -void JobManager::InfoMsg(XpanId aNid, std::string aMessage) +void JobManager::InfoMsg(uint64_t aNid, std::string aMessage) { - mInterpreter.PrintNetworkMessage(aNid.mValue, aMessage, Console::Color::kDefault); + mInterpreter.PrintNetworkMessage(aNid, aMessage, Console::Color::kDefault); } } // namespace commissioner diff --git a/src/app/cli/job_manager.hpp b/src/app/cli/job_manager.hpp index dae006b0f..72143c3d0 100644 --- a/src/app/cli/job_manager.hpp +++ b/src/app/cli/job_manager.hpp @@ -45,7 +45,6 @@ namespace commissioner { using CommissionerAppPtr = std::shared_ptr; using RegistryStatus = ot::commissioner::persistent_storage::Registry::Status; -using ot::commissioner::XpanId; using ot::commissioner::persistent_storage::BorderRouter; class JobManager @@ -61,7 +60,7 @@ class JobManager * * @see JobManager::CreateJob() */ - Error PrepareJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias); + Error PrepareJobs(const Interpreter::Expression &aExpr, const std::vector &aNids, bool aGroupAlias); /** * Run all prepared jobs. * @@ -118,7 +117,7 @@ class JobManager * It is expected mImportFile contains a JSON object where the * imported part is a value of a map entry under aNid key. */ - Error AppendImport(XpanId aXpanId, Interpreter::Expression &aExpr); + Error AppendImport(uint64_t aXpanId, Interpreter::Expression &aExpr); /** * Make a well-thought choice from border routers belonging to a * given network identified by aNid XPAN ID. @@ -129,12 +128,12 @@ class JobManager * If in non-CCM mode, a BR with most highly avaliable and Thread-active * interface becomes the choice. */ - Error MakeBorderRouterChoice(const XpanId aNid, BorderRouter &br); + Error MakeBorderRouterChoice(uint64_t aNid, BorderRouter &br); private: friend class JobManagerTestSuite; - using CommissionerPool = std::map; + using CommissionerPool = std::map; using JobPool = std::vector; /** * Wait for all job threads to join. @@ -143,11 +142,11 @@ class JobManager /** * A flavor of JobManager::PrepareJobs() for `start' command specifically. */ - Error PrepareStartJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias); + Error PrepareStartJobs(const Interpreter::Expression &aExpr, const std::vector &aNids, bool aGroupAlias); /** * A flavor of JobManager::PrepareJobs() for `stop' command specifically. */ - Error PrepareStopJobs(const Interpreter::Expression &aExpr, const XpanIdArray &aNids, bool aGroupAlias); + Error PrepareStopJobs(const Interpreter::Expression &aExpr, const std::vector &aNids, bool aGroupAlias); /** * Updates DTLS parts of Config for the given network. * @@ -173,15 +172,15 @@ class JobManager * connection must restart to adopt the most recent connection * information. */ - Error PrepareDtlsConfig(const XpanId aNid, Config &aConfig); + Error PrepareDtlsConfig(uint64_t aNid, Config &aConfig); /** * Creates @ref Job object and places the job into @ref JobManager::mJobPool. */ - Error CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter::Expression &aExpr, XpanId aXpanId); + Error CreateJob(CommissionerAppPtr &aCommissioner, const Interpreter::Expression &aExpr, uint64_t aXpanId); - void ErrorMsg(XpanId aNid, std::string aMessage); - void WarningMsg(XpanId aNid, std::string aMessage); - void InfoMsg(XpanId aNid, std::string aMessage); + void ErrorMsg(uint64_t aNid, std::string aMessage); + void WarningMsg(uint64_t aNid, std::string aMessage); + void InfoMsg(uint64_t aNid, std::string aMessage); /** * Pool of @ref Job objects prepared per command execution. The diff --git a/src/app/cli/job_manager_test.cpp b/src/app/cli/job_manager_test.cpp index ad9705fd2..8efacd410 100644 --- a/src/app/cli/job_manager_test.cpp +++ b/src/app/cli/job_manager_test.cpp @@ -627,9 +627,9 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) Interpreter::Value value = ctx.mJobManager.CollectJobsValue(); nlohmann::json json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_FALSE(json.contains(XpanId{3}.str())); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_FALSE(json.contains("0x0000000000000003")); ctx.mJobManager.CleanupJobs(); // "active" command @@ -637,12 +637,12 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) ctx.mJobManager.RunJobs(); value = ctx.mJobManager.CollectJobsValue(); json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_TRUE(json.contains(XpanId{3}.str())); - EXPECT_TRUE(json[XpanId{1}.str()]); - EXPECT_TRUE(json[XpanId{2}.str()]); - EXPECT_FALSE(json[XpanId{3}.str()]); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_TRUE(json.contains("0x0000000000000003")); + EXPECT_TRUE(json["0x0000000000000001"]); + EXPECT_TRUE(json["0x0000000000000002"]); + EXPECT_FALSE(json["0x0000000000000003"]); ctx.mJobManager.CleanupJobs(); // "sessionid" command @@ -654,10 +654,10 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) ctx.mJobManager.RunJobs(); value = ctx.mJobManager.CollectJobsValue(); json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_EQ(json[XpanId{1}.str()], 0); - EXPECT_EQ(json[XpanId{2}.str()], 1); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_EQ(json["0x0000000000000001"], 0); + EXPECT_EQ(json["0x0000000000000002"], 1); ctx.mJobManager.CleanupJobs(); // "opdataset get active" command @@ -677,12 +677,12 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) ctx.mJobManager.RunJobs(); value = ctx.mJobManager.CollectJobsValue(); json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_TRUE(json[XpanId{1}.str()].contains("PanId")); - EXPECT_TRUE(json[XpanId{2}.str()].contains("PanId")); - EXPECT_STREQ("0x0001", json[XpanId{1}.str()]["PanId"].get().c_str()); - EXPECT_STREQ("0x0002", json[XpanId{2}.str()]["PanId"].get().c_str()); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_TRUE(json["0x0000000000000001"].contains("PanId")); + EXPECT_TRUE(json["0x0000000000000002"].contains("PanId")); + EXPECT_STREQ("0x0001", json["0x0000000000000001"]["PanId"].get().c_str()); + EXPECT_STREQ("0x0002", json["0x0000000000000002"]["PanId"].get().c_str()); ctx.mJobManager.CleanupJobs(); // "opdataset set securitypolicy" command @@ -696,10 +696,10 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) ctx.mJobManager.RunJobs(); value = ctx.mJobManager.CollectJobsValue(); json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_TRUE(json[XpanId{1}.str()]); - EXPECT_TRUE(json[XpanId{2}.str()]); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_TRUE(json["0x0000000000000001"]); + EXPECT_TRUE(json["0x0000000000000002"]); ctx.mJobManager.CleanupJobs(); // "stop" command @@ -709,9 +709,9 @@ TEST_F(JobManagerTestSuite, BuildFinalResultString) ctx.mJobManager.RunJobs(); value = ctx.mJobManager.CollectJobsValue(); json = nlohmann::json::parse(value.ToString()); - EXPECT_TRUE(json.contains(XpanId{1}.str())); - EXPECT_TRUE(json.contains(XpanId{2}.str())); - EXPECT_TRUE(json[XpanId{1}.str()]); - EXPECT_TRUE(json[XpanId{2}.str()]); + EXPECT_TRUE(json.contains("0x0000000000000001")); + EXPECT_TRUE(json.contains("0x0000000000000002")); + EXPECT_TRUE(json["0x0000000000000001"]); + EXPECT_TRUE(json["0x0000000000000002"]); ctx.mJobManager.CleanupJobs(); } diff --git a/src/app/commissioner_app.cpp b/src/app/commissioner_app.cpp index 6eb7cb2be..fbd612e3c 100644 --- a/src/app/commissioner_app.cpp +++ b/src/app/commissioner_app.cpp @@ -523,7 +523,7 @@ Error CommissionerApp::GetExtendedPanId(ByteArray &aExtendedPanId) const VerifyOrExit(mActiveDataset.mPresentFlags & ActiveOperationalDataset::kExtendedPanIdBit, error = ERROR_NOT_FOUND("cannot find valid Extended PAN ID in Active Operational Dataset")); - aExtendedPanId = utils::Encode(mActiveDataset.mExtendedPanId.mValue); + aExtendedPanId = mActiveDataset.mExtendedPanId; exit: return error; @@ -536,7 +536,7 @@ Error CommissionerApp::SetExtendedPanId(const ByteArray &aExtendedPanId) VerifyOrExit(IsActive(), error = ERROR_INVALID_STATE("the commissioner is not active")); - activeDataset.mExtendedPanId = XpanId{utils::Decode(aExtendedPanId)}; + activeDataset.mExtendedPanId = aExtendedPanId; activeDataset.mPresentFlags |= ActiveOperationalDataset::kExtendedPanIdBit; SuccessOrExit(error = mCommissioner->SetActiveDataset(activeDataset)); @@ -680,7 +680,7 @@ Error CommissionerApp::SetNetworkName(const std::string &aNetworkName) return error; } -Error CommissionerApp::GetPanId(PanId &aPanId) +Error CommissionerApp::GetPanId(uint16_t &aPanId) { Error error; @@ -696,7 +696,7 @@ Error CommissionerApp::GetPanId(PanId &aPanId) return error; } -Error CommissionerApp::SetPanId(PanId aPanId, MilliSeconds aDelay) +Error CommissionerApp::SetPanId(uint16_t aPanId, MilliSeconds aDelay) { Error error; PendingOperationalDataset pendingDataset; diff --git a/src/app/commissioner_app.hpp b/src/app/commissioner_app.hpp index 288a0933c..fe08d5dfe 100644 --- a/src/app/commissioner_app.hpp +++ b/src/app/commissioner_app.hpp @@ -202,8 +202,8 @@ class CommissionerApp : public CommissionerHandler MOCKABLE Error SetNetworkMasterKey(const ByteArray &aMasterKey, MilliSeconds aDelay); MOCKABLE Error GetNetworkName(std::string &aNetworkName) const; MOCKABLE Error SetNetworkName(const std::string &aNetworkName); - MOCKABLE Error GetPanId(PanId &aPanId); - MOCKABLE Error SetPanId(PanId aPanId, MilliSeconds aDelay); + MOCKABLE Error GetPanId(uint16_t &aPanId); + MOCKABLE Error SetPanId(uint16_t aPanId, MilliSeconds aDelay); MOCKABLE Error GetPSKc(ByteArray &aPSKc) const; MOCKABLE Error SetPSKc(const ByteArray &aPSKc); diff --git a/src/app/commissioner_app_dummy.cpp b/src/app/commissioner_app_dummy.cpp index f9024be2e..248ab00ac 100644 --- a/src/app/commissioner_app_dummy.cpp +++ b/src/app/commissioner_app_dummy.cpp @@ -303,13 +303,13 @@ Error CommissionerApp::SetNetworkName(const std::string &aNetworkName) return Error{}; } -Error CommissionerApp::GetPanId(PanId &aPanId) +Error CommissionerApp::GetPanId(uint16_t &aPanId) { UNUSED(aPanId); return Error{}; } -Error CommissionerApp::SetPanId(PanId aPanId, MilliSeconds aDelay) +Error CommissionerApp::SetPanId(uint16_t aPanId, MilliSeconds aDelay) { UNUSED(aPanId); UNUSED(aDelay); diff --git a/src/app/commissioner_app_mock.hpp b/src/app/commissioner_app_mock.hpp index 5ce32476c..5b31862f6 100644 --- a/src/app/commissioner_app_mock.hpp +++ b/src/app/commissioner_app_mock.hpp @@ -104,8 +104,8 @@ class CommissionerAppMock : public ::ot::commissioner::CommissionerApp MOCK_METHOD(Error, SetNetworkMasterKey, (const ByteArray &, MilliSeconds)); MOCK_METHOD(Error, GetNetworkName, (std::string &), (const)); MOCK_METHOD(Error, SetNetworkName, (const std::string &)); - MOCK_METHOD(Error, GetPanId, (PanId &)); - MOCK_METHOD(Error, SetPanId, (PanId, MilliSeconds)); + MOCK_METHOD(Error, GetPanId, (uint16_t &)); + MOCK_METHOD(Error, SetPanId, (uint16_t, MilliSeconds)); MOCK_METHOD(Error, GetPSKc, (ByteArray &), (const)); MOCK_METHOD(Error, SetPSKc, (const ByteArray &)); MOCK_METHOD(Error, GetSecurityPolicy, (SecurityPolicy &), (const)); diff --git a/src/app/json.cpp b/src/app/json.cpp index d81323583..7f24f91d9 100644 --- a/src/app/json.cpp +++ b/src/app/json.cpp @@ -417,18 +417,6 @@ static void from_json(const Json &aJson, SecurityPolicy &aSecurityPolicy) #undef SET } -static void to_json(Json &aJson, const ot::commissioner::PanId &aPanId) -{ - aJson = std::string(aPanId); -} - -static void from_json(const Json &aJson, ot::commissioner::PanId &aPanId) -{ - std::string panIdStr; - panIdStr = aJson.get(); - SuccessOrThrow(aPanId.FromHex(panIdStr)); -} - static void to_json(Json &aJson, const ActiveOperationalDataset &aDataset) { #define SET_IF_PRESENT(name) \ @@ -438,9 +426,19 @@ static void to_json(Json &aJson, const ActiveOperationalDataset &aDataset) }; SET_IF_PRESENT(ActiveTimestamp); + SET_IF_PRESENT(NetworkName); SET_IF_PRESENT(Channel); SET_IF_PRESENT(ChannelMask); - SET_IF_PRESENT(ExtendedPanId); + + if (aDataset.mPresentFlags & ActiveOperationalDataset::kExtendedPanIdBit) + { + aJson["ExtendedPanId"] = utils::Hex(aDataset.mExtendedPanId); + } + + if (aDataset.mPresentFlags & ActiveOperationalDataset::kPanIdBit) + { + aJson["PanId"] = utils::Hex(aDataset.mPanId); + } if (aDataset.mPresentFlags & ActiveOperationalDataset::kMeshLocalPrefixBit) { @@ -448,20 +446,12 @@ static void to_json(Json &aJson, const ActiveOperationalDataset &aDataset) }; SET_IF_PRESENT(NetworkMasterKey); - SET_IF_PRESENT(NetworkName); - SET_IF_PRESENT(PanId); SET_IF_PRESENT(PSKc); SET_IF_PRESENT(SecurityPolicy); #undef SET_IF_PRESENT } -static void from_json(const Json &aJson, XpanId &aXpanId) -{ - std::string xpanStr = aJson.get(); - SuccessOrThrow(aXpanId.FromHex(xpanStr)); -} - static void from_json(const Json &aJson, ActiveOperationalDataset &aDataset) { #define SET_IF_PRESENT(name) \ @@ -472,9 +462,21 @@ static void from_json(const Json &aJson, ActiveOperationalDataset &aDataset) }; SET_IF_PRESENT(ActiveTimestamp); + SET_IF_PRESENT(NetworkName); SET_IF_PRESENT(Channel); SET_IF_PRESENT(ChannelMask); - SET_IF_PRESENT(ExtendedPanId); + + if (aJson.contains("ExtendedPanId")) + { + SuccessOrThrow(utils::Hex(aDataset.mExtendedPanId, aJson["ExtendedPanId"])); + aDataset.mPresentFlags |= ActiveOperationalDataset::kExtendedPanIdBit; + } + + if (aJson.contains("PanId")) + { + SuccessOrThrow(utils::ParseInteger(aDataset.mPanId, aJson["PanId"])); + aDataset.mPresentFlags |= ActiveOperationalDataset::kPanIdBit; + } if (aJson.contains("MeshLocalPrefix")) { @@ -485,8 +487,6 @@ static void from_json(const Json &aJson, ActiveOperationalDataset &aDataset) }; SET_IF_PRESENT(NetworkMasterKey); - SET_IF_PRESENT(NetworkName); - SET_IF_PRESENT(PanId); SET_IF_PRESENT(PSKc); SET_IF_PRESENT(SecurityPolicy); @@ -796,11 +796,6 @@ static void from_json(const nlohmann::json &aJson, BorderAgent::State &aState) void BorderAgentFromJson(BorderAgent &aAgent, const nlohmann::json &aJson) { BorderAgent agent; - auto json2xpan = [](const nlohmann::json &field) { - XpanId xpan; - Error error = xpan.FromHex(field.get()); - return error == ERROR_NONE ? xpan.mValue : XpanId::kEmptyXpanId; - }; #define SET_IF_PRESENT(field) \ do \ @@ -812,22 +807,18 @@ void BorderAgentFromJson(BorderAgent &aAgent, const nlohmann::json &aJson) } \ } while (false) -#define SET_IF_PRESENT_X(field) \ - do \ - { \ - if (aJson.contains(#field)) \ - { \ - agent.mPresentFlags |= BorderAgent::k##field##Bit; \ - agent.m##field = json2xpan(aJson.at(#field)); \ - } \ - } while (false) - SET_IF_PRESENT(Addr); SET_IF_PRESENT(Port); SET_IF_PRESENT(ThreadVersion); SET_IF_PRESENT(State); SET_IF_PRESENT(NetworkName); - SET_IF_PRESENT_X(ExtendedPanId); + + if (aJson.contains("ExtendedPanId")) + { + SuccessOrThrow(utils::ParseInteger(agent.mExtendedPanId, aJson["ExtendedPanId"])); + agent.mPresentFlags |= BorderAgent::kExtendedPanIdBit; + } + SET_IF_PRESENT(VendorName); SET_IF_PRESENT(ModelName); SET_IF_PRESENT(ActiveTimestamp); @@ -840,7 +831,6 @@ void BorderAgentFromJson(BorderAgent &aAgent, const nlohmann::json &aJson) SET_IF_PRESENT(ServiceName); #undef SET_IF_PRESENT -#undef SET_IF_PRESENT_X aAgent = agent; } @@ -863,21 +853,17 @@ void BorderAgentToJson(const BorderAgent &aAgent, nlohmann::json &aJson) } \ } while (false) -#define SET_IF_PRESENT_X(field) \ - do \ - { \ - if (aAgent.mPresentFlags & BorderAgent::k##field##Bit) \ - { \ - aJson[#field] = XpanId(aAgent.m##field).str(); \ - } \ - } while (false) - SET_IF_PRESENT(Addr); SET_IF_PRESENT(Port); SET_IF_PRESENT(ThreadVersion); SET_IF_PRESENT(State); SET_IF_PRESENT(NetworkName); - SET_IF_PRESENT_X(ExtendedPanId); + + if (aAgent.mPresentFlags & BorderAgent::kExtendedPanIdBit) + { + aJson["ExtendedPanId"] = utils::Hex(aAgent.mExtendedPanId); + } + SET_IF_PRESENT(VendorName); SET_IF_PRESENT(ModelName); SET_IF_PRESENT(ActiveTimestamp); @@ -890,7 +876,6 @@ void BorderAgentToJson(const BorderAgent &aAgent, nlohmann::json &aJson) SET_IF_PRESENT(ServiceName); #undef SET_IF_PRESENT -#undef SET_IF_PRESENT_X } } // namespace commissioner diff --git a/src/app/ps/CMakeLists.txt b/src/app/ps/CMakeLists.txt index 2db452a92..f999f9da1 100644 --- a/src/app/ps/CMakeLists.txt +++ b/src/app/ps/CMakeLists.txt @@ -74,4 +74,10 @@ if (OT_COMM_TEST) ${PROJECT_SOURCE_DIR}/third_party/googletest/repo/googletest/include $ ) + + target_link_libraries(commissioner-ps-test + PUBLIC + commissioner-common + commissioner-app + ) endif (OT_COMM_TEST) diff --git a/src/app/ps/persistent_storage_json.cpp b/src/app/ps/persistent_storage_json.cpp index 5f1539615..006a6a55b 100644 --- a/src/app/ps/persistent_storage_json.cpp +++ b/src/app/ps/persistent_storage_json.cpp @@ -285,9 +285,9 @@ PersistentStorage::Status PersistentStorageJson::Get(BorderRouterId const &aId, aRetValue.mAgent.mNetworkName = nwk.mName; aRetValue.mAgent.mPresentFlags |= BorderAgent::kNetworkNameBit; } - if (nwk.mXpan.mValue != XpanId::kEmptyXpanId) + if (nwk.mXpan != 0) { - aRetValue.mAgent.mExtendedPanId = nwk.mXpan.mValue; + aRetValue.mAgent.mExtendedPanId = nwk.mXpan; aRetValue.mAgent.mPresentFlags |= BorderAgent::kExtendedPanIdBit; } if (nwk.mDomainId.mId != EMPTY_ID) @@ -371,8 +371,7 @@ PersistentStorage::Status PersistentStorageJson::Lookup(Network const &aValue, s (aValue.mId.mId == EMPTY_ID || (el.mId.mId == aValue.mId.mId)) && (aValue.mDomainId.mId == EMPTY_ID || (el.mDomainId.mId == aValue.mDomainId.mId)) && (aValue.mName.empty() || (aValue.mName == el.mName)) && - (aValue.mXpan.mValue == XpanId::kEmptyXpanId || aValue.mXpan == el.mXpan) && - (aValue.mPan.mValue == PanId::kEmptyPanId || (aValue.mPan.mValue == el.mPan.mValue)) && + (aValue.mXpan == 0 || aValue.mXpan == el.mXpan) && (aValue.mPan == 0 || (aValue.mPan == el.mPan)) && (aValue.mMlp.empty() || CaseInsensitiveEqual(aValue.mMlp, el.mMlp)) && (aValue.mChannel == 0 || (aValue.mChannel == el.mChannel)); @@ -476,8 +475,7 @@ PersistentStorage::Status PersistentStorageJson::LookupAny(Network const &aValue (aValue.mId.mId == EMPTY_ID || (el.mId.mId == aValue.mId.mId)) || (aValue.mDomainId.mId == EMPTY_ID || (el.mDomainId.mId == aValue.mDomainId.mId)) || (aValue.mName.empty() || (aValue.mName == el.mName)) || - (aValue.mXpan.mValue == XpanId::kEmptyXpanId || aValue.mXpan == el.mXpan) || - (aValue.mPan.mValue == PanId::kEmptyPanId || (aValue.mPan.mValue == el.mPan.mValue)) || + (aValue.mXpan == 0 || aValue.mXpan == el.mXpan) || (aValue.mPan == 0 || (aValue.mPan == el.mPan)) || (aValue.mMlp.empty() || CaseInsensitiveEqual(aValue.mMlp, el.mMlp)) || (aValue.mChannel == 0 || (aValue.mChannel == el.mChannel)); diff --git a/src/app/ps/persistent_storage_json_test.cpp b/src/app/ps/persistent_storage_json_test.cpp index b51ee8281..295dc3f28 100644 --- a/src/app/ps/persistent_storage_json_test.cpp +++ b/src/app/ps/persistent_storage_json_test.cpp @@ -128,17 +128,14 @@ TEST(PersistentStorageJsonTestSuite, AddNetwork) NetworkId newId; - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", XpanId{0xFFFFFFFFFFFFFFF1ll}, 11, 0xFFF1, "2000:aaa1::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", 0xFFFFFFFFFFFFFFF1ll, 11, 0xFFF1, "2000:aaa1::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 0); - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk2", XpanId{0xFFFFFFFFFFFFFFF2ll}, 11, 0xFFF2, "2000:aaa2::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk2", 0xFFFFFFFFFFFFFFF2ll, 11, 0xFFF2, "2000:aaa2::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 1); - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk3", XpanId{0xFFFFFFFFFFFFFFF3ll}, 11, 0xFFF3, "2000:aaa3::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk3", 0xFFFFFFFFFFFFFFF3ll, 11, 0xFFF3, "2000:aaa3::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 2); EXPECT_TRUE(psj.Close() == PersistentStorage::Status::kSuccess); @@ -212,9 +209,8 @@ TEST(PersistentStorageJsonTestSuite, DelNetwork) EXPECT_TRUE(psj.Open() == PersistentStorage::Status::kSuccess); NetworkId newId; - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", XpanId{0xFFFFFFFFFFFFFFF1ll}, 11, 0xFFF1, "2000:aaa1::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", 0xFFFFFFFFFFFFFFF1ll, 11, 0xFFF1, "2000:aaa1::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 0); EXPECT_TRUE(psj.Del(NetworkId(0)) == PersistentStorage::Status::kSuccess); @@ -658,17 +654,14 @@ TEST(PersistentStorageJsonTestSuite, LookupNetwork) // Populate storage with initial data NetworkId newId; - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", XpanId{0xFFFFFFFFFFFFFFF1ll}, 11, 0xFFF1, "2000:aaa1::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk1", 0xFFFFFFFFFFFFFFF1ll, 11, 0xFFF1, "2000:aaa1::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 0); - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk2", XpanId{0xFFFFFFFFFFFFFFF2ll}, 11, 0xFFF2, "2000:aaa2::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk2", 0xFFFFFFFFFFFFFFF2ll, 11, 0xFFF2, "2000:aaa2::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 1); - EXPECT_TRUE( - psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk3", XpanId{0xFFFFFFFFFFFFFFF3ll}, 11, 0xFFF3, "2000:aaa3::0/8", 1}, - newId) == PersistentStorage::Status::kSuccess); + EXPECT_TRUE(psj.Add(Network{EMPTY_ID, EMPTY_ID, "nwk3", 0xFFFFFFFFFFFFFFF3ll, 11, 0xFFF3, "2000:aaa3::0/8", 1}, + newId) == PersistentStorage::Status::kSuccess); EXPECT_TRUE(newId.mId == 2); // The test diff --git a/src/app/ps/registry.cpp b/src/app/ps/registry.cpp index eef177afe..15956b8ba 100644 --- a/src/app/ps/registry.cpp +++ b/src/app/ps/registry.cpp @@ -175,7 +175,7 @@ Registry::Status Registry::Add(BorderAgent const &aValue) // Decided: update network aName in the network entity if ((aValue.mPresentFlags & BorderAgent::kExtendedPanIdBit) != 0) { - nwk.mXpan = XpanId{aValue.mExtendedPanId}; + nwk.mXpan = aValue.mExtendedPanId; } else { @@ -199,7 +199,7 @@ Registry::Status Registry::Add(BorderAgent const &aValue) nwk.mName = aValue.mNetworkName; } nwk.mDomainId = dom.mId; - nwk.mXpan = XpanId{aValue.mExtendedPanId}; + nwk.mXpan = aValue.mExtendedPanId; // Provisionally set network's CCM flag considering // advertised Connection Mode. @@ -293,7 +293,7 @@ Registry::Status Registry::GetBorderRouter(const BorderRouterId aRawId, BorderRo return MapStatus(mStorage->Get(aRawId, br)); } -Registry::Status Registry::GetBorderRoutersInNetwork(const XpanId aXpan, BorderRouterArray &aRet) +Registry::Status Registry::GetBorderRoutersInNetwork(uint64_t aXpan, BorderRouterArray &aRet) { Network nwk; BorderRouter pred; @@ -306,7 +306,7 @@ Registry::Status Registry::GetBorderRoutersInNetwork(const XpanId aXpan, BorderR return status; } -Registry::Status Registry::GetNetworkXpansInDomain(const std::string &aDomainName, XpanIdArray &aRet) +Registry::Status Registry::GetNetworkXpansInDomain(const std::string &aDomainName, std::vector &aRet) { NetworkArray networks; Registry::Status status = GetNetworksInDomain(aDomainName, networks); @@ -421,9 +421,9 @@ Registry::Status Registry::GetAllNetworks(NetworkArray &aRet) return status; } -Registry::Status Registry::GetNetworkXpansByAliases(const StringArray &aAliases, - XpanIdArray &aRet, - StringArray &aUnresolved) +Registry::Status Registry::GetNetworkXpansByAliases(const StringArray &aAliases, + std::vector &aRet, + StringArray &aUnresolved) { NetworkArray networks; Registry::Status status = GetNetworksByAliases(aAliases, networks, aUnresolved); @@ -484,23 +484,32 @@ Registry::Status Registry::GetNetworksByAliases(const StringArray &aAliases, } else { - Network nwk; - XpanId xpid; - PanId pid; + Network nwk; + uint64_t xpid; + uint16_t pid; status = Registry::Status::kNotFound; - if (xpid.FromHex(alias) == ERROR_NONE) + if (utils::ParseInteger(xpid, alias) == ERROR_NONE) { status = GetNetworkByXpan(xpid, nwk); } + if (status != Registry::Status::kSuccess) { status = GetNetworkByName(alias, nwk); } - if (status != Registry::Status::kSuccess && pid.FromHex(alias) == ERROR_NONE) + + if (status != Registry::Status::kSuccess) { - status = GetNetworkByPan(alias, nwk); + bool hasHexPrefix = utils::ToLower(alias.substr(0, 2)) != "0x"; + std::string aliasAsPanId = std::string(hasHexPrefix ? "0x" : "") + alias; + + if (utils::ParseInteger(pid, aliasAsPanId) == ERROR_NONE) + { + status = GetNetworkByPan(aliasAsPanId, nwk); + } } + if (status == Registry::Status::kSuccess) { networks.push_back(nwk); @@ -533,7 +542,7 @@ Registry::Status Registry::ForgetCurrentNetwork() return SetCurrentNetwork(NetworkId{}); } -Registry::Status Registry::SetCurrentNetwork(const XpanId &aXpan) +Registry::Status Registry::SetCurrentNetwork(uint64_t aXpan) { Network nwk; Registry::Status status; @@ -571,7 +580,7 @@ Registry::Status Registry::GetCurrentNetwork(Network &aRet) : MapStatus(mStorage->Get(networkId, aRet)); } -Registry::Status Registry::GetCurrentNetworkXpan(XpanId &aRet) +Registry::Status Registry::GetCurrentNetworkXpan(uint64_t &aRet) { Registry::Status status; Network nwk; @@ -592,7 +601,7 @@ Registry::Status Registry::LookupOne(const Network &aPred, Network &aRet) return status; } -Registry::Status Registry::GetNetworkByXpan(const XpanId &aXpan, Network &aRet) +Registry::Status Registry::GetNetworkByXpan(uint64_t aXpan, Network &aRet) { Network nwk{}; nwk.mXpan = aXpan; @@ -608,9 +617,10 @@ Registry::Status Registry::GetNetworkByName(const std::string &aName, Network &a Registry::Status Registry::GetNetworkByPan(const std::string &aPan, Network &aRet) { - Network nwk{}; - PanId panId; - if (panId.FromHex(aPan).GetCode() != ErrorCode::kNone) + Network nwk{}; + uint16_t panId = 0; + + if (utils::ParseInteger(panId, aPan).GetCode() != ErrorCode::kNone) { return Registry::Status::kError; } @@ -618,7 +628,7 @@ Registry::Status Registry::GetNetworkByPan(const std::string &aPan, Network &aRe return LookupOne(nwk, aRet); } -Registry::Status Registry::GetDomainNameByXpan(const XpanId &aXpan, std::string &aName) +Registry::Status Registry::GetDomainNameByXpan(uint64_t aXpan, std::string &aName) { Registry::Status status; Network nwk; @@ -753,13 +763,13 @@ Registry::Status Registry::DropDomainIfEmpty(const DomainId &aDomainId) Registry::Status Registry::DeleteBorderRoutersInDomain(const std::string &aDomainName) { - Domain dom; - DomainArray doms; - Registry::Status status; - Network current; - XpanIdArray xpans; - StringArray aAliases; - StringArray aUnresolved; + Domain dom; + DomainArray doms; + Registry::Status status; + Network current; + std::vector xpans; + StringArray aAliases; + StringArray aUnresolved; dom.mName = aDomainName; VerifyOrExit((status = MapStatus(mStorage->Lookup(dom, doms))) == Registry::Status::kSuccess); @@ -779,7 +789,7 @@ Registry::Status Registry::DeleteBorderRoutersInDomain(const std::string &aDomai for (auto &&xpan : xpans) { - aAliases.push_back(XpanId(xpan).str()); + aAliases.push_back(utils::Hex(xpan)); } VerifyOrExit((status = DeleteBorderRoutersInNetworks(aAliases, aUnresolved)) == Registry::Status::kSuccess); VerifyOrExit(aUnresolved.empty(), status = Registry::Status::kAmbiguity); diff --git a/src/app/ps/registry.hpp b/src/app/ps/registry.hpp index af89604aa..97cc57df8 100644 --- a/src/app/ps/registry.hpp +++ b/src/app/ps/registry.hpp @@ -136,7 +136,7 @@ class Registry * @param[in] aXpan network's XPAN ID * @param[out] aRetValue resultant array of @ref BorderRouter records */ - Status GetBorderRoutersInNetwork(XpanId aXpan, BorderRouterArray &aRetValue); + Status GetBorderRoutersInNetwork(uint64_t aXpan, BorderRouterArray &aRetValue); /** * Get networks of the domain @@ -154,7 +154,7 @@ class Registry * @param[out] aRetValue vector of network XPAN IDs belonging to the domain * @note Network XPAN IDs will be appended to the end of the output vector */ - Status GetNetworkXpansInDomain(const std::string &aDomainName, XpanIdArray &aRetValue); + Status GetNetworkXpansInDomain(const std::string &aDomainName, std::vector &aRetValue); /** * Get list of all domains @@ -191,12 +191,14 @@ class Registry * @param[out] aRetValue list of network XPAN IDs * @param[out] aUnresolved list of aliases failed to resolve */ - Status GetNetworkXpansByAliases(const StringArray &aAliases, XpanIdArray &aRetValue, StringArray &aUnresolved); + Status GetNetworkXpansByAliases(const StringArray &aAliases, + std::vector &aRetValue, + StringArray &aUnresolved); /** * Set current network. */ - Status SetCurrentNetwork(const XpanId &aXpan); + Status SetCurrentNetwork(uint64_t aXpan); /** * Set current network by border router specified. @@ -219,7 +221,7 @@ class Registry * * @param [out] aRetValue current network XPAN ID */ - Status GetCurrentNetworkXpan(XpanId &aRetValue); + Status GetCurrentNetworkXpan(uint64_t &aRetValue); /** * Get network with specified extended PAN id @@ -232,7 +234,7 @@ class Registry * @li @ref REG_DATA_INVALID is more than one network was found * @li @ref REG_ERROR on other errors */ - Status GetNetworkByXpan(const XpanId &aXpan, Network &aRetValue); + Status GetNetworkByXpan(uint64_t aXpan, Network &aRetValue); /** * Get network with specified name @@ -271,7 +273,7 @@ class Registry * @li @ref REG_AMBUGUITY is more than one network was found * @li @ref REG_ERROR on other errors */ - Status GetDomainNameByXpan(const XpanId &aXpan, std::string &aName); + Status GetDomainNameByXpan(uint64_t aXpan, std::string &aName); /** * Remove border router record. diff --git a/src/app/ps/registry_entries.cpp b/src/app/ps/registry_entries.cpp index 2ee214942..e9586800e 100644 --- a/src/app/ps/registry_entries.cpp +++ b/src/app/ps/registry_entries.cpp @@ -156,8 +156,8 @@ void to_json(json &aJson, const Network &aValue) aJson = json{{JSON_ID, aValue.mId}, {JSON_DOM_REF, aValue.mDomainId}, {JSON_NAME, aValue.mName}, - {JSON_PAN, std::string(aValue.mPan)}, - {JSON_XPAN, std::string(aValue.mXpan)}, + {JSON_PAN, utils::Hex(aValue.mPan)}, + {JSON_XPAN, utils::Hex(aValue.mXpan)}, {JSON_CHANNEL, aValue.mChannel}, {JSON_MLP, aValue.mMlp}, {JSON_CCM, aValue.mCcm}}; @@ -165,15 +165,16 @@ void to_json(json &aJson, const Network &aValue) void from_json(const json &aJson, Network &aValue) { + std::string hexStr; + aJson.at(JSON_ID).get_to(aValue.mId); aJson.at(JSON_DOM_REF).get_to(aValue.mDomainId); aJson.at(JSON_NAME).get_to(aValue.mName); - std::string hexStr; aJson.at(JSON_PAN).get_to(hexStr); - SuccessOrThrow(aValue.mPan.FromHex(hexStr)); + SuccessOrThrow(utils::ParseInteger(aValue.mPan, hexStr)); aJson.at(JSON_XPAN).get_to(hexStr); - SuccessOrThrow(aValue.mXpan.FromHex(hexStr)); + SuccessOrThrow(utils::ParseInteger(aValue.mXpan, hexStr)); aJson.at(JSON_CHANNEL).get_to(aValue.mChannel); aJson.at(JSON_MLP).get_to(aValue.mMlp); @@ -407,14 +408,14 @@ Domain::Domain() { } -Network::Network(NetworkId const &aId, - DomainId const &aDomainId, - std::string const &aName, - XpanId const &aXpan, - unsigned int const aChannel, - uint16_t const aPan, - std::string const &aMlp, - int const aCcm) +Network::Network(const NetworkId &aId, + const DomainId &aDomainId, + const std::string &aName, + uint64_t aXpan, + unsigned int aChannel, + uint16_t aPan, + const std::string &aMlp, + int aCcm) : mId(aId) , mDomainId(aDomainId) , mName(aName) @@ -427,7 +428,7 @@ Network::Network(NetworkId const &aId, } Network::Network() - : Network(EMPTY_ID, EMPTY_ID, "", XpanId{}, 0, 0, "", -1) + : Network(EMPTY_ID, EMPTY_ID, "", 0, 0, 0, "", -1) { } diff --git a/src/app/ps/registry_entries.hpp b/src/app/ps/registry_entries.hpp index cf69e8c4e..39dac0cf7 100644 --- a/src/app/ps/registry_entries.hpp +++ b/src/app/ps/registry_entries.hpp @@ -138,20 +138,20 @@ struct Network NetworkId mId; /**< unique mId in registry */ DomainId mDomainId; /**< reference to the domain the network belongs to */ std::string mName; /**< network name */ - XpanId mXpan; /**< Extended PAN_ID */ + uint64_t mXpan; /**< Extended PAN_ID */ unsigned int mChannel; /**< network channel */ - PanId mPan; /**< PAN_ID */ + uint16_t mPan; /**< PAN_ID */ std::string mMlp; /**< Mesh-local prefix */ int mCcm; /**< Commercial commissioning mode;<0 not set, * 0 false, >0 true */ - Network(NetworkId const &aId, - DomainId const &aDomainId, - std::string const &aName, - XpanId const &aXpan, + Network(const NetworkId &aId, + const DomainId &aDomainId, + const std::string &aName, + uint64_t aXpan, unsigned int aChannel, uint16_t aPan, - std::string const &aMlp, + const std::string &aMlp, int aCcm); Network(); }; diff --git a/src/app/ps/registry_test.cpp b/src/app/ps/registry_test.cpp index 4b8a2bc7a..867b28386 100644 --- a/src/app/ps/registry_test.cpp +++ b/src/app/ps/registry_test.cpp @@ -31,15 +31,19 @@ * The file implements registry test suite. */ -#include #include +#include + +#include #include "app/cli/console.hpp" #include "app/ps/registry.hpp" +#include "common/utils.hpp" #define INFO(str) Console::Write(str) using namespace ot::commissioner::persistent_storage; +using namespace ot::commissioner::utils; using namespace ot::commissioner; const char json_path[] = "./tmp/registry_test.json"; @@ -87,7 +91,7 @@ TEST(RegJson, CreateBorderRouterFromBorderAgent) EXPECT_TRUE(reg.GetBorderRouter(BorderRouterId{0}, ret_val) == Registry::Status::kSuccess); EXPECT_TRUE(ret_val.mNetworkId.mId == 0); Network nwk; - EXPECT_TRUE(reg.GetNetworkByXpan(XpanId{0}, nwk) == Registry::Status::kSuccess); + EXPECT_TRUE(reg.GetNetworkByXpan(0, nwk) == Registry::Status::kSuccess); EXPECT_TRUE(nwk.mDomainId.mId == EMPTY_ID); } @@ -159,7 +163,7 @@ TEST(RegJson, CreateBorderRouterFromBorderAgent) EXPECT_TRUE((val.mAgent.mPresentFlags & (BorderAgent::kAddrBit | BorderAgent::kPortBit)) == (BorderAgent::kAddrBit | BorderAgent::kPortBit)); EXPECT_TRUE(val.mAgent.mAddr == "1.1.1.1"); - INFO(val.mAgent.mNetworkName + " : " + XpanId(val.mAgent.mExtendedPanId).str()); + INFO(val.mAgent.mNetworkName + " : " + utils::Hex(val.mAgent.mExtendedPanId)); Network nwk; EXPECT_TRUE(reg.GetNetworkByXpan(val.mAgent.mExtendedPanId, nwk) == Registry::Status::kSuccess); // Modify explicitly diff --git a/src/common/error.cpp b/src/common/error.cpp index 37c0f6acb..7467cd9ad 100644 --- a/src/common/error.cpp +++ b/src/common/error.cpp @@ -42,7 +42,7 @@ namespace ot { namespace commissioner { // Returns the std::string representation of the status code. -static std::string ErrorCodeToString(ErrorCode code) +std::string ErrorCodeToString(ErrorCode code) { switch (code) { diff --git a/src/common/utils.hpp b/src/common/utils.hpp index fa900ffaf..c48286fae 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -42,6 +42,7 @@ #include "commissioner/defines.hpp" #include "commissioner/error.hpp" +#include "common/error_macros.hpp" #define ASSERT(aCondition) \ do \ @@ -188,6 +189,33 @@ std::string ToLower(const std::string &aStr); bool CaseInsensitiveEqual(const std::string &aLhs, const std::string &aRhs); +/** + * Returns the Hex string of an integer with zero paddings. + * + * For example, it returns "0xface" for integer 0xface. + */ +template std::string Hex(T aInteger) +{ + return std::string("0x") + Hex(Encode(aInteger)); +} + +template Error ParseInteger(T &aInteger, const std::string &aStr) +{ + Error error; + uint64_t integer; + char *endPtr = nullptr; + + integer = strtoull(aStr.c_str(), &endPtr, 0); + + VerifyOrExit(endPtr != nullptr && endPtr == aStr.c_str() + aStr.length(), + error = ERROR_INVALID_ARGS("{} is not a valid integer", aStr)); + + aInteger = integer; + +exit: + return error; +} + } // namespace utils } // namespace commissioner diff --git a/src/common/utils_test.cpp b/src/common/utils_test.cpp index 0d228e63a..b454e59db 100644 --- a/src/common/utils_test.cpp +++ b/src/common/utils_test.cpp @@ -118,6 +118,29 @@ TEST(UtilsTest, HexEncodingDecoding_DecodingHexStringWithInvalidCharactersShould EXPECT_EQ(utils::Hex(buf, "00010g"), ErrorCode::kInvalidArgs); } +TEST(UtilsTest, ParseInteger_HexStringShouldSuccess) +{ + int value; + + EXPECT_EQ(utils::ParseInteger(value, "0xface"), ErrorCode::kNone); + EXPECT_EQ(value, 0xface); +} + +TEST(UtilsTest, ParseInteger_DecimalStringShouldSuccess) +{ + int value; + + EXPECT_EQ(utils::ParseInteger(value, "65535"), ErrorCode::kNone); + EXPECT_EQ(value, 65535); +} + +TEST(UtilsTest, ParseInteger_IntegerWithTrailingNonDigitsShouldFail) +{ + int value; + + EXPECT_EQ(utils::ParseInteger(value, "0xfacegg"), ErrorCode::kInvalidArgs); +} + } // namespace commissioner } // namespace ot diff --git a/src/java/commissioner.i b/src/java/commissioner.i index 4d65d5aa2..65f381e37 100644 --- a/src/java/commissioner.i +++ b/src/java/commissioner.i @@ -169,18 +169,12 @@ namespace commissioner { uint32_t aTimeout); %ignore Commissioner::RequestToken(Handler aHandler, const std::string &aAddr, uint16_t aPort); - // Remove operators and move constructor of Error, XpanId, PanId. + // Remove operators and move constructor of Error. %ignore Error::operator=(const Error &aError); %ignore Error::Error(Error &&aError) noexcept; %ignore Error::operator=(Error &&aError) noexcept; %ignore Error::operator==(const Error &aOther) const; %ignore Error::operator!=(const Error &aOther) const; - %ignore XpanId::operator==(const XpanId &aOther) const; - %ignore XpanId::operator!=(const uint64_t aOther) const; - %ignore XpanId::operator<(const XpanId aOther) const; - %ignore XpanId::operator std::string() const; - %ignore PanId::operator=(uint16_t aValue); - %ignore PanId::operator uint16_t() const; %ignore operator==(const Error &aError, const ErrorCode &aErrorCode); %ignore operator!=(const Error &aError, const ErrorCode &aErrorCode); %ignore operator==(const ErrorCode &aErrorCode, const Error &aError); diff --git a/src/library/commissioner_impl.cpp b/src/library/commissioner_impl.cpp index de96b1586..57370d5a7 100644 --- a/src/library/commissioner_impl.cpp +++ b/src/library/commissioner_impl.cpp @@ -1488,7 +1488,7 @@ Error CommissionerImpl::DecodeActiveOperationalDataset(ActiveOperationalDataset if (auto extendedPanId = tlvSet[tlv::Type::kExtendedPanId]) { - dataset.mExtendedPanId = XpanId{utils::Decode(extendedPanId->GetValue())}; + dataset.mExtendedPanId = extendedPanId->GetValue(); dataset.mPresentFlags |= ActiveOperationalDataset::kExtendedPanIdBit; } @@ -1627,7 +1627,7 @@ Error CommissionerImpl::EncodeActiveOperationalDataset(coap::Request if (aDataset.mPresentFlags & ActiveOperationalDataset::kExtendedPanIdBit) { - SuccessOrExit(error = AppendTlv(aRequest, {tlv::Type::kExtendedPanId, aDataset.mExtendedPanId.mValue})); + SuccessOrExit(error = AppendTlv(aRequest, {tlv::Type::kExtendedPanId, aDataset.mExtendedPanId})); } if (aDataset.mPresentFlags & ActiveOperationalDataset::kMeshLocalPrefixBit) @@ -1647,7 +1647,7 @@ Error CommissionerImpl::EncodeActiveOperationalDataset(coap::Request if (aDataset.mPresentFlags & ActiveOperationalDataset::kPanIdBit) { - SuccessOrExit(error = AppendTlv(aRequest, {tlv::Type::kPanId, aDataset.mPanId.mValue})); + SuccessOrExit(error = AppendTlv(aRequest, {tlv::Type::kPanId, aDataset.mPanId})); } if (aDataset.mPresentFlags & ActiveOperationalDataset::kPSKcBit) diff --git a/src/library/network_data.cpp b/src/library/network_data.cpp index cbb8cc396..c062d2e44 100644 --- a/src/library/network_data.cpp +++ b/src/library/network_data.cpp @@ -123,123 +123,6 @@ std::string Ipv6PrefixToString(ByteArray aPrefix) return addr.ToString() + "/" + std::to_string(prefixLength); } -XpanId::XpanId(uint64_t val) - : mValue(val) -{ -} - -XpanId::XpanId() - : XpanId(kEmptyXpanId) -{ -} - -std::string XpanId::str() const -{ - return std::string(*this); -} - -bool XpanId::operator==(const XpanId &aOther) const -{ - return mValue == aOther.mValue; -} - -bool XpanId::operator!=(const XpanId &aOther) const -{ - return !(*this == aOther); -} - -bool XpanId::operator<(const XpanId &aOther) const -{ - return mValue < aOther.mValue; -} - -XpanId::operator std::string() const -{ - std::ostringstream value; - value << std::uppercase << std::hex << std::setw(sizeof(mValue) * 2) << std::setfill('0') << mValue; - return value.str(); -} - -/** - * Converts hex string to the corresponding integer type. - * @attention Makes no validity checks. - */ -Error XpanId::FromHex(const std::string &aInput) -{ - mValue = 0; - - std::string input = aInput; - if (utils::ToLower(input.substr(0, 2)) == "0x") - { - input = input.substr(2); - } - if (input.empty() || input.length() > 16) - return ERROR_BAD_FORMAT("{}: wrong XPAN ID string length", input.length()); - for (auto c : input) - { - if (!std::isxdigit(c)) - { - return ERROR_BAD_FORMAT("{}: not a hex string", input); - } - } - - std::istringstream is(input); - is >> std::hex >> mValue; - return ERROR_NONE; -} - -PanId::PanId(uint16_t aValue) - : mValue(aValue) -{ -} - -PanId::PanId() - : PanId(kEmptyPanId) -{ -} - -PanId &PanId::operator=(uint16_t aValue) -{ - mValue = aValue; - return *this; -} - -PanId::operator uint16_t() const -{ - return mValue; -} - -PanId::operator std::string() const -{ - std::ostringstream value; - value << "0x" << std::uppercase << std::hex << std::setw(sizeof(mValue) * 2) << std::setfill('0') << mValue; - return value.str(); -} - -Error PanId::FromHex(const std::string &aInput) -{ - mValue = 0; - - std::string input = aInput; - if (utils::ToLower(input.substr(0, 2)) == "0x") - { - input = input.substr(2); - } - if (input.empty() || input.length() > 4) - return ERROR_BAD_FORMAT("{}: wrong PAN ID string length", input.length()); - for (auto c : input) - { - if (!std::isxdigit(c)) - { - return ERROR_BAD_FORMAT("{}: not a hex string", input); - } - } - - std::istringstream is(input); - is >> std::hex >> mValue; - return ERROR_NONE; -} - ActiveOperationalDataset::ActiveOperationalDataset() : mActiveTimestamp(Timestamp::Cur()) , mPresentFlags(kActiveTimestampBit) diff --git a/tests/integration/test_domain_syntax.sh b/tests/integration/test_domain_syntax.sh index d41d3074b..468edd644 100755 --- a/tests/integration/test_domain_syntax.sh +++ b/tests/integration/test_domain_syntax.sh @@ -75,7 +75,7 @@ test_select_identify() { send_command_to_commissioner "network select thread1" send_command_to_commissioner "network identify" "thread1" - send_command_to_commissioner "network select 2222222222222222" + send_command_to_commissioner "network select 0x2222222222222222" send_command_to_commissioner "network identify" "thread2" send_command_to_commissioner "network select none" @@ -151,10 +151,10 @@ test_start_stop_mn_all() { commissioner_mdns_scan_import ${CUR_DIR}/etc/br_scan_initial send_command_to_commissioner "start --nwk all" - send_command_to_commissioner "active --nwk all" 'DEAD00BEEF00CAFE.*true' - send_command_to_commissioner "sessionid --nwk all" 'DEAD00BEEF00CAFE": \d*' - send_command_to_commissioner "opdataset get active --nwk all" 'DEAD00BEEF00CAFE": {' - send_command_to_commissioner "commdataset get --nwk all" 'DEAD00BEEF00CAFE": {' + send_command_to_commissioner "active --nwk all" '0xdead00beef00cafe.*true' + send_command_to_commissioner "sessionid --nwk all" '0xdead00beef00cafe": \d*' + send_command_to_commissioner "opdataset get active --nwk all" '0xdead00beef00cafe": {' + send_command_to_commissioner "commdataset get --nwk all" '0xdead00beef00cafe": {' send_command_to_commissioner "opdataset set securitypolicy 1000 ff --nwk all" send_command_to_commissioner "opdataset get pending --nwk all" send_command_to_commissioner "stop --nwk all" @@ -179,12 +179,12 @@ test_start_stop_mn_other() { mdns_hosts_map_addresses commissioner_mdns_scan_import ${CUR_DIR}/etc/br_scan_initial - send_command_to_commissioner "network select 2222222222222222" + send_command_to_commissioner "network select 0x2222222222222222" send_command_to_commissioner "start --nwk other" send_command_to_commissioner "active --nwk other" '1111111111111111": false' - send_command_to_commissioner "sessionid --nwk other" 'DEAD00BEEF00CAFE": \d*' - send_command_to_commissioner "opdataset get active --nwk other" 'DEAD00BEEF00CAFE": {' - send_command_to_commissioner "commdataset get --nwk other" 'DEAD00BEEF00CAFE": {' + send_command_to_commissioner "sessionid --nwk other" '0xdead00beef00cafe": \d*' + send_command_to_commissioner "opdataset get active --nwk other" '0xdead00beef00cafe": {' + send_command_to_commissioner "commdataset get --nwk other" '0xdead00beef00cafe": {' send_command_to_commissioner "opdataset set securitypolicy 1000 ff --nwk other" send_command_to_commissioner "opdataset get pending --nwk other" send_command_to_commissioner "stop --nwk other" @@ -212,10 +212,10 @@ test_start_stop_mn_dom() { set -x send_command_to_commissioner "start --dom TestDomainName" - send_command_to_commissioner "active --dom TestDomainName" '2222222222222222": false' - send_command_to_commissioner "sessionid --dom TestDomainName" 'DEAD00BEEF00CAFE": \d*' - send_command_to_commissioner "opdataset get active --dom TestDomainName" 'DEAD00BEEF00CAFE": {' - send_command_to_commissioner "commdataset get --dom TestDomainName" 'DEAD00BEEF00CAFE": {' + send_command_to_commissioner "active --dom TestDomainName" '0x2222222222222222": false' + send_command_to_commissioner "sessionid --dom TestDomainName" '0xdead00beef00cafe": \d*' + send_command_to_commissioner "opdataset get active --dom TestDomainName" '0xdead00beef00cafe": {' + send_command_to_commissioner "commdataset get --dom TestDomainName" '0xdead00beef00cafe": {' send_command_to_commissioner "opdataset set securitypolicy 1000 ff --dom TestDomainName" send_command_to_commissioner "opdataset get pending --dom TestDomainName" send_command_to_commissioner "stop --dom TestDomainName"