Skip to content

Commit

Permalink
Linux: add support for GetClock_RealTimeMS
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebin14 committed Apr 23, 2022
1 parent 0cbfaec commit d4b1f59
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/platform/Linux/SystemTimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,64 @@ Milliseconds64 ClockImpl::GetMonotonicMilliseconds64()

CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
struct timeval tv;
if (gettimeofday(&tv, nullptr) != 0)
{
return CHIP_ERROR_POSIX(errno);
}
if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
if (tv.tv_usec < 0)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!");
aCurTime = Clock::Microseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec));
return CHIP_NO_ERROR;
}

CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
struct timeval tv;
if (gettimeofday(&tv, nullptr) != 0)
{
return CHIP_ERROR_POSIX(errno);
}
if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
if (tv.tv_usec < 0)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
}
static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!");
aCurTime =
Clock::Milliseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000)) + (static_cast<uint64_t>(tv.tv_usec) / 1000));
return CHIP_NO_ERROR;
}

CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
struct timeval tv;
tv.tv_sec = static_cast<time_t>(aNewCurTime.count() / UINT64_C(1000000));
tv.tv_usec = static_cast<long>(aNewCurTime.count() % UINT64_C(1000000));
if (settimeofday(&tv, nullptr) != 0)
{
return (errno == EPERM) ? CHIP_ERROR_ACCESS_DENIED : CHIP_ERROR_POSIX(errno);
}
#if CHIP_PROGRESS_LOGGING
{
const time_t timep = tv.tv_sec;
struct tm calendar;
localtime_r(&timep, &calendar);
ChipLogProgress(DeviceLayer, "Real time clock set to %ld (%04d/%02d/%02d %02d:%02d:%02d UTC)", tv.tv_sec, calendar.tm_year,
calendar.tm_mon, calendar.tm_mday, calendar.tm_hour, calendar.tm_min, calendar.tm_sec);
}
#endif // CHIP_PROGRESS_LOGGING
return CHIP_NO_ERROR;
}

} // namespace Clock
Expand Down

0 comments on commit d4b1f59

Please sign in to comment.