Compare commits

...

17 Commits

Author SHA1 Message Date
5599c56114 Minor: added some debug output 2024-02-05 14:52:09 +01:00
0723ef335c Added some debug output. 2024-02-05 14:51:16 +01:00
ef31911e74 hw/sw-version: always store different version than current onew 2024-02-05 14:50:17 +01:00
100ada06f8 Improve on storing hw/sw version of device controller 2024-02-02 13:41:13 +01:00
fb5cf3cc6a Minor: add debug out put. memset buffers to be printed. 2024-02-02 13:40:21 +01:00
287106d8f3 Make sure reporting thread is executed by GUI thread. 2024-02-02 13:39:00 +01:00
e7f45251a9 Improve on download functionlity 2024-02-02 13:38:40 +01:00
0839254f06 Do real download, not just some simulation. 2024-02-02 13:36:09 +01:00
6005fddc66 Start download thread when requested. 2024-02-02 13:35:06 +01:00
392b849a9b User version 1.0.2 2024-02-02 13:34:39 +01:00
6fa689f142 Use version 1.0.2 2024-02-02 13:34:13 +01:00
fc28666946 Improved creating/attaching shared memory: there is a possibility of a stale shared memory
under /tmp.
2024-02-02 13:32:56 +01:00
3bf3e741a0 Add os_release, date and creator variables. 2024-02-02 13:32:16 +01:00
f6efb1ee9a Use actually reporting coe, not just some test code. 2024-02-02 13:31:32 +01:00
a1237749dc Minor change: add some commented out code 2024-02-02 13:31:04 +01:00
f5613d5aad Minor: directly initialize doenload/reporting-thread pointers. 2024-02-02 11:09:14 +01:00
3564bac8d4 Add reference to plugin (m_hw), so it is possible to start the download thread. 2024-02-02 11:07:40 +01:00
13 changed files with 332 additions and 137 deletions

View File

@ -134,7 +134,8 @@
class DownloadThread;
class hwinf;
class T_datif : public QObject
{
Q_OBJECT
@ -165,13 +166,14 @@ class T_datif : public QObject
T_prot *myDCIF;
QTimer *datif_trigger;
uint8_t selectedSlaveAddr;
hwinf *m_hw = nullptr;
private slots:
char datif_cycleSend();
void StoredRecData();
public:
T_datif(QObject *parent = nullptr);
T_datif(hwinf *hw, QObject *parent = nullptr);
void resetChain(void);
char isPortOpen(void);

View File

@ -93,14 +93,13 @@ private:
void sub_storeSendingText(QByteArray *buf) const;
QTimer *hwapi_TimerPayment;
QSharedMemory *m_sharedMem;
ReportingThread *m_reportingThread;
DownloadThread *m_downloadThread;
ReportingThread *m_reportingThread = nullptr;
DownloadThread *m_downloadThread = nullptr;
//QTimer *hwapi_triggerBL;
public:
explicit hwapi(QWidget *parent = nullptr);
#ifdef THIS_IS_CA_MASTER
T_datif *myDatif;
#endif

View File

@ -3,6 +3,7 @@
#include <QThread>
#include <QString>
// #include <functional>
class hwapi;
class ReportingThread : public QThread {
@ -12,6 +13,14 @@ public:
ReportingThread(hwapi *hw);
~ReportingThread();
//void setFunction(std::function<void(QString const&)> f) {
// m_f = f;
//}
//std::function<void(QString const&)> function() {
// return m_f;
//}
protected:
// reporting thread does not have a running event queue, and therefore
// no slots. signals work the usual way.
@ -20,6 +29,7 @@ protected:
private:
hwapi *m_hw;
QString m_fileToDownload;
//std::function<void(QString const&)> m_f;
};
#endif // REPORTING_THREAD_H_INCLUDED

View File

@ -321,6 +321,10 @@ struct SharedMem
std::atomic_bool m_finished{false};
} m_downLoadDC;
// meta-data
char os_release[64];
char date_of_creation[32];
char creator[128]; // name of application plus pid
static QSharedMemory *getShm(std::size_t s = 0);

View File

@ -1,6 +1,6 @@
TEMPLATE = lib
TARGET = CAmaster
VERSION="1.0.1"
VERSION="1.0.2"
HEADERS += \
../include/com.h \

View File

