From 8ff3459c75b4405dac64c8096e3ea1c72b569cce Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Wed, 7 Dec 2022 21:51:44 -0500 Subject: [PATCH] Fixed several DBC related bugs, expanded support for FD frames in the DBC editor windows. Should more or less function now even for FD frames. --- dbc/dbc_classes.cpp | 4 +-- dbc/dbchandler.cpp | 36 ++++++++++---------- dbc/dbcmaineditor.cpp | 2 +- dbc/dbcsignaleditor.cpp | 73 +++++++++++++++++++++++++++++++---------- dbc/dbcsignaleditor.h | 2 +- utility.h | 4 +-- 6 files changed, 81 insertions(+), 40 deletions(-) diff --git a/dbc/dbc_classes.cpp b/dbc/dbc_classes.cpp index a562ba7..99c477c 100644 --- a/dbc/dbc_classes.cpp +++ b/dbc/dbc_classes.cpp @@ -166,7 +166,7 @@ bool DBC_SIGNAL::processAsText(const CANFrame &frame, QString &outString, bool o } //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.payload(), 0, 64, intelByteOrder, false); + result = Utility::processIntegerSignal(frame.payload(), startBit, 64, intelByteOrder, false); endResult = (*((double *)(&result)) * factor) + bias; } @@ -306,7 +306,7 @@ bool DBC_SIGNAL::processAsDouble(const CANFrame &frame, double &outValue) } //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.payload(), 0, 64, false, false); + result = Utility::processIntegerSignal(frame.payload(), startBit, 64, false, false); endResult = (*((double *)(&result)) * factor) + bias; } cachedValue = endResult; diff --git a/dbc/dbchandler.cpp b/dbc/dbchandler.cpp index b756290..b65de70 100644 --- a/dbc/dbchandler.cpp +++ b/dbc/dbchandler.cpp @@ -460,7 +460,7 @@ DBC_MESSAGE* DBCFile::parseMessageLine(QString line) DBC_MESSAGE *msgPtr; qDebug() << "Found a BO line"; - regex.setPattern("^BO\\_ (\\w+) (\\w+) *: (\\w+) (\\w+)"); + regex.setPattern("^BO\\_ (\\w+) ([-\\w]+) *: (\\w+) ([-\\w]+)"); match = regex.match(line); //captured 1 = the ID in decimal //captured 2 = The message name @@ -499,7 +499,7 @@ DBC_SIGNAL* DBCFile::parseSignalLine(QString line, DBC_MESSAGE *msg) sig.isMultiplexor = false; qDebug() << "Found a SG line"; - regex.setPattern("^SG\\_ *(\\w+) +M *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); + regex.setPattern("^SG\\_ *([-\\w]+) +M *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); match = regex.match(line); if (match.hasMatch()) @@ -510,7 +510,7 @@ DBC_SIGNAL* DBCFile::parseSignalLine(QString line, DBC_MESSAGE *msg) } else { - regex.setPattern("^SG\\_ *(\\w+) +m(\\d+) *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); + regex.setPattern("^SG\\_ *([-\\w]+) +m(\\d+) *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); match = regex.match(line); if (match.hasMatch()) { @@ -523,7 +523,7 @@ DBC_SIGNAL* DBCFile::parseSignalLine(QString line, DBC_MESSAGE *msg) } else { - regex.setPattern("^SG\\_ *(\\w+) +m(\\d+)M *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); + regex.setPattern("^SG\\_ *([-\\w]+) +m(\\d+)M *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); match = regex.match(line); if (match.hasMatch()) { @@ -537,7 +537,7 @@ DBC_SIGNAL* DBCFile::parseSignalLine(QString line, DBC_MESSAGE *msg) else { qDebug() << "standard signal"; - regex.setPattern("^SG\\_ *(\\w+) *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); + regex.setPattern("^SG\\_ *([-\\w]+) *: *(\\d+)\\|(\\d+)@(\\d+)([\\+|\\-]) \\(([0-9.+\\-eE]+),([0-9.+\\-eE]+)\\) \\[([0-9.+\\-eE]+)\\|([0-9.+\\-eE]+)\\] \\\"(.*)\\\" (.*)"); match = regex.match(line); sig.isMultiplexed = false; sig.isMultiplexor = false; @@ -629,7 +629,7 @@ bool DBCFile::parseSignalMultiplexValueLine(QString line) QRegularExpressionMatch match; qDebug() << "Found a multiplex definition line"; - regex.setPattern("^SG\\_MUL\\_VAL\\_ (\\d+) (\\w+) (\\w+) (\\d+)\\-(\\d+);"); + regex.setPattern("^SG\\_MUL\\_VAL\\_ (\\d+) ([-\\w]+) ([-\\w]+) (\\d+)\\-(\\d+);"); match = regex.match(line); //captured 1 is message ID //Captured 2 is signal name @@ -650,6 +650,8 @@ bool DBCFile::parseSignalMultiplexValueLine(QString line) //now need to add "thisSignal" to the children multiplexed signals of "parentSignal" parentSignal->multiplexedChildren.append(thisSignal); thisSignal->multiplexParent = parentSignal; + thisSignal->multiplexLowValue = match.captured(4).toInt(); + thisSignal->multiplexHighValue = match.captured(5).toInt(); return true; } } @@ -664,7 +666,7 @@ bool DBCFile::parseValueLine(QString line) QRegularExpressionMatch match; qDebug() << "Found a value definition line"; - regex.setPattern("^VAL\\_ (\\w+) (\\w+) (.*);"); + regex.setPattern("^VAL\\_ (\\w+) ([-\\w]+) (.*);"); match = regex.match(line); //captured 1 is the ID to match against //captured 2 is the signal name to match against @@ -709,7 +711,7 @@ bool DBCFile::parseAttributeLine(QString line) QRegularExpression regex; QRegularExpressionMatch match; - regex.setPattern("^BA\\_ \\\"*(\\w+)\\\"* BO\\_ (\\d+) \\\"*([#\\w]+)\\\"*"); + regex.setPattern("^BA\\_ \\\"*([-\\w]+)\\\"* BO\\_ (\\d+) \\\"*([#\\w]+)\\\"*"); match = regex.match(line); //captured 1 is the attribute name //captured 2 is the message ID number (frame ID) @@ -740,7 +742,7 @@ bool DBCFile::parseAttributeLine(QString line) } } - regex.setPattern("^BA\\_ \\\"*(\\w+)\\\"* SG\\_ (\\d+) \\\"*(\\w+)\\\"* \\\"*([#\\w]+)\\\"*"); + regex.setPattern("^BA\\_ \\\"*([-\\w]+)\\\"* SG\\_ (\\d+) \\\"*([-\\w]+)\\\"* \\\"*([#\\w]+)\\\"*"); match = regex.match(line); //captured 1 is the attribute name //captured 2 is the message ID number (frame ID) @@ -774,7 +776,7 @@ bool DBCFile::parseAttributeLine(QString line) } } - regex.setPattern("^BA\\_ \\\"*(\\w+)\\\"* BU\\_ \\\"*(\\w+)\\\"* \\\"*([#\\w]+)\\\"*"); + regex.setPattern("^BA\\_ \\\"*([-\\w]+)\\\"* BU\\_ \\\"*([-\\w]+)\\\"* \\\"*([#\\w]+)\\\"*"); match = regex.match(line); //captured 1 is the attribute name //captured 2 is the name of the node @@ -812,7 +814,7 @@ bool DBCFile::parseDefaultAttrLine(QString line) QRegularExpression regex; QRegularExpressionMatch match; - regex.setPattern("^BA\\_DEF\\_DEF\\_ \\\"*(\\w+)\\\"* \\\"*([#\\w]*)\\\"*"); + regex.setPattern("^BA\\_DEF\\_DEF\\_ \\\"*([-\\w]+)\\\"* \\\"*([#\\w]*)\\\"*"); match = regex.match(line); //captured 1 is the name of the attribute //captured 2 is the default value for that attribute @@ -949,7 +951,7 @@ bool DBCFile::loadFile(QString fileName) if (line.startsWith("CM_ SG_ ")) { qDebug() << "Found an SG comment line"; - regex.setPattern("^CM\\_ SG\\_ *(\\w+) *(\\w+) *\\\"(.*)\\\";"); + regex.setPattern("^CM\\_ SG\\_ *(\\w+) *([-\\w]+) *\\\"(.*)\\\";"); match = regex.match(line); //captured 1 is the ID to match against to get to the message //captured 2 is the signal name from that message @@ -988,7 +990,7 @@ bool DBCFile::loadFile(QString fileName) if (line.startsWith("CM_ BU_ ")) { qDebug() << "Found a BU comment line"; - regex.setPattern("^CM\\_ BU\\_ *(\\w+) *\\\"(.*)\\\";"); + regex.setPattern("^CM\\_ BU\\_ *([-\\w]+) *\\\"(.*)\\\";"); match = regex.match(line); //captured 1 is the Node name //captured 2 is the comment itself @@ -1361,7 +1363,7 @@ bool DBCFile::saveFile(QString fileName) attrValOutput.append("BA_ \"" + val.attrName + "\" BU_ "); switch (val.value.type()) { - case QMetaType::QString: + case QVariant::Type::String: attrValOutput.append("\"" + val.value.toString() + "\";\n"); break; default: @@ -1406,7 +1408,7 @@ bool DBCFile::saveFile(QString fileName) attrValOutput.append("BA_ \"" + val.attrName + "\" BO_ " + QString::number(ID) + " "); switch (val.value.type()) { - case QMetaType::QString: + case QVariant::Type::String: attrValOutput.append("\"" + val.value.toString() + "\";\n"); break; default: @@ -1483,7 +1485,7 @@ bool DBCFile::saveFile(QString fileName) attrValOutput.append("BA_ \"" + val.attrName + "\" SG_ " + QString::number(ID) + " " + sig->name + " "); switch (val.value.type()) { - case QMetaType::QString: + case QVariant::Type::String: attrValOutput.append("\"" + val.value.toString() + "\";\n"); break; default: @@ -1600,7 +1602,7 @@ bool DBCFile::saveFile(QString fileName) if (sig->isMultiplexed) { msgOutput.append("SG_MUL_VAL_ " + QString::number(ID) + " "); - msgOutput.append(sig->name + " " + sig->parentMessage->name + " "); + msgOutput.append(sig->name + " " + sig->multiplexParent->name + " "); msgOutput.append(QString::number(sig->multiplexLowValue) + "-" + QString::number(sig->multiplexHighValue) + ";"); msgOutput.append("\n"); outFile->write(msgOutput.toUtf8()); diff --git a/dbc/dbcmaineditor.cpp b/dbc/dbcmaineditor.cpp index 7da57a4..e62dae6 100644 --- a/dbc/dbcmaineditor.cpp +++ b/dbc/dbcmaineditor.cpp @@ -860,7 +860,7 @@ void DBCMainEditor::deleteCurrentTreeItem() } else { - qDebug() << "WTF, could not find the signal in the map. That should not happen."; + qDebug() << "Could not find the signal in the map. That should not happen."; } } break; diff --git a/dbc/dbcsignaleditor.cpp b/dbc/dbcsignaleditor.cpp index 3788781..3b089e7 100644 --- a/dbc/dbcsignaleditor.cpp +++ b/dbc/dbcsignaleditor.cpp @@ -37,7 +37,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : ui->bitfield->setMode(GridMode::SIGNAL_VIEW); - connect(ui->bitfield, SIGNAL(gridClicked(int,int)), this, SLOT(bitfieldClicked(int,int))); + connect(ui->bitfield, SIGNAL(gridClicked(int)), this, SLOT(bitfieldClicked(int))); connect(ui->valuesTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomMenuValues(QPoint))); ui->valuesTable->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->valuesTable, SIGNAL(cellChanged(int,int)), this, SLOT(onValuesCellChanged(int,int))); @@ -74,12 +74,23 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : break; case 2: currentSignal->valType = SP_FLOAT; - if (currentSignal->startBit > 39) currentSignal->startBit = 39; + if (dbcMessage) //if we have a good msg reference we can use it to get the # of bytes expected. + { + int maxBit = ((dbcMessage->len * 8) - 32 + 7); + if (maxBit < 0) maxBit = 0; + if (currentSignal->startBit > maxBit) currentSignal->startBit = maxBit; + } + else if (currentSignal->startBit > 39) currentSignal->startBit = 39; currentSignal->signalSize = 32; break; case 3: currentSignal->valType = DP_FLOAT; - currentSignal->startBit = 7; //has to be! + if (dbcMessage) + { + int maxBit = ((dbcMessage->len * 8) - 64 + 7); + if (currentSignal->startBit > maxBit) currentSignal->startBit = maxBit; + } + else currentSignal->startBit = 7; //has to be! currentSignal->signalSize = 64; break; case 4: @@ -166,7 +177,11 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : int temp; temp = Utility::ParseStringToNum(ui->txtBitLength->text()); if (temp < 1) return; - if (temp > 64) return; + if (dbcMessage) + { + if (temp > (int)(dbcMessage->len * 8)) return; + } + else if (temp > 64) return; if (currentSignal->signalSize != temp) dbcFile->setDirtyFlag(); if (currentSignal->valType != SP_FLOAT && currentSignal->valType != DP_FLOAT) currentSignal->signalSize = temp; @@ -197,7 +212,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : connect(ui->txtMultiplexHigh, &QLineEdit::editingFinished, [=]() { - if (currentSignal == nullptr) return; + if (!currentSignal) return; int temp; temp = Utility::ParseStringToNum(ui->txtMultiplexHigh->text()); if (currentSignal->multiplexHighValue != temp) dbcFile->setDirtyFlag(); @@ -208,6 +223,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : connect(ui->rbExtended, &QRadioButton::toggled, [=](bool state) { + if (!currentSignal) return; if (state && currentSignal) //signal is now set as an extended multiplex/multiplexor { currentSignal->isMultiplexed = true; @@ -224,6 +240,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : connect(ui->rbMultiplexed, &QRadioButton::toggled, [=](bool state) { + if (!currentSignal) return; if (state && currentSignal) //signal is now set as a multiplexed signal { currentSignal->isMultiplexed = true; @@ -240,6 +257,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : connect(ui->rbMultiplexor, &QRadioButton::toggled, [=](bool state) { + if (!currentSignal) return; if (state && currentSignal) //signal is now set as a multiplexed signal { //don't allow this signal to be a multiplexor if there is already one for this message. @@ -258,6 +276,7 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : connect(ui->rbNotMulti, &QRadioButton::toggled, [=](bool state) { + if (!currentSignal) return; if (state && currentSignal) //signal is now set as a multiplexed signal { currentSignal->isMultiplexed = false; @@ -448,7 +467,7 @@ void DBCSignalEditor::deleteCurrentValue() /* WARNING: fillSignalForm can be called recursively since it is in the listener of cbIntelFormat */ void DBCSignalEditor::fillSignalForm(DBC_SIGNAL *sig) { - unsigned char bitpattern[8]; + unsigned char bitpattern[64]; inhibitMsgProc = true; @@ -467,7 +486,7 @@ void DBCSignalEditor::fillSignalForm(DBC_SIGNAL *sig) ui->rbMultiplexed->setChecked(false); ui->rbMultiplexor->setChecked(false); ui->rbNotMulti->setChecked(true); - memset(bitpattern, 0, 8); //clear it out + memset(bitpattern, 0, 64); //clear it out ui->bitfield->setReference(bitpattern, false); ui->bitfield->updateData(bitpattern, true); ui->comboReceiver->setCurrentIndex(0); @@ -522,7 +541,7 @@ void DBCSignalEditor::fillSignalForm(DBC_SIGNAL *sig) ui->txtMultiplexHigh->setEnabled(sig->isMultiplexed); ui->cbMultiplexParent->setEnabled(sig->isMultiplexed); - memset(bitpattern, 0, 8); //clear it out first. + memset(bitpattern, 0, 64); //clear it out first. int startBit, endBit; @@ -535,7 +554,7 @@ void DBCSignalEditor::fillSignalForm(DBC_SIGNAL *sig) { endBit = startBit + sig->signalSize - 1; if (startBit < 0) startBit = 0; - if (endBit > 63) endBit = 63; + if (endBit > 511) endBit = 511; for (int y = startBit; y <= endBit; y++) { int byt = y / 8; @@ -553,7 +572,7 @@ void DBCSignalEditor::fillSignalForm(DBC_SIGNAL *sig) size--; if ((startBit % 8) == 0) startBit += 15; else startBit--; - if (startBit > 63) startBit = 63; + if (startBit > 511) startBit = 511; } } @@ -625,26 +644,43 @@ void DBCSignalEditor::fillValueTable(DBC_SIGNAL *sig) inhibitCellChanged = false; } -void DBCSignalEditor::bitfieldClicked(int x, int y) +void DBCSignalEditor::bitfieldClicked(int bit) { - int bit = (7 - x) + (y * 8); if (currentSignal == nullptr) return; currentSignal->startBit = bit; if (currentSignal->valType == SP_FLOAT) { - if (currentSignal->startBit > 31) currentSignal->startBit = 39; + if (dbcMessage) + { + int maxBit = ((dbcMessage->len * 8) - 32 + 7); + if (maxBit < 0) maxBit = 0; + if (currentSignal->startBit > maxBit) currentSignal->startBit = maxBit; + } + else if (currentSignal->startBit > 31) currentSignal->startBit = 39; } if (currentSignal->valType == DP_FLOAT) - currentSignal->startBit = 7; + { + if (dbcMessage) + { + int maxBit = ((dbcMessage->len * 8) - 64 + 7); + if (maxBit < 0) maxBit = 0; + if (currentSignal->startBit > maxBit) currentSignal->startBit = maxBit; + } + else currentSignal->startBit = 7; + } fillSignalForm(currentSignal); } void DBCSignalEditor::generateUsedBits() { - uint8_t usedBits[8] = {0,0,0,0,0,0,0,0}; + uint8_t usedBits[64]; int startBit, endBit; + memset(usedBits, 0, 64); + + if (!dbcMessage || !dbcMessage->sigHandler) return; + for (int x = 0; x < dbcMessage->sigHandler->getCount(); x++) { DBC_SIGNAL *sig = dbcMessage->sigHandler->findSignalByIdx(x); @@ -663,7 +699,8 @@ void DBCSignalEditor::generateUsedBits() { endBit = startBit + sig->signalSize - 1; if (startBit < 0) startBit = 0; - if (endBit > 63) endBit = 63; + int maxBit = (dbcMessage->len * 8) - 1; + if (endBit > maxBit) endBit = maxBit; for (int y = startBit; y <= endBit; y++) { int byt = y / 8; @@ -683,9 +720,11 @@ void DBCSignalEditor::generateUsedBits() size--; if ((startBit % 8) == 0) startBit += 15; else startBit--; - if (startBit > 63) startBit = 63; + int maxBit = (dbcMessage->len * 8) - 1; + if (startBit > maxBit) startBit = maxBit; } } } ui->bitfield->setUsed(usedBits, false); + ui->bitfield->setBytesToDraw(dbcMessage->len); } diff --git a/dbc/dbcsignaleditor.h b/dbc/dbcsignaleditor.h index 37f5e6d..c5e5fca 100644 --- a/dbc/dbcsignaleditor.h +++ b/dbc/dbcsignaleditor.h @@ -26,7 +26,7 @@ signals: void updatedTreeInfo(DBC_SIGNAL *sig); private slots: - void bitfieldClicked(int x, int y); + void bitfieldClicked(int bit); void onValuesCellChanged(int row,int col); void onCustomMenuValues(QPoint); void deleteCurrentValue(); diff --git a/utility.h b/utility.h index 78819f7..e86b69c 100644 --- a/utility.h +++ b/utility.h @@ -256,7 +256,7 @@ public: bit = startBit; for (int bitpos = 0; bitpos < sigSize; bitpos++) { - if (bit < 64) { + if (bit < 512) { int bytePos = bit / 8; if (bytePos >= data.count()) return 0; //error! if (data[bit / 8] & (1 << (bit % 8))) @@ -271,7 +271,7 @@ public: bit = startBit; for (int bitpos = 0; bitpos < sigSize; bitpos++) { - if (bit < 64) { + if (bit < 512) { int bytePos = bit / 8; if (bytePos >= data.count()) return 0; //error! if (data[bit / 8] & (1 << (bit % 8)))