Skip to content

Commit

Permalink
[cli] add state command (openthread#293)
Browse files Browse the repository at this point in the history
This commit adds `state` command to show the state
("active", "petitioning", "connected", or "disabled").
  • Loading branch information
librasungirl authored Aug 6, 2024
1 parent c3a7fca commit 639e627
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/app/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@ To start establishing secure session, and petitioning as the active Commissioner
>
```
### State
Show the state of the commissioner candidate
- "active": It's now an active commissioner accepted and will periodically sends keep alive messages in background
- "petitioning": It's attempting to petition as active commissioner
- "connected": The commissioner candidate has successfully established secure session with border agent
- "disabled": It's not an active commissioner, nor securely connected with the Border Agent
```shell
> state
connected
[done]
>
```
### Active
Upon success, the Commissioner periodically sends keep alive messages in background. The Commissioner now is in the active state:
Expand Down
30 changes: 30 additions & 0 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,15 @@ const std::map<std::string, Interpreter::Evaluator> &Interpreter::mEvaluatorMap
{"announce", &Interpreter::ProcessAnnounce}, {"panid", &Interpreter::ProcessPanId},
{"energy", &Interpreter::ProcessEnergy}, {"exit", &Interpreter::ProcessExit},
{"quit", &Interpreter::ProcessExit}, {"help", &Interpreter::ProcessHelp},
{"state", &Interpreter::ProcessState},
};

const std::map<std::string, std::string> &Interpreter::mUsageMap = *new std::map<std::string, std::string>{
{"config", "config get admincode\n"
"config set admincode <9-digits-thread-administrator-passcode>\n"
"config get pskc\n"
"config set pskc <pskc-hex-string>"},
{"state", "state"},
{"start", "start <border-agent-addr> <border-agent-port> [--connect-only]\n"
"start [ --nwk <network-alias-list | --dom <domain-alias>]"},
{"stop", "stop\n"
Expand Down Expand Up @@ -288,6 +290,7 @@ const std::vector<Interpreter::StringArray> &Interpreter::mMultiNetworkSyntax =
Interpreter::StringArray{"start"},
Interpreter::StringArray{"stop"},
Interpreter::StringArray{"active"},
Interpreter::StringArray{"state"},
Interpreter::StringArray{"sessionid"},
Interpreter::StringArray{"bbrdataset", "get"},
Interpreter::StringArray{"commdataset", "get"},
Expand Down Expand Up @@ -942,6 +945,33 @@ Interpreter::Value Interpreter::ProcessConfig(const Expression &aExpr)
return value;
}

Interpreter::Value Interpreter::ProcessState(const Expression &)
{
Value value;
CommissionerAppPtr commissioner = nullptr;

SuccessOrExit(value = mJobManager->GetSelectedCommissioner(commissioner));

switch (commissioner->GetState())
{
case State::kDisabled:
value = std::string("disabled");
break;
case State::kConnected:
value = std::string("connected");
break;
case State::kPetitioning:
value = std::string("petitioning");
break;
case State::kActive:
value = std::string("active");
break;
}

exit:
return value;
}

Interpreter::Value Interpreter::ProcessStart(const Expression &aExpr)
{
Value value;
Expand Down
1 change: 1 addition & 0 deletions src/app/cli/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Interpreter

Value ProcessConfig(const Expression &aExpr);
Value ProcessStart(const Expression &aExpr);
Value ProcessState(const Expression &aExpr);
Value ProcessStop(const Expression &aExpr);
Value ProcessActive(const Expression &aExpr);
Value ProcessToken(const Expression &aExpr);
Expand Down
31 changes: 31 additions & 0 deletions src/app/cli/interpreter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,37 @@ TEST_F(InterpreterTestSuite, PC_StartLegacySyntaxSuccess)
EXPECT_TRUE(value.HasNoError());
}

TEST_F(InterpreterTestSuite, PC_State)
{
TestContext ctx;
InitContext(ctx);

EXPECT_CALL(*ctx.mDefaultCommissionerObject, GetState())
.Times(4)
.WillOnce(Return(State::kActive))
.WillOnce(Return(State::kPetitioning))
.WillOnce(Return(State::kConnected))
.WillOnce(Return(State::kDisabled));

Interpreter::Expression expr;
Interpreter::Value value;
expr = ctx.mInterpreter.ParseExpression("state");
value = ctx.mInterpreter.Eval(expr);
EXPECT_STREQ("active", value.ToString().c_str());

expr = ctx.mInterpreter.ParseExpression("state");
value = ctx.mInterpreter.Eval(expr);
EXPECT_STREQ("petitioning", value.ToString().c_str());

expr = ctx.mInterpreter.ParseExpression("state");
value = ctx.mInterpreter.Eval(expr);
EXPECT_STREQ("connected", value.ToString().c_str());

expr = ctx.mInterpreter.ParseExpression("state");
value = ctx.mInterpreter.Eval(expr);
EXPECT_STREQ("disabled", value.ToString().c_str());
}

TEST_F(InterpreterTestSuite, PC_StartLegacyConnectSyntaxErrorFails)
{
TestContext ctx;
Expand Down
5 changes: 5 additions & 0 deletions src/app/commissioner_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ bool CommissionerApp::IsActive() const
return mCommissioner->IsActive();
}

State CommissionerApp::GetState() const
{
return mCommissioner->GetState();
}

bool CommissionerApp::IsCcmMode() const
{
return mCommissioner->IsCcmMode();
Expand Down
2 changes: 2 additions & 0 deletions src/app/commissioner_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class CommissionerApp : public CommissionerHandler

MOCKABLE Error Connect(const std::string &aBorderAgentAddr, uint16_t aBorderAgentPort);

MOCKABLE State GetState() const;

MOCKABLE Error Start(std::string &aExistingCommissionerId,
const std::string &aBorderAgentAddr,
uint16_t aBorderAgentPort);
Expand Down
5 changes: 5 additions & 0 deletions src/app/commissioner_app_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ bool CommissionerApp::IsActive() const
return false;
}

State CommissionerApp::GetState() const
{
return State::kDisabled;
}

Error CommissionerApp::SaveNetworkData(const std::string &aFilename)
{
UNUSED(aFilename);
Expand Down
2 changes: 2 additions & 0 deletions src/app/commissioner_app_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class CommissionerAppMock : public ::ot::commissioner::CommissionerApp
MOCK_METHOD(void, Stop, ());
MOCK_METHOD(void, CancelRequests, ());
MOCK_METHOD(bool, IsActive, (), (const));
MOCK_METHOD(State, GetState, (), (const));

MOCK_METHOD(Error, SaveNetworkData, (const std::string &));
MOCK_METHOD(Error, SyncNetworkData, ());

Expand Down

0 comments on commit 639e627

Please sign in to comment.