diff --git a/bus_protocols/isotp_handler.cpp b/bus_protocols/isotp_handler.cpp index db47747..a4db3cc 100644 --- a/bus_protocols/isotp_handler.cpp +++ b/bus_protocols/isotp_handler.cpp @@ -130,7 +130,19 @@ void ISOTP_HANDLER::rapidFrames(const CANConnection* conn, const QVector data); void setProcessAll(bool state); void setFlowCtrl(bool state); - void addID(uint32_t id); - void removeID(uint32_t id); - void clearAllIDs(); + void addFilter(int pBusId, uint32_t ID, uint32_t mask); + void removeFilter(int pBusId, uint32_t ID, uint32_t mask); + void clearAllFilters(); public slots: void updatedFrames(int); @@ -37,7 +38,7 @@ signals: private: QList messageBuffer; QList sendingFrames; - QMap isoIDs; + QList filters; const QVector *modelFrames; bool useExtendedAddressing; bool isReceiving; diff --git a/bus_protocols/uds_handler.cpp b/bus_protocols/uds_handler.cpp index 01f514b..5eb5174 100644 --- a/bus_protocols/uds_handler.cpp +++ b/bus_protocols/uds_handler.cpp @@ -270,19 +270,18 @@ void UDS_HANDLER::setProcessAllIDs(bool state) isoHandler->setProcessAll(state); } -void UDS_HANDLER::addID(uint32_t id) +void UDS_HANDLER::addFilter(int pBusId, uint32_t ID, uint32_t mask) { - isoHandler->addID(id); + isoHandler->addFilter(pBusId, ID, mask); } -void UDS_HANDLER::removeID(uint32_t id) +void UDS_HANDLER::removeFilter(int pBusId, uint32_t ID, uint32_t mask) { - isoHandler->removeID(id); + isoHandler->removeFilter(pBusId, ID, mask); } -void UDS_HANDLER::clearAllIDs() +void UDS_HANDLER::clearAllFilters() { - isoHandler->clearAllIDs(); + isoHandler->clearAllFilters(); } - diff --git a/bus_protocols/uds_handler.h b/bus_protocols/uds_handler.h index 7436b6e..d2470ee 100644 --- a/bus_protocols/uds_handler.h +++ b/bus_protocols/uds_handler.h @@ -94,9 +94,10 @@ public: void sendUDSFrame(const UDS_MESSAGE &msg); void setProcessAllIDs(bool state); void setFlowCtrl(bool state); - void addID(uint32_t id); - void removeID(uint32_t id); - void clearAllIDs(); + void addFilter(int pBusId, uint32_t ID, uint32_t mask); + void removeFilter(int pBusId, uint32_t ID, uint32_t mask); + void clearAllFilters(); + QString getServiceShortDesc(int service); QString getServiceLongDesc(int service); QString getNegativeResponseShort(int respCode); diff --git a/can_structs.h b/can_structs.h index ece14b1..42c5c2e 100644 --- a/can_structs.h +++ b/can_structs.h @@ -17,14 +17,14 @@ public: uint64_t timestamp; }; -class CANFlt +class CANFltObserver { public: quint32 id; quint32 mask; QObject * observer; //used to target the specific object that setup this filter - bool operator ==(const CANFlt &b) const + bool operator ==(const CANFltObserver &b) const { if ( (id == b.id) && (mask == b.mask) && (observer == b.observer) ) return true; diff --git a/config.h b/config.h index f8d9978..870af30 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG #define CONFIG -#define VERSION 173 +#define VERSION 174 //try to keep this in sync. //SavvyCAN will complain if you connect a GVRET board with a revision diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index 43aebc4..0294795 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -6,7 +6,7 @@ struct BusData { CANBus mBus; bool mConfigured; - QVector mTargettedFrames; + QVector mTargettedFrames; }; @@ -28,7 +28,7 @@ CANConnection::CANConnection(QString pPort, qRegisterMetaType("CANBus"); qRegisterMetaType("CANFrame"); qRegisterMetaType("CANCon::status"); - qRegisterMetaType("CANFlt"); + qRegisterMetaType("CANFlt"); /* set queue size */ mQueue.setSize(pQueueLen); /*TODO add check on returned value */ @@ -289,7 +289,7 @@ bool CANConnection::addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QO return false; qDebug() << "Connection is registering a new targetted frame filter, local bus " << pBusId; - CANFlt target; + CANFltObserver target; target.id = ID; target.mask = mask; target.observer = receiver; @@ -317,7 +317,7 @@ bool CANConnection::removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, if(pBusId < -1 || pBusId >= getNumBuses()) return false; - CANFlt target; + CANFltObserver target; target.id = ID; target.mask = mask; target.observer = receiver; @@ -329,7 +329,7 @@ bool CANConnection::removeTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, bool CANConnection::removeAllTargettedFrames(QObject *receiver) { for (int i = 0; i < getNumBuses(); i++) { - foreach (const CANFlt filt, mBusData_p[i].mTargettedFrames) + foreach (const CANFltObserver filt, mBusData_p[i].mTargettedFrames) { if (filt.observer == receiver) mBusData_p[i].mTargettedFrames.removeOne(filt); } @@ -345,7 +345,7 @@ void CANConnection::checkTargettedFrame(CANFrame &frame) if (mBusData_p == 0) return; if (mBusData_p[frame.bus].mTargettedFrames.length() == 0) return; - foreach (const CANFlt filt, mBusData_p[frame.bus].mTargettedFrames) + foreach (const CANFltObserver filt, mBusData_p[frame.bus].mTargettedFrames) { //qDebug() << "Checking filter with id " << filt.id << " mask " << filt.mask; maskedID = frame.ID & filt.mask; diff --git a/mainwindow.cpp b/mainwindow.cpp index 789173d..1a45190 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -396,7 +396,7 @@ void MainWindow::tickGUIUpdate() //{ int elapsed = elapsedTime->elapsed(); if(elapsed) { - framesPerSec = rxFrames * 1000 / elapsed; + framesPerSec = (framesPerSec + (rxFrames * 1000 / elapsed)) / 2; elapsedTime->restart(); } else diff --git a/mainwindow.h b/mainwindow.h index 6f79c09..920ba9a 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -35,6 +35,7 @@ class ConnectionWindow; class ISOTP_InterpreterWindow; +class ScriptingWindow; namespace Ui { class MainWindow; diff --git a/scriptcontainer.cpp b/scriptcontainer.cpp index d534227..b429c6d 100644 --- a/scriptcontainer.cpp +++ b/scriptcontainer.cpp @@ -7,8 +7,8 @@ ScriptContainer::ScriptContainer() { canHelper = new CANScriptHelper(&scriptEngine); - isoHelper = new ISOTPScriptHelper; - udsHelper = new UDSScriptHelper; + isoHelper = new ISOTPScriptHelper(&scriptEngine); + udsHelper = new UDSScriptHelper(&scriptEngine); elapsedTime.start(); connect(&timer, SIGNAL(timeout()), this, SLOT(tick())); } @@ -104,6 +104,9 @@ void ScriptContainer::tick() } + + + /* CANScriptHandler Methods */ CANScriptHelper::CANScriptHelper(QJSEngine *engine) @@ -186,3 +189,144 @@ void CANScriptHelper::gotTargettedFrame(const CANFrame &frame) } + + +/* ISOTPScriptHelper methods */ +ISOTPScriptHelper::ISOTPScriptHelper(QJSEngine *engine) +{ + scriptEngine = engine; + handler = new ISOTP_HANDLER; + connect(handler, SIGNAL(newISOMessage(ISOTP_MESSAGE)), this, SLOT(newISOMessage(ISOTP_MESSAGE))); + handler->setReception(true); +} + +void ISOTPScriptHelper::clearFilters() +{ + handler->clearAllFilters(); +} + +void ISOTPScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus) +{ + uint32_t idVal = id.toUInt(); + uint32_t maskVal = mask.toUInt(); + int busVal = bus.toInt(); + qDebug() << "Called isotp set filter"; + qDebug() << idVal << "*" << maskVal << "*" << busVal; + + handler->addFilter(busVal, idVal, maskVal); +} + +void ISOTPScriptHelper::sendISOTP(QJSValue bus, QJSValue id, QJSValue length, QJSValue data) +{ + ISOTP_MESSAGE msg; + msg.extended = false; + msg.ID = id.toInt(); + msg.len = length.toUInt(); + + if (!data.isArray()) qDebug() << "data isn't an array"; + + for (unsigned int i = 0; i < msg.len; i++) + { + msg.data[i] = (uint8_t)data.property(i).toInt(); + } + + msg.bus = (uint32_t)bus.toInt(); + + if (msg.ID > 0x7FF) msg.extended = true; + + qDebug() << "sending isotp message from script"; + handler->sendISOTPFrame(msg.bus, msg.ID, msg.data); +} + +void ISOTPScriptHelper::setRxCallback(QJSValue cb) +{ + gotFrameFunction = cb; +} + +void ISOTPScriptHelper::newISOMessage(ISOTP_MESSAGE msg) +{ + if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function + //qDebug() << "Got frame in script interface"; + + QJSValueList args; + args << msg.bus << msg.ID << msg.len; + QJSValue dataBytes = scriptEngine->newArray(msg.len); + + for (unsigned int j = 0; j < msg.len; j++) dataBytes.setProperty(j, QJSValue(msg.data[j])); + args.append(dataBytes); + gotFrameFunction.call(args); +} + + + + +/* UDSScriptHelper methods */ +UDSScriptHelper::UDSScriptHelper(QJSEngine *engine) +{ + scriptEngine = engine; + handler = new UDS_HANDLER; + connect(handler, SIGNAL(newUDSMessage(UDS_MESSAGE)), this, SLOT(newUDSMessage(UDS_MESSAGE))); + handler->setReception(true); +} + +void UDSScriptHelper::clearFilters() +{ + handler->clearAllFilters(); +} + +void UDSScriptHelper::setFilter(QJSValue id, QJSValue mask, QJSValue bus) +{ + uint32_t idVal = id.toUInt(); + uint32_t maskVal = mask.toUInt(); + int busVal = bus.toInt(); + qDebug() << "Called uds set filter"; + qDebug() << idVal << "*" << maskVal << "*" << busVal; + + handler->addFilter(busVal, idVal, maskVal); +} + +void UDSScriptHelper::sendUDS(QJSValue bus, QJSValue id, QJSValue service, QJSValue sublen, QJSValue subFunc, QJSValue length, QJSValue data) +{ + UDS_MESSAGE msg; + msg.extended = false; + msg.ID = id.toInt(); + msg.len = length.toUInt(); + msg.service = service.toUInt(); + msg.subFuncLen = sublen.toUInt(); + msg.subFunc = subFunc.toUInt(); + + if (!data.isArray()) qDebug() << "data isn't an array"; + + for (unsigned int i = 0; i < msg.len; i++) + { + msg.data[i] = (uint8_t)data.property(i).toInt(); + } + + msg.bus = (uint32_t)bus.toInt(); + + if (msg.ID > 0x7FF) msg.extended = true; + + qDebug() << "sending UDS message from script"; + + handler->sendUDSFrame(msg); +} + +void UDSScriptHelper::setRxCallback(QJSValue cb) +{ + gotFrameFunction = cb; +} + +void UDSScriptHelper::newUDSMessage(UDS_MESSAGE msg) +{ + if (!gotFrameFunction.isCallable()) return; //nothing to do if we can't even call the function + //qDebug() << "Got frame in script interface"; + + QJSValueList args; + args << msg.bus << msg.ID << msg.service << msg.subFunc << msg.len; + QJSValue dataBytes = scriptEngine->newArray(msg.len); + + for (unsigned int j = 0; j < msg.len; j++) dataBytes.setProperty(j, QJSValue(msg.data[j])); + args.append(dataBytes); + gotFrameFunction.call(args); +} + diff --git a/scriptcontainer.h b/scriptcontainer.h index fbabacd..10e1c40 100644 --- a/scriptcontainer.h +++ b/scriptcontainer.h @@ -3,6 +3,9 @@ #include "can_structs.h" #include "canfilter.h" +#include "bus_protocols/isotp_handler.h" +#include "bus_protocols/isotp_message.h" +#include "bus_protocols/uds_handler.h" #include #include @@ -33,17 +36,37 @@ private: class ISOTPScriptHelper: public QObject { Q_OBJECT +public: + ISOTPScriptHelper(QJSEngine *engine); +public slots: + void setFilter(QJSValue id, QJSValue mask, QJSValue bus); + void clearFilters(); + void sendISOTP(QJSValue bus, QJSValue id, QJSValue length, QJSValue data); + void setRxCallback(QJSValue cb); +private slots: + void newISOMessage(ISOTP_MESSAGE msg); private: QJSValue gotFrameFunction; QJSEngine *scriptEngine; + ISOTP_HANDLER *handler; }; class UDSScriptHelper: public QObject { Q_OBJECT +public: + UDSScriptHelper(QJSEngine *engine); +public slots: + void setFilter(QJSValue id, QJSValue mask, QJSValue bus); + void clearFilters(); + void sendUDS(QJSValue bus, QJSValue id, QJSValue service, QJSValue sublen, QJSValue subFunc, QJSValue length, QJSValue data); + void setRxCallback(QJSValue cb); +private slots: + void newUDSMessage(UDS_MESSAGE msg); private: QJSValue gotFrameFunction; QJSEngine *scriptEngine; + UDS_HANDLER *handler; }; class ScriptContainer : public QObject diff --git a/scriptingwindow.h b/scriptingwindow.h index 986cf75..1f77cbf 100644 --- a/scriptingwindow.h +++ b/scriptingwindow.h @@ -1,14 +1,16 @@ #ifndef SCRIPTINGWINDOW_H #define SCRIPTINGWINDOW_H -#include "can_structs.h" #include "scriptcontainer.h" +#include "can_structs.h" #include "connections/canconnection.h" #include "jsedit.h" #include #include +class ScriptContainer; + namespace Ui { class ScriptingWindow; }