Skip to content

Preferences fixes #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions UNOR4USBBridge/cmds_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ void CAtHandler::add_cmds_preferences() {
bool readOnly = strtol(parser.args[1].c_str(), NULL, 10) != 0;
auto &partition = parser.args[2];

// calling end before begin, because the renesas mcu may have been restarted
pref.end();

String error = String();
if (partition.empty()) {
error = String(pref.begin(name.c_str(), readOnly)) + "\r\n";
Expand Down Expand Up @@ -165,8 +168,20 @@ void CAtHandler::add_cmds_preferences() {
}
break;
case PreferenceType::PT_STR: {
auto value = parser.args[2];
error = String(pref.putString(key.c_str(), value.c_str())) + "\r\n";
int value = atoi(parser.args[2].c_str());
pref_buf = srv.inhibit_read(value);
size_t offset = pref_buf.size();
if(offset < value) {
pref_buf.resize(value);
do {
offset += serial->read(pref_buf.data() + offset, value - offset);
} while (offset < value);
}

pref_buf.push_back('\0');

srv.continue_read();
error = String(pref.putString(key.c_str(), (char*)pref_buf.data())) + "\r\n";
}
break;
case PreferenceType::PT_BLOB: {
Expand Down Expand Up @@ -200,6 +215,33 @@ void CAtHandler::add_cmds_preferences() {
}
};

/* ....................................................................... */
command_table[_PREF_TYPE] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
switch (parser.cmd_mode) {
case chAT::CommandMode::Write: {
if (parser.args.size() != 1) {
return chAT::CommandStatus::ERROR;
}

auto &key = parser.args[0];
if (key.empty()) {
return chAT::CommandStatus::ERROR;
}

String error = String(pref.getType(key.c_str())) + "\r\n";

srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
return chAT::CommandStatus::OK;

}
default:
return chAT::CommandStatus::ERROR;
}
};

/* ....................................................................... */
command_table[_PREF_GET] = [this](auto & srv, auto & parser) {
/* ....................................................................... */
Expand Down Expand Up @@ -267,7 +309,13 @@ void CAtHandler::add_cmds_preferences() {
break;
case PreferenceType::PT_STR: {
auto value = parser.args[2];
error = String(pref.getString(key.c_str(), value.c_str())) + "\r\n";
auto res = pref.getString(key.c_str(), value.c_str());

srv.write_response_prompt();
srv.write_str(String(res.length()).c_str());
srv.write_str("|");
srv.write_str(res.c_str());
srv.write_line_end();
}
break;
case PreferenceType::PT_BLOB: {
Expand All @@ -290,7 +338,7 @@ void CAtHandler::add_cmds_preferences() {
}


if (type != PreferenceType::PT_BLOB) {
if (type != PreferenceType::PT_BLOB && type != PreferenceType::PT_STR) {
srv.write_response_prompt();
srv.write_str((const char *)(error.c_str()));
srv.write_line_end();
Expand Down
1 change: 1 addition & 0 deletions UNOR4USBBridge/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enum file_op {
#define _PREF_CLEAR "+PREFCLEAR"
#define _PREF_REMOVE "+PREFREMOVE"
#define _PREF_PUT "+PREFPUT"
#define _PREF_TYPE "+PREFTYPE"
#define _PREF_GET "+PREFGET"
#define _PREF_LEN "+PREFLEN"
#define _PREF_STAT "+PREFSTAT"
Expand Down