22/* *
33 * @file minbasecli.cpp
44 * @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
5- * @date 02-04 -2022
6- * @version 1.1.1
5+ * @date 09-07 -2022
6+ * @version 1.2.0
77 *
88 * @section DESCRIPTION
99 *
@@ -59,6 +59,10 @@ MINBASECLI::MINBASECLI()
5959{
6060 this ->initialized = false ;
6161 this ->received_bytes = 0 ;
62+ this ->num_added_commands = 0 ;
63+ this ->cli_result .argc = 0U ;
64+ this ->cli_result .argv [0 ][0 ] = ' \0 ' ;
65+ this ->cli_result .cmd [0 ] = ' \0 ' ;
6266 this ->rx_read [0 ] = ' \0 ' ;
6367}
6468
@@ -78,6 +82,93 @@ bool MINBASECLI::setup(void* iface, const uint32_t baud_rate)
7882 return true ;
7983}
8084
85+ /* *
86+ * @details
87+ * This function check if provided arguments are valid and if they there is
88+ * enough space in the added commands array to store a new command callback
89+ * info, and add a new command callback element to the list according to
90+ * provided arguments.
91+ */
92+ bool MINBASECLI::add_cmd (const char * command,
93+ void (*callback)(int argc, char * argv[]),
94+ const char* description)
95+ {
96+ t_cmd_cb_info cmd_cb_info;
97+ size_t cmd_len = 0U ;
98+ size_t cmd_description_len = 0U ;
99+
100+ // Check if there is enough space to add a new command
101+ if (num_added_commands >= MINBASECLI_MAX_CMD_TO_ADD)
102+ return false ;
103+
104+ // Check if provided argument are valid
105+ if ( (command == NULL ) || (callback == NULL ) || (description == NULL ) )
106+ return false ;
107+
108+ // Check and limit provided arguments lengths
109+ cmd_len = strlen (command);
110+ cmd_description_len = strlen (description);
111+ if (cmd_len >= MINBASECLI_MAX_CMD_LEN)
112+ cmd_len = MINBASECLI_MAX_CMD_LEN - 1U ;
113+ if (cmd_description_len >= MINBASECLI_MAX_CMD_DESCRIPTION)
114+ cmd_description_len = MINBASECLI_MAX_CMD_DESCRIPTION - 1U ;
115+
116+ // Create a new t_cmd_cb_info element with provided command data
117+ strncpy (cmd_cb_info.command , command, cmd_len);
118+ cmd_cb_info.command [cmd_len] = ' \0 ' ;
119+ strncpy (cmd_cb_info.description , description, cmd_description_len);
120+ cmd_cb_info.description [cmd_description_len] = ' \0 ' ;
121+ cmd_cb_info.callback = callback;
122+
123+ // Add the new command to the list of binded commands and increase the
124+ // number of added commands
125+ added_commands[num_added_commands] = cmd_cb_info;
126+ num_added_commands = num_added_commands + 1U ;
127+
128+ return true ;
129+ }
130+
131+ /* *
132+ * @details
133+ * This function calls to manage the CLI to check if there is any new command
134+ * received avaliable to be handled, then check if the received command is one
135+ * of the added inside CLI component to be handle through a callback, and call
136+ * to the corresponding callback for it.
137+ */
138+ bool MINBASECLI::run ()
139+ {
140+ bool cmd_found = false ;
141+
142+ // Do nothing if there is no added commands
143+ if (num_added_commands == 0U )
144+ return false ;
145+
146+ // Check if there is any new command received by the CLI
147+ if (manage (&cli_result) == false )
148+ return false ;
149+
150+ // Check if th command is added in the callback handle list
151+ for (uint8_t i = 0U ; i < num_added_commands; i++)
152+ {
153+ // If command is found in the callbacks list, call to the callback
154+ if (strcmp (cli_result.cmd , added_commands[i].command ) == 0U )
155+ {
156+ // Compose array of pointer for arguments
157+ char * ptr_argv[MINBASECLI_MAX_ARGV];
158+ for (int i = 0 ; i < MINBASECLI_MAX_ARGV; i++)
159+ ptr_argv[i] = cli_result.argv [i];
160+
161+ // Call to command callback
162+ added_commands[i].callback (cli_result.argc , ptr_argv);
163+
164+ cmd_found = true ;
165+ break ;
166+ }
167+ }
168+
169+ return cmd_found;
170+ }
171+
81172/* *
82173 * @details
83174 * This function checks and get any received data from the CLI interface and
0 commit comments