10#if HAL_USE_CAN || EFI_UNIT_TEST
35 maxNumBytes = dlc -
offset;
49 int numBytes = minI(maxNumBytes, num);
51 if (data !=
nullptr) {
52 for (
int i = 0; i < numBytes; i++) {
53 txmsg[i +
offset] = data[i];
58 if (
transmit(txmsg, timeout) == CAN_MSG_OK)
70 efiPrintf(
"receiveFrame frameType=%d", frameType);
75 int numBytesAvailable, frameIdx;
76 const uint8_t *srcBuf;
135 int numBytesToCopy = minI(availableAtBuffer, numBytesAvailable);
136 if (destinationBuff !=
nullptr) {
137 memcpy(destinationBuff, srcBuf, numBytesToCopy);
139 srcBuf += numBytesToCopy;
142 numBytesAvailable -= numBytesToCopy;
144 for (
int i = 0; i < numBytesAvailable; i++) {
158 return numBytesToCopy;
171 PRINT(
"*** INFO: sendDataTimeout %d" PRINT_EOL, numBytes);
194 int totalNumSent = numSent;
199 for (
size_t numFcReceived = 0; ; numFcReceived++) {
201#ifdef SERIAL_CAN_DEBUG
202 PRINT(
"*** ERROR: CAN Flow Control frame not received" PRINT_EOL);
216#ifdef SERIAL_CAN_DEBUG
217 efiPrintf(
"*** ERROR: CAN Flow Control mode not supported");
224 if (blockSize != 0 || minSeparationTime != 0) {
226#ifdef SERIAL_CAN_DEBUG
227 efiPrintf(
"*** ERROR: CAN Flow Control fields not supported");
237 while (numBytes > 0) {
241 header.
index = ((idx++) & 0x0f);
246 totalNumSent += numSent;
256 int numReadFromFifo = minI(numBytes,
rxFifoBuf.getCount());
259 for (i = 0; !
rxFifoBuf.isEmpty() && i < numReadFromFifo; i++) {
271 PRINT(
"*** INFO: streamAddToTxTimeout adding %d, in buffer %d" PRINT_EOL, numBytes,
txFifoBuf.getCount());
281 PRINT(
"*** INFO: streamAddToTxTimeout numBytesToAdd %d / numSent %d / numBytes %d" PRINT_EOL, numBytesToAdd, numSent, numBytes);
288 numBytes -= numBytesToAdd;
292 PRINT(
"*** INFO: streamAddToTxTimeout remaining goes to buffer %d" PRINT_EOL, numBytes);
300 PRINT(
"*** INFO: in buffer %d" PRINT_EOL,
txFifoBuf.getCount());
317 size_t availableBufferSpace = *np;
323 while (availableBufferSpace > 0) {
326 int numReceived =
receiveFrame(rxmsg, rxbuf + receivedSoFar, availableBufferSpace, timeout);
330 availableBufferSpace -= numReceived;
331 receivedSoFar += numReceived;
336 *np -= availableBufferSpace;
338#ifdef SERIAL_CAN_DEBUG
339 efiPrintf(
"* ret: %d %d (%d)", i, *np, availableBufferSpace);
340 for (
int j = 0; j < receivedSoFar; j++) {
341 efiPrintf(
"* [%d]: %02x", j, rxbuf[j]);
351 bool overflow =
false;
352 bool isFirstFrame =
true;
353 size_t availableAtBuffer = *
size;
354 uint8_t *buf = rxbuf;
363 efiPrintf(
"IsoTp: rx timeout, nothing received");
372 efiPrintf(
"receiveFrame frameType=%d", frameType);
377 size_t numBytesAvailable;
379 const uint8_t *srcBuf;
418 efiPrintf(
"receive buffer is not enough %d > %d",
waitingForNumBytes, availableAtBuffer);
420 isFirstFrame =
false;
423 if (buf !=
nullptr) {
424 int numBytesToCopy = minI(availableAtBuffer, numBytesAvailable);
426 memcpy(buf, srcBuf, numBytesToCopy);
427 buf += numBytesToCopy;
428 availableAtBuffer -= numBytesToCopy;
431 if (numBytesAvailable > numBytesToCopy) {
452 return overflow ? 1 : 0;
459 PRINT(
"*** INFO: sendDataTimeout %d" PRINT_EOL,
size);
486 size_t numFcReceived = 0;
487 int separationTimeUs = 0;
488 while (numFcReceived < 3) {
491 efiPrintf(
"IsoTp: Flow Control frame not received");
507 if (flowStatus == CAN_FLOW_STATUS_ABORT) {
508 efiPrintf(
"IsoTp: Flow Control ABORT");
513 if (flowStatus == CAN_FLOW_STATUS_WAIT_MORE) {
515 if (numFcReceived < 3) {
522 if (flowStatus != CAN_FLOW_STATUS_OK) {
523 efiPrintf(
"IsoTp: Flow Control unknown Status %d", flowStatus);
530 if (blockSize != 0) {
532 efiPrintf(
"IsoTp: Flow Control blockSize is not supported %d", blockSize);
537 if (minSeparationTime <= 0x7f) {
539 separationTimeUs = minSeparationTime * 1000;
540 }
else if ((minSeparationTime >= 0xf1) && (minSeparationTime <= 0xf9)) {
542 separationTimeUs = (minSeparationTime - 0xf0) * 100;
555 header.
index = ((idx++) & 0x0f);
564 if (separationTimeUs) {
565 chThdSleepMicroseconds(separationTimeUs);
int32_t can_sysinterval_t
void printCANRxFrame(const size_t busIndex, const CANRxFrame &rx)
fifo_buffer< uint8_t, CAN_FIFO_BUF_SIZE > txFifoBuf
fifo_buffer< uint8_t, CAN_FIFO_BUF_SIZE > rxFifoBuf
can_msg_t streamFlushTx(can_sysinterval_t timeout)
int sendDataTimeout(const uint8_t *txbuf, int numBytes, can_sysinterval_t timeout)
int receiveFrame(const CANRxFrame &rxmsg, uint8_t *buf, int num, can_sysinterval_t timeout)
ICanReceiver * rxTransport
can_msg_t streamReceiveTimeout(size_t *np, uint8_t *rxbuf, can_sysinterval_t timeout)
int getDataFromFifo(uint8_t *rxbuf, size_t &numBytes)
can_msg_t streamAddToTxTimeout(size_t *np, const uint8_t *txbuf, can_sysinterval_t timeout)
virtual can_msg_t receive(CANRxFrame *crfp, can_sysinterval_t timeout)=0
virtual void onTpFirstFrame()=0
can_msg_t transmit(CanTxMessage &ctfp, can_sysinterval_t timeout)
int sendFrame(const IsoTpFrameHeader &header, const uint8_t *data, int num, can_sysinterval_t timeout)
size_t isoHeaderByteIndex
int readTimeout(uint8_t *rxbuf, size_t *size, sysinterval_t timeout)
uint8_t waitingForFrameIndex
fifo_buffer_sync< CANRxFrame, ISOTP_RX_QUEUE_LEN > rxFifoBuf
int writeTimeout(const uint8_t *txbuf, size_t size, sysinterval_t timeout)
static EngineAccessor engine
static constexpr engine_configuration_s * engineConfiguration
@ ISO_TP_FRAME_CONSECUTIVE
@ ISO_TP_FRAME_FLOW_CONTROL
uint8_t data8[8]
Frame data.