Files
esp-protocols/esp_modem/examples/modem_console/main/console_helper.cpp

113 lines
3.3 KiB
C++
Raw Normal View History

2021-04-15 17:23:37 +02:00
/* Modem console example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
2021-03-16 21:36:13 +01:00
#include "console_helper.hpp"
#include "esp_log.h"
static const char *TAG = "modem_console_helper";
2021-04-18 19:14:22 +02:00
ConsoleCommand::ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f):
2021-03-16 21:36:13 +01:00
func(std::move(f))
{
RegisterCommand(command, help, args);
}
2021-04-18 19:14:22 +02:00
void ConsoleCommand::RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args)
2021-03-16 21:36:13 +01:00
{
assert(last_command <= MAX_REPEAT_NR);
void * common_arg = nullptr;
for (auto it: args) {
switch(it.type) {
case ARG_END:
break;
case STR0:
common_arg = arg_str0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case STR1:
common_arg = arg_str1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT0:
common_arg = arg_int0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT1:
common_arg = arg_int1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
2021-03-24 21:06:27 +01:00
case LIT0:
common_arg = arg_lit0(it.shortopts, it.longopts, it.glossary);
break;
2021-03-16 21:36:13 +01:00
}
if (common_arg) {
arg_table.emplace_back(common_arg);
} else {
ESP_LOGE(TAG, "Creating argument parser failed for %s", it.glossary);
abort();
}
}
arg_table.emplace_back( arg_end(1));
const esp_console_cmd_t command_def = {
.command = command,
.help = help,
.hint = nullptr,
.func = command_func_pts[last_command],
.argtable = &arg_table[0]
};
ESP_ERROR_CHECK(esp_console_cmd_register(&command_def));
last_command++;
console_commands.emplace_back(this);
}
int ConsoleCommand::get_count(int index)
{
return ((struct arg_str *)arg_table[index])->count;
}
std::string ConsoleCommand::get_string(int index)
{
if (get_count(index) > 0) {
return std::string(((struct arg_str *)arg_table[index])->sval[0]);
}
return std::string();
}
2021-03-24 21:06:27 +01:00
int ConsoleCommand::get_int(int index)
{
if (get_count(index) > 0) {
return *((struct arg_int *)arg_table[index])->ival;
}
return -1;
}
2021-03-16 21:36:13 +01:00
std::vector<ConsoleCommand*> ConsoleCommand::console_commands;
int ConsoleCommand::last_command = 0;
const esp_console_cmd_func_t ConsoleCommand::command_func_pts[] = {
#define TEMPLATE(index) ConsoleCommand::command_func_ ## index ,
REPEAT_TEMPLATE_DEF(list of function pointers to command_func_XX() )
#undef TEMPLATE
};
int ConsoleCommand::command_func(int argc, char **argv) {
void * plain_arg_array = &arg_table[0];
int nerrors = arg_parse(argc, argv, (void **)plain_arg_array);
if (nerrors != 0) {
arg_print_errors(stderr, (struct arg_end *) arg_table.back(), argv[0]);
return 1;
}
if (func) {
return func(this);
}
return 0;
}