enhance encode+decode commands
This commit is contained in:
+38
-14
@@ -1719,6 +1719,7 @@ result_t MainLoop::executeDefine(const vector<string>& args, ostringstream* ostr
|
|||||||
result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostream) {
|
result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostream) {
|
||||||
size_t argPos = 1;
|
size_t argPos = 1;
|
||||||
OutputFormat verbosity = OF_NONE;
|
OutputFormat verbosity = OF_NONE;
|
||||||
|
bool toMaster = false;
|
||||||
while (args.size() > argPos && args[argPos][0] == '-') {
|
while (args.size() > argPos && args[argPos][0] == '-') {
|
||||||
if (args[argPos] == "-v") {
|
if (args[argPos] == "-v") {
|
||||||
if ((verbosity & VERBOSITY_3) == VERBOSITY_0) {
|
if ((verbosity & VERBOSITY_3) == VERBOSITY_0) {
|
||||||
@@ -1732,6 +1733,8 @@ result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostr
|
|||||||
verbosity |= VERBOSITY_2;
|
verbosity |= VERBOSITY_2;
|
||||||
} else if (args[argPos] == "-vvv" || args[argPos] == "-V") {
|
} else if (args[argPos] == "-vvv" || args[argPos] == "-V") {
|
||||||
verbosity |= VERBOSITY_3;
|
verbosity |= VERBOSITY_3;
|
||||||
|
} else if (args[argPos] == "-m") {
|
||||||
|
toMaster = true;
|
||||||
} else if (args[argPos] == "-n") {
|
} else if (args[argPos] == "-n") {
|
||||||
verbosity = (verbosity & ~OF_VALUENAME) | OF_NUMERIC;
|
verbosity = (verbosity & ~OF_VALUENAME) | OF_NUMERIC;
|
||||||
} else if (args[argPos] == "-N") {
|
} else if (args[argPos] == "-N") {
|
||||||
@@ -1748,10 +1751,11 @@ result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostr
|
|||||||
|
|
||||||
if (argPos == 0 || args.size() != argPos + 2) {
|
if (argPos == 0 || args.size() != argPos + 2) {
|
||||||
*ostream <<
|
*ostream <<
|
||||||
"usage: decode [-v|-V] [-n|-N] DEFINITION DD[DD]*\n"
|
"usage: decode [-v|-V] [-m] [-n|-N] DEFINITION DD[DD]*\n"
|
||||||
" Decode field(s) by definition and hex data.\n"
|
" Decode field(s) by definition and hex data.\n"
|
||||||
" -v increase verbosity (include names/units/comments)\n"
|
" -v increase verbosity (include names/units/comments)\n"
|
||||||
" -V be very verbose (include names, units, and comments)\n"
|
" -V be very verbose (include names, units, and comments)\n"
|
||||||
|
" -m target a master instead of a slave\n"
|
||||||
" -n use numeric value of value=name pairs\n"
|
" -n use numeric value of value=name pairs\n"
|
||||||
" -N use numeric and named value of value=name pairs\n"
|
" -N use numeric and named value of value=name pairs\n"
|
||||||
" DEFINITION field definition (type,divisor/values,unit,comment,...)\n"
|
" DEFINITION field definition (type,divisor/values,unit,comment,...)\n"
|
||||||
@@ -1764,28 +1768,45 @@ result_t MainLoop::executeDecode(const vector<string>& args, ostringstream* ostr
|
|||||||
istringstream defstr("#\n" + args[argPos]); // ensure first line is not used for determining col names
|
istringstream defstr("#\n" + args[argPos]); // ensure first line is not used for determining col names
|
||||||
string errorDescription;
|
string errorDescription;
|
||||||
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
||||||
LoadableDataFieldSet fields("", templates);
|
LoadableDataFieldSet fields("", templates, toMaster);
|
||||||
result_t ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
result_t ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
||||||
if (ret != RESULT_OK) {
|
if (ret != RESULT_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
SlaveSymbolString slave;
|
SlaveSymbolString sdata;
|
||||||
slave.push_back(0); // dummy length
|
MasterSymbolString mdata;
|
||||||
ret = slave.parseHex(args[argPos+1]);
|
SymbolString* data = toMaster ? &mdata : (SymbolString*)&sdata;
|
||||||
|
data->adjustHeader();
|
||||||
|
ret = data->parseHex(args[argPos+1]);
|
||||||
if (ret != RESULT_OK) {
|
if (ret != RESULT_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
slave.adjustHeader();
|
data->adjustHeader();
|
||||||
return fields.read(slave, 0, false, nullptr, -1, verbosity, -1, ostream);
|
return fields.read(*data, 0, false, nullptr, -1, verbosity, -1, ostream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
result_t MainLoop::executeEncode(const vector<string>& args, ostringstream* ostream) {
|
result_t MainLoop::executeEncode(const vector<string>& args, ostringstream* ostream) {
|
||||||
size_t argPos = 1;
|
size_t argPos = 1;
|
||||||
if (argPos == 0 || args.size() != argPos + 2) {
|
bool toMaster = false;
|
||||||
|
while (args.size() > argPos && args[argPos][0] == '-') {
|
||||||
|
if (args[argPos] == "-m") {
|
||||||
|
toMaster = true;
|
||||||
|
} else {
|
||||||
|
argPos = 0; // print usage
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
argPos++;
|
||||||
|
}
|
||||||
|
if (args.size() != argPos + 2) {
|
||||||
|
argPos = 0; // print usage
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argPos == 0) {
|
||||||
*ostream <<
|
*ostream <<
|
||||||
"usage: encode DEFINITION VALUE[;VALUE]*\n"
|
"usage: encode [-m] DEFINITION VALUE[;VALUE]*\n"
|
||||||
" Encode field(s) by definition and decoded value(s).\n"
|
" Encode field(s) by definition and decoded value(s).\n"
|
||||||
|
" -m target a master instead of a slave\n"
|
||||||
" DEFINITION field definition (type,divisor/values,unit,comment,...)\n"
|
" DEFINITION field definition (type,divisor/values,unit,comment,...)\n"
|
||||||
" VALUE single field VALUE to encode";
|
" VALUE single field VALUE to encode";
|
||||||
return RESULT_OK;
|
return RESULT_OK;
|
||||||
@@ -1796,18 +1817,21 @@ result_t MainLoop::executeEncode(const vector<string>& args, ostringstream* ostr
|
|||||||
istringstream defstr("#\n" + args[argPos]); // ensure first line is not used for determining col names
|
istringstream defstr("#\n" + args[argPos]); // ensure first line is not used for determining col names
|
||||||
string errorDescription;
|
string errorDescription;
|
||||||
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
||||||
LoadableDataFieldSet fields("", templates);
|
LoadableDataFieldSet fields("", templates, toMaster);
|
||||||
result_t ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
result_t ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
||||||
if (ret != RESULT_OK) {
|
if (ret != RESULT_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
istringstream datastr(args[argPos+1]);
|
istringstream datastr(args[argPos+1]);
|
||||||
SlaveSymbolString slave;
|
SlaveSymbolString sdata;
|
||||||
ret = fields.write(UI_FIELD_SEPARATOR, 0, &datastr, &slave, nullptr);
|
MasterSymbolString mdata;
|
||||||
|
SymbolString* data = toMaster ? &mdata : (SymbolString*)&sdata;
|
||||||
|
data->adjustHeader();
|
||||||
|
ret = fields.write(UI_FIELD_SEPARATOR, 0, &datastr, data, nullptr);
|
||||||
if (ret != RESULT_OK) {
|
if (ret != RESULT_OK) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
*ostream << slave.getStr(1);
|
*ostream << data->getStr(toMaster ? 5 : 1);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2375,7 +2399,7 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
|
|||||||
istringstream defstr("#\n" + def); // ensure first line is not used for determining col names
|
istringstream defstr("#\n" + def); // ensure first line is not used for determining col names
|
||||||
string errorDescription;
|
string errorDescription;
|
||||||
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
DataFieldTemplates* templates = m_scanHelper->getTemplates("*");
|
||||||
LoadableDataFieldSet fields("", templates);
|
LoadableDataFieldSet fields("", templates, false);
|
||||||
ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
ret = fields.readFromStream(&defstr, "temporary", now, true, nullptr, &errorDescription);
|
||||||
if (ret == RESULT_OK && fields.size()) {
|
if (ret == RESULT_OK && fields.size()) {
|
||||||
SlaveSymbolString slave;
|
SlaveSymbolString slave;
|
||||||
|
|||||||
+6
-2
@@ -792,9 +792,10 @@ class LoadableDataFieldSet : public DataFieldSet, public MappedFileReader {
|
|||||||
* Constructs a new instance.
|
* Constructs a new instance.
|
||||||
* @param name the field name.
|
* @param name the field name.
|
||||||
* @param templates the @a DataFieldTemplates instance to use.
|
* @param templates the @a DataFieldTemplates instance to use.
|
||||||
|
* @param isWrite true for a write message, false for read.
|
||||||
*/
|
*/
|
||||||
LoadableDataFieldSet(const string& name, DataFieldTemplates* templates)
|
LoadableDataFieldSet(const string& name, DataFieldTemplates* templates, bool isWrite)
|
||||||
: DataFieldSet(name, vector<const SingleDataField*>()), MappedFileReader(false), m_templates(templates) {
|
: DataFieldSet(name, vector<const SingleDataField*>()), MappedFileReader(false), m_templates(templates), m_isWrite(isWrite) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// @copydoc
|
// @copydoc
|
||||||
@@ -807,6 +808,9 @@ class LoadableDataFieldSet : public DataFieldSet, public MappedFileReader {
|
|||||||
private:
|
private:
|
||||||
/** the @a DataFieldTemplates instance to use. */
|
/** the @a DataFieldTemplates instance to use. */
|
||||||
DataFieldTemplates* m_templates;
|
DataFieldTemplates* m_templates;
|
||||||
|
|
||||||
|
/** true for a write message, false for read. */
|
||||||
|
bool m_isWrite;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user