@ -1,6 +1,6 @@
TEMPLATE = lib
TARGET = CAslave
VERSION="1.0.1"
VERSION="1.0.2"
HEADERS += \
../include/reporting_thread.h

View File

@ -9,10 +9,14 @@ History:
#include "sendWRcmd.h"
#include "controlBus.h"
#include "storeINdata.h"
#include "download_thread.h"
#include <QDebug>
#include <datei.h>
#include <QDir>
#include <QString>
#include <QDateTime>
@ -54,8 +58,10 @@ static uint8_t datif_pNextCmd, datif_sendSlowCmd;
//#define DATIF_CTR_GOTRESPVAL 100
T_datif::T_datif(QObject *parent) : QObject(parent)
T_datif::T_datif(hwinf *hw, QObject *parent) : QObject(parent)
{
m_hw = hw;
QByteArray myBA;
QDir myDir("../dmd");
@ -170,10 +176,22 @@ char T_datif::datif_cycleSend()
// b) Antwort meldet Fehler -> 2x wiederholen (nach einer Luecke von 10ms )
// c) gar keine Antwort, Timeout nach 100ms -> 2x wiederholen (nach einer Luecke von 10ms )
// cycl_running=0: nichts zu tun 1: Mitteilung: Kommando wurde soeben abgesendet, 2,3,4 = Wiederholung
if (cycl_running == 0) {
if (m_hw) {
if (m_hw->dcDownloadRequested()) { // only happens in ca-master
qCritical() << "DOWNLOAD REQUESTED";
if (!m_hw->dcDownloadThreadStart()) {
qCritical() << "DOWNLOAD-THREAD NOT RUNNING WITHIN 1000ms";
} else {
qCritical() << "DOWNLOAD-THREAD RUNNING";
}
return 0;
}
}
} else {
// 21.9.23 doRepeat hier raus sonst gehts warten auch nicht mehr (BL)
// if (cycl_running && doRepeat)
if (cycl_running) // 21.9.23 doRepeat hier raus sonst gehts warten auch nicht mehr (BL)
// if (cycl_running && doRepeat)
{
// request is still running, wait for response before next sending
//qDebug()<< "datif wait for response";
datif_trigger->stop();

View File

@ -104,7 +104,10 @@ void DownloadThread::run() {
m_hw->dcDownloadRequestAck();
qCritical() << "DownloadThread::run(): DOWNLOAD THREAD STARTED";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): DOWNLOAD THREAD STARTED:";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< " DownloadThread::run(): Filename:" << m_hw->dcDownloadFileName();
// load binary device controller file into memory
QByteArray ba = loadBinaryDCFile(m_hw->dcDownloadFileName());
@ -115,13 +118,31 @@ void DownloadThread::run() {
// fill last block of data to be sent with 0xFF
ba = ba.leftJustified(totalBlocks*64, (char)(0xFF));
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): TOTAL NUMBER OF BYTES TO SEND TO DC" << ba.size();
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): TOTAL NUMBER OF BLOCKS" << totalBlocks;
m_hw->dc_autoRequest(true); // turn auto-request setting on
m_hw->request_DC2_HWversion();
m_hw->request_DC2_SWversion();
QThread::sleep(1);
resetDeviceController();
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): RESET DEVICE-CONTROLLER";
QThread::sleep(1);
if (startBootloader()) {
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): STARTED BOOT-LOADER";
qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BYTES TO SEND TO DC" << ba.size();
qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BLOCKS" << totalBlocks;
int currentBlock = 0;
m_hw->dc_autoRequest(false);// turn auto-request setting off for
// download of binary dc
#if 0
int currentBlock = 0; // download of binary dc
DownloadResult res = DownloadResult::OK;
qCritical() << "64-byte block " << currentBlock;
while (res != DownloadResult::ERROR && currentBlock < totalBlocks) {
@ -149,27 +170,31 @@ void DownloadThread::run() {
} else {
m_hw->bl_sendLastBlock();
m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
qCritical() << "DownloadThread::run(): currentBlockNumber" << currentBlock;
// QThread::msleep(250);
}
qCritical() << "DownloadThread::run(): last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK());
#else
for (uint16_t currentBlock = 0; currentBlock <= totalBlocks; ++currentBlock) {
m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
qCritical() << "DownloadThread::run(): currentBlockNumber" << currentBlock;
QThread::msleep(250);
}
#endif
m_hw->dc_autoRequest(true); // turn auto-request setting on again
}
stopBootloader(); // there is no harm in stopping the bootloader even
} // if it was not started at all
#if 0
// test code:
uint16_t const totalBlocks = 100;
m_hw->dcDownloadSetTotalBlockNumber(totalBlocks);
for (uint16_t currentBlock = 0; currentBlock <= totalBlocks; ++currentBlock) {
m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
QThread::msleep(100);
// if it was not started at all
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DownloadThread::run(): STOPPED BOOT-LOADER";
}
#endif
m_hw->dcDownloadSetRunning(false);
m_hw->dcDownloadSetFinished(true);
qCritical() << QDateTime::currentDateTime().toString(Qt::ISODate) + "DOWNLOAD THREAD FINISHED";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "DOWNLOAD THREAD FINISHED";
// the object deletes itself ! This is the last line in run().
// Never touch the object after this statement
@ -261,45 +286,59 @@ DownloadThread::sendNextDataBlock(QByteArray const &binary, int bNum) const {
}
bool DownloadThread::startBootloader() const {
qDebug() << "starting bootloader...";
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "starting bootloader...";
int nTry = 5;
while (--nTry >= 0) {
m_hw->bl_startBL();
QThread::msleep(5000);
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << "bl_startBL() ..." << nTry;
QThread::msleep(1000);
m_hw->bl_checkBL();
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << "bl_checkBL() ..." << nTry;
QThread::msleep(1000);
if (m_hw->bl_isUp()) {
qInfo() << "starting bootloader...OK";
QThread::msleep(5000);
qInfo() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << "bootloader... isUP" << nTry;
return true;
} else {
qCritical() << "bootloader not up (" << nTry << ")";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "bootloader not up (" << nTry << ")";
}
}
qCritical() << "starting bootloader...FAILED";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "starting bootloader FAILED";
return false;
}
bool DownloadThread::stopBootloader() const {
qDebug() << "stopping bootloader...";
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "stopping bootloader...";
int nTry = 5;
while (--nTry >= 0) {
m_hw->bl_stopBL();
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "bl_stopBL() ...";
QThread::msleep(500);
m_hw->bl_checkBL();
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << "bl_checkBL() ..." << nTry;
if (!m_hw->bl_isUp()) {
qInfo() << "stopping bootloader...OK";
qInfo() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "stopping bootloader OK";
return true;
}
}
qCritical() << "stopping bootloader...FAILED";
qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "stopping bootloader FAILED";
return false;
}
bool DownloadThread::resetDeviceController() const {
qDebug() << "resetting device controller...";
qDebug() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "resetting device controller...";
m_hw->bl_rebootDC();
// wait maximally 3 seconds, before starting bootloader
QThread::sleep(1);
qInfo() << "resetting device controller...OK";
qInfo() << QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "resetting device controller...OK";
return true;
}

