Skip to content

Commit

Permalink
fix: fixed buggy timestamp when its 1 byte long
Browse files Browse the repository at this point in the history
  • Loading branch information
klonyyy committed Aug 29, 2023
1 parent 0ba7877 commit 0ae7af1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/TraceReader/TraceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ TraceReader::TraceState TraceReader::updateTraceIdle(uint8_t c)
else
{
timestampVec.push_back(c);
timestampEnd();
timestampEnd(true);
return TRACE_STATE_IDLE;
}
}
Expand All @@ -145,9 +145,9 @@ TraceReader::TraceState TraceReader::updateTraceIdle(uint8_t c)
return TRACE_OP_GET_CONTINUATION(c) ? TRACE_STATE_SKIP_FRAME : TRACE_STATE_IDLE;
}

void TraceReader::timestampEnd()
void TraceReader::timestampEnd(bool headerData)
{
if (timestampVec.size() == 1)
if (headerData)
timestamp = (uint32_t)(timestampVec[0] & 0x7f) >> 4;
else
{
Expand Down Expand Up @@ -227,7 +227,7 @@ TraceReader::TraceState TraceReader::updateTrace(uint8_t c)
if (TRACE_OP_GET_CONTINUATION(c))
return TRACE_STATE_TARGET_TIMESTAMP_CONT;
else
timestampEnd();
timestampEnd(false);
return TRACE_STATE_IDLE;
}

Expand All @@ -237,7 +237,7 @@ TraceReader::TraceState TraceReader::updateTrace(uint8_t c)
if (TRACE_OP_GET_CONTINUATION(c))
return TRACE_STATE_TARGET_TIMESTAMP_CONT;
else
timestampEnd();
timestampEnd(false);
return TRACE_STATE_IDLE;
}

Expand Down
2 changes: 1 addition & 1 deletion src/TraceReader/TraceReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class TraceReader

TraceState updateTraceIdle(uint8_t c);
TraceState updateTrace(uint8_t c);
void timestampEnd();
void timestampEnd(bool headerData);

void readerThread();

Expand Down
85 changes: 48 additions & 37 deletions src/TraceReader/TraceReaderNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,14 @@ void TraceReaderNew::processTimestamp(std::vector<uint8_t>& chunk)
timestamp = (uint32_t)(chunk[0] & 0x7f) >> 4;
else
{
for (uint32_t i = 0; i < chunk.size(); i++)
timestamp |= (uint32_t)(chunk[i] & 0x7f) << 7 * i;
for (uint32_t i = 1; i < chunk.size(); i++)
timestamp |= (uint32_t)(chunk[i] & 0x7f) << 7 * (i - 1);
}

std::array<uint32_t, channels> currentEntry{previousEntry};
std::cout << "timestamp double: " << (double)timestamp / 160000000.0 << std::endl;

std::array<uint32_t, channels>
currentEntry{previousEntry};

uint32_t i = 0;
while (awaitingTimestamp--)
Expand Down Expand Up @@ -203,39 +206,41 @@ void TraceReaderNew::readerThread()

uint32_t idx = 0;

// if (remainingFrameType == 2)
// {
// while (TRACE_OP_GET_CONTINUATION(buffer[idx]))
// {
// chunk.push_back(buffer[idx]);

// if (idx < length - 1)
// idx++;
// else
// {
// remainingBytes = 1;
// remainingFrameType = 2;
// break;
// }
// }
// length -= chunk.size();
// processTimestamp(chunk);
// }
// else if (remainingFrameType == 1 && remainingBytes)
// {
// while (remainingBytes--)
// {
// chunk.push_back(buffer[idx]);
// if (idx < length - 1)
// idx++;
// else
// remainingFrameType = 1;
// }
// if (remainingFrameType == 0)
// processSource(chunk);
// }

while (idx < length - 1)
if (remainingFrameType == 2)
{
while (TRACE_OP_GET_CONTINUATION(buffer[idx]))
{
chunk.push_back(buffer[idx]);
if (idx < length - 1)
idx++;
else
{
std::cout << " remaining timestamp REST " << std::endl;
remainingFrameType = 2;
remainingBytes = 1;
}
}
if (remainingFrameType == 0)
processTimestamp(chunk);
}
else if (remainingFrameType == 1 && remainingBytes)
{
while (remainingBytes--)
{
chunk.push_back(buffer[idx]);
if (idx < length - 1)
idx++;
else
remainingFrameType = 1;
}
length -= idx;
if (remainingFrameType == 0)
processSource(chunk);
}

remainingFrameType = 0;

