From a8cde9fc5f5d39527ccb5d1eafee3b2e9059de41 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Wed, 17 Feb 2021 21:19:30 -0500 Subject: [PATCH] Fixed several seg faults and other errors in DBC code --- dbc/dbc_classes.cpp | 22 ++++++++++++ dbc/dbc_classes.h | 1 + dbc/dbchandler.cpp | 78 +++++++++++++++++++++++++------------------ dbc/dbchandler.h | 4 +-- dbc/dbcmaineditor.cpp | 16 +++++---- 5 files changed, 81 insertions(+), 40 deletions(-) diff --git a/dbc/dbc_classes.cpp b/dbc/dbc_classes.cpp index fb67985..904dc2e 100644 --- a/dbc/dbc_classes.cpp +++ b/dbc/dbc_classes.cpp @@ -6,8 +6,30 @@ DBC_MESSAGE::DBC_MESSAGE() { sigHandler = new DBCSignalHandler; + ID = 0; + len = 0; + multiplexorSignal = nullptr; + sender = nullptr; } +DBC_SIGNAL::DBC_SIGNAL() +{ + bias = 0; + isMultiplexed = false; + isMultiplexor = false; + max = 1; + min = 0; + multiplexValue = 0; + factor = 1.0; + intelByteOrder = false; + parentMessage = nullptr; + receiver = nullptr; + signalSize = 1; + startBit = 1; + valType = DBC_SIG_VAL_TYPE::UNSIGNED_INT; +} + + /* 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 diff --git a/dbc/dbc_classes.h b/dbc/dbc_classes.h index 3006737..a2eb3b9 100644 --- a/dbc/dbc_classes.h +++ b/dbc/dbc_classes.h @@ -106,6 +106,7 @@ public: //TODO: this is sloppy. It shouldn't all be public! QList attributes; QList valList; + DBC_SIGNAL(); bool processAsText(const CANFrame &frame, QString &outString, bool outputName = true); bool processAsInt(const CANFrame &frame, int32_t &outValue); bool processAsDouble(const CANFrame &frame, double &outValue); diff --git a/dbc/dbchandler.cpp b/dbc/dbchandler.cpp index f296c89..8332958 100644 --- a/dbc/dbchandler.cpp +++ b/dbc/dbchandler.cpp @@ -760,7 +760,7 @@ bool DBCFile::parseDefaultAttrLine(QString line) return false; } -void DBCFile::loadFile(QString fileName) +bool DBCFile::loadFile(QString fileName) { QFile *inFile = new QFile(fileName); QString line, rawLine; @@ -778,7 +778,8 @@ void DBCFile::loadFile(QString fileName) if (!inFile->open(QIODevice::ReadOnly | QIODevice::Text)) { delete inFile; - return; + qDebug() << "Could not load the file!"; + return false; } qDebug() << "Starting DBC load"; @@ -1040,6 +1041,7 @@ void DBCFile::loadFile(QString fileName) filePath = fileName.left(fileName.length() - this->fileName.length()); assocBuses = -1; isDirty = false; + return true; } QVariant DBCFile::processAttributeVal(QString input, DBC_ATTRIBUTE_VAL_TYPE typ) @@ -1157,7 +1159,7 @@ bool DBCFile::parseAttribute(QString inpString, DBC_ATTRIBUTE &attr) return goodAttr; } -void DBCFile::saveFile(QString fileName) +bool DBCFile::saveFile(QString fileName) { int nodeNumber = 1; int msgNumber = 1; @@ -1169,7 +1171,7 @@ void DBCFile::saveFile(QString fileName) if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text)) { delete outFile; - return; + return false; } //right now it outputs a standard hard coded boilerplate @@ -1455,6 +1457,7 @@ void DBCFile::saveFile(QString fileName) 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()); + return true; } void DBCHandler::saveDBCFile(int idx) @@ -1544,9 +1547,16 @@ int DBCHandler::createBlankFile() DBCFile* DBCHandler::loadDBCFile(QString filename) { DBCFile newFile; - newFile.loadFile(filename); - loadedFiles.append(newFile); - return &loadedFiles.last(); + if (newFile.loadFile(filename)) + { + loadedFiles.append(newFile); + } + else + { + //createBlankFile(); + } + if (loadedFiles.count()> 0) return &loadedFiles.last(); + else return nullptr; } //the only reason to even bother sending the index is to see if @@ -1846,40 +1856,44 @@ DBCHandler::DBCHandler() // Load previously saved DBC file settings QSettings settings; int filecount = settings.value("DBC/FileCount", 0).toInt(); + qDebug() << "Previously loaded DBC file count: " << filecount; for (int i=0; isetAssocBus(bus); + if (file) + { + int bus = settings.value("DBC/AssocBus_" + QString(i),0).toInt(); + file->setAssocBus(bus); - MatchingCriteria_t matchingCriteria = (MatchingCriteria_t)settings.value("DBC/MatchingCriteria_" + QString(i),0).toInt(); + MatchingCriteria_t matchingCriteria = (MatchingCriteria_t)settings.value("DBC/MatchingCriteria_" + QString(i),0).toInt(); - DBC_ATTRIBUTE attr; + DBC_ATTRIBUTE attr; - attr.attrType = MESSAGE; - attr.defaultValue = matchingCriteria; - attr.enumVals.clear(); - attr.lower = 0; - attr.upper = 0; - attr.name = "matchingcriteria"; - attr.valType = QINT; - file->dbc_attributes.append(attr); - file->messageHandler->setMatchingCriteria(matchingCriteria); + attr.attrType = MESSAGE; + attr.defaultValue = matchingCriteria; + attr.enumVals.clear(); + attr.lower = 0; + attr.upper = 0; + attr.name = "matchingcriteria"; + attr.valType = QINT; + file->dbc_attributes.append(attr); + file->messageHandler->setMatchingCriteria(matchingCriteria); - bool filterLabeling = settings.value("DBC/FilterLabeling_" + QString(i),0).toBool(); - attr.attrType = MESSAGE; - attr.defaultValue = filterLabeling; - attr.enumVals.clear(); - attr.lower = 0; - attr.upper = 0; - attr.name = "filterlabeling"; - attr.valType = QINT; - file->dbc_attributes.append(attr); - file->messageHandler->setFilterLabeling(filterLabeling); + bool filterLabeling = settings.value("DBC/FilterLabeling_" + QString(i),0).toBool(); + attr.attrType = MESSAGE; + attr.defaultValue = filterLabeling; + attr.enumVals.clear(); + attr.lower = 0; + attr.upper = 0; + attr.name = "filterlabeling"; + attr.valType = QINT; + file->dbc_attributes.append(attr); + file->messageHandler->setFilterLabeling(filterLabeling); - qInfo() << "Loaded DBC file" << filename << " (bus:" << bus - << ", Matching Criteria:" << (int)matchingCriteria << "Filter labeling: " << (filterLabeling?"enabled":"disabled") << ")"; + qInfo() << "Loaded DBC file" << filename << " (bus:" << bus + << ", Matching Criteria:" << (int)matchingCriteria << "Filter labeling: " << (filterLabeling?"enabled":"disabled") << ")"; + } } } diff --git a/dbc/dbchandler.h b/dbc/dbchandler.h index f2341eb..11bd480 100644 --- a/dbc/dbchandler.h +++ b/dbc/dbchandler.h @@ -76,8 +76,8 @@ public: DBC_ATTRIBUTE *findAttributeByName(QString name); DBC_ATTRIBUTE *findAttributeByIdx(int idx); void findAttributesByType(DBC_ATTRIBUTE_TYPE typ, QList *list); - void saveFile(QString); - void loadFile(QString); + bool saveFile(QString); + bool loadFile(QString); QString getFullFilename(); QString getFilename(); QString getPath(); diff --git a/dbc/dbcmaineditor.cpp b/dbc/dbcmaineditor.cpp index 967a9f2..76dd76c 100644 --- a/dbc/dbcmaineditor.cpp +++ b/dbc/dbcmaineditor.cpp @@ -370,8 +370,8 @@ void DBCMainEditor::updatedMessage(DBC_MESSAGE *msg) item->setText(0, msgInfo); //editor could have changed the parent Node too. Have to figure out which node //is parent in the GUI and compare that to parent in the data. - DBC_NODE *oldParent = dbcFile->findNodeByName(item->parent()->text(0)); - if (oldParent != msg->sender) + DBC_NODE *oldParent = dbcFile->findNodeByName(item->parent()->text(0).split(" - ")[0]); + if (oldParent != msg->sender && oldParent) { qDebug() << "Changed parent of message. Trying to rehome it."; QTreeWidgetItem *newParent = nullptr; @@ -448,7 +448,11 @@ void DBCMainEditor::newMessage() nodeItem = msgItem->parent(); } - DBC_NODE *node = dbcFile->findNodeByName(nodeItem->data(0, Qt::DisplayRole).toString()); + //if there was a comment this will find the location of the comment and snip it out. + QString nodeName = nodeItem->data(0, Qt::DisplayRole).toString().split(" - ")[0]; + + DBC_NODE *node = dbcFile->findNodeByName(nodeName); + if (!node) node = dbcFile->findNodeByIdx(0); DBC_MESSAGE msg; DBC_MESSAGE *msgPtr; if (msgItem) @@ -467,17 +471,17 @@ void DBCMainEditor::newMessage() } else { - msg.name = nodeItem->text(0) + "Msg" + QString::number(randGen.bounded(500)); + msg.name = nodeName + "Msg" + QString::number(randGen.bounded(500)); msg.ID = 0; msg.len = 0; } } else { - msg.name = nodeItem->text(0) + "Msg" + QString::number(randGen.bounded(500)); + msg.name = nodeName + "Msg" + QString::number(randGen.bounded(500)); msg.ID = 0; msg.len = 0; - } + } msg.sender = node; dbcFile->messageHandler->addMessage(msg);