Skip to content

Commit

Permalink
Fix non-monotonic timestamps under Windows
Browse files Browse the repository at this point in the history
Fix usage of two separate calls to determine seconds and microseconds.
Replaced by single timespec_get call.
Fixed as well a race condition where provided sec or usec to
writeDLTMessageToFile are 0.

Issue: #232
  • Loading branch information
mbehr1 authored and alexmucde committed Oct 5, 2023
1 parent 180bdf3 commit a09306e
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3869,7 +3869,6 @@ void MainWindow::writeDLTMessageToFile(QByteArray &bufferHeader,char* bufferPayl
{
DltStorageHeader str;

//DltStorageHeader str;
str.pattern[0]='D';
str.pattern[1]='L';
str.pattern[2]='T';
Expand All @@ -3879,35 +3878,27 @@ void MainWindow::writeDLTMessageToFile(QByteArray &bufferHeader,char* bufferPayl
str.ecu[2]=0;
str.ecu[3]=0;

/* get time of day */
#if defined(_MSC_VER)
SYSTEMTIME systemtime;
GetSystemTime(&systemtime);
time_t timestamp_sec;
time(&timestamp_sec);
if(sec)
str.seconds = (time_t)sec;
else
str.seconds = (time_t)timestamp_sec;
if(usec)
str.microseconds = (int32_t)usec; // for some reasons we do not have microseconds in Windows !
else
str.microseconds = (int32_t)systemtime.wMilliseconds * 1000; // for some reasons we do not have microseconds in Windows !
#else
if (sec || usec)
{ // todo should better use ptrs and not != 0
str.seconds = (time_t)sec;
str.microseconds = (int32_t)usec;
}
else
{
#if defined(_MSC_VER)
struct timespec ts;
(void)timespec_get(&ts, TIME_UTC);
str.seconds = (time_t)ts.tv_sec;
str.microseconds = (int32_t)(ts.tv_nsec / 1000);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
if(sec)
str.seconds = (time_t)sec; /* value is long */
else
str.seconds = (time_t)tv.tv_sec; /* value is long */
if(usec)
str.microseconds = (int32_t)usec; /* value is long */
else
str.microseconds = (int32_t)tv.tv_usec; /* value is long */
#endif

if(ecuitem)
dlt_set_id(str.ecu,ecuitem->id.toLatin1());
str.seconds = (time_t)tv.tv_sec; /* value is long */
str.microseconds = (int32_t)tv.tv_usec; /* value is long */
#endif
}
if (ecuitem)
dlt_set_id(str.ecu, ecuitem->id.toLatin1());

/* check if message is matching the filter */
if (outputfile.isOpen())
Expand Down

0 comments on commit a09306e

Please sign in to comment.