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" ,
54
+ (uint8_t ) OperationalState::OperationalStateEnum::kError } };
55
+
56
+ static void InvalidStateHandler (void )
42
57
{
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" ))
58
+ ChipLogError (Shell, " Invalid State/Error to set" );
59
+ MATTER_CLI_LOG (" Invalid. Supported commands are:\n " );
60
+ for (auto const & it : map_cmd_opstate)
49
61
{
50
- ChipLogDetail (Shell, " OpSState : Set to %s state" , argv[0 ]);
51
- OperationalState::GetOperationalStateInstance ()->SetOperationalState (
52
- to_underlying (OperationalState::OperationalStateEnum::kStopped ));
62
+ MATTER_CLI_LOG ((" \t opstate " + it.first + " \n " ).c_str ());
53
63
}
54
- else if (! strcmp (argv[ 0 ], " run " ) )
64
+ for ( auto const & it : map_cmd_errstate )
55
65
{
56
- ChipLogDetail (Shell, " OpSState : Set to %s state" , argv[0 ]);
57
- OperationalState::GetOperationalStateInstance ()->SetOperationalState (
58
- to_underlying (OperationalState::OperationalStateEnum::kRunning ));
66
+ MATTER_CLI_LOG ((" \t opstate error " + it.first + " \n " ).c_str ());
59
67
}
60
- else if (!strcmp (argv[0 ], " pause" ))
68
+ }
69
+
70
+ static CHIP_ERROR cliOpState (int argc, char * argv[])
71
+ {
72
+ bool inputErr = false ;
73
+ if ((argc != 1 ) && (argc != 2 ))
61
74
{
62
- ChipLogDetail (Shell, " OpSState : Set to %s state" , argv[0 ]);
63
- OperationalState::GetOperationalStateInstance ()->SetOperationalState (
64
- to_underlying (OperationalState::OperationalStateEnum::kPaused ));
75
+ inputErr = true ;
76
+ goto exit ;
65
77
}
66
- else if (!strcmp (argv[0 ], " error" ))
78
+
79
+ if (map_cmd_opstate.find (argv[0 ]) != map_cmd_opstate.end ())
67
80
{
68
- OperationalState::Structs::ErrorStateStruct::Type err ;
81
+ OperationalState::GetOperationalStateInstance ()-> SetOperationalState (map_cmd_opstate. at (argv[ 0 ])) ;
69
82
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
83
+ if (!strcmp (argv[0 ], " error" ) && argc == 2 )
91
84
{
92
- ChipLogError (Shell, " Invalid Error State to set" );
93
- return CHIP_ERROR_INVALID_ARGUMENT;
85
+ OperationalState::Structs::ErrorStateStruct::Type err;
86
+ if (map_cmd_errstate.find (argv[1 ]) != map_cmd_errstate.end ())
87
+ {
88
+ ChipLogDetail (Shell, " OpSState_error : Set to %s state" , argv[1 ]);
89
+ err.errorStateID = map_cmd_errstate.at (argv[1 ]);
90
+ OperationalState::GetOperationalStateInstance ()->OnOperationalErrorDetected (err);
91
+ }
92
+ else
93
+ {
94
+ inputErr = true ;
95
+ goto exit ;
96
+ }
94
97
}
95
- OperationalState::GetOperationalStateInstance ()->OnOperationalErrorDetected (err);
96
- OperationalState::GetOperationalStateInstance ()->SetOperationalState (
97
- to_underlying (OperationalState::OperationalStateEnum::kError ));
98
98
}
99
99
else
100
100
{
101
- ChipLogError (Shell, " Invalid State to set" );
101
+ inputErr = true ;
102
+ }
103
+ exit :
104
+ if (inputErr)
105
+ {
106
+ InvalidStateHandler ();
102
107
return CHIP_ERROR_INVALID_ARGUMENT;
103
108
}
109
+
104
110
return CHIP_NO_ERROR;
105
111
}
112
+ #endif /* ENABLE_CHIP_SHELL */
106
113
107
114
void LaundryWasherApp::AppTask::PreInitMatterStack ()
108
115
{
@@ -121,7 +128,9 @@ void LaundryWasherApp::AppTask::AppMatter_RegisterCustomCliCommands()
121
128
#ifdef ENABLE_CHIP_SHELL
122
129
/* Register application commands */
123
130
static const shell_command_t kCommands [] = {
124
- { .cmd_func = cliOpState, .cmd_name = " opstate" , .cmd_help = " Set the Operational State" },
131
+ { .cmd_func = cliOpState,
132
+ .cmd_name = " opstate" ,
133
+ .cmd_help = " Set the Operational State. Usage:[stop|run|pause|dock|error 'state'] " },
125
134
};
126
135
Engine::Root ().RegisterCommands (kCommands , sizeof (kCommands ) / sizeof (kCommands [0 ]));
127
136
#endif
0 commit comments