while (idx < length)
{
uint8_t c = buffer[idx];

Expand All @@ -255,6 +260,7 @@ void TraceReaderNew::readerThread()
{
remainingFrameType = 1;
remainingBytes = size - i;
std::cout << "remaining: " << (int)remainingBytes << std::endl;
}

chunk.push_back(buffer[idx]);
Expand All @@ -266,12 +272,15 @@ void TraceReaderNew::readerThread()
{
timestamp = 0;

while (TRACE_OP_GET_CONTINUATION(buffer[idx]))
chunk.push_back(buffer[idx]);

while (TRACE_OP_GET_CONTINUATION(buffer[idx]) && !remainingBytes)
{
if (idx < length - 1)
idx++;
else
{
std::cout << " remaining timestamp " << std::endl;
remainingFrameType = 2;
remainingBytes = 1;
}
Expand All @@ -286,6 +295,8 @@ void TraceReaderNew::readerThread()
if (!isRunning)
break;
}

std::cout << " NEXT BUFFER " << std::endl;
}
logger->info("Closing trace thread {}", isRunning);
}
14 changes: 7 additions & 7 deletions test/TraceReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <memory>

#include "ITraceDevice.hpp"
// #include "TraceReader/TraceReader.hpp"
#include "TraceReader/TraceReaderNew.hpp"
#include "TraceReader/TraceReader.hpp"
// #include "TraceReader/TraceReaderNew.hpp"
#include "gmock/gmock.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
Expand Down Expand Up @@ -34,7 +34,7 @@ class TraceReaderTest : public ::testing::Test
void SetUp() override
{
traceDevice = std::make_shared<::NiceMock<TraceDeviceMock>>();
traceReader = std::make_shared<TraceReaderNew>(traceDevice, logger);
traceReader = std::make_shared<TraceReader>(traceDevice, logger);

ON_CALL(*traceDevice, startTrace(_, _, _)).WillByDefault(Return(true));
ON_CALL(*traceDevice, stopTrace()).WillByDefault(Return(true));
Expand All @@ -55,7 +55,7 @@ class TraceReaderTest : public ::testing::Test
std::shared_ptr<spdlog::sinks::stdout_color_sink_mt> stdout_sink;
std::shared_ptr<spdlog::logger> logger;

std::shared_ptr<TraceReaderNew> traceReader;
std::shared_ptr<TraceReader> traceReader;
std::shared_ptr<::NiceMock<TraceDeviceMock>> traceDevice;
};

Expand Down Expand Up @@ -98,7 +98,7 @@ TEST_F(TraceReaderTest, testdoubleBuffersBoundaryTimestamp)

std::array<uint32_t, channels> trace{};

std::array<double, 10> expectedTimestamp = {7.6875e-06, 1.25e-08, 2.25625e-06, 1.03125e-06, 7.6625e-06, 1.25e-08, 2.2625e-06, 6.25e-09};
std::array<double, 10> expectedTimestamp = {7.6875e-06, 2.1875e-07, 2.25625e-06, 1.03125e-06, 7.6625e-06, 2.1875e-07, 2.2625e-06, 1.4375e-07};
std::array<std::array<uint32_t, channels>, 8> expectedTrace{{{{0, 187, 0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 170, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 187, 0, 0, 0, 0, 0, 0, 0}},
Expand Down Expand Up @@ -141,7 +141,7 @@ TEST_F(TraceReaderTest, testChannelsAndTimestamp2)
17, 187, 192, 234, 2,
25, 170, 192, 23};

std::array<double, 10> expectedTimestamp = {7.6875e-06, 1.25e-08, 2.25625e-06, 1.03125e-06, 7.6625e-06, 1.25e-08, 2.2625e-06, 6.25e-09};
std::array<double, 10> expectedTimestamp = {7.6875e-06, 2.1875e-07, 2.25625e-06, 1.03125e-06, 7.6625e-06, 2.1875e-07, 2.2625e-06, 1.4375e-07};
std::array<std::array<uint32_t, channels>, 8> expectedTrace{{{{0, 187, 0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 170, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 187, 0, 0, 0, 0, 0, 0, 0}},
Expand Down Expand Up @@ -185,7 +185,7 @@ TEST_F(TraceReaderTest, testdoubleBuffers)

std::array<uint32_t, channels> trace{};

std::array<double, 10> expectedTimestamp = {7.6875e-06, 1.25e-08, 2.25625e-06, 1.03125e-06, 7.6625e-06, 1.25e-08, 2.2625e-06, 6.25e-09};
std::array<double, 10> expectedTimestamp = {7.6875e-06, 2.1875e-07, 2.25625e-06, 1.03125e-06, 7.6625e-06, 2.1875e-07, 2.2625e-06, 1.4375e-07};
std::array<std::array<uint32_t, channels>, 8> expectedTrace{{{{0, 187, 0, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 170, 0, 0, 0, 0, 0, 0, 0}},
{{0, 187, 187, 0, 0, 0, 0, 0, 0, 0}},
Expand Down

0 comments on commit 0ae7af1

Please sign in to comment.