Skip to content

Commit 76b9737

Browse files
authored
Merge pull request collin80#537 from qurrent-llc/fix_customsendcrash
Fix custom frame sender crashing
2 parents 8a929c1 + f587144 commit 76b9737

16 files changed

+203
-119
lines changed

canframemodel.cpp

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ void CANFrameModel::setAllFilters(bool state)
236236
* quicksort on the columns and interpret the columns numerically. But, correct or not, this implementation is quite fast
237237
* and sorts the columns properly.
238238
*/
239-
uint64_t CANFrameModel::getCANFrameVal(int row, Column col)
239+
uint64_t CANFrameModel::getCANFrameVal(QVector<CANFrame> *frames, int row, Column col)
240240
{
241241
uint64_t temp = 0;
242-
if (row >= frames.count()) return 0;
243-
CANFrame frame = frames[row];
242+
if (row >= frames->count()) return 0;
243+
CANFrame frame = frames->at(row);
244244
switch (col)
245245
{
246246
case Column::TimeStamp:
@@ -279,18 +279,18 @@ void CANFrameModel::qSortCANFrameAsc(QVector<CANFrame> *frames, Column column, i
279279
qDebug() << "Lower " << lowerBound << " Upper" << upperBound;
280280
if (lowerBound < upperBound)
281281
{
282-
uint64_t piv = getCANFrameVal(lowerBound + (upperBound - lowerBound) / 2, column);
282+
uint64_t piv = getCANFrameVal(frames, lowerBound + (upperBound - lowerBound) / 2, column);
283283
i = lowerBound - 1;
284284
j = upperBound + 1;
285285
for (;;){
286286
do {
287287
i++;
288-
} while ((i < upperBound) && getCANFrameVal(i, column) < piv);
288+
} while ((i < upperBound) && getCANFrameVal(frames, i, column) < piv);
289289

290290
do
291291
{
292292
j--;
293-
} while ((j > lowerBound) && getCANFrameVal(j, column) > piv);
293+
} while ((j > lowerBound) && getCANFrameVal(frames, j, column) > piv);
294294
if (i < j) {
295295
CANFrame temp = frames->at(i);
296296
frames->replace(i, frames->at(j));
@@ -310,18 +310,18 @@ void CANFrameModel::qSortCANFrameDesc(QVector<CANFrame> *frames, Column column,
310310
qDebug() << "Lower " << lowerBound << " Upper" << upperBound;
311311
if (lowerBound < upperBound)
312312
{
313-
uint64_t piv = getCANFrameVal(lowerBound + (upperBound - lowerBound) / 2, column);
313+
uint64_t piv = getCANFrameVal(frames, lowerBound + (upperBound - lowerBound) / 2, column);
314314
i = lowerBound - 1;
315315
j = upperBound + 1;
316316
for (;;){
317317
do {
318318
i++;
319-
} while ((i < upperBound) && getCANFrameVal(i, column) > piv);
319+
} while ((i < upperBound) && getCANFrameVal(frames, i, column) > piv);
320320

321321
do
322322
{
323323
j--;
324-
} while ((j > lowerBound) && getCANFrameVal(j, column) < piv);
324+
} while ((j > lowerBound) && getCANFrameVal(frames, j, column) < piv);
325325
if (i < j) {
326326
CANFrame temp = frames->at(i);
327327
frames->replace(i, frames->at(j));
@@ -338,11 +338,13 @@ void CANFrameModel::qSortCANFrameDesc(QVector<CANFrame> *frames, Column column,
338338
void CANFrameModel::sortByColumn(int column)
339339
{
340340
sortDirAsc = !sortDirAsc;
341-
//beginResetModel();
342-
if (sortDirAsc) qSortCANFrameAsc(&frames, Column(column), 0, frames.count()-1);
343-
else qSortCANFrameDesc(&frames, Column(column), 0, frames.count()-1);
344-
//endResetModel();
345-
sendRefresh();
341+
if (sortDirAsc) qSortCANFrameAsc(&filteredFrames, Column(column), 0, filteredFrames.count()-1);
342+
else qSortCANFrameDesc(&filteredFrames, Column(column), 0, filteredFrames.count()-1);
343+
344+
mutex.lock();
345+
beginResetModel();
346+
endResetModel();
347+
mutex.unlock();
346348
}
347349

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

366368
idAugmented = frame.frameId();
367369
idAugmented = idAugmented + (frame.bus << 29ull);
368-
if (!overWriteFrames.contains(idAugmented))
369-
{
370-
frame.timedelta = 0;
371-
frame.frameCount = 1;
372-
overWriteFrames.insert(idAugmented, frame);
373-
}
374-
else
370+
if (filters[frame.frameId()] && busFilters[frame.bus])
375371
{
376-
frame.timedelta = frame.timeStamp().microSeconds() - overWriteFrames[idAugmented].timeStamp().microSeconds();
377-
frame.frameCount = overWriteFrames[idAugmented].frameCount + 1;
378-
overWriteFrames[idAugmented] = frame;
372+
if (!overWriteFrames.contains(idAugmented))
373+
{
374+
frame.timedelta = 0;
375+
frame.frameCount = 1;
376+
overWriteFrames.insert(idAugmented, frame);
377+
}
378+
else
379+
{
380+
frame.timedelta = frame.timeStamp().microSeconds() - overWriteFrames[idAugmented].timeStamp().microSeconds();
381+
frame.frameCount = overWriteFrames[idAugmented].frameCount + 1;
382+
overWriteFrames[idAugmented] = frame;
383+
}
379384
}
380385
}
381386
//Then replace the old list of frames with just the unique list
382-
frames.clear();
383-
frames.append(overWriteFrames.values().toVector());
384-
frames.reserve(preallocSize);
387+
//frames.clear();
388+
//frames.append(overWriteFrames.values().toVector());
389+
//frames.reserve(preallocSize);
385390

386391
filteredFrames.clear();
392+
filteredFrames.append(overWriteFrames.values().toVector());
387393
filteredFrames.reserve(preallocSize);
388394

389-
for (int i = 0; i < frames.count(); i++)
395+
/*for (int i = 0; i < frames.count(); i++)
390396
{
391397
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
392398
{
393399
filteredFrames.append(frames[i]);
394400
}
395-
}
401+
}*/
396402

397403
endResetModel();
398404
mutex.unlock();
@@ -705,20 +711,32 @@ void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
705711
else //yes, overwrite dups
706712
{
707713
bool found = false;
708-
for (int i = 0; i < frames.count(); i++)
714+
// for (int i = 0; i < frames.count(); i++)
715+
// {
716+
// if ( (frames[i].frameId() == tempFrame.frameId()) && (frames[i].bus == tempFrame.bus) )
717+
// {
718+
// tempFrame.frameCount = frames[i].frameCount + 1;
719+
// tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - frames[i].timeStamp().microSeconds();
720+
// frames.replace(i, tempFrame);
721+
// found = true;
722+
// break;
723+
// }
724+
// }
725+
for (int i = 0; i < filteredFrames.count(); i++)
709726
{
710-
if ( (frames[i].frameId() == tempFrame.frameId()) && (frames[i].bus == tempFrame.bus) )
727+
if ( (filteredFrames[i].frameId() == tempFrame.frameId()) && (filteredFrames[i].bus == tempFrame.bus) )
711728
{
712-
tempFrame.frameCount = frames[i].frameCount + 1;
713-
tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - frames[i].timeStamp().microSeconds();
714-
frames.replace(i, tempFrame);
729+
tempFrame.frameCount = filteredFrames[i].frameCount + 1;
730+
tempFrame.timedelta = tempFrame.timeStamp().microSeconds() - filteredFrames[i].timeStamp().microSeconds();
731+
filteredFrames.replace(i, tempFrame);
715732
found = true;
716733
break;
717734
}
718735
}
736+
frames.append(tempFrame);
719737
if (!found)
720738
{
721-
frames.append(tempFrame);
739+
//frames.append(tempFrame);
722740
if (filters[tempFrame.frameId()] && busFilters[tempFrame.bus])
723741
{
724742
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count(), filteredFrames.count());
@@ -750,16 +768,20 @@ void CANFrameModel::addFrames(const CANConnection*, const QVector<CANFrame>& pFr
750768
{
751769
if(frames.length() > frames.capacity() * 0.99)
752770
{
771+
mutex.lock();
753772
qDebug() << "Frames count: " << frames.length() << " of " << frames.capacity() << " capacity, removing first " << (int)(frames.capacity() * 0.05) << " frames";
754773
frames.remove(0, (int)(frames.capacity() * 0.05));
755774
qDebug() << "Frames removed, new count: " << frames.length();
775+
mutex.unlock();
756776
}
757777

758778
if(filteredFrames.length() > filteredFrames.capacity() * 0.99)
759779
{
780+
mutex.lock();
760781
qDebug() << "filteredFrames count: " << filteredFrames.length() << " of " << filteredFrames.capacity() << " capacity, removing first " << (int)(filteredFrames.capacity() * 0.05) << " frames";
761782
filteredFrames.remove(0, (int)(filteredFrames.capacity() * 0.05));
762783
qDebug() << "filteredFrames removed, new count: " << filteredFrames.length();
784+
mutex.unlock();
763785
}
764786

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

776798
void CANFrameModel::sendRefresh()
777799
{
778-
qDebug() << "Sending mass refresh";
779-
QVector<CANFrame> tempContainer;
780-
int count = frames.count();
781-
for (int i = 0; i < count; i++)
800+
qDebug() << "Sending mass refresh";
801+
802+
if(overwriteDups)
782803
{
783-
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
804+
recalcOverwrite();
805+
}
806+
else
807+
{
808+
QVector<CANFrame> tempContainer;
809+
int count = frames.count();
810+
for (int i = 0; i < count; i++)
784811
{
785-
tempContainer.append(frames[i]);
812+
if (filters[frames[i].frameId()] && busFilters[frames[i].bus])
813+
{
814+
tempContainer.append(frames[i]);
815+
}
786816
}
787-
}
788-
mutex.lock();
789-
beginResetModel();
790-
filteredFrames.clear();
791-
filteredFrames.append(tempContainer);
792-
filteredFrames.reserve(preallocSize);
793817

794-
lastUpdateNumFrames = 0;
795-
endResetModel();
796-
mutex.unlock();
818+
mutex.lock();
819+
beginResetModel();
820+
filteredFrames.clear();
821+
filteredFrames.append(tempContainer);
822+
filteredFrames.reserve(preallocSize);
823+
lastUpdateNumFrames = 0;
824+
endResetModel();
825+
mutex.unlock();
826+
}
797827
}
798828

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

813843
if (lastUpdateNumFrames == 0 && !overwriteDups) return 0;
814-
if (filteredFrames.count() == 0) return 0;
844+
//if (filteredFrames.count() == 0) return 0;
815845

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

canframemodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public slots:
7878
private:
7979
void qSortCANFrameAsc(QVector<CANFrame>* frames, Column column, int lowerBound, int upperBound);
8080
void qSortCANFrameDesc(QVector<CANFrame>* frames, Column column, int lowerBound, int upperBound);
81-
uint64_t getCANFrameVal(int row, Column col);
81+
uint64_t getCANFrameVal(QVector<CANFrame> *frames, int row, Column col);
8282
bool any_filters_are_configured(void);
8383
bool any_busfilters_are_configured(void);
8484

connections/canconmanager.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ bool CANConManager::sendFrame(const CANFrame& pFrame)
204204
{
205205
int busBase = 0;
206206
CANFrame workingFrame = pFrame;
207-
CANFrame *txFrame;
208207

209208
if (mConns.count() == 0)
210209
{
@@ -228,10 +227,7 @@ bool CANConManager::sendFrame(const CANFrame& pFrame)
228227
workingFrame.setTimeStamp(QCanBusFrame::TimeStamp(0, mElapsedTimer.nsecsElapsed() / 1000));
229228
//workingFrame.timestamp -= mTimestampBasis;
230229
}
231-
txFrame = conn->getQueue().get();
232-
QCoreApplication::processEvents();
233-
*txFrame = workingFrame;
234-
conn->getQueue().queue();
230+
235231
return conn->sendFrame(workingFrame);
236232
}
237233
busBase += conn->getNumBuses();

connections/canconnection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ bool CANConnection::sendFrame(const CANFrame& pFrame)
173173
return ret;
174174
}
175175

176+
CANFrame *txFrame;
177+
txFrame = getQueue().get();
178+
*txFrame = pFrame;
179+
getQueue().queue();
180+
176181
return piSendFrame(pFrame);
177182
}
178183

connections/connectionwindow.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,12 @@ void ConnectionWindow::currentTabChanged(int newIdx)
426426

427427
void ConnectionWindow::currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
428428
{
429-
Q_UNUSED(previous);
430-
431429
int selIdx = current.row();
432-
433-
disconnect(connModel->getAtIdx(previous.row()), SIGNAL(debugOutput(QString)), nullptr, nullptr);
430+
CANConnection* prevConn = connModel->getAtIdx(previous.row());
431+
if(prevConn != nullptr)
432+
disconnect(prevConn, SIGNAL(debugOutput(QString)), nullptr, nullptr);
434433
disconnect(this, SIGNAL(sendDebugData(QByteArray)), nullptr, nullptr);
435434

436-
437435
/* set parameters */
438436
if (selIdx == -1) {
439437
ui->groupBus->setEnabled(false);

connections/socketcand.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void SocketCANd::sendBytesToTCP(const QByteArray &bytes, int busNum)
6464
byt = (unsigned char)byt;
6565
buildDebug = buildDebug % QString::number(byt, 16) % " ";
6666
}
67-
sendDebug(buildDebug);
67+
//sendDebug(buildDebug);
6868

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

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

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

298+
QByteArray buildData;
298299
buildData.resize(framelength);
299300

300301
int c;
@@ -366,15 +367,15 @@ void SocketCANd::invokeReadTCPData()
366367

367368
void SocketCANd::readTCPData(int busNum)
368369
{
369-
QByteArray data;
370+
QString data;
370371

371-
if (tcpClient[busNum]) data = tcpClient[busNum]->readAll();
372+
if (tcpClient[busNum])
373+
data = QString(tcpClient[busNum]->readAll());
372374
//sendDebug("Got data from TCP. Len = " % QString::number(data.length()));
373375
//qDebug() << "Received datagramm: " << data;
374376
procRXData(data, busNum);
375377
}
376378

377-
378379
void SocketCANd::procRXData(QString data, int busNum)
379380
{
380381
if (data != "")

connections/socketcand.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ private slots:
7373
int hostPort;
7474
QList<QString> hostCanIDs;
7575
int framesRapid;
76-
QByteArray buildData;
7776
QVarLengthArray<MODE> rx_state;
7877
CANFrame buildFrame;
7978
QVarLengthArray<QString> unprocessedData;

0 commit comments

Comments
 (0)