blob: 5b7d03b212d67226a785ed2989055af891e44efc [file] [log] [blame]
pankore78f204a2022-01-25 22:21:09 +08001
pankore544da73c2021-10-19 02:51:16 +08002/*
3 *
4 * Copyright (c) 2020 Project CHIP Authors
5 * All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @file
22 * Provides implementations of the CHIP System Layer platform
23 * time/clock functions that are suitable for use on the Ameba platform.
24 */
25/* this file behaves like a config.h, comes first */
26#include <platform/internal/CHIPDeviceLayerInternal.h>
27
pankoreb79b7812022-01-24 21:03:12 +080028#include <platform/Ameba/SystemTimeSupport.h>
pankore544da73c2021-10-19 02:51:16 +080029#include <support/logging/CHIPLogging.h>
30
31#include "task.h"
pankore395e4042023-06-15 20:59:23 +080032#include <chip_porting.h>
pankore544da73c2021-10-19 02:51:16 +080033#include <time.h>
34
pankore544da73c2021-10-19 02:51:16 +080035struct rtkTimeVal
36{
37 uint32_t tv_sec; /* seconds */
38 uint32_t tv_usec; /* microseconds */
39};
40
41namespace chip {
42namespace System {
pankoreb79b7812022-01-24 21:03:12 +080043namespace Clock {
pankore544da73c2021-10-19 02:51:16 +080044
pankoreb79b7812022-01-24 21:03:12 +080045namespace Internal {
46ClockImpl gClockImpl;
47} // namespace Internal
48
49Microseconds64 ClockImpl::GetMonotonicMicroseconds64(void)
pankore544da73c2021-10-19 02:51:16 +080050{
pankoreb79b7812022-01-24 21:03:12 +080051 return (Clock::Microseconds64(xTaskGetTickCount()) * configTICK_RATE_HZ);
pankore544da73c2021-10-19 02:51:16 +080052}
53
pankoreb79b7812022-01-24 21:03:12 +080054Milliseconds64 ClockImpl::GetMonotonicMilliseconds64(void)
pankore544da73c2021-10-19 02:51:16 +080055{
pankoreb79b7812022-01-24 21:03:12 +080056 return (Clock::Milliseconds64(xTaskGetTickCount()));
pankore544da73c2021-10-19 02:51:16 +080057}
58
pankoreb79b7812022-01-24 21:03:12 +080059CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & curTime)
pankore544da73c2021-10-19 02:51:16 +080060{
Michael Sandstedt530e3252022-06-10 15:17:26 -050061 // TODO(19081): This platform does not properly error out if wall clock has
62 // not been set. For now, short circuit this.
63 return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
64#if 0
pankore544da73c2021-10-19 02:51:16 +080065 time_t seconds;
66 struct rtkTimeVal tv;
67
pankore395e4042023-06-15 20:59:23 +080068 seconds = matter_rtc_read();
pankore544da73c2021-10-19 02:51:16 +080069
70 tv.tv_sec = (uint32_t) seconds;
71 tv.tv_usec = 0;
72
73 if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD)
74 {
75 return CHIP_ERROR_REAL_TIME_NOT_SYNCED;
76 }
pankoreb79b7812022-01-24 21:03:12 +080077 static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!");
Arkadiusz Bokowy410face2022-05-31 20:02:00 +020078 curTime = Microseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec));
pankore544da73c2021-10-19 02:51:16 +080079
80 return CHIP_NO_ERROR;
Michael Sandstedt530e3252022-06-10 15:17:26 -050081#endif
pankore544da73c2021-10-19 02:51:16 +080082}
83
Arkadiusz Bokowy410face2022-05-31 20:02:00 +020084CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Milliseconds64 & aCurTime)
pankore544da73c2021-10-19 02:51:16 +080085{
Arkadiusz Bokowy410face2022-05-31 20:02:00 +020086 Microseconds64 curTimeUs;
87 auto err = GetClock_RealTime(curTimeUs);
88 aCurTime = std::chrono::duration_cast<Milliseconds64>(curTimeUs);
89 return err;
pankore544da73c2021-10-19 02:51:16 +080090}
91
Arkadiusz Bokowy410face2022-05-31 20:02:00 +020092CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime)
pankore544da73c2021-10-19 02:51:16 +080093{
pankoreb79b7812022-01-24 21:03:12 +080094 struct rtkTimeVal tv;
95 tv.tv_sec = static_cast<uint32_t>(aNewCurTime.count() / UINT64_C(1000000));
96 tv.tv_usec = static_cast<uint32_t>(aNewCurTime.count() % UINT64_C(1000000));
pankore395e4042023-06-15 20:59:23 +080097 matter_rtc_init();
98 matter_rtc_write(tv.tv_sec);
pankore544da73c2021-10-19 02:51:16 +080099
100 return CHIP_NO_ERROR;
101}
102
pankoreb79b7812022-01-24 21:03:12 +0800103CHIP_ERROR InitClock_RealTime()
104{
105 Clock::Microseconds64 curTime =
106 Clock::Microseconds64((static_cast<uint64_t>(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) * UINT64_C(1000000)));
107 // Use CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD as the initial value of RealTime.
108 // Then the RealTime obtained from GetClock_RealTime will be always valid.
Michael Sandstedt530e3252022-06-10 15:17:26 -0500109 //
110 // TODO(19081): This is broken because it causes the platform to report
111 // that it does have wall clock time when it actually doesn't.
pankoreb79b7812022-01-24 21:03:12 +0800112 return System::SystemClock().SetClock_RealTime(curTime);
113}
114
115} // namespace Clock
pankore544da73c2021-10-19 02:51:16 +0800116} // namespace System
117} // namespace chip