From fb75074955a282756b31a073dd9c71beb8638d91 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Wed, 26 Apr 2023 14:39:32 +0200 Subject: [PATCH] Checking to test compilation --- DCPlugin.pri | 8 +- include/dcBL.h | 2 +- include/sendWRcmd.h | 22 +- include/serial_port_thread.h | 22 + include/serial_port_worker.h | 121 +++++ include/shared_mem_buffer.h | 232 ++++++---- src/controlBus.cpp | 148 +++--- src/datIf.cpp | 622 +++++++------------------- src/dcBL.cpp | 750 +++++++++++++------------------ src/hwapi.cpp | 34 +- src/main.cpp | 9 + src/sendWRcmd.cpp | 840 +++++++++++++---------------------- src/serial_port_thread.cpp | 15 + src/serial_port_worker.cpp | 244 ++++++++++ src/shared_mem_buffer.cpp | 84 ++++ 15 files changed, 1520 insertions(+), 1633 deletions(-) create mode 100644 include/serial_port_thread.h create mode 100644 include/serial_port_worker.h create mode 100644 src/main.cpp create mode 100644 src/serial_port_thread.cpp create mode 100644 src/serial_port_worker.cpp diff --git a/DCPlugin.pri b/DCPlugin.pri index 1f6add0..c0ba873 100644 --- a/DCPlugin.pri +++ b/DCPlugin.pri @@ -11,7 +11,9 @@ HEADERS += $${PWD}/include/com.h \ $${PWD}/include/sendWRcmd.h \ $${PWD}/include/storeINdata.h \ $${PWD}/include/tslib.h \ - $${PWD}/include/shared_mem_buffer.h + $${PWD}/include/shared_mem_buffer.h \ + $${PWD}/include/serial_port_thread.h \ + $${PWD}/include/serial_port_worker.h SOURCES += $${PWD}/src/com.cpp \ $${PWD}/src/controlBus.cpp \ @@ -22,4 +24,6 @@ SOURCES += $${PWD}/src/com.cpp \ $${PWD}/src/sendWRcmd.cpp \ $${PWD}/src/storeINdata.cpp \ $${PWD}/src/tslib.cpp \ - $${PWD}/src/shared_mem_buffer.cpp + $${PWD}/src/shared_mem_buffer.cpp \ + $${PWD}/src/serial_port_thread.cpp \ + $${PWD}/src/serial_port_worker.cpp diff --git a/include/dcBL.h b/include/dcBL.h index f605270..d2ecc45 100644 --- a/include/dcBL.h +++ b/include/dcBL.h @@ -78,7 +78,7 @@ uint8_t dcBL_getResult(void); #define RAW_BL_DATALEN 150 -void gpi_storeRawReceivedData(uint8_t RdDlen, uint8_t *receivedData); +void gpi_storeRawReceivedData(uint8_t RdDlen, uint8_t const *receivedData); uint8_t epi_getRawReceivedData(uint8_t *receivedData); // retval=length, will be zeroed after first reading diff --git a/include/sendWRcmd.h b/include/sendWRcmd.h index dd37064..1330724 100644 --- a/include/sendWRcmd.h +++ b/include/sendWRcmd.h @@ -148,21 +148,23 @@ // highest priority -#define CMDSTACKDEPTH 16 +// #define CMDSTACKDEPTH 16 // means: up to 16 cmd can be stored. They are issued one by one every 100ms void sendWRcmd_clrCmdStack(void); -bool sendWRcmd_setSendCommand0(uint16_t nextCmd); +bool sendWRcmd_setSendCommand0(uint16_t nextCmd, uint8_t dat1=0, uint8_t dat2=0, + uint8_t dat3=0, uint8_t dat4=0); // GUI or app sends a command to DC transfered by serial -uint16_t sendWRcmd_getSendCommand0(void); +uint16_t sendWRcmd_getSendCommand0(uint8_t *dat1=0, uint8_t *dat2=0, + uint8_t *dat3=0, uint8_t *dat4=0); // lower priority -#define CMD4STACKDEPTH 8 +// #define CMD4STACKDEPTH 8 void sendWRcmd_clrCmd4Stack(void); bool sendWRcmd_setSendCommand4(uint16_t nextCmd, uint8_t dat1, uint8_t dat2, uint8_t dat3, uint8_t dat4); uint16_t sendWRcmd_getSendCommand4(uint8_t *dat1, uint8_t *dat2, uint8_t *dat3, uint8_t *dat4); -#define CMD8STACKDEPTH 4 +// #define CMD8STACKDEPTH 4 void sendWRcmd_clrCmd8Stack(void); bool sendWRcmd_setSendCommand8(uint16_t nextCmd, uint8_t dat1, uint8_t dat2, uint16_t dat3, uint32_t dat4); uint16_t sendWRcmd_getSendCommand8(uint8_t *dat1, uint8_t *dat2, uint16_t *dat3, uint32_t *dat4); @@ -171,7 +173,7 @@ uint16_t sendWRcmd_getSendCommand8(uint8_t *dat1, uint8_t *dat2, uint16_t *dat3, // lowest priority // wait for resonse before send next! -bool sendWRcmd_setSendBlock160(uint8_t leng, uint8_t *buf); +bool sendWRcmd_setSendBlock160(uint8_t leng, uint8_t const *buf); uint8_t sendWRcmd_getSendBlock160(uint8_t *leng, uint8_t *buf); // retval = *leng @@ -179,7 +181,7 @@ void sendWRcmd_INI(void); -uint8_t epi_store64ByteSendData(uint8_t length, uint8_t *buf); +uint8_t epi_store64ByteSendData(uint8_t length, uint8_t const *buf); // HWapi writes data to be forwarded to DC and further to mdb-device // not batched! don't use twice within 100ms @@ -211,7 +213,7 @@ uint8_t gpi_getUserOfSendingTextBuffer(uint8_t *para1, uint8_t *para2, uint8_t * // user=1: Text-Print is using this buffer // 2: QR-code-Printer is using this buffer -#define FDCMD_STACKDEPTH 16 +//#define FDCMD_STACKDEPTH 16 void sendFDcmd_clrStack(void); bool sendFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, uint8_t dat1, uint8_t dat2, uint8_t dat3, uint8_t dat4); // write Command to memory, wait for transport @@ -223,9 +225,9 @@ uint8_t check4FDshortCmd(void); uint8_t check4freeFDshortCmd(void); // returns number of free places in short-command stack -#define FDLONG_STACKDEPTH 16 +//#define FDLONG_STACKDEPTH 16 void longFDcmd_clrStack(void); -bool longFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, uint8_t length, uint8_t *data); +bool longFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, uint8_t length, uint8_t const *data); // write Command to memory, wait for transport // data buffer size always 64! data[64], padded with 0 bool longFDcmd_get(uint8_t *nextWrCmd, uint8_t *nextRdCmd, uint8_t *blockNum, uint8_t *length, uint8_t *data); diff --git a/include/serial_port_thread.h b/include/serial_port_thread.h new file mode 100644 index 0000000..63ef8c8 --- /dev/null +++ b/include/serial_port_thread.h @@ -0,0 +1,22 @@ +#ifndef SERIAL_PORT_THREAD_H_INCLUDED +#define SERIAL_PORT_THREAD_H_INCLUDED + +#include +#include +#include + +class SerialPortThread : public QThread { + Q_OBJECT + + QEventLoop m_loop; +public: + SerialPortThread(QObject *parent = nullptr); + virtual ~SerialPortThread(); + + virtual void run() override; + + QEventLoop &getEventLoop() { return m_loop; } +}; + + +#endif // SERIAL_PORT_THREAD_H_INCLUDED diff --git a/include/serial_port_worker.h b/include/serial_port_worker.h new file mode 100644 index 0000000..ac2058c --- /dev/null +++ b/include/serial_port_worker.h @@ -0,0 +1,121 @@ +#ifndef SERIAL_PORT_WORKER_H_INCLUDED +#define SERIAL_PORT_WORKER_H_INCLUDED + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + + +struct Command { + enum : uint16_t {PRIORITY_1, PRIORITY_2, PRIORITY_3, PRIORITY_4, END_MARK}; + enum : uint16_t {QUIT_SENTINEL = 0x1000, SEND_BLOCK_SIZE = 160}; + uint32_t m_priority; + uint16_t m_priorityClass; + uint16_t m_cmdId; + char *m_sendBuffer; + char *m_recvBuffer; + uint8_t m_sendBufferLength; + uint8_t m_recvBufferLength; // expected result length + + static std::atomic m_nextSendBufferIndex; + static std::atomic m_nextRecvBufferIndex; + static char m_sendBuffers[256][SEND_BLOCK_SIZE]; + static char m_recvBuffers[256][SEND_BLOCK_SIZE]; + + static uint32_t getNextCommandPriority(uint8_t cmdType); + static char *getNextSendBuffer(); + static char *getNextRecvBuffer(); + + public: + Command(uint16_t cmdId, uint8_t sendBufferLength, uint8_t recvBufferLength, uint8_t priorityClass); + Command(); + + friend bool operator< (Command const& lhs, Command const& rhs) { + return lhs.m_priority < rhs.m_priority; + } + + friend std::ostream& operator<< (std::ostream& os, Command const& cmd) { + return os << "{ " << cmd.m_priority << ", '" << cmd.m_priority << "' } "; + } +}; + +/// \brief SerialPortWorker +/// +/// SerialPortWorker sends and receives data from serial port +/// +class SerialPortWorker : public QThread { + Q_OBJECT + + /// \brief Internal serial port. + QSerialPort m_serialPort; + + /// \brief Internal serial port info. + QSerialPortInfo m_serialPortInfo; + + /// \brief Flag indicating if serial port is opened. + bool m_serialPortOpened; + + /// \brief Port name of serial port. + QString m_portName; + + /// \brief Baudrate of internal serial port. + enum QSerialPort::BaudRate m_baudRate; + + std::priority_queue> m_sendQueue; + std::deque m_recvQueue; + std::mutex m_sendQueueMutex; + std::mutex m_recvQueueMutex; + std::condition_variable m_sendQueueCondVar; + std::condition_variable m_recvQueueCondVar; + + std::atomic m_bytesWritten; + std::atomic m_bytesAvailable; + + SerialPortWorker(QString portName, QSerialPort::BaudRate baudRate, QObject *parent = nullptr); + + Command getNextCommand(); + void insertResult(Command const &result); + + enum : uint16_t {TIMEOUT = 100, WAIT_TIME_MAX = 1000}; + + public: + ~SerialPortWorker(); + + SerialPortWorker(SerialPortWorker const &) = delete; + void operator=(SerialPortWorker const &) = delete; + + // serial port im plugin anlegen. erst dann kann getInstance() benutzt werden + static SerialPortWorker &getInstance(QString portName, QSerialPort::BaudRate baudRate, QObject *parent = nullptr) { + static SerialPortWorker serialPortWorker(portName, baudRate, parent); + return serialPortWorker; + } + + bool openSerialPort(); + void closeSerialPort(); + + bool isPortOpen() const { return m_serialPortOpened; } + + void insertCommand(Command const &cmd); + bool getNextResult(Command &result, int timeout = 1000); + + virtual void run() override; + + void quit(); + +private slots: + void getBytesAvailable(); + bool getNumberOfBytesWritten(qint64); +}; + +#endif // SERIAL_PORT_WORKER_H_INCLUDED diff --git a/include/shared_mem_buffer.h b/include/shared_mem_buffer.h index 59410dc..8b6ddd9 100644 --- a/include/shared_mem_buffer.h +++ b/include/shared_mem_buffer.h @@ -6,7 +6,33 @@ #include +// TODO: pid eintragen und convars/mutexe eintragen + +#ifdef __linux__ + + +#include + +// PTHREAD_PROCESS_SHARED + +#else +// WIN32 +#endif + struct SharedMemBuffer { + int init; + char appName[32]; + pid_t pid; + pthread_condattr_t sndCndA; + pthread_cond_t sndCndV; + pthread_mutexattr_t sndMtxA; + pthread_mutex_t sndMtx; + + pthread_condattr_t rcvCndA; + pthread_cond_t rcvCndV; + pthread_mutexattr_t rcvMtxA; + pthread_mutex_t rcvMtx; + struct rs { char comportName[16]; // z.B. "COM48" char baudStr[16]; // z.B. "19200" @@ -18,109 +44,130 @@ struct SharedMemBuffer { char AutoEmissionOn; // 1: zyklisch Anfragen zum Slave senden struct datif { +#define DATIF_MAXCMDS 16 uint16_t sendingPeriod; bool sendingPer_changed; + uint8_t OutCmdpara1; + uint8_t OutCmdpara2; + uint8_t OutCmdpara3; + uint8_t OutCmdpara4; + uint16_t OutCmdpara5; + uint32_t OutCmdpara6; + uint8_t dataStep; + uint8_t scanStep; + uint8_t RDBLKNR; + uint8_t cycl_running; } datif; -#if 0 - // controlBus.cpp - char txt4comStateLine[32]; - char txt4HsStateLine[32]; - char txt4masterStateLine[32]; - char txt4resultStateLine[32]; - char txt4dataLine[32]; - char txt4datifReceive[32]; - char txt4diagWindow[32]; - char sndTxt4diagWindow[32]; - bool Sdata_serialTestResult[32]; - uint8_t Sdata_pProtResultOk[32]; - uint16_t Sdata_receivedDataLength[32]; - uint8_t Sdata_receivedDataBlock[64]; + struct txt4 { + char comStateLine[32]; + char HsStateLine[32]; + char masterStateLine[32]; + char resultStateLine[32]; + char dataLine[32]; + char datifReceive[32]; + char diagWindow[32]; + char sndDiagWindow[32]; + } txt4; - // datif.cpp - uint8_t dif_dataStep; - uint8_t dif_scanStep; - uint8_t RDBLKNR; - uint8_t datif_OutCmdpara1; - uint8_t datif_OutCmdpara2; - uint8_t datif_OutCmdpara3; - uint8_t datif_OutCmdpara4; + struct dcBL { +#define SIZEBLRESP 50 +#define RAW_BL_DATALEN 150 + uint8_t LastBLcmd; // stored the last sent cmd in order to analys response + uint8_t AtbBinFile[300000]; + uint32_t fileSize; + uint16_t nrOfBlocks; + uint16_t fileCrc; + uint8_t myBuf[300000]; // same content like "dcBL_AtbBinFile" but bytewise + char Resp[SIZEBLRESP][32]; + int pResp; + uint8_t step; + uint8_t state; + uint16_t BlkCtr; + uint16_t cyclCtr; + uint16_t repeatCtr; + } dcBL; - uint16_t datif_OutCmdpara5; - uint32_t datif_OutCmdpara6; - uint8_t cycl_running; + struct { + uint16_t shutterTime; + char ticketTemplate[1024]; + } hwapi; - // dcBL.cpp - uint8_t dcBL_LastBLcmd; // stored the last sent cmd in order to analys response - uint8_t dcBL_AtbBinFile[300000]; - uint32_t dcBL_fileSize; - uint16_t dcBL_nrOfBlocks; - uint16_t dcBL_fileCrc; - uint8_t dcBL_myBuf[300000]; // same content like "dcBL_AtbBinFile" but bytewise + struct { + uint8_t sendAsynchDataBuf[160]; // no stack, only ONE buffer + uint8_t sendAsyDatLen; + } next; - char BlResp[50][32]; - uint8_t dcBL_step; - uint8_t dcBL_state; - uint16_t dcBL_BlkCtr; - uint16_t dcBL_cyclCtr; - uint16_t repeatCtr; - uint8_t Sdata_rawData[150]; - uint8_t Sdata_LengthRawData; + struct { // highest priority +#define CMDSTACKDEPTH 16 + // up to 16 cmd can be stored. They are issued one by one every 100ms + uint16_t AsynchSend[CMDSTACKDEPTH]; + uint8_t para1[CMDSTACKDEPTH]; /* used to streamline source */ + uint8_t para2[CMDSTACKDEPTH]; /* used to streamline source */ + uint8_t para3[CMDSTACKDEPTH]; /* used to streamline source */ + uint8_t para4[CMDSTACKDEPTH]; /* used to streamline source */ + uint8_t nrOfCmdsInQueue; + } Cmd0; - // hwapi.cpp - uint16_t hwapi_shutterTime; - char ticketTemplate[1024]; + struct { // lower priority + // Command Stack for commands with 4 parameters +#define CMD4STACKDEPTH 8 + uint16_t AsynchSend[CMD4STACKDEPTH]; + uint8_t para1[CMD4STACKDEPTH]; + uint8_t para2[CMD4STACKDEPTH]; + uint8_t para3[CMD4STACKDEPTH]; + uint8_t para4[CMD4STACKDEPTH]; + uint8_t nrOfCmdsInQueue; + } Cmd4; - // sendWRcmd.cpp - uint16_t nextAsynchsendCmd0[16]; - uint8_t nrOfCmdsInQueue; - uint16_t nextAsynchsendCmd4[8]; - uint8_t nextCmd4para1[8]; - uint8_t nextCmd4para2[8]; - uint8_t nextCmd4para3[8]; - uint8_t nextCmd4para4[8]; - uint8_t nrOfCmds4InQueue; - uint16_t nextAsynchsendCmd8[4]; - uint8_t nextCmd8para1[4]; - uint8_t nextCmd8para2[4]; - uint16_t nextCmd8para3[4]; - uint32_t nextCmd8para4[4]; - uint8_t nrOfCmds8InQueue; - uint8_t sendAsynchDataBuf[160]; // no stack, only ONE buffer - uint8_t sendAsyDatLen; - uint8_t Sdata_mdbSendBuffer[64]; - uint8_t Sdata_mdbSendLen; - uint8_t prnDataParameters[4]; - uint8_t prnDataBufferUser; - char Sdata_PRN_TEXT[20][64]; - uint8_t pPrnDataBuff; // points to next PRINTER_BLOCK - uint8_t nextFDwrCmd[16]; - uint8_t nextFDrdCmd[16]; - uint8_t nextFDblkNr[16]; + struct { +#define CMD8STACKDEPTH 4 + uint16_t AsynchSend[CMD8STACKDEPTH]; + uint8_t para1[CMD8STACKDEPTH]; + uint8_t para2[CMD8STACKDEPTH]; + uint16_t para3[CMD8STACKDEPTH]; + uint32_t para4[CMD8STACKDEPTH]; + uint8_t nrOfCmdsInQueue; + } Cmd8; - uint8_t nextFDpara1[16]; - uint8_t nextFDpara2[16]; - uint8_t nextFDpara3[16]; - uint8_t nextFDpara4[16]; - uint8_t p_nextFDcmdsInQueue; - uint8_t longFDwrCmd[16]; - uint8_t longFDrdCmd[16]; - uint8_t longFDblkNr[16]; - uint8_t longFDlength[16]; + struct { // short command, 4 data bytes +#define FDCMD_STACKDEPTH 16 + uint8_t wrCmd[FDCMD_STACKDEPTH]; + uint8_t rdCmd[FDCMD_STACKDEPTH]; + uint8_t blkNr[FDCMD_STACKDEPTH]; + uint8_t para1[FDCMD_STACKDEPTH]; + uint8_t para2[FDCMD_STACKDEPTH]; + uint8_t para3[FDCMD_STACKDEPTH]; + uint8_t para4[FDCMD_STACKDEPTH]; + uint8_t cmdsInQueue; + } FDShort; - uint8_t longFDpara[16][64]; - uint8_t p_longFDcmdsInQueue; + struct { // long command, 64 data bytes +#define FDLONG_STACKDEPTH 16 + uint8_t wrCmd[FDLONG_STACKDEPTH]; + uint8_t rdCmd[FDLONG_STACKDEPTH]; + uint8_t blkNr[FDLONG_STACKDEPTH]; + uint8_t length[FDLONG_STACKDEPTH]; + uint8_t para[FDLONG_STACKDEPTH][64]; + uint8_t cmdsInQueue; + } FDLong; - // storeInData.cpp - bool indat_savePrnPwr; - bool indat_saveMifPwr; - bool indat_MdbIsOn; -#endif + struct { + uint8_t DataParameters[4]; + uint8_t DataBufferUser; + uint8_t pDataBuff; // points to next PRINTER_BLOCK + } prn; - uint8_t ndbs; - uint8_t pari; - uint8_t nsb; - uint8_t br; + struct { + bool savePrnPwr; + bool saveMifPwr; + bool MdbIsOn; + } indat; + + uint8_t ndbs; + uint8_t pari; + uint8_t nsb; + uint8_t br; #define MAXNROF_GENSTR 16 char genStrings[MAXNROF_GENSTR][64]; @@ -170,10 +217,13 @@ struct SharedMemBuffer { #define NROFMIFSTATEBYTES 40 #define PRN_STATE_ARRAY_SIZE 20 #define PRN_STATE_FONT_SIZE 20 +#define MAXNROF_PRNBYTES 64 +#define MAXNROF_PRNBLOCKS 20 uint8_t MIF_STATE[NROFMIFSTATEBYTES]; uint8_t MIF_DATA[12][64]; uint8_t PRN_STATE[PRN_STATE_ARRAY_SIZE]; uint8_t PRN_FONTS[PRN_STATE_FONT_SIZE]; + char PRN_TEXT[MAXNROF_PRNBLOCKS][MAXNROF_PRNBYTES]; bool mdb_busRdy; bool mdb_V12on; bool mdb_V5on; @@ -193,6 +243,10 @@ struct SharedMemBuffer { uint8_t pProtResultOk; uint16_t receivedDataLength; uint8_t receivedDataBlock[64]; + uint8_t rawData[150]; + uint8_t LengthRawData; + uint8_t mdbSendBuffer[64]; + uint8_t mdbSendLen; } Sdata; uint8_t mif_cardType; diff --git a/src/controlBus.cpp b/src/controlBus.cpp index b017aaf..e10ece3 100644 --- a/src/controlBus.cpp +++ b/src/controlBus.cpp @@ -96,66 +96,64 @@ bool gpi_PeriodicSendTimeHasChanged() { // Status Display gui <--> serial // /////////////////////////////////////////////////////////////////////////////////// -// linke Spalte, über Connect Button -static QString txt4comStateLine; - +// linke Spalte, über Connect Button QString epi_getTxt4comStateLine(void) { // GUI: get Text for serial Comport-State Line - return txt4comStateLine; + return SharedMemBuffer::getDataConst()->txt4.comStateLine; } void gpi_setTxt4comStateLine(QString txtline) { // serial: write Text to be displayed in serial Comport-State line (like "connected") - txt4comStateLine.clear(); - if (txtline=="") - txt4comStateLine.clear(); - else - txt4comStateLine=txtline; + memset(SharedMemBuffer::getData()->txt4.comStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.comStateLine)); + memcpy(SharedMemBuffer::getData()->txt4.comStateLine, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.comStateLine)-1); } void epi_clrTxt4comStateLine() { - txt4comStateLine.clear(); + memset(SharedMemBuffer::getData()->txt4.comStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.comStateLine)); } // rechte Spalte, oberste Statuszeile // I) "Handshakes" (serial Control) flow.cpp // geht überhaupt was raus? kommt überhaupt was zurück? -static QString txt4HsStateLine; - QString epi_getTxt4HsStateLine(void) { - return txt4HsStateLine; + return SharedMemBuffer::getDataConst()->txt4.HsStateLine; } void gpi_setTxt4HsStateLine(QString txtline) { - txt4HsStateLine.clear(); - if (txtline=="") - txt4HsStateLine.clear(); - else - txt4HsStateLine=txtline; + // serial: write Text to be displayed in serial Comport-State line (like "connected") + memset(SharedMemBuffer::getData()->txt4.HsStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.HsStateLine)); + memcpy(SharedMemBuffer::getData()->txt4.HsStateLine, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.HsStateLine)-1); } void epi_clrTxt4HsStateLine() { - txt4HsStateLine.clear(); + memset(SharedMemBuffer::getData()->txt4.HsStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.HsStateLine)); } // II) Master receive state (empfangenes Telgramm OK? crc? length? ) // Statuszeile Auswertung der SlaveResponse (serial Frame, CRC usw) (prot.cpp) -static QString txt4masterStateLine; - QString epi_getTxt4masterStateLine(void) { - return txt4masterStateLine; + return SharedMemBuffer::getDataConst()->txt4.masterStateLine; } void gpi_setTxt4masterStateLine(QString txtline) { - txt4masterStateLine.clear(); - if (txtline=="") - txt4masterStateLine.clear(); - else - txt4masterStateLine=txtline; + memset(SharedMemBuffer::getData()->txt4.masterStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.masterStateLine)); + memcpy(SharedMemBuffer::getData()->txt4.masterStateLine, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.masterStateLine)-1); } void epi_clrTxt4masterStateLine() { - txt4masterStateLine.clear(); + memset(SharedMemBuffer::getData()->txt4.masterStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.masterStateLine)); } //--------------------------------------------------------------------------------------------- @@ -164,110 +162,96 @@ void epi_clrTxt4masterStateLine() { // entweder Empfangsfehler anzeigen (crc? length?) oder result OUT-OK, OUT_ERR, IN_OK, IN_ERR // Hintergrund: wenn der Slave Fehler im Master-Telegramm gefunden hat, dann kann er es auch // nicht verwenden und nichts ausgeben oder einlesen - -static QString txt4resultStateLine; - QString epi_getTxt4resultStateLine(void) { - return txt4resultStateLine; + return SharedMemBuffer::getDataConst()->txt4.resultStateLine; } void gpi_setTxt4resultStateLine(QString txtline) { - txt4resultStateLine.clear(); - if (txtline=="") - txt4resultStateLine.clear(); - else - txt4resultStateLine=txtline; + memset(SharedMemBuffer::getData()->txt4.resultStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.resultStateLine)); + memcpy(SharedMemBuffer::getData()->txt4.resultStateLine, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.resultStateLine)-1); } void epi_clrTxt4resultStateLine() { - txt4resultStateLine.clear(); + memset(SharedMemBuffer::getData()->txt4.resultStateLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.resultStateLine)); } //--------------------------------------------------------------------------------------------- - // IV Statuszeile Empfangsdaten -static QString txt4dataLine; - QString epi_getTxt4dataStateLine(void) { // GUI: get Text for serial Comport-State Line - return txt4dataLine; - + return SharedMemBuffer::getDataConst()->txt4.dataLine; } void gpi_setTxt4dataStateLine(QString txtline) { // serial: write Text to be displayed in serial Comport-State line (like "connected") - txt4dataLine.clear(); - if (txtline=="") - txt4dataLine.clear(); - else - txt4dataLine=txtline; + memset(SharedMemBuffer::getData()->txt4.dataLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.dataLine)); + memcpy(SharedMemBuffer::getData()->txt4.dataLine, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.dataLine)-1); } void epi_clrTxt4dataStateLine() { - txt4dataLine.clear(); + memset(SharedMemBuffer::getData()->txt4.dataLine, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.dataLine)); } //--------------------------------------------------------------------------------------------- // 5. Zeile: Datif Ergebnis, Daten brauchbar? - -static QString txt4datifReceive; - QString epi_getTxt4datifLine(void) { - return txt4datifReceive; + return SharedMemBuffer::getDataConst()->txt4.datifReceive; } void gpi_setTxt4datifLine(QString txtline) { - txt4datifReceive.clear(); - if (txtline=="") - txt4datifReceive.clear(); - else - txt4datifReceive=txtline; + memset(SharedMemBuffer::getData()->txt4.datifReceive, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.datifReceive)); + memcpy(SharedMemBuffer::getData()->txt4.datifReceive, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.datifReceive)-1); } void epi_clrTxt4datifLine() { - txt4datifReceive.clear(); + memset(SharedMemBuffer::getData()->txt4.datifReceive, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.datifReceive)); } -//--------------------------------------------------------------------------------------------- -//--------------------------------------------------------------------------------------------- - -static QString txt4diagWindow; - QString epi_getTxt4RsDiagWin(void) { - return txt4diagWindow; + return SharedMemBuffer::getDataConst()->txt4.diagWindow; } void gpi_setTxt4RsDiagWin(QString txtline) { - txt4diagWindow.clear(); - if (txtline=="") - txt4diagWindow.clear(); - else - txt4diagWindow=txtline; + memset(SharedMemBuffer::getData()->txt4.diagWindow, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.diagWindow)); + memcpy(SharedMemBuffer::getData()->txt4.diagWindow, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.diagWindow)-1); } void epi_clrTxt4RsDiagWin() { - txt4diagWindow.clear(); + memset(SharedMemBuffer::getData()->txt4.diagWindow, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.diagWindow)); } -//--------------------------------------------------------------------------------------------- - -static QString sndTxt4diagWindow; - QString epi_get2ndTxt4RsDiagWin(void) { - return sndTxt4diagWindow; + return SharedMemBuffer::getDataConst()->txt4.sndDiagWindow; } void gpi_set2ndTxt4RsDiagWin(QString txtline) { - sndTxt4diagWindow.clear(); - if (txtline=="") - sndTxt4diagWindow.clear(); - else - sndTxt4diagWindow=txtline; + memset(SharedMemBuffer::getData()->txt4.sndDiagWindow, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.sndDiagWindow)); + memcpy(SharedMemBuffer::getData()->txt4.sndDiagWindow, + txtline.toStdString().c_str(), + sizeof(SharedMemBuffer::getData()->txt4.sndDiagWindow)-1); } void epi_clr2ndTxt4RsDiagWin() { - sndTxt4diagWindow.clear(); + memset(SharedMemBuffer::getData()->txt4.sndDiagWindow, + 0x00, sizeof(SharedMemBuffer::getData()->txt4.sndDiagWindow)); } // /////////////////////////////////////////////////////////////////////////////////// diff --git a/src/datIf.cpp b/src/datIf.cpp index 60e76d3..17b8560 100644 --- a/src/datIf.cpp +++ b/src/datIf.cpp @@ -2,24 +2,12 @@ #include "sendWRcmd.h" #include "controlBus.h" #include "storeINdata.h" +#include "shared_mem_buffer.h" #include // called from MainWindow() - -#define DATIF_MAXCMDS 16 -static uint8_t dif_dataStep; -static uint8_t dif_scanStep, RDBLKNR; - - -static uint8_t datif_OutCmdpara1, datif_OutCmdpara2, datif_OutCmdpara3, datif_OutCmdpara4; -static uint16_t datif_OutCmdpara5; -static uint32_t datif_OutCmdpara6; -static uint8_t cycl_running; - -T_datif::T_datif(QWidget *parent) : QMainWindow(parent) -{ - +T_datif::T_datif(QWidget *parent) : QMainWindow(parent) { myDCIF = new T_prot(); // valid data was received, storing @@ -34,27 +22,22 @@ T_datif::T_datif(QWidget *parent) : QMainWindow(parent) datif_trigger->setSingleShot(false); datif_trigger->start(10); // in ms, 80 gut, default 100 50....200 - // passing Signal through - //connect(myDCIF, SIGNAL(framerecieved()), this, SLOT( ResponseRecieved() )); + SharedMemBuffer::getData()->datif.OutCmdpara1 = 0; + SharedMemBuffer::getData()->datif.OutCmdpara2 = 0; + SharedMemBuffer::getData()->datif.OutCmdpara3 = 0; + SharedMemBuffer::getData()->datif.OutCmdpara4 = 0; + SharedMemBuffer::getData()->datif.dataStep = 1; + SharedMemBuffer::getData()->datif.scanStep = 0; + SharedMemBuffer::getData()->datif.cycl_running = 0; - datif_OutCmdpara1=0; - datif_OutCmdpara2=0; - datif_OutCmdpara3=0; - datif_OutCmdpara4=0; - dif_dataStep=1; - - dif_scanStep=0; - selectedSlaveAddr=FIX_SLAVE_ADDR; - cycl_running=0; + selectedSlaveAddr = FIX_SLAVE_ADDR; } -void T_datif::resetChain(void) -{ - dif_scanStep=0; +void T_datif::resetChain(void) { + SharedMemBuffer::getData()->datif.scanStep = 0; } -char T_datif::datif_cycleSend() -{ +char T_datif::datif_cycleSend() { // cyclic transmission of INPUT-Requests // call cyclic to send next request every 100ms, then wait for response before sending again!!! uint16_t nxtAsCmd; @@ -65,18 +48,17 @@ char T_datif::datif_cycleSend() uint8_t length, data[66]; bool b_ret; - if (cycl_running) - { + if (SharedMemBuffer::getDataConst()->datif.cycl_running) { // request is still running, wait for response before next sending datif_trigger->stop(); datif_trigger->start(10); - cycl_running++; - if (cycl_running>10) - cycl_running=0; + SharedMemBuffer::getData()->datif.cycl_running += 1; + if (SharedMemBuffer::getDataConst()->datif.cycl_running > 10) { + SharedMemBuffer::getData()->datif.cycl_running = 0; + } return 0; } - if (myDCIF->isPortOpen()) - { + if (myDCIF->isPortOpen()) { #ifdef USEHANDSHAKES if (myDCIF->isSerialFree()) #endif @@ -95,11 +77,9 @@ char T_datif::datif_cycleSend() //void myDCIF->sendUserData(uint16_t slaveAdr); - if (check4FDshortCmd()) - { + if (check4FDshortCmd()) { b_ret=sendFDcmd_get(&nextWrCmd, &nextRdCmd, &blockNum, &dat1, &dat2, &dat3, &dat4); - if (b_ret) - { + if (b_ret) { data[0]=dat1; data[1]=dat2; data[2]=dat3; data[3]=dat4; data[4]=0; myDCIF->setUserWriteData(nextWrCmd, blockNum, 4, data); myDCIF->setUserReadData(nextRdCmd); @@ -107,8 +87,7 @@ char T_datif::datif_cycleSend() } } - if (check4FDlongCmd()) - { + if (check4FDlongCmd()) { b_ret=longFDcmd_get(&nextWrCmd, &nextRdCmd, &blockNum, &length, data); if (b_ret) { @@ -120,9 +99,8 @@ char T_datif::datif_cycleSend() // direct commands have highest prio (setting OUTPUTS) nxtAsCmd=sendWRcmd_getSendCommand0(); // command was stored by Gui - if (nxtAsCmd>0) - { - cycl_running=1; + if (nxtAsCmd>0) { + SharedMemBuffer::getData()->datif.cycl_running = 1; // qDebug() << "datif: send next cmd0"; sendWRcommand(nxtAsCmd); send_requests(nxtAsCmd); @@ -131,11 +109,14 @@ char T_datif::datif_cycleSend() return 0; } - nxtAsCmd=sendWRcmd_getSendCommand4(&datif_OutCmdpara1, &datif_OutCmdpara2, &datif_OutCmdpara3, &datif_OutCmdpara4); + nxtAsCmd=sendWRcmd_getSendCommand4( + &SharedMemBuffer::getData()->datif.OutCmdpara1, + &SharedMemBuffer::getData()->datif.OutCmdpara2, + &SharedMemBuffer::getData()->datif.OutCmdpara3, + &SharedMemBuffer::getData()->datif.OutCmdpara4); // command was stored by Gui - if (nxtAsCmd>0) - { - cycl_running=1; + if (nxtAsCmd>0) { + SharedMemBuffer::getData()->datif.cycl_running = 1; //qDebug() << "datif: send next cmd4"; sendWRcommand(nxtAsCmd); send_requests(nxtAsCmd); @@ -144,11 +125,14 @@ char T_datif::datif_cycleSend() return 0; } - nxtAsCmd=sendWRcmd_getSendCommand8(&datif_OutCmdpara1, &datif_OutCmdpara2, &datif_OutCmdpara5, &datif_OutCmdpara6); + nxtAsCmd=sendWRcmd_getSendCommand8( + &SharedMemBuffer::getData()->datif.OutCmdpara1, + &SharedMemBuffer::getData()->datif.OutCmdpara2, + &SharedMemBuffer::getData()->datif.OutCmdpara5, + &SharedMemBuffer::getData()->datif.OutCmdpara6); // command was stored by Gui - if (nxtAsCmd>0) - { - cycl_running=1; + if (nxtAsCmd>0) { + SharedMemBuffer::getData()->datif.cycl_running = 1; //qDebug() << "datif: send next cmd8"; sendWRcommand(nxtAsCmd); send_requests(nxtAsCmd); @@ -158,9 +142,8 @@ char T_datif::datif_cycleSend() } dbl=sendWRcmd_getSendBlock160(&dataBufLen, dataSendBuf); // used for bootloader - if (dbl>0) - { - cycl_running=1; + if (dbl>0) { + SharedMemBuffer::getData()->datif.cycl_running = 1; //qDebug() << "datif: sending 160 byte block, len: " << dataBufLen; datif_OUT_SendRandomData(dataSendBuf, dataBufLen); BlockCounter=0; @@ -168,9 +151,8 @@ char T_datif::datif_cycleSend() } dbl=gpi_chk4remainingText(); - if (dbl>0) - { - cycl_running=1; + if (dbl>0) { + SharedMemBuffer::getData()->datif.cycl_running = 1; //qDebug() << "datif: sending printer text "; gpi_restorePrnText(&dataSendBuf[0]); // can hold 1280 byte, get next 64 @@ -178,23 +160,24 @@ char T_datif::datif_cycleSend() // qDebug() << dataSendBuf[nn] << " "; - who=gpi_getUserOfSendingTextBuffer(&datif_OutCmdpara1, &datif_OutCmdpara2, &datif_OutCmdpara3, &datif_OutCmdpara4); + who=gpi_getUserOfSendingTextBuffer( + &SharedMemBuffer::getData()->datif.OutCmdpara1, + &SharedMemBuffer::getData()->datif.OutCmdpara2, + &SharedMemBuffer::getData()->datif.OutCmdpara3, + &SharedMemBuffer::getData()->datif.OutCmdpara4); - if (who==1) - { + if (who==1) { epi_store64ByteSendData(64, dataSendBuf); // "dueway", copy this 64 back to pi, used by datif_ send64byteOutCmd datif_send64byteOutCmd(CMD2DC_PRI_PRINT_TXT, 0, 0); - } else - if (who==2) - { + } else if (who==2) { epi_store64ByteSendData(64, dataSendBuf); // "dueway", copy this 64 back to pi, used by datif_ send64byteOutCmd datif_send64byteOutCmd(CMD2DC_STOR_QR_DATA, 0, 0); - } else - if (who==3) - { + } else if (who==3) { //qDebug() << "datif: sending printer text, docnr: " <datif.OutCmdpara1, + BlockCounter, dataSendBuf); // "docNr" in datif_OutCmdpara1 blockNr in dbl //for (uint8_t nn=0; nn<64; nn++) // qDebug() << dataSendBuf[nn] << " "; @@ -204,31 +187,27 @@ char T_datif::datif_cycleSend() } - if (gpi_isEmmisionOn()) // auto send button is pressed - { + if (gpi_isEmmisionOn()) { // auto send button is pressed //qDebug() << "auto request is on"; sendINrequestsAutomatic(); // sendCyclicCmd(); // request all cyclic data sequential + } else { + // always start from beginning + SharedMemBuffer::getData()->datif.scanStep = 0; } - else - dif_scanStep=0; // always start from beginning } - #ifdef USEHANDSHAKES +#ifdef USEHANDSHAKES else { //qDebug() << "last request not yet finished"; } - #endif - } else - { +#endif + } else { //qDebug() << "com port not available"; // wird ununterbrochen ausgegeben } return 0; } - - - // ############################################################################## // ############################################################################## // ############################################################################## @@ -241,38 +220,30 @@ void T_datif::sendWRcommand(uint16_t nextWrCmd) //uint8_t LL; //uint8_t restBuf66[66]; - switch (nextWrCmd) - { + switch (nextWrCmd) { // basically this is the junction between asynchronous cmd and synchron sending // case SENDDIRCMD_TestSerial: // datif_sendIOrequest(0, CMD2DC_TestSerial, 0); // break; - case SENDDIRCMD_setTime: datif_OUT_setTime(); // 0x1310 break; - case SENDDIRCMD_setWakeFrequ: datif_sendIOrequest(CMD2DC_setWakeFrequ, 0,0); break; - case SENDDIRCMD_MOVEUP_LOCK: datif_sendIOrequest(CMD2DC_MOV_UPLOCK, 0, 1); break; - case SENDDIRCMD_MOVEDN_LOCK: datif_sendIOrequest(CMD2DC_MOV_DNLOCK, 0, 1); break; - case SENDDIRCMD_OPENUP_DOOR: // Riegelleisten auf/ab bis ES datif_sendIOrequest(CMD2DC_UPPER_DOOR, 0, 1); break; - case SENDDIRCMD_OPENDN_DOOR: datif_sendIOrequest(CMD2DC_LOWER_DOOR, 0, 1); break; - case SENDDIRCMD_LEDILLU: datif_sendIOrequest(CMD2DC_LED_ILLU, CMD2DC_RdBkAllOutputs, 1); // WRITE cmd Read Request @@ -280,28 +251,22 @@ void T_datif::sendWRcommand(uint16_t nextWrCmd) // but always an advantage (data are quicker actual) // the request does not influence the WRiting, it's independant break; - case SENDDIRCMD_LEDCOIN: datif_sendIOrequest(CMD2DC_LED_COIN, CMD2DC_RdBkAllOutputs, 3); // ^ nr of params break; - case SENDDIRCMD_LEDTICKET: datif_sendIOrequest(CMD2DC_LED_TICKET, CMD2DC_RdBkAllOutputs, 3); break; - case SENDDIRCMD_LEDPAD: datif_sendIOrequest(CMD2DC_LED_PIN, CMD2DC_RdBkAllOutputs, 3); break; - case SENDDIRCMD_LEDSTART: datif_sendIOrequest(CMD2DC_LED_START, CMD2DC_RdBkAllOutputs, 3); break; - case SENDDIRCMD_LEDINSIDE: datif_sendIOrequest(CMD2DC_LED_IN, CMD2DC_RdBkAllOutputs, 1); break; - case SENDDIRCMD_FAN: datif_sendIOrequest(CMD2DC_FAN, CMD2DC_RdBkAllOutputs, 1); break; @@ -311,166 +276,125 @@ void T_datif::sendWRcommand(uint16_t nextWrCmd) case SENDDIRCMD_REL1: datif_sendIOrequest(CMD2DC_BARRIER, CMD2DC_RdBkAllOutputs, 1); break; - case SENDDIRCMD_WAKEPTU: datif_sendIOrequest(CMD2DC_WAKEPTU, 0, 1); break; - case SENDDIRCMD_AUXPWR: datif_sendIOrequest(CMD2DC_SWITCHAUXPWR, CMD2DC_RdBkAllOutputs, 1); break; - case SENDDIRCMD_AUXDDR: datif_sendIOrequest(CMD2DC_SWITCHAUXDDR, CMD2DC_RdBkAllOutputs, 1); break; - case SENDDIRCMD_AUXOUT: datif_sendIOrequest(CMD2DC_SWITCHAUXOUT, CMD2DC_RdBkAllOutputs, 1); break; - case SENDDIRCMD_PRN2_SWONOFF: datif_sendIOrequest(CMD2DC_PRINTERON, 0, 1); break; - case SENDDIRCMD_UCONTACT_ON: datif_sendIOrequest(CMD2DC_UCONTACTON, 0, 1); break; - case SENDDIRCMD_MIF_SWONOFF: datif_sendIOrequest(CMD2DC_MIFREADERON, 0, 1); // 0x2900 - indat_storeMifarePower(datif_OutCmdpara1); + indat_storeMifarePower(SharedMemBuffer::getDataConst()->datif.OutCmdpara1); //qDebug()<<"switching mifare on/off "<< datif_OutCmdpara1; - break; - + break; case SENDDIRCMD_MOD_SWONOFF: datif_sendIOrequest(CMD2DC_MOD_ON, 0, 1); break; case SENDDIRCMD_MOD_WAKE: datif_sendIOrequest(CMD2DC_MOD_WAK, 0, 1); break; - - case SENDDIRCMD_MDB_POWER: //qDebug()<<"switch MDB pwr" << datif_OutCmdpara1; datif_sendIOrequest(CMD2DC_MDB_ON, 0, 1); indat_storeMDBisOn(CMD2DC_MDB_ON); break; - case SENDDIRCMD_MDB_RES: datif_sendIOrequest(CMD2DC_MDB_DORESET, 0, 1); break; - case SENDDIRCMD_MDB_WAKE: datif_sendIOrequest(CMD2DC_MDB_SETWAK, 0, 1); break; - case SENDDIRCMD_CRED_ON: datif_sendIOrequest(CMD2DC_CRED_ON, 0, 1); break; case SENDDIRCMD_CRED_WAKE: datif_sendIOrequest(CMD2DC_CRED_WAK, 0, 1); break; - case SENDDIRCMD_SHUT_MOV: datif_sendIOrequest(CMD2DC_SHUTTER_OPEN, 0, 1); break; - case SENDDIRCMD_ESCRO_MOV: datif_sendIOrequest(CMD2DC_ESCR_OPEN, 0, 1); break; - case SENDDIR_OPENVAULT: datif_sendIOrequest(CMD2DC_VAULT_DOOR, 0, 0); //qDebug() << "Datif: send cmd open vault"; break; - case SENDDIR_REJMOT_ON: datif_sendIOrequest(CMD2DC_REJMOT_ON, 0, 1); //qDebug() << "Datif: send cmd reject motor"; break; - case SENDDIR_REJMOT_RUN: datif_sendIOrequest(CMD2DC_REJMOT_RUN, 0, 0); //qDebug() << "Datif: send cmd do coin reject"; break; - - } - } - -void T_datif::send_requests(uint16_t nextWrCmd) -{ - switch (nextWrCmd) - { - +void T_datif::send_requests(uint16_t nextWrCmd) { + switch (nextWrCmd) { case SENDDIRCMD_TestSerial: datif_sendIOrequest(0, CMD2DC_TestSerial, 0); // ^ under no circumstances give a write cmd with the request!!!! break; - case SEND_REQU_SERCONF: datif_sendIOrequest(0, CMD2DC_GetSerialConfig, 0); break; - case SEND_REQU_HWversion: datif_sendIOrequest(0, CMD2DC_RdBkHWversion, 0); break; - case SEND_REQU_SWversion: datif_sendIOrequest(0, CMD2DC_RdBkSWversion, 0); break; - case SEND_REQU_CONDITION : datif_sendIOrequest(0, CMD2DC_RdBkDCstate, 0); break; - case SEND_REQU_UID : datif_sendIOrequest(0, CMD2DC_RdBkUID, 0); break; - case SEND_REQU_TIME : datif_sendIOrequest(0, CMD2DC_RdBkTime, 0); break; - // includes wake frequency case SEND_REQU_ANALOGS : datif_sendIOrequest(0, CMD2DC_RdBkAnalog, 0); break; - case SEND_REQU_DIG_INPUTS : datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); break; - case SEND_REQU_DIG_OUTPUTS: datif_sendIOrequest(0, CMD2DC_RdBkAllOutputs, 0); break; - case SEND_REQU_PRN_STATE: datif_sendIOrequest(0, CMD2DC_RdBk_PrnState, 0); break; - case SEND_REQU_PRN_FONTS : datif_sendIOrequest(0, CMD2DC_RdBk_PrnFonts, 0); break; - case SEND_REQU_PRN_ALL : datif_sendIOrequest(0, CMD2DC_RdBk_AllPrnData, 0); break; - - case SEND_REQU_MIFSTATE : // Type and state of reader datif_sendIOrequest(0, CMD2DC_RdBk_MifState, 0); break; - case SEND_REQU_MIFDATA : // Type, UID, Header of card datif_sendIOrequest(0, CMD2DC_RdBk_MifData, 1); // immer nur blk 0 anfordern //qDebug()<<"manual requesting mif card data"; break; - // case SEND_REQU_MIF_ATB_TYPE : // // read one card sector // // sectors must be addressed by RD_ADD @@ -483,186 +407,127 @@ void T_datif::send_requests(uint16_t nextWrCmd) // //datif_sendIOrequest(0, CMD2DC_RdBk_CardData, 1); ist das gleiche // datif_sendIOrequest(0, CMD2DC_RdBk_MifData, 1); // jeden beliebigen Block anfordern // break; - - case SEND_REQU_MDB_GETSTAT : - datif_sendIOrequest(0, CMD2DC_MDB_GET_STATE, 0); - break; - - //case SEND_REQU_MDB_GETWAK : hamma eh schoo - // datif_sendIOrequest(0, CMD2DC_MDB_GETWAK, 0); - // break; - - case SEND_REQU_MDB_GETRESP : - datif_sendIOrequest(0, CMD2DC_MDB_GETRESP, 0); - break; - - case SEND_REQU_EMP_GETALL : - datif_sendIOrequest(0, CMD2DC_EMP_GET_ALL, 0); - break; - - case SEND_REQU_EMP_GETCOIN : - datif_sendIOrequest(0, CMD2DC_EMP_GOTCOIN, 0); - break; - - case SEND_REQU_DEVICE_PARA : - datif_sendIOrequest(0, CMD2DC_RDBK_DEV_PARA, 0); - break; - - case SEND_REQU_MACINE_ID : - datif_sendIOrequest(0, CMD2DC_RDBK_MACH_ID, 0); - break; - - + case SEND_REQU_MDB_GETSTAT : + datif_sendIOrequest(0, CMD2DC_MDB_GET_STATE, 0); + break; + //case SEND_REQU_MDB_GETWAK : hamma eh schoo + // datif_sendIOrequest(0, CMD2DC_MDB_GETWAK, 0); + // break; + case SEND_REQU_MDB_GETRESP : + datif_sendIOrequest(0, CMD2DC_MDB_GETRESP, 0); + break; + case SEND_REQU_EMP_GETALL : + datif_sendIOrequest(0, CMD2DC_EMP_GET_ALL, 0); + break; + case SEND_REQU_EMP_GETCOIN : + datif_sendIOrequest(0, CMD2DC_EMP_GOTCOIN, 0); + break; + case SEND_REQU_DEVICE_PARA : + datif_sendIOrequest(0, CMD2DC_RDBK_DEV_PARA, 0); + break; + case SEND_REQU_MACINE_ID : + datif_sendIOrequest(0, CMD2DC_RDBK_MACH_ID, 0); + break; } - } - -void T_datif::sendHighLevel(uint16_t nxtHLCmd) -{ - switch (nxtHLCmd) - { - +void T_datif::sendHighLevel(uint16_t nxtHLCmd) { + switch (nxtHLCmd) { case SENDDIRCMD_SHUTOPENBYTIME: //qDebug() << "open by time " << datif_OutCmdpara1 << " "<< datif_OutCmdpara2; datif_sendIOrequest(CMD2DC_SHUTTER_OPEN3S, 0, 1); break; - case SENDDIRCMD_SHUTOPENBYCOIN: //qDebug() << "open by coin " << datif_OutCmdpara1 << " "<< datif_OutCmdpara2; datif_sendIOrequest(CMD2DC_SHUTTER_COIN, 0, 2); break; - case SENDDIRCMD_ESCRO_TAKE: datif_sendIOrequest(CMD2DC_ESCR_TAKE, 0, 0); break; - case SENDDIRCMD_ESCRO_GIVE: datif_sendIOrequest(CMD2DC_ESCR_RETURN, 0, 0); break; - case SENDDIRCMD_MIF_ATBCREATE: datif_sendIOrequest(CMD2DC_ATB_CREATE, 0, 1); // 0x2907 break; - - - -// yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy - - case SENDDIRCMD_MDB_SENDCMD: datif_sendIOrequest(CMD2DC_MDB_SENDCMD, 0, 1); break; - case SENDDIRCMD_MDB_SNDMSG: datif_sendIOrequest(CMD2DC_MDB_SENDMSG, 0, 1); break; - case SENDDIRCMD_EMP_SETT: //datif_sendIOrequest(CMD2DC_EMP_SET, 0, 1); datif_send64byteOutCmd(CMD2DC_EMP_SET,0,0); break; - case SENDDIRCMD_EMP_POLL: datif_sendIOrequest(CMD2DC_EMP_STARTPOLL, 0, 1); break; - case SENDDIRCMD_EMP_STARPPAY: datif_sendIOrequest(CMD2DC_EMP_STARTPAY, 0, 1); break; - case SENDDIRCMD_EMP_STOPPAY: datif_sendIOrequest(CMD2DC_EMP_STOPPAY, 0, 1); break; - -// yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy - - - - - case SENDDIRCMD_PRN_SYS_CMD: datif_send8byteOutCmd(CMD2DC_PRI_SYS_CMD, 0); break; - case SENDDIRCMD_PRN_ESC_CMD: datif_sendIOrequest(CMD2DC_PRI_ESC_CMD, 0, 4); break; - case SENDDIRCMD_PRN_SETUP: // byte 0,1: speed byte2: density byte3: alignment byte4: orientation datif_send64byteOutCmd(CMD2DC_PRI_SETUP, 0, 0); break; - case SENDDIRCMD_PRN_MOVE: datif_sendIOrequest(CMD2DC_PRI_MOVE, 0, 2); break; - case SENDDIRCMD_PRN_SETFONT: datif_sendIOrequest(CMD2DC_PRI_SETFONT, 0, 4); break; - case SENDDIRCMD_PRN_SETLETT: datif_sendIOrequest(CMD2DC_PRI_SETLETTER, 0, 3); break; - case SENDDIRCMD_PRN_CUT: datif_sendIOrequest(CMD2DC_PRI_CUT, 0, 1); break; - case SENDDIRCMD_PRN_LF: datif_sendIOrequest(CMD2DC_PRI_LF, 0, 1); break; - case SENDDIRCMD_PRN_FONTTAB: datif_sendIOrequest(CMD2DC_PRI_PRIFONTTABLE, 0, 0); break; - case SENDDIRCMD_PRN_BC: datif_send64byteOutCmd(CMD2DC_PRI_BARCODE, 0, 0); break; - case SENDDIRCMD_PRN_LOGO_FL: datif_sendIOrequest(CMD2DC_PRI_LOGOFROMFLASH, 0, 1); break; - case SENDDIRCMD_PRN_DOC: - datif_send64byteOutCmd(CMD2DC_PRI_DOCUMENT_NR, datif_OutCmdpara1, 0); + datif_send64byteOutCmd(CMD2DC_PRI_DOCUMENT_NR, + SharedMemBuffer::getData()->datif.OutCmdpara1, 0); break; - case SENDDIRCMD_PRN_QR: datif_sendIOrequest(CMD2DC_PRI_QR_CODE, 0, 0); break; - - case SENDDIRCMD_PRN_CLEARDOC: datif_sendIOrequest(CMD2DC_PRI_CLEAR_DOC, 0, 1); break; - - case SENDDIRCMD_DEVICE_PARA: datif_send64byteOutCmd(CMD2DC_DEVICE_PARAM, 0, 0); break; - - case SENDDIRCMD_MACHINE_ID: datif_send64byteOutCmd(CMD2DC_SEND_MACH_ID, 0, 0); break; - } - } - - // ############################################################################## // ############################################################################## // ############################################################################## - // sende alle Befehle um die Eingangsdaten abzufragen der Reihe nach: -char T_datif::sendINrequestsAutomatic(void) -{ +char T_datif::sendINrequestsAutomatic(void) { //qDebug() << "send IN request " << dif_scanStep; // one time: 105, 11,12,101,18 @@ -671,123 +536,100 @@ char T_datif::sendINrequestsAutomatic(void) // fehlen: 112, 113,14,17, 19, 30,31,32,33, // 38 on request - switch (dif_scanStep) - { + switch (SharedMemBuffer::getDataConst()->datif.scanStep) { // first 5 items are performed only one time after startup as this data never change - case 0: datif_sendIOrequest(0, CMD2DC_GetSerialConfig, 0); // 105 break; - case 1: datif_sendIOrequest(0, CMD2DC_RdBkHWversion, 0); // 11 break; - case 2: datif_sendIOrequest(0, CMD2DC_RdBkSWversion, 0); // 12 break; - case 3: datif_sendIOrequest(0, CMD2DC_RdBkDCstate, 0); // 101 break; - case 4: datif_sendIOrequest(0, CMD2DC_RdBkUID, 0); // 18 break; - // repeat cyclic from here: case 5: datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); // 102 break; - case 6: datif_sendIOrequest(0, CMD2DC_RdBkTime, 0); // 104 break; - case 7: //datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); datif_sendIOrequest(0, 112, 0); break; - case 8: datif_sendIOrequest(0, CMD2DC_RdBkAnalog, 0); // 106 break; - case 9: //datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); datif_sendIOrequest(0, 113, 0); break; - case 10: datif_sendIOrequest(0, CMD2DC_RdBkAllOutputs, 0); // 103 break; - case 11: //datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); datif_sendIOrequest(0, 14, 0); break; - case 12: if (indat_isPrinterOn()) datif_sendIOrequest(0, CMD2DC_RdBk_AllPrnData, 0); // 27 // datif_sendIOrequest(0, CMD2DC_RdBk_PrnState, 0); break; - case 13: datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); break; - case 14: - if (indat_isMifareOn()) - { + if (indat_isMifareOn()) { datif_sendIOrequest(0, CMD2DC_RdBk_MifState, 0); // 109 //qDebug()<<"requesting MIF reader state"; } break; - case 15: //datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); datif_sendIOrequest(0, 17, 0); break; - case 16: - if (indat_isMifareOn()) - { - datif_OutCmdpara1=0; // data block number 0 contains the Mifare-ID + if (indat_isMifareOn()) { + // data block number 0 contains the Mifare-ID + SharedMemBuffer::getData()->datif.OutCmdpara1 = 0; datif_sendIOrequest(0, CMD2DC_RdBk_MifData, 1); // 24 // qDebug()<<"automatically requesting mif card data"; - if (++RDBLKNR>11) - RDBLKNR=0; - datif_OutCmdpara1=RDBLKNR; + if (++SharedMemBuffer::getData()->datif.RDBLKNR > 11) { + SharedMemBuffer::getData()->datif.RDBLKNR = 0; + } + SharedMemBuffer::getData()->datif.OutCmdpara1 = + SharedMemBuffer::getDataConst()->datif.RDBLKNR; } break; - case 17: //datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); datif_sendIOrequest(0, 19, 0); break; - case 18: if (indat_isMdbOn()) datif_sendIOrequest(0, CMD2DC_MDB_GET_STATE, 0); // 107 //else // dif_scanStep=24; unsinn break; - case 19: datif_sendIOrequest(0, 30, 0); break; - case 20: //if (indat_isMdbOn()) datif_sendIOrequest(0, CMD2DC_MDB_GETRESP, 0); // 22 break; - case 21: datif_sendIOrequest(0, CMD2DC_GetAllInputs, 0); break; - case 22: //if (indat_isMdbOn()) // kein eigener Schalter für EMP datif_sendIOrequest(0, CMD2DC_EMP_GET_ALL, 0); // 23 @@ -796,34 +638,28 @@ char T_datif::sendINrequestsAutomatic(void) case 23: datif_sendIOrequest(0, 31, 0); break; - case 24: //if (indat_isMdbOn()) // kein eigener Schalter für EMP datif_sendIOrequest(0, CMD2DC_EMP_GOTCOIN, 0); // 108 break; - case 25: datif_sendIOrequest(0, 32, 0); break; - case 26: datif_sendIOrequest(0, 33, 0); break; - - } - dif_scanStep++; - if (dif_scanStep>26) - dif_scanStep=5; - -return 0; + SharedMemBuffer::getData()->datif.scanStep += 1; + if (SharedMemBuffer::getDataConst()->datif.scanStep > 26) { + SharedMemBuffer::getData()->datif.scanStep = 5; + } + return 0; } -char T_datif::isPortOpen(void) -{ +char T_datif::isPortOpen(void) { return (myDCIF->isPortOpen()); } @@ -834,10 +670,7 @@ char T_datif::isPortOpen(void) // Empfangsdaten einsortieren // ---------------------------------------------------------------------------------------------- - - -void T_datif::StoredRecData() -{ +void T_datif::StoredRecData() { //qDebug() << "StoreRecData called"; // call automatically by T_prot //if (myDCIF->ifDataReceived()) @@ -847,11 +680,10 @@ void T_datif::StoredRecData() // "neu" is the same as "INdataValid" loadRecDataFromFrame(); } - cycl_running=0; + SharedMemBuffer::getData()->datif.cycl_running = 0; } -char T_datif::loadRecDataFromFrame() -{ +char T_datif::loadRecDataFromFrame() { // necessary data in T_prot: uint16_t readSource, uitmp,uit2; uint16_t readAddress; @@ -872,8 +704,7 @@ char T_datif::loadRecDataFromFrame() ret=myDCIF->getReceivedInData(&SlaveAdr, &readSource, &readAddress, &RdDleng, receivedData); // retval: data valid, only one time true, true if CommandState OK and readState OK gpi_storeResultOfLastRequest(ret); - if (ret==false) - { + if (ret==false) { // qDebug() << "datif: rec data not valid"; return 0; } @@ -905,15 +736,13 @@ char T_datif::loadRecDataFromFrame() // receivedData[3]='-'; - for (int ii=0; ii0: error // bit0: paper low 1: no paper 2: temperature error // 3: head open 4: paper jam in cutter // 6: no response 7: bad response from printer - gpi_storePrinterState(receivedData); // derzeit 10bytes ( 0x2A02) break; - case CMD2DC_RdBk_PrnFonts: // 26 //D0: font table/type //D1: size @@ -1162,7 +971,6 @@ char T_datif::loadRecDataFromFrame() //D9: Alignment gpi_storePrinterFonts(receivedData); // derzeit 10bytes break; - case CMD2DC_RdBk_AllPrnData: // 27 gpi_storePrinterState(receivedData); // derzeit 10bytes ( 0x2A02) gpi_storePrinterFonts(&receivedData[10]); // derzeit 10bytes @@ -1175,7 +983,6 @@ char T_datif::loadRecDataFromFrame() <sendUserData(uint16_t slaveAdr); -void T_datif::datif_startSending(void) -{ +void T_datif::datif_startSending(void) { // egal ob WR und RD Daten gesetzt wurden myDCIF->sendUserData(selectedSlaveAddr); // starte Sendung gpi_startNewRequest(); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } // allgemeine Schreib/Lese-Funktion -void T_datif::datif_sendIOrequest(uint16_t WRcmd, uint16_t RDcmd, uint8_t nrOfWrData) -{ +void T_datif::datif_sendIOrequest(uint16_t WRcmd, uint16_t RDcmd, uint8_t nrOfWrData) { uint8_t data[6]; if (nrOfWrData>4) nrOfWrData=0; tslib_strclr(data,0,6); - if (nrOfWrData>0) - data[0]=datif_OutCmdpara1; - if (nrOfWrData>1) - data[1]=datif_OutCmdpara2; - if (nrOfWrData>2) - data[2]=datif_OutCmdpara3; - if (nrOfWrData>3) - data[3]=datif_OutCmdpara4; - data[4]=0; data[5]=0; + if (nrOfWrData>0) { + data[0] = SharedMemBuffer::getDataConst()->datif.OutCmdpara1; + } + if (nrOfWrData>1) { + data[1] = SharedMemBuffer::getDataConst()->datif.OutCmdpara2; + } + if (nrOfWrData>2) { + data[2] = SharedMemBuffer::getDataConst()->datif.OutCmdpara3; + } + if (nrOfWrData>3) { + data[3] = SharedMemBuffer::getDataConst()->datif.OutCmdpara4; + } + data[4] = data[5] = 0; myDCIF->setUserWriteData(WRcmd, 0, nrOfWrData, data); myDCIF->setUserReadData(RDcmd); myDCIF->sendUserData(selectedSlaveAddr); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } -void T_datif::datif_send8byteOutCmd(uint16_t WRcmd, uint16_t RDcmd) -{ +void T_datif::datif_send8byteOutCmd(uint16_t WRcmd, uint16_t RDcmd) { uint8_t data[10]; uint16_t uitmp; uint32_t ultmp; - tslib_strclr(data,0,10); // 8 used + memset(data, 0x00, sizeof(data)); // 8 used - data[0]=datif_OutCmdpara1; - data[1]=datif_OutCmdpara2; - uitmp=datif_OutCmdpara5; - ultmp=datif_OutCmdpara6; + data[0] = SharedMemBuffer::getDataConst()->datif.OutCmdpara1; + data[1] = SharedMemBuffer::getDataConst()->datif.OutCmdpara2; + uitmp = SharedMemBuffer::getDataConst()->datif.OutCmdpara5; + ultmp = SharedMemBuffer::getDataConst()->datif.OutCmdpara6; data[2]=uint8_t(uitmp); uitmp>>=8; @@ -1667,105 +1430,64 @@ void T_datif::datif_send8byteOutCmd(uint16_t WRcmd, uint16_t RDcmd) myDCIF->setUserWriteData(WRcmd, 0, 8, data); myDCIF->setUserReadData(RDcmd); myDCIF->sendUserData(selectedSlaveAddr); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } - -bool T_datif::verifyLineTestresponse(uint8_t RdDlen, uint8_t *receivedData) -{ - if (RdDlen < 16) +bool T_datif::verifyLineTestresponse(uint8_t RdDlen, uint8_t *receivedData) { + if (RdDlen < 16) { return false; - QString myStr; - char ctmp; - - myStr.clear(); - myStr.append("< Slave Response"); - - for (int nn=0; nn<16; nn++) - { - ctmp=receivedData[nn]; - if (myStr[nn] != ctmp) - { - //qDebug() << " datif cmd 10: got wrong string "; - //qDebug() << myStr; - //qDebug() << receivedData; - //qDebug() << nn; - return false; - } } - return true; + QString myStr("< Slave Response"); + QString recStr((char const *)receivedData); + return (myStr == recStr); } - // RTC ---------------------------------------------------------------------- -void T_datif::datif_OUT_setTime(void) -{ +void T_datif::datif_OUT_setTime(void) { // send PC time/date to slave //uint8_t hour,min, sec, year, month, day, dayOfWeek, //uint8_t dayOfYear, isLeap, weekOfYear; uint8_t buff[15]; uint16_t uitmp; - QTime *systTime = new QTime(); - // qDebug() << systTime->currentTime().hour() <<":" - // << systTime->currentTime().minute() <<":" - // << systTime->currentTime().second(); + QTime const cT = QTime::currentTime(); - buff[0]=uint8_t(systTime->currentTime().hour()); - buff[1]=uint8_t(systTime->currentTime().minute()); - buff[2]=uint8_t(systTime->currentTime().second()); + buff[0] = uint8_t(cT.hour()); + buff[1] = uint8_t(cT.minute()); + buff[2] = uint8_t(cT.second()); + QDate const cD = QDate::currentDate(); - QDate *systDate = new QDate(); - systDate->currentDate(); + uitmp = uint16_t(cD.year()); + buff[3] = uint8_t(uitmp); + buff[4] = uint8_t(uitmp>>8); - uitmp= uint16_t(systDate->currentDate().year()); - buff[3]=uint8_t(uitmp); - buff[4]=uint8_t(uitmp>>8); - - buff[5]=uint8_t(systDate->currentDate().month()); - buff[6]=uint8_t(systDate->currentDate().day()); - buff[7]=uint8_t(systDate->currentDate().dayOfWeek()); - -// uitmp=systDate->currentDate().dayOfYear(); -// buff[8]=uint8_t(uitmp); -// buff[9]=uint8_t(uitmp>>8); - -// buff[10]=uint8_t(systDate->currentDate().isLeapYear(systDate->currentDate().year())); -// buff[11]=uint8_t(systDate->currentDate().weekNumber()); //weekOfYear -// buff[12]=0; + buff[5] = uint8_t(cD.month()); + buff[6] = uint8_t(cD.day()); + buff[7] = uint8_t(cD.dayOfWeek()); //myDCIF->setUserWriteData(0x1310,0,8, buff); myDCIF->setUserWriteData(CMD2DC_sendTime,0,8, buff); myDCIF->setUserReadData(0); myDCIF->sendUserData(selectedSlaveAddr); // jetzt wegsckicken - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } // 0x2311: set time to RTC // 0x2312: set date to RTC -uint8_t T_datif::datif_OUT_SendRandomData(uint8_t *buf, uint8_t Length) -{ - uint8_t len=Length; +uint8_t T_datif::datif_OUT_SendRandomData(uint8_t *buf, uint8_t Length) { + uint8_t len = Length; - myDCIF->setBLsendData(len, buf ); + myDCIF->setBLsendData(len, buf); myDCIF->setUserReadData(0); myDCIF->sendUserData(selectedSlaveAddr); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; return 0; - } - - - -void T_datif::datif_send64byteOutCmd(uint16_t WRcmd, uint16_t addr, uint16_t RDcmd) -{ +void T_datif::datif_send64byteOutCmd(uint16_t WRcmd, uint16_t addr, uint16_t RDcmd) { // sending length is already defined by stored data // not batched! don't use twice within 100ms uint8_t LL; @@ -1776,28 +1498,20 @@ void T_datif::datif_send64byteOutCmd(uint16_t WRcmd, uint16_t addr, uint16_t RDc myDCIF->setUserWriteData(WRcmd, addr, LL, data); myDCIF->setUserReadData(RDcmd); myDCIF->sendUserData(selectedSlaveAddr); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } -void T_datif::datif_sendToMemory(uint16_t WRcmd, uint16_t docNr, uint16_t blockNr, uint8_t *data64) -{ +void T_datif::datif_sendToMemory(uint16_t WRcmd, uint16_t docNr, uint16_t blockNr, uint8_t *data64) { // send printer documents to DC2 memory // docNr: 0...15(31) with 1280 byte each (20 blocks a 64byte) // blockNr=0...19 with 64byte each // docNr =transmitted in WRITEADDRESS high byte // blockNr=transmitted in WRITEADDRESS low byte - - uint16_t aa=0; - - aa=docNr; - aa<<=8; - aa |=blockNr; + uint16_t const aa = (docNr << 8) | blockNr; myDCIF->setUserWriteData(WRcmd, aa, 64, data64); myDCIF->setUserReadData(0); myDCIF->sendUserData(selectedSlaveAddr); - cycl_running=1; - + SharedMemBuffer::getData()->datif.cycl_running = 1; } diff --git a/src/dcBL.cpp b/src/dcBL.cpp index 0d114f7..219b6d3 100644 --- a/src/dcBL.cpp +++ b/src/dcBL.cpp @@ -1,16 +1,17 @@ //#include #include "dcBL.h" #include +#include +#include + //#include "tabFw.h" #include "sendWRcmd.h" +#include "shared_mem_buffer.h" // File sits "behind" the HWapi and in front of "PI", so all necessary Bootloader functions can be called from HWapi - - -uint16_t ucharTOuint(uint8_t Highbyte, uint8_t Lowbyte) -{ +uint16_t ucharTOuint(uint8_t Highbyte, uint8_t Lowbyte) { uint16_t uitmp; uitmp=0; uitmp |= uint8_t(Highbyte); @@ -112,11 +113,8 @@ uint16_t dcBL_calcCrcCcitt(uint32_t BufLength, uint8_t *buf) #define SEND_ETX 3 #define SEND_ESC 0x1B -static uint8_t dcBL_LastBLcmd; // stored the last sent cmd in order to analys response - // cmd echo'ed: error cmd or'ed with 0x80: OK - -uint8_t dcBL_prepareDC_BLcmd(uint8_t Cmd, uint8_t SendDataLength, uint8_t *sendData, uint8_t *outBuf) -{ +// cmd echo'ed: error cmd or'ed with 0x80: OK +uint8_t dcBL_prepareDC_BLcmd(uint8_t Cmd, uint8_t SendDataLength, uint8_t *sendData, uint8_t *outBuf) { // make BL protocol, retval = outbuf length (5...133) // bring data in correct form: start always with 0x02 finish with 0x03 and append checksum // 0x02 Cmd < ...sendData ..> CRC CRC 0x03 @@ -134,14 +132,13 @@ uint8_t dcBL_prepareDC_BLcmd(uint8_t Cmd, uint8_t SendDataLength, uint8_t *sendD strclr(myBuf, 0, 140); strclr(crcBuf, 0, 140); // extra Puffer because STX must not be caculated - myBuf[pp++]=SEND_STX; - myBuf[pp++]=Cmd; - crcBuf[mm++]=Cmd; - dcBL_LastBLcmd=Cmd; + myBuf[pp++] = SEND_STX; + myBuf[pp++] = Cmd; + crcBuf[mm++] = Cmd; + SharedMemBuffer::getData()->dcBL.LastBLcmd = Cmd; // append data: - for (nn=0; nndcBL.LastBLcmd | 0x80)) { return 10; // OK + } // 27.01.21: Problem aufgetaucht: Fehler nur bei CRC melden // -> block wiederholen. In den anderen Fällen NICHT wiederholen @@ -392,8 +392,7 @@ qDebug()<<" DCBL_chkResp wrong start "; qDebug()<<" DCBL_chkResp wrong crc"; return 1; // error } - if (buf[0]==2 && buf[1]==dcBL_LastBLcmd ) - { + if (buf[0]==2 && buf[1]==SharedMemBuffer::getDataConst()->dcBL.LastBLcmd) { dcBL_writeText("error wrong cmd"); //exactError=4; // wrong cmd qDebug()<<" DCBL_chkResp wrong cmd"; @@ -405,13 +404,11 @@ qDebug()<<" DCBL_chkResp wrong cmd"; return 1; // error } - - -uint8_t dcBL_sendSuccess(uint8_t lastCommand) -{ +uint8_t dcBL_sendSuccess(uint8_t lastCommand) { // return val: 0: no response by now 1:error 10: OK - uint8_t buf[152], recLen; + uint8_t buf[152], recLen = 0; + memset(buf, 0x00, sizeof(buf)); recLen=dcBL_getResponse(buf,0); if (recLen==0) @@ -467,8 +464,7 @@ uint8_t dcBL_sendSuccess(uint8_t lastCommand) // ------------------------------------------------------------------------- -char dcBL_loadBinary(char withDisplay) -{ +char dcBL_loadBinary(char withDisplay) { // same function as in "tabFw" //uint8_t uit8tmp; uint32_t ultmp, fileLen; @@ -476,14 +472,12 @@ char dcBL_loadBinary(char withDisplay) qDebug()<<"dcBL loading DC2c binary..."; QFile file("../dc2c4.bin"); - if (!file.exists()) - { + if (!file.exists()) { //qDebug()<<"file _own_H2B_dc2.bin does not exist"; dcBL_writeText("file _own_H2B_dc2.bin does not exist"); return 1; } - if (!file.open(QIODevice::ReadOnly) ) - { + if (!file.open(QIODevice::ReadOnly)) { //qDebug()<<"cannot open"; dcBL_writeText("cannot open"); return 1; @@ -491,8 +485,7 @@ char dcBL_loadBinary(char withDisplay) //qDebug()<<"loading...."; //qDebug()<<"size: " << file.size() <<"\n"; - if (withDisplay) - { + if (withDisplay) { s2tmp="loading file with "; tmpStr.setNum(file.size()); s2tmp.append(tmpStr); @@ -522,72 +515,70 @@ char dcBL_loadBinary(char withDisplay) dcBL_importBinFile(myBin, fileLen, withDisplay); return 0; - } - -static QByteArray dcBL_AtbBinFile; -static uint32_t dcBL_fileSize; -static uint16_t dcBL_nrOfBlocks; -static uint16_t dcBL_fileCrc; -//static uint8_t dcBL_myBuf[2570000]; // same content like "dcBL_AtbBinFile" but bytewise -static uint8_t dcBL_myBuf[300000]; // same content like "dcBL_AtbBinFile" but bytewise - -bool dcBL_importBinFile(QByteArray readBinFile, uint32_t fileSize, char withDispl) -{ +bool dcBL_importBinFile(QByteArray readBinFile, uint32_t fileSize, char withDispl) { uint16_t uitmp; uint32_t ultmp; uint32_t LL; QString tmpStr="", s2tmp=""; - dcBL_AtbBinFile=readBinFile; - dcBL_fileSize=fileSize; //if (dcBL_fileSize>258048) // 27.3.23TS woher kommt die Zahl??? // das hex file hat 278kB // hex file nicht erlaubt??? - if (dcBL_fileSize>=300000) + if (readBinFile.size() > 300000) { return false; // keep last file - ultmp=dcBL_fileSize; - ultmp %=64; - uitmp=uint16_t(ultmp); - ultmp=dcBL_fileSize; - ultmp /=64; - dcBL_nrOfBlocks=uint16_t(ultmp); - if (uitmp>0) - dcBL_nrOfBlocks++; // letzter Block hat !=64 byte + } + memset((char *)(&SharedMemBuffer::getData()->dcBL.AtbBinFile[0]), 0x00, + sizeof(SharedMemBuffer::getData()->dcBL.AtbBinFile)); + memcpy((char *)(&SharedMemBuffer::getData()->dcBL.AtbBinFile[0]), + readBinFile, readBinFile.size()); + + SharedMemBuffer::getData()->dcBL.fileSize = fileSize; + + ultmp = SharedMemBuffer::getDataConst()->dcBL.fileSize; + ultmp %= 64; + uitmp = uint16_t(ultmp); + ultmp = SharedMemBuffer::getDataConst()->dcBL.fileSize; + ultmp /= 64; + SharedMemBuffer::getData()->dcBL.nrOfBlocks = uint16_t(ultmp); + if (uitmp>0) { + // letzter Block hat !=64 byte + SharedMemBuffer::getData()->dcBL.nrOfBlocks++; + } // type conversion for crc calc - for (LL=0; LLdcBL.fileSize; LL++) { + SharedMemBuffer::getData()->dcBL.myBuf[LL] = + uint8_t(SharedMemBuffer::getDataConst()->dcBL.AtbBinFile[LL]); + } - dcBL_fileCrc=dcBL_calcCrcCcitt(LL, dcBL_myBuf); + SharedMemBuffer::getData()->dcBL.fileCrc = + dcBL_calcCrcCcitt(LL, SharedMemBuffer::getData()->dcBL.myBuf); // dcBL_writeText("file crc calculated successful"); - if (withDispl) - { + if (withDispl) { s2tmp="crc: "; - tmpStr.setNum(dcBL_fileCrc,16); + tmpStr.setNum(SharedMemBuffer::getDataConst()->dcBL.fileCrc, 16); s2tmp.append(tmpStr); dcBL_writeText(s2tmp); tmpStr.clear(); s2tmp="blocks: "; - tmpStr.setNum(dcBL_nrOfBlocks,10); + tmpStr.setNum(SharedMemBuffer::getDataConst()->dcBL.nrOfBlocks, 10); s2tmp.append(tmpStr); dcBL_writeText(s2tmp); tmpStr.clear(); s2tmp="last block size: "; - tmpStr.setNum(uitmp,10); + tmpStr.setNum(uitmp, 10); s2tmp.append(tmpStr); dcBL_writeText(s2tmp); } return true; - // erstmal nicht wie bisher um 14 Bytes nach oben schieben und Laenge/crc anhaengen, // weil man sonst den BL und das Win-tool auch aendern muesste - } char dcBL_loadBinSafe(void) @@ -607,22 +598,22 @@ char dcBL_loadBinSafe(void) //qDebug() <<"loading 1st time:"; dcBL_loadBinary(0); // file jetzt in: "QByteArray dcBL_AtbBinFile" mit Laenge "uint32_t dcBL_fileSize" - BINFILECOPY1=dcBL_AtbBinFile; - BinFileCpyLen1=dcBL_fileSize; + BINFILECOPY1 = (char *)(&SharedMemBuffer::getData()->dcBL.AtbBinFile[0]); + BinFileCpyLen1 = SharedMemBuffer::getDataConst()->dcBL.fileSize; //dcBL_writeText("loading 2nd time:"); //qDebug() <<"loading 2. time:"; dcBL_loadBinary(0); // file jetzt in: "QByteArray dcBL_AtbBinFile" mit Laenge "uint32_t dcBL_fileSize" - BINFILECOPY2=dcBL_AtbBinFile; - BinFileCpyLen2=dcBL_fileSize; + BINFILECOPY2 = (char *)(&SharedMemBuffer::getData()->dcBL.AtbBinFile[0]); + BinFileCpyLen2 = SharedMemBuffer::getDataConst()->dcBL.fileSize; //dcBL_writeText("loading 3rd time:"); //qDebug() <<"loading 3. time:"; dcBL_loadBinary(0); // file jetzt in: "QByteArray dcBL_AtbBinFile" mit Laenge "uint32_t dcBL_fileSize" - BINFILECOPY3=dcBL_AtbBinFile; - BinFileCpyLen3=dcBL_fileSize; + BINFILECOPY3 = (char *)(&SharedMemBuffer::getData()->dcBL.AtbBinFile[0]); + BinFileCpyLen3 = SharedMemBuffer::getDataConst()->dcBL.fileSize; //cc=BINFILECOPY2[100]; //if (cc>0) @@ -645,9 +636,9 @@ char dcBL_loadBinSafe(void) do { - if (dcBL_AtbBinFile[LL] != BINFILECOPY1[LL]) + if (SharedMemBuffer::getDataConst()->dcBL.AtbBinFile[LL] != BINFILECOPY1[LL]) stopp=1; - } while((++LL < dcBL_fileSize) && !stopp); + } while((++LL < SharedMemBuffer::getDataConst()->dcBL.fileSize) && !stopp); if (stopp) { @@ -662,9 +653,9 @@ char dcBL_loadBinSafe(void) //qDebug() <<"2. loop"; do { - if (dcBL_AtbBinFile[LL] != BINFILECOPY2[LL]) + if (SharedMemBuffer::getDataConst()->dcBL.AtbBinFile[LL] != BINFILECOPY2[LL]) stopp=1; - } while((++LL < dcBL_fileSize) && !stopp); + } while((++LL < SharedMemBuffer::getDataConst()->dcBL.fileSize) && !stopp); if (stopp) { @@ -701,13 +692,13 @@ uint8_t dcBL_getAtbFileHeader(uint8_t *buf) myBuf[pp++]='C'; myBuf[pp++]='2'; myBuf[pp++]='-'; - myBuf[pp++]=ulongTOuchar(dcBL_fileSize, GETHIGHBYTE); - myBuf[pp++]=ulongTOuchar(dcBL_fileSize, GETMIDHIGHBYTE); - myBuf[pp++]=ulongTOuchar(dcBL_fileSize, GETMIDLOWBYTE); - myBuf[pp++]=ulongTOuchar(dcBL_fileSize, GETLOWBYTE); + myBuf[pp++]=ulongTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileSize, GETHIGHBYTE); + myBuf[pp++]=ulongTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileSize, GETMIDHIGHBYTE); + myBuf[pp++]=ulongTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileSize, GETMIDLOWBYTE); + myBuf[pp++]=ulongTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileSize, GETLOWBYTE); myBuf[pp++]='-'; - myBuf[pp++]=uintTOuchar(dcBL_fileCrc, GETHIGHBYTE); - myBuf[pp++]=uintTOuchar(dcBL_fileCrc, GETLOWBYTE); + myBuf[pp++]=uintTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileCrc, GETHIGHBYTE); + myBuf[pp++]=uintTOuchar(SharedMemBuffer::getDataConst()->dcBL.fileCrc, GETLOWBYTE); myBuf[pp++]='-'; len=pp; //tslib_strcpy(myBuf, buf, (len+3)); @@ -744,8 +735,7 @@ uint8_t dcBL_getFileBlock(uint16_t blockPointer, uint8_t *buf) // all 64bytes can be read: for (LL=0; LL<64; LL++) { - buf[LL]=uint8_t(dcBL_AtbBinFile[LL+addr]); - //buf[LL]=dcBL_myBuf[LL+addr]; + buf[LL]=uint8_t(SharedMemBuffer::getDataConst()->dcBL.AtbBinFile[LL+addr]); } /* @@ -793,80 +783,79 @@ uint8_t dcBL_getFileBlock(uint16_t blockPointer, uint8_t *buf) // this lines shall be displayed in tabFW / FW_responseWindow // they are read every 100ms. Make batch for 10 lines -#define SIZEBLRESP 50 -static QString BlResp[SIZEBLRESP]; -static int pBlResp; +//#define SIZEBLRESP 50 +//static QString BlResp[SIZEBLRESP]; +//static int pBlResp; -bool dcBL_isTextMemFree(void) -{ - if (pBlResp<0) pBlResp=0; // just for security - if (pBlResp<(SIZEBLRESP-1)) - return true; - return false; +bool dcBL_isTextMemFree(void) { + if (SharedMemBuffer::getDataConst()->dcBL.pResp < 0) { + SharedMemBuffer::getData()->dcBL.pResp = 0; // just for security + } + return (SharedMemBuffer::getDataConst()->dcBL.pResp < SIZEBLRESP-1); } -void dcBL_writeText(QString newTxt) -{ +void dcBL_writeText(QString newTxt) { // write text her in this file from memory 1....9, mem 0 is always free - if (dcBL_isTextMemFree()) - BlResp[++pBlResp]=newTxt; + if (dcBL_isTextMemFree()) { + int const pResp = ++SharedMemBuffer::getData()->dcBL.pResp; + memset((char *)(&SharedMemBuffer::getData()->dcBL.Resp[pResp][0]), 0x00, + sizeof(SharedMemBuffer::getDataConst()->dcBL.Resp[pResp])); + strncpy((char *)(&SharedMemBuffer::getData()->dcBL.Resp[pResp][0]), + newTxt.toStdString().c_str(), + sizeof(SharedMemBuffer::getDataConst()->dcBL.Resp[pResp])-1); + } } -bool dcBL_checkForText(void) -{ +bool dcBL_checkForText(void) { // if pointer at 0 then no more content - if (pBlResp>0) - return true; - return false; + return (SharedMemBuffer::getDataConst()->dcBL.pResp > 0); } -QString dcBL_readText(void) -{ +QString dcBL_readText(void) { // read from 0...9 (oldest first) - if (pBlResp<=0) // should never be <0 + if (SharedMemBuffer::getDataConst()->dcBL.pResp <= 0) { // should never be < 0 return ""; - QString locStr=BlResp[1]; // store memory[1] + } + // store memory[1] + QString locStr = SharedMemBuffer::getDataConst()->dcBL.Resp[1]; // move memories down by one: 2->1 3->2 4>3....9->8. Place 1 is free now // example: if pBlResp==3 then we have still text 2 and 3 to display. 1 will be transfered now with this call - int nn; - for (nn=1; nn0) pBlResp--; + for (int nn = 1; nn < SharedMemBuffer::getDataConst()->dcBL.pResp; nn++) { + memcpy((char *)(&SharedMemBuffer::getData()->dcBL.Resp[nn][0]), + (char const *)(&SharedMemBuffer::getData()->dcBL.Resp[nn+1][0]), + sizeof(SharedMemBuffer::getData()->dcBL.Resp[0])); + } + if (SharedMemBuffer::getDataConst()->dcBL.pResp > 0) { + SharedMemBuffer::getData()->dcBL.pResp--; + } return locStr; } - - // ------------------------------------------------------------------------- - -static uint8_t dcBL_step, dcBL_state; -static uint16_t dcBL_BlkCtr, dcBL_cyclCtr, repeatCtr; - -void dcBL_iniChain(void) -{ - int nn; - dcBL_step=0; - dcBL_cyclCtr=0; - dcBL_state=0; - dcBL_BlkCtr=0; - repeatCtr=0; +void dcBL_iniChain(void) { + SharedMemBuffer::getData()->dcBL.step = 0; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; + SharedMemBuffer::getData()->dcBL.state = 0; + SharedMemBuffer::getData()->dcBL.BlkCtr = 0; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; // delete output window: - pBlResp=0; - for (nn=0;nndcBL.pResp = 0; + for (int nn=0;nndcBL.Resp[nn][0]), 0x00, + sizeof(SharedMemBuffer::getDataConst()->dcBL.Resp[0])); + } //dcBL_writeText("**CLEAR**"); dcBL_writeText("bl chain ini"); } -uint8_t dcBL_startChain(void) -{ - if (dcBL_step==0 || dcBL_step>17) - { +uint8_t dcBL_startChain(void) { + if (SharedMemBuffer::getDataConst()->dcBL.step == 0 || + SharedMemBuffer::getDataConst()->dcBL.step > 17) { dcBL_iniChain(); - dcBL_step=1; + SharedMemBuffer::getData()->dcBL.step = 1; //epi_clrRawReceivedString(); qDebug()<<"starting chain..."; //qDebug()<dcBL.step) { case 1: // tell DC-app to restart dcBL_writeText("loading binary.."); ret=dcBL_loadBinSafe(); - if (ret) - dcBL_step++; - else - { + if (ret) { + SharedMemBuffer::getData()->dcBL.step++; + } else { dcBL_writeText("cancel, cannot load binary!"); - dcBL_step=0; + SharedMemBuffer::getData()->dcBL.step = 0; } break; - case 2: dcBL_writeText("sending restart..."); len=dcBL_restartDC(buf); sendWRcmd_setSendBlock160(len, buf); - dcBL_step++; + SharedMemBuffer::getData()->dcBL.step++; break; - case 3: // wait 100ms more - dcBL_step++; + SharedMemBuffer::getData()->dcBL.step++; break; - case 4: // start BL dcBL_writeText("sending BL start..."); len=dcBL_activatBootloader(buf); sendWRcmd_setSendBlock160(5, buf); - dcBL_step++; - dcBL_cyclCtr=0; + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; break; - case 5: // wait for answer: 2 99 52 53 98 51 3 // Problem: kommt von app, nicht vom BL. Also wenn BL schon aktiv ist dann gehts nicht @@ -954,313 +936,257 @@ uint8_t dcBL_runChain(void) dcBL_step=0; // stop chain return 0; } */ - - dcBL_step++; - repeatCtr=0; + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; break; - case 6: // request Version number len=dcBL_readFWversion(buf); sendWRcmd_setSendBlock160(len, buf); dcBL_writeText("request version nr..."); - dcBL_cyclCtr=0; - dcBL_step++; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; + SharedMemBuffer::getData()->dcBL.step++; break; - case 7: // wait for answer 2 146 45 45 95 176 3 - - if (gotResp==ISOK) - { - dcBL_state=1; // BL started - dcBL_step++; - repeatCtr=0; + if (gotResp==ISOK) { // BL started + SharedMemBuffer::getData()->dcBL.state = 1; + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr<3) - { - dcBL_step--; - }else - { - dcBL_step=0; // stop chain + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr < 3) { + SharedMemBuffer::getData()->dcBL.step--; + } else { // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; } } - - if (++dcBL_cyclCtr>10) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 10) { dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - case 8: // send start address - if (dcBL_BlkCtr==0 || dcBL_BlkCtr==1024 || dcBL_BlkCtr==2048 || dcBL_BlkCtr==3072 || dcBL_BlkCtr==4096) - { - ultmp=uint32_t(dcBL_BlkCtr); - ultmp*=64; + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 0 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 1024 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 2048 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 3072 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 4096) { + ultmp = (uint32_t)(SharedMemBuffer::getData()->dcBL.BlkCtr); + ultmp *= 64; len=dcBL_sendFlashStartAddr2BL(ultmp, buf); sendWRcmd_setSendBlock160(len, buf); dcBL_writeText("sending start address"); - dcBL_cyclCtr=0; - dcBL_step++; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; + SharedMemBuffer::getData()->dcBL.step++; return 0; - } else - dcBL_step=12; + } else { + SharedMemBuffer::getData()->dcBL.step = 12; + } break; - case 9: // wait for answer 2 161 68 59 3 - if (gotResp==ISOK) - { - dcBL_step=12; // Header nicht senden, unnötig - dcBL_BlkCtr=0; // 0 - repeatCtr=0; + if (gotResp==ISOK) { // Header nicht senden, unnötig + SharedMemBuffer::getData()->dcBL.step = 12; + SharedMemBuffer::getData()->dcBL.BlkCtr = 0; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr<3) - { - dcBL_step--; - } else - { - dcBL_step=0; // stop chain + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr < 3) { + SharedMemBuffer::getData()->dcBL.step--; + } else { + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } } - if (++dcBL_cyclCtr>10) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 10) { dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - - case 12: // send binary 64 byte wise - - len=dcBL_getFileBlock(dcBL_BlkCtr, buf); // read from file, len = 0...64 + len=dcBL_getFileBlock(SharedMemBuffer::getData()->dcBL.BlkCtr, + buf); // read from file, len = 0...64 //lastBlkLength=len; // recognize last block or end sendLen=dcBL_prepareDC_BLcmd(0x22, len, buf, sendBuf); // pack into protocol frame - - // Kontrolle: qDebug()<<"dcBL sending: "; ii=0; - do - { + do { uit8tmp=(uint8_t)sendBuf[ii++]; //qDebug() << uit8tmp << " "; // Anzeige in dez stemp.setNum(uit8tmp,16); qDebug() << stemp << " "; // Anzeige in hex - } while (ii<100); - sendWRcmd_setSendBlock160(sendLen, sendBuf); // send 140 bytes delay(100); //dcBL_writeText("blk nr: "); - tmpStr.setNum(dcBL_BlkCtr); + tmpStr.setNum(SharedMemBuffer::getDataConst()->dcBL.BlkCtr); dcBL_writeText(tmpStr); delay(100); //qDebug()<<"sending: " << buf; - dcBL_cyclCtr=0; - dcBL_BlkCtr++; - dcBL_step++; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; + SharedMemBuffer::getData()->dcBL.BlkCtr++; + SharedMemBuffer::getData()->dcBL.step++; break; - case 13: // wait for answer 2 162 116 88 3 - - if (gotResp==ISOK) - { + if (gotResp==ISOK) { // check if next address is needed (every 1024Blocks = 65536bytes) - if (dcBL_BlkCtr==1024 || dcBL_BlkCtr==2048 || dcBL_BlkCtr==3072 || dcBL_BlkCtr==4096) - { - dcBL_step=8; + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 0 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 1024 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 2048 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 3072 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 4096) { + SharedMemBuffer::getData()->dcBL.step = 8; return 0; } // check for EOF: - if (dcBL_BlkCtr >= dcBL_nrOfBlocks) - { + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr >= + SharedMemBuffer::getDataConst()->dcBL.nrOfBlocks) { dcBL_writeText("last block successful sent..."); - dcBL_step++; // stop chain - dcBL_state=3; // transmission SUCCESSFUL + SharedMemBuffer::getData()->dcBL.step++; // stop chain + SharedMemBuffer::getData()->dcBL.state = 3; // transmission SUCCESSFUL return 0; } - dcBL_state=2; // transmission started - dcBL_step--; // send next data block - repeatCtr=0; + SharedMemBuffer::getData()->dcBL.state = 2; // transmission started + SharedMemBuffer::getData()->dcBL.step--; // send next data block + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; } - if (gotResp==ISWRONG) - { - if (++repeatCtr<3) - { + if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr < 3) { dcBL_writeText("error"); - dcBL_BlkCtr--; - dcBL_step--; // send same block again - } else - { - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.BlkCtr--; + SharedMemBuffer::getData()->dcBL.step--; // send same block again + } else { + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } } - if (++dcBL_cyclCtr>30) // longer TO due to flashing - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 30) { // longer TO due to flashing dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - case 14: // dcBL_writeText("finish writing flash"); len=dcBL_writeLastPage(buf); // nur zur Sicherheit sendWRcmd_setSendBlock160(len, buf); - dcBL_step++; + SharedMemBuffer::getData()->dcBL.step++; break; - case 15: // wait for answer - if (gotResp==ISOK) - { - dcBL_step++; - repeatCtr=0; + if (gotResp==ISOK) { + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr>3) - { - dcBL_step--; + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr > 3) { + SharedMemBuffer::getData()->dcBL.step--; } } - if (++dcBL_cyclCtr>10) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 10) { dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - - case 16: // dcBL_writeText("exit BL, bye"); len=dcBL_exitBL(buf); sendWRcmd_setSendBlock160(len, buf); - dcBL_step++; - + SharedMemBuffer::getData()->dcBL.step++; break; - case 17: // wait for answer, not sure if it comes! - if (gotResp==ISOK) - { - dcBL_step++; - repeatCtr=0; + if (gotResp==ISOK) { + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr>10) - { - dcBL_step--; + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr > 10) { + SharedMemBuffer::getData()->dcBL.step--; } } - if (++dcBL_cyclCtr>30) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 30) { dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - case 18: - dcBL_step=0; + SharedMemBuffer::getData()->dcBL.step = 0; break; } - return 0; } - - -void dcBL_iniLoading(void) -{ +void dcBL_iniLoading(void) { dcBL_iniChain(); } // nicht verwendet -void dcBL_startLoading(void) -{ - if (dcBL_step==0 || dcBL_step>17) - { +void dcBL_startLoading(void) { + if (SharedMemBuffer::getDataConst()->dcBL.step == 0 || + SharedMemBuffer::getDataConst()->dcBL.step > 17) { //dcBL_iniChain(); - dcBL_step=1; + SharedMemBuffer::getData()->dcBL.step = 1; //epi_clrRawReceivedString(); qDebug()<<"DCBL start sending hexfile..."; dcBL_writeText("DCBL start sending hexfile..."); - dcBL_BlkCtr=0; - dcBL_cyclCtr=0; + SharedMemBuffer::getData()->dcBL.BlkCtr = 0; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; } - } - // nicht verwendet -uint8_t dcBL_sendHexfile(void) -{ +uint8_t dcBL_sendHexfile(void) { QString tmpStr=""; uint8_t buf[70], sendBuf[160], len, sendLen, gotResp; //, recLen; recBuff[160], ii, QString stemp=" "; uint32_t ultmp; //uint8_t uit8tmp, ii; - gotResp=dcBL_ChkResponse(); - switch (dcBL_step) - { + switch (SharedMemBuffer::getDataConst()->dcBL.step) { case 1: // send start address - if (dcBL_BlkCtr==0 || dcBL_BlkCtr==1024 || dcBL_BlkCtr==2048 || dcBL_BlkCtr==3072 || dcBL_BlkCtr==4096) - { - ultmp=uint32_t(dcBL_BlkCtr); + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 0 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 1024 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 2048 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 3072 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 4096) { + ultmp=uint32_t(SharedMemBuffer::getDataConst()->dcBL.BlkCtr); ultmp*=64; len=dcBL_sendFlashStartAddr2BL(ultmp, buf); sendWRcmd_setSendBlock160(len, buf); qDebug()<<" DCBL_CYCL_sending HexFile address "; dcBL_writeText("sending addr"); - dcBL_step++; + SharedMemBuffer::getData()->dcBL.step++; return 0; - } else - dcBL_step=12; + } else { + SharedMemBuffer::getData()->dcBL.step = 12; + } break; - case 2: // wait for answer 2 161 68 59 3 - if (gotResp==ISOK) - { - dcBL_step=12; // Header nicht senden, unnötig - dcBL_BlkCtr=0; // 0 - repeatCtr=0; + if (gotResp==ISOK) { + // Header nicht senden, unnötig + SharedMemBuffer::getData()->dcBL.step = 12; + SharedMemBuffer::getData()->dcBL.BlkCtr = 0; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr<3) - { - dcBL_step--; - } else - { - dcBL_step=0; // stop chain + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr < 3) { + SharedMemBuffer::getData()->dcBL.step--; + } else { + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain qDebug()<<" DCBL_CYCL_got wrong resp to addr"; dcBL_writeText("wrong resp"); } } - if (++dcBL_cyclCtr>100) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 100) { dcBL_writeText("cancel"); - qDebug()<<" DCBL_CYCL_step 2 got NO resp to addr"; - dcBL_step=0; // stop chain + qDebug()<<" DCBL_CYCL_step 2 got NO resp to addr"; + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - case 12: // send binary 64 byte wise - - len=dcBL_getFileBlock(dcBL_BlkCtr, buf); // read from file, len = 0...64 + len=dcBL_getFileBlock(SharedMemBuffer::getData()->dcBL.BlkCtr, + buf); // read from file, len = 0...64 //lastBlkLength=len; // recognize last block or end sendLen=dcBL_prepareDC_BLcmd(0x22, len, buf, sendBuf); // pack into protocol frame /* @@ -1279,83 +1205,74 @@ uint8_t dcBL_sendHexfile(void) sendWRcmd_setSendBlock160(sendLen, sendBuf); // send 140 bytes delay(100); dcBL_writeText("blk nr: "); - tmpStr.setNum(dcBL_BlkCtr); + tmpStr.setNum(SharedMemBuffer::getData()->dcBL.BlkCtr); dcBL_writeText(tmpStr); delay(100); - qDebug()<<"DCBL sending blk nr: " << dcBL_BlkCtr; - dcBL_cyclCtr=0; - dcBL_BlkCtr++; - dcBL_step++; + qDebug()<<"DCBL sending blk nr: " + << SharedMemBuffer::getData()->dcBL.BlkCtr; + SharedMemBuffer::getData()->dcBL.cyclCtr = 0; + SharedMemBuffer::getData()->dcBL.BlkCtr++; + SharedMemBuffer::getData()->dcBL.step++; break; - case 13: // wait for answer 2 162 116 88 3 - - if (gotResp==ISOK) - { + if (gotResp==ISOK) { // check if next address is needed (every 1024Blocks = 65536bytes) - if (dcBL_BlkCtr==1024 || dcBL_BlkCtr==2048 || dcBL_BlkCtr==3072 || dcBL_BlkCtr==4096) - { - dcBL_step=8; + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 0 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 1024 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 2048 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 3072 || + SharedMemBuffer::getDataConst()->dcBL.BlkCtr == 4096) { + SharedMemBuffer::getData()->dcBL.step = 8; return 0; } // check for EOF: - if (dcBL_BlkCtr >= dcBL_nrOfBlocks) - { + if (SharedMemBuffer::getDataConst()->dcBL.BlkCtr >= + SharedMemBuffer::getDataConst()->dcBL.nrOfBlocks) { dcBL_writeText("last block successful sent..."); - dcBL_step++; // stop chain - dcBL_state=3; // transmission SUCCESSFUL + SharedMemBuffer::getData()->dcBL.step++; // stop chain + SharedMemBuffer::getData()->dcBL.state = 3; // transmission SUCCESSFUL return 0; } - dcBL_state=2; // transmission started - dcBL_step--; // send next data block - repeatCtr=0; + SharedMemBuffer::getData()->dcBL.state = 2; // transmission started + SharedMemBuffer::getData()->dcBL.step--; // send next data block + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; } - if (gotResp==ISWRONG) - { - if (++repeatCtr<3) - { + if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr < 3) { dcBL_writeText("error"); - dcBL_BlkCtr--; - dcBL_step--; // send same block again - } else - { - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.BlkCtr--; + SharedMemBuffer::getData()->dcBL.step--; // send same block again + } else { + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } } - if (++dcBL_cyclCtr>30) // longer TO due to flashing - { + if (++SharedMemBuffer::getData()->dcBL.BlkCtr > 30) { // longer TO due to flashing dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; - case 14: // dcBL_writeText("finish writing flash"); len=dcBL_writeLastPage(buf); // nur zur Sicherheit sendWRcmd_setSendBlock160(len, buf); - dcBL_step++; + SharedMemBuffer::getData()->dcBL.step++; break; case 15: // wait for answer - if (gotResp==ISOK) - { - dcBL_step++; - repeatCtr=0; + if (gotResp==ISOK) { + SharedMemBuffer::getData()->dcBL.step++; + SharedMemBuffer::getData()->dcBL.repeatCtr = 0; return 0; - } else - if (gotResp==ISWRONG) - { - if (++repeatCtr>3) - { - dcBL_step--; + } else if (gotResp==ISWRONG) { + if (++SharedMemBuffer::getData()->dcBL.repeatCtr > 3) { + SharedMemBuffer::getData()->dcBL.step--; } } - if (++dcBL_cyclCtr>10) - { + if (++SharedMemBuffer::getData()->dcBL.cyclCtr > 10) { dcBL_writeText("cancel"); - dcBL_step=0; // stop chain + SharedMemBuffer::getData()->dcBL.step = 0; // stop chain } break; @@ -1363,92 +1280,44 @@ uint8_t dcBL_sendHexfile(void) return 0; } - - - - -uint8_t dcBL_getResult(void) -{ +uint8_t dcBL_getResult(void) { // call after every step to what's going on.... // 1: connected to BL // 2: transmission started // 3: transmission successful - - return dcBL_state; + return SharedMemBuffer::getDataConst()->dcBL.state; } -// ------------------------------------------------------------------------- -// ------------------------------------------------------------------------- -// ------------------------------------------------------------------------- - - -static uint8_t Sdata_rawData[RAW_BL_DATALEN]; - -static uint8_t Sdata_LengthRawData; - -void gpi_storeRawReceivedData(uint8_t RdDlen, uint8_t *receivedData) -{ - uint8_t nn; - Sdata_LengthRawData=RdDlen; - if (Sdata_LengthRawData>RAW_BL_DATALEN) - Sdata_LengthRawData=RAW_BL_DATALEN; - for (nn=0; nnSdata.LengthRawData + = std::min(RdDlen, (uint8_t)RAW_BL_DATALEN); + memcpy((char *)SharedMemBuffer::getData()->Sdata.rawData, + receivedData, SharedMemBuffer::getDataConst()->Sdata.LengthRawData); } -uint8_t epi_getRawReceivedData(uint8_t *receivedData) -{ - // retval=length, will be zeroed after first reading - uint8_t nn, ret; - - for (nn=0; nnSdata.rawData, + SharedMemBuffer::getDataConst()->Sdata.LengthRawData); + return SharedMemBuffer::getDataConst()->Sdata.LengthRawData; } -uint8_t epi_getRawRecLength(void) -{ +uint8_t epi_getRawRecLength(void) { // retval=length - return Sdata_LengthRawData; + return SharedMemBuffer::getDataConst()->Sdata.LengthRawData; } -QString epi_getRawReceivedString() -{ - uint8_t nn; //, ret; - QString myString=nullptr, tmpStr=nullptr; - - myString.clear(); - if (Sdata_LengthRawData==0) - return myString; - - for (nn=0; nn0x80 dann wird EIN Byte 16 stellig angezeigt - int ll=tmpStr.length(); - if (ll>2) - { - myString.append(tmpStr[ll-2]); - myString.append(tmpStr[ll-1]); - } else - { - myString.append(tmpStr); - } - myString.append(" "); - +QString epi_getRawReceivedString() { + if (SharedMemBuffer::getDataConst()->Sdata.LengthRawData > 0) { + QByteArray ba((char *)SharedMemBuffer::getData()->Sdata.rawData, + SharedMemBuffer::getDataConst()->Sdata.LengthRawData); + return ba.toHex(); } - //ret=Sdata_LengthRawData; - //Sdata_LengthRawData=0; - return myString; + return ""; } -void epi_clrRawReceivedString() -{ - Sdata_LengthRawData=0; +void epi_clrRawReceivedString() { + SharedMemBuffer::getData()->Sdata.LengthRawData = 0; } @@ -1456,6 +1325,3 @@ void epi_clrRawReceivedString() - - - diff --git a/src/hwapi.cpp b/src/hwapi.cpp index 86e1068..74f1fce 100644 --- a/src/hwapi.cpp +++ b/src/hwapi.cpp @@ -1772,8 +1772,7 @@ void hwapi::readback_machineIDdata(uint8_t *length, uint8_t *data) const static uint16_t hwapi_shutterTime; // locks, 2.Level: (Motor stops automatical on end switch or by 5s timeout) -uint8_t hwapi::lock_openUpperDoor(void) const -{ +uint8_t hwapi::lock_openUpperDoor(void) const { //bool sendWRcmd_setSendCommand4(uint16_t nextCmd, uint8_t dat1, uint8_t dat2, uint8_t dat3, uint8_t dat4); // commands are defined in PIdefines.h sendWRcmd_setSendCommand4(SENDDIRCMD_OPENUP_DOOR, 1, 0, 0, 0); @@ -1801,54 +1800,39 @@ uint8_t hwapi::lock_closeLowerDoor(void) const return 0; } -void hwapi::shut_openOnce(void) const -{ +void hwapi::shut_openOnce(void) const { // and close automatic after shutter time - uint16_t zeit=hwapi_shutterTime; - zeit/=100; + uint16_t zeit = (SharedMemBuffer::getDataConst()->hwapi.shutterTime) / 100; sendWRcmd_setSendCommand4(SENDDIRCMD_SHUTOPENBYTIME, uint8_t(zeit) ,0,0,0); } -void hwapi::shut_openForCoin(bool start) const -{ +void hwapi::shut_openForCoin(bool start) const { // start=true: start opening flap if coin is attached // start=false: stop process - - uint16_t zeit=hwapi_shutterTime; - zeit/=100; + uint16_t zeit = (SharedMemBuffer::getDataConst()->hwapi.shutterTime) / 100; sendWRcmd_setSendCommand4(SENDDIRCMD_SHUTOPENBYCOIN, uint8_t(start), uint8_t(zeit),0,0); } -void hwapi::shut_sendOpeningTime(uint16_t timeIn_ms ) const -{ +void hwapi::shut_sendOpeningTime(uint16_t timeIn_ms ) const { // after this time without retrigger the flap is closed //sendWRcmd_setSendCommand4(SENDDIRCMD_SHUT_SENDTIME, timeIn100ms,0,0,0); - hwapi_shutterTime=timeIn_ms; - + SharedMemBuffer::getData()->hwapi.shutterTime = timeIn_ms; } -void hwapi::esc_takeMoney(void) const -{ +void hwapi::esc_takeMoney(void) const { // and close automatically after escrow time (1s) sendWRcmd_setSendCommand0(SENDDIRCMD_ESCRO_TAKE); } -void hwapi::esc_returnMoney(void) const -{ +void hwapi::esc_returnMoney(void) const { // and close automatically after time sendWRcmd_setSendCommand0(SENDDIRCMD_ESCRO_GIVE); } - - - - - - // ---------------------------------------------------------------------------------------------------------- // --------------------------------------------- MIFARE ----------------------------------------------------- // ---------------------------------------------------------------------------------------------------------- diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..cd9704a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,9 @@ +#include +#include "tslib.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + return a.exec(); +} diff --git a/src/sendWRcmd.cpp b/src/sendWRcmd.cpp index afca5a6..e47899e 100644 --- a/src/sendWRcmd.cpp +++ b/src/sendWRcmd.cpp @@ -1,283 +1,157 @@ -#include +#include +#include + #include #include + #include "tslib.h" #include "sendWRcmd.h" - +#include "shared_mem_buffer.h" void indat_PrnPwr(void); +/* convention: use simple (not rotating) FIFO Stack: +Example: nrOfCmdsInQueue=4 then + nextAsynchsendCmd0[0]=cmd1 // was stored as first + nextAsynchsendCmd0[1]=cmd2 + nextAsynchsendCmd0[2]=cmd3 + nextAsynchsendCmd0[3]=cmd4 // came in as last -void sendWRcmd_INI(void) -{ + Send: [0] first, then move buffer 1 down: + nextAsynchsendCmd0[0]=cmd2 + nextAsynchsendCmd0[1]=cmd3 + nextAsynchsendCmd0[2]=cmd4 + nextAsynchsendCmd0[3]=0; + nrOfCmdsInQueue=3 now +*/ +template +static void *move_buffer_down_by_one(T (&buffer)[N]) { + return memmove(buffer, &buffer[1], sizeof(buffer)-sizeof(buffer[0])); +} +template +static void clear(T (&buffer)[N]) { + memset((char *)(&buffer[0]), 0x00, sizeof(buffer)); +} +template +static void clear(T (&buffer)[M][N]) { + memset((char *)(&buffer[0][0]), 0x00, sizeof(buffer)); +} +template +static void sendWRcmd_clearCmdStack(T &cmdStack) { + clear(cmdStack.AsynchSend); + clear(cmdStack.para1); + clear(cmdStack.para2); + clear(cmdStack.para3); + clear(cmdStack.para4); + cmdStack.nrOfCmdsInQueue = 0; +} +template +static bool sendWRcmd_setSendCmd(T &cmdStack, uint16_t nextCmd, T1 dat1, + T2 dat2, T3 dat3, T4 dat4) { + uint8_t const &n = cmdStack.nrOfCmdsInQueue; + int const STACKDEPTH = sizeof(cmdStack.para1); + if (n < STACKDEPTH) { + cmdStack.AsynchSend[n] = nextCmd; + cmdStack.para1[n] = dat1; + cmdStack.para2[n] = dat2; + cmdStack.para3[n] = dat3; + cmdStack.para4[n] = dat4; + cmdStack.nrOfCmdsInQueue += 1; + return true; // ok, will be sent + } + qCritical() << "cannot save cmd because stack is full"; + return false; // not possible +} +template +static bool sendWRcmd_getSendCmd(T &cmdStack, T1 *dat1, T2 *dat2, + T3 *dat3, T4 *dat4) { + if (dat1 && dat2 && dat3 && dat4) { + uint8_t const &n = cmdStack.nrOfCmdsInQueue; + int const STACKDEPTH = sizeof(cmdStack.para1); + if ((n > 0) && (n <= STACKDEPTH)) { + uint16_t const nxt = cmdStack.AsynchSend[0]; + move_buffer_down_by_one(cmdStack.AsynchSend); + *dat1 = cmdStack.para1[n]; + *dat2 = cmdStack.para2[n]; + *dat3 = cmdStack.para3[n]; + *dat4 = cmdStack.para4[n]; + cmdStack.nrOfCmdsInQueue = n - 1; + return nxt; + } + } + qCritical() << "cannot fetch cmd"; + return 0; // error +} +void sendWRcmd_INI(void) { sendWRcmd_clrCmdStack(); sendWRcmd_clrCmd4Stack(); sendFDcmd_clrStack(); longFDcmd_clrStack(); } -// Command Stack for commands without parameters - - -static uint16_t nextAsynchsendCmd0[CMDSTACKDEPTH]; -static uint8_t nrOfCmdsInQueue; -/* convention: use simple (not rotating) FIFO Stack: -Example: nrOfCmdsInQueue=4 then - nextAsynchsendCmd0[0]=cmd1 // was stored as first - nextAsynchsendCmd0[1]=cmd2 - nextAsynchsendCmd0[2]=cmd3 - nextAsynchsendCmd0[3]=cmd4 // came in as last - - Send: [0] first, then move buffer 1 down: - nextAsynchsendCmd0[0]=cmd2 - nextAsynchsendCmd0[1]=cmd3 - nextAsynchsendCmd0[2]=cmd4 - nextAsynchsendCmd0[3]=0; - nrOfCmdsInQueue=3 now -*/ - -void sendWRcmd_clrCmdStack(void) -{ - uint8_t nn; - for (nn=0; nnCmd0); +} +void sendWRcmd_clrCmd4Stack(void) { + sendWRcmd_clearCmdStack(SharedMemBuffer::getData()->Cmd4); +} +void sendWRcmd_clrCmd8Stack(void) { + sendWRcmd_clearCmdStack(SharedMemBuffer::getData()->Cmd8); } -bool sendWRcmd_setSendCommand0(uint16_t nextCmd) -{ +bool sendWRcmd_setSendCommand0(uint16_t nextCmd, uint8_t dat1, uint8_t dat2, + uint8_t dat3, uint8_t dat4) { // write Command to memory, wait for transport - if (nrOfCmdsInQueue>=CMDSTACKDEPTH) - { - qDebug() << "cannot save cmd because stack is full"; - return false; // not possible - } - nextAsynchsendCmd0[nrOfCmdsInQueue++]=nextCmd; - //qDebug() << "PI cmd queued:"<< nextCmd << ", saved, pp=" << nrOfCmdsInQueue; - return true; // ok, will be sent + return sendWRcmd_setSendCmd(SharedMemBuffer::getData()->Cmd0, + nextCmd, dat1, dat2, dat3, dat4); } - -uint16_t sendWRcmd_getSendCommand0(void) -{ - uint16_t nxtAsynchCmd; - uint8_t nn, ll; - if (nrOfCmdsInQueue==0 || nrOfCmdsInQueue>CMDSTACKDEPTH) - return 0; // error - nxtAsynchCmd=nextAsynchsendCmd0[0]; - - // move Puffer down by one element - if (CMDSTACKDEPTH>0) - ll=CMDSTACKDEPTH-1; - else - ll=0; - for (nn=0; nn0) - nrOfCmdsInQueue--; - //qDebug() << "PI cmd queued:"<< nxtAsynchCmd << ", restored, pp now =" << nrOfCmdsInQueue; - return nxtAsynchCmd; -} - -//--------------------------------------------------------------------------------------------------------------------- -//--------------------------------------------------------------------------------------------------------------------- -// Command Stack for commands with 4 parameters - - - -static uint16_t nextAsynchsendCmd4[CMD4STACKDEPTH]; -static uint8_t nextCmd4para1[CMD4STACKDEPTH]; -static uint8_t nextCmd4para2[CMD4STACKDEPTH]; -static uint8_t nextCmd4para3[CMD4STACKDEPTH]; -static uint8_t nextCmd4para4[CMD4STACKDEPTH]; -static uint8_t nrOfCmds4InQueue; -/* convention: use simple (not rotating) FIFO Stack: -Example: nrOfCmdsInQueue=4 then - nextAsynchsendCmd0[0]=cmd1 // was stored as first - nextAsynchsendCmd0[1]=cmd2 - nextAsynchsendCmd0[2]=cmd3 - nextAsynchsendCmd0[3]=cmd4 // came in as last - - Send: [0] first, then move buffer 1 down: - nextAsynchsendCmd0[0]=cmd2 - nextAsynchsendCmd0[1]=cmd3 - nextAsynchsendCmd0[2]=cmd4 - nextAsynchsendCmd0[3]=0; - nrOfCmdsInQueue=3 now -*/ - - -void sendWRcmd_clrCmd4Stack(void) -{ - uint8_t nn; - for (nn=0; nn=CMD4STACKDEPTH) - { - qDebug() << "cannot save cmd because stack is full"; - return false; // not possible - } - nextAsynchsendCmd4[nrOfCmds4InQueue]=nextCmd; - nextCmd4para1[nrOfCmds4InQueue]=dat1; - nextCmd4para2[nrOfCmds4InQueue]=dat2; - nextCmd4para3[nrOfCmds4InQueue]=dat3; - nextCmd4para4[nrOfCmds4InQueue]=dat4; - //qDebug() << "data with 4 data byte saved, pp=" << nrOfCmds4InQueue; - //qDebug() << " dat1=" << nextCmd4para1[nrOfCmds4InQueue] << " dat2=" << nextCmd4para2[nrOfCmds4InQueue] - // << " dat3=" << nextCmd4para3[nrOfCmds4InQueue] << " dat4=" << nextCmd4para4[nrOfCmds4InQueue]; - nrOfCmds4InQueue++; - return true; // ok, will be sent + return sendWRcmd_setSendCmd(SharedMemBuffer::getData()->Cmd4, nextCmd, dat1, + dat2, dat3, dat4); } - -uint16_t sendWRcmd_getSendCommand4(uint8_t *dat1, uint8_t *dat2, uint8_t *dat3, uint8_t *dat4) -{ - uint16_t nxtAsynchCmd; - uint8_t nn, ll; - - if (nrOfCmds4InQueue==0 || nrOfCmds4InQueue>CMD4STACKDEPTH) - return 0; // error - nxtAsynchCmd=nextAsynchsendCmd4[0]; - *dat1=nextCmd4para1[0]; - *dat2=nextCmd4para2[0]; - *dat3=nextCmd4para3[0]; - *dat4=nextCmd4para4[0]; - //qDebug() << "cmd4 restored to send from [0]; pp=" << nrOfCmds4InQueue; - //qDebug() << " data1: " << nextCmd4para1[0] << " data2: " << nextCmd4para2[0] << - // " data3: " << nextCmd4para3[0] << " data4: " << nextCmd4para4[0]; - - // move Puffer down by one element - if (CMD4STACKDEPTH>0) - ll=CMD4STACKDEPTH-1; - else - ll=0; - for (nn=0; nn0) - nrOfCmds4InQueue--; - //qDebug() << "cmd4 after push down: pp=" << nrOfCmds4InQueue; - return nxtAsynchCmd; -} - - - - - -static uint16_t nextAsynchsendCmd8[CMD8STACKDEPTH]; -static uint8_t nextCmd8para1[CMD8STACKDEPTH]; -static uint8_t nextCmd8para2[CMD8STACKDEPTH]; -static uint16_t nextCmd8para3[CMD8STACKDEPTH]; -static uint32_t nextCmd8para4[CMD8STACKDEPTH]; -static uint8_t nrOfCmds8InQueue; - -void sendWRcmd_clrCmd8Stack(void) -{ - uint8_t nn; - for (nn=0; nn=CMD8STACKDEPTH) - { - qDebug() << "cannot save cmd because stack is full"; - return false; // not possible - } - nextAsynchsendCmd8[nrOfCmds8InQueue]=nextCmd; - nextCmd8para1[nrOfCmds8InQueue]=dat1; - nextCmd8para2[nrOfCmds8InQueue]=dat2; - nextCmd8para3[nrOfCmds8InQueue]=dat3; - nextCmd8para4[nrOfCmds8InQueue]=dat4; - nrOfCmds8InQueue++; + return sendWRcmd_setSendCmd(SharedMemBuffer::getData()->Cmd8, nextCmd, dat1, + dat2, dat3, dat4); +} + +uint16_t sendWRcmd_getSendCommand0(uint8_t *dat1, uint8_t *dat2, + uint8_t *dat3, uint8_t *dat4) { + return sendWRcmd_getSendCmd(SharedMemBuffer::getData()->Cmd0, dat1, dat2, + dat3, dat4); +} +uint16_t sendWRcmd_getSendCommand4(uint8_t *dat1, uint8_t *dat2, + uint8_t *dat3, uint8_t *dat4) { + return sendWRcmd_getSendCmd(SharedMemBuffer::getData()->Cmd4, dat1, dat2, + dat3, dat4); +} +uint16_t sendWRcmd_getSendCommand8(uint8_t *dat1, uint8_t *dat2, + uint16_t *dat3, uint32_t *dat4) { + return sendWRcmd_getSendCmd(SharedMemBuffer::getData()->Cmd8, dat1, dat2, + dat3, dat4); +} + +bool sendWRcmd_setSendBlock160(uint8_t leng, uint8_t const *buf) { + SharedMemBuffer::getData()->next.sendAsyDatLen = std::min(leng, (uint8_t)160); + clear(SharedMemBuffer::getData()->next.sendAsynchDataBuf); + memcpy((char *)(&SharedMemBuffer::getData()->next.sendAsynchDataBuf), + (char const *)(buf), + SharedMemBuffer::getDataConst()->next.sendAsyDatLen); return true; // ok, will be sent } - -uint16_t sendWRcmd_getSendCommand8(uint8_t *dat1, uint8_t *dat2, uint16_t *dat3, uint32_t *dat4) -{ - uint16_t nxtAsynchCmd; - uint8_t nn, ll; - - if (nrOfCmds8InQueue==0 || nrOfCmds8InQueue>CMD8STACKDEPTH) - return 0; // error - nxtAsynchCmd=nextAsynchsendCmd8[0]; - *dat1=nextCmd8para1[0]; - *dat2=nextCmd8para2[0]; - *dat3=nextCmd8para3[0]; - *dat4=nextCmd8para4[0]; - - // move buffer down by one element - if (CMD8STACKDEPTH>0) - ll=CMD8STACKDEPTH-1; - else - ll=0; - for (nn=0; nn0) - nrOfCmds8InQueue--; - return nxtAsynchCmd; -} - - - - - - - -static uint8_t sendAsynchDataBuf[160]; // no stack, only ONE buffer -static uint8_t sendAsyDatLen; - -bool sendWRcmd_setSendBlock160(uint8_t leng, uint8_t *buf) -{ - //qDebug() << "pi epi: storing send data"; - if (leng>160) leng=160; - sendAsyDatLen=leng; - tslib_strclr(sendAsynchDataBuf, 0, 160); - for (uint8_t nn=0; nnnext.sendAsyDatLen; + memcpy((char *)buf, + (char const *)(SharedMemBuffer::getData()->next.sendAsynchDataBuf), + *leng); + SharedMemBuffer::getData()->next.sendAsyDatLen = 0; return *leng; } @@ -290,184 +164,141 @@ uint8_t sendWRcmd_getSendBlock160(uint8_t *leng, uint8_t *buf) // start with: SENDDIRCMD_EXCHGMDB, // send crude data from here to DC, DC to mdb slaves, mdb answer, return here within 50ms - -static uint8_t Sdata_mdbSendBuffer[64]; -static uint8_t Sdata_mdbSendLen; - -uint8_t epi_store64ByteSendData(uint8_t length, uint8_t *buf) -{ +uint8_t epi_store64ByteSendData(uint8_t length, uint8_t const *buf) { // HWapi writes data to be forwarded to DC and further to mdb-device - for (uint8_t nn=0; nnSdata.mdbSendBuffer[0]), + (char const *)buf, length); + SharedMemBuffer::getData()->Sdata.mdbSendLen = length; return 0; } -uint8_t gpi_restore64ByteSendData(uint8_t *length, uint8_t *buf) -{ +uint8_t gpi_restore64ByteSendData(uint8_t *length, uint8_t *buf) { // datif reads data to forward to dc - for (uint8_t nn=0; nnSdata.mdbSendBuffer[0]), + SharedMemBuffer::getDataConst()->Sdata.mdbSendLen); + *length = SharedMemBuffer::getDataConst()->Sdata.mdbSendLen; + SharedMemBuffer::getData()->Sdata.mdbSendLen = 0; return 0; } - - - - - //------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------ //---------------------------------------- Printer Text Fifo ------------------------- -static uint8_t prnDataParameters[4]; -static uint8_t prnDataBufferUser; - -void epi_storeUserOfSendingTextBuffer(uint8_t user, uint8_t para1, uint8_t para2, uint8_t para3, uint8_t para4 ) -{ +void epi_storeUserOfSendingTextBuffer(uint8_t user, uint8_t para1, + uint8_t para2, uint8_t para3, + uint8_t para4 ) { // user=1: Text-Print is using this buffer // 2: QR-code-Printer is using this buffer - prnDataBufferUser=user; - prnDataParameters[0]=para1; - prnDataParameters[1]=para2; - prnDataParameters[2]=para3; - prnDataParameters[3]=para4; - -// qDebug() << "new user stored: " << user; - + SharedMemBuffer::getData()->prn.DataBufferUser = user; + SharedMemBuffer::getData()->prn.DataParameters[0] = para1; + SharedMemBuffer::getData()->prn.DataParameters[1] = para2; + SharedMemBuffer::getData()->prn.DataParameters[2] = para3; + SharedMemBuffer::getData()->prn.DataParameters[3] = para4; } -uint8_t gpi_getUserOfSendingTextBuffer(uint8_t *para1, uint8_t *para2, uint8_t *para3, uint8_t *para4) -{ +uint8_t gpi_getUserOfSendingTextBuffer(uint8_t *para1, uint8_t *para2, + uint8_t *para3, uint8_t *para4) { // user=1: Text-Print is using this buffer // 2: QR-code-Printer is using this buffer //qDebug() << "returning user "<< prnDataBufferUser; - - *para1=prnDataParameters[0]; - *para2=prnDataParameters[1]; - *para3=prnDataParameters[2]; - *para4=prnDataParameters[3]; - return prnDataBufferUser; - + *para1 = SharedMemBuffer::getDataConst()->prn.DataParameters[0]; + *para2 = SharedMemBuffer::getDataConst()->prn.DataParameters[1]; + *para3 = SharedMemBuffer::getDataConst()->prn.DataParameters[2]; + *para4 = SharedMemBuffer::getDataConst()->prn.DataParameters[3]; + return SharedMemBuffer::getDataConst()->prn.DataBufferUser; } - - // Sending Text Fifo +// Sending Text Fifo // ONE printer doc consists of 20 x 64 byte // #define MAXNROF_PRNBYTES 64 // #define MAXNROF_PRNBLOCKS 20 -static char Sdata_PRN_TEXT[MAXNROF_PRNBLOCKS][MAXNROF_PRNBYTES]; -static uint8_t pPrnDataBuff; // points to next PRINTER_BLOCK - +// static char Sdata_PRN_TEXT[MAXNROF_PRNBLOCKS][MAXNROF_PRNBYTES]; +// static uint8_t pPrnDataBuff; // points to next PRINTER_BLOCK //static uint8_t pPrnDataBuff; // points to next waiting printer text // defined above, needed if more then one text is stored (before sent) // every block will be sent after 100ms, if 8 blocks are stored within this 100ms // then pointer goes up to 8. Important: FIFO!!!!!!!! -void epi_resetPrinterStack(void) -{ - pPrnDataBuff=0; +void epi_resetPrinterStack(void) { + SharedMemBuffer::getData()->prn.pDataBuff = 0; } -uint8_t epi_storePrnText(char *buf, uint8_t leng) -{ +uint8_t epi_storePrnText(char *buf, uint8_t leng) { // store text from Gui in next higher free memory 0....9 - uint16_t len; - uint8_t pp, nn; + uint16_t const len = std::min(leng, (uint8_t)MAXNROF_PRNBYTES); + int const pp = SharedMemBuffer::getDataConst()->prn.pDataBuff; - pp=pPrnDataBuff; // next free memory block with 64byte each - if (pp>=MAXNROF_PRNBLOCKS) - return 1; // not possible, no free mem + // next free memory block with 64byte each + if (pp < MAXNROF_PRNBLOCKS) { + clear(SharedMemBuffer::getData()->Sdata.PRN_TEXT[pp]); + // copy new text into buffer + memcpy((char *)(&SharedMemBuffer::getData()->Sdata.PRN_TEXT[pp][0]), + (char const *)(buf), len); + // inc pointer if end not yet reached + SharedMemBuffer::getData()->prn.pDataBuff++; + return 0; // OK + } - //len=tslib_strlen(buf); // kennt keine Binärzeichen!!!!!! - len=leng; - if (len>MAXNROF_PRNBYTES) - len=MAXNROF_PRNBYTES; - - tslib_strclr(Sdata_PRN_TEXT[pp], 0, MAXNROF_PRNBYTES); - - for (nn=0; nnprn.pDataBuff; - uint8_t nn, pp=pPrnDataBuff; + if (pp > 0) { // next free memory block with 64byte each + // example: pp=5: then buffers [0...4] are occupied - if (pp==0) // next free memory block with 64byte each - return 1; // no text in buffer + memcpy((char *)retbuf, // restore oldest text + (char const *)(&SharedMemBuffer::getData()->Sdata.PRN_TEXT[0][0]), + sizeof(SharedMemBuffer::getDataConst()->Sdata.PRN_TEXT[0])); - // example: pp=5: then buffers [0...4] are occupied + // now copy textline [1] to [0], then + // copy textline [2] to [1], then + // copy textline [3] to [2] .... upto [pp-1] to [pp-2] + // hint: copying from 9....0 would delete all strings!!!!!! - for (nn=0; nnSdata.PRN_TEXT[nn+1][0]), + (char const *)(&SharedMemBuffer::getDataConst()->Sdata.PRN_TEXT[nn][0]), + sizeof(SharedMemBuffer::getDataConst()->Sdata.PRN_TEXT[0])); + } - // now copy textline [1] to [0], then - // copy textline [2] to [1], then - // copy textline [3] to [2] .... upto [pp-1] to [pp-2] - // hint: copying from 9....0 would delete all strings!!!!!! + if (SharedMemBuffer::getDataConst()->prn.pDataBuff > 0) { + SharedMemBuffer::getData()->prn.pDataBuff--; + } - for (nn=0; nn<(pp-1); nn++) - tslib_strcpy(Sdata_PRN_TEXT[nn+1], Sdata_PRN_TEXT[nn], MAXNROF_PRNBYTES); - if (pPrnDataBuff>0) - pPrnDataBuff--; - pp=pPrnDataBuff; - // example: pp=4: then buffers [0...3] are still occupied, pp=0: all buffers empty + pp = SharedMemBuffer::getDataConst()->prn.pDataBuff; + // example: pp=4: then buffers [0...3] are still occupied, pp=0: all buffers empty - // now clear highest copyed line (which got free now) - tslib_strclr(Sdata_PRN_TEXT[pp], 0, MAXNROF_PRNBYTES); + // now clear highest copyed line (which got free now) + clear(SharedMemBuffer::getData()->Sdata.PRN_TEXT[pp]); - // optionally: clear all remaining higher lines: - for (nn=(pp+1); nnSdata.PRN_TEXT[nn]); + } + return 0; + } - return 0; + return 1; // no text in buffer } -uint8_t gpi_chk4remainingText(void) -{ +uint8_t gpi_chk4remainingText(void) { // retval: 0: no more textline left (to send) >0: nr of 64byte-blocks - return (pPrnDataBuff); + return SharedMemBuffer::getDataConst()->prn.pDataBuff; } - - - - - - - - - // --------------------------------------------------------------------------------- // 11.4.23 neu, Kommando direkt an "FastDevice"-protokoll senden, nicht mehr umsetzen // --------------------------------------------------------------------------------- - -// short command, 4 data bytes -static uint8_t nextFDwrCmd[FDCMD_STACKDEPTH]; -static uint8_t nextFDrdCmd[FDCMD_STACKDEPTH]; -static uint8_t nextFDblkNr[FDCMD_STACKDEPTH]; - -static uint8_t nextFDpara1[FDCMD_STACKDEPTH]; -static uint8_t nextFDpara2[FDCMD_STACKDEPTH]; -static uint8_t nextFDpara3[FDCMD_STACKDEPTH]; -static uint8_t nextFDpara4[FDCMD_STACKDEPTH]; -static uint8_t p_nextFDcmdsInQueue; /* convention: use simple (not rotating) FIFO Stack: Example: nrOfCmdsInQueue=4 then nextAsynchsendCmd0[0]=cmd1 // was stored as first @@ -483,191 +314,144 @@ Example: nrOfCmdsInQueue=4 then nrOfCmdsInQueue=3 now */ - -void sendFDcmd_clrStack(void) -{ - uint8_t nn; - for (nn=0; nnFDShort.wrCmd); + clear(SharedMemBuffer::getData()->FDShort.rdCmd); + clear(SharedMemBuffer::getData()->FDShort.blkNr); + clear(SharedMemBuffer::getData()->FDShort.para1); + clear(SharedMemBuffer::getData()->FDShort.para2); + clear(SharedMemBuffer::getData()->FDShort.para3); + clear(SharedMemBuffer::getData()->FDShort.para4); + SharedMemBuffer::getData()->FDShort.cmdsInQueue = 0; } -bool sendFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, uint8_t dat1, uint8_t dat2, uint8_t dat3, uint8_t dat4) -{ +bool sendFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, + uint8_t dat1, uint8_t dat2, uint8_t dat3, uint8_t dat4) { // write Command to memory, wait for transport - if (p_nextFDcmdsInQueue>=FDCMD_STACKDEPTH) - { - qDebug() << "cannot save cmd because stack is full"; - return false; // not possible + int const c = SharedMemBuffer::getDataConst()->FDShort.cmdsInQueue; + if (c < FDCMD_STACKDEPTH) { + SharedMemBuffer::getData()->FDShort.wrCmd[c] = nextWrCmd; + SharedMemBuffer::getData()->FDShort.rdCmd[c] = nextRdCmd; + SharedMemBuffer::getData()->FDShort.blkNr[c] = blockNum; + SharedMemBuffer::getData()->FDShort.para1[c] = dat1; + SharedMemBuffer::getData()->FDShort.para2[c] = dat2; + SharedMemBuffer::getData()->FDShort.para3[c] = dat3; + SharedMemBuffer::getData()->FDShort.para4[c] = dat4; + SharedMemBuffer::getData()->FDShort.cmdsInQueue = (c + 1); + return true; // ok, will be sent } - nextFDwrCmd[p_nextFDcmdsInQueue]=nextWrCmd; - nextFDrdCmd[p_nextFDcmdsInQueue]=nextRdCmd; - nextFDblkNr[p_nextFDcmdsInQueue]=blockNum; - nextFDpara1[p_nextFDcmdsInQueue]=dat1; - nextFDpara2[p_nextFDcmdsInQueue]=dat2; - nextFDpara3[p_nextFDcmdsInQueue]=dat3; - nextFDpara4[p_nextFDcmdsInQueue]=dat4; - //qDebug() << "data with 4 data byte saved, pp=" << nrOfCmds4InQueue; - //qDebug() << " dat1=" << nextCmd4para1[nrOfCmds4InQueue] << " dat2=" << nextCmd4para2[nrOfCmds4InQueue] - // << " dat3=" << nextCmd4para3[nrOfCmds4InQueue] << " dat4=" << nextCmd4para4[nrOfCmds4InQueue]; - p_nextFDcmdsInQueue++; - return true; // ok, will be sent + qCritical() << "cannot save cmd because stack is full"; + return false; // not possible } -bool sendFDcmd_get(uint8_t *nextWrCmd, uint8_t *nextRdCmd, uint8_t *blockNum, uint8_t *dat1, uint8_t *dat2, uint8_t *dat3, uint8_t *dat4) -{ - uint8_t nn, ll; +bool sendFDcmd_get(uint8_t *nextWrCmd, uint8_t *nextRdCmd, uint8_t *blockNum, + uint8_t *dat1, uint8_t *dat2, uint8_t *dat3, uint8_t *dat4) { - if (p_nextFDcmdsInQueue==0 || p_nextFDcmdsInQueue>FDCMD_STACKDEPTH) - return false; // not possible + int const c = SharedMemBuffer::getDataConst()->FDShort.cmdsInQueue; - *nextWrCmd=nextFDwrCmd[0]; - *nextRdCmd=nextFDrdCmd[0]; - *blockNum=nextFDblkNr[0]; - *dat1=nextFDpara1[0]; - *dat2=nextFDpara2[0]; - *dat3=nextFDpara3[0]; - *dat4=nextFDpara4[0]; - //qDebug() << "cmd4 restored to send from [0]; pp=" << nrOfCmds4InQueue; - //qDebug() << " data1: " << nextCmd4para1[0] << " data2: " << nextCmd4para2[0] << - // " data3: " << nextCmd4para3[0] << " data4: " << nextCmd4para4[0]; + if ((c > 0) && (c <= FDCMD_STACKDEPTH)) { + *nextWrCmd = SharedMemBuffer::getDataConst()->FDShort.wrCmd[c]; + *nextRdCmd = SharedMemBuffer::getDataConst()->FDShort.rdCmd[c]; + *blockNum = SharedMemBuffer::getDataConst()->FDShort.blkNr[c]; + *dat1 = SharedMemBuffer::getDataConst()->FDShort.para1[c]; + *dat2 = SharedMemBuffer::getDataConst()->FDShort.para2[c]; + *dat3 = SharedMemBuffer::getDataConst()->FDShort.para3[c]; + *dat4 = SharedMemBuffer::getDataConst()->FDShort.para4[c]; - // move Puffer down by one element - if (FDCMD_STACKDEPTH>0) - ll=FDCMD_STACKDEPTH-1; - else - ll=0; - for (nn=0; nnFDShort.wrCmd[m] = SharedMemBuffer::getDataConst()->FDShort.wrCmd[n]; + SharedMemBuffer::getData()->FDShort.rdCmd[m] = SharedMemBuffer::getDataConst()->FDShort.rdCmd[n]; + SharedMemBuffer::getData()->FDShort.blkNr[m] = SharedMemBuffer::getDataConst()->FDShort.blkNr[n]; + SharedMemBuffer::getData()->FDShort.para1[m] = SharedMemBuffer::getDataConst()->FDShort.para1[n]; + SharedMemBuffer::getData()->FDShort.para2[m] = SharedMemBuffer::getDataConst()->FDShort.para2[n]; + SharedMemBuffer::getData()->FDShort.para3[m] = SharedMemBuffer::getDataConst()->FDShort.para3[n]; + SharedMemBuffer::getData()->FDShort.para4[m] = SharedMemBuffer::getDataConst()->FDShort.para4[n]; + } + SharedMemBuffer::getData()->FDShort.cmdsInQueue = c - 1; + return true; // ok, will be sent } - if (p_nextFDcmdsInQueue>0) - p_nextFDcmdsInQueue--; - //qDebug() << "cmd4 after push down: pp=" << nrOfCmds4InQueue; - return true; // ok, will be sent + return false; // not possible } -uint8_t check4FDshortCmd(void) -{ - // returns number of waiting command, max FDCMD_STACKDEPTH - return p_nextFDcmdsInQueue; +uint8_t check4FDshortCmd(void) { // returns number of waiting command, max FDCMD_STACKDEPTH + return SharedMemBuffer::getDataConst()->FDShort.cmdsInQueue; } -uint8_t check4freeFDshortCmd(void) -{ - // returns number of free places in short-command stack - return FDCMD_STACKDEPTH - p_nextFDcmdsInQueue; +uint8_t check4freeFDshortCmd(void) { // returns number of free places in short-command stack + return FDCMD_STACKDEPTH - SharedMemBuffer::getDataConst()->FDShort.cmdsInQueue; } - -// long command, 64 data bytes -static uint8_t longFDwrCmd[FDLONG_STACKDEPTH]; -static uint8_t longFDrdCmd[FDLONG_STACKDEPTH]; -static uint8_t longFDblkNr[FDLONG_STACKDEPTH]; -static uint8_t longFDlength[FDLONG_STACKDEPTH]; - -static uint8_t longFDpara[FDLONG_STACKDEPTH][64]; -static uint8_t p_longFDcmdsInQueue; - - -void longFDcmd_clrStack(void) -{ - uint8_t nn, mm; - for (nn=0; nnFDLong.wrCmd); + clear(SharedMemBuffer::getData()->FDLong.rdCmd); + clear(SharedMemBuffer::getData()->FDLong.blkNr); + clear(SharedMemBuffer::getData()->FDLong.length); + clear(SharedMemBuffer::getData()->FDLong.para); + SharedMemBuffer::getData()->FDLong.cmdsInQueue = 0; } - -bool longFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, uint8_t length, uint8_t *data) -{ +bool longFDcmd_set(uint8_t nextWrCmd, uint8_t nextRdCmd, uint8_t blockNum, + uint8_t length, uint8_t const *data) { // write Command to memory, wait for transport // data buffer size always 64! data[64], padded with 0 - uint8_t nn; - if (p_longFDcmdsInQueue>=FDLONG_STACKDEPTH) - { - qDebug() << "cannot save cmd because stack is full"; - return false; // not possible + int const c = SharedMemBuffer::getDataConst()->FDLong.cmdsInQueue; + + if (c > 0 && c <= FDLONG_STACKDEPTH) { + SharedMemBuffer::getData()->FDLong.wrCmd[c] = nextWrCmd; + SharedMemBuffer::getData()->FDLong.rdCmd[c] = nextRdCmd; + SharedMemBuffer::getData()->FDLong.blkNr[c] = blockNum; + SharedMemBuffer::getData()->FDLong.length[c] = length; + memcpy((char *)SharedMemBuffer::getData()->FDLong.para[c], + (char const *)data, + sizeof(SharedMemBuffer::getDataConst()->FDLong.para[0])); + + SharedMemBuffer::getData()->FDLong.cmdsInQueue = c + 1; + return true; // ok, will be sent } - longFDwrCmd[p_longFDcmdsInQueue]=nextWrCmd; - longFDrdCmd[p_longFDcmdsInQueue]=nextRdCmd; - longFDblkNr[p_longFDcmdsInQueue]=blockNum; - longFDlength[p_longFDcmdsInQueue]=length; - for (nn=0; nn<64; nn++) - longFDpara[p_longFDcmdsInQueue][nn]=data[nn]; - - p_longFDcmdsInQueue++; - return true; // ok, will be sent + qCritical() << "cannot save cmd because stack is full"; + return false; // not possible } -bool longFDcmd_get(uint8_t *nextWrCmd, uint8_t *nextRdCmd, uint8_t *blockNum, uint8_t *length, uint8_t *data) -{ - uint8_t nn, mm, ll; +bool longFDcmd_get(uint8_t *nextWrCmd, uint8_t *nextRdCmd, uint8_t *blockNum, + uint8_t *length, uint8_t *data) { - if (p_longFDcmdsInQueue==0 || p_longFDcmdsInQueue>FDLONG_STACKDEPTH) - return false; // not possible + int const c = SharedMemBuffer::getDataConst()->FDLong.cmdsInQueue; - *nextWrCmd= longFDwrCmd[0]; - *nextRdCmd= longFDrdCmd[0]; - *blockNum = longFDblkNr[0]; - *length = longFDlength[0]; - for (mm=0; mm<64; mm++) - data[mm] = longFDpara[0][mm]; - - // move Puffer down by one element - if (FDLONG_STACKDEPTH>0) - ll=FDLONG_STACKDEPTH-1; - else - ll=0; - for (nn=0; nn 0 && c <= FDLONG_STACKDEPTH) { + *nextWrCmd = SharedMemBuffer::getDataConst()->FDLong.wrCmd[c]; + *nextRdCmd = SharedMemBuffer::getDataConst()->FDLong.rdCmd[c]; + *blockNum = SharedMemBuffer::getDataConst()->FDLong.blkNr[c]; + *length = SharedMemBuffer::getDataConst()->FDLong.length[c]; + memcpy((char *)data, + (char const *)SharedMemBuffer::getDataConst()->FDLong.para[c], + sizeof(SharedMemBuffer::getDataConst()->FDLong.para[0])); + // move Puffer down by one element + for (int m = 0; m < c-1; ++m) { + int const n = m + 1; + SharedMemBuffer::getData()->FDLong.wrCmd[m] = SharedMemBuffer::getDataConst()->FDLong.wrCmd[n]; + SharedMemBuffer::getData()->FDLong.rdCmd[m] = SharedMemBuffer::getDataConst()->FDLong.rdCmd[n]; + SharedMemBuffer::getData()->FDLong.blkNr[m] = SharedMemBuffer::getDataConst()->FDLong.blkNr[n]; + SharedMemBuffer::getData()->FDLong.length[m] = SharedMemBuffer::getData()->FDLong.length[n]; + memcpy((char *)SharedMemBuffer::getData()->FDLong.para[m], + (char const *)SharedMemBuffer::getData()->FDLong.para[n], + sizeof(SharedMemBuffer::getDataConst()->FDLong.para[0])); + } + SharedMemBuffer::getData()->FDLong.cmdsInQueue = c - 1; + return true; // ok, will be sent } - if (p_longFDcmdsInQueue>0) - p_longFDcmdsInQueue--; - return true; // ok, will be sent + qCritical() << "cannot save cmd because stack is full"; + return false; // not possible } -uint8_t check4FDlongCmd(void) -{ - // returns number of waiting command - return p_longFDcmdsInQueue; +uint8_t check4FDlongCmd(void) { // returns number of waiting command + return SharedMemBuffer::getDataConst()->FDLong.cmdsInQueue; } -uint8_t check4freeFDlongCmd(void) -{ - // returns number of free places in command stack - return FDLONG_STACKDEPTH - p_longFDcmdsInQueue; +uint8_t check4freeFDlongCmd(void) { // returns number of free places in command stack + return FDLONG_STACKDEPTH - SharedMemBuffer::getDataConst()->FDLong.cmdsInQueue; } - - diff --git a/src/serial_port_thread.cpp b/src/serial_port_thread.cpp new file mode 100644 index 0000000..3079007 --- /dev/null +++ b/src/serial_port_thread.cpp @@ -0,0 +1,15 @@ +#include "serial_port_thread.h" + + +SerialPortThread::SerialPortThread(QObject *parent) + : QThread(parent) { + this->setObjectName("SerialPortThread"); +} + +SerialPortThread::~SerialPortThread() { + m_loop.quit(); +} + +void SerialPortThread::run() { + m_loop.exec(); +} diff --git a/src/serial_port_worker.cpp b/src/serial_port_worker.cpp new file mode 100644 index 0000000..cc79ad2 --- /dev/null +++ b/src/serial_port_worker.cpp @@ -0,0 +1,244 @@ +#include "serial_port_worker.h" +#include "serial_port_thread.h" + +#include +#include + +std::atomic Command::m_nextSendBufferIndex{0}; +std::atomic Command::m_nextRecvBufferIndex{0}; +char Command::m_sendBuffers[256][SEND_BLOCK_SIZE]{{0}}; +char Command::m_recvBuffers[256][SEND_BLOCK_SIZE]{{0}}; + +uint32_t Command::getNextCommandPriority(uint8_t priorityClass) { + static std::atomic priority_1{0}; + static std::atomic priority_2{1000000000}; + static std::atomic priority_3{2000000000}; + static std::atomic priority_4{3000000000}; + switch (priorityClass) { + case PRIORITY_1: + return priority_1.fetch_add(1); + case PRIORITY_2: + return priority_2.fetch_add(1); + case PRIORITY_3: + return priority_3.fetch_add(1); + case PRIORITY_4: + return priority_4.fetch_add(1); + } + return -1; +} + +char *Command::getNextSendBuffer() { + uint8_t const nextIndex = m_nextSendBufferIndex.fetch_add(1); + return &m_sendBuffers[nextIndex][0]; +} + +char *Command::getNextRecvBuffer() { + uint8_t const nextIndex = m_nextRecvBufferIndex.fetch_add(1); + return &m_recvBuffers[nextIndex][0]; +} + +Command::Command(uint16_t cmdId, uint8_t sendBufferLength, + uint8_t recvBufferLength, uint8_t priorityClass) + : m_priority(getNextCommandPriority(priorityClass)) + , m_priorityClass(priorityClass) + , m_cmdId(cmdId) + , m_sendBuffer(Command::getNextSendBuffer()) + , m_recvBuffer(Command::getNextRecvBuffer()) + , m_sendBufferLength(sendBufferLength) + , m_recvBufferLength(recvBufferLength) { + memset(m_sendBuffer, 0x00, sizeof(m_sendBuffers[0])); +} + +Command::Command() + : m_priority(0) + , m_priorityClass(0) + , m_cmdId(0) + , m_sendBuffer(0) + , m_recvBuffer(0) + , m_sendBufferLength(0) + , m_recvBufferLength(0) { +} + +/** +* \brief Constructor for SerialPort +* +* @date 08.11.2022 – first draft. +* +*/ +SerialPortWorker::SerialPortWorker(QString portName, enum QSerialPort::BaudRate baudRate, QObject *parent) + : QThread(parent) + , m_serialPort() + , m_serialPortOpened(false) + , m_portName(portName) + , m_baudRate(baudRate) + , m_bytesWritten(0) { + this->setObjectName("SerialPortWorker"); + + foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { + if (!info.isNull()) { + qDebug() << "Name .......... " << info.portName(); + qDebug() << "Description ... " << info.description(); + qDebug() << "Manufacturer .. " << info.manufacturer(); + qDebug() << "SerialNumber .. " << info.serialNumber(); + if (info.hasProductIdentifier()) { + qDebug() << "Product-ID .... " << info.productIdentifier(); + } + if (info.hasVendorIdentifier()) { + qDebug() << "Vendor-ID ..... " << info.vendorIdentifier(); + } + qDebug() << "Location ...... " << info.systemLocation(); + + if (info.portName() == m_portName) { + m_serialPortInfo = info; + } + } + } + + if (m_serialPortInfo.portName() == m_portName) { + m_serialPort.setPort(m_serialPortInfo); + m_serialPort.setPortName(m_portName); + m_serialPort.setDataBits(QSerialPort::Data8); + m_serialPort.setParity(QSerialPort::NoParity); + m_serialPort.setStopBits(QSerialPort::OneStop); + m_serialPort.setFlowControl(QSerialPort::NoFlowControl); + m_serialPortOpened = m_serialPort.open(QIODevice::ReadWrite); + } +} + +SerialPortWorker::~SerialPortWorker() { + closeSerialPort(); +} + +void SerialPortWorker::run() { + SerialPortThread serialPortThread; + this->moveToThread(&serialPortThread); // not really necessary + + // Note: because this thread is moved to serialPortThread, the event loop + // used will be the one of serialPortThread (this-thread does not even have one). + // This is the only purpose of serialPortThread. + serialPortThread.start(); + + connect(&m_serialPort, SIGNAL(readyRead()), this, SLOT(getBytesAvailable())); + connect(&m_serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(getNumberOfBytesWritten(qint64))); + + while (m_serialPortOpened) { + Command nextCmd = getNextCommand(); + if (nextCmd.m_cmdId == Command::QUIT_SENTINEL) { + break; + } + QByteArray data(nextCmd.getNextSendBuffer(), nextCmd.m_sendBufferLength); + m_bytesWritten = 0; + m_serialPort.write(data); + // process events in event loop of associated thread + serialPortThread.getEventLoop().processEvents(QEventLoop::ExcludeUserInputEvents, TIMEOUT); + int maxWaitTime = WAIT_TIME_MAX; + + while (maxWaitTime > 0 && m_bytesWritten < (uint32_t)data.size()) { + if (!m_serialPort.waitForBytesWritten(100)) { // wait timeout-ms for the bytes to be written + // process events in event loop of associated thread + serialPortThread.getEventLoop().processEvents(QEventLoop::ExcludeUserInputEvents, TIMEOUT); + maxWaitTime -= TIMEOUT; + } + } + if (m_bytesWritten == (uint32_t)data.size()) { // data written, wait for result + if (nextCmd.m_recvBufferLength > 0) { + // process events in event loop of associated thread + serialPortThread.getEventLoop().processEvents(QEventLoop::ExcludeUserInputEvents, TIMEOUT); + maxWaitTime = WAIT_TIME_MAX; + while (maxWaitTime > 0 && m_bytesAvailable < nextCmd.m_recvBufferLength) { + if (!m_serialPort.waitForReadyRead(100)) { + // process events in event loop of associated thread + serialPortThread.getEventLoop().processEvents(QEventLoop::ExcludeUserInputEvents, TIMEOUT); + maxWaitTime -= TIMEOUT; + } + } + uint32_t const bytesAvailable = m_bytesAvailable; + if (bytesAvailable == nextCmd.m_recvBufferLength) { + QByteArray result = m_serialPort.read(bytesAvailable); + if (!result.isEmpty() && ((uint32_t)result.size() == bytesAvailable)) { + memcpy(nextCmd.m_recvBuffer, result.data(), bytesAvailable); + insertResult(nextCmd); + } + } else { + // error + } + } + } else { + // error + } + } // while (m_serialPortOpened) + + serialPortThread.quit(); + serialPortThread.wait(); +} + +bool SerialPortWorker::openSerialPort() { + if (!m_serialPortOpened) { + return m_serialPort.open(QIODevice::ReadWrite); + } + return false; // opening twice is not allowed +} + +void SerialPortWorker::closeSerialPort() { + if (m_serialPortOpened) { + m_serialPort.close(); + m_serialPortOpened = false; + } +} + +void SerialPortWorker::quit() { + Command cmd(Command::QUIT_SENTINEL, 0, 0, Command::PRIORITY_1); + insertCommand(cmd); +} + +void SerialPortWorker::insertCommand(Command const &cmd) { + { std::lock_guard lock(m_sendQueueMutex); + m_sendQueue.push(cmd); + } // release lock + m_sendQueueCondVar.notify_one(); +} + +Command SerialPortWorker::getNextCommand() { + Command nextCmd; + { std::unique_lock lock(m_sendQueueMutex); + m_sendQueueCondVar.wait(lock, [this]{ return !this->m_sendQueue.empty(); }); + nextCmd = m_sendQueue.top(); + m_sendQueue.pop(); + } // release lock + return nextCmd; +} + +void SerialPortWorker::insertResult(Command const &result) { + { std::lock_guard lock(m_recvQueueMutex); + m_recvQueue.push_back(result); + } // release lock + m_recvQueueCondVar.notify_one(); +} + +bool SerialPortWorker::getNextResult(Command &nextResult, int timeout) { + bool timed_out = false; + { std::unique_lock lock(m_recvQueueMutex); + if (m_recvQueueCondVar.wait_for(lock, std::chrono::milliseconds(timeout), + [this]{ return !this->m_recvQueue.empty(); })) { + nextResult = m_recvQueue.front(); + m_recvQueue.pop_front(); + return true; + } else { + timed_out = true; + } + } // release lock + return (timed_out == false); +} + +void SerialPortWorker::getBytesAvailable() { + m_bytesAvailable += m_serialPort.bytesAvailable(); +} + +bool SerialPortWorker::getNumberOfBytesWritten(qint64 bytesWritten) { + if (bytesWritten > 0) { + m_bytesWritten += (uint32_t)bytesWritten; + return true; + } + return false; +} + diff --git a/src/shared_mem_buffer.cpp b/src/shared_mem_buffer.cpp index 8026c19..4d790cd 100644 --- a/src/shared_mem_buffer.cpp +++ b/src/shared_mem_buffer.cpp @@ -1,6 +1,7 @@ #include "shared_mem_buffer.h" #include +#include #include #ifdef QT_POSIX_IPC @@ -12,6 +13,12 @@ #include // ftok #endif +#ifdef __linux__ +#include +#endif + +#define PROCESS_SHARED (PTHREAD_PROCESS_SHARED) + // std::atomic_bool SharedMemBuffer::__sharedMemLocked{false}; QSharedMemory *SharedMemBuffer::getShm(std::size_t size) { @@ -22,6 +29,83 @@ QSharedMemory *SharedMemBuffer::getShm(std::size_t size) { shMem.setKey(fkey); if (!shMem.isAttached()) { if (shMem.create(size)) { + // the creator is the owner of the shared-memory segment + SharedMemBuffer *shm = (SharedMemBuffer *)(shMem.data()); + if (shm) { + shm->init = 0; + // mark the shm + memset(shm->appName, 0x00, sizeof(shm->appName)); + strncpy(shm->appName, + QCoreApplication::applicationName().toLocal8Bit().constData(), + sizeof(shm->appName)-1); + shm->pid = getpid(); + + // create condition variables plus associated mutexes for + // sending/receiving commands to dc + int r = 0; + if ((r = pthread_mutexattr_init(&shm->sndMtxA)) != 0) { + qCritical() << "init mutex attribute" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_mutexattr_setpshared( + &shm->sndMtxA, PTHREAD_PROCESS_SHARED) != 0)) { + qCritical() << "set s-mutex attribute to process shared" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_mutex_init(&shm->sndMtx, + &shm->sndMtxA) != 0)) { + qCritical() << "init s-mutex" << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_condattr_init(&shm->sndCndA) != 0)) { + qCritical() << "init s-mutex" << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_condattr_setpshared( + &shm->sndCndA, PTHREAD_PROCESS_SHARED) != 0)) { + qCritical() << "init s-cond attribute to process shared" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_cond_init(&shm->sndCndV, + &shm->sndCndA) != 0)) { + qCritical() << "init s-condvar" << strerror(errno) << r; + } + + if ((r = pthread_mutexattr_init(&shm->rcvMtxA)) != 0) { + qCritical() << "init r-mutex attribute" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_mutexattr_setpshared( + &shm->rcvMtxA, PTHREAD_PROCESS_SHARED) != 0)) { + qCritical() << "set r-mutex attribute to process shared" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_mutex_init(&shm->rcvMtx, + &shm->rcvMtxA) != 0)) { + qCritical() << "init r-mutex" << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_condattr_init(&shm->rcvCndA) != 0)) { + qCritical() << "init r-mutex" << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_condattr_setpshared( + &shm->rcvCndA, PTHREAD_PROCESS_SHARED) != 0)) { + qCritical() << "init r-cond attribute to process shared" + << strerror(errno) << r; + } + if ((r == 0) && + (r = pthread_cond_init(&shm->rcvCndV, + &shm->rcvCndA) != 0)) { + qCritical() << "init s-condvar" << strerror(errno) << r; + } + shm->init = 1; + } return &shMem; } else { if (shMem.error() == QSharedMemory::AlreadyExists) {