CAN ISO-TP progress (+unit-tests fix) (#3677)

Co-authored-by: Andrei <andreikagit@users.noreply.github.com>
This commit is contained in:
Andreika 2021-12-08 22:11:19 +02:00 committed by GitHub
parent f44370a5b8
commit 72cefd42de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 27 deletions

View File

@ -180,6 +180,7 @@ int CanStreamerState::sendDataTimeout(const uint8_t *txbuf, int numBytes, can_sy
int totalNumSent = numSent; int totalNumSent = numSent;
// get a flow control (FC) frame // get a flow control (FC) frame
#if !EFI_UNIT_TEST // todo: add FC to unit-tests?
CANRxFrame rxmsg; CANRxFrame rxmsg;
for (int numFcReceived = 0; ; numFcReceived++) { for (int numFcReceived = 0; ; numFcReceived++) {
if (streamer->receive(CAN_ANY_MAILBOX, &rxmsg, timeout) != CAN_MSG_OK) { if (streamer->receive(CAN_ANY_MAILBOX, &rxmsg, timeout) != CAN_MSG_OK) {
@ -214,6 +215,7 @@ int CanStreamerState::sendDataTimeout(const uint8_t *txbuf, int numBytes, can_sy
} }
break; break;
} }
#endif /* EFI_UNIT_TEST */
// send the rest of the data // send the rest of the data
int idx = 1; int idx = 1;
@ -250,16 +252,17 @@ int CanStreamerState::getDataFromFifo(uint8_t *rxbuf, size_t &numBytes) {
can_msg_t CanStreamerState::streamAddToTxTimeout(size_t *np, const uint8_t *txbuf, can_sysinterval_t timeout) { can_msg_t CanStreamerState::streamAddToTxTimeout(size_t *np, const uint8_t *txbuf, can_sysinterval_t timeout) {
int numBytes = *np; int numBytes = *np;
int offset = 0; int offset = 0;
int minNumBytesRequiredToSend = 7 - txFifoBuf.getCount();
while (numBytes >= minNumBytesRequiredToSend) { // we send here only if the TX FIFO buffer is getting overflowed
txFifoBuf.put(txbuf + offset, numBytes); while (numBytes >= txFifoBuf.getSize() - txFifoBuf.getCount()) {
int numBytesToAdd = txFifoBuf.getSize() - txFifoBuf.getCount();
txFifoBuf.put(txbuf + offset, numBytesToAdd);
int numSent = sendDataTimeout((const uint8_t *)txFifoBuf.getElements(), txFifoBuf.getCount(), timeout); int numSent = sendDataTimeout((const uint8_t *)txFifoBuf.getElements(), txFifoBuf.getCount(), timeout);
if (numSent < 1) if (numSent < 1)
break; break;
txFifoBuf.clear(); txFifoBuf.clear();
offset += numSent; offset += numSent;
numBytes -= numSent; numBytes -= numSent;
minNumBytesRequiredToSend = 7;
} }
// now we put the rest on hold // now we put the rest on hold

View File

@ -11,17 +11,18 @@
#include "can_msg_tx.h" #include "can_msg_tx.h"
#if EFI_CAN_SUPPORT
#include "can.h" #include "can.h"
extern int canWriteOk; extern int canWriteOk;
extern int canWriteNotOk; extern int canWriteNotOk;
#if EFI_CAN_SUPPORT
/*static*/ CANDriver* CanTxMessage::s_device = nullptr; /*static*/ CANDriver* CanTxMessage::s_device = nullptr;
/*static*/ void CanTxMessage::setDevice(CANDriver* device) { /*static*/ void CanTxMessage::setDevice(CANDriver* device) {
s_device = device; s_device = device;
} }
#endif // EFI_CAN_SUPPORT
CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) { CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) {
#ifndef STM32H7XX #ifndef STM32H7XX
@ -46,6 +47,7 @@ CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) {
} }
CanTxMessage::~CanTxMessage() { CanTxMessage::~CanTxMessage() {
#if EFI_CAN_SUPPORT
auto device = s_device; auto device = s_device;
if (!device) { if (!device) {
@ -72,6 +74,7 @@ CanTxMessage::~CanTxMessage() {
} else { } else {
canWriteNotOk++; canWriteNotOk++;
} }
#endif /* EFI_CAN_SUPPORT */
} }
void CanTxMessage::setDlc(uint8_t dlc) { void CanTxMessage::setDlc(uint8_t dlc) {
@ -87,20 +90,6 @@ void CanTxMessage::setBit(size_t byteIdx, size_t bitIdx) {
m_frame.data8[byteIdx] |= 1 << bitIdx; m_frame.data8[byteIdx] |= 1 << bitIdx;
} }
#else
CanTxMessage::CanTxMessage(uint32_t /*eid*/, uint8_t /*dlc*/, bool /*isExtended*/) {
}
CanTxMessage::~CanTxMessage() {
}
void CanTxMessage::setDlc(uint8_t) { }
#endif // EFI_CAN_SUPPORT
uint8_t& CanTxMessage::operator[](size_t index) { uint8_t& CanTxMessage::operator[](size_t index) {
return m_frame.data8[index]; return m_frame.data8[index];
} }

View File

@ -49,6 +49,10 @@ public:
return cyclic_buffer<T, maxSize>::getCount(); return cyclic_buffer<T, maxSize>::getCount();
} }
int getSize() const {
return cyclic_buffer<T, maxSize>::getSize();
}
const volatile T* getElements() const { const volatile T* getElements() const {
return elements; return elements;
} }

View File

@ -28,6 +28,8 @@ public:
} }
virtual can_msg_t receive(canmbx_t mailbox, CANRxFrame *crfp, can_sysinterval_t timeout) override { virtual can_msg_t receive(canmbx_t mailbox, CANRxFrame *crfp, can_sysinterval_t timeout) override {
if (crfList.empty())
return CAN_MSG_TIMEOUT;
*crfp = *crfList.begin(); *crfp = *crfList.begin();
crfList.pop_front(); crfList.pop_front();
return CAN_MSG_OK; return CAN_MSG_OK;
@ -51,6 +53,8 @@ public:
TestCanStreamerState() : CanStreamerState(&streamer) {} TestCanStreamerState() : CanStreamerState(&streamer) {}
void test(const std::vector<std::string> & dataList, const std::vector<std::string> & frames, int fifoLeftoverSize, const std::vector<size_t> & receiveChunks) { void test(const std::vector<std::string> & dataList, const std::vector<std::string> & frames, int fifoLeftoverSize, const std::vector<size_t> & receiveChunks) {
EngineTestHelper eth(TEST_ENGINE);
size_t totalSize = 0; size_t totalSize = 0;
std::string totalData; std::string totalData;
for (auto data : dataList) { for (auto data : dataList) {
@ -110,37 +114,37 @@ protected:
TestCanStreamer streamer; TestCanStreamer streamer;
}; };
TEST(testCanSerial, test1Frame) { TEST(testCanSerial, test1Frame) {
/*
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "1" }, { "\x01"s "1\0\0\0\0\0\0"s }, 1, { 1 }); // 1 byte -> 1 frame, 1 byte in FIFO state.test({ "1" }, { "\x01"s "1\0\0\0\0\0\0"s }, 1, { 1 }); // 1 byte -> 1 frame, 1 byte in FIFO
} }
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 7 }); // 7 bytes -> 1 8-byte frame state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 7 }); // 7 bytes -> 1 8-byte frame
} }
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 1, 1, 1, 1, 1, 1, 1 }); // 7 bytes -> 1 8-byte frame, split receive test state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 1, 1, 1, 1, 1, 1, 1 }); // 7 bytes -> 1 8-byte frame, split receive test
} }
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 3, 4 }); // 7 bytes -> 1 8-byte frame, split receive test state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 3, 4 }); // 7 bytes -> 1 8-byte frame, split receive test
} }
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0", "1", "2", "3", "4", "5", "6" }, { "\x07"s "0123456"s }, 0, { 7 }); // 7 bytes separately -> 1 8-byte frame state.test({ "0", "1", "2", "3", "4", "5", "6" }, { "\x07"s "0123456"s }, 7, { 7 }); // 7 bytes separately -> 1 8-byte frame
} }
*/
} }
/*
TEST(testCanSerial, test2Frames) { TEST(testCanSerial, test2Frames) {
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "01234567" }, { "\x07"s "0123456"s, "\x01"s "7\0\0\0\0\0\0"s }, 1, { 8 }); // 8 bytes -> 2 8-byte frames, 1 byte in FIFO state.test({ "01234567" }, { "\x10"s "\x08"s "012345"s, "\x21"s "67\0\0\0\0\0"s }, 8, { 8 }); // 8 bytes -> 2 8-byte frames, 8 bytes in FIFO
} }
/*
{ {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 14 }); // 14 bytes -> 2 8-byte frames, empty FIFO state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 14 }); // 14 bytes -> 2 8-byte frames, empty FIFO
@ -149,8 +153,10 @@ TEST(testCanSerial, test2Frames) {
TestCanStreamerState state; TestCanStreamerState state;
state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 6, 1, 1, 6 }); // 14 bytes -> 2 8-byte frames, empty FIFO, split receive test state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 6, 1, 1, 6 }); // 14 bytes -> 2 8-byte frames, empty FIFO, split receive test
} }
*/
} }
/*
TEST(testCanSerial, testIrregularSplits) { TEST(testCanSerial, testIrregularSplits) {
{ {
TestCanStreamerState state; TestCanStreamerState state;