| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | /* | |||
| 2 | * @file logicdata_csv_reader.cpp | |||
| 3 | * | |||
| 4 | * @date Jun 26, 2021 | |||
| 5 | * @author Andrey Belomutskiy, (c) 2012-2021 | |||
| 6 | */ | |||
| 7 | ||||
| 8 | #include "pch.h" | |||
| 9 | #include "logicdata_csv_reader.h" | |||
| 10 | #include "unit_test_logger.h" | |||
| 11 | ||||
| 12 | 875923 | static char* trim(char *str) { | ||
| 13 |
3/4✓ Branch 0 taken 1003381 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127458 times.
✓ Branch 3 taken 875923 times.
|
0/1? Decision couldn't be analyzed.
|
1003381 | while (str != nullptr && str[0] == ' ') { |
| 14 | 127458 | str++; | ||
| 15 | } | |||
| 16 | 875923 | return str; | ||
| 17 | } | |||
| 18 | ||||
| 19 | 68 | CsvReader::~CsvReader() { | ||
| 20 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 68 times.
✗ Decision 'false' not taken.
|
68 | if (fp) { |
| 21 | 68 | fclose(fp); | ||
| 22 | } | |||
| 23 | 68 | } | ||
| 24 | ||||
| 25 | 68 | void CsvReader::open(const char *fileName, const int* triggerColumnIndeces, const int *vvtColumnIndeces) { | ||
| 26 | 68 | printf("Reading from %s\r\n", fileName); | ||
| 27 | 68 | fp = fopen(fileName, "r"); | ||
| 28 | 68 | this->triggerColumnIndeces = triggerColumnIndeces; | ||
| 29 | 68 | this->vvtColumnIndeces = vvtColumnIndeces; | ||
| 30 |
2/8✗ Branch 5 not taken.
✓ Branch 6 taken 68 times.
✗ Branch 9 not taken.
✗ Branch 14 not taken.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✓ Branch 30 taken 68 times.
✗ Branch 31 not taken.
|
68 | ASSERT_TRUE(fp != nullptr); | |
| 31 | } | |||
| 32 | ||||
| 33 | 424008 | bool CsvReader::haveMore() { | ||
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 424008 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 424008 times.
|
424008 | if (fp == nullptr) { |
| 35 | ✗ | throw std::runtime_error("No file"); | ||
| 36 | } | |||
| 37 | 424008 | bool result = fgets(buffer, sizeof(buffer), fp) != nullptr; | ||
| 38 | 424008 | m_lineIndex++; | ||
| 39 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 423940 times.
|
2/2✓ Decision 'true' taken 68 times.
✓ Decision 'false' taken 423940 times.
|
424008 | if (m_lineIndex == 0) { |
| 40 | // skip header | |||
| 41 | 68 | return haveMore(); | ||
| 42 | } | |||
| 43 | ||||
| 44 | 423940 | return result; | ||
| 45 | } | |||
| 46 | ||||
| 47 | static const char COMMA_SEPARATOR[2] = ","; | |||
| 48 | ||||
| 49 | #define readFirstTokenAndRememberInputString(input) trim(strtok(input, COMMA_SEPARATOR)) | |||
| 50 | #define readNextToken() trim(strtok(nullptr, COMMA_SEPARATOR)) | |||
| 51 | ||||
| 52 | /** | |||
| 53 | * @param values reference of values array to modify | |||
| 54 | * @return timestamp of current line | |||
| 55 | */ | |||
| 56 | 355156 | double CsvReader::readTimestampAndValues(double *values) { | ||
| 57 | 355156 | char *timeStampstr = readFirstTokenAndRememberInputString(buffer); | ||
| 58 |
2/2✓ Branch 3 taken 355156 times.
✓ Branch 6 taken 355156 times.
|
1065468 | double timeStamp = std::stod(timeStampstr); | |
| 59 | ||||
| 60 |
2/2✓ Branch 0 taken 355156 times.
✓ Branch 1 taken 355156 times.
|
2/2✓ Decision 'true' taken 355156 times.
✓ Decision 'false' taken 355156 times.
|
710312 | for (size_t i = 0; i < m_triggerCount; i++) { |
| 61 | 355156 | char *triggerToken = readNextToken(); | ||
| 62 |
2/2✓ Branch 3 taken 355156 times.
✓ Branch 6 taken 355156 times.
|
1065468 | values[i] = std::stod(triggerToken); | |
| 63 | } | |||
| 64 | ||||
| 65 | 355156 | return timeStamp; | ||
| 66 | } | |||
| 67 | ||||
| 68 | // this is about TS logs generated during trigger tests and viewing these files by humans. | |||
| 69 | // Emulate 500Hz refresh rate | |||
| 70 | #define TIME_DELTA (1.0/500.0) | |||
| 71 | ||||
| 72 | // todo: separate trigger handling from csv file processing, maybe reuse 'readTimestampAndValues'? | |||
| 73 | 68716 | void CsvReader::processLine(EngineTestHelper *eth) { | ||
| 74 | 68716 | Engine *engine = ð->engine; | ||
| 75 | ||||
| 76 | 68716 | const char s[2] = ","; | ||
| 77 | 68716 | char *timeStampstr = readFirstTokenAndRememberInputString(buffer); | ||
| 78 | ||||
| 79 |
2/2✓ Branch 0 taken 976 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 976 times.
✓ Decision 'false' taken 68716 times.
|
69692 | for (int i = 0;i<readingOffset;i++) { |
| 80 | 976 | readNextToken(); | ||
| 81 | } | |||
| 82 | ||||
| 83 | 68716 | bool newTriggerState[TRIGGER_INPUT_PIN_COUNT]; | ||
| 84 | 68716 | bool newVvtState[CAM_INPUTS_COUNT]; | ||
| 85 | ||||
| 86 |
2/2✓ Branch 0 taken 69803 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 69803 times.
✓ Decision 'false' taken 68716 times.
|
138519 | for (size_t i = 0;i<m_triggerCount;i++) { |
| 87 | 69803 | char * triggerToken = readNextToken(); | ||
| 88 | 69803 | newTriggerState[triggerColumnIndeces[i]] = triggerToken[0] == '1'; | ||
| 89 | } | |||
| 90 | ||||
| 91 |
2/2✓ Branch 0 taken 26116 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 26116 times.
✓ Decision 'false' taken 68716 times.
|
94832 | for (size_t i = 0;i<m_vvtCount;i++) { |
| 92 | 26116 | char *vvtToken = readNextToken(); | ||
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26116 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 26116 times.
|
26116 | if (vvtToken == nullptr) { |
| 94 | ✗ | criticalError("Null token in [%s]", buffer); | ||
| 95 | } | |||
| 96 | 26116 | bool state = vvtToken[0] == '1'; | ||
| 97 | 26116 | newVvtState[vvtColumnIndeces[i]] = state; | ||
| 98 | } | |||
| 99 | ||||
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68716 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 68716 times.
|
68716 | if (timeStampstr == nullptr) { |
| 101 | ✗ | criticalError("End of File"); | ||
| 102 | ✗ | return; | ||
| 103 | } | |||
| 104 | ||||
| 105 |
2/2✓ Branch 3 taken 68716 times.
✓ Branch 6 taken 68716 times.
|
206148 | double timeStamp = std::stod(timeStampstr); | |
| 106 | 68716 | history.add(timeStamp); | ||
| 107 | ||||
| 108 | 68716 | timeStamp += m_timestampOffset; | ||
| 109 | ||||
| 110 | #ifdef TIME_DELTA | |||
| 111 | // Fill the gap | |||
| 112 |
2/2✓ Branch 0 taken 462362 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 462362 times.
✓ Decision 'false' taken 68716 times.
|
531078 | while (lastTimeStamp + TIME_DELTA < timeStamp) { |
| 113 | 462362 | lastTimeStamp += TIME_DELTA; | ||
| 114 |
1/1✓ Branch 1 taken 462362 times.
|
462362 | eth->setTimeAndInvokeEventsUs(1'000'000 * lastTimeStamp); | |
| 115 |
1/1✓ Branch 1 taken 462362 times.
|
462362 | writeUnitTestLogLine(); | |
| 116 | } | |||
| 117 | #endif | |||
| 118 | ||||
| 119 |
1/1✓ Branch 1 taken 68716 times.
|
68716 | eth->setTimeAndInvokeEventsUs(1'000'000 * timeStamp); | |
| 120 |
2/2✓ Branch 0 taken 69803 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 69803 times.
✓ Decision 'false' taken 68716 times.
|
138519 | for (size_t index = 0; index < m_triggerCount; index++) { |
| 121 |
2/2✓ Branch 2 taken 8630 times.
✓ Branch 3 taken 61173 times.
|
2/2✓ Decision 'true' taken 8630 times.
✓ Decision 'false' taken 61173 times.
|
69803 | if (currentState[index] == newTriggerState[index]) { |
| 122 | 8630 | continue; | ||
| 123 | } | |||
| 124 | ||||
| 125 |
1/1✓ Branch 1 taken 61173 times.
|
61173 | efitick_t nowNt = getTimeNowNt(); | |
| 126 | 61173 | bool state = newTriggerState[index] ^ flipOnRead; | ||
| 127 |
1/1✓ Branch 1 taken 61173 times.
|
61173 | hwHandleShaftSignal(index, state, nowNt); | |
| 128 | ||||
| 129 | 61173 | currentState[index] = newTriggerState[index]; | ||
| 130 | } | |||
| 131 | ||||
| 132 |
2/2✓ Branch 0 taken 26116 times.
✓ Branch 1 taken 68716 times.
|
2/2✓ Decision 'true' taken 26116 times.
✓ Decision 'false' taken 68716 times.
|
94832 | for (size_t vvtIndex = 0; vvtIndex < m_vvtCount ; vvtIndex++) { |
| 133 |
2/2✓ Branch 2 taken 20240 times.
✓ Branch 3 taken 5876 times.
|
2/2✓ Decision 'true' taken 20240 times.
✓ Decision 'false' taken 5876 times.
|
26116 | if (currentVvtState[vvtIndex] == newVvtState[vvtIndex]) { |
| 134 | 20240 | continue; | ||
| 135 | } | |||
| 136 | ||||
| 137 |
1/1✓ Branch 1 taken 5876 times.
|
5876 | efitick_t nowNt = getTimeNowNt(); | |
| 138 | 5876 | bool state = newVvtState[vvtIndex] ^ flipVvtOnRead; | ||
| 139 | ||||
| 140 | // todo: configurable selection of vvt mode - dual bank or dual cam single bank | |||
| 141 | int bankIndex; | |||
| 142 | int camIndex; | |||
| 143 |
2/2✓ Branch 0 taken 5460 times.
✓ Branch 1 taken 416 times.
|
2/2✓ Decision 'true' taken 5460 times.
✓ Decision 'false' taken 416 times.
|
5876 | if (twoBanksSingleCamMode) { |
| 144 | 5460 | bankIndex = vvtIndex; | ||
| 145 | 5460 | camIndex = 0; | ||
| 146 | } else { | |||
| 147 | 416 | bankIndex = vvtIndex / 2; | ||
| 148 | 416 | camIndex = vvtIndex % 2; | ||
| 149 | } | |||
| 150 |
1/1✓ Branch 1 taken 5876 times.
|
5876 | hwHandleVvtCamSignal(state, nowNt, bankIndex *2 + camIndex); | |
| 151 | ||||
| 152 | 5876 | currentVvtState[vvtIndex] = newVvtState[vvtIndex]; | ||
| 153 | ||||
| 154 | } | |||
| 155 |
1/1✓ Branch 1 taken 68716 times.
|
68716 | writeUnitTestLogLine(); | |
| 156 | 68716 | lastTimeStamp = timeStamp; | ||
| 157 | } | |||
| 158 | ||||
| 159 | 84 | void CsvReader::readLine(EngineTestHelper *eth) { | ||
| 160 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 84 times.
|
84 | if (!haveMore()) |
| 161 | ✗ | return; | ||
| 162 | 84 | processLine(eth); | ||
| 163 | 84 | engine->periodicSlowCallback(); | ||
| 164 | } | |||
| 165 |