Skip to content

Commit 31d95eb

Browse files
committed
[NXP] Adapt CLI to be used both for Zephyr and FreeRTOS
Adapt CLI + update opstate subcommands required by opstate tests
1 parent 23e0f3b commit 31d95eb

File tree

20 files changed

+555
-357
lines changed

20 files changed

+555
-357
lines changed

examples/all-clusters-app/nxp/rt/rw61x/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ rt_executable("all_cluster_app") {
168168
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
169169
"${common_example_dir}/icd/source/ICDUtil.cpp",
170170
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
171-
"${common_example_dir}/matter_cli/source/AppMatterCli.cpp",
172171
]
173172

174173
deps = [ "${chip_root}/examples/${app_common_folder}" ]
@@ -185,6 +184,10 @@ rt_executable("all_cluster_app") {
185184
"${chip_root}/examples/shell/shell_common:shell_common",
186185
"${chip_root}/src/lib/shell:shell",
187186
]
187+
sources += [
188+
"${common_example_dir}/matter_cli/source/AppCLIBase.cpp",
189+
"${common_example_dir}/matter_cli/source/AppCLIFreeRTOS.cpp",
190+
]
188191
}
189192

190193
if (chip_enable_ota_requestor) {

examples/laundry-washer-app/nxp/common/main/AppTask.cpp

+57-49
Original file line numberDiff line numberDiff line change
@@ -29,80 +29,86 @@
2929

3030
#ifdef ENABLE_CHIP_SHELL
3131
#include <lib/shell/Engine.h>
32-
32+
#include <map>
3333
using namespace chip::Shell;
34+
#define MATTER_CLI_LOG(message) (streamer_printf(streamer_get(), message))
3435
#endif /* ENABLE_CHIP_SHELL */
3536

3637
using namespace chip;
3738
using namespace chip::app::Clusters;
3839

3940
app::Clusters::TemperatureControl::AppSupportedTemperatureLevelsDelegate sAppSupportedTemperatureLevelsDelegate;
4041

41-
CHIP_ERROR cliOpState(int argc, char * argv[])
42+
#ifdef ENABLE_CHIP_SHELL
43+
const static std::map<std::string, uint8_t> map_cmd_errstate{
44+
{ "no_error", (uint8_t) OperationalState::ErrorStateEnum::kNoError },
45+
{ "unable_to_start_or_resume", (uint8_t) OperationalState::ErrorStateEnum::kUnableToStartOrResume },
46+
{ "unable_to_complete_operation", (uint8_t) OperationalState::ErrorStateEnum::kUnableToCompleteOperation },
47+
{ "command_invalid_in_state", (uint8_t) OperationalState::ErrorStateEnum::kCommandInvalidInState }
48+
};
49+
50+
const static std::map<std::string, uint8_t> map_cmd_opstate{ { "stop", (uint8_t) OperationalState::OperationalStateEnum::kStopped },
51+
{ "run", (uint8_t) OperationalState::OperationalStateEnum::kRunning },
52+
{ "pause", (uint8_t) OperationalState::OperationalStateEnum::kPaused },
53+
{ "error", (uint8_t) OperationalState::OperationalStateEnum::kError } };
54+
55+
static void InvalidStateHandler(void)
4256
{
43-
if ((argc != 1) && (argc != 2))
44-
{
45-
ChipLogError(Shell, "Target State is missing");
46-
return CHIP_ERROR_INVALID_ARGUMENT;
47-
}
48-
if (!strcmp(argv[0], "stop"))
57+
ChipLogError(Shell, "Invalid State/Error to set");
58+
MATTER_CLI_LOG("Invalid. Supported commands are:\n");
59+
for (auto const & it : map_cmd_opstate)
4960
{
50-
ChipLogDetail(Shell, "OpSState : Set to %s state", argv[0]);
51-
OperationalState::GetOperationalStateInstance()->SetOperationalState(
52-
to_underlying(OperationalState::OperationalStateEnum::kStopped));
61+
MATTER_CLI_LOG(("\t opstate " + it.first + "\n").c_str());
5362
}
54-
else if (!strcmp(argv[0], "run"))
63+
for (auto const & it : map_cmd_errstate)
5564
{
56-
ChipLogDetail(Shell, "OpSState : Set to %s state", argv[0]);
57-
OperationalState::GetOperationalStateInstance()->SetOperationalState(
58-
to_underlying(OperationalState::OperationalStateEnum::kRunning));
65+
MATTER_CLI_LOG(("\t opstate error " + it.first + "\n").c_str());
5966
}
60-
else if (!strcmp(argv[0], "pause"))
67+
}
68+
69+
static CHIP_ERROR cliOpState(int argc, char * argv[])
70+
{
71+
bool inputErr = false;
72+
if ((argc != 1) && (argc != 2))
6173
{
62-
ChipLogDetail(Shell, "OpSState : Set to %s state", argv[0]);
63-
OperationalState::GetOperationalStateInstance()->SetOperationalState(
64-
to_underlying(OperationalState::OperationalStateEnum::kPaused));
74+
inputErr = true;
75+
goto exit;
6576
}
66-
else if (!strcmp(argv[0], "error"))
77+
78+
if (map_cmd_opstate.find(argv[0]) != map_cmd_opstate.end())
6779
{
68-
OperationalState::Structs::ErrorStateStruct::Type err;
80+
OperationalState::GetOperationalStateInstance()->SetOperationalState(map_cmd_opstate.at(argv[0]));
6981
ChipLogDetail(Shell, "OpSState : Set to %s state", argv[0]);
70-
if (!strcmp(argv[1], "no_error"))
71-
{
72-
ChipLogDetail(Shell, "OpSState_error : Error: %s state", argv[1]);
73-
err.errorStateID = (uint8_t) OperationalState::ErrorStateEnum::kNoError;
74-
}
75-
else if (!strcmp(argv[1], "unable_to_start_or_resume"))
76-
{
77-
ChipLogDetail(Shell, "OpSState_error : Error: %s state", argv[1]);
78-
err.errorStateID = (uint8_t) OperationalState::ErrorStateEnum::kUnableToStartOrResume;
79-
}
80-
else if (!strcmp(argv[1], "unable_to_complete_operation"))
81-
{
82-
ChipLogDetail(Shell, "OpSState_error : Error: %s state", argv[1]);
83-
err.errorStateID = (uint8_t) OperationalState::ErrorStateEnum::kUnableToCompleteOperation;
84-
}
85-
else if (!strcmp(argv[1], "command_invalid_in_state"))
86-
{
87-
ChipLogDetail(Shell, "OpSState_error : Error: %s state", argv[1]);
88-
err.errorStateID = (uint8_t) OperationalState::ErrorStateEnum::kCommandInvalidInState;
89-
}
90-
else
82+
if (!strcmp(argv[0], "error") && argc == 2)
9183
{
92-
ChipLogError(Shell, "Invalid Error State to set");
93-
return CHIP_ERROR_INVALID_ARGUMENT;
84+
OperationalState::Structs::ErrorStateStruct::Type err;
85+
if (map_cmd_errstate.find(argv[1]) != map_cmd_errstate.end())
86+
{
87+
ChipLogDetail(Shell, "OpSState_error : Set to %s state", argv[1]);
88+
err.errorStateID = map_cmd_errstate.at(argv[1]);
89+
OperationalState::GetOperationalStateInstance()->OnOperationalErrorDetected(err);
90+
}
91+
else
92+
{
93+
inputErr = true;
94+
goto exit;
95+
}
9496
}
95-
OperationalState::GetOperationalStateInstance()->OnOperationalErrorDetected(err);
96-
OperationalState::GetOperationalStateInstance()->SetOperationalState(
97-
to_underlying(OperationalState::OperationalStateEnum::kError));
9897
}
9998
else
10099
{
101-
ChipLogError(Shell, "Invalid State to set");
100+
inputErr = true;
101+
}
102+
exit:
103+
if (inputErr)
104+
{
105+
InvalidStateHandler();
102106
return CHIP_ERROR_INVALID_ARGUMENT;
103107
}
108+
104109
return CHIP_NO_ERROR;
105110
}
111+
#endif /* ENABLE_CHIP_SHELL */
106112

107113
void LaundryWasherApp::AppTask::PreInitMatterStack()
108114
{
@@ -121,7 +127,9 @@ void LaundryWasherApp::AppTask::AppMatter_RegisterCustomCliCommands()
121127
#ifdef ENABLE_CHIP_SHELL
122128
/* Register application commands */
123129
static const shell_command_t kCommands[] = {
124-
{ .cmd_func = cliOpState, .cmd_name = "opstate", .cmd_help = "Set the Operational State" },
130+
{ .cmd_func = cliOpState,
131+
.cmd_name = "opstate",
132+
.cmd_help = "Set the Operational State. Usage:[stop|run|pause|dock|error 'state'] " },
125133
};
126134
Engine::Root().RegisterCommands(kCommands, sizeof(kCommands) / sizeof(kCommands[0]));
127135
#endif

examples/laundry-washer-app/nxp/rt/rw61x/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ rt_executable("laundry-washer") {
169169
"${common_example_dir}/device_manager/source/CHIPDeviceManager.cpp",
170170
"${common_example_dir}/icd/source/ICDUtil.cpp",
171171
"${common_example_dir}/matter_button/source/AppMatterButtonEmpty.cpp",
172-
"${common_example_dir}/matter_cli/source/AppMatterCli.cpp",
173172
]
174173

175174
deps = [ "${chip_root}/examples/${app_common_folder}" ]
@@ -190,6 +189,10 @@ rt_executable("laundry-washer") {
190189
"${chip_root}/examples/shell/shell_common:shell_common",
191190
"${chip_root}/src/lib/shell:shell",
192191
]
192+
sources += [
193+
"${common_example_dir}/matter_cli/source/AppCLIBase.cpp",
194+
"${common_example_dir}/matter_cli/source/AppCLIFreeRTOS.cpp",
195+
]
193196
}
194197

195198
if (chip_enable_ota_requestor) {

examples/platform/nxp/common/app_task/include/AppTaskBase.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,22 @@ class AppTaskBase
130130
*/
131131
static void InitServer(intptr_t arg);
132132

133-
/* Commissioning handlers */
134-
virtual void StartCommissioningHandler(void){};
135-
virtual void StopCommissioningHandler(void){};
136-
virtual void SwitchCommissioningStateHandler(void){};
137-
virtual void FactoryResetHandler(void){};
133+
/**
134+
* Commissioning handlers
135+
* Generic implementation is provided within this class
136+
* Can be overridden by a child class
137+
*/
138+
virtual void StartCommissioningHandler(void);
139+
virtual void StopCommissioningHandler(void);
140+
virtual void SwitchCommissioningStateHandler(void);
141+
virtual void FactoryResetHandler(void);
138142

139143
private:
140144
inline static chip::CommonCaseDeviceServerInitParams initParams;
145+
/* Functions used by the public commisioning handlers */
146+
static void StartCommissioning(intptr_t arg);
147+
static void StopCommissioning(intptr_t arg);
148+
static void SwitchCommissioningState(intptr_t arg);
141149
};
142150

143151
/**

examples/platform/nxp/common/app_task/include/AppTaskFreeRTOS.h

+1-14
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,13 @@ class AppTaskFreeRTOS : public AppTaskBase
6565
#endif
6666

6767
/**
68-
* \brief This function register matter CLI and button features.
68+
* \brief This function registers custom matter CLI and button features.
6969
*
7070
* \return CHIP_ERROR
7171
*
7272
*/
7373
virtual CHIP_ERROR AppMatter_Register(void) override;
7474

75-
/* Functions that would be called in the Matter task context */
76-
static void StartCommissioning(intptr_t arg);
77-
static void StopCommissioning(intptr_t arg);
78-
static void SwitchCommissioningState(intptr_t arg);
79-
80-
/* Commissioning handlers */
81-
virtual void StartCommissioningHandler(void) override;
82-
virtual void StopCommissioningHandler(void) override;
83-
virtual void SwitchCommissioningStateHandler(void) override;
84-
85-
/* FactoryResetHandler */
86-
virtual void FactoryResetHandler(void) override;
87-
8875
private:
8976
void DispatchEvent(const AppEvent & event);
9077
};

