131 lines
4.0 KiB
C++
131 lines
4.0 KiB
C++
|
#include <QCoreApplication>
|
||
|
#include <QApplication>
|
||
|
#include <QDebug>
|
||
|
#include <QTimer>
|
||
|
#include <QFileInfo>
|
||
|
|
||
|
#ifdef __linux__
|
||
|
#include <stdlib.h> // system()
|
||
|
#include <unistd.h>
|
||
|
#endif
|
||
|
|
||
|
#include "message_handler.h"
|
||
|
#include "plugins/interfaces.h"
|
||
|
|
||
|
#include <memory>
|
||
|
#include <QSharedMemory>
|
||
|
#include <QRunnable>
|
||
|
#include <QThreadPool>
|
||
|
#include <QDir>
|
||
|
#include <QProcess>
|
||
|
#include <QCommandLineParser>
|
||
|
#include <QStandardPaths>
|
||
|
#include <QMainWindow>
|
||
|
|
||
|
#include <QThread>
|
||
|
#include <QtWidgets>
|
||
|
#include <QScopedPointer>
|
||
|
|
||
|
#ifdef PTU5
|
||
|
#define SERIAL_PORT "ttymxc2"
|
||
|
#else
|
||
|
#define SERIAL_PORT "ttyUSB0"
|
||
|
#endif
|
||
|
|
||
|
|
||
|
QPluginLoader pluginLoader;
|
||
|
|
||
|
static hwinf *loadDCPlugin(QDir const &plugInDir, QString const &fname) {
|
||
|
hwinf *hw = nullptr;
|
||
|
if (plugInDir.exists()) {
|
||
|
QString pluginLibName(fname);
|
||
|
pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
|
||
|
QFileInfo info(pluginLibName);
|
||
|
if (info.exists()) {
|
||
|
pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
|
||
|
pluginLoader.setFileName(pluginLibName);
|
||
|
// static QPluginLoader pluginLoader(pluginLibName);
|
||
|
if (!pluginLoader.load()) {
|
||
|
qCritical() << "in directory" << plugInDir.absolutePath();
|
||
|
qCritical() << "cannot load plugin" << pluginLoader.fileName();
|
||
|
qCritical() << pluginLoader.errorString();
|
||
|
exit(-1);
|
||
|
}
|
||
|
if (!pluginLoader.isLoaded()) {
|
||
|
qCritical() << pluginLoader.errorString();
|
||
|
exit(-2);
|
||
|
}
|
||
|
QObject *plugin = pluginLoader.instance();
|
||
|
if (!plugin) {
|
||
|
qCritical() << "cannot start instance";
|
||
|
exit(-3);
|
||
|
}
|
||
|
if (! (hw = qobject_cast<hwinf *>(plugin))) {
|
||
|
qCritical() << "cannot cast plugin" << plugin << "to hwinf";
|
||
|
exit(-4);
|
||
|
}
|
||
|
} else {
|
||
|
qCritical() << pluginLibName << "does not exist";
|
||
|
exit(-5);
|
||
|
}
|
||
|
} else {
|
||
|
qCritical() << "plugins directory" << plugInDir.absolutePath()
|
||
|
<< "does not exist";
|
||
|
exit(-6);
|
||
|
}
|
||
|
return hw;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
|
||
|
QByteArray const value = qgetenv("LC_ALL");
|
||
|
if (value != "C") {
|
||
|
qputenv("LC_ALL", "C");
|
||
|
}
|
||
|
|
||
|
openlog("DC-LIB-MASTER", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);
|
||
|
|
||
|
QApplication a(argc, argv);
|
||
|
QApplication::setApplicationName("dc-lib-master");
|
||
|
QApplication::setApplicationVersion(APP_VERSION);
|
||
|
|
||
|
if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
|
||
|
atbInstallMessageHandler(atbDebugOutput);
|
||
|
setDebugLevel(LOG_NOTICE);
|
||
|
}
|
||
|
|
||
|
QCommandLineParser parser;
|
||
|
parser.setApplicationDescription("Master for loading dc-library.");
|
||
|
parser.addHelpOption();
|
||
|
parser.addVersionOption();
|
||
|
|
||
|
QCommandLineOption pluginDirectoryOption(QStringList() << "plugin-directory" << "plugin-directory",
|
||
|
QCoreApplication::translate("main", "Where to find dc-plugin."),
|
||
|
QCoreApplication::translate("main", "directory"));
|
||
|
QString const pluginDefault = "/usr/lib";
|
||
|
pluginDirectoryOption.setDefaultValue(pluginDefault);
|
||
|
parser.addOption(pluginDirectoryOption);
|
||
|
|
||
|
QCommandLineOption pluginNameOption(QStringList() << "plugin-name" << "plugin-name",
|
||
|
QCoreApplication::translate("main", "Name of dc-plugin."),
|
||
|
QCoreApplication::translate("main", "directory"));
|
||
|
QString const pluginNameDefault = "libCAslave.so";
|
||
|
pluginNameOption.setDefaultValue(pluginNameDefault);
|
||
|
parser.addOption(pluginNameOption);
|
||
|
|
||
|
// Process the actual command line arguments given by the user
|
||
|
parser.process(a);
|
||
|
QString plugInDir = parser.value(pluginDirectoryOption);
|
||
|
QString plugInName = parser.value(pluginNameOption);
|
||
|
|
||
|
QThread::currentThread()->setObjectName("main thread");
|
||
|
qInfo() << "Main thread" << QThread::currentThreadId();
|
||
|
|
||
|
hwinf *hw = loadDCPlugin(QDir(plugInDir), plugInName);
|
||
|
|
||
|
hw->dc_autoRequest(true);
|
||
|
hw->dc_openSerial(5, "115200", SERIAL_PORT, 1);
|
||
|
|
||
|
return a.exec();
|
||
|
}
|