From 5f27a9f1f9fa05d6b06c1e19f0e0cc20ff03adda Mon Sep 17 00:00:00 2001 From: li_junsong Date: Fri, 27 Dec 2024 16:07:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9NTP=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E4=BD=BF=E7=94=A8boottime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li_junsong --- services/time/include/sntp_client.h | 2 +- services/time/src/ntp_update_time.cpp | 4 +- services/time/src/sntp_client.cpp | 28 ++++---- services/time_system_ability.cpp | 46 +++--------- .../common/time_service_fuzz_utils.cpp | 2 +- utils/BUILD.gn | 1 + utils/native/include/time_common.h | 9 +++ utils/native/src/time_common.cpp | 71 +++++++++++++++++++ 8 files changed, 106 insertions(+), 57 deletions(-) create mode 100644 utils/native/src/time_common.cpp diff --git a/services/time/include/sntp_client.h b/services/time/include/sntp_client.h index 8a88c47d..51044c04 100644 --- a/services/time/include/sntp_client.h +++ b/services/time/include/sntp_client.h @@ -129,7 +129,7 @@ private: */ int64_t ConvertNtpToStamp(uint64_t _ntpTs); int64_t m_clockOffset; - uint64_t m_originateTimestamp; + int64_t m_originateTimestamp; int64_t mNtpTime; int64_t mNtpTimeReference; int64_t mRoundTripTime; diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index 5245abdb..4444dee9 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -109,7 +109,7 @@ void NtpUpdateTime::UpdateNITZSetTime() { auto bootTimeNano = steady_clock::now().time_since_epoch().count(); auto bootTimeMilli = bootTimeNano / NANO_TO_MILLISECOND; - if (TimeSystemAbility::GetInstance()->GetBootTimeMs(lastNITZUpdateTime_) != ERR_OK) { + if (TimeUtils::GetBootTimeMs(lastNITZUpdateTime_) != ERR_OK) { TIME_HILOGE(TIME_MODULE_SERVICE, "get boot time fail."); } TIME_HILOGD(TIME_MODULE_SERVICE, "nitz time changed."); @@ -232,7 +232,7 @@ void NtpUpdateTime::SetSystemTime() return; } int64_t curBootTime = 0; - if (TimeSystemAbility::GetInstance()->GetBootTimeMs(curBootTime) != ERR_OK) { + if (TimeUtils::GetBootTimeMs(curBootTime) != ERR_OK) { TIME_HILOGE(TIME_MODULE_SERVICE, "get boot time fail."); requestMutex_.unlock(); return; diff --git a/services/time/src/sntp_client.cpp b/services/time/src/sntp_client.cpp index a13f2832..8e9864d6 100644 --- a/services/time/src/sntp_client.cpp +++ b/services/time/src/sntp_client.cpp @@ -194,7 +194,10 @@ void SNTPClient::CreateMessage(char *buffer) ConvertUnixToNtp(&ntp, &unix); uint64_t _ntpTs = ntp.second; _ntpTs = (_ntpTs << RECEIVE_TIMESTAMP_OFFSET) | ntp.fraction; - m_originateTimestamp = _ntpTs; + errno_t ret = TimeUtils::GetBootTimeMs(m_originateTimestamp); + if (ret != E_TIME_OK) { + return; + } SNTPMessage _sntpMsg{}; // Important, if you don't set the version/mode, the server will ignore you. @@ -205,7 +208,7 @@ void SNTPClient::CreateMessage(char *buffer) // optional (?) _sntpMsg._originateTimestamp = _ntpTs; char value[sizeof(uint64_t)]; - errno_t ret = memcpy_s(value, sizeof(uint64_t), &_sntpMsg._originateTimestamp, sizeof(uint64_t)); + ret = memcpy_s(value, sizeof(uint64_t), &_sntpMsg._originateTimestamp, sizeof(uint64_t)); if (ret != EOK) { TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %{public}d", ret); return; @@ -224,12 +227,11 @@ void SNTPClient::CreateMessage(char *buffer) void SNTPClient::ReceivedMessage(char *buffer) { - struct ntp_timestamp ntp{}; - struct timeval unix; - gettimeofday(&unix, NULL); - ConvertUnixToNtp(&ntp, &unix); - uint64_t _ntpTs = ntp.second; - _ntpTs = (_ntpTs << RECEIVE_TIMESTAMP_OFFSET) | ntp.fraction; + int64_t receiveBootTime = 0; + errno_t ret = TimeUtils::GetBootTimeMs(receiveBootTime); + if (ret != E_TIME_OK) { + return; + } SNTPMessage _sntpMsg; _sntpMsg.clear(); _sntpMsg._leapIndicator = buffer[INDEX_ZERO] >> SNTP_MSG_OFFSET_SIX; @@ -250,18 +252,14 @@ void SNTPClient::ReceivedMessage(char *buffer) _sntpMsg._originateTimestamp = GetNtpTimestamp64(ORIGINATE_TIMESTAMP_OFFSET, buffer); _sntpMsg._receiveTimestamp = GetNtpTimestamp64(RECEIVE_TIMESTAMP_OFFSET, buffer); _sntpMsg._transmitTimestamp = GetNtpTimestamp64(TRANSMIT_TIMESTAMP_OFFSET, buffer); - uint64_t _tempOriginate = m_originateTimestamp; - if (_sntpMsg._originateTimestamp > 0) { - _tempOriginate = _sntpMsg._originateTimestamp; - } - int64_t _originClient = ConvertNtpToStamp(_tempOriginate); + int64_t _originClient = m_originateTimestamp; int64_t _receiveServer = ConvertNtpToStamp(_sntpMsg._receiveTimestamp); int64_t _transmitServer = ConvertNtpToStamp(_sntpMsg._transmitTimestamp); - int64_t _receiveClient = ConvertNtpToStamp(_ntpTs); + int64_t _receiveClient = receiveBootTime; int64_t _clockOffset = (((_receiveServer - _originClient) + (_transmitServer - _receiveClient)) / INDEX_TWO); int64_t _roundTripDelay = (_receiveClient - _originClient) - (_transmitServer - _receiveServer); mRoundTripTime = _roundTripDelay; - mNtpTime = ConvertNtpToStamp(_ntpTs) + _clockOffset; + mNtpTime = receiveBootTime + _clockOffset; mNtpTimeReference = std::chrono::duration_cast( NtpTrustedTime::GetInstance().GetBootTimeNs().time_since_epoch()).count(); SetClockOffset(_clockOffset); diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index 7ba860cd..a45dea11 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -498,9 +498,9 @@ bool TimeSystemAbility::SetRealTime(int64_t time) } sptr instance = TimeSystemAbility::GetInstance(); int64_t beforeTime = 0; - instance->GetWallTimeMs(beforeTime); + TimeUtils::GetWallTimeMs(beforeTime); int64_t bootTime = 0; - instance->GetBootTimeMs(bootTime); + TimeUtils::GetBootTimeMs(bootTime); TIME_HILOGI(TIME_MODULE_SERVICE, "Before Current Time: %{public}s" " Set time: %{public}s" @@ -513,7 +513,7 @@ bool TimeSystemAbility::SetRealTime(int64_t time) return false; } int64_t currentTime = 0; - if (GetWallTimeMs(currentTime) != ERR_OK) { + if (TimeUtils::GetWallTimeMs(currentTime) != ERR_OK) { TIME_HILOGE(TIME_MODULE_SERVICE, "currentTime get failed"); return false; } @@ -636,7 +636,7 @@ void TimeSystemAbility::DumpProxyTimerInfo(int fd, const std::vectorGetWallTimeMs(currentTime); + TimeUtils::GetWallTimeMs(currentTime); auto bundleList = TimeFileUtils::GetParameterList(SCHEDULED_POWER_ON_APPS); for (uint32_t i = 0; i < size; ++i) { std::string bundleName = std::get(resultSet[i]); diff --git a/test/fuzztest/timeservice_fuzzer/common/time_service_fuzz_utils.cpp b/test/fuzztest/timeservice_fuzzer/common/time_service_fuzz_utils.cpp index 678130af..a30e272c 100644 --- a/test/fuzztest/timeservice_fuzzer/common/time_service_fuzz_utils.cpp +++ b/test/fuzztest/timeservice_fuzzer/common/time_service_fuzz_utils.cpp @@ -43,7 +43,7 @@ void TimeServiceFuzzUtils::SetTimer() TimeSystemAbility::GetInstance()->CreateTimer( timerPara, [](uint64_t id) {return 0;}, timerId); int64_t triggerTime = 0; - TimeSystemAbility::GetInstance()->GetWallTimeMs(triggerTime); + TimeUtils::GetWallTimeMs(triggerTime); TimeSystemAbility::GetInstance()->StartTimer(timerId, triggerTime + DELAY_TIME); } } // namespace OHOS \ No newline at end of file diff --git a/utils/BUILD.gn b/utils/BUILD.gn index f262a277..9ca13e50 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -27,6 +27,7 @@ ohos_source_set("time_utils") { sources = [ "${time_service_path}/time_permission.cpp", + "${time_utils_path}/native/src/time_common.cpp", "${time_utils_path}/native/src/time_file_utils.cpp", "${time_utils_path}/native/src/time_xcollie.cpp", ] diff --git a/utils/native/include/time_common.h b/utils/native/include/time_common.h index 3cd8a301..121c8e9e 100644 --- a/utils/native/include/time_common.h +++ b/utils/native/include/time_common.h @@ -17,6 +17,7 @@ #define SERVICES_INCLUDE_TIME_COMMON_H #include +#include #include "errors.h" #include "time_hilog.h" @@ -68,6 +69,14 @@ enum DatabaseType { NOT_STORE = 0, STORE, }; + +class TimeUtils { +public: + static int32_t GetWallTimeMs(int64_t &time); + static int32_t GetBootTimeMs(int64_t &time); + static int32_t GetBootTimeNs(int64_t &time); + static bool GetTimeByClockId(clockid_t clockId, struct timespec &tv); +}; } // namespace MiscServices } // namespace OHOS #endif // SERVICES_INCLUDE_TIME_COMMON_H \ No newline at end of file diff --git a/utils/native/src/time_common.cpp b/utils/native/src/time_common.cpp new file mode 100644 index 00000000..7ecd28cd --- /dev/null +++ b/utils/native/src/time_common.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "time_common.h" +#include "time_hilog.h" + + +namespace OHOS { +namespace MiscServices { +namespace { +static const int MILLI_TO_SEC = 1000LL; +static const int NANO_TO_SEC = 1000000000LL; +constexpr int32_t NANO_TO_MILLI = NANO_TO_SEC / MILLI_TO_SEC; +} + +int32_t TimeUtils::GetWallTimeMs(int64_t &time) +{ + struct timespec tv {}; + if (!GetTimeByClockId(CLOCK_REALTIME, tv)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "get failed."); + return E_TIME_SA_DIED; + } + time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI; + return E_TIME_OK; +} + +int32_t TimeUtils::GetBootTimeNs(int64_t &time) +{ + struct timespec tv {}; + if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "get failed."); + return E_TIME_SA_DIED; + } + time = tv.tv_sec * NANO_TO_SEC + tv.tv_nsec; + return E_TIME_OK; +} + +int32_t TimeUtils::GetBootTimeMs(int64_t &time) +{ + struct timespec tv {}; + if (!GetTimeByClockId(CLOCK_BOOTTIME, tv)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "get failed."); + return E_TIME_SA_DIED; + } + time = tv.tv_sec * MILLI_TO_SEC + tv.tv_nsec / NANO_TO_MILLI; + return E_TIME_OK; +} + +bool TimeUtils::GetTimeByClockId(clockid_t clockId, struct timespec &tv) +{ + if (clock_gettime(clockId, &tv) < 0) { + TIME_HILOGE(TIME_MODULE_CLIENT, "Failed clock_gettime, errno: %{public}s.", strerror(errno)); + return false; + } + return true; +} + +} +} \ No newline at end of file -- Gitee