From 9eaea9664613ae927cf46a6969033c848e013f30 Mon Sep 17 00:00:00 2001 From: kangping Date: Sat, 14 Mar 2020 16:53:34 +0800 Subject: [PATCH 1/4] [petition] returns its ID if there is already an active commissioner --- include/commissioner/commissioner.hpp | 22 +++++++++++++++++----- src/app/cli/interpreter.cpp | 7 ++++++- src/app/commissioner_app.cpp | 5 +++-- src/app/commissioner_app.hpp | 2 +- src/library/commissioner_impl.cpp | 20 ++++++++++++++------ src/library/commissioner_impl.hpp | 6 +++--- src/library/commissioner_safe.cpp | 13 ++++++++++--- src/library/commissioner_safe.hpp | 4 ++-- tests/unit/test_commissioner_impl.cpp | 4 +++- 9 files changed, 59 insertions(+), 24 deletions(-) diff --git a/include/commissioner/commissioner.hpp b/include/commissioner/commissioner.hpp index 408bf939f..79135a1dd 100644 --- a/include/commissioner/commissioner.hpp +++ b/include/commissioner/commissioner.hpp @@ -173,7 +173,18 @@ class Commissioner * @note @p aResponseData is guaranteed to be not null only when @p aError == Error::kNone. * Otherwise, @p aResponseData should never be accessed. */ - template using Handler = std::function; + template using Handler = std::function; + + /** + * The petition result handler. + * + * @param[in] aActiveCommissionerId A active commissioner Id. nullable. + * @param[in] aError A error code. + * + * @note There is another active commissioner if @p aError != Error::kNone + * and @p aActiveCommissionerId is not null. + */ + using PetitionHandler = std::function; /** * The MGMT_PANID_CONFLICT.ans handler. @@ -422,7 +433,7 @@ class Commissioner * * @note The commissioner will be first connected to the network if it is not. */ - virtual void Petition(ErrorHandler aHandler, const std::string &aAddr, uint16_t aPort) = 0; + virtual void Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) = 0; /** * @brief Synchronously petition to a Thread network. @@ -431,12 +442,13 @@ class Commissioner * If succeed, a keep-alive message will be periodically sent to keep itself active. * It will not return until errors happened, timeouted or succeed. * - * @param[in] aAddr A border agent address. - * @param[in] aPort A border agent port. + * @param[out] aActiveCommissionerId A Commissioner ID of an already active commissioner. + * @param[in] aAddr A border agent address. + * @param[in] aPort A border agent port. * * @return Error::kNone, succeed; otherwise, failed; */ - virtual Error Petition(const std::string &aAddr, uint16_t aPort) = 0; + virtual Error Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) = 0; /** * @brief Asynchronously resign from the commissioner role. diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index ed8bdf718..eb265a84b 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -316,13 +316,18 @@ Interpreter::Value Interpreter::ProcessStart(const Expression &aExpr) { Error error = Error::kInvalidArgs; std::string msg; + std::string activeCommissionerId; uint16_t port; VerifyOrExit(aExpr.size() >= 3, msg = Usage(aExpr)); SuccessOrExit(ParseInteger(port, aExpr[2]), msg = aExpr[2]); - SuccessOrExit(error = mCommissioner->Start(aExpr[1], port)); + SuccessOrExit(error = mCommissioner->Start(activeCommissionerId, aExpr[1], port)); exit: + if (!activeCommissionerId.empty()) + { + msg = "there is already an active commissioner: " + activeCommissionerId; + } return {error, msg}; } diff --git a/src/app/commissioner_app.cpp b/src/app/commissioner_app.cpp index d8de83c77..e34f66961 100644 --- a/src/app/commissioner_app.cpp +++ b/src/app/commissioner_app.cpp @@ -135,11 +135,12 @@ const BorderAgent *CommissionerApp::GetBorderAgent(const std::string &aNetworkNa return nullptr; } -Error CommissionerApp::Start(const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort) +Error CommissionerApp::Start(std::string &aActiveCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort) { Error error = Error::kNone; - SuccessOrExit(error = mCommissioner->Petition(aBorderAgentAddr, aBorderAgentPort)); + // We need to report the active commissioner ID even when pulling network data failed. + SuccessOrExit(error = mCommissioner->Petition(aActiveCommissionerId, aBorderAgentAddr, aBorderAgentPort)); SuccessOrExit(error = PullNetworkData()); exit: diff --git a/src/app/commissioner_app.hpp b/src/app/commissioner_app.hpp index ad83f49e4..012866324 100644 --- a/src/app/commissioner_app.hpp +++ b/src/app/commissioner_app.hpp @@ -82,7 +82,7 @@ class CommissionerApp // Set @p aNetworkName to empty to match any network name. const BorderAgent *GetBorderAgent(const std::string &aNetworkName); - Error Start(const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort); + Error Start(std::string &aActiveCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort); void Stop(); void AbortRequests(); diff --git a/src/library/commissioner_impl.cpp b/src/library/commissioner_impl.cpp index 85aae8476..2e73ee4ab 100644 --- a/src/library/commissioner_impl.cpp +++ b/src/library/commissioner_impl.cpp @@ -290,12 +290,12 @@ void CommissionerImpl::Stop() event_base_loopbreak(mEventBase); } -void CommissionerImpl::Petition(ErrorHandler aHandler, const std::string &aAddr, uint16_t aPort) +void CommissionerImpl::Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) { auto onConnected = [this, aHandler](Error aError) { if (aError != Error::kNone) { - aHandler(aError); + aHandler(nullptr, aError); } else { @@ -1101,7 +1101,7 @@ void CommissionerImpl::SetEnergyReportHandler(EnergyReportHandler aHandler) mEnergyReportHandler = aHandler; } -void CommissionerImpl::SendPetition(ErrorHandler aHandler) +void CommissionerImpl::SendPetition(PetitionHandler aHandler) { Error error = Error::kNone; coap::Request request{coap::Type::kConfirmable, coap::Code::kPost}; @@ -1111,6 +1111,8 @@ void CommissionerImpl::SendPetition(ErrorHandler aHandler) tlv::TlvSet tlvSet; tlv::TlvPtr stateTlv = nullptr; tlv::TlvPtr sessionIdTlv = nullptr; + tlv::TlvPtr commissionerIdTlv = nullptr; + std::string anotherCommissionerId; SuccessOrExit(error = aError); @@ -1121,7 +1123,13 @@ void CommissionerImpl::SendPetition(ErrorHandler aHandler) VerifyOrExit(stateTlv != nullptr, error = Error::kNotFound); VerifyOrExit(stateTlv->IsValid(), error = Error::kBadFormat); - VerifyOrExit(stateTlv->GetValueAsInt8() == tlv::kStateAccept, error = Error::kReject); + if (stateTlv->GetValueAsInt8() != tlv::kStateAccept) { + commissionerIdTlv = tlvSet[tlv::Type::kCommissionerId]; + if (commissionerIdTlv != nullptr && commissionerIdTlv->IsValid()) { + anotherCommissionerId = commissionerIdTlv->GetValueAsString(); + } + ExitNow(error = Error::kReject); + } sessionIdTlv = tlvSet[tlv::Type::kCommissionerSessionId]; VerifyOrExit(sessionIdTlv != nullptr, error = Error::kNotFound); @@ -1141,7 +1149,7 @@ void CommissionerImpl::SendPetition(ErrorHandler aHandler) { mState = State::kDisabled; } - aHandler(error); + aHandler(anotherCommissionerId.empty() ? nullptr : &anotherCommissionerId, error); }; VerifyOrExit(mState == State::kDisabled, error = Error::kInvalidState); @@ -1163,7 +1171,7 @@ void CommissionerImpl::SendPetition(ErrorHandler aHandler) exit: if (error != Error::kNone) { - aHandler(error); + aHandler(nullptr, error); } } diff --git a/src/library/commissioner_impl.hpp b/src/library/commissioner_impl.hpp index 3a9a811f3..44a8f4b00 100644 --- a/src/library/commissioner_impl.hpp +++ b/src/library/commissioner_impl.hpp @@ -108,8 +108,8 @@ class CommissionerImpl : public Commissioner void Disconnect() override; - void Petition(ErrorHandler aHandler, const std::string &aAddr, uint16_t aPort) override; - Error Petition(const std::string &, uint16_t) override { return Error::kNotImplemented; } + void Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) override; + Error Petition(std::string &, const std::string &, uint16_t) override { return Error::kNotImplemented; } void Resign(ErrorHandler aHandler) override; Error Resign() override { return Error::kNotImplemented; } @@ -234,7 +234,7 @@ class CommissionerImpl : public Commissioner static Error EncodeCommissionerDataset(coap::Request &aRequest, const CommissionerDataset &aDataset); static ByteArray GetCommissionerDatasetTlvs(uint16_t aDatasetFlags); - void SendPetition(ErrorHandler aHandler); + void SendPetition(PetitionHandler aHandler); // Set @p aKeepAlive to false to resign the commissioner role. void SendKeepAlive(Timer &aTimer, bool aKeepAlive = true); diff --git a/src/library/commissioner_safe.cpp b/src/library/commissioner_safe.cpp index e27008c7a..49f25651f 100644 --- a/src/library/commissioner_safe.cpp +++ b/src/library/commissioner_safe.cpp @@ -190,15 +190,22 @@ void CommissionerSafe::AbortRequests() PushAsyncRequest([=]() { mImpl.AbortRequests(); }); } -void CommissionerSafe::Petition(ErrorHandler aHandler, const std::string &aAddr, uint16_t aPort) +void CommissionerSafe::Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) { PushAsyncRequest([=]() { mImpl.Petition(aHandler, aAddr, aPort); }); } -Error CommissionerSafe::Petition(const std::string &aAddr, uint16_t aPort) +Error CommissionerSafe::Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) { std::promise pro; - auto wait = [&pro](Error aError) { pro.set_value(aError); }; + auto wait = [&pro, &aActiveCommissionerId](const std::string *activeCommissionerId, Error error) + { + if (activeCommissionerId != nullptr) + { + aActiveCommissionerId = *activeCommissionerId; + } + pro.set_value(error); + }; Petition(wait, aAddr, aPort); return pro.get_future().get(); diff --git a/src/library/commissioner_safe.hpp b/src/library/commissioner_safe.hpp index c5b8968e1..49e4d9df4 100644 --- a/src/library/commissioner_safe.hpp +++ b/src/library/commissioner_safe.hpp @@ -104,8 +104,8 @@ class CommissionerSafe : public Commissioner void Disconnect() override; - void Petition(ErrorHandler aHandler, const std::string &aAddr, uint16_t aPort) override; - Error Petition(const std::string &aAddr, uint16_t aPort) override; + void Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) override; + Error Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) override; void Resign(ErrorHandler aHandler) override; Error Resign() override; diff --git a/tests/unit/test_commissioner_impl.cpp b/tests/unit/test_commissioner_impl.cpp index 07d302e81..014e4123e 100644 --- a/tests/unit/test_commissioner_impl.cpp +++ b/tests/unit/test_commissioner_impl.cpp @@ -58,7 +58,9 @@ TEST_CASE("commissioner-impl-not-implemented-APIs", "[comm-impl]") std::list baList; REQUIRE(commImpl.Discover(baList) == Error::kNotImplemented); REQUIRE(commImpl.Connect("::1", 5684) == Error::kNotImplemented); - REQUIRE(commImpl.Petition("::1", 5684) == Error::kNotImplemented); + + std::string activeCommissionerId; + REQUIRE(commImpl.Petition(activeCommissionerId, "::1", 5684) == Error::kNotImplemented); REQUIRE(commImpl.Resign() == Error::kNotImplemented); CommissionerDataset commDataset; From c1cf4794e50d42f47fb915f876d9fee69c48d475 Mon Sep 17 00:00:00 2001 From: kangping Date: Mon, 16 Mar 2020 11:45:01 +0800 Subject: [PATCH 2/4] make pretty --- src/app/commissioner_app.cpp | 4 +++- src/library/commissioner_impl.cpp | 10 ++++++---- src/library/commissioner_safe.cpp | 3 +-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/app/commissioner_app.cpp b/src/app/commissioner_app.cpp index e34f66961..b62e5843b 100644 --- a/src/app/commissioner_app.cpp +++ b/src/app/commissioner_app.cpp @@ -135,7 +135,9 @@ const BorderAgent *CommissionerApp::GetBorderAgent(const std::string &aNetworkNa return nullptr; } -Error CommissionerApp::Start(std::string &aActiveCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort) +Error CommissionerApp::Start(std::string & aActiveCommissionerId, + const std::string &aBorderAgentAddr, + uint16_t aBorderAgentPort) { Error error = Error::kNone; diff --git a/src/library/commissioner_impl.cpp b/src/library/commissioner_impl.cpp index 2e73ee4ab..3f7c24867 100644 --- a/src/library/commissioner_impl.cpp +++ b/src/library/commissioner_impl.cpp @@ -1109,8 +1109,8 @@ void CommissionerImpl::SendPetition(PetitionHandler aHandler) auto onResponse = [this, aHandler](const coap::Response *aResponse, Error aError) { Error error = Error::kNone; tlv::TlvSet tlvSet; - tlv::TlvPtr stateTlv = nullptr; - tlv::TlvPtr sessionIdTlv = nullptr; + tlv::TlvPtr stateTlv = nullptr; + tlv::TlvPtr sessionIdTlv = nullptr; tlv::TlvPtr commissionerIdTlv = nullptr; std::string anotherCommissionerId; @@ -1123,9 +1123,11 @@ void CommissionerImpl::SendPetition(PetitionHandler aHandler) VerifyOrExit(stateTlv != nullptr, error = Error::kNotFound); VerifyOrExit(stateTlv->IsValid(), error = Error::kBadFormat); - if (stateTlv->GetValueAsInt8() != tlv::kStateAccept) { + if (stateTlv->GetValueAsInt8() != tlv::kStateAccept) + { commissionerIdTlv = tlvSet[tlv::Type::kCommissionerId]; - if (commissionerIdTlv != nullptr && commissionerIdTlv->IsValid()) { + if (commissionerIdTlv != nullptr && commissionerIdTlv->IsValid()) + { anotherCommissionerId = commissionerIdTlv->GetValueAsString(); } ExitNow(error = Error::kReject); diff --git a/src/library/commissioner_safe.cpp b/src/library/commissioner_safe.cpp index 49f25651f..6d07e97cc 100644 --- a/src/library/commissioner_safe.cpp +++ b/src/library/commissioner_safe.cpp @@ -198,8 +198,7 @@ void CommissionerSafe::Petition(PetitionHandler aHandler, const std::string &aAd Error CommissionerSafe::Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) { std::promise pro; - auto wait = [&pro, &aActiveCommissionerId](const std::string *activeCommissionerId, Error error) - { + auto wait = [&pro, &aActiveCommissionerId](const std::string *activeCommissionerId, Error error) { if (activeCommissionerId != nullptr) { aActiveCommissionerId = *activeCommissionerId; From 37c47eb3e5d1f2bd81491f015bd0d59cd0dc5c0e Mon Sep 17 00:00:00 2001 From: kangping Date: Wed, 18 Mar 2020 10:52:57 +0800 Subject: [PATCH 3/4] rename 'activeCommissionerId' to 'existingCommissionerId' --- include/commissioner/commissioner.hpp | 31 ++++++++++++++++----------- include/commissioner/error.hpp | 2 +- src/app/cli/interpreter.cpp | 8 +++---- src/app/commissioner_app.cpp | 4 ++-- src/app/commissioner_app.hpp | 2 +- src/library/commissioner_impl.cpp | 6 +++--- src/library/commissioner_safe.cpp | 8 +++---- src/library/commissioner_safe.hpp | 2 +- tests/unit/test_commissioner_impl.cpp | 4 ++-- 9 files changed, 36 insertions(+), 31 deletions(-) diff --git a/include/commissioner/commissioner.hpp b/include/commissioner/commissioner.hpp index 79135a1dd..3f9327e29 100644 --- a/include/commissioner/commissioner.hpp +++ b/include/commissioner/commissioner.hpp @@ -160,7 +160,7 @@ class Commissioner /** * The response handler of a general TMF request. * - * @param[in] aError A error code. + * @param[in] aError An error code. */ using ErrorHandler = std::function; @@ -168,7 +168,7 @@ class Commissioner * The response handler of a general TMF request. * * @param[in] aResponseData A response data. nullable. - * @param[in] aError A error code. + * @param[in] aError An error code. * * @note @p aResponseData is guaranteed to be not null only when @p aError == Error::kNone. * Otherwise, @p aResponseData should never be accessed. @@ -178,13 +178,13 @@ class Commissioner /** * The petition result handler. * - * @param[in] aActiveCommissionerId A active commissioner Id. nullable. - * @param[in] aError A error code. + * @param[in] aExistingCommissionerId The Existing commissioner Id. nullable. + * @param[in] aError An error code. * - * @note There is another active commissioner if @p aError != Error::kNone - * and @p aActiveCommissionerId is not null. + * @note There is an exiting active commissioner if @p aError != Error::kNone + * and @p aExistingCommissionerId is not null. */ - using PetitionHandler = std::function; + using PetitionHandler = std::function; /** * The MGMT_PANID_CONFLICT.ans handler. @@ -192,7 +192,7 @@ class Commissioner * @param[in] aPeerAddr A peer address that sent this MGMT_PANID_CONFLICT.ans request. * @param[in] aChannelMask A channel mask the peer scanned at. * @param[in] aPanId A PAN ID that has a conflict. - * @param[in] aError A error code. + * @param[in] aError An error code. * * @note @p aPeerAddr, aChannelMask and aPanId are guaranteed to * be not null only when @p aError == Error::kNone. Otherwise, @@ -207,7 +207,7 @@ class Commissioner * @param[in] aPeerAddr A peer address that sent this MGMT_PANID_CONFLICT.ans request. * @param[in] aChannelMask A channel mask the peer scanned at. * @param[in] aEnergyList A list of measured energy level in dBm. - * @param[in] aError A error code. + * @param[in] aError An error code. * * @note @p aPeerAddr, aChannelMask and aEnergyList are guaranteed to * be not null only when @p aError == Error::kNone. Otherwise, @@ -442,13 +442,18 @@ class Commissioner * If succeed, a keep-alive message will be periodically sent to keep itself active. * It will not return until errors happened, timeouted or succeed. * - * @param[out] aActiveCommissionerId A Commissioner ID of an already active commissioner. - * @param[in] aAddr A border agent address. - * @param[in] aPort A border agent port. + * @param[out] aExistingCommissionerId The existing active commissioner ID. + * @param[in] aAddr A border agent address. + * @param[in] aPort A border agent port. * * @return Error::kNone, succeed; otherwise, failed; + * + * @note @p aExistingCommissionerId is valid only when return value + * is not Error::kNone and itself is not empty. Otherwise, + * its content should never be accessed. + * */ - virtual Error Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) = 0; + virtual Error Petition(std::string &aExistingCommissionerId, const std::string &aAddr, uint16_t aPort) = 0; /** * @brief Asynchronously resign from the commissioner role. diff --git a/include/commissioner/error.hpp b/include/commissioner/error.hpp index b6ad2a661..e9f58fea2 100644 --- a/include/commissioner/error.hpp +++ b/include/commissioner/error.hpp @@ -65,7 +65,7 @@ enum class Error : int /** * @brief Convert Error to printable string. * - * @param aError A error code. + * @param aError An error code. * * @return The printable string of the error. */ diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index eb265a84b..aa01f896a 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -316,17 +316,17 @@ Interpreter::Value Interpreter::ProcessStart(const Expression &aExpr) { Error error = Error::kInvalidArgs; std::string msg; - std::string activeCommissionerId; + std::string existingCommissionerId; uint16_t port; VerifyOrExit(aExpr.size() >= 3, msg = Usage(aExpr)); SuccessOrExit(ParseInteger(port, aExpr[2]), msg = aExpr[2]); - SuccessOrExit(error = mCommissioner->Start(activeCommissionerId, aExpr[1], port)); + SuccessOrExit(error = mCommissioner->Start(existingCommissionerId, aExpr[1], port)); exit: - if (!activeCommissionerId.empty()) + if (!existingCommissionerId.empty()) { - msg = "there is already an active commissioner: " + activeCommissionerId; + msg = "there is an existing active commissioner: " + existingCommissionerId; } return {error, msg}; } diff --git a/src/app/commissioner_app.cpp b/src/app/commissioner_app.cpp index b62e5843b..754118abe 100644 --- a/src/app/commissioner_app.cpp +++ b/src/app/commissioner_app.cpp @@ -135,14 +135,14 @@ const BorderAgent *CommissionerApp::GetBorderAgent(const std::string &aNetworkNa return nullptr; } -Error CommissionerApp::Start(std::string & aActiveCommissionerId, +Error CommissionerApp::Start(std::string & aExistingCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort) { Error error = Error::kNone; // We need to report the active commissioner ID even when pulling network data failed. - SuccessOrExit(error = mCommissioner->Petition(aActiveCommissionerId, aBorderAgentAddr, aBorderAgentPort)); + SuccessOrExit(error = mCommissioner->Petition(aExistingCommissionerId, aBorderAgentAddr, aBorderAgentPort)); SuccessOrExit(error = PullNetworkData()); exit: diff --git a/src/app/commissioner_app.hpp b/src/app/commissioner_app.hpp index 012866324..eb3044d01 100644 --- a/src/app/commissioner_app.hpp +++ b/src/app/commissioner_app.hpp @@ -82,7 +82,7 @@ class CommissionerApp // Set @p aNetworkName to empty to match any network name. const BorderAgent *GetBorderAgent(const std::string &aNetworkName); - Error Start(std::string &aActiveCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort); + Error Start(std::string &aExistingCommissionerId, const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort); void Stop(); void AbortRequests(); diff --git a/src/library/commissioner_impl.cpp b/src/library/commissioner_impl.cpp index 3f7c24867..c0295589b 100644 --- a/src/library/commissioner_impl.cpp +++ b/src/library/commissioner_impl.cpp @@ -1112,7 +1112,7 @@ void CommissionerImpl::SendPetition(PetitionHandler aHandler) tlv::TlvPtr stateTlv = nullptr; tlv::TlvPtr sessionIdTlv = nullptr; tlv::TlvPtr commissionerIdTlv = nullptr; - std::string anotherCommissionerId; + std::string existingCommissionerId; SuccessOrExit(error = aError); @@ -1128,7 +1128,7 @@ void CommissionerImpl::SendPetition(PetitionHandler aHandler) commissionerIdTlv = tlvSet[tlv::Type::kCommissionerId]; if (commissionerIdTlv != nullptr && commissionerIdTlv->IsValid()) { - anotherCommissionerId = commissionerIdTlv->GetValueAsString(); + existingCommissionerId = commissionerIdTlv->GetValueAsString(); } ExitNow(error = Error::kReject); } @@ -1151,7 +1151,7 @@ void CommissionerImpl::SendPetition(PetitionHandler aHandler) { mState = State::kDisabled; } - aHandler(anotherCommissionerId.empty() ? nullptr : &anotherCommissionerId, error); + aHandler(existingCommissionerId.empty() ? nullptr : &existingCommissionerId, error); }; VerifyOrExit(mState == State::kDisabled, error = Error::kInvalidState); diff --git a/src/library/commissioner_safe.cpp b/src/library/commissioner_safe.cpp index 6d07e97cc..c05340d3f 100644 --- a/src/library/commissioner_safe.cpp +++ b/src/library/commissioner_safe.cpp @@ -195,13 +195,13 @@ void CommissionerSafe::Petition(PetitionHandler aHandler, const std::string &aAd PushAsyncRequest([=]() { mImpl.Petition(aHandler, aAddr, aPort); }); } -Error CommissionerSafe::Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) +Error CommissionerSafe::Petition(std::string &aExistingCommissionerId, const std::string &aAddr, uint16_t aPort) { std::promise pro; - auto wait = [&pro, &aActiveCommissionerId](const std::string *activeCommissionerId, Error error) { - if (activeCommissionerId != nullptr) + auto wait = [&pro, &aExistingCommissionerId](const std::string *existingCommissionerId, Error error) { + if (existingCommissionerId != nullptr) { - aActiveCommissionerId = *activeCommissionerId; + aExistingCommissionerId = *existingCommissionerId; } pro.set_value(error); }; diff --git a/src/library/commissioner_safe.hpp b/src/library/commissioner_safe.hpp index 49e4d9df4..d13e02bcc 100644 --- a/src/library/commissioner_safe.hpp +++ b/src/library/commissioner_safe.hpp @@ -105,7 +105,7 @@ class CommissionerSafe : public Commissioner void Disconnect() override; void Petition(PetitionHandler aHandler, const std::string &aAddr, uint16_t aPort) override; - Error Petition(std::string &aActiveCommissionerId, const std::string &aAddr, uint16_t aPort) override; + Error Petition(std::string &aExistingCommissionerId, const std::string &aAddr, uint16_t aPort) override; void Resign(ErrorHandler aHandler) override; Error Resign() override; diff --git a/tests/unit/test_commissioner_impl.cpp b/tests/unit/test_commissioner_impl.cpp index 014e4123e..df2c31775 100644 --- a/tests/unit/test_commissioner_impl.cpp +++ b/tests/unit/test_commissioner_impl.cpp @@ -59,8 +59,8 @@ TEST_CASE("commissioner-impl-not-implemented-APIs", "[comm-impl]") REQUIRE(commImpl.Discover(baList) == Error::kNotImplemented); REQUIRE(commImpl.Connect("::1", 5684) == Error::kNotImplemented); - std::string activeCommissionerId; - REQUIRE(commImpl.Petition(activeCommissionerId, "::1", 5684) == Error::kNotImplemented); + std::string existingCommissionerId; + REQUIRE(commImpl.Petition(existingCommissionerId, "::1", 5684) == Error::kNotImplemented); REQUIRE(commImpl.Resign() == Error::kNotImplemented); CommissionerDataset commDataset; From 341957250c3bc46fbc01fb97f8a575bb2abee4b1 Mon Sep 17 00:00:00 2001 From: kangping Date: Wed, 18 Mar 2020 13:17:39 +0800 Subject: [PATCH 4/4] update comments --- src/app/commissioner_app.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/commissioner_app.cpp b/src/app/commissioner_app.cpp index 754118abe..224a738d5 100644 --- a/src/app/commissioner_app.cpp +++ b/src/app/commissioner_app.cpp @@ -141,7 +141,7 @@ Error CommissionerApp::Start(std::string & aExistingCommissionerId, { Error error = Error::kNone; - // We need to report the active commissioner ID even when pulling network data failed. + // We need to report the already active commissioner ID if one exists. SuccessOrExit(error = mCommissioner->Petition(aExistingCommissionerId, aBorderAgentAddr, aBorderAgentPort)); SuccessOrExit(error = PullNetworkData());