Skip to content

Commit

Permalink
[Tizen] Enable system time platform support (project-chip#18834)
Browse files Browse the repository at this point in the history
* Remove unused CHIP system config platform define

It seems that the CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS
is not used anywhere.

* Enable time support for Tizen platform

This commit syncs SystemTimeSupport implementation between Linux and
Tizen platforms. Time support is required for CASE session setup.
  • Loading branch information
arkq authored May 27, 2022
1 parent d2a6470 commit 67a9f1e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/platform/Ameba/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct ChipDeviceEvent;
} // namespace chip

// ==================== Platform Adaptations ====================
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1
#define CHIP_SYSTEM_CONFIG_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *

Expand Down
1 change: 0 additions & 1 deletion src/platform/P6/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct ChipDeviceEvent;

// ==================== Platform Adaptations ====================

#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0
#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
Expand Down
3 changes: 1 addition & 2 deletions src/platform/Tizen/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct ChipDeviceEvent;
#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 0
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1

#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 1

Expand Down
69 changes: 25 additions & 44 deletions src/platform/Tizen/SystemTimeSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,30 @@
#include <lib/support/TimeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include <chrono>
#include <errno.h>
#include <inttypes.h>
#include <sys/time.h>

namespace chip {
namespace System {
namespace Platform {
namespace Layer {
namespace Clock {

uint64_t GetClock_Monotonic()
{
std::chrono::microseconds epoch =
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch());
// count() is nominally signed, but for a monotonic clock it cannot be
// negative.
return static_cast<uint64_t>(epoch.count());
}
namespace Internal {
ClockImpl gClockImpl;
} // namespace Internal

uint64_t GetClock_MonotonicMS()
Microseconds64 ClockImpl::GetMonotonicMicroseconds64()
{
std::chrono::milliseconds epoch =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch());
// count() is nominally signed, but for a monotonic clock it cannot be
// negative.
return static_cast<uint64_t>(epoch.count());
return std::chrono::duration_cast<Microseconds64>(std::chrono::steady_clock::now().time_since_epoch());
}

uint64_t GetClock_MonotonicHiRes()
Milliseconds64 ClockImpl::GetMonotonicMilliseconds64()
{
std::chrono::microseconds epoch =
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch());
// count() is nominally signed, but for a monotonic clock it cannot be
// negative.
return static_cast<uint64_t>(epoch.count());
return std::chrono::duration_cast<Milliseconds64>(std::chrono::steady_clock::now().time_since_epoch());
}

CHIP_ERROR GetClock_RealTime(uint64_t & curTime)
CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & aCurTime)
{
struct timeval tv;
if (gettimeofday(&tv, nullptr) != 0)
Expand All @@ -76,35 +67,26 @@ CHIP_ERROR GetClock_RealTime(uint64_t & curTime)
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!");
curTime = (static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec);
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 GetClock_RealTimeMS(uint64_t & curTime)
CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Clock::Milliseconds64 & aCurTime)
{
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)
Clock::Microseconds64 curTimeUs;
CHIP_ERROR err = GetClock_RealTime(curTimeUs);
if (err == CHIP_NO_ERROR)
{
return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
aCurTime = std::chrono::duration_cast<Clock::Milliseconds64>(curTimeUs);
}
static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!");
curTime = (static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000)) + (static_cast<uint64_t>(tv.tv_usec) / 1000);
return CHIP_NO_ERROR;
return err;
}

CHIP_ERROR SetClock_RealTime(uint64_t newCurTime)
CHIP_ERROR ClockImpl::SetClock_RealTime(Clock::Microseconds64 aNewCurTime)
{
struct timeval tv;
tv.tv_sec = static_cast<time_t>(newCurTime / UINT64_C(1000000));
tv.tv_usec = static_cast<long>(newCurTime % UINT64_C(1000000));
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);
Expand All @@ -121,7 +103,6 @@ CHIP_ERROR SetClock_RealTime(uint64_t newCurTime)
return CHIP_NO_ERROR;
}

} // namespace Layer
} // namespace Platform
} // namespace Clock
} // namespace System
} // namespace chip
1 change: 0 additions & 1 deletion src/platform/bouffalolab/BL602/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct ChipDeviceEvent;
} // namespace chip

// ==================== Platform Adaptations ====================
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_EVENT_FUNCTIONS 1
#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME 1
#define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
#define CHIP_SYSTEM_CONFIG_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
Expand Down
7 changes: 4 additions & 3 deletions src/protocols/secure_channel/CASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1463,9 +1463,10 @@ CHIP_ERROR CASESession::SetEffectiveTime()

if (err != CHIP_NO_ERROR)
{
ChipLogError(
SecureChannel,
"The device does not support GetClock_RealTimeMS() API. This will eventually result in CASE session setup failures.");
ChipLogError(SecureChannel,
"The device does not support GetClock_RealTimeMS() API. This will eventually result in CASE session setup "
"failures. API error: %" CHIP_ERROR_FORMAT,
err.Format());
// TODO: Remove use of hardcoded time during CASE setup
return GetHardcodedTime();
}
Expand Down

0 comments on commit 67a9f1e

Please sign in to comment.