From 96b73bd1caf09b8039e5c3e129ab9115ca789095 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sun, 4 Feb 2018 18:11:25 -0500 Subject: [PATCH] ISOTP and Scripting bug fixes --- bus_protocols/isotp_handler.cpp | 8 +++- bus_protocols/isotp_handler.h | 1 + scriptcontainer.cpp | 72 ++++++++++++++++++++++++--------- scriptcontainer.h | 3 +- scriptingwindow.cpp | 9 ++++- 5 files changed, 70 insertions(+), 23 deletions(-) diff --git a/bus_protocols/isotp_handler.cpp b/bus_protocols/isotp_handler.cpp index a376e1d..67d3f83 100644 --- a/bus_protocols/isotp_handler.cpp +++ b/bus_protocols/isotp_handler.cpp @@ -9,6 +9,7 @@ ISOTP_HANDLER::ISOTP_HANDLER() processAll = false; lastSenderBus = 0; lastSenderID = 0; + issuedMultiFrame = false; modelFrames = MainWindow::getReference()->getCANFrameModel()->getListReference(); @@ -60,6 +61,7 @@ void ISOTP_HANDLER::sendISOTPFrame(int bus, int ID, QVector data) if (data.length() < 8) { + issuedMultiFrame = false; frame.bus = bus; frame.extended = false; frame.ID = ID; @@ -71,6 +73,7 @@ void ISOTP_HANDLER::sendISOTPFrame(int bus, int ID, QVector data) } else //need to send a multi-part ISO_TP message - Respects timing and frame number based flow control { + issuedMultiFrame = true; frame.bus = bus; frame.ID = ID; frame.extended = false; @@ -200,6 +203,7 @@ void ISOTP_HANDLER::processFrame(const CANFrame &frame) msg.ID = ID; msg.timestamp = frame.timestamp; msg.isReceived = frame.isReceived; + issuedMultiFrame = true; frameLen = frameLen << 8; if (useExtendedAddressing) { @@ -220,7 +224,7 @@ void ISOTP_HANDLER::processFrame(const CANFrame &frame) messageBuffer.append(msg); //The sending ID is set to the last ID we used to send from this class which is //very likely to be correct. But, caution, there is a chance that it isn't. Beware. - if (issueFlowMsgs && lastSenderID > 0) + if (issueFlowMsgs && lastSenderID > 0 && issuedMultiFrame) { CANFrame outFrame; outFrame.bus = lastSenderBus; @@ -260,6 +264,7 @@ void ISOTP_HANDLER::processFrame(const CANFrame &frame) if (pMsg->len <= pMsg->data.count()) { //qDebug() << "Emitting multiframe ISOTP message"; + issuedMultiFrame = false; emit newISOMessage(*pMsg); } break; @@ -300,6 +305,7 @@ void ISOTP_HANDLER::checkNeedFlush(uint64_t ID) //used to pass by reference but now newISOMessage should pass by value which makes it easier to use cross thread //qDebug() << "Flushing a partial frame"; emit newISOMessage(messageBuffer[i]); + issuedMultiFrame = false; messageBuffer.removeAt(i); return; } diff --git a/bus_protocols/isotp_handler.h b/bus_protocols/isotp_handler.h index 611f63a..1aa4652 100644 --- a/bus_protocols/isotp_handler.h +++ b/bus_protocols/isotp_handler.h @@ -46,6 +46,7 @@ private: int framesUntilFlow; bool processAll; bool issueFlowMsgs; + bool issuedMultiFrame; QTimer frameTimer; uint32_t lastSenderID; uint32_t lastSenderBus; diff --git a/scriptcontainer.cpp b/scriptcontainer.cpp index cf5f285..6d004c0 100644 --- a/scriptcontainer.cpp +++ b/scriptcontainer.cpp @@ -6,15 +6,47 @@ ScriptContainer::ScriptContainer() { - canHelper = new CANScriptHelper(&scriptEngine); - isoHelper = new ISOTPScriptHelper(&scriptEngine); - udsHelper = new UDSScriptHelper(&scriptEngine); + qDebug() << "Script Container Constructor"; + scriptEngine = new QJSEngine(); + canHelper = new CANScriptHelper(scriptEngine); + isoHelper = new ISOTPScriptHelper(scriptEngine); + udsHelper = new UDSScriptHelper(scriptEngine); connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); } +ScriptContainer::~ScriptContainer() +{ + qDebug() << "Script Container Destructor " << (uint64_t)this << "c: " << (uint64_t)canHelper; + timer.stop(); + disconnect(&timer, SIGNAL(timeout()), this, SLOT(tick())); + if (scriptEngine) + { + scriptText = ""; + compileScript(); + //delete scriptEngine; //doing this here seems to cause a crash. No crash if you don't. + //scriptEngine = NULL; + } + if (canHelper) + { + delete canHelper; + canHelper = NULL; + } + if (isoHelper) + { + delete isoHelper; + isoHelper = NULL; + } + if (udsHelper) + { + delete udsHelper; + udsHelper = NULL; + } + qDebug() << "end of destruct"; +} + void ScriptContainer::compileScript() { - QJSValue result = scriptEngine.evaluate(scriptText, fileName); + QJSValue result = scriptEngine->evaluate(scriptText, fileName); emit sendLog("Compiling script..."); @@ -37,22 +69,22 @@ void ScriptContainer::compileScript() //Add a bunch of helper objects into javascript that the scripts //can use to interact with the CAN buses - QJSValue hostObj = scriptEngine.newQObject(this); - scriptEngine.globalObject().setProperty("host", hostObj); - QJSValue canObj = scriptEngine.newQObject(canHelper); - scriptEngine.globalObject().setProperty("can", canObj); - QJSValue isoObj = scriptEngine.newQObject(isoHelper); - scriptEngine.globalObject().setProperty("isotp", isoObj); - QJSValue udsObj = scriptEngine.newQObject(udsHelper); - scriptEngine.globalObject().setProperty("uds", udsObj); + QJSValue hostObj = scriptEngine->newQObject(this); + scriptEngine->globalObject().setProperty("host", hostObj); + QJSValue canObj = scriptEngine->newQObject(canHelper); + scriptEngine->globalObject().setProperty("can", canObj); + QJSValue isoObj = scriptEngine->newQObject(isoHelper); + scriptEngine->globalObject().setProperty("isotp", isoObj); + QJSValue udsObj = scriptEngine->newQObject(udsHelper); + scriptEngine->globalObject().setProperty("uds", udsObj); //Find out which callbacks the script has created. - setupFunction = scriptEngine.globalObject().property("setup"); - canHelper->setRxCallback(scriptEngine.globalObject().property("gotCANFrame")); - isoHelper->setRxCallback(scriptEngine.globalObject().property("gotISOTPMessage")); - udsHelper->setRxCallback(scriptEngine.globalObject().property("gotUDSMessage")); + setupFunction = scriptEngine->globalObject().property("setup"); + canHelper->setRxCallback(scriptEngine->globalObject().property("gotCANFrame")); + isoHelper->setRxCallback(scriptEngine->globalObject().property("gotISOTPMessage")); + udsHelper->setRxCallback(scriptEngine->globalObject().property("gotUDSMessage")); - tickFunction = scriptEngine.globalObject().property("tick"); + tickFunction = scriptEngine->globalObject().property("tick"); if (setupFunction.isCallable()) { @@ -117,7 +149,7 @@ void ScriptContainer::updateValuesTable(QTableWidget *widget) foreach (QString paramName, scriptParams) { - valu = scriptEngine.globalObject().property(paramName).toString(); + valu = scriptEngine->globalObject().property(paramName).toString(); qDebug() << paramName << " - " << valu; bool found = false; for (int i = 0; i < widget->rowCount(); i++) @@ -152,7 +184,7 @@ void ScriptContainer::updateParameter(QString name, QString value) { qDebug() << name << " * " << value; QJSValue val(value); - scriptEngine.globalObject().setProperty(name, val); + scriptEngine->globalObject().setProperty(name, val); } @@ -249,6 +281,7 @@ ISOTPScriptHelper::ISOTPScriptHelper(QJSEngine *engine) handler = new ISOTP_HANDLER; connect(handler, SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(newISOMessage(ISOTP_MESSAGE))); handler->setReception(true); + handler->setFlowCtrl(true); } void ISOTPScriptHelper::clearFilters() @@ -319,6 +352,7 @@ UDSScriptHelper::UDSScriptHelper(QJSEngine *engine) handler = new UDS_HANDLER; connect(handler, SIGNAL(newUDSMessage(UDS_MESSAGE)), this, SLOT(newUDSMessage(UDS_MESSAGE))); handler->setReception(true); + handler->setFlowCtrl(true); } void UDSScriptHelper::clearFilters() diff --git a/scriptcontainer.h b/scriptcontainer.h index 04d10f4..ec9f4c8 100644 --- a/scriptcontainer.h +++ b/scriptcontainer.h @@ -77,6 +77,7 @@ class ScriptContainer : public QObject public: ScriptContainer(); + virtual ~ScriptContainer(); void setScriptWindow(ScriptingWindow *win); QString fileName; @@ -102,7 +103,7 @@ private slots: void tick(); private: - QJSEngine scriptEngine; + QJSEngine *scriptEngine; QJSValue compiledScript; QJSValue setupFunction; QJSValue tickFunction; diff --git a/scriptingwindow.cpp b/scriptingwindow.cpp index 1ea6e36..5b1fbf9 100644 --- a/scriptingwindow.cpp +++ b/scriptingwindow.cpp @@ -204,7 +204,7 @@ void ScriptingWindow::loadNewScript() QString justFileName = fileList[fileList.length() - 1]; ui->listLoadedScripts->addItem(justFileName); - container = new ScriptContainer; + container = new ScriptContainer(); container->fileName = justFileName; container->filePath = filename; container->scriptText = contents; @@ -223,7 +223,7 @@ void ScriptingWindow::createNewScript() { ScriptContainer *container; - container = new ScriptContainer; + container = new ScriptContainer(); container->fileName = "UNNAMED_" + QString::number((qrand() % 10000)) + ".js"; container->filePath = QString(); @@ -238,6 +238,8 @@ void ScriptingWindow::createNewScript() void ScriptingWindow::deleteCurrentScript() { + ScriptContainer* thisScript; + int sel = ui->listLoadedScripts->currentRow(); if (sel < 0) return; @@ -250,7 +252,10 @@ void ScriptingWindow::deleteCurrentScript() { case QMessageBox::Yes: ui->listLoadedScripts->takeItem(sel); + thisScript = scripts.at(sel); scripts.removeAt(sel); + delete thisScript; //causes a seg fault. Seems to be due to currently running javascript code. No idea how to stop code from running + thisScript = NULL; currentScript = NULL; if (ui->listLoadedScripts->count() > 0)