rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Private Attributes
IsoTpRx Class Reference

#include <isotp.h>

Inheritance diagram for IsoTpRx:
Inheritance graph
[legend]
Collaboration diagram for IsoTpRx:
Collaboration graph
[legend]

Public Member Functions

 IsoTpRx (size_t p_busIndex, uint32_t p_rxFrameId, uint32_t p_txFrameId)
 
 ~IsoTpRx ()
 
void reset ()
 
bool isRxEmpty ()
 
virtual void decodeFrame (const CANRxFrame &frame, efitick_t nowNt)
 
int readTimeout (uint8_t *rxbuf, size_t *size, sysinterval_t timeout)
 
void resetRxVerbose ()
 
- Public Member Functions inherited from CanListener
 CanListener (uint32_t id)
 
CanListenerprocessFrame (const size_t busIndex, const CANRxFrame &frame, efitick_t nowNt)
 
uint32_t getId ()
 
void setNext (CanListener *next)
 
virtual CanListenerrequest ()
 
CanListenergetNext () const
 
virtual bool acceptFrame (const size_t busIndex, const CANRxFrame &frame) const
 
- Public Member Functions inherited from IsoTpBase
 IsoTpBase (ICanTransmitter *p_txTransport, size_t p_busIndex, uint32_t p_rxFrameId, uint32_t p_txFrameId)
 
int sendFrame (const IsoTpFrameHeader &header, const uint8_t *data, int num, can_sysinterval_t timeout)
 
void sendFlowControl (can_sysinterval_t timeout)
 
can_msg_t transmit (CanTxMessage &ctfp, can_sysinterval_t timeout)
 

Protected Attributes

fifo_buffer_sync< CANRxFrame, ISOTP_RX_QUEUE_LEN > rxFifoBuf
 

Private Attributes

int waitingForNumBytes = 0
 
uint8_t waitingForFrameIndex = 0
 

Additional Inherited Members

- Data Fields inherited from IsoTpBase
size_t isoHeaderByteIndex = 0
 
ICanTransmittertxTransport
 
size_t busIndex
 
uint32_t rxFrameId
 
uint32_t txFrameId
 

Detailed Description

Definition at line 178 of file isotp.h.

Constructor & Destructor Documentation

◆ IsoTpRx()

IsoTpRx::IsoTpRx ( size_t  p_busIndex,
uint32_t  p_rxFrameId,
uint32_t  p_txFrameId 
)
inline

Definition at line 180 of file isotp.h.

181 :
182 CanListener(p_rxFrameId),
183 IsoTpBase(nullptr, p_busIndex, p_rxFrameId, p_txFrameId)
184 {
185 // not cool: this would invoke ChibiOS meaning we have to be careful where instances are declared in order to avoid
186 // initialization too soon
187 // todo: https://github.com/rusefi/rusefi/issues/8938
188 rxFifoBuf.clear();
189 registerCanListener(*this);
190 }
void registerCanListener(CanListener &listener)
Definition can_rx.cpp:86
fifo_buffer_sync< CANRxFrame, ISOTP_RX_QUEUE_LEN > rxFifoBuf
Definition isotp.h:235
Here is the call graph for this function:

◆ ~IsoTpRx()

IsoTpRx::~IsoTpRx ( )
inline

Definition at line 192 of file isotp.h.

192 {
194 }
void unregisterCanListener(CanListener &listener)
Definition can_rx.cpp:96
Here is the call graph for this function:

Member Function Documentation

◆ decodeFrame()

virtual void IsoTpRx::decodeFrame ( const CANRxFrame frame,
efitick_t  nowNt 
)
inlinevirtual

Implements CanListener.

Definition at line 207 of file isotp.h.

208 {
209 if (frame.DLC < 1 + isoHeaderByteIndex) {
210 // invalid++;
211 return;
212 }
213
214 if (isoHeaderByteIndex) {
215 if (frame.data8[0] != (txFrameId & 0xff)) {
216 return;
217 }
218 }
219
220 if (!rxFifoBuf.put(frame)) {
221 // overruns++;
222 }
223 }
uint32_t txFrameId
Definition isotp.h:120
size_t isoHeaderByteIndex
Definition isotp.h:114
uint8_t data8[8]
Frame data.
Definition can_mocks.h:55
uint8_t DLC
Data length.
Definition can_mocks.h:42

◆ isRxEmpty()

bool IsoTpRx::isRxEmpty ( )
inline

Definition at line 202 of file isotp.h.

202 {
203 return rxFifoBuf.isEmpty();
204 }

◆ readTimeout()

int IsoTpRx::readTimeout ( uint8_t *  rxbuf,
size_t size,
sysinterval_t  timeout 
)

Definition at line 367 of file isotp.cpp.

