From cd5f851f353075e6f679daa7f6524e550cfa202e Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sun, 17 Jan 2016 13:30:42 -0500 Subject: [PATCH] Major mondification to how DBC files are handled in-program. Now it is much more object oriented and encapsulated. The biggest addition is that it is now possible to support multiple DBC files being loaded at once with each file associated to one or both buses. Note that currently the program still does not support multiple DBC files but the groundwork is there. --- SavvyCAN.pro | 3 +- canframemodel.cpp | 8 +- dbc_classes.cpp | 203 ++++++++++++ dbc_classes.h | 15 +- dbchandler.cpp | 731 +++++++++++++++++++++++--------------------- dbchandler.h | 89 ++++-- dbcmaineditor.cpp | 68 ++--- dbcsignaleditor.cpp | 41 ++- graphingwindow.cpp | 16 +- mainwindow.cpp | 57 +--- newgraphdialog.cpp | 14 +- 11 files changed, 760 insertions(+), 485 deletions(-) create mode 100644 dbc_classes.cpp diff --git a/SavvyCAN.pro b/SavvyCAN.pro index 40140e3..4e0c4a1 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -41,7 +41,8 @@ SOURCES += main.cpp\ scriptingwindow.cpp \ scriptcontainer.cpp \ canfilter.cpp \ - rangestatewindow.cpp + rangestatewindow.cpp \ + dbc_classes.cpp HEADERS += mainwindow.h \ can_structs.h \ diff --git a/canframemodel.cpp b/canframemodel.cpp index cba0699..39722fb 100644 --- a/canframemodel.cpp +++ b/canframemodel.cpp @@ -195,15 +195,15 @@ QVariant CANFrameModel::data(const QModelIndex &index, int role) const //now, if we're supposed to interpret the data and the DBC handler is loaded then use it if (dbcHandler != NULL && interpretFrames) { - DBC_MESSAGE *msg = dbcHandler->findMsgByID(thisFrame.ID); + DBC_MESSAGE *msg = dbcHandler->findMessage(thisFrame); if (msg != NULL) { tempString.append("\r\n"); tempString.append(msg->name + " " + msg->comment + "\r\n"); - for (int j = 0; j < msg->msgSignals.length(); j++) + for (int j = 0; j < msg->sigHandler->getCount(); j++) { - QString sigString = dbcHandler->processSignal(thisFrame, msg->msgSignals.at(j)); - if (sigString.length() > 1) //weeds out signals that don't actually exist in this message (due to multiplexing) + QString sigString; + if (msg->sigHandler->findSignalByIdx(j)->processAsText(thisFrame, sigString)) { tempString.append(sigString); tempString.append("\r\n"); diff --git a/dbc_classes.cpp b/dbc_classes.cpp new file mode 100644 index 0000000..8885b3f --- /dev/null +++ b/dbc_classes.cpp @@ -0,0 +1,203 @@ +#include "dbc_classes.h" +#include "dbchandler.h" +#include "utility.h" + +DBC_MESSAGE::DBC_MESSAGE() +{ + sigHandler = new DBCSignalHandler; +} + +/* + The way that the DBC file format works is kind of weird... For intel format signals you count up +from the start bit to the end bit which is (startbit + signallength - 1). At each point +bits are numbered in a sawtooth manner. What that means is that the very first bit is 0 and you count up +from there all of the way to 63 with each byte being 8 bits so bit 0 is the lowest bit in the first byte +and 8 is the lowest bit in the next byte up. The whole thing looks like this: + Bits + 7 6 5 4 3 2 1 0 + + 0 7 6 5 4 3 2 1 0 +b 1 15 14 13 12 11 10 9 8 +y 2 23 22 21 20 19 18 17 16 +t 3 31 30 29 28 27 26 25 24 +e 4 39 38 37 36 35 34 33 32 +s 5 47 46 45 44 43 42 41 40 + 6 55 54 53 52 51 50 49 48 + 7 63 62 61 60 59 58 57 56 + + For intel format you start at the start bit and keep counting up. If you have a signal size of 8 + and start at bit 12 then the bits are 12, 13, 14, 15, 16, 17, 18, 19 which spans across two bytes. + In this format each bit is worth twice as much as the last and you just keep counting up. + Bit 12 is worth 1, 13 is worth 2, 14 is worth 4, etc all of the way to bit 19 is worth 128. + + Motorola format turns most everything on its head. You count backward from the start bit but + only within the current byte. If you are about to exit the current byte you go one higher and then keep + going backward as before. Using the same example as for intel, start bit of 12 and a signal length of 8. + So, the bits are 12, 11, 10, 9, 8, 23, 22, 21. Yes, that's confusing. They now go in reverse value order too. + Bit 12 is worth 128, 11 is worth 64, etc until bit 21 is worth 1. +*/ +bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString) +{ + int64_t result = 0; + bool isSigned = false; + double endResult; + + if (valType == STRING) + { + QString buildString; + int startByte = signalSize / 8; + int bytes = signalSize / 8; + for (int x = 0; x < bytes; x++) buildString.append(frame.data[startByte + x]); + outString = buildString; + return true; + } + + //if this is a multiplexed signal then we have to see if it is even found in the current message + if (isMultiplexed) + { + if (parentMessage->multiplexorSignal != NULL) + { + int val; + if (!parentMessage->multiplexorSignal->processAsInt(frame, val)) return false; + if (val != multiplexValue) return false; //signal not found in this message + } + else return false; + } + + if (valType == SIGNED_INT) isSigned = true; + if (valType == SIGNED_INT || valType == UNSIGNED_INT) + { + result = Utility::processIntegerSignal(frame.data, startBit, signalSize, intelByteOrder, isSigned); + endResult = ((double)result * factor) + bias; + result = (int64_t)endResult; + } + else if (valType == SP_FLOAT) + { + //The theory here is that we force the integer signal code to treat this as + //a 32 bit unsigned integer. This integer is then cast into a float in such a way + //that the bytes that make up the integer are instead treated as having made up + //a 32 bit single precision float. That's evil incarnate but it is very fast and small + //in terms of new code. + result = Utility::processIntegerSignal(frame.data, startBit, 32, false, false); + endResult = (*((float *)(&result)) * factor) + bias; + } + else //double precision float + { + //like the above, this is rotten and evil and wrong in so many ways. Force + //calculation of a 64 bit integer and then cast it into a double. + result = Utility::processIntegerSignal(frame.data, 0, 64, false, false); + endResult = (*((double *)(&result)) * factor) + bias; + } + + QString outputString; + + outputString = name + ": "; + + if (valList.count() > 0) //if this is a value list type then look it up and display the proper string + { + for (int x = 0; x < valList.count(); x++) + { + if (valList.at(x).value == result) outputString += valList.at(x).descript; + } + } + else //otherwise display the actual number and unit (if it exists) + { + outputString += QString::number(endResult) + unitName; + } + + outString = outputString; + return true; +} + +//Works quite a bit like the above version but this one is cut down and only will return int32_t which is perfect for +//uses like calculating a multiplexor value or if you know you are going to get an integer returned +//from a signal and you want to use it as-is and not have to convert back from a string. Use with caution though +//as this basically assumes the signal is an integer. +//The call syntax is different from the more generic processSignal. Instead of returning the value we return +//true or false to show whether the function succeeded. The variable to fill out is passed by reference. +bool DBC_SIGNAL::processAsInt(const CANFrame &frame, int32_t &outValue) +{ + int32_t result = 0; + bool isSigned = false; + if (valType == STRING || valType == SP_FLOAT || valType == DP_FLOAT) + { + return false; + } + + //if this is a multiplexed signal then we have to see if it is even found in the current message + if (isMultiplexed) + { + if (parentMessage->multiplexorSignal != NULL) + { + int val; + if (!parentMessage->multiplexorSignal->processAsInt(frame, val)) return false; + if (val != multiplexValue) return false; //signal not found in this message + } + else return false; + } + + if (valType == SIGNED_INT) isSigned = true; + result = Utility::processIntegerSignal(frame.data, startBit, signalSize, intelByteOrder, isSigned); + + double endResult = ((double)result * factor) + bias; + result = (int32_t)endResult; + + outValue = result; + return true; +} + +//Another cut down version that will only return double precision data. This can be used on any of the types +//except STRING. Useful for when you know you'll need floating point data and don't want to incur a conversion +//back and forth to double or float. Such a use is the graphing window. +//Similar syntax to processSignalInt but with double instead. +bool DBC_SIGNAL::processAsDouble(const CANFrame &frame, double &outValue) +{ + int64_t result = 0; + bool isSigned = false; + double endResult; + + if (valType == STRING) + { + return false; + } + + //if this is a multiplexed signal then we have to see if it is even found in the current message + if (isMultiplexed) + { + if (parentMessage->multiplexorSignal != NULL) + { + int val; + if (!parentMessage->multiplexorSignal->processAsInt(frame, val)) return false; + if (val != multiplexValue) return false; //signal not found in this message + } + else return false; + } + + if (valType == SIGNED_INT) isSigned = true; + if (valType == SIGNED_INT || valType == UNSIGNED_INT) + { + result = Utility::processIntegerSignal(frame.data, startBit, signalSize, intelByteOrder, isSigned); + endResult = ((double)result * factor) + bias; + result = (int64_t)endResult; + } + else if (valType == SP_FLOAT) + { + //The theory here is that we force the integer signal code to treat this as + //a 32 bit unsigned integer. This integer is then cast into a float in such a way + //that the bytes that make up the integer are instead treated as having made up + //a 32 bit single precision float. That's evil incarnate but it is very fast and small + //in terms of new code. + result = Utility::processIntegerSignal(frame.data, startBit, 32, false, false); + endResult = (*((float *)(&result)) * factor) + bias; + } + else //double precision float + { + //like the above, this is rotten and evil and wrong in so many ways. Force + //calculation of a 64 bit integer and then cast it into a double. + result = Utility::processIntegerSignal(frame.data, 0, 64, false, false); + endResult = (*((double *)(&result)) * factor) + bias; + } + + outValue = endResult; + return true; +} diff --git a/dbc_classes.h b/dbc_classes.h index 9449c78..9318791 100644 --- a/dbc_classes.h +++ b/dbc_classes.h @@ -3,6 +3,7 @@ #include #include +#include "can_structs.h" /*classes to encapsulate data from a DBC file. Really, the stuff of interest are the nodes, messages, signals, attributes, and comments. @@ -58,7 +59,7 @@ class DBC_MESSAGE; //forward reference so that DBC_SIGNAL can compile before we class DBC_SIGNAL { -public: +public: //TODO: this is sloppy. It shouldn't all be public! QString name; int startBit; int signalSize; @@ -71,24 +72,32 @@ public: double bias; double min; double max; - DBC_NODE *receiver; + DBC_NODE *receiver; //it is fast to have a pointer but dangerous... Make sure to walk the whole tree and delete everything so nobody has stale references. DBC_MESSAGE *parentMessage; QString unitName; QString comment; QList attributes; QList valList; + + bool processAsText(const CANFrame &frame, QString &outString); + bool processAsInt(const CANFrame &frame, int32_t &outValue); + bool processAsDouble(const CANFrame &frame, double &outValue); }; +class DBCSignalHandler; //forward declaration to keep from having to include dbchandler.h in this file and thus create a loop + class DBC_MESSAGE { public: + DBC_MESSAGE(); + int ID; QString name; QString comment; int len; DBC_NODE *sender; QList attributes; - QList msgSignals; + DBCSignalHandler *sigHandler; DBC_SIGNAL* multiplexorSignal; }; diff --git a/dbchandler.cpp b/dbchandler.cpp index 396c095..6075ddb 100644 --- a/dbchandler.cpp +++ b/dbchandler.cpp @@ -4,23 +4,261 @@ #include #include #include +#include #include "utility.h" -DBCHandler::DBCHandler(QObject *parent) : QObject(parent) +DBC_SIGNAL* DBCSignalHandler::findSignalByIdx(int idx) { + if (sigs.count() == 0) return NULL; + if (idx < 0) return NULL; + if (idx >= sigs.count()) return NULL; + return &sigs[idx]; } -void DBCHandler::loadDBCFile(QString filename) +DBC_SIGNAL* DBCSignalHandler::findSignalByName(QString name) { - QFile *inFile = new QFile(filename); + if (sigs.count() == 0) return NULL; + for (int i = 0; i < sigs.count(); i++) + { + if (sigs[i].name.compare(name, Qt::CaseInsensitive) == 0) + { + return &sigs[i]; + } + } + return NULL; +} + +bool DBCSignalHandler::addSignal(DBC_SIGNAL &sig) +{ + sigs.append(sig); + return true; +} + +bool DBCSignalHandler::removeSignal(DBC_SIGNAL *sig) +{ + //if (sigs.removeAll(*sig) > 0) return true; + return false; +} + +bool DBCSignalHandler::removeSignal(int idx) +{ + if (sigs.count() == 0) return false; + if (idx < 0) return false; + if (idx >= sigs.count()) return false; + sigs.removeAt(idx); + return true; +} + +bool DBCSignalHandler::removeSignal(QString name) +{ + bool foundSome = false; + if (sigs.count() == 0) return false; + for (int i = sigs.count() - 1; i >= 0; i--) + { + if (sigs[i].name.compare(name, Qt::CaseInsensitive) == 0) + { + sigs.removeAt(i); + foundSome = true; + } + } + return foundSome; +} + +void DBCSignalHandler::removeAllSignals() +{ + sigs.clear(); +} + +int DBCSignalHandler::getCount() +{ + return sigs.count(); +} + +DBC_MESSAGE* DBCMessageHandler::findMsgByID(int id) +{ + if (messages.count() == 0) return NULL; + for (int i = 0; i < messages.count(); i++) + { + if (messages[i].ID == id) + { + return &messages[i]; + } + } + return NULL; +} + +DBC_MESSAGE* DBCMessageHandler::findMsgByIdx(int idx) +{ + if (messages.count() == 0) return NULL; + if (idx < 0) return NULL; + if (idx >= messages.count()) return NULL; + return &messages[idx]; +} + +DBC_MESSAGE* DBCMessageHandler::findMsgByName(QString name) +{ + if (messages.count() == 0) return NULL; + for (int i = 0; i < messages.count(); i++) + { + if (messages[i].name.compare(name, Qt::CaseInsensitive) == 0) + { + return &messages[i]; + } + } + return NULL; +} + +bool DBCMessageHandler::addMessage(DBC_MESSAGE &msg) +{ + messages.append(msg); + return true; +} + +bool DBCMessageHandler::removeMessage(DBC_MESSAGE *msg) +{ + //if (messages.removeAll(*msg) > 0) return true; + return false; +} + +bool DBCMessageHandler::removeMessageByIndex(int idx) +{ + if (messages.count() == 0) return false; + if (idx < 0) return false; + if (idx >= messages.count()) return false; + messages.removeAt(idx); + return true; +} + +bool DBCMessageHandler::removeMessage(int ID) +{ + bool foundSome = false; + if (messages.count() == 0) return false; + for (int i = messages.count() - 1; i >= 0; i--) + { + if (messages[i].ID == ID) + { + messages.removeAt(i); + foundSome = true; + } + } + return foundSome; +} + +bool DBCMessageHandler::removeMessage(QString name) +{ + bool foundSome = false; + if (messages.count() == 0) return false; + for (int i = messages.count() - 1; i >= 0; i--) + { + if (messages[i].name.compare(name, Qt::CaseInsensitive) == 0) + { + messages.removeAt(i); + foundSome = true; + } + } + return foundSome; +} + +void DBCMessageHandler::removeAllMessages() +{ + messages.clear(); +} + +int DBCMessageHandler::getCount() +{ + return messages.count(); +} + +DBCFile::DBCFile() +{ + messageHandler = new DBCMessageHandler; +} + +DBCFile::DBCFile(const DBCFile& cpy) +{ + messageHandler = new DBCMessageHandler; + for (int i = 0 ; i < cpy.messageHandler->getCount() ; i++) + messageHandler->addMessage(*cpy.messageHandler->findMsgByIdx(i)); + + fileName = cpy.fileName; + filePath = cpy.filePath; + assocBuses = cpy.assocBuses; + dbc_nodes.clear(); + dbc_nodes.append(cpy.dbc_nodes); +} + +DBCFile& DBCFile::operator=(const DBCFile& cpy) +{ + if (this != &cpy) // protect against invalid self-assignment + { + messageHandler = cpy.messageHandler; + fileName = cpy.fileName; + filePath = cpy.filePath; + assocBuses = cpy.assocBuses; + dbc_nodes.clear(); + dbc_nodes.append(cpy.dbc_nodes); + } + return *this; +} + +DBC_NODE* DBCFile::findNodeByIdx(int idx) +{ + if (idx < 0) return NULL; + if (idx >= dbc_nodes.count()) return NULL; + return &dbc_nodes[idx]; +} + +DBC_NODE* DBCFile::findNodeByName(QString name) +{ + if (dbc_nodes.length() == 0) return NULL; + for (int i = 0; i < dbc_nodes.length(); i++) + { + if (dbc_nodes[i].name.compare(name, Qt::CaseInsensitive) == 0) + { + return &dbc_nodes[i]; + } + } + return NULL; +} + +QString DBCFile::getFullFilename() +{ + return filePath + fileName; +} + +QString DBCFile::getFilename() +{ + return fileName; +} + +QString DBCFile::getPath() +{ + return filePath; +} + +int DBCFile::getAssocBus() +{ + return assocBuses; +} + +void DBCFile::setAssocBus(int bus) +{ + if (bus < -1) return; + if (bus > 1) return; + assocBuses = bus; +} + +void DBCFile::loadFile(QString fileName) +{ + QFile *inFile = new QFile(fileName); QString line; QRegularExpression regex; QRegularExpressionMatch match; DBC_MESSAGE *currentMessage = NULL; int numSigFaults = 0, numMsgFaults = 0; - qDebug() << "DBC File: " << filename; + qDebug() << "DBC File: " << fileName; if (!inFile->open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -30,7 +268,7 @@ void DBCHandler::loadDBCFile(QString filename) qDebug() << "Starting DBC load"; dbc_nodes.clear(); - dbc_messages.clear(); + messageHandler->removeAllMessages(); DBC_NODE falseNode; falseNode.name = "Vector__XXX"; @@ -55,8 +293,8 @@ void DBCHandler::loadDBCFile(QString filename) msg.name = match.captured(2); msg.len = match.captured(3).toInt(); msg.sender = findNodeByName(match.captured(4)); - dbc_messages.append(msg); - currentMessage = &dbc_messages.last(); + messageHandler->addMessage(msg); + currentMessage = messageHandler->findMsgByID(msg.ID); } else numMsgFaults++; } @@ -116,7 +354,7 @@ void DBCHandler::loadDBCFile(QString filename) //captured 11 is the receiving node if (match.hasMatch()) - { + { sig.name = match.captured(1); sig.startBit = match.captured(2 + offset).toInt(); sig.signalSize = match.captured(3 + offset).toInt(); @@ -156,8 +394,8 @@ void DBCHandler::loadDBCFile(QString filename) } else sig.receiver = findNodeByName(match.captured(11 + offset)); sig.parentMessage = currentMessage; - currentMessage->msgSignals.append(sig); - if (isMultiplexor) currentMessage->multiplexorSignal = ¤tMessage->msgSignals.last(); + currentMessage->sigHandler->addSignal(sig); + if (isMultiplexor) currentMessage->multiplexorSignal = currentMessage->sigHandler->findSignalByName(sig.name); } else numSigFaults++; } @@ -194,10 +432,10 @@ void DBCHandler::loadDBCFile(QString filename) if (match.hasMatch()) { //qDebug() << "Comment was: " << match.captured(3); - DBC_MESSAGE *msg = findMsgByID(match.captured(1).toInt()); + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); if (msg != NULL) { - DBC_SIGNAL *sig = findSignalByName(msg, match.captured(2)); + DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); if (sig != NULL) { sig->comment = match.captured(3); @@ -215,7 +453,7 @@ void DBCHandler::loadDBCFile(QString filename) if (match.hasMatch()) { //qDebug() << "Comment was: " << match.captured(2); - DBC_MESSAGE *msg = findMsgByID(match.captured(1).toInt()); + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); if (msg != NULL) { msg->comment = match.captured(2); @@ -251,10 +489,10 @@ void DBCHandler::loadDBCFile(QString filename) if (match.hasMatch()) { //qDebug() << "Data was: " << match.captured(3); - DBC_MESSAGE *msg = findMsgByID(match.captured(1).toInt()); + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); if (msg != NULL) { - DBC_SIGNAL *sig = findSignalByName(msg, match.captured(2)); + DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); if (sig != NULL) { QString tokenString = match.captured(3); @@ -316,14 +554,15 @@ void DBCHandler::loadDBCFile(QString filename) } inFile->close(); delete inFile; + QStringList fileList = fileName.split('/'); + this->fileName = fileList[fileList.length() - 1]; //whoops... same name as parameter in this function. + filePath = fileName.left(fileName.length() - this->fileName.length()); + } -/*Yes, this is really hard to follow and all of the sections are mixed up in code - * believe it or not I think this is actually the easiest, simplest way to do it. -*/ -void DBCHandler::saveDBCFile(QString filename) +void DBCFile::saveFile(QString fileName) { - QFile *outFile = new QFile(filename); + QFile *outFile = new QFile(fileName); QString nodesOutput, msgOutput, commentsOutput, valuesOutput; if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text)) @@ -384,36 +623,36 @@ void DBCHandler::saveDBCFile(QString filename) nodesOutput.append("\n"); outFile->write(nodesOutput.toUtf8()); - for (int x = 0; x < dbc_messages.count(); x++) + for (int x = 0; x < messageHandler->getCount(); x++) { - DBC_MESSAGE msg = dbc_messages[x]; - msgOutput.append("BO_ " + QString::number(msg.ID) + " " + msg.name + ": " + QString::number(msg.len) + - " " + msg.sender->name + "\n"); - if (msg.comment.length() > 0) + DBC_MESSAGE *msg = messageHandler->findMsgByIdx(x); + msgOutput.append("BO_ " + QString::number(msg->ID) + " " + msg->name + ": " + QString::number(msg->len) + + " " + msg->sender->name + "\n"); + if (msg->comment.length() > 0) { - commentsOutput.append("CM_ BO_ " + QString::number(msg.ID) + " \"" + msg.comment + "\";\n"); + commentsOutput.append("CM_ BO_ " + QString::number(msg->ID) + " \"" + msg->comment + "\";\n"); } - for (int s = 0; s < msg.msgSignals.count(); s++) + for (int s = 0; s < msg->sigHandler->getCount(); s++) { - DBC_SIGNAL sig = msg.msgSignals[s]; - msgOutput.append(" SG_ " + sig.name); + DBC_SIGNAL *sig = msg->sigHandler->findSignalByIdx(s); + msgOutput.append(" SG_ " + sig->name); - if (sig.isMultiplexor) msgOutput.append(" M"); - if (sig.isMultiplexed) + if (sig->isMultiplexor) msgOutput.append(" M"); + if (sig->isMultiplexed) { - msgOutput.append(" m" + QString::number(sig.multiplexValue)); + msgOutput.append(" m" + QString::number(sig->multiplexValue)); } - msgOutput.append(" : " + QString::number(sig.startBit) + "|" + QString::number(sig.signalSize) + "@"); + msgOutput.append(" : " + QString::number(sig->startBit) + "|" + QString::number(sig->signalSize) + "@"); - switch (sig.valType) + switch (sig->valType) { case UNSIGNED_INT: - if (sig.intelByteOrder) msgOutput.append("1+"); + if (sig->intelByteOrder) msgOutput.append("1+"); else msgOutput.append("0+"); break; case SIGNED_INT: - if (sig.intelByteOrder) msgOutput.append("1-"); + if (sig->intelByteOrder) msgOutput.append("1-"); else msgOutput.append("0-"); break; case SP_FLOAT: @@ -429,19 +668,19 @@ void DBCHandler::saveDBCFile(QString filename) msgOutput.append("0-"); break; } - msgOutput.append(" (" + QString::number(sig.factor) + "," + QString::number(sig.bias) + ") [" + - QString::number(sig.min) + "|" + QString::number(sig.max) + "] \"" + sig.unitName - + "\" " + sig.receiver->name + "\n"); - if (sig.comment.length() > 0) + msgOutput.append(" (" + QString::number(sig->factor) + "," + QString::number(sig->bias) + ") [" + + QString::number(sig->min) + "|" + QString::number(sig->max) + "] \"" + sig->unitName + + "\" " + sig->receiver->name + "\n"); + if (sig->comment.length() > 0) { - commentsOutput.append("CM_ SG_ " + QString::number(msg.ID) + " " + sig.name + " \"" + sig.comment + "\";\n"); + commentsOutput.append("CM_ SG_ " + QString::number(msg->ID) + " " + sig->name + " \"" + sig->comment + "\";\n"); } - if (sig.valList.count() > 0) + if (sig->valList.count() > 0) { - valuesOutput.append("VAL_ " + QString::number(msg.ID) + " " + sig.name); - for (int v = 0; v < sig.valList.count(); v++) + valuesOutput.append("VAL_ " + QString::number(msg->ID) + " " + sig->name); + for (int v = 0; v < sig->valList.count(); v++) { - DBC_VAL val = sig.valList[v]; + DBC_VAL val = sig->valList[v]; valuesOutput.append(" " + QString::number(val.value) + " \"" + val.descript +"\""); } valuesOutput.append(";\n"); @@ -459,324 +698,132 @@ void DBCHandler::saveDBCFile(QString filename) outFile->close(); delete outFile; + + QStringList fileList = fileName.split('/'); + this->fileName = fileList[fileList.length() - 1]; //whoops... same name as parameter in this function. + filePath = fileName.left(fileName.length() - this->fileName.length()); } - -DBC_NODE *DBCHandler::findNodeByName(QString name) +void DBCHandler::saveDBCFile(int idx) { - if (dbc_nodes.length() == 0) return NULL; - for (int i = 0; i < dbc_nodes.length(); i++) + if (loadedFiles.count() == 0) return; + if (idx < 0) return; + if (idx >= loadedFiles.count()) return; + + QString filename; + QFileDialog dialog; + + QStringList filters; + filters.append(QString(tr("DBC File (*.dbc)"))); + + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilters(filters); + dialog.setViewMode(QFileDialog::Detail); + dialog.setAcceptMode(QFileDialog::AcceptSave); + dialog.selectFile(loadedFiles[idx].getFullFilename()); + + if (dialog.exec() == QDialog::Accepted) { - if (dbc_nodes[i].name.compare(name, Qt::CaseInsensitive) == 0) - { - return &dbc_nodes[i]; - } - } - return NULL; -} - -DBC_NODE *DBCHandler::findNodeByIdx(int idx) -{ - if (idx < 0) return NULL; - if (idx >= dbc_nodes.count()) return NULL; - return &dbc_nodes[idx]; -} - -DBC_MESSAGE *DBCHandler::findMsgByID(int id) -{ - if (dbc_messages.length() == 0) return NULL; - for (int i = 0; i < dbc_messages.length(); i++) - { - if (dbc_messages[i].ID == id) - { - return &dbc_messages[i]; - } - } - return NULL; -} - -DBC_MESSAGE *DBCHandler::findMsgByIdx(int idx) -{ - if (dbc_messages.length() == 0) return NULL; - if (idx < 0) return NULL; - if (idx >= dbc_messages.count()) return NULL; - return &dbc_messages[idx]; -} - -DBC_MESSAGE *DBCHandler::findMsgByName(QString name) -{ - if (dbc_messages.length() == 0) return NULL; - for (int i = 0; i < dbc_messages.length(); i++) - { - if (dbc_messages[i].name.compare(name, Qt::CaseInsensitive) == 0) - { - return &dbc_messages[i]; - } - } - return NULL; -} - -DBC_SIGNAL *DBCHandler::findSignalByName(DBC_MESSAGE *msg, QString name) -{ - if (msg == NULL) return NULL; - if (msg->msgSignals.length() == 0) return NULL; - for (int i = 0; i < msg->msgSignals.length(); i++) - { - if (msg->msgSignals[i].name.compare(name, Qt::CaseInsensitive) == 0) - { - return &msg->msgSignals[i]; - } - } - return NULL; -} - -DBC_SIGNAL *DBCHandler::findSignalByIdx(DBC_MESSAGE *msg, int idx) -{ - if (msg == NULL) return NULL; - if (msg->msgSignals.length() == 0) return NULL; - if (idx < 0) return NULL; - if (idx >= msg->msgSignals.count()) return NULL; - return &msg->msgSignals[idx]; -} - -//Dumps the messages, signals, values structs out in order to debugging console. Used only for debugging -//not really meant for general consumption. -void DBCHandler::listDebugging() -{ - for (int i = 0; i < dbc_messages.length(); i++) - { - DBC_MESSAGE msg = dbc_messages.at(i); - qDebug() << " "; - qDebug() << "Msg ID: " << msg.ID << " Name: " << msg.name; - - for (int j = 0; j < msg.msgSignals.length(); j++) - { - DBC_SIGNAL sig; - sig = msg.msgSignals.at(j); - qDebug() << " Signal Name: " << sig.name; - qDebug() << " Start bit: " << sig.startBit; - qDebug() << " Bit Length: " << sig.signalSize; - if (sig.valList.length() > 1) qDebug() << " Values: "; - for (int k = 0; k < sig.valList.length(); k++) - { - DBC_VAL val = sig.valList.at(k); - qDebug() << " " << val.value << " Description: " << val.descript; - } - } + filename = dialog.selectedFiles()[0]; + if (!filename.contains('.')) filename += ".dbc"; + loadedFiles[idx].saveFile(filename); } } +//the only reason to even bother sending the index is to see if +//the user wants to replace an already loaded DBC. +//Otherwise add a new one. Well, always add a new one. +//If a valid index is passed we'll remove that one and then commence +//adding. Otherwise, just go straight to adding. +void DBCHandler::loadDBCFile(int idx) +{ + if (idx > -1 && idx < loadedFiles.count()) removeDBCFile(idx); + + QString filename; + QFileDialog dialog; + + QStringList filters; + filters.append(QString(tr("DBC File (*.dbc)"))); + + dialog.setFileMode(QFileDialog::ExistingFile); + dialog.setNameFilters(filters); + dialog.setViewMode(QFileDialog::Detail); + + if (dialog.exec() == QDialog::Accepted) + { + filename = dialog.selectedFiles()[0]; + //right now there is only one file type that can be loaded here so just do it. + DBCFile newFile; + newFile.loadFile(filename); + loadedFiles.append(newFile); + } +} + +void DBCHandler::removeDBCFile(int idx) +{ + if (loadedFiles.count() == 0) return; + if (idx < 0) return; + if (idx >= loadedFiles.count()) return; + loadedFiles.removeAt(idx); +} + +void DBCHandler::removeAllFiles() +{ + loadedFiles.clear(); +} + +void DBCHandler::swapFiles(int pos1, int pos2) +{ + if (loadedFiles.count() < 2) return; + if (pos1 < 0) return; + if (pos1 >= loadedFiles.count()) return; + if (pos2 < 0) return; + if (pos2 >= loadedFiles.count()) return; + + loadedFiles.swap(pos1, pos2); +} /* - The way that the DBC file format works is kind of weird... For intel format signals you count up -from the start bit to the end bit which is (startbit + signallength - 1). At each point -bits are numbered in a sawtooth manner. What that means is that the very first bit is 0 and you count up -from there all of the way to 63 with each byte being 8 bits so bit 0 is the lowest bit in the first byte -and 8 is the lowest bit in the next byte up. The whole thing looks like this: - Bits - 7 6 5 4 3 2 1 0 - - 0 7 6 5 4 3 2 1 0 -b 1 15 14 13 12 11 10 9 8 -y 2 23 22 21 20 19 18 17 16 -t 3 31 30 29 28 27 26 25 24 -e 4 39 38 37 36 35 34 33 32 -s 5 47 46 45 44 43 42 41 40 - 6 55 54 53 52 51 50 49 48 - 7 63 62 61 60 59 58 57 56 - - For intel format you start at the start bit and keep counting up. If you have a signal size of 8 - and start at bit 12 then the bits are 12, 13, 14, 15, 16, 17, 18, 19 which spans across two bytes. - In this format each bit is worth twice as much as the last and you just keep counting up. - Bit 12 is worth 1, 13 is worth 2, 14 is worth 4, etc all of the way to bit 19 is worth 128. - - Motorola format turns most everything on its head. You count backward from the start bit but - only within the current byte. If you are about to exit the current byte you go one higher and then keep - going backward as before. Using the same example as for intel, start bit of 12 and a signal length of 8. - So, the bits are 12, 11, 10, 9, 8, 23, 22, 21. Yes, that's confusing. They now go in reverse value order too. - Bit 12 is worth 128, 11 is worth 64, etc until bit 21 is worth 1. + * Convenience function that encapsulates a whole lot of the details. + * You give it a canbus frame and it'll tell you whether there is a loaded DBC file that can + * interpret that frame for you. + * Returns NULL if there is no message definition that matches. */ - -QString DBCHandler::processSignal(const CANFrame &frame, const DBC_SIGNAL &sig) +DBC_MESSAGE* DBCHandler::findMessage(const CANFrame &frame) { - - int64_t result = 0; - bool isSigned = false; - double endResult; - - if (sig.valType == STRING) + for(int i = 0; i < loadedFiles.count(); i++) { - QString buildString; - int startByte = sig.signalSize / 8; - int bytes = sig.signalSize / 8; - for (int x = 0; x < bytes; x++) buildString.append(frame.data[startByte + x]); - return buildString; - } - - //if this is a multiplexed signal then we have to see if it is even found in the current message - if (sig.isMultiplexed) - { - if (sig.parentMessage->multiplexorSignal != NULL) + if (loadedFiles[i].getAssocBus() == -1 || frame.bus == loadedFiles[i].getAssocBus()) { - int val; - if (!processSignalInt(frame, *sig.parentMessage->multiplexorSignal, val)) return ""; - if (val != sig.multiplexValue) return ""; //signal not found in this message - } - else return ""; - } - - if (sig.valType == SIGNED_INT) isSigned = true; - if (sig.valType == SIGNED_INT || sig.valType == UNSIGNED_INT) - { - result = Utility::processIntegerSignal(frame.data, sig.startBit, sig.signalSize, sig.intelByteOrder, isSigned); - endResult = ((double)result * sig.factor) + sig.bias; - result = (int64_t)endResult; - } - else if (sig.valType == SP_FLOAT) - { - //The theory here is that we force the integer signal code to treat this as - //a 32 bit unsigned integer. This integer is then cast into a float in such a way - //that the bytes that make up the integer are instead treated as having made up - //a 32 bit single precision float. That's evil incarnate but it is very fast and small - //in terms of new code. - result = Utility::processIntegerSignal(frame.data, sig.startBit, 32, false, false); - endResult = (*((float *)(&result)) * sig.factor) + sig.bias; - } - else //double precision float - { - //like the above, this is rotten and evil and wrong in so many ways. Force - //calculation of a 64 bit integer and then cast it into a double. - result = Utility::processIntegerSignal(frame.data, 0, 64, false, false); - endResult = (*((double *)(&result)) * sig.factor) + sig.bias; - } - - QString outputString; - - outputString = sig.name + ": "; - - if (sig.valList.count() > 0) //if this is a value list type then look it up and display the proper string - { - for (int x = 0; x < sig.valList.count(); x++) - { - if (sig.valList.at(x).value == result) outputString += sig.valList.at(x).descript; + DBC_MESSAGE* msg = loadedFiles[i].messageHandler->findMsgByID(frame.ID); + if (msg != NULL) return msg; } } - else //otherwise display the actual number and unit (if it exists) - { - outputString += QString::number(endResult) + sig.unitName; - } - - return outputString; + return NULL; } -//Works quite a bit like the above version but this one is cut down and only will return int32_t which is perfect for -//uses like calculating a multiplexor value or if you know you are going to get an integer returned -//from a signal and you want to use it as-is and not have to convert back from a string. Use with caution though -//as this basically assumes the signal is an integer. -//The call syntax is different from the more generic processSignal. Instead of returning the value we return -//true or false to show whether the function succeeded. The variable to fill out is passed by reference. -bool DBCHandler::processSignalInt(const CANFrame &frame, const DBC_SIGNAL &sig, int32_t &outValue) +int DBCHandler::getFileCount() { - int32_t result = 0; - bool isSigned = false; - if (sig.valType == STRING || sig.valType == SP_FLOAT || sig.valType == DP_FLOAT) - { - return false; - } + return loadedFiles.count(); +} - //if this is a multiplexed signal then we have to see if it is even found in the current message - if (sig.isMultiplexed) +DBCFile* DBCHandler::getFileByIdx(int idx) +{ + if (loadedFiles.count() == 0) return NULL; + if (idx < 0) return NULL; + if (idx >= loadedFiles.count()) return NULL; + return &loadedFiles[idx]; +} + +DBCFile* DBCHandler::getFileByName(QString name) +{ + if (loadedFiles.count() == 0) return NULL; + for (int i = 0; i < loadedFiles.count(); i++) { - if (sig.parentMessage->multiplexorSignal != NULL) + if (loadedFiles[i].getFilename().compare(name, Qt::CaseInsensitive) == 0) { - int val; - if (!processSignalInt(frame, *sig.parentMessage->multiplexorSignal, val)) return false; - if (val != sig.multiplexValue) return false; //signal not found in this message + return &loadedFiles[i]; } - else return false; } - - if (sig.valType == SIGNED_INT) isSigned = true; - result = Utility::processIntegerSignal(frame.data, sig.startBit, sig.signalSize, sig.intelByteOrder, isSigned); - - double endResult = ((double)result * sig.factor) + sig.bias; - result = (int32_t)endResult; - - outValue = result; - return true; -} - - -//Another cut down version that will only return double precision data. This can be used on any of the types -//except STRING. Useful for when you know you'll need floating point data and don't want to incur a conversion -//back and forth to double or float. Such a use is the graphing window. -//Similar syntax to processSignalInt but with double instead. -bool DBCHandler::processSignalDouble(const CANFrame &frame, const DBC_SIGNAL &sig, double &outValue) -{ - int64_t result = 0; - bool isSigned = false; - double endResult; - - if (sig.valType == STRING) - { - return false; - } - - //if this is a multiplexed signal then we have to see if it is even found in the current message - if (sig.isMultiplexed) - { - if (sig.parentMessage->multiplexorSignal != NULL) - { - int val; - if (!processSignalInt(frame, *sig.parentMessage->multiplexorSignal, val)) return false; - if (val != sig.multiplexValue) return false; //signal not found in this message - } - else return false; - } - - if (sig.valType == SIGNED_INT) isSigned = true; - if (sig.valType == SIGNED_INT || sig.valType == UNSIGNED_INT) - { - result = Utility::processIntegerSignal(frame.data, sig.startBit, sig.signalSize, sig.intelByteOrder, isSigned); - endResult = ((double)result * sig.factor) + sig.bias; - result = (int64_t)endResult; - } - else if (sig.valType == SP_FLOAT) - { - //The theory here is that we force the integer signal code to treat this as - //a 32 bit unsigned integer. This integer is then cast into a float in such a way - //that the bytes that make up the integer are instead treated as having made up - //a 32 bit single precision float. That's evil incarnate but it is very fast and small - //in terms of new code. - result = Utility::processIntegerSignal(frame.data, sig.startBit, 32, false, false); - endResult = (*((float *)(&result)) * sig.factor) + sig.bias; - } - else //double precision float - { - //like the above, this is rotten and evil and wrong in so many ways. Force - //calculation of a 64 bit integer and then cast it into a double. - result = Utility::processIntegerSignal(frame.data, 0, 64, false, false); - endResult = (*((double *)(&result)) * sig.factor) + sig.bias; - } - - outValue = endResult; - return true; -} - -//given a byte it will reverse the bit order in that byte -unsigned char DBCHandler::reverseBits(unsigned char b) { - b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; - b = (b & 0xCC) >> 2 | (b & 0x33) << 2; - b = (b & 0xAA) >> 1 | (b & 0x55) << 1; - return b; -} - -unsigned char DBCHandler::processByte(unsigned char input, int start, int end) -{ - unsigned char output = 0, size = end - start + 1; - //first knock it down so that bottom is is start - output = input >> start; - //then mask off all bits above the proper ending - output &= ((1 << size) - 1); - return output; + return NULL; } diff --git a/dbchandler.h b/dbchandler.h index 291ac8c..137fe5f 100644 --- a/dbchandler.h +++ b/dbchandler.h @@ -6,42 +6,89 @@ #include "can_structs.h" /* - * For ease of quick testing and development this is all run together. - * It should be decoupled once the functionality is confirmed. + * TODO: + * Finish coding up the decoupled design + * */ -class DBCHandler : public QObject + +class DBCSignalHandler: public QObject { Q_OBJECT public: - explicit DBCHandler(QObject *parent = 0); - void loadDBCFile(QString); - void saveDBCFile(QString); - void listDebugging(); - QString processSignal(const CANFrame &frame, const DBC_SIGNAL &sig); - bool processSignalInt(const CANFrame &frame, const DBC_SIGNAL &sig, int32_t &outValue); - bool processSignalDouble(const CANFrame &frame, const DBC_SIGNAL &sig, double &outValue); - DBC_NODE *findNodeByName(QString name); - DBC_NODE *findNodeByIdx(int idx); + DBC_SIGNAL *findSignalByName(QString name); + DBC_SIGNAL *findSignalByIdx(int idx); + bool addSignal(DBC_SIGNAL &sig); + bool removeSignal(DBC_SIGNAL *sig); + bool removeSignal(int idx); + bool removeSignal(QString name); + void removeAllSignals(); + int getCount(); +private: + QList sigs; //signals is a reserved word or I'd have used that +}; +class DBCMessageHandler: public QObject +{ + Q_OBJECT +public: DBC_MESSAGE *findMsgByID(int id); DBC_MESSAGE *findMsgByIdx(int idx); DBC_MESSAGE *findMsgByName(QString name); + bool addMessage(DBC_MESSAGE &msg); + bool removeMessage(DBC_MESSAGE *msg); + bool removeMessageByIndex(int idx); + bool removeMessage(int ID); + bool removeMessage(QString name); + void removeAllMessages(); + int getCount(); +private: + QList messages; +}; - DBC_SIGNAL *findSignalByName(DBC_MESSAGE *msg, QString name); - DBC_SIGNAL *findSignalByIdx(DBC_MESSAGE *msg, int idx); +//technically there should be a node handler too but I'm sort of treating nodes as second class +//citizens since they aren't really all that important (to me anyway) +class DBCFile: public QObject +{ + Q_OBJECT +public: + DBCFile(); + DBCFile(const DBCFile& cpy); + DBCFile& operator=(const DBCFile& cpy); + DBC_NODE *findNodeByName(QString name); + DBC_NODE *findNodeByIdx(int idx); + void saveFile(QString); + void loadFile(QString); + QString getFullFilename(); + QString getFilename(); + QString getPath(); + int getAssocBus(); + void setAssocBus(int bus); + DBCMessageHandler *messageHandler; QList dbc_nodes; - QList dbc_messages; +private: + QString fileName; + QString filePath; + int assocBuses; //-1 = all buses, 0 = first bus, 1 = second bus +}; -signals: - -public slots: +class DBCHandler: public QObject +{ + Q_OBJECT +public: + void loadDBCFile(int); + void saveDBCFile(int); + void removeDBCFile(int); + void removeAllFiles(); + void swapFiles(int pos1, int pos2); + DBC_MESSAGE* findMessage(const CANFrame &frame); + int getFileCount(); + DBCFile* getFileByIdx(int idx); + DBCFile* getFileByName(QString name); private: - - unsigned char reverseBits(unsigned char); - unsigned char processByte(unsigned char, int, int); + QList loadedFiles; }; #endif // DBCHANDLER_H diff --git a/dbcmaineditor.cpp b/dbcmaineditor.cpp index 44c1e79..eb3b573 100644 --- a/dbcmaineditor.cpp +++ b/dbcmaineditor.cpp @@ -54,8 +54,8 @@ void DBCMainEditor::showEvent(QShowEvent* event) inhibitCellChanged = true; refreshNodesTable(); - if (dbcHandler->dbc_nodes.count() > 0) - refreshMessagesTable(&dbcHandler->dbc_nodes.at(0)); + if (dbcHandler->getFileByIdx(0)->dbc_nodes.count() > 0) + refreshMessagesTable(&dbcHandler->getFileByIdx(0)->dbc_nodes.at(0)); currRow = 0; inhibitCellChanged = false; @@ -123,9 +123,9 @@ void DBCMainEditor::deleteCurrentNode() if (nodeName.length() > 0 && nodeName.compare("Vector__XXX", Qt::CaseInsensitive) != 0) { ui->NodesTable->removeRow(thisRow); - dbcHandler->dbc_nodes.removeAt(thisRow); + dbcHandler->getFileByIdx(0)->dbc_nodes.removeAt(thisRow); inhibitCellChanged = true; - refreshMessagesTable(&dbcHandler->dbc_nodes[0]); + refreshMessagesTable(&dbcHandler->getFileByIdx(0)->dbc_nodes[0]); ui->NodesTable->selectRow(0); inhibitCellChanged = false; } @@ -137,7 +137,7 @@ void DBCMainEditor::deleteCurrentMessage() if (ui->MessagesTable->item(thisRow, 0)->text().length() > 0) { ui->MessagesTable->removeRow(thisRow); - dbcHandler->dbc_messages.removeAt(thisRow); + dbcHandler->getFileByIdx(0)->messageHandler->removeMessageByIndex(thisRow); } } @@ -156,7 +156,7 @@ void DBCMainEditor::onCellChangedNode(int row,int col) QString newName = ui->NodesTable->item(row, col)->text().simplified().replace(' ', '_'); qDebug() << "new name: " << newName; if (newName.length() == 0) return; - if (dbcHandler->findNodeByName(newName) != NULL) //duplicates an existing node! + if (dbcHandler->getFileByIdx(0)->findNodeByName(newName) != NULL) //duplicates an existing node! { QMessageBox msg; msg.setParent(0); @@ -165,8 +165,8 @@ void DBCMainEditor::onCellChangedNode(int row,int col) return; } newNode.name = newName; - dbcHandler->dbc_nodes.append(newNode); - qDebug() << "# of nodes now " << dbcHandler->dbc_nodes.count(); + dbcHandler->getFileByIdx(0)->dbc_nodes.append(newNode); + qDebug() << "# of nodes now " << dbcHandler->getFileByIdx(0)->dbc_nodes.count(); QTableWidgetItem *widgetName = new QTableWidgetItem(newName); inhibitCellChanged = true; ui->NodesTable->setItem(row, col, widgetName); @@ -178,7 +178,7 @@ void DBCMainEditor::onCellChangedNode(int row,int col) { if (col == 0) { - DBC_NODE *oldNode = dbcHandler->findNodeByIdx(row); + DBC_NODE *oldNode = dbcHandler->getFileByIdx(0)->findNodeByIdx(row); QString nodeName = ui->NodesTable->item(row, col)->text().simplified().replace(' ', '_'); if (oldNode == NULL) return; if (row != 0) oldNode->name = nodeName; @@ -192,7 +192,7 @@ void DBCMainEditor::onCellChangedNode(int row,int col) { QString nodeName = ui->NodesTable->item(row, 0)->text().simplified().replace(' ', '_'); qDebug() << "searching for node " << nodeName; - DBC_NODE *thisNode = dbcHandler->findNodeByName(nodeName); + DBC_NODE *thisNode = dbcHandler->getFileByIdx(0)->findNodeByName(nodeName); if (thisNode == NULL) return; thisNode->comment = ui->NodesTable->item(row, col)->text().simplified(); qDebug() << "New comment: " << thisNode->comment; @@ -213,7 +213,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) int msgLen; QString msgComment; DBC_MESSAGE newMsg; - DBC_NODE *node = dbcHandler->findNodeByIdx(ui->NodesTable->currentRow()); + DBC_NODE *node = dbcHandler->getFileByIdx(0)->findNodeByIdx(ui->NodesTable->currentRow()); DBC_MESSAGE *msg; if (node == NULL) { @@ -224,14 +224,14 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) msgID = Utility::ParseStringToNum(ui->MessagesTable->item(row, 0)->text()); qDebug() << "Msg ID of edited: " << msgID; - msg = dbcHandler->findMsgByID(msgID); + msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(msgID); switch(col) { case 0: //msg id if (row == ui->MessagesTable->rowCount() - 1) //new record { - if (dbcHandler->findMsgByID(msgID) != NULL) + if (dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(msgID) != NULL) { QMessageBox msg; msg.setParent(0); @@ -251,7 +251,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) break; } } - dbcHandler->dbc_messages.append(newMsg); + dbcHandler->getFileByIdx(0)->messageHandler->addMessage(newMsg); } else //editing an existing record { @@ -270,7 +270,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) if (msgName.length() == 0) return; if (row == ui->MessagesTable->rowCount() - 1) //new record { - if (dbcHandler->findMsgByName(msgName) != NULL) + if (dbcHandler->getFileByIdx(0)->messageHandler->findMsgByName(msgName) != NULL) { QMessageBox msg; msg.setParent(0); @@ -281,7 +281,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) newMsg.ID = -1; newMsg.name = msgName; newMsg.sender = node; - dbcHandler->dbc_messages.append(newMsg); + dbcHandler->getFileByIdx(0)->messageHandler->addMessage(newMsg); } else { @@ -304,7 +304,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) newMsg.name = ""; newMsg.len = msgLen; newMsg.sender = node; - dbcHandler->dbc_messages.append(newMsg); + dbcHandler->getFileByIdx(0)->messageHandler->addMessage(newMsg); } else //editing an existing record { @@ -328,7 +328,7 @@ void DBCMainEditor::onCellChangedMessage(int row,int col) newMsg.len = 0; newMsg.comment = msgComment; newMsg.sender = node; - dbcHandler->dbc_messages.append(newMsg); + dbcHandler->getFileByIdx(0)->messageHandler->addMessage(newMsg); } else //editing an existing record { @@ -363,7 +363,7 @@ void DBCMainEditor::onCellClickedNode(int row, int col) nodeName = item->text(); qDebug() << "Trying to find node with name " << nodeName; - DBC_NODE *node = dbcHandler->findNodeByName(nodeName); + DBC_NODE *node = dbcHandler->getFileByIdx(0)->findNodeByName(nodeName); //qDebug() << "Address of node: " << (int)node; inhibitCellChanged = true; refreshMessagesTable(node); @@ -376,12 +376,12 @@ void DBCMainEditor::onCellClickedMessage(int row, int col) if (col == 3) //3 is the signals field. If clicked we go to the signals dialog { QString idString = ui->MessagesTable->item(row, 0)->text(); - DBC_MESSAGE *message = dbcHandler->findMsgByID(Utility::ParseStringToNum(idString)); + DBC_MESSAGE *message = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(Utility::ParseStringToNum(idString)); sigEditor->setMessageRef(message); sigEditor->exec(); //blocks this window from being active until we're done //now update the displayed # of signals - inhibitCellChanged = true; - QTableWidgetItem *replacement = new QTableWidgetItem(QString::number(message->msgSignals.count())); + inhibitCellChanged = true; + QTableWidgetItem *replacement = new QTableWidgetItem(QString::number(message->sigHandler->getCount())); ui->MessagesTable->setItem(row, col, replacement); inhibitCellChanged = false; } @@ -394,17 +394,17 @@ void DBCMainEditor::refreshNodesTable() int rowIdx; - if (dbcHandler->findNodeByName("Vector__XXX") == NULL) + if (dbcHandler->getFileByIdx(0)->findNodeByName("Vector__XXX") == NULL) { DBC_NODE newNode; newNode.name = "Vector__XXX"; newNode.comment = "Default node if no other node is specified"; - dbcHandler->dbc_nodes.append(newNode); + dbcHandler->getFileByIdx(0)->dbc_nodes.append(newNode); } - for (int x = 0; x < dbcHandler->dbc_nodes.count(); x++) + for (int x = 0; x < dbcHandler->getFileByIdx(0)->dbc_nodes.count(); x++) { - DBC_NODE node = dbcHandler->dbc_nodes.at(x); + DBC_NODE node = dbcHandler->getFileByIdx(0)->dbc_nodes.at(x); QTableWidgetItem *nodeName = new QTableWidgetItem(node.name); QTableWidgetItem *nodeComment = new QTableWidgetItem(node.comment); rowIdx = ui->NodesTable->rowCount(); @@ -428,17 +428,17 @@ void DBCMainEditor::refreshMessagesTable(const DBC_NODE *node) if (node != NULL) { - for (int x = 0; x < dbcHandler->dbc_messages.count(); x++) + for (int x = 0; x < dbcHandler->getFileByIdx(0)->messageHandler->getCount(); x++) { - DBC_MESSAGE msg = dbcHandler->dbc_messages.at(x); - if (msg.sender == node) + DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByIdx(x); + if (msg->sender == node) { //many of these are simplistic first versions just to test functionality. - QTableWidgetItem *msgID = new QTableWidgetItem(Utility::formatNumber(msg.ID)); - QTableWidgetItem *msgName = new QTableWidgetItem(msg.name); - QTableWidgetItem *msgLen = new QTableWidgetItem(QString::number(msg.len)); - QTableWidgetItem *msgSignals = new QTableWidgetItem(QString::number(msg.msgSignals.count())); - QTableWidgetItem *msgComment = new QTableWidgetItem(msg.comment); + QTableWidgetItem *msgID = new QTableWidgetItem(Utility::formatNumber(msg->ID)); + QTableWidgetItem *msgName = new QTableWidgetItem(msg->name); + QTableWidgetItem *msgLen = new QTableWidgetItem(QString::number(msg->len)); + QTableWidgetItem *msgSignals = new QTableWidgetItem(QString::number(msg->sigHandler->getCount())); + QTableWidgetItem *msgComment = new QTableWidgetItem(msg->comment); rowIdx = ui->MessagesTable->rowCount(); ui->MessagesTable->insertRow(rowIdx); diff --git a/dbcsignaleditor.cpp b/dbcsignaleditor.cpp index 6b51190..5e1e395 100644 --- a/dbcsignaleditor.cpp +++ b/dbcsignaleditor.cpp @@ -33,9 +33,9 @@ DBCSignalEditor::DBCSignalEditor(DBCHandler *handler, QWidget *parent) : ui->comboType->addItem("DOUBLE PRECISION"); ui->comboType->addItem("STRING"); - for (int x = 0; x < dbcHandler->dbc_nodes.count(); x++) + for (int x = 0; x < dbcHandler->getFileByIdx(0)->dbc_nodes.count(); x++) { - ui->comboReceiver->addItem(dbcHandler->dbc_nodes[x].name); + ui->comboReceiver->addItem(dbcHandler->getFileByIdx(0)->dbc_nodes[x].name); } connect(ui->signalsList, SIGNAL(currentRowChanged(int)), this, SLOT(clickSignalList(int))); @@ -61,7 +61,7 @@ DBCSignalEditor::DBCSignalEditor(DBCHandler *handler, QWidget *parent) : [=]() { if (currentSignal == NULL) return; - currentSignal->receiver = dbcHandler->findNodeByName(ui->comboReceiver->currentText()); + currentSignal->receiver = dbcHandler->getFileByIdx(0)->findNodeByName(ui->comboReceiver->currentText()); }); connect(ui->comboType, &QComboBox::currentTextChanged, [=]() @@ -256,9 +256,9 @@ void DBCSignalEditor::showEvent(QShowEvent* event) refreshSignalsList(); currentSignal = NULL; - if (dbcMessage->msgSignals.count() > 0) + if (dbcMessage->sigHandler->getCount() > 0) { - currentSignal = &dbcMessage->msgSignals[0]; + currentSignal = dbcMessage->sigHandler->findSignalByIdx(0); fillSignalForm(currentSignal); fillValueTable(currentSignal); } @@ -326,7 +326,7 @@ void DBCSignalEditor::addNewSignal() newSig.intelByteOrder = true; newSig.max = 0.0; newSig.min = 0.0; - newSig.receiver = &dbcHandler->dbc_nodes[0]; + newSig.receiver = dbcHandler->getFileByIdx(0)->findNodeByIdx(0); newSig.signalSize = 1; newSig.startBit = 0; newSig.valType = UNSIGNED_INT; @@ -335,8 +335,8 @@ void DBCSignalEditor::addNewSignal() newSig.multiplexValue = 0; newSig.parentMessage = dbcMessage; ui->signalsList->addItem(newName); - dbcMessage->msgSignals.append(newSig); - if (dbcMessage->msgSignals.count() == 1) clickSignalList(0); + dbcMessage->sigHandler->addSignal(newSig); + if (dbcMessage->sigHandler->getCount() == 1) clickSignalList(0); } void DBCSignalEditor::deleteCurrentSignal() @@ -345,10 +345,10 @@ void DBCSignalEditor::deleteCurrentSignal() if (currIdx > -1) { delete(ui->signalsList->item(currIdx)); - dbcMessage->msgSignals.removeAt(currIdx); + dbcMessage->sigHandler->removeSignal(currIdx); currentSignal = NULL; currIdx = ui->signalsList->currentRow(); - if (currIdx > -1) currentSignal = &dbcMessage->msgSignals[currIdx]; + if (currIdx > -1) currentSignal = dbcMessage->sigHandler->findSignalByIdx(currIdx); fillSignalForm(currentSignal); fillValueTable(currentSignal); } @@ -367,10 +367,10 @@ void DBCSignalEditor::deleteCurrentValue() void DBCSignalEditor::refreshSignalsList() { ui->signalsList->clear(); - for (int x = 0; x < dbcMessage->msgSignals.count(); x++) + for (int x = 0; x < dbcMessage->sigHandler->getCount(); x++) { - DBC_SIGNAL sig = dbcMessage->msgSignals.at(x); - ui->signalsList->addItem(sig.name); + DBC_SIGNAL *sig = dbcMessage->sigHandler->findSignalByIdx(x); + ui->signalsList->addItem(sig->name); } } @@ -518,12 +518,11 @@ void DBCSignalEditor::clickSignalList(int row) if (row < 0) return; //qDebug() << ui->signalsList->item(row)->text(); - DBC_SIGNAL *thisSig = dbcHandler->findSignalByName(dbcMessage, ui->signalsList->item(row)->text()); + DBC_SIGNAL *thisSig = dbcMessage->sigHandler->findSignalByName(ui->signalsList->item(row)->text()); if (thisSig == NULL) return; currentSignal = thisSig; fillSignalForm(thisSig); fillValueTable(thisSig); - } void DBCSignalEditor::bitfieldClicked(int x, int y) @@ -546,15 +545,15 @@ void DBCSignalEditor::generateUsedBits() uint8_t usedBits[8] = {0,0,0,0,0,0,0,0}; int startBit, endBit; - for (int x = 0; x < dbcMessage->msgSignals.count(); x++) + for (int x = 0; x < dbcMessage->sigHandler->getCount(); x++) { - DBC_SIGNAL sig = dbcMessage->msgSignals.at(x); + DBC_SIGNAL *sig = dbcMessage->sigHandler->findSignalByIdx(x); - startBit = sig.startBit; + startBit = sig->startBit; - if (sig.intelByteOrder) + if (sig->intelByteOrder) { - endBit = startBit + sig.signalSize - 1; + endBit = startBit + sig->signalSize - 1; if (startBit < 0) startBit = 0; if (endBit > 63) endBit = 63; for (int y = startBit; y <= endBit; y++) @@ -566,7 +565,7 @@ void DBCSignalEditor::generateUsedBits() else //big endian / motorola format { //much more irritating than the intel version... - int size = sig.signalSize; + int size = sig->signalSize; while (size > 0) { int byt = startBit / 8; diff --git a/graphingwindow.cpp b/graphingwindow.cpp index c5900a1..2f9463e 100644 --- a/graphingwindow.cpp +++ b/graphingwindow.cpp @@ -759,10 +759,10 @@ void GraphingWindow::loadDefinitions() { gp.isDBCSignal = true; //tokens[2] is the signal name. Need to use the message ID and this name to look it up - DBC_MESSAGE *msg = dbcHandler->findMsgByID(gp.ID); + DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(gp.ID); if (msg != NULL) { - DBC_SIGNAL *sig = dbcHandler->findSignalByName(msg, tokens[2]); + DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(tokens[2]); if (sig == NULL) dbcMissing = true; gp.signal = tokens[2]; } @@ -850,12 +850,12 @@ void GraphingWindow::appendToGraph(GraphParams ¶ms, CANFrame &frame) if (params.isDBCSignal) { double tempValue; - DBC_MESSAGE *msg = dbcHandler->findMsgByID(params.ID); + DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(params.ID); DBC_SIGNAL *sig = NULL; - if (msg) sig = dbcHandler->findSignalByName(msg, params.signal); + if (msg) sig = msg->sigHandler->findSignalByName(params.signal); if (sig == NULL) return; //if the given signal was found and successfully processed in this frame then add it to the graph - if (dbcHandler->processSignalDouble(frame, *sig, tempValue)) + if (sig->processAsDouble(frame, tempValue)) { //qDebug() << "tempValue: " << tempValue; if (secondsMode) @@ -989,8 +989,8 @@ void GraphingWindow::createGraph(GraphParams ¶ms, bool createGraphParam) if (params.isDBCSignal) { - msg = dbcHandler->findMsgByID(params.ID); - if (msg) sig = dbcHandler->findSignalByName(msg, params.signal); + msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByID(params.ID); + if (msg) sig = msg->sigHandler->findSignalByName(params.signal); if (sig == NULL) return; qDebug() << "New signal graph: " << params.signal <<" in ID:" << params.ID; } @@ -1023,7 +1023,7 @@ void GraphingWindow::createGraph(GraphParams ¶ms, bool createGraphParam) for (int j = 0; j < numEntries; j++) { //if the given signal was found and successfully processed in this frame then add it to the graph - if (dbcHandler->processSignalDouble(frameCache[j], *sig, tempValue)) + if (sig->processAsDouble(frameCache[j], tempValue)) { //qDebug() << "tempValue: " << tempValue; if (secondsMode) diff --git a/mainwindow.cpp b/mainwindow.cpp index baeca16..b50ca67 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -16,10 +16,9 @@ Single / Multi state - The goal is to find bits that change based on toggles or fuzzy scope - Try to find potential places where a given value might be stored - offer guesses and the program tries to find candidates for you or, try to find things that appear to be multi-byte integers -Make the graphing window use signals either exclusively or as an option for each signal graphed Allow multiple DBC files to be loaded in the program and also allow them to be associated with a bus Allow scripts to read/write signals from DBC files -allow scripts to load DBC files in support of the script +allow scripts to load DBC files in support of the script - maybe the graphing system too. */ QString MainWindow::loadedFileName = ""; @@ -756,49 +755,15 @@ void MainWindow::handleLoadFilters() void MainWindow::handleLoadDBC() { - QString filename; - QFileDialog dialog(this); + dbcHandler->loadDBCFile(-1); - QStringList filters; - filters.append(QString(tr("DBC File (*.dbc)"))); - - dialog.setFileMode(QFileDialog::ExistingFile); - dialog.setNameFilters(filters); - dialog.setViewMode(QFileDialog::Detail); - - if (dialog.exec() == QDialog::Accepted) - { - filename = dialog.selectedFiles()[0]; - //right now there is only one file type that can be loaded here so just do it. - dbcHandler->loadDBCFile(filename); - //dbcHandler->listDebugging(); - QStringList fileList = filename.split('/'); - lbStatusDatabase.setText(fileList[fileList.length() - 1] + tr(" loaded.")); - } + //lbStatusDatabase.setText(fileList[fileList.length() - 1] + tr(" loaded.")); } void MainWindow::handleSaveDBC() { - QString filename; - QFileDialog dialog(this); - - QStringList filters; - filters.append(QString(tr("DBC File (*.dbc)"))); - - dialog.setFileMode(QFileDialog::AnyFile); - dialog.setNameFilters(filters); - dialog.setViewMode(QFileDialog::Detail); - dialog.setAcceptMode(QFileDialog::AcceptSave); - - if (dialog.exec() == QDialog::Accepted) - { - filename = dialog.selectedFiles()[0]; - if (!filename.contains('.')) filename += ".dbc"; - dbcHandler->saveDBCFile(filename); - QStringList fileList = filename.split('/'); - lbStatusDatabase.setText(fileList[fileList.length() - 1] + tr(" loaded.")); - } - + dbcHandler->saveDBCFile(0); + //lbStatusDatabase.setText(fileList[fileList.length() - 1] + tr(" loaded.")); } void MainWindow::handleSaveDecoded() @@ -857,14 +822,18 @@ Data Bytes: 88 10 00 13 BB 00 06 00 builderString = ""; if (dbcHandler != NULL) { - DBC_MESSAGE *msg = dbcHandler->findMsgByID(thisFrame.ID); + DBC_MESSAGE *msg = dbcHandler->findMessage(thisFrame); if (msg != NULL) { - for (int j = 0; j < msg->msgSignals.length(); j++) + for (int j = 0; j < msg->sigHandler->getCount(); j++) { - builderString.append("\t" + dbcHandler->processSignal(thisFrame, msg->msgSignals.at(j))); - builderString.append("\n"); + QString temp; + if (msg->sigHandler->findSignalByIdx(j)->processAsText(thisFrame, temp)) + { + builderString.append("\t" + temp); + builderString.append("\n"); + } } } builderString.append("\n"); diff --git a/newgraphdialog.cpp b/newgraphdialog.cpp index 7761472..cd34ec3 100644 --- a/newgraphdialog.cpp +++ b/newgraphdialog.cpp @@ -150,9 +150,9 @@ void NewGraphDialog::loadMessages() { ui->cbMessages->clear(); if (dbcHandler == NULL) return; - for (int x = 0; x < dbcHandler->dbc_messages.count(); x++) + for (int x = 0; x < dbcHandler->getFileByIdx(0)->messageHandler->getCount(); x++) { - ui->cbMessages->addItem(dbcHandler->dbc_messages[x].name); + ui->cbMessages->addItem(dbcHandler->getFileByIdx(0)->messageHandler->findMsgByIdx(x)->name); } } @@ -163,13 +163,13 @@ void NewGraphDialog::loadSignals(int idx) //in the data structure so it should have been possible to just //look it up based on index but by name is probably safer and this operation //is not time critical at all. - DBC_MESSAGE *msg = dbcHandler->findMsgByName(ui->cbMessages->currentText()); + DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByName(ui->cbMessages->currentText()); if (msg == NULL) return; ui->cbSignals->clear(); - for (int x = 0; x < msg->msgSignals.count(); x++) + for (int x = 0; x < msg->sigHandler->getCount(); x++) { - ui->cbSignals->addItem(msg->msgSignals[x].name); + ui->cbSignals->addItem(msg->sigHandler->findSignalByIdx(x)->name); } } @@ -177,11 +177,11 @@ void NewGraphDialog::fillFormFromSignal(int idx) { Q_UNUSED(idx); GraphParams params; - DBC_MESSAGE *msg = dbcHandler->findMsgByName(ui->cbMessages->currentText()); + DBC_MESSAGE *msg = dbcHandler->getFileByIdx(0)->messageHandler->findMsgByName(ui->cbMessages->currentText()); if (msg == NULL) return; - DBC_SIGNAL *sig = dbcHandler->findSignalByName(msg, ui->cbSignals->currentText()); + DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(ui->cbSignals->currentText()); if (sig == NULL) return;