examples/platform/nxp/common/app_task/include/AppTaskZephyr.h

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class AppTaskZephyr : public AppTaskBase
5454
virtual chip::DeviceLayer::NetworkCommissioning::WiFiDriver * GetWifiDriverInstance(void) override;
5555
#endif
5656

57+
/**
58+
* \brief This function registers applicative features such as custom CLI commands
59+
*
60+
* \return CHIP_ERROR
61+
*
62+
*/
63+
virtual CHIP_ERROR AppMatter_Register(void) override;
64+
5765
private:
5866
void DispatchEvent(const AppEvent & event);
5967
};

examples/platform/nxp/common/app_task/source/AppTaskBase.cpp

+82-6
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ CHIP_ERROR chip::NXP::App::AppTaskBase::Init()
172172
ChipLogError(DeviceLayer, "CHIPDeviceManager.Init() failed: %s", ErrorStr(err));
173173
goto exit;
174174
}
175+
/* Make sure to initialize the Matter CLI which will include the ot-cli first.
176+
* In fact it is mandatory to enable first the ot-cli before initializing the Matter openthread layer
177+
* which would modify some contexts of the openthread instance.
178+
*/
179+
err = AppMatter_Register();
180+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Error during APP features registration"));
175181

176182
#if CONFIG_NET_L2_OPENTHREAD
177183
err = ThreadStackMgr().InitThreadStack();
@@ -218,12 +224,6 @@ CHIP_ERROR chip::NXP::App::AppTaskBase::Init()
218224
}
219225
#endif
220226