368{
369 //is fxbuf is too small?
370 bool overflow = false;
371 bool isFirstFrame = true;
372 size_t availableAtBuffer = *size;
373 uint8_t *buf = rxbuf;
374
375 do {
376 CANRxFrame rxmsg;
377
378 // TODO: adjust timeout!
379 if (!rxFifoBuf.get(rxmsg, timeout)) {
380 // TODO: error codes
381 if (isFirstFrame) {
382 // this is not an error
383 //efiPrintf("IsoTp: rx timeout, nothing received");
384 *size = 0;
385 return 0;
386 }
387
388 efiPrintf("IsoTP: rx timeout, %d left to receive", waitingForNumBytes);
389 *size = buf - rxbuf;
390 return -1;
391 }
392
393 uint8_t frameType = (rxmsg.data8[isoHeaderByteIndex] >> 4) & 0xf;
395 efiPrintf("receiveFrame frameType=%d", frameType);
396 #if EFI_PROD_CODE
397 printCANRxFrame(-1, rxmsg);
398 #endif // EFI_PROD_CODE
399 }
400 size_t numBytesAvailable;
401 uint8_t frameIdx;
402 const uint8_t *srcBuf;
403 switch (frameType) {
405 // TODO: check that this is first packet! see isFirstFrame
406 numBytesAvailable = rxmsg.data8[isoHeaderByteIndex] & 0xf;
407 waitingForNumBytes = numBytesAvailable;
408 srcBuf = rxmsg.data8 + 1 + isoHeaderByteIndex;
409 break;
411 // TODO: check that this is first packet! see isFirstFrame
412 waitingForNumBytes = ((rxmsg.data8[isoHeaderByteIndex] & 0xf) << 8) | rxmsg.data8[isoHeaderByteIndex + 1];
414 numBytesAvailable = minI(waitingForNumBytes, 6 - isoHeaderByteIndex);
415 srcBuf = rxmsg.data8 + 2 + isoHeaderByteIndex;
416// rxTransport->onTpFirstFrame(); // used to send flow control message
417 break;
419 frameIdx = rxmsg.data8[isoHeaderByteIndex] & 0xf;
420 if (waitingForNumBytes < 0) {
421 // Should not happen
422 return -4;
423 }
424 if (waitingForFrameIndex != frameIdx) {
425 // todo: that's an abnormal situation, and we probably should react?
426 // TODO: error codes
427 efiPrintf("received frame index %d is not what expected %d",
428 frameIdx, waitingForFrameIndex);
429 return -2;
430 }
431 numBytesAvailable = minI(waitingForNumBytes, 7 - isoHeaderByteIndex);
432 srcBuf = rxmsg.data8 + 1 + isoHeaderByteIndex;
434 break;
436 // todo: currently we just ignore the FC frame
437 // TODO: we should not receive FC frame while receiving data
438 break;
439 default:
440 // bad frame type
441 // TODO: error codes
442 return -3;
443 }
444
445 if (isFirstFrame) {
446 if ((buf) && (waitingForNumBytes > availableAtBuffer)) {
447 efiPrintf("receive buffer is not enough %d > %d", waitingForNumBytes, availableAtBuffer);
448 }
449 isFirstFrame = false;
450 }
451
452 if (buf != nullptr) {
453 int numBytesToCopy = minI(availableAtBuffer, numBytesAvailable);
454
455 memcpy(buf, srcBuf, numBytesToCopy);
456 buf += numBytesToCopy;
457 availableAtBuffer -= numBytesToCopy;
458
459 // if there are some more bytes left, receive and drop
460 if (numBytesAvailable > numBytesToCopy) {
461 overflow = true;
462 }
463 }
464
465 // according to the specs, we need to acknowledge the received multi-frame start frame
466 if (frameType == ISO_TP_FRAME_FIRST) {
467 sendFlowControl(timeout);
468 }
469
470 waitingForNumBytes -= numBytesAvailable;
471 } while (waitingForNumBytes > 0);
472
473 // received size
474 *size = buf - rxbuf;
475
476 return overflow ? 1 : 0;
477}
void printCANRxFrame(const size_t busIndex, const CANRxFrame &rx)
Definition can_rx.cpp:29
void sendFlowControl(can_sysinterval_t timeout)
Definition isotp.cpp:80
int waitingForNumBytes
Definition isotp.h:231
uint8_t waitingForFrameIndex
Definition isotp.h:232
static constexpr engine_configuration_s * engineConfiguration
@ ISO_TP_FRAME_CONSECUTIVE
Definition isotp.h:32
@ ISO_TP_FRAME_FIRST
Definition isotp.h:31
@ ISO_TP_FRAME_FLOW_CONTROL
Definition isotp.h:33
@ ISO_TP_FRAME_SINGLE
Definition isotp.h:30
composite packet size
Here is the call graph for this function:

◆ reset()

void IsoTpRx::reset ( )
inline

Definition at line 196 of file isotp.h.

196 {
197 rxFifoBuf.clear();
200 }

◆ resetRxVerbose()

void IsoTpRx::resetRxVerbose ( )

Definition at line 479 of file isotp.cpp.

479 {
480#if EFI_PROD_CODE || SIMULATOR
481 CANRxFrame rxmsg;
482
483 while (rxFifoBuf.get(rxmsg, 0)) {
485 }
486#endif
487
490}
size_t busIndex
Definition isotp.h:118
Here is the call graph for this function:

Field Documentation

◆ rxFifoBuf

fifo_buffer_sync<CANRxFrame, ISOTP_RX_QUEUE_LEN> IsoTpRx::rxFifoBuf
protected

◆ waitingForFrameIndex

uint8_t IsoTpRx::waitingForFrameIndex = 0
private

Definition at line 232 of file isotp.h.

Referenced by readTimeout(), reset(), and resetRxVerbose().

◆ waitingForNumBytes

int IsoTpRx::waitingForNumBytes = 0
private

Definition at line 231 of file isotp.h.

Referenced by readTimeout(), reset(), and resetRxVerbose().


The documentation for this class was generated from the following files: