29
29
30
30
#ifdef ENABLE_CHIP_SHELL
31
31
#include < lib/shell/Engine.h>
32
-
32
+ # include < map >
33
33
using namespace chip ::Shell;
34
+ #define MATTER_CLI_LOG (message ) (streamer_printf(streamer_get(), message))
34
35
#endif /* ENABLE_CHIP_SHELL */
35
36
36
37
using namespace chip ;
37
38
using namespace chip ::app::Clusters;
38
39
39
40
app::Clusters::TemperatureControl::AppSupportedTemperatureLevelsDelegate sAppSupportedTemperatureLevelsDelegate ;
40
41
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 )
42
56
{
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)
49
60
{
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 ());
53
62
}
54
- else if (! strcmp (argv[ 0 ], " run " ) )
63
+ for ( auto const & it : map_cmd_errstate )
55
64
{
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 ());
59
66
}
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 ))
61
73
{
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 ;
65
76
}
66
- else if (!strcmp (argv[0 ], " error" ))
77
+
78
+ if (map_cmd_opstate.find (argv[0 ]) != map_cmd_opstate.end ())
67
79
{
68
- OperationalState::Structs::ErrorStateStruct::Type err ;
80
+ OperationalState::GetOperationalStateInstance ()-> SetOperationalState (map_cmd_opstate. at (argv[ 0 ])) ;
69
81
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 )
91
83
{
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
+ }
94
96
}
95
- OperationalState::GetOperationalStateInstance ()->OnOperationalErrorDetected (err);
96
- OperationalState::GetOperationalStateInstance ()->SetOperationalState (
97
- to_underlying (OperationalState::OperationalStateEnum::kError ));
98
97
}
99
98
else
100
99
{
101
- ChipLogError (Shell, " Invalid State to set" );
100
+ inputErr = true ;
101
+ }
102
+ exit :
103
+ if (inputErr)
104
+ {
105
+ InvalidStateHandler ();
102
106
return CHIP_ERROR_INVALID_ARGUMENT;
103
107
}
108
+
104
109
return CHIP_NO_ERROR;
105
110
}
111
+ #endif /* ENABLE_CHIP_SHELL */
106
112
107
113
void LaundryWasherApp::AppTask::PreInitMatterStack ()
108
114
{
@@ -121,7 +127,9 @@ void LaundryWasherApp::AppTask::AppMatter_RegisterCustomCliCommands()
121
127
#ifdef ENABLE_CHIP_SHELL
122
128
/* Register application commands */
123
129
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'] " },
125
133
};
126
134
Engine::Root ().RegisterCommands (kCommands , sizeof (kCommands ) / sizeof (kCommands [0 ]));
127
135
#endif
0 commit comments