221-
err = AppMatter_Register();
222-
if (err != CHIP_NO_ERROR)
223-
{
224-
goto exit;
225-
}
226-
227227
ConfigurationMgr().LogDeviceConfig();
228228

229229
// QR code will be used with CHIP Tool
@@ -257,3 +257,79 @@ CHIP_ERROR chip::NXP::App::AppTaskBase::Init()
257257
exit:
258258
return err;
259259
}
260+
261+
void chip::NXP::App::AppTaskBase::StartCommissioning(intptr_t arg)
262+
{
263+
/* Check the status of the commissioning */
264+
if (ConfigurationMgr().IsFullyProvisioned())
265+
{
266+
ChipLogProgress(DeviceLayer, "Device already commissioned");
267+
}
268+
else if (chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
269+
{
270+
ChipLogProgress(DeviceLayer, "Commissioning window already opened");
271+
}
272+
else
273+
{
274+
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
275+
}
276+
}
277+
278+
void chip::NXP::App::AppTaskBase::StopCommissioning(intptr_t arg)
279+
{
280+
/* Check the status of the commissioning */
281+
if (ConfigurationMgr().IsFullyProvisioned())
282+
{
283+
ChipLogProgress(DeviceLayer, "Device already commissioned");
284+
}
285+
else if (!chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
286+
{
287+
ChipLogProgress(DeviceLayer, "Commissioning window not opened");
288+
}
289+
else
290+
{
291+
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
292+
}
293+
}
294+
295+
void chip::NXP::App::AppTaskBase::SwitchCommissioningState(intptr_t arg)
296+
{
297+
/* Check the status of the commissioning */
298+
if (ConfigurationMgr().IsFullyProvisioned())
299+
{
300+
ChipLogProgress(DeviceLayer, "Device already commissioned");
301+
}
302+
else if (!chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
303+
{
304+
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
305+
}
306+
else
307+
{
308+
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
309+
}
310+
}
311+
312+
void chip::NXP::App::AppTaskBase::StartCommissioningHandler(void)
313+
{
314+
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
315+
PlatformMgr().ScheduleWork(StartCommissioning, 0);
316+
}
317+
318+
void chip::NXP::App::AppTaskBase::StopCommissioningHandler(void)
319+
{
320+
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
321+
PlatformMgr().ScheduleWork(StopCommissioning, 0);
322+
}
323+
324+
void chip::NXP::App::AppTaskBase::SwitchCommissioningStateHandler(void)
325+
{
326+
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
327+
PlatformMgr().ScheduleWork(SwitchCommissioningState, 0);
328+
}
329+
330+
void chip::NXP::App::AppTaskBase::FactoryResetHandler(void)
331+
{
332+
/* Emit the ShutDown event before factory reset */
333+
chip::Server::GetInstance().GenerateShutDownEvent();
334+
chip::Server::GetInstance().ScheduleFactoryReset();
335+
}

0 commit comments

Comments
 (0)