View File

@ -22,11 +22,12 @@
#include "hwapi.h"
#include "download_thread.h"
#include "reporting_thread.h"
#include "shared_mem_buffer.h"
#include <cstring>
#include <QThread>
#include <QDebug>
#include <QApplication>
static uint32_t hwapi_lastStartAmount;
static uint32_t hwapi_lastTotalAmount;
@ -58,6 +59,9 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
qCritical() << "Creating/attaching shared memory failed";
}
Q_ASSERT_X(sizeof(SharedMem) != m_sharedMem->size(),
"compare sizes", "sizes different");
//if (shdMem_firstUse()) // für Master raus
// {
@ -67,7 +71,7 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
#error "SLAVE LIB COMPILED INTO MASTER"
#endif
myDatif = new T_datif(); // für die CAslave-Lib auskommentieren!
myDatif = new T_datif(this); // für die CAslave-Lib auskommentieren!
#endif
@ -4412,6 +4416,7 @@ QObject const *hwapi::getAPI() {
}
bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
// called by worker-thread (see atbupdatetool)
SharedMem *data = SharedMem::getData();
if (!data) {
return false;
@ -4435,9 +4440,15 @@ bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
}
bool hwapi::dcDownloadRequested() const {
SharedMem const *data = SharedMem::getData();
// should be false at entry
return data ? data->m_downLoadDC.m_requested.load() : false;
SharedMem *data = SharedMem::getData();
Q_ASSERT_X(data != nullptr, "check", "pointer invalid");
Q_ASSERT_X((void *)data != m_sharedMem->data(), "compare pointers", "pointers different");
Q_ASSERT_X(sizeof(*data) != m_sharedMem->size(), "compare sizes", "sizes different");
// called by download-thread
// 1: true at entry: reset atomically to false
// 2: false at entry: no change
return data ? data->m_downLoadDC.m_requested.exchange(false) : false;
}
bool hwapi::dcDownloadResetRequest() const {
@ -4449,13 +4460,11 @@ bool hwapi::dcDownloadResetRequest() const {
}
bool hwapi::dcDownloadRequestAck() const {
// called by download-thread
SharedMem *data = SharedMem::getData();
if (data) {
if (data->m_downLoadDC.m_requested) {
data->m_downLoadDC.m_requested = false;
data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_finished = false;
}
data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_finished = false;
}
return false;
}
@ -4465,14 +4474,16 @@ bool hwapi::dcDownloadRunning() const {
if (data) {
int cnt = 10;
while (--cnt > 0) {
bool running = data->m_downLoadDC.m_running.load();
bool finished = data->m_downLoadDC.m_finished.load();
if (!running || finished) {
if (cnt < 3) {
qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished;
}
QThread::msleep(500);
} else break;
bool running = data->m_downLoadDC.m_running;
bool finished = data->m_downLoadDC.m_finished;
if ((running == true) && (finished == false)) {
// see dcDownloadRequestAck()
break;
}
if (cnt < 3) {
qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished;
}
QThread::msleep(500);
}
// qCritical() << "DOWNLOAD RUNNING" << cnt << (cnt > 0);
return (cnt > 0);
@ -4481,18 +4492,16 @@ bool hwapi::dcDownloadRunning() const {
}
void hwapi::dcDownloadThreadFinalize(DownloadThread *dthread) {
delete dthread;
Q_UNUSED(dthread);
// delete dthread;
m_downloadThread = nullptr;
}
bool hwapi::dcDownloadFinished() {
SharedMem const *data = SharedMem::getDataConst();
if (data) {
int cnt = 10;
while ((--cnt > 0) &&
((data->m_downLoadDC.m_running.load() == true) &&
(data->m_downLoadDC.m_finished.load() == false))) {
QThread::sleep(1);
int cnt = 10;
while (dcDownloadRunning()) {
if (--cnt == 0) {
return false;
}
//if (cnt > 0) {
@ -4501,12 +4510,13 @@ bool hwapi::dcDownloadFinished() {
// return true;
//}
}
return false;
return true;
}
// download thread
bool hwapi::dcDownloadThreadStart() {
// called by timer in datIf.cpp: T_datif::datif_cycleSend()
m_downloadThread = new DownloadThread(this);
if (m_downloadThread) {
m_downloadThread->start();
@ -4538,6 +4548,7 @@ bool hwapi::dcDownloadReportThreadStart() { // only start reporting thread
if (cnt > 0) {
m_reportingThread = new ReportingThread(this);
if (m_reportingThread) {
m_reportingThread->moveToThread(QApplication::instance()->thread());
m_reportingThread->start();
cnt = 10;
while (--cnt > 0 && !dcDownloadReportThreadRunning()) {
@ -4569,7 +4580,9 @@ void hwapi::dcDownloadReportThreadQuit() {
}
bool hwapi::dcDownloadReportThreadFinished() const {
return m_reportingThread ? m_reportingThread->isFinished() : false;
// if the pointer to the underlying c++-object is not valid, the thread
// counts as finished
return m_reportingThread ? m_reportingThread->isFinished() : true;
}
bool hwapi::dcDownloadReportStart() const {
@ -4595,8 +4608,10 @@ bool hwapi::dcDownloadReportFinished() {
}
if (dcDownloadReportThreadFinished()) {
delete m_reportingThread;
m_reportingThread = nullptr;
if (m_reportingThread) {
delete m_reportingThread;
m_reportingThread = nullptr;
}
}
return true;

View File

@ -4,10 +4,15 @@
#include <QDateTime>
#include <QDebug>
#include <QApplication>
#include <QCoreApplication>
#include <cmath>
ReportingThread::ReportingThread(hwapi *hw)
: m_hw(hw)
, m_fileToDownload(m_hw->dcDownloadFileName()) {
// , m_f([](QString const&){}) {
}
ReportingThread::~ReportingThread() {
@ -17,7 +22,10 @@ ReportingThread::~ReportingThread() {
// each component which has connects for the corresponding signals.
void ReportingThread::run() {
qCritical() << QDateTime::currentDateTime() << "START DOWNLOAD THREAD";
qCritical()
<< QDateTime::currentDateTime().time().toString(Qt::ISODate)
<< "START REPORT THREAD"
<< "(PART OF APPLICATION" << QCoreApplication::applicationName() << ")";
static QString report("");
@ -25,9 +33,9 @@ void ReportingThread::run() {
while (!m_hw->dcDownloadGetRunning()) {
if (--cnt > 0) {
report = QString("%1 waiting for download to start %2")
.arg(QDateTime::currentDateTime().toString(Qt::ISODate))
.arg(QDateTime::currentDateTime().time().toString(Qt::ISODate))
.arg(cnt);
qCritical() << __LINE__ << "STATUS" << report;
qCritical() << "(" << __func__ << ":" << __LINE__ << ") STATUS" << report;
emit m_hw->hwapi_reportDCDownloadStatus(report);
QThread::sleep(1);
} else break;
@ -96,28 +104,37 @@ void ReportingThread::run() {
qCritical() << QDateTime::currentDateTime() << "TOTAL BLOCKS" << totalBlocks;
if (totalBlocks) {
qint64 const start = QDateTime::currentMSecsSinceEpoch();
double durationMillis = 0;
QDateTime const start = QDateTime::currentDateTime();
uint16_t currentBlockNumber = 0;
uint16_t prevBlockNumber = ~0;
while (m_hw->dcDownloadGetRunning()) {
currentBlockNumber = m_hw->dcDownloadGetCurrentBlockNumber();
if (prevBlockNumber != currentBlockNumber) {
double durationSecs = start.secsTo(QDateTime::currentDateTime());
durationMillis += QDateTime::currentMSecsSinceEpoch() - start;
double const timeAveragePerBlock = (currentBlockNumber > 0) ? (durationSecs / currentBlockNumber) : durationSecs;
long estimatedSecondsLeft = lround((timeAveragePerBlock * (totalBlocks - currentBlockNumber)));
long estimatedMinutesLeft =
((estimatedSecondsLeft % 60) == 0) ?
(estimatedSecondsLeft / 60) :
((estimatedSecondsLeft + 60) / 60);
estimatedSecondsLeft = (estimatedSecondsLeft % 60);
double const timeAveragePerBlock = (currentBlockNumber > 0) ? (durationMillis / currentBlockNumber) : durationMillis;
double const estimatedSecondsLeft = (timeAveragePerBlock * (totalBlocks - currentBlockNumber)) / 1000.0;
double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0;
report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4min %5s)")
.arg(totalBlocks)
.arg(currentBlockNumber)
.arg(percent, 0, 'f', 2)
.arg(estimatedMinutesLeft)
.arg(estimatedSecondsLeft, 2);
double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0;
report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4s)")
.arg(totalBlocks)
.arg(currentBlockNumber)
.arg(percent, 0, 'f', 2)
.arg(estimatedSecondsLeft, 0, 'f', 2);
qCritical() << m_hw << "RT report" << report;
qCritical() << "RT report" << report;
emit m_hw->hwapi_reportDCDownloadStatus(report);
emit m_hw->hwapi_reportDCDownloadStatus(report);
prevBlockNumber = currentBlockNumber;
}
QThread::msleep(100);
}

View File

@ -641,8 +641,11 @@ void T_runProc::bl_rebootDC(void)
// BL is working for 5s after power-on-reset.
uint8_t len, buf[20];
memset(buf, 0x00, sizeof(buf));
len=dcBL_restartDC(buf);
qCritical() << __func__ << QByteArray((const char *)buf, len).toHex(':');
sendWRcmd_setSendBlock160(len, buf);
}
@ -652,8 +655,13 @@ void T_runProc::bl_startBL(void)
// otherwise the BL jumps to normal DC application
uint8_t len, buf[20];
memset(buf, 0x00, sizeof(buf));
len=dcBL_activatBootloader(buf);
qCritical() << __func__ << QByteArray((const char *)buf, len).toHex(':');
sendWRcmd_setSendBlock160(len, buf);
epi_setNowIsBootload(true);
}
@ -661,8 +669,11 @@ void T_runProc::bl_checkBL(void)
{
// call this function in order to get information, afterwards use "bl_isUp()"
uint8_t len, buf[20];
memset(buf, 0x00, sizeof(buf));
len=dcBL_readFWversion(buf);
qCritical() << __func__ << QByteArray((const char *)buf, len).toHex(':');
sendWRcmd_setSendBlock160(len, buf);
}

View File

@ -1,7 +1,13 @@
#include "shared_mem_buffer.h"
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QDateTime>
#include <QCoreApplication>
#include <atomic>
#include <algorithm>
#ifdef QT_POSIX_IPC
// The POSIX backend can be explicitly selected using the -feature-ipc_posix
@ -9,33 +15,124 @@
// macro will be defined. -> we use SystemV shared memory
#error "QT_POSIX_IPC defined"
#else
#ifdef __linux__
#ifdef linux
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h> // ftok
#endif
#endif
//static bool shdMemFirstUse;
static QString read1stLineOfFile(QString fileName) { // read for instance /etc/os-release
QFile f(fileName);
if (f.exists()) {
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&f);
in.setCodec("UTF-8");
while(!in.atEnd()) {
return in.readLine();
}
}
}
return "N/A";
}
//QSharedMemory *SharedMemBuffer::getShm(std::size_t size) {
QSharedMemory *SharedMem::getShm(std::size_t size) {
static QSharedMemory shMem;
if (size > 0) {
#ifdef __linux__
//static const long nativeKey = ftok("/etc/os-release", 'H');
//static const QString fkey = std::to_string(nativeKey).c_str();
static const QString fkey = "0123456?000=7";
#ifdef linux
static const long nativeKey = ftok("/etc/os-release", 'H');
static const QString fkey = std::to_string(nativeKey).c_str();
//static const QString fkey = "0123456?000=7";
#else
static const QString fkey = "0123456?000=9";
#endif
qCritical() << __func__ << ":" << __LINE__ << ": size" << size;
shMem.setKey(fkey);
if (!shMem.isAttached()) {
if (shMem.create(size)) {
qCritical() << __func__ << ":" << __LINE__ << ": created shared memory";
#ifdef linux
if ((size != (std::size_t)shMem.size()) || (sizeof(SharedMem) != shMem.size())) {
qCritical() << __func__ << ":" << __LINE__
<< "size=" << size
<< "shMem.size=" << shMem.size()
<< "sizeof(SharedMem)=" << sizeof(SharedMem)
<< "ABOUT TO REBOOT SYSTEM...";
if (system("reboot")) {
// reboot system -> shared memory re-created
}
}
struct SharedMem *sh = (SharedMem *)(shMem.data());
memset(sh->indat_HWversion, 0x00, sizeof(sh->indat_HWversion));
memset(sh->indat_SWversion, 0x00, sizeof(sh->indat_SWversion));
memset(sh->os_release, 0x00, sizeof(sh->os_release));
memset(sh->date_of_creation, 0x00, sizeof(sh->date_of_creation));
memset(sh->creator, 0x00, sizeof(sh->creator));
QString const &os_release = read1stLineOfFile("/etc/os-release");
strncpy(sh->os_release, os_release.toStdString().c_str(),
std::min((int)os_release.size(), (int)sizeof(sh->os_release)-1));
QString const &currentDateTime = QDateTime::currentDateTime().toString(Qt::ISODate);
char const *current = currentDateTime.toUtf8().constData();
strncpy(sh->date_of_creation, current,
std::min((int)strlen(current), (int)sizeof(sh->date_of_creation)-1));
QString const &appPid = QString("%1 (%2)").arg(QCoreApplication::applicationName())
.arg(QCoreApplication::applicationPid());
strncpy(sh->creator, appPid.toStdString().c_str(),
std::min((int)strlen(appPid.toStdString().c_str()),
(int)sizeof(sh->creator)-1));
qCritical() << "os-release:" << sh->os_release;
qCritical() << " creator:" << sh->creator;
qCritical() << " date:" << sh->date_of_creation;
#else
Q_ASSERT_X(size != shMem.size(), "compare sizes", "sizes different");
Q_ASSERT_X(sizeof(SharedMem) != shMem.size(), "compare sizes", "sizes different");
#endif
return &shMem;
} else {
if (shMem.error() == QSharedMemory::AlreadyExists) {
if (shMem.attach()) {
#ifdef linux
if ((size != (std::size_t)shMem.size()) || (sizeof(SharedMem) != shMem.size())) {
qCritical() << __func__ << ":" << __LINE__
<< "size=" << size
<< "shMem.size=" << shMem.size()
<< "sizeof(SharedMem)=" << sizeof(SharedMem)
<< "ABOUT TO REBOOT SYSTEM...";
if (system("reboot")) {
// reboot system -> shared memory re-created
}
}
#else
Q_ASSERT_X(size != shMem.size(), "compare sizes", "sizes different");
Q_ASSERT_X(sizeof(SharedMem) != shMem.size(), "compare sizes", "sizes different");
#endif
QString const &appPid = QString("%1 (%2)").arg(QCoreApplication::applicationName())
.arg(QCoreApplication::applicationPid());
qCritical() << __func__ << ":" << __LINE__ << appPid << "attached shared memory";
struct SharedMem const *sh = (SharedMem const *)(shMem.data());
qCritical() << "os-release:" << sh->os_release;
qCritical() << " creator:" << sh->creator;
qCritical() << " date:" << sh->date_of_creation;
return &shMem;
}
}
@ -43,6 +140,13 @@ QSharedMemory *SharedMem::getShm(std::size_t size) {
qCritical() << shMem.nativeKey() << shMem.key() << shMem.data()
<< shMem.error() << shMem.errorString();
return nullptr;
} else {
qCritical() << __func__ << ":" << __LINE__ << "shared memory already attached";
struct SharedMem const *sh = (SharedMem const *)(shMem.data());
qCritical() << "os-release:" << sh->os_release;
qCritical() << " creator:" << sh->creator;
qCritical() << " date:" << sh->date_of_creation;
}
}
return &shMem;

View File

@ -64,66 +64,42 @@ bool indat_isMdbOn()
void gpi_storeHWver(QString text)
{
// change Qstring to array of chars, because shared mem allowes no QString!
int nn, LL = text.length();
if (LL >= versionBufferLen)
LL=versionBufferLen-1; // leave place for termination
for (nn=0; nn<LL; nn++)
{
SharedMem::write()->indat_HWversion[nn] = text.at(nn).toLatin1();
QString hwVersion((char const *)SharedMem::read()->indat_HWversion);
if (hwVersion != text) {
char const *p = text.toUtf8().constData();
if (qstrlen(p) > 0) {
memset(SharedMem::write()->indat_HWversion, 0, versionBufferLen);
memcpy(SharedMem::write()->indat_HWversion, p,
qMin((uint)versionBufferLen, qstrlen(p)) - 1);
}
}
for (nn=LL; nn<versionBufferLen; nn++)
SharedMem::write()->indat_HWversion[nn] =0;
qCritical() << "HW TEXT" << epi_loadHWver();
}
QString epi_loadHWver(void)
{
// load array of chars from SM and change to QString
int nn, LL = versionBufferLen;
char cc;
QString myStr;
myStr.clear();
for (nn=0; nn<LL; nn++)
{
cc = SharedMem::read()->indat_HWversion[nn];
myStr.append(cc);
}
return myStr;
return QString((char const*)SharedMem::read()->indat_HWversion);
}
void gpi_storeSWver(QString text)
{
int nn, LL = text.length();
if (LL >= versionBufferLen)
LL=versionBufferLen-1; // leave place for termination
for (nn=0; nn<LL; nn++)
{
SharedMem::write()->indat_SWversion[nn] = text.at(nn).toLatin1();
{
QString swVersion((char const *)SharedMem::read()->indat_SWversion);
if (swVersion != text) {
char const *p = text.toUtf8().constData();
if (qstrlen(p) > 0) {
memset(SharedMem::write()->indat_SWversion, 0, versionBufferLen);
memcpy(SharedMem::write()->indat_SWversion, p,
qMin((uint)versionBufferLen, qstrlen(p)) - 1);
}
}
for (nn=LL; nn<versionBufferLen; nn++)
SharedMem::write()->indat_SWversion[nn] =0;
qCritical() << "SW TEXT" << epi_loadSWver();
}
QString epi_loadSWver(void)
{
int nn, LL = versionBufferLen;
char cc;
QString myStr;
myStr.clear();
for (nn=0; nn<LL; nn++)
{
cc = SharedMem::read()->indat_SWversion[nn];
myStr.append(cc);
}
return myStr;
return QString((char const*)SharedMem::read()->indat_SWversion);
}
void gpi_storeDCstate(QString text)