mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-29 02:07:27 +02:00
update per design review
This commit is contained in:
@ -33,6 +33,9 @@ void ConsoleCommand::RegisterCommand(const char* command, const char* help, std:
|
||||
case INT1:
|
||||
common_arg = arg_int1(it.shortopts, it.longopts, it.datatype, it.glossary);
|
||||
break;
|
||||
case LIT0:
|
||||
common_arg = arg_lit0(it.shortopts, it.longopts, it.glossary);
|
||||
break;
|
||||
}
|
||||
if (common_arg) {
|
||||
arg_table.emplace_back(common_arg);
|
||||
@ -67,6 +70,13 @@ std::string ConsoleCommand::get_string(int index)
|
||||
return std::string();
|
||||
}
|
||||
|
||||
int ConsoleCommand::get_int(int index)
|
||||
{
|
||||
if (get_count(index) > 0) {
|
||||
return *((struct arg_int *)arg_table[index])->ival;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
std::vector<ConsoleCommand*> ConsoleCommand::console_commands;
|
||||
|
@ -19,12 +19,16 @@ enum arg_type {
|
||||
STR1,
|
||||
INT0,
|
||||
INT1,
|
||||
LIT0,
|
||||
ARG_END,
|
||||
};
|
||||
|
||||
struct CommandArgs {
|
||||
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * data, const char * glos):
|
||||
type(t), shortopts(shopts), longopts(lopts), datatype(data), glossary(glos) {}
|
||||
type(t), shortopts(shopts), longopts(lopts), datatype(data), glossary(glos) {}
|
||||
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * glos):
|
||||
type(t), shortopts(shopts), longopts(lopts), datatype(nullptr), glossary(glos) {}
|
||||
|
||||
arg_type type;
|
||||
const char *shortopts;
|
||||
const char *longopts;
|
||||
@ -48,8 +52,10 @@ public:
|
||||
int get_count(int index);
|
||||
template<typename T> int get_count_of(CommandArgs T::*member) { return get_count(index_arg(member)); }
|
||||
template<typename T> std::string get_string_of(CommandArgs T::*member) { return get_string(index_arg(member)); }
|
||||
template<typename T> int get_int_of(CommandArgs T::*member) { return get_int(index_arg(member)); }
|
||||
|
||||
std::string get_string(int index);
|
||||
int get_int(int index);
|
||||
|
||||
private:
|
||||
void RegisterCommand(const char* command, const char* help, std::vector<CommandArgs>& args);
|
||||
|
@ -323,6 +323,15 @@ static esp_console_repl_t *s_repl = NULL;
|
||||
// return (char*)&((T*)nullptr->*member) - (char*)nullptr;
|
||||
//}
|
||||
|
||||
#define CHECK_ERR(cmd, success_action) do { \
|
||||
auto err = cmd; \
|
||||
if (err == command_result::OK) { \
|
||||
success_action; \
|
||||
return 0; \
|
||||
} else { \
|
||||
ESP_LOGE(TAG, "Failed with %s", err == command_result::TIMEOUT ? "TIMEOUT":"ERROR"); \
|
||||
return 1; \
|
||||
} } while (0)
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
@ -395,6 +404,7 @@ extern "C" void app_main(void)
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
std::vector<CommandArgs> no_args;
|
||||
ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand *c){
|
||||
bool pin_ok;
|
||||
@ -409,10 +419,35 @@ extern "C" void app_main(void)
|
||||
return 0;
|
||||
});
|
||||
|
||||
ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand *c){
|
||||
std::string module_name;
|
||||
ESP_LOGI(TAG, "Reading module name...");
|
||||
CHECK_ERR(dce->get_module_name(module_name), ESP_LOGI(TAG, "OK. Module name: %s", module_name.c_str()));
|
||||
});
|
||||
|
||||
const struct GenericCommandArgs {
|
||||
GenericCommandArgs():
|
||||
cmd(STR1, nullptr, nullptr, "<command>", "AT command to send to the modem"),
|
||||
timeout(INT1, "t", "timeout", "<timeout>", "command timeout"),
|
||||
pattern(STR0, "p", "pattern", "<pattern>", "command response to wait for"),
|
||||
no_cr(LIT0, "n", "no-cr", "not add trailing CR to the command") {}
|
||||
CommandArgs cmd;
|
||||
CommandArgs timeout;
|
||||
CommandArgs pattern;
|
||||
CommandArgs no_cr;
|
||||
} send_cmd_args;
|
||||
|
||||
ConsoleCommand SendCommand("cmd", "sends generic AT command, no_args", &send_cmd_args, sizeof(send_cmd_args), [&](ConsoleCommand *c){
|
||||
auto cmd = c->get_string_of(&GenericCommandArgs::cmd);
|
||||
auto timeout = c->get_int_of(&GenericCommandArgs::timeout);
|
||||
ESP_LOGI(TAG, "Sending command %s with timeout %d", cmd.c_str(), timeout);
|
||||
CHECK_ERR(dce->command(cmd, nullptr, timeout),
|
||||
ESP_LOGI(TAG, "OK"));
|
||||
});
|
||||
// start console REPL
|
||||
ESP_ERROR_CHECK(esp_console_start_repl(s_repl));
|
||||
ESP_LOGE(TAG, "Exit console!!!");
|
||||
ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %d bytes)", __LINE__, esp_get_free_heap_size());
|
||||
while(1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(50000));
|
||||
ESP_LOGI(TAG, "working!");
|
||||
|
Reference in New Issue
Block a user