Skip to content

Commit 6e22665

Browse files
Merge pull request #62 from andreagilardoni/prefernces-fixes
Preferences fixes
2 parents 02a4039 + 1cde96e commit 6e22665

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

UNOR4USBBridge/cmds_preferences.h

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void CAtHandler::add_cmds_preferences() {
2323
bool readOnly = strtol(parser.args[1].c_str(), NULL, 10) != 0;
2424
auto &partition = parser.args[2];
2525

26+
// calling end before begin, because the renesas mcu may have been restarted
27+
pref.end();
28+
2629
String error = String();
2730
if (partition.empty()) {
2831
error = String(pref.begin(name.c_str(), readOnly)) + "\r\n";
@@ -165,8 +168,20 @@ void CAtHandler::add_cmds_preferences() {
165168
}
166169
break;
167170
case PreferenceType::PT_STR: {
168-
auto value = parser.args[2];
169-
error = String(pref.putString(key.c_str(), value.c_str())) + "\r\n";
171+
int value = atoi(parser.args[2].c_str());
172+
pref_buf = srv.inhibit_read(value);
173+
size_t offset = pref_buf.size();
174+
if(offset < value) {
175+
pref_buf.resize(value);
176+
do {
177+
offset += serial->read(pref_buf.data() + offset, value - offset);
178+
} while (offset < value);
179+
}
180+
181+
pref_buf.push_back('\0');
182+
183+
srv.continue_read();
184+
error = String(pref.putString(key.c_str(), (char*)pref_buf.data())) + "\r\n";
170185
}
171186
break;
172187
case PreferenceType::PT_BLOB: {
@@ -200,6 +215,33 @@ void CAtHandler::add_cmds_preferences() {
200215
}
201216
};
202217

218+
/* ....................................................................... */
219+
command_table[_PREF_TYPE] = [this](auto & srv, auto & parser) {
220+
/* ....................................................................... */
221+
switch (parser.cmd_mode) {
222+
case chAT::CommandMode::Write: {
223+
if (parser.args.size() != 1) {
224+
return chAT::CommandStatus::ERROR;
225+
}
226+
227+
auto &key = parser.args[0];
228+
if (key.empty()) {
229+
return chAT::CommandStatus::ERROR;
230+
}
231+
232+
String error = String(pref.getType(key.c_str())) + "\r\n";
233+
234+
srv.write_response_prompt();
235+
srv.write_str((const char *)(error.c_str()));
236+
srv.write_line_end();
237+
return chAT::CommandStatus::OK;
238+
239+
}
240+
default:
241+
return chAT::CommandStatus::ERROR;
242+
}
243+
};
244+
203245
/* ....................................................................... */
204246
command_table[_PREF_GET] = [this](auto & srv, auto & parser) {
205247
/* ....................................................................... */
@@ -267,7 +309,13 @@ void CAtHandler::add_cmds_preferences() {
267309
break;
268310
case PreferenceType::PT_STR: {
269311
auto value = parser.args[2];
270-
error = String(pref.getString(key.c_str(), value.c_str())) + "\r\n";
312+
auto res = pref.getString(key.c_str(), value.c_str());
313+
314+
srv.write_response_prompt();
315+
srv.write_str(String(res.length()).c_str());
316+
srv.write_str("|");
317+
srv.write_str(res.c_str());
318+
srv.write_line_end();
271319
}
272320
break;
273321
case PreferenceType::PT_BLOB: {
@@ -290,7 +338,7 @@ void CAtHandler::add_cmds_preferences() {
290338
}
291339

292340

293-
if (type != PreferenceType::PT_BLOB) {
341+
if (type != PreferenceType::PT_BLOB && type != PreferenceType::PT_STR) {
294342
srv.write_response_prompt();
295343
srv.write_str((const char *)(error.c_str()));
296344
srv.write_line_end();

UNOR4USBBridge/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ enum file_op {
130130
#define _PREF_CLEAR "+PREFCLEAR"
131131
#define _PREF_REMOVE "+PREFREMOVE"
132132
#define _PREF_PUT "+PREFPUT"
133+
#define _PREF_TYPE "+PREFTYPE"
133134
#define _PREF_GET "+PREFGET"
134135
#define _PREF_LEN "+PREFLEN"
135136
#define _PREF_STAT "+PREFSTAT"

0 commit comments

Comments
 (0)