DCLibraries/src/datIf.cpp

1378 lines
48 KiB
C++

/*
History:
06.12.2023: weitere IN-requests in Autorequest übernommen
Umschaltung mit/ohne Verkauf, während VK nur 12 wichtige, sonst alle 36
*/
#include "datIf.h"
#include "sendWRcmd.h"
#include "controlBus.h"
#include "storeINdata.h"
#include <QDebug>
#include <datei.h>
#include <QDir>
// 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;
static bool doRepeat;
static uint8_t keepLastWrCmd, keepLastRdCmd;
static uint8_t keepLastblockNum, keepLastdat1, keepLastdat2, keepLastdat3, keepLastdat4;
static uint8_t keepLastlength, keepLastdata64[66];
// new, 17.7.23:
static uint8_t datif_cmdWasPerformed;
static uint8_t datif_repeatCtr;
static uint8_t datif_kindOfCmd;
static uint8_t datif_pNextCmd, datif_sendSlowCmd;
//#define DATIF_MAXTO_WAIT4RESP 80 //20 erhöht am 17.7 geht viel besser
// höchster gemessener Wert (bei 20ms): 6
//#define DATIF_GAP4RESP 1
// weniger als 1 geht nicht. erzeugt eine Lücke zw. 20..65ms (2 Programmzyklen)
//#define DATIF_CTR_GOTRESPVAL 100
T_datif::T_datif(QObject *parent) : QObject(parent)
{
QByteArray myBA;
QDir myDir("../dmd");
if (!myDir.exists())
myDir.mkdir("../dmd");
myDCIF = new T_prot();
// valid data was received, storing
connect(myDCIF, SIGNAL(framerecieved()), this, SLOT(StoredRecData()));
// new, 9.11.20 data for DC-bootloader received not matchind Pprot
//connect(myDCIF, SIGNAL(rawDataRecieved()), this, SLOT(BLdataRecData()));
// cyclic transmission of INPUT-Requests
datif_trigger = new QTimer();
connect(datif_trigger, SIGNAL(timeout()), this, SLOT(datif_cycleSend()));
datif_trigger->setSingleShot(false);
datif_trigger->start(20); // in ms,
// passing Signal through
//connect(myDCIF, SIGNAL(framerecieved()), this, SLOT( ResponseRecieved() ));
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;
epi_resetDcDataValid(); // data are not yet valid, no response from DC by now
datif_noResponseCtr=0;
datif_repeatCtr=0;
datif_cmdWasPerformed=0; // 0: no response by now
datif_kindOfCmd=0;
gpi_storeOverallResult(0xFF);
// neu, 24.5.23: Alle Daten zusätzlich in Datei speichern
// if (datei_ifFileExists(FILENAME_SHAREDDATA))
// {
// myBA.clear();
// myBA=datei_readFromFile(FILENAME_SHAREDDATA);
// }
/*
// Testfile ereugen:
csv_startCreatingFile();
csv_addUintToFile(0);
csv_addUintToFile(1);
csv_addUintToFile(2);
csv_addUintToFile(3);
csv_addUintToFile(4);
csv_addUintToFile(5);
myBA=csv_readbackArray();
datei_clearFile(FILENAME_SHAREDDATA);
datei_writeToFile(FILENAME_SHAREDDATA, myBA);
*/
doRepeat=true;
datif_pNextCmd=0;
datif_sendSlowCmd=0;
}
void T_datif::resetChain(void)
{
dif_scanStep=0;
}
char T_datif::datif_cycleSend()
{
// cyclic transmission of INPUT-Requests
// call cyclic every 20ms to send next request , then wait for response before sending again!!!
//uint16_t nxtAsCmd;
uint8_t dataSendBuf[160], dataBufLen, dbl;
//static uint8_t BlockCounter;
uint8_t nextWrCmd, nextRdCmd, blockNum, dat1, dat2, dat3, dat4;
uint8_t length, data[66];
bool b_ret;
// send next command
if ( !myDCIF->isPortOpen())
{
//qDebug() << "com port not available"; // wird ununterbrochen ausgegeben
epi_resetDcDataValid(); // DC data not valid
return 0;
}
// supervise if DC data are valid
datif_noResponseCtr++; // inc every 20ms
if (datif_noResponseCtr>250) // no life sign from device controller (DC) for about 3s
epi_resetDcDataValid(); // DC data has not updated for >=5s -> no longer valid!
// Ueberwachung ob ein oder mehrere Commands am Stueck erfolgreich waren
if (gpi_wantToResetSupervision())
{
gpi_storeOverallResult(0xFF);
}
// wait for response ----------------------------------------------------------------------------
// "cycl_running" entscheidet ob ein neues Kommando gesendet werden darf oder ob wir noch auf die Antwort des letzten warten
// bei der Antwort gibt's drei Faelle:
// a) Antwort OK --> nach einer Luecke von 10ms neues Kommando schicken
// b) Antwort meldet Fehler -> 2x wiederholen (nach einer Luecke von 10ms )
// c) gar keine Antwort, Timeout nach 100ms -> 2x wiederholen (nach einer Luecke von 10ms )
// cycl_running=0: nichts zu tun 1: Mitteilung: Kommando wurde soeben abgesendet, 2,3,4 = Wiederholung
if (cycl_running) // 21.9.23 doRepeat hier raus sonst gehts warten auch nicht mehr (BL)
// if (cycl_running && doRepeat)
{
// request is still running, wait for response before next sending
//qDebug()<< "datif wait for response";
datif_trigger->stop();
datif_trigger->start(20); // ruft "this" (datif_cycleSend) erneut in 20ms auf
// mit 10 kein Unterscheid weil Zykluszeit grösser
cycl_running++; // inc every 20...30ms // warte max 100ms auf Antwort
if (cycl_running >80 && cycl_running <95) // neu 13.9.23 mind. 40 damit Templates
// in Folge gedruckt werden koennen
// 95: muss nur kleiner sein als die 100 fuer die Luecke
// 17.10.23: 50--> 80
{
// 100ms vergangen, bisher keine Antwort, also Kommando wiederholen
qDebug()<< "datif timeout no response for wr/rd cmd "<< keepLastWrCmd << " " << keepLastRdCmd;
cycl_running = 0; // gleich wiederholen weil ja schon ewig nichts mehr reinkam
datif_cmdWasPerformed=2; // NO :((
gpi_storeLastResult(8);
}
if (cycl_running>=101) // 100 + 1
{
// Antwort ist gekommen, also nach kurzer Luecke naechstes (datif_cmdWasPerformed==1)
// oder nochmal gleiches (datif_cmdWasPerformed==2) Kommando senden
//qDebug()<< "datif got any response";
cycl_running=0;
}
// hier stoppen, weil Antwort des letzten Cmds noch nicht da
return 0;
}
// 17.7.2023: repeat commands if result was !=OK -------------------------------------------------------------------
if (datif_cmdWasPerformed==2 && doRepeat) // Cmd was not or false performed und Wiederholen erwuenscht
{
//qDebug()<<"datif: repeating...";
datif_repeatCtr++;
if (datif_repeatCtr>3)
{
// give in, 3x no or wrong response
datif_cmdWasPerformed=0;
datif_kindOfCmd=0;
cycl_running=0;
gpi_storeOverallResult(2);
return 0;
}
// repeat last command as we got no or bad result (error)
if (datif_kindOfCmd==2)
{
//qDebug() << "datif: repeat long FDcmd (wr/rd): " <<keepLastWrCmd<< "/" << keepLastRdCmd;
myDCIF->setUserWriteData(keepLastWrCmd, keepLastblockNum, keepLastlength, keepLastdata64);
myDCIF->setUserReadData(keepLastRdCmd);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1; // 1: start transmission and reset TOcounter
datif_cmdWasPerformed=0;
} else
if (datif_kindOfCmd==1)
{
//qDebug() << "datif: repeat short FDcmd (wr/rd): " <<keepLastWrCmd<< "/" << keepLastRdCmd;
data[0]=keepLastdat1; data[1]=keepLastdat2; data[2]=keepLastdat3; data[3]=keepLastdat4; data[4]=0;
myDCIF->setUserWriteData(keepLastWrCmd, keepLastblockNum, 4, data);
myDCIF->setUserReadData(keepLastRdCmd);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1; // 1: start transmission
datif_cmdWasPerformed=0;
} else
{
qDebug() << "datif: unknown cmd, stop repeating wr/rd:" << keepLastWrCmd << " " << keepLastRdCmd;
datif_cmdWasPerformed=0;
cycl_running=0;
datif_kindOfCmd=0;
}
return 0;
} // end of repeat commands
// Send new command ---------------------------------------------------------------
// from 11.4.23: direkt access to fastDevice Interface
// send long fast direct command
if (checkNextFDcmd()==2)
{
// return 0: no command waiting
// 1: short cmd
// 2: long cmd
gpi_storeOverallResult(0);
b_ret=longFDcmd_get(&nextWrCmd, &nextRdCmd, &blockNum, &length, data);
if (b_ret)
{
keepLastWrCmd=nextWrCmd; keepLastRdCmd=nextRdCmd;
keepLastblockNum=blockNum;
keepLastlength=length;
for (int ii=0; ii<66; ii++)
keepLastdata64[ii]=data[ii];
myDCIF->setUserWriteData(nextWrCmd, blockNum, length, data);
myDCIF->setUserReadData(nextRdCmd);
myDCIF->sendUserData(selectedSlaveAddr);
//qDebug()<<"Datif send long FD cmd (wr/rd): "<<nextWrCmd<< " / " << nextRdCmd
// << " " << data[0]<< " " << data[1]<< " " << data[2]<< " " << data[3];
cycl_running=1; // 1: start transmission
datif_kindOfCmd=2;
}
datif_repeatCtr=0;
doRepeat=true;
return 0;
} else
// send short fast direct command
if (checkNextFDcmd()==1)
{
gpi_storeOverallResult(0);
b_ret=sendFDcmd_get(&nextWrCmd, &nextRdCmd, &blockNum, &dat1, &dat2, &dat3, &dat4);
if (b_ret)
{
keepLastWrCmd=nextWrCmd; keepLastRdCmd=nextRdCmd;
data[0]=dat1; data[1]=dat2; data[2]=dat3; data[3]=dat4; data[4]=0;
keepLastblockNum=blockNum;
keepLastdat1=dat1; keepLastdat2=dat2; keepLastdat3=dat3; keepLastdat4=dat4;
myDCIF->setUserWriteData(nextWrCmd, blockNum, 4, data);
myDCIF->setUserReadData(nextRdCmd);
myDCIF->sendUserData(selectedSlaveAddr);
//qDebug()<<"Datif send short FD cmd (wr/rd): "<<nextWrCmd<< " / " << nextRdCmd << " "
// << blockNum << " " << dat1 << " " << dat2<< " " << dat3<< " " << dat4;
cycl_running=1; // 1: start transmission
datif_kindOfCmd=1;
}
datif_repeatCtr=0;
doRepeat=true;
return 0;
}
// send data to DC-Bootloader
dbl=sendWRcmd_getSendBlock160(&dataBufLen, dataSendBuf);
if (dbl>0)
{
cycl_running=1; // 1: start transmission
//qDebug() << "datif: sending " << dataBufLen << " bytes to BL: "
// << QTime::currentTime().toString(Qt::ISODateWithMs) <<" "
// << QByteArray((const char*)dataSendBuf,dataBufLen).toHex(':');
datif_OUT_SendRandomData(dataSendBuf, dataBufLen);
datif_kindOfCmd=0;
datif_repeatCtr=0;
doRepeat=false;
return 0;
}
// if no direct comands need to be sent then send input requests
if (gpi_isEmmisionOn()) // auto send button is pressed
{
//qDebug() << "auto request is on";
datif_kindOfCmd=0;
sendINrequestsAutomatic(); // request all cyclic data sequential
}
else
{
dif_scanStep=0; // always start from beginning
epi_resetDcDataValid();
}
datif_cmdWasPerformed=0; // 0: no response by now
return 0;
}
// ##############################################################################
// sende alle Befehle um die Eingangsdaten abzufragen der Reihe nach:
char T_datif::sendINrequestsAutomatic(void)
{
//qDebug() << "send IN request " << dif_scanStep;
//uint8_t datif_autoRequCommandList[30]={11, 12, 14, 17, 18, 19, 22, 23, 27, 30,
// 31, 32, 33, 35, 102, 103, 104, 106, 107, 109,
// 114,0,0,0,0,0,0,0,0,0};
// extension 6.12.23, complete list:
uint8_t datif_autoRequCommandList[40]={11, 12, 13, 14, 17, 18, 19, 21, 22, 23,
24, 25, 27, 30, 31, 32, 33, 35, 39, 40,
41, 42, 102,103,104,106,107,108,109,110,
112,113,114,115,116,0, 0, 0, 0, 0};
uint8_t datif_maxNrCommands=35, datif_sendNow;
// send quicker while transaction is ongoing:
uint8_t datif_vendRequCommandList[15]={102,107,108,110,112,115,116,31,32,40,41,42,23,0,0};
uint8_t datif_maxVendingCmds=13;
// special commands:
// 102: get IOs run constantly!!!
// 108, 112: get inserted amount in cent in sum, quick while purchase
// 113: get wake source, can be requested one time by Master after wake. Not here
// 19: get time and date and Extra values. poll occasionally and if needed
// 107, 22: MDB: poll if needed
doRepeat=true; // 20.9.23 15uhr (after release)
if (gpi_getNowCoinPay())
{
// send only important commands while transaction in progress
if (datif_pNextCmd>=datif_maxVendingCmds ) datif_pNextCmd=0;
datif_sendNow=datif_vendRequCommandList[datif_pNextCmd++];
if (datif_sendNow>0) // never send Command 0
{
datif_sendIOrequest(0, datif_sendNow, 0);
//qDebug() << "datif, VEND-request: " << datif_sendNow;
} else
datif_pNextCmd=0; // falls in der Liste 0 vorkommt dann von vorne beginnen
} else
{
// no transaction, request all but request DI's more frequently
if (datif_sendSlowCmd>0) // send slow and fast commands alternating
{
// send special command, slowly
if (datif_pNextCmd>=datif_maxNrCommands ) datif_pNextCmd=0;
datif_sendNow=datif_autoRequCommandList[datif_pNextCmd++];
if (datif_sendNow>0) // never send Command 0
{
datif_sendIOrequest(0, datif_sendNow, 0);
// qDebug() << "datif, auto-requ: " << datif_sendNow;
} else
datif_pNextCmd=0; // falls in der Liste 0 vorkommt dann von vorne beginnen
} else
{
// request inputs, high priority
datif_sendIOrequest(0, 31, 0); // 102
// while coin collection DIs are polled slower
//qDebug()<< "datif send requ.31 get DIs";
}
if (++datif_sendSlowCmd>1) datif_sendSlowCmd=0; // 0,1,0,1,0,1,0....
}
return 0; // 25.9.2023, wichtig sonst bleibt die komplette PTU stehen!!!!
}
char T_datif::isPortOpen(void)
{
return (myDCIF->isPortOpen());
}
// ##############################################################################
// Empfangsdaten einsortieren
// ----------------------------------------------------------------------------------------------
void T_datif::StoredRecData()
{
// got response from slave on last request
// call automatically by T_prot
uint8_t res;
datif_noResponseCtr=0;
loadRecDataFromFrame();
res=myDCIF->ifDataReceived();
// return: 0xFF: result unknown 0=OK
// 1= wrong length 2=wrong start sign 5= wrong crc
// 6= slave: master cmd was wrong 7: slave: could not write/read data
gpi_storeLastResult(res);
}
char T_datif::loadRecDataFromFrame()
{
// is called even with wrong received data in order to speed up the process (stop waiting for response)
// necessary data in T_prot:
uint16_t readSource, uitmp,uit2;
uint16_t readAddress;
//uint8_t pp;
uint8_t SlaveAdr, RdDleng;
uint8_t receivedData[FRAME_DATALEN];
QString localStr;
uint32_t ultmp;
//int portNr;
bool ret;
uint8_t uctmp; //, nn; //, res; // maxai
char ctmp;
//static uint8_t lastResult;
//uint8_t prnResult;
static uint32_t lastInsertedAmount;
uint32_t newInsertedAmount;
// uint32_t mifatb_cunu, mifatb_cardnu, mifatb_creditAmount, mifatb_creditTime;
// QString mifatb_cardTyp, mifatb_lpr, mifatb_group, mifatb_zone;
// uint8_t nn; char cc;
// uint8_t mifatb_times[10];
if (gpi_getNowIsBootload())
{
datif_cmdWasPerformed=1;
cycl_running=100; // stop waiting for response and wait 1cycle till next sending
return 0;
}
ret=myDCIF->getReceivedInData(&SlaveAdr, &readSource, &readAddress, &RdDleng, receivedData);
// nur true wenn CommandState OK und readState OK
// retval: data valid, only one time true, true if CommandState OK and readState OK
gpi_storeResultOfLastRequest(ret);
if (ret==false)
{
// Details anzeigen:
//res=
myDCIF->ifDataReceived();
// return: 0xFF: result unknown 0=OK
// 1= wrong length 2=wrong start sign 5= wrong crc
// 6= slave: master cmd was wrong 7: slave: could not write/read data
/*
qDebug() << "datif: bad response, error: " << res << " " << " lastWR: " << keepLastWrCmd << " lastRd: " << keepLastRdCmd;
qDebug("datif_recData: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",
receivedData[0], receivedData[1], receivedData[2], receivedData[3],
receivedData[4], receivedData[5], receivedData[6], receivedData[7],
receivedData[8], receivedData[9], receivedData[10], receivedData[11],
receivedData[12], receivedData[13], receivedData[14], receivedData[15]);
*/
datif_cmdWasPerformed=2; // NO :((
cycl_running=100; // stop waiting for response and wait 1cycle till next sending
return 0;
}
datif_cmdWasPerformed=1; // YES :), stop repeating
cycl_running=100; // stop waiting for response
//qDebug() << "datif: got valid response ";
gpi_storeRecPayLoad(RdDleng, receivedData); // save for host (user of hwapi)
// uint8_t nn;
//qDebug() << "\n datif: got valid data, rdsrc:" << readSource << " rdadd:" << readAddress
// << " rdlen:" << RdDleng;
// qDebug("datif_recData: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",
// receivedData[0], receivedData[1], receivedData[2], receivedData[3],
// receivedData[4], receivedData[5], receivedData[6], receivedData[7],
// receivedData[8], receivedData[9], receivedData[10], receivedData[11],
// receivedData[12], receivedData[13], receivedData[14], receivedData[15]);
// readSource: reflects the READ Command if possible
// readAddress: reflects the requested read/write address if slave could perform
// lastWakeSrc: if Slave wakes master, this ist the reason for it (e.g. button pressed)
// RdDataLength: number of bytes in receivedData
//firstDB=receivedData[0]; // just shorter, is used very often
//scndDB=receivedData[1];
//thrdDB=receivedData[2];
localStr.clear();
// receivedData[0]='a';
// receivedData[1]='B';
// receivedData[2]='1';
// receivedData[3]='-';
for (int ii=0; ii<RdDleng; ii++)
{
ctmp=receivedData[ii];
localStr.append(ctmp);
}
//qDebug() << "got HW version: " << localStr;
switch (readSource) // = request command
{
case CMD2DC_TestSerial: // serial line check 10
ret=verifyLineTestresponse(RdDleng, receivedData);
gpi_storeResult_serialTestOK(ret);
if (ret==true)
gpi_setTxt4datifLine("correct");
else
gpi_setTxt4datifLine("false");
break;
case CMD2DC_GetSerialConfig: // get slave's RS232 config 105
gpi_storeSlaveSerParams(receivedData[0], receivedData[1], receivedData[2], receivedData[3]);
//gpi_storeSlaveBusAddr(SlaveAdr);
break;
case CMD2DC_RdBkHWversion: //11
//qDebug() << "got HW version: " << localStr;
gpi_storeHWver(localStr);
break;
case CMD2DC_RdBkSWversion: //12
//qDebug() << "got SW version: " << localStr;
gpi_storeSWver(localStr);
break;
case CMD2DC_RdBkDCstate: //101
//qDebug() << "got DC state " << localStr;
//localStr[8]=0;
//gpi_storeDCstate(localStr);
break;
case CMD2DC_RdBkUID: // getback UID in DB0....7 18
gpi_storeUID(receivedData);
//qDebug()<<"datif got UID: "<<receivedData;
break;
case CMD2DC_RdBkTime: // slave returned RTC time and date 104
// new, FastProt: 0=dayOfWeek 1=min 2=sec 3=hours 4=year 5=month 6=dayOfYear
gpi_backupTime(receivedData, RdDleng); // function reads 20 bytes from the buffer
break;
case CMD2DC_RdBkAnalog: // get ALL AIs 0...3, 4byte + 2byte each 106
// DB0,1: Value0=Temperature
// DB2,3: Value1=Voltage
// DB4,5: ADC0 input value 0...1023
// DB6,7: ADC1 input value 0...1023
uitmp=uchar2uint(receivedData[1], receivedData[0]);
gpi_storeMeasureValue(0, uitmp);
uitmp=uchar2uint(receivedData[3], receivedData[2]);
gpi_storeMeasureValue(1, uitmp);
uitmp=uchar2uint(receivedData[5], receivedData[4]);
gpi_storeAIs(0, uitmp);
uitmp=uchar2uint(receivedData[7], receivedData[6]);
gpi_storeAIs(1, uitmp);
break;
case CMD2DC_GetAllInputs: //102
/*
// alle DI's in einen 8byte Puffer zusammenstellen, werden in einen Rutsch zum Master gesendet
outBuf[0]: door switches and cash boxes:
bit 0: service
1: battery
2: vault
3: coin box
4: bill box
outBuf[1]: lock bar switches:
bit 0: upper lock up
1: down
bit 2: lower lock up
3: down
4: contact pwr on
6: opto in2
outBuf[2]: aux inputs:
2: aux2....6:aux6
outBuf[3]: 0: ptu wake
1: rdBack MdbTx = 5V on if Tx low
2: prn ready
3: coin attached
4: Escrow
5: MifCardPresent
6: reject switch
outBuf[4]: 0: rdMdbWake 1: Wake=Low=Active
1: rdBackVmif
2: rdBack_MdbBusPwr5V
3: rdBackVbar
4: rdBackVgsm
5: rdBack_CreditPwr
6: rdBack_Vprint
7: rdBack_VmdbDevice
outBuf[5]: 0: coin Reject SwitchOn
1: PaperLowSensor
*/
// qDebug()<<"datif got DIs: "<< receivedData[0] << " "
// << receivedData[1] << " " << receivedData[2] << " "
//<< receivedData[3] << " " << receivedData[4] << " " << receivedData[5];
uctmp=receivedData[0];
//qDebug()<<"datif DOOR bits: "<< (uctmp&1) << (uctmp&2) << (uctmp&4);
gpi_storeDI_doorSwitches(uctmp&1, uctmp&2, uctmp&4);
gpi_storeDI_vaultSwitches(uctmp&8, uctmp&16);
uctmp=receivedData[1];
gpi_storeDI_lockSwitches((uctmp&3), ((uctmp>>2)&3));
uctmp=receivedData[1];
gpi_storeDI_contactPowerIsOn(uctmp&0x10);
gpi_storeDI_optos((uctmp>>5)&3);
uctmp=receivedData[2];
gpi_storeDI_auxIn(uctmp);
uctmp=receivedData[3];
gpi_storeDI_ptuWake(uctmp&1);
gpi_storeDI_readbackMdbTxD(uctmp&2);
gpi_storeDI_prnReady(uctmp&4);
gpi_storeDI_CoinAttach(uctmp&8);
gpi_storeDI_CoinEscrow(uctmp&16);
gpi_storeDI_mifareCardTapped(uctmp&32);
gpi_storeDI_rejMot_home(uctmp&64);
uctmp=receivedData[4];
//gpi_storeDI_modemWake(0); // gibt's gar nicht!
gpi_storeDI_mbdWake(uctmp&1);
gpi_storeDI_MifarePowerIsOn(uctmp&2);
gpi_storeDI_AuxPowerIsOn(uctmp&8);
gpi_storeDI_GsmPowerIsOn(uctmp&16);
gpi_storeDI_CreditPowerIsOn(uctmp&32);
gpi_storeDI_PrinterPowerIsOn(uctmp&64);
gpi_storeDI_MdbPowerIsOn(uctmp&128);
gpi_storeDI_paperLow(receivedData[5]);
break;
case CMD2DC_RdBkAllOutputs: //103
/*
// alle DO's in einen 8byte Puffer zusammenstellen, werden in einen Rutsch zum Master gesendet
outBuf[0]: bit 0: TestMdbInput
1: RS1 drv enabled
2: RS1switch: 1=GSM 0=printer
3: RS2switch: H: mifare L: credit
4: Vbarcode=Vaux is on
outBuf[1]: bit 0: coin led
1: 0
2: paper led
3: pinpad led
4: start led
5: service led
outBuf[2]: bit 0: siren
1: relay
2: ptu wake
3: shutter
4: shutter test
5: escrowGive
6: escrowTake
7: prn power
outBuf[3]: Motor 1: 0:off 1=vorwärts / öffnen 2=rückw./zu 3: beide ein
outBuf[4]: Motor 2: 0:off 1=vorwärts / öffnen 2=rückw./zu 3: beide ein
*/
uctmp=receivedData[0];
gpi_storeDO_mdbRxTst(uctmp&1);
gpi_storeDO_auxPower(uctmp&0x10);
gpi_storeDO_serialSwitch((uctmp>>1)&7); // serial drv 0:on/off, 1:Serial mux1, 2:Serial mux2
uctmp=receivedData[1];
gpi_storeDO_ledsAndFan(uctmp);
// bit0: coinled 1:front_illu 2: paper-led 3:pinpad-led 4:start-led 5:service-led 6:fan
uctmp=receivedData[2];
gpi_storeDO_sirenAndRelay(uctmp&3); // bit0: siren 1:relay
gpi_storeDO_ptuWake(uctmp&4);
gpi_storeDO_coinShutter(uctmp>>3); // bit0: Coin shutter output, bit1: input-test-output
gpi_storeDO_coinEscrow(uctmp>>5); // retval: 1:return flap is open 2:take flap is open 0:closed
gpi_storeDO_printerPwrOn(uctmp&0x80);
uctmp=(receivedData[4] & 3);
uctmp<<=2;
if (receivedData[3] & 1) uctmp |=1;
if (receivedData[3] & 2) uctmp |=2;
gpi_storeDO_motorOutputs(uctmp);
// bit0: upper lock forw bit 1 backw bit2: lowLock forw bit3: LL backw
break;
case 109:
// get reader status and card type
//gpi_storeMifReaderStateAndCardType(receivedData);
//gpi_storeNewMifareCard(receivedData[0], &receivedData[1]);
break;
case 24:
gpi_storeMifHwData(receivedData);
/*
struct T_Mifare
{
UCHAR ReaderState; // 1: OK 0: not OK
UCHAR res1;
UCHAR ReaderVersion[10];
UCHAR CardPresent;
UCHAR CardSelected;
UCHAR Cardtype;
UCHAR CardAllowed; // 0,1 nur Mifare Classic 1k und 4k zugelassen
UCHAR CardSize; // 1 or 4 (kB)
UCHAR LengOfUid; // 4 or 7 (byte)
UCHAR UID[8]; // 4 byte oder 7 byte, Format binär
// 26 byte bis hier
UCHAR res2;
UCHAR res3;
ULONG UidH; // bei 4byte Uid alles 0
ULONG UidL;
UCHAR SectorLogged; // result of loggin: 2=success 3=failed 1=no_tag 8=addr overflow F0=wrongCmd
UCHAR CurrentSector;
UCHAR RD_WR_Result;
UCHAR res4;
// 40byte lang
};
*/
break;
case CMD2DC_RdBk_AtbCardType: //25
// DC reports the type of mif-card (valid ATB card?)
gpi_storeMifAtbData(receivedData);
/*
mifatb_cunu=uchar2ulong(receivedData[3], receivedData[2],receivedData[1],receivedData[0]);
mifatb_cardnu=uchar2ulong(receivedData[7], receivedData[6],receivedData[5],receivedData[4]);
mifatb_cardTyp.clear();
for (nn=0; nn<4; nn++)
mifatb_cardTyp.append(char(receivedData[8+nn]));
mifatb_lpr.clear();
for (nn=0; nn<16; nn++)
mifatb_lpr.append(char(receivedData[12+nn]));
mifatb_group.clear();
for (nn=0; nn<8; nn++)
mifatb_group.append(char(receivedData[28+nn]));
mifatb_zone.clear();
for (nn=0; nn<8; nn++)
mifatb_zone.append(char(receivedData[36+nn]));
for (nn=0; nn<10; nn++)
mifatb_times[nn]=receivedData[44+nn];
// 0..9: (Start) day, month, year, hh, min (Stop) day, month, year, hh, min,
mifatb_creditAmount= uchar2ulong(receivedData[57], receivedData[56],receivedData[55],receivedData[54]);
mifatb_creditTime = uchar2ulong(receivedData[61], receivedData[60],receivedData[59],receivedData[58]);
// for the 2020 cards the field "mifatb_creditAmount" is used as expire date/time:
//for (nn=0; nn<5; nn++)
// mifatb_times[nn]=receivedData[54+nn]; falsch
ultmp=mifatb_creditAmount;
mifatb_times[4]=ultmp%100;
ultmp/=100;
mifatb_times[3]=ultmp%100;
ultmp/=100;
mifatb_times[2]=ultmp%100;
ultmp/=100;
mifatb_times[1]=ultmp%100;
ultmp/=100;
mifatb_times[0]=ultmp%100;
qDebug()<<"got MifAtbData: "<<mifatb_cunu<<" "<< mifatb_cardnu<<" "<< mifatb_cardTyp<<" "<< mifatb_lpr
<<" "<< mifatb_group<<" "<< mifatb_zone<<" "<< mifatb_times[0]<<" "<< mifatb_times[1]<<" "<< mifatb_times[2]
<<" "<< mifatb_times[3]<<" "<< mifatb_times[4]
<<" "<< mifatb_creditAmount<<" "<< mifatb_creditTime;
*/
// << mifatb_times[5]<<" "<< mifatb_times[6] <<" "<< mifatb_times[7]<<" "<< mifatb_times[8]<<" "<< mifatb_times[9]
break;
case 28:
//gpi_storeMifCardSectorData(readAddress, receivedData);
break;
case CMD2DC_RdBk_PrnState: // 110
// byte 1...6 come right from printer, see printer manual
// byte 0 = all important infos:
// byte 0 = 0: prnter OK, >0: 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
//D2: height
//D3: width
//D4: bold
//D5: invers
//D6: underlined
//D7: density
//D8: speed
//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
/*
qDebug()<<"printer fonts stored " <<receivedData[0]<<" "<<receivedData[1]<<" " \
<<receivedData[2]<<" "<<receivedData[3]<<" " \
<<receivedData[4]<<" "<<receivedData[5]<<" " \
<<receivedData[6]<<" "<<receivedData[10]<<" " \
<<receivedData[11]<<" "<<receivedData[18]<<" " \
<<receivedData[19]<<" "<<receivedData[20]<<" ";
*/
break;
case CMD2DC_MDB_GET_STATE: //107
// DB0: mdb_bus_ready (switched on)
// DB1: rdBackV12devicePower
// DB2: rdBackV5busPwr kommt immer mit 1
//qDebug() << "got MDB state " << receivedData[0] << " " << receivedData[1] << " " << receivedData[2];
gpi_storeMdbState(receivedData[0],receivedData[1],receivedData[2]);
break;
case CMD2DC_MDB_GETRESP: //22
// last received mdb answer (from mdb device)
// only needed if a special command was sent directly
// DB0: mdb Device-Nr
// DB1: last sent mdb command
// DB2: lastResult 1=got ACK 2=got 3xNAK 3=no or bad response 4:got Data (after ACK)
// DB3: nr of received (payload) data bytes (apart from ACK, can be 0....34)
// DB4...DB39: rec.data (payload)
gpi_storeMdbResponse(receivedData[2]+3, receivedData);
break;
case CMD2DC_EMP_GET_ALL: //23
//qDebug() << "got emp parameters "<< receivedData[1];
gpi_storeEmpSettings(64, receivedData);
break;
case CMD2DC_EMP_GOTCOIN: //108
// DB0: 1=coin 0xFF=error 0=got nothing
// DB1: last coin signal (value / scale)
// DB2,3: last coin value
// DB4: lastError from Emp
// 0: nr of stored coins
// 1: got 2:type 3:err 4=valL 5=valH
// qDebug() << "got emp coin "<< " " << receivedData[0] <<" " << receivedData[1]
// << " " << receivedData[2]<< " " << receivedData[3]
// << " " << receivedData[4]<< " " << receivedData[5]
// << " " << receivedData[6]<< " " << receivedData[7];
gpi_storeEmpCoinSignal(receivedData[0], &receivedData[1]);
break;
// ab hier neu: 12.4.23
case CMD2DC_RDBK_DEV_PARA: //14
/*
buf66[0]=devPara.kindOfPrinter;
buf66[1]=devPara.kindOfCoinChecker;
buf66[2]=devPara.kindOfMifareReader;
buf66[3]=devPara.suppressSleepMode;
buf66[4]=devPara.kindOfModem;
buf66[5]=devPara.kindOfCreditcard;
buf66[6]=devPara.CoinEscrow;
buf66[7]=devPara.CoinRejectUnit;
buf66[8]=devPara.CoinShutter;
buf66[9]=devPara.BillAcceptor;
buf66[10]=devPara.usevaultLock;
buf66[11]=devPara.autoAlarm;
buf66[12]=devPara.autoOpen;
buf66[13]=devPara.printAccReceipt;
buf66[14]=devPara.printDoorReceipt;
buf66[15]=devPara.printTokenTicket;
uitmp=devPara.VaultFullWarnLevel;
buf66[16]=swl_getOneByteFromUint(uitmp, GETLOWBYT);
buf66[17]=swl_getOneByteFromUint(uitmp, GETHIGHBYT);
uitmp=devPara.VaultFullErrorLevel;
buf66[18]=swl_getOneByteFromUint(uitmp, GETLOWBYT);
buf66[19]=swl_getOneByteFromUint(uitmp, GETHIGHBYT);
*/
gpi_storeRbDeviceSettings(RdDleng, receivedData);
break;
// get machine parameters and location
case CMD2DC_RDBK_MACH_ID: //15
case 17:
gpi_storeMachineIDsettings(RdDleng, receivedData);
break;
case 19: // get time and date and Extra values
// speicher-fkt fehlt noch, gpi_backupTime ist nur kurz
gpi_storeExtendedTime(RdDleng, receivedData);
break;
case 111: // get square wafe settings
gpi_backupSquareMode(receivedData[0]);
break;
case 112: // get inserted amount in cent in sum
// byte 0..3: amount just paid 4,5:last coin type 6,7: last coin value
newInsertedAmount=uchar2ulong(receivedData[3],receivedData[2],receivedData[1],receivedData[0]);
uitmp=uchar2uint(receivedData[5],receivedData[4]);
uit2=uchar2uint(receivedData[7],receivedData[6]);
//if (uitmp>0) // nur 1x bei neuer Münze 6.10.23 aendern:
// beim Wechsler hat die kleinste Muenze immer coin type 0!
if (uitmp>10000 || uit2>10000)
{
uitmp=0;
uit2=0;
}
if (newInsertedAmount != lastInsertedAmount)
{
gpi_storeCurrentPayment(newInsertedAmount, uitmp, uit2);
emit datif_gotNewCoin(); // OR BILL if (uitmp & 0x8000)>0
//qDebug()<<"emit new coin";
lastInsertedAmount=newInsertedAmount;
//qCritical()<<"datif 112 store and emit new coin "<<newInsertedAmount<<" "<<uitmp<<" "<<uit2;
}
break;
case 113: // get wake source, 8byte
gpi_storeWakeSources(receivedData);
break;
case 114:
// readback current accounting number in DC
uitmp=uchar2uint(receivedData[1],receivedData[0]);
gpi_storeNextDCaccNr(uitmp );
break;
case 115: // get result of coin dispensing
// receivedData[0]: 0: not yet started 1:amount returned
// 2:only partial return 3: no return possible
// receivedData[2,3,4,5]: returned amount
//qDebug()<<"datif got response 115";
ultmp=uchar2ulong(receivedData[5],receivedData[4],receivedData[3],receivedData[2]);
gpi_storeChangerResult(receivedData[0], ultmp);
break;
case 30: // Get Devices Condition (warnings, errors)
if (RdDleng>28)
{
gpi_storeDeviceConditions(RdDleng, receivedData);
}
break;
case 31: // Get dynamic machine conditions (doors, voltage, alarm….)
if (RdDleng>50)
{
epi_setDcDataValid(); // DC-Data are valid as DC responded.
// Could be set to every response but this (31)
// is a very common and very important request
gpi_storeDynMachineConditions(RdDleng, receivedData);
gpi_storeDI_CoinAttach(receivedData[6]); // new, 14.2.24 needed for direct coin insertion
}
/* funktioniert, ist aber nicht nötig. Signal wird nach dem shared memory erzeugt
prnResult=receivedData[52];
if (prnResult != lastResult)
{
// new result
if (prnResult==1)
{
emit datif_templatePrintFinished_OK();
} else
if (prnResult==2)
{
emit datif_templatePrintFinished_Err();
}
lastResult=prnResult;
}*/
break;
case 32: // Get current cash level (in vault)
// bytes 0..3: amount bytes 4,5=Nr.ofCoins in vault
ultmp=uchar2ulong(receivedData[3],receivedData[2],receivedData[1],receivedData[0]);
uitmp=uchar2uint(receivedData[5],receivedData[4]);
gpi_storeCBlevel(ultmp, uitmp);
break;
case 33: // Get all backuped acc. Numbers
// 8 UINTs with acc-nr
gpi_storeDCbackupAccNr(RdDleng, receivedData);
break;
case 35:
gpi_storeMifCardType(RdDleng, receivedData);
break;
case 38: // Get stored account record backup
// readAddress, &RdDleng, receivedData
//if (RdDleng>50) // 1.8.23 nach Verlängerung des Datensatzes um 20byte falsch!!!
//{
gpi_storeVaultRecord(readAddress, receivedData ); // always/max 64byte
/*
qDebug()<<"datif cmd38 got vault data. blockNr: "<<readAddress;
if (readAddress==0)
for (nn=0; nn<64; nn+=8)
{
qDebug()<<receivedData[nn]<<" "<<receivedData[nn+1]<<" "<<receivedData[nn+2]<<" "<<receivedData[nn+3]<<" "
<<receivedData[nn+4]<<" "<<receivedData[nn+5]<<" "<<receivedData[nn+6]<<" "<<receivedData[nn+7];
}
*/
//}
break;
case 39:
if (RdDleng>60)
{
gpi_storeDynData(receivedData);
}
break;
case 40:
//qDebug()<<"datif got response 40 bytes: " << RdDleng;
if (RdDleng>60)
{
gpi_storeTubeLevel(receivedData);
}
break;
case 41: // get BNA parameters
if (RdDleng>50)
{
gpi_storeBnaParams(receivedData);
}
break;
case 21: // readback version strings of all Json-File in DC
//
gpi_storeJsonVersion(readAddress, receivedData);
break;
case 116: // get BNA current collection
if (RdDleng>7)
{
gpi_storeBnaCollection(receivedData);
}
break;
case 42: // get BNA box content and value of types
//qDebug()<< "CAslave datif_got 42";
if (RdDleng>60)
{
gpi_storeBnaContent(receivedData);
}
break;
}
readSource=0; // 17.05.2023: to avoid multiple recording
return 0;
}
// subs:
// ----------------------------------------------------------------------------------------------
// erstelle WRITE Datensätze
// ----------------------------------------------------------------------------------------------
// always use this three functions to send data:
//void myDCIF->setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDatLen, uint8_t *data);
//void myDCIF->setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr);
//void myDCIF->setUserWriteData(uint16_t WriteCmd);
//void myDCIF->setUserWrite1DB (uint16_t WriteCmd, uint16_t WrAddr, uint8_t val);
//void myDCIF->setUserWrite2DB (uint16_t WriteCmd, uint16_t WrAddr, uint8_t val0, uint8_t val1);
//void myDCIF->setUserReadData( uint16_t ReadCmd);
//void myDCIF->sendUserData(uint16_t slaveAdr);
void T_datif::datif_startSending(void)
{
// egal ob WR und RD Daten gesetzt wurden
myDCIF->sendUserData(selectedSlaveAddr); // starte Sendung
gpi_startNewRequest();
cycl_running=1;
}
// allgemeine Schreib/Lese-Funktion
void T_datif::datif_sendIOrequest(uint16_t WRcmd, uint16_t RDcmd, uint8_t nrOfWrData)
{
uint8_t data[6]; //, who;
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;
keepLastWrCmd=WRcmd; keepLastRdCmd=RDcmd;
//if (RDcmd==101)
//{
// who=0;
//}
myDCIF->setUserWriteData(WRcmd, 0, nrOfWrData, data);
myDCIF->setUserReadData(RDcmd);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1;
}
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
data[0]=datif_OutCmdpara1;
data[1]=datif_OutCmdpara2;
uitmp=datif_OutCmdpara5;
ultmp=datif_OutCmdpara6;
data[2]=uint8_t(uitmp);
uitmp>>=8;
data[3]=uint8_t(uitmp);
data[4]=uint8_t(ultmp);
ultmp>>=8;
data[5]=uint8_t(ultmp);
ultmp>>=8;
data[6]=uint8_t(ultmp);
ultmp>>=8;
data[7]=uint8_t(ultmp);
data[8]=0; data[9]=0;
myDCIF->setUserWriteData(WRcmd, 0, 8, data);
myDCIF->setUserReadData(RDcmd);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1;
}
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;
}
// RTC ----------------------------------------------------------------------
/*
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();
buff[0]=uint8_t(systTime->currentTime().hour());
buff[1]=uint8_t(systTime->currentTime().minute());
buff[2]=uint8_t(systTime->currentTime().second());
QDate *systDate = new QDate();
systDate->currentDate();
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;
//myDCIF->setUserWriteData(0x1310,0,8, buff);
myDCIF->setUserWriteData(CMD2DC_sendTime,0,8, buff);
myDCIF->setUserReadData(0);
myDCIF->sendUserData(selectedSlaveAddr); // jetzt wegsckicken
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;
myDCIF->setBLsendData(len, buf );
myDCIF->setUserReadData(0);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1;
return 0;
}
/*
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;
uint8_t data[66];
tslib_strclr(data,0,66); // up to 64 used
gpi_restore64ByteSendData(&LL, data); // LL bytes was stored to be sent
myDCIF->setUserWriteData(WRcmd, addr, LL, data);
myDCIF->setUserReadData(RDcmd);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1;
}*/
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;
myDCIF->setUserWriteData(WRcmd, aa, 64, data64);
myDCIF->setUserReadData(0);
myDCIF->sendUserData(selectedSlaveAddr);
cycl_running=1;
}
void T_datif::startSupervision(void)
{
gpi_storeOverallResult(0xFF);
}