Skip to content

Commit

Permalink
Merge pull request #537 from qurrent-llc/fix_customsendcrash
Browse files Browse the repository at this point in the history
Fix custom frame sender crashing
  • Loading branch information
collin80 authored Nov 29, 2022
2 parents 8a929c1 + f587144 commit 76b9737
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 119 deletions.
132 changes: 81 additions & 51 deletions canframemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ void CANFrameModel::setAllFilters(bool state)
* quicksort on the columns and interpret the columns numerically. But, correct or not, this implementation is quite fast
* and sorts the columns properly.
*/
uint64_t CANFrameModel::getCANFrameVal(int row, Column col)
uint64_t CANFrameModel::getCANFrameVal(QVector<CANFrame> *frames, int row, Column col)
{
uint64_t temp = 0;
if (row >= frames.count()) return 0;
CANFrame frame = frames[row];
if (row >= frames->count()) return 0;
CANFrame frame = frames->at(row);
switch (col)
{
case Column::TimeStamp:
Expand Down Expand Up @@ -279,18 +279,18 @@ void CANFrameModel::qSortCANFrameAsc(QVector<CANFrame> *frames, Column column, i
qDebug() << "Lower " << lowerBound << " Upper" << upperBound;
if (lowerBound < upperBound)
{
uint64_t piv = getCANFrameVal(lowerBound + (upperBound - lowerBound) / 2, column);
uint64_t piv = getCANFrameVal(frames, lowerBound + (upperBound - lowerBound) / 2, column);
i = lowerBound - 1;
j = upperBound + 1;
for (;;){
do {
i++;
} while ((i < upperBound) && getCANFrameVal(i, column) < piv);
} while ((i < upperBound) && getCANFrameVal(frames, i, column) < piv);

do
{
j--;
} while ((j > lowerBound) && getCANFrameVal(j, column) > piv);
} while ((j > lowerBound) && getCANFrameVal(frames, j, column) > piv);
if (i < j) {
CANFrame temp = frames->at(i);
frames->replace(i, frames->at(j));
Expand All @@ -310,18 +310,18 @@ void CANFrameModel::qSortCANFrameDesc(QVector<CANFrame> *frames, Column column,
qDebug() << "Lower " << lowerBound << " Upper" << upperBound;
if (lowerBound < upperBound)
{
uint64_t piv = getCANFrameVal(lowerBound + (upperBound - lowerBound) / 2, column);
uint64_t piv = getCANFrameVal(frames, lowerBound + (upperBound - lowerBound) / 2, column);
i = lowerBound - 1;
j = upperBound + 1;
for (;;){
do {
i++;
} while ((i < upperBound) && getCANFrameVal(i, column) > piv);
} while ((i < upperBound) && getCANFrameVal(frames, i, column) > piv);

do
{
j--;
} while ((j > lowerBound) && getCANFrameVal(j, column) < piv);
} while ((j > lowerBound) && getCANFrameVal(frames, j, column) < piv);
if (i < j) {
CANFrame temp = frames->at(i);
frames->replace(i, frames->at(j));
Expand All @@ -338,11 +338,13 @@ void CANFrameModel::qSortCANFrameDesc(QVector<CANFrame> *frames, Column column,
void CANFrameModel::sortByColumn(int column)
{
sortDirAsc = !sortDirAsc;
//beginResetModel();
if (sortDirAsc) qSortCANFrameAsc(&frames, Column(column), 0, frames.count()-1);
else qSortCANFrameDesc(&frames, Column(column), 0, frames.count()-1);
//endResetModel();
sendRefresh();
if (sortDirAsc) qSortCANFrameAsc(&filteredFrames, Column(column), 0, filteredFrames.count()-1);
else qSortCANFrameDesc(&filteredFrames, Column(column), 0, filteredFrames.count()-1);

mutex.lock();
beginResetModel();
endResetModel();
mutex.unlock();
}

//End of custom sorting code
Expand All @@ -365,34 +367,38 @@ void CANFrameModel::recalcOverwrite()

idAugmented = frame.frameId();
idAugmented = idAugmented + (frame.bus << 29ull);
if (!overWriteFrames.contains(idAugmented))
{
frame.timedelta = 0;
frame.frameCount = 1;
overWriteFrames.insert(idAugmented, frame);
}
else
if (filters[frame.frameId()] && busFilters[frame.bus])
{
frame.timedelta = frame.timeStamp().microSeconds() - overWriteFrames[idAugmented].timeStamp().microSeconds();
frame.frameCount = overWriteFrames[idAugmented].frameCount + 1;
overWriteFrames[idAugmented] = frame;
if (!overWriteFrames.contains(idAugmented))
{
frame.timedelta = 0;
frame.frameCount = 1;
overWriteFrames.insert(idAugmented, frame);
}
else
{
frame.timedelta = frame.timeStamp().microSeconds() - overWriteFrames[idAugmented].timeStamp().microSeconds();
frame.frameCount = overWriteFrames[idAugmented].frameCount + 1;
overWriteFrames[idAugmented] = frame;
}
}
}
//Then replace the old list of frames with just the unique list
frames.clear();
frames.append(overWriteFrames.values().toVector());
frames.reserve(preallocSize);
//frames.clear();
//frames.append(overWriteFrames.values().toVector());
//frames.reserve(preallocSize);

filteredFrames.clear();
filteredFrames.append(overWriteFrames.values().toVector());
filteredFrames.reserve(preallocSize);

for (int i = 0; i < frames.count(); i++)
/*for (int i = 0; i < frames.count(); i++)
{
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
{
filteredFrames.append(frames[i]);
}
}
}*/

endResetModel();
mutex.unlock();
Expand Down Expand Up @@ -705,20 +711,32 @@ void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
else //yes, overwrite dups
{
bool found = false;
for (int i = 0; i < frames.count(); i++)
// for (int i = 0; i < frames.count(); i++)
// {
// if ( (frames[i].frameId() == tempFrame.frameId()) && (frames[i].bus == tempFrame.bus) )
// {
// tempFrame.frameCount = frames[i].frameCount + 1;
// tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - frames[i].timeStamp().microSeconds();
// frames.replace(i, tempFrame);
// found = true;
// break;
// }
// }
for (int i = 0; i < filteredFrames.count(); i++)
{
if ( (frames[i].frameId() == tempFrame.frameId()) && (frames[i].bus == tempFrame.bus) )
if ( (filteredFrames[i].frameId() == tempFrame.frameId()) && (filteredFrames[i].bus == tempFrame.bus) )
{
tempFrame.frameCount = frames[i].frameCount + 1;
tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - frames[i].timeStamp().microSeconds();
frames.replace(i, tempFrame);
tempFrame.frameCount = filteredFrames[i].frameCount + 1;
tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - filteredFrames[i].timeStamp().microSeconds();
filteredFrames.replace(i, tempFrame);
found = true;
break;
}
}
frames.append(tempFrame);
if (!found)
{
frames.append(tempFrame);
//frames.append(tempFrame);
if (filters[tempFrame.frameId()] && busFilters[tempFrame.bus])
{
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count(), filteredFrames.count());
Expand Down Expand Up @@ -750,16 +768,20 @@ void CANFrameModel::addFrames(const CANConnection*, const QVector<CANFrame>& pFr
{
if(frames.length() > frames.capacity() * 0.99)
{
mutex.lock();
qDebug() << "Frames count: " << frames.length() << " of " << frames.capacity() << " capacity, removing first " << (int)(frames.capacity() * 0.05) << " frames";
frames.remove(0, (int)(frames.capacity() * 0.05));
qDebug() << "Frames removed, new count: " << frames.length();
mutex.unlock();
}

if(filteredFrames.length() > filteredFrames.capacity() * 0.99)
{
mutex.lock();
qDebug() << "filteredFrames count: " << filteredFrames.length() << " of " << filteredFrames.capacity() << " capacity, removing first " << (int)(filteredFrames.capacity() * 0.05) << " frames";
filteredFrames.remove(0, (int)(filteredFrames.capacity() * 0.05));
qDebug() << "filteredFrames removed, new count: " << filteredFrames.length();
mutex.unlock();
}

foreach(const CANFrame& frame, pFrames)
Expand All @@ -775,25 +797,33 @@ void CANFrameModel::addFrames(const CANConnection*, const QVector<CANFrame>& pFr

void CANFrameModel::sendRefresh()
{
qDebug() << "Sending mass refresh";
QVector<CANFrame> tempContainer;
int count = frames.count();
for (int i = 0; i < count; i++)
qDebug() << "Sending mass refresh";

if(overwriteDups)
{
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
recalcOverwrite();
}
else
{
QVector<CANFrame> tempContainer;
int count = frames.count();
for (int i = 0; i < count; i++)
{
tempContainer.append(frames[i]);
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
{
tempContainer.append(frames[i]);
}
}
}
mutex.lock();
beginResetModel();
filteredFrames.clear();
filteredFrames.append(tempContainer);
filteredFrames.reserve(preallocSize);

lastUpdateNumFrames = 0;
endResetModel();
mutex.unlock();
mutex.lock();
beginResetModel();
filteredFrames.clear();
filteredFrames.append(tempContainer);
filteredFrames.reserve(preallocSize);
lastUpdateNumFrames = 0;
endResetModel();
mutex.unlock();
}
}

void CANFrameModel::sendRefresh(int pos)
Expand All @@ -811,7 +841,7 @@ int CANFrameModel::sendBulkRefresh()
if (lastUpdateNumFrames <= 0) return 0;

if (lastUpdateNumFrames == 0 && !overwriteDups) return 0;
if (filteredFrames.count() == 0) return 0;
//if (filteredFrames.count() == 0) return 0;

//qDebug() << "Bulk refresh of " << lastUpdateNumFrames;

Expand Down
2 changes: 1 addition & 1 deletion canframemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public slots:
private:
void qSortCANFrameAsc(QVector<CANFrame>* frames, Column column, int lowerBound, int upperBound);
void qSortCANFrameDesc(QVector<CANFrame>* frames, Column column, int lowerBound, int upperBound);
uint64_t getCANFrameVal(int row, Column col);
uint64_t getCANFrameVal(QVector<CANFrame> *frames, int row, Column col);
bool any_filters_are_configured(void);
bool any_busfilters_are_configured(void);

Expand Down
6 changes: 1 addition & 5 deletions connections/canconmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ bool CANConManager::sendFrame(const CANFrame& pFrame)
{
int busBase = 0;
CANFrame workingFrame = pFrame;
CANFrame *txFrame;

if (mConns.count() == 0)
{
Expand All @@ -228,10 +227,7 @@ bool CANConManager::sendFrame(const CANFrame& pFrame)
workingFrame.setTimeStamp(QCanBusFrame::TimeStamp(0, mElapsedTimer.nsecsElapsed() / 1000));
//workingFrame.timestamp -= mTimestampBasis;
}
txFrame = conn->getQueue().get();
QCoreApplication::processEvents();
*txFrame = workingFrame;
conn->getQueue().queue();

return conn->sendFrame(workingFrame);
}
busBase += conn->getNumBuses();
Expand Down
5 changes: 5 additions & 0 deletions connections/canconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ bool CANConnection::sendFrame(const CANFrame& pFrame)
return ret;
}

CANFrame *txFrame;
txFrame = getQueue().get();
*txFrame = pFrame;
getQueue().queue();

return piSendFrame(pFrame);
}

Expand Down
8 changes: 3 additions & 5 deletions connections/connectionwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,12 @@ void ConnectionWindow::currentTabChanged(int newIdx)

void ConnectionWindow::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
Q_UNUSED(previous);

int selIdx = current.row();

disconnect(connModel->getAtIdx(previous.row()), SIGNAL(debugOutput(QString)), nullptr, nullptr);
CANConnection* prevConn = connModel->getAtIdx(previous.row());
if(prevConn != nullptr)
disconnect(prevConn, SIGNAL(debugOutput(QString)), nullptr, nullptr);
disconnect(this, SIGNAL(sendDebugData(QByteArray)), nullptr, nullptr);


/* set parameters */
if (selIdx == -1) {
ui->groupBus->setEnabled(false);
Expand Down
15 changes: 8 additions & 7 deletions connections/socketcand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void SocketCANd::sendBytesToTCP(const QByteArray &bytes, int busNum)
byt = (unsigned char)byt;
buildDebug = buildDebug % QString::number(byt, 16) % " ";
}
sendDebug(buildDebug);
//sendDebug(buildDebug);

if (tcpClient[busNum]) tcpClient[busNum]->write(bytes);
}
Expand All @@ -77,9 +77,9 @@ void SocketCANd::sendStringToTCP(const char* data, int busNum)
return;
}

QString buildDebug;
buildDebug = "Send data to " + hostIP.toString() + ":" + QString::number(hostPort) + " -> " + data;
sendDebug(buildDebug);
//QString buildDebug;
//buildDebug = "Send data to " + hostIP.toString() + ":" + QString::number(hostPort) + " -> " + data;
//sendDebug(buildDebug);
//qInfo() << buildDebug;

if (tcpClient[busNum]) tcpClient[busNum]->write(data);
Expand Down Expand Up @@ -295,6 +295,7 @@ QString SocketCANd::decodeFrames(QString data, int busNum)
framelength = frameParsed[3].length() * 0.5;
}

QByteArray buildData;
buildData.resize(framelength);

int c;
Expand Down Expand Up @@ -366,15 +367,15 @@ void SocketCANd::invokeReadTCPData()

void SocketCANd::readTCPData(int busNum)
{
QByteArray data;
QString data;

if (tcpClient[busNum]) data = tcpClient[busNum]->readAll();
if (tcpClient[busNum])
data = QString(tcpClient[busNum]->readAll());
//sendDebug("Got data from TCP. Len = " % QString::number(data.length()));
//qDebug() << "Received datagramm: " << data;
procRXData(data, busNum);
}


void SocketCANd::procRXData(QString data, int busNum)
{
if (data != "")
Expand Down
1 change: 0 additions & 1 deletion connections/socketcand.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private slots:
int hostPort;
QList<QString> hostCanIDs;
int framesRapid;
QByteArray buildData;
QVarLengthArray<MODE> rx_state;
CANFrame buildFrame;
QVarLengthArray<QString> unprocessedData;
Expand Down
Loading

0 comments on commit 76b9737

Please sign in to comment.