1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include "readcodeplug.hh"
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QFile>
#include "logger.hh"
#include "radio.hh"
#include "printprogress.hh"
#include "config.hh"
#include "codeplug.hh"
#include "progressbar.hh"
#include "autodetect.hh"
int readCodeplug(QCommandLineParser &parser, QCoreApplication &app)
{
Q_UNUSED(app);
if (2 > parser.positionalArguments().size())
parser.showHelp(-1);
ErrorStack err;
Radio *radio = autoDetect(parser, app, err);
if (nullptr == radio) {
logError() << "Cannot detect radio: " << err.format();
return -1;
}
QString filename = parser.positionalArguments().at(1);
if (! parser.isSet("verbose")) {
showProgress();
QObject::connect(radio, &Radio::downloadProgress, updateProgress);
}
TransferFlags flags;
flags.setBlocking(true);
Config config;
if (! radio->startDownload(flags, err)) {
logError() << "Codeplug download error: " << err.format();
return -1;
}
if (Radio::StatusError == radio->status()) {
logError() << "Codeplug download error: " << err.format();
return -1;
}
logDebug() << "Save codeplug at '" << filename << "'.";
// If output is CSV -> decode code-plug
if (parser.isSet("csv") || (filename.endsWith(".conf") || filename.endsWith(".csv"))) {
logError() << "Export of the old table based format was disabled with 0.9.0. "
"Import still works.";
return -1;
} else if (parser.isSet("yaml") || filename.endsWith(".yaml")) {
// decode codeplug
if (! radio->codeplug().decode(&config, err)) {
logError() << "Cannot decode codeplug: " << err.format();
return -1;
}
// post-process decoded codeplug
if (! radio->codeplug().postprocess(&config, err)) {
logError() << "Cannot post-process codeplug: " << err.format();
return -1;
}
// try to write YAML file
QFile file(filename);
if (! file.open(QIODevice::WriteOnly)) {
logError() << "Cannot write YAML file '" << filename << "': " << file.errorString();
return -1;
}
QTextStream stream(&file);
if (! config.toYAML(stream)) {
logError() << "Cannot serialize config to YAML file '" << filename << "'.";
return -1;
}
stream.flush();
file.close();
} else if (parser.isSet("bin") || filename.endsWith(".bin") || filename.endsWith(".dfu")) {
// otherwise write binary code-plug
if (! radio->codeplug().write(filename, err)) {
logError() << "Cannot dump codeplug into file '" << filename << "': " << err.format();
return -1;
}
} else {
logError() << "Cannot determine file output type from '" << filename << "'. "
<< "Consider using --csv, --yaml or --bin.";
return -1;
}
return 0;
}
|