88
99CANDriverKvaser::CANDriverKvaser (canRecvCallback_t onRecvCallback,
1010 std::function<void (std::string *)> onErrorCallback, std::vector<uint32_t> &canBusChannelIDs, Config &config) :
11- CANDriver(onRecvCallback, onErrorCallback)
11+ CANDriver(std::move( onRecvCallback), std::move( onErrorCallback) )
1212{
1313 // arbitration bus parameters
1414 int32_t bitrate = config[" /CAN/BUS/ARBITRATION/bitrate" ];
@@ -29,6 +29,8 @@ CANDriverKvaser::CANDriverKvaser(canRecvCallback_t onRecvCallback,
2929
3030 nlohmann::json busExtra = config[" /CAN/BUS_EXTRA" ];
3131
32+ receiveThread = std::make_unique<CanKvaserReceiveThread>(onRecvCallback);
33+
3234 canStatus stat;
3335 for (auto &channelID : canBusChannelIDs)
3436 {
@@ -167,15 +169,15 @@ void CANDriverKvaser::OnCANCallback(int handle, void *driver, unsigned int event
167169 std::string errorMsg = " canNOTIFY_ERROR: " + canDriver->CANError (stat);
168170 canDriver->onErrorCallback (&errorMsg);
169171 }
170- break ;
172+ break ;
171173 }
172174 case canNOTIFY_STATUS:
173175 {
174176 uint64_t statFlags = 0 ;
175177 stat = canReadStatus (handle, &statFlags);
176178 Debug::print (" canNOTIFY_STATUS changed" );
177179 if (statFlags & canSTAT_OVERRUN) {
178- Debug::print (" canNOTIFY_STATUS: buffer overflow" );
180+ Debug::warning (" canNOTIFY_STATUS: buffer overflow" );
179181 }
180182 break ;
181183 }
@@ -208,11 +210,14 @@ void CANDriverKvaser::OnCANCallback(int handle, void *driver, unsigned int event
208210 try
209211 {
210212 // Copy the received data into a new buffer for the thread
211- auto threadData = std::make_unique<uint8_t []>(dlc);
212- std::copy_n (data, dlc, threadData.get ());
213- std::thread ([canDriver, canBusChannelID, id, dlc, softwareTime, threadData = std::move (threadData)]() {
214- canDriver->onRecvCallback (canBusChannelID, id, threadData.get (), dlc, softwareTime, canDriver);
215- }).detach ();
213+ std::unique_ptr<RawKvaserMessage> threadData = std::make_unique<RawKvaserMessage>();
214+ std::ranges::copy (data, threadData->data );
215+ threadData->dlc = dlc;
216+ threadData->busChannelID = canBusChannelID;
217+ threadData->messageID = id;
218+ threadData->timestamp = softwareTime;
219+ threadData->driver = canDriver;
220+ canDriver->receiveThread ->pushMessage (std::move (threadData));
216221 }
217222 catch (const std::exception& e)
218223 {
@@ -236,7 +241,7 @@ void CANDriverKvaser::OnCANCallback(int handle, void *driver, unsigned int event
236241 }
237242 Debug::print (" \t\t CAN Status Flags: 0x%016x" , statFlags);
238243 }
239-
244+
240245 stat = canRead (handle, &id, data, &dlc, &flags, ×tamp);
241246 }
242247 // stat is either canERR_NOMSG or any different error code
@@ -246,7 +251,7 @@ void CANDriverKvaser::OnCANCallback(int handle, void *driver, unsigned int event
246251 }
247252 break ;
248253 }
249-
254+
250255 default :
251256 // TODO: MP since this thread is managed by the canlib, should we really throw exceptions?
252257 throw std::runtime_error (" Callback got called with neither ERROR nor RX, gigantic UFF" );
0 commit comments