remove flash

This commit is contained in:
proddy
2022-09-28 15:47:27 +02:00
parent 815397dba6
commit 777c9db0f6
45 changed files with 1919 additions and 2265 deletions

View File

@@ -42,30 +42,36 @@ namespace uuid {
namespace console {
void Commands::add_command(const flash_string_vector & name, command_function function) {
add_command(0, 0, name, flash_string_vector{}, function, nullptr);
void Commands::add_command(const string_vector & name, command_function function) {
add_command(0, 0, name, string_vector{}, function, nullptr);
}
void Commands::add_command(const flash_string_vector & name, const flash_string_vector & arguments, command_function function) {
void Commands::add_command(const string_vector & name, const string_vector & arguments, command_function function) {
add_command(0, 0, name, arguments, function, nullptr);
}
void Commands::add_command(const flash_string_vector & name, const flash_string_vector & arguments, command_function function, argument_completion_function arg_function) {
void Commands::add_command(const string_vector & name, const string_vector & arguments, command_function function, argument_completion_function arg_function) {
add_command(0, 0, name, arguments, function, arg_function);
}
void Commands::add_command(unsigned int context, unsigned int flags, const flash_string_vector & name, command_function function) {
add_command(context, flags, name, flash_string_vector{}, function, nullptr);
void Commands::add_command(unsigned int context, unsigned int flags, const string_vector & name, command_function function) {
add_command(context, flags, name, string_vector{}, function, nullptr);
}
void Commands::add_command(unsigned int context, unsigned int flags, const flash_string_vector & name, const flash_string_vector & arguments, command_function function) {
void Commands::add_command(unsigned int context, unsigned int flags, const string_vector & name, const string_vector & arguments, command_function function) {
add_command(context, flags, name, arguments, function, nullptr);
}
void Commands::add_command(unsigned int context, unsigned int flags, const flash_string_vector & name, const flash_string_vector & arguments, command_function function, argument_completion_function arg_function) {
void Commands::add_command(unsigned int context,
unsigned int flags,
const string_vector & name,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function) {
commands_.emplace(std::piecewise_construct, std::forward_as_tuple(context), std::forward_as_tuple(flags, name, arguments, function, arg_function));
}
// added by proddy
// note we should really iterate and free up the lambda code and any flashstrings
void Commands::remove_all_commands() {
@@ -139,7 +145,7 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
for (size_t length = 0; all_match && length < shortest_match; length++) {
for (auto command_it = std::next(commands.begin()); command_it != commands.end(); command_it++) {
if (read_flash_string(*std::next(first.begin(), length)) != read_flash_string(*std::next(command_it->second->name_.begin(), length))) {
if ((*std::next(first.begin(), length)) != (*std::next(command_it->second->name_.begin(), length))) {
all_match = false;
break;
}
@@ -152,7 +158,7 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
auto name_it = first.begin();
for (size_t i = 0; i < component_prefix; i++) {
longest_name.push_back(std::move(read_flash_string(*name_it)));
longest_name.push_back(std::move((*name_it)));
name_it++;
}
}
@@ -167,7 +173,8 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
for (auto command_it = std::next(commands.begin()); command_it != commands.end(); command_it++) {
// This relies on the null terminator character limiting the
// length before it becomes longer than any of the strings
if (pgm_read_byte(reinterpret_cast<PGM_P>(first) + length) != pgm_read_byte(reinterpret_cast<PGM_P>(*std::next(command_it->second->name_.begin(), component_prefix)) + length)) {
if (pgm_read_byte(reinterpret_cast<PGM_P>(first) + length)
!= pgm_read_byte(reinterpret_cast<PGM_P>(*std::next(command_it->second->name_.begin(), component_prefix)) + length)) {
all_match = false;
break;
}
@@ -179,7 +186,8 @@ bool Commands::find_longest_common_prefix(const std::multimap<size_t, const Comm
}
if (chars_prefix > 0) {
longest_name.push_back(std::move(read_flash_string(first).substr(0, chars_prefix)));
// TODO fix, no more PGM
longest_name.push_back(std::string(first).substr(0, chars_prefix));
return false;
}
}
@@ -243,7 +251,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
}
if (!temp_command_name.empty() && command_line.total_size() <= temp_command_name.size()) {
temp_command = std::make_unique<Command>(0, flash_string_vector{}, flash_string_vector{}, nullptr, nullptr);
temp_command = std::make_unique<Command>(0, string_vector{}, string_vector{}, nullptr, nullptr);
count = 1;
match = commands.partial.end();
result.replacement.trailing_space = whole_components;
@@ -259,10 +267,11 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
auto & matching_command = match->second;
for (auto & name : matching_command->name_) {
result.replacement->push_back(std::move(read_flash_string(name)));
result.replacement->push_back(std::move((name)));
}
if (command_line.total_size() > result.replacement->size() && command_line.total_size() <= matching_command->name_.size() + matching_command->maximum_arguments()) {
if (command_line.total_size() > result.replacement->size()
&& command_line.total_size() <= matching_command->name_.size() + matching_command->maximum_arguments()) {
// Try to auto-complete arguments
std::vector<std::string> arguments{std::next(command_line->cbegin(), result.replacement->size()), command_line->cend()};
@@ -332,7 +341,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
remaining_help.escape_initial_parameters();
for (auto it = std::next(matching_command->arguments_.cbegin(), current_args_count); it != matching_command->arguments_.cend(); it++) {
remaining_help->push_back(std::move(read_flash_string(*it)));
remaining_help->push_back(std::move((*it)));
}
}
@@ -380,7 +389,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
}
for (; flash_name_it != command_it->second->name_.cend(); flash_name_it++) {
std::string name = read_flash_string(*flash_name_it);
std::string name = (*flash_name_it);
// Skip parts of the command name that match the command line
if (line_it != command_line->cend()) {
@@ -403,7 +412,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
continue;
}
help->push_back(std::move(read_flash_string(argument)));
help->push_back(std::move((argument)));
}
result.help.push_back(std::move(help));
@@ -416,7 +425,7 @@ Commands::Completion Commands::complete_command(Shell & shell, const CommandLine
if (commands.exact.count(longest->first) == 1) {
for (auto & name : longest->second->name_) {
result.replacement->push_back(std::move(read_flash_string(name)));
result.replacement->push_back(std::move((name))); // TODO remove all moves?
}
// Add a space because there are sub-commands for a command that has matched exactly
@@ -449,7 +458,7 @@ Commands::Match Commands::find_command(Shell & shell, const CommandLine & comman
auto line_it = command_line->cbegin();
for (; name_it != command.name_.cend() && line_it != command_line->cend(); name_it++, line_it++) {
std::string name = read_flash_string(*name_it);
std::string name = (*name_it);
size_t found = name.rfind(*line_it, 0);
if (found == std::string::npos) {
@@ -499,12 +508,12 @@ void Commands::for_each_available_command(Shell & shell, apply_function f) const
name.reserve(command_it->second.name_.size());
for (auto flash_name : command_it->second.name_) {
name.push_back(std::move(read_flash_string(flash_name)));
name.push_back(std::move((flash_name)));
}
arguments.reserve(command_it->second.arguments_.size());
for (auto flash_argument : command_it->second.arguments_) {
arguments.push_back(std::move(read_flash_string(flash_argument)));
arguments.push_back(std::move((flash_argument)));
}
f(name, arguments);
@@ -512,7 +521,11 @@ void Commands::for_each_available_command(Shell & shell, apply_function f) const
}
}
Commands::Command::Command(unsigned int flags, const flash_string_vector name, const flash_string_vector arguments, command_function function, argument_completion_function arg_function)
Commands::Command::Command(unsigned int flags,
const string_vector name,
const string_vector arguments,
command_function function,
argument_completion_function arg_function)
: flags_(flags)
, name_(name)
, arguments_(arguments)
@@ -524,7 +537,7 @@ Commands::Command::~Command() {
}
size_t Commands::Command::minimum_arguments() const {
return std::count_if(arguments_.cbegin(), arguments_.cend(), [](const __FlashStringHelper * argument) { return pgm_read_byte(argument) == '<'; });
return std::count_if(arguments_.cbegin(), arguments_.cend(), [](const char * argument) { return pgm_read_byte(argument) == '<'; });
}
} // namespace console

View File

@@ -135,8 +135,8 @@ void Shell::loop_one() {
}
}
void Shell::set_command_str(const __FlashStringHelper * str) {
line_buffer_ = read_flash_string(str);
void Shell::set_command_str(const char * str) {
line_buffer_ = (str);
erase_current_line();
prompt_displayed_ = false;
display_prompt();
@@ -262,30 +262,32 @@ void Shell::loop_normal() {
} else if (esc_ == 1) { // pos1
cursor_ = line_buffer_.length();
} else if (esc_ == 11) { // F1
set_command_str(F("help"));
// TODO use flash here?
set_command_str(("help"));
} else if (esc_ == 12) { // F2
set_command_str(F("show"));
set_command_str(("show"));
} else if (esc_ == 13) { // F3
set_command_str(F("log notice"));
set_command_str(("log notice"));
} else if (esc_ == 14) { // F4
set_command_str(F("log info"));
set_command_str(("log info"));
} else if (esc_ == 15) { // F5
set_command_str(F("log debug"));
set_command_str(("log debug"));
} else if (esc_ == 17) { // F6
set_command_str(F("watch off"));
set_command_str(("watch off"));
} else if (esc_ == 18) { // F7
set_command_str(F("watch on"));
set_command_str(("watch on"));
} else if (esc_ == 19) { // F8
set_command_str(F("watch raw"));
set_command_str(("watch raw"));
} else if (esc_ == 20) { // F9
set_command_str(F("call system info"));
set_command_str(("call system info"));
} else if (esc_ == 21) { // F10
set_command_str(F("call system settings"));
set_command_str(("call system settings"));
} else if (esc_ == 23) { // F11
line_buffer_ = read_flash_string(F("call send \"0B \""));
line_buffer_ = (("call send \"0B \""));
cursor_ = 1;
} else if (esc_ == 24) { // F12
set_command_str(F("log debug; watch raw"));
set_command_str(("log debug; watch raw"));
}
esc_ = 0;
} else if (c >= '0' && (c <= '9')) { // numbers
@@ -320,7 +322,7 @@ void Shell::loop_normal() {
idle_time_ = uuid::get_uptime_ms();
}
Shell::PasswordData::PasswordData(const __FlashStringHelper * password_prompt, password_function && password_function)
Shell::PasswordData::PasswordData(const char * password_prompt, password_function && password_function)
: password_prompt_(password_prompt)
, password_function_(std::move(password_function)) {
}
@@ -452,7 +454,7 @@ void Shell::loop_blocking() {
}
}
void Shell::enter_password(const __FlashStringHelper * prompt, password_function function) {
void Shell::enter_password(const char * prompt, password_function function) {
if (mode_ == Mode::NORMAL) {
mode_ = Mode::PASSWORD;
mode_data_ = std::make_unique<Shell::PasswordData>(prompt, std::move(function));

View File

@@ -30,8 +30,8 @@ namespace uuid {
namespace console {
static const char __pstr__logger_name[] __attribute__((__aligned__(sizeof(uint32_t)))) PROGMEM = "shell";
const uuid::log::Logger Shell::logger_{reinterpret_cast<const __FlashStringHelper *>(__pstr__logger_name), uuid::log::Facility::LPR};
static const char __pstr__logger_name[] = "shell";
const uuid::log::Logger Shell::logger_{(__pstr__logger_name), uuid::log::Facility::LPR};
Shell::QueuedLogMessage::QueuedLogMessage(unsigned long id, std::shared_ptr<uuid::log::Message> && content)
: id_(id)

View File

@@ -335,7 +335,7 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
* execution.
* @since 0.1.0
*/
void enter_password(const __FlashStringHelper * prompt, password_function function);
void enter_password(const char * prompt, password_function function);
/**
* Stop executing anything on this shell for a period of time.
@@ -682,10 +682,10 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
* execution.
* @since 0.1.0
*/
PasswordData(const __FlashStringHelper * password_prompt, password_function && password_function);
PasswordData(const char * password_prompt, password_function && password_function);
~PasswordData() override = default;
const __FlashStringHelper * password_prompt_; /*!< Prompt requesting password input. @since 0.1.0 */
const char * password_prompt_; /*!< Prompt requesting password input. @since 0.1.0 */
password_function password_function_; /*!< Function to execute after password entry. @since 0.1.0 */
};
@@ -893,7 +893,7 @@ class Shell : public std::enable_shared_from_this<Shell>, public uuid::log::Hand
* @since 0.1.0
*/
size_t vprintf(const __FlashStringHelper * format, va_list ap);
void set_command_str(const __FlashStringHelper * str);
void set_command_str(const char * str);
static const uuid::log::Logger logger_; /*!< uuid::log::Logger instance for shells. @since 0.1.0 */
static std::set<std::shared_ptr<Shell>> shells_; /*!< Registered running shells to be executed. @since 0.1.0 */
@@ -1188,7 +1188,7 @@ class Commands {
* executed.
* @since 0.2.0
*/
void add_command(const flash_string_vector & name, command_function function);
void add_command(const string_vector & name, command_function function);
/**
* Add a command with arguments to the list of commands in this
* container.
@@ -1205,7 +1205,7 @@ class Commands {
* executed.
* @since 0.2.0
*/
void add_command(const flash_string_vector & name, const flash_string_vector & arguments, command_function function);
void add_command(const string_vector & name, const string_vector & arguments, command_function function);
/**
* Add a command with arguments and automatic argument completion
* to the list of commands in this container.
@@ -1224,7 +1224,7 @@ class Commands {
* completions for this command.
* @since 0.2.0
*/
void add_command(const flash_string_vector & name, const flash_string_vector & arguments, command_function function, argument_completion_function arg_function);
void add_command(const string_vector & name, const string_vector & arguments, command_function function, argument_completion_function arg_function);
/**
* Add a command with no arguments to the list of commands in this
* container.
@@ -1239,7 +1239,7 @@ class Commands {
* executed.
* @since 0.1.0
*/
void add_command(unsigned int context, unsigned int flags, const flash_string_vector & name, command_function function);
void add_command(unsigned int context, unsigned int flags, const string_vector & name, command_function function);
/**
* Add a command with arguments to the list of commands in this
* container.
@@ -1257,7 +1257,7 @@ class Commands {
* executed.
* @since 0.1.0
*/
void add_command(unsigned int context, unsigned int flags, const flash_string_vector & name, const flash_string_vector & arguments, command_function function);
void add_command(unsigned int context, unsigned int flags, const string_vector & name, const string_vector & arguments, command_function function);
/**
* Add a command with arguments and automatic argument completion
* to the list of commands in this container.
@@ -1279,8 +1279,8 @@ class Commands {
*/
void add_command(unsigned int context,
unsigned int flags,
const flash_string_vector & name,
const flash_string_vector & arguments,
const string_vector & name,
const string_vector & arguments,
command_function function,
argument_completion_function arg_function);
@@ -1345,8 +1345,8 @@ class Commands {
* @since 0.1.0
*/
Command(unsigned int flags,
const flash_string_vector name,
const flash_string_vector arguments,
const string_vector name,
const string_vector arguments,
command_function function,
argument_completion_function arg_function);
~Command();
@@ -1372,8 +1372,8 @@ class Commands {
}
unsigned int flags_; /*!< Shell flags that must be set for this command to be available. @since 0.1.0 */
const flash_string_vector name_; /*!< Name of the command as a std::vector of flash strings. @since 0.1.0 */
const flash_string_vector arguments_; /*!< Help text for arguments that the command accepts as a std::vector of flash strings. @since 0.1.0 */
const string_vector name_; /*!< Name of the command as a std::vector of flash strings. @since 0.1.0 */
const string_vector arguments_; /*!< Help text for arguments that the command accepts as a std::vector of flash strings. @since 0.1.0 */
command_function function_; /*!< Function to be used when the command is executed. @since 0.1.0 */
argument_completion_function arg_function_; /*!< Function to be used to perform argument completions for this command. @since 0.1.0 */