diff --git a/interfaces/kits/js/napi/system_time/BUILD.gn b/interfaces/kits/js/napi/system_time/BUILD.gn index c161edf697c5b196c9a9a3abd745d1959c75deee..641f90b1284ee1150ff889c244abbcb408855281 100644 --- a/interfaces/kits/js/napi/system_time/BUILD.gn +++ b/interfaces/kits/js/napi/system_time/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("systemtime") { ] external_deps = [ + "ability_base:base", "ability_base:want", "bundle_framework:appexecfwk_base", "hiviewdfx_hilog_native:libhilog", diff --git a/interfaces/kits/js/napi/system_timer/BUILD.gn b/interfaces/kits/js/napi/system_timer/BUILD.gn index c4cc654ca9862a2176851d10fc68649573ad8c17..6d8a7cc2d58321dddf46c9a8cf5d619932f947d4 100644 --- a/interfaces/kits/js/napi/system_timer/BUILD.gn +++ b/interfaces/kits/js/napi/system_timer/BUILD.gn @@ -58,6 +58,7 @@ ohos_shared_library("systemtimer") { ] external_deps = [ + "ability_base:base", "ability_base:want", "bundle_framework:appexecfwk_base", "hiviewdfx_hilog_native:libhilog", diff --git a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp index 8a2af2ecf00dd474b8f43692b335940c303aafa6..d9c6dee4f3499b3fbe8096123ff13cf7fcbd5c9c 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -406,7 +406,7 @@ napi_value ParseParametersByStartTimer(const napi_env &env, const napi_value (&a int64_t timerId = 0; napi_get_value_int64(env, argv[0], &timerId); NAPI_ASSERT(env, timerId >= 0, "Wrong argument timer. Positive number expected."); - uintTimerId = (uint64_t)timerId; + uintTimerId = static_cast(timerId); // argv[1]: triggerTime NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); @@ -528,7 +528,7 @@ napi_value ParseParametersByStopTimer(const napi_env &env, const napi_value (&ar int64_t timerId = 0; napi_get_value_int64(env, argv[0], &timerId); NAPI_ASSERT(env, timerId >= 0, "Wrong argument timer. Positive number expected."); - uintTimerId = (uint64_t)timerId; + uintTimerId = static_cast(timerId); // argv[1]:callback if (argc >= STOP_MAX_PARA) { diff --git a/services/time_manager/.DS_Store b/services/time_manager/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/services/time_manager/.DS_Store differ diff --git a/services/time_manager/include/ntp_trusted_time.h b/services/time_manager/include/ntp_trusted_time.h index 331cb8e3a5a19b52d41a39bc7dce3ffebe8e93aa..3d87834123c29776a80e1431723a4a55e38a3f89 100644 --- a/services/time_manager/include/ntp_trusted_time.h +++ b/services/time_manager/include/ntp_trusted_time.h @@ -48,6 +48,7 @@ public: int64_t GetCertaintyMillis(); int64_t CurrentTimeMillis(); int64_t GetAgeMillis(); + void Clear(); private: int64_t mTimeMillis; int64_t mElapsedRealtimeMillis; diff --git a/services/time_manager/include/sntp_client.h b/services/time_manager/include/sntp_client.h index d9c3517fbf0119cc3c69234d1c216ec52494be62..d4bc6a162165ed6caec456a923ad1cc3b4443e42 100644 --- a/services/time_manager/include/sntp_client.h +++ b/services/time_manager/include/sntp_client.h @@ -144,7 +144,7 @@ private: * @param _ntpTs the NTP timestamp to be converted * Returns the milliseconds */ - uint64_t ConvertNtpToStamp(uint64_t _ntpTs); + int64_t ConvertNtpToStamp(uint64_t _ntpTs); /** * This function converts the NTP time to local time @@ -154,8 +154,8 @@ private: */ uint64_t ConvertNtpToDate(uint64_t _ntpTs, struct date_structure* _outDataTs); - int m_clockOffset; - int64_t m_originateTimestamp; + int64_t m_clockOffset; + uint64_t m_originateTimestamp; int64_t mNtpTime; int64_t mNtpTimeReference; int64_t mRoundTripTime; diff --git a/services/time_manager/src/.DS_Store b/services/time_manager/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/services/time_manager/src/.DS_Store differ diff --git a/services/time_manager/src/ntp_trusted_time.cpp b/services/time_manager/src/ntp_trusted_time.cpp index 6dd59895cc7eb7863a02bcc19f566ddec13d8869..c787893514fe5e15fa796eabac67b2abfb5b931e 100644 --- a/services/time_manager/src/ntp_trusted_time.cpp +++ b/services/time_manager/src/ntp_trusted_time.cpp @@ -14,6 +14,11 @@ */ #include +#include +#include +#include +#include +#include #include "time_common.h" #include "sntp_client.h" #include "ntp_trusted_time.h" @@ -31,6 +36,9 @@ bool NtpTrustedTime::ForceRefresh(std::string ntpServer) { SNTPClient client; if (client.RequestTime(ntpServer)) { + if (mTimeResult != nullptr) { + mTimeResult->Clear(); + } int64_t ntpCertainty = client.getRoundTripTime() / 2; mTimeResult = std::make_shared(client.getNtpTIme(), client.getNtpTimeReference(), ntpCertainty); TIME_HILOGD(TIME_MODULE_SERVICE, "Get Ntp time result"); @@ -46,7 +54,7 @@ int64_t NtpTrustedTime::CurrentTimeMillis() TIME_HILOGD(TIME_MODULE_SERVICE, "Missing authoritative time source"); return INVALID_MILLIS; } - return mTimeResult->CurrentTimeMillis(); + return mTimeResult->GetTimeMillis(); } bool NtpTrustedTime::HasCache() @@ -109,5 +117,12 @@ NtpTrustedTime::TimeResult::TimeResult(int64_t mTimeMillis, int64_t mElapsedReal this->mElapsedRealtimeMillis = mElapsedRealtimeMills; this->mCertaintyMillis = mCertaintyMillis; } + +void NtpTrustedTime::TimeResult::Clear() +{ + TIME_HILOGD(TIME_MODULE_SERVICE, "start."); + (void)memset_s(this, sizeof(*this), 0, sizeof(*this)); + TIME_HILOGD(TIME_MODULE_SERVICE, "end."); +} } // MiscServices } // OHOS \ No newline at end of file diff --git a/services/time_manager/src/sntp_client.cpp b/services/time_manager/src/sntp_client.cpp index cf2b23622aea4caeca5d481667a7d0d53c032ef3..49a5797b572a00fa287a2ff7f390e526c4f74531 100644 --- a/services/time_manager/src/sntp_client.cpp +++ b/services/time_manager/src/sntp_client.cpp @@ -33,7 +33,6 @@ namespace OHOS { namespace MiscServices { namespace { constexpr auto SECONDS_SINCE_FIRST_EPOCH = (2208988800UL); // Seconds from 1/1/1900 00.00 to 1/1/1970 00.00; - constexpr uint64_t MICRO_TO_MILESECOND = 1000; constexpr uint64_t MILLISECOND_TO_SECOND = 1000; constexpr uint64_t FRACTION_TO_SECOND = 0x100000000; constexpr uint64_t UINT32_MASK = 0xFFFFFFFF; @@ -88,18 +87,9 @@ bool SNTPClient::RequestTime(std::string host) TIME_HILOGE(TIME_MODULE_SERVICE, "Get host by name %{public}s but get nullptr.", host.c_str()); return false; } - - errno_t ret = memset_s((char*)& RecvAddr, sizeof(RecvAddr), 0, sizeof(RecvAddr)); - if (ret != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret); - return false; - } + (void)memset_s((char*)& RecvAddr, sizeof(RecvAddr), 0, sizeof(RecvAddr)); RecvAddr.sin_family = AF_INET; - errno_t ret1 = memcpy_s((char*)& RecvAddr.sin_addr.s_addr, hostV->h_length, (char*)hostV->h_addr, hostV->h_length); - if (ret1 != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret1); - return false; - } + (void)memcpy_s((char*)& RecvAddr.sin_addr.s_addr, hostV->h_length, (char*)hostV->h_addr, hostV->h_length); RecvAddr.sin_port = htons(Port); if (connect(SendSocket, (struct sockaddr*) & RecvAddr, sizeof(RecvAddr)) < 0) { TIME_HILOGE(TIME_MODULE_SERVICE, "Connect socket failed with host: %{public}s", host.c_str()); @@ -144,11 +134,8 @@ uint64_t SNTPClient::GetNtpTimestamp64(int offset, char* buffer) { const int _len = sizeof(uint64_t); char valueRx[_len]; - errno_t ret = memset_s(valueRx, sizeof(uint64_t), 0, sizeof(uint64_t)); - if (ret != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret); - return false; - } + (void)memset_s(valueRx, sizeof(uint64_t), 0, sizeof(uint64_t)); + int numOfBit = sizeof(uint64_t) - 1; for (int loop = offset; loop < offset + _len; loop++) { valueRx[numOfBit] = buffer[loop]; @@ -156,12 +143,7 @@ uint64_t SNTPClient::GetNtpTimestamp64(int offset, char* buffer) } uint64_t milliseconds; - errno_t ret1 = memcpy_s(&milliseconds, sizeof(uint64_t), valueRx, sizeof(uint64_t)); - if (ret1 != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret1); - return false; - } - + (void)memcpy_s(&milliseconds, sizeof(uint64_t), valueRx, sizeof(uint64_t)); return milliseconds; } @@ -228,7 +210,7 @@ uint64_t SNTPClient::ConvertNtpToDate(uint64_t _ntpTs, struct date_structure *_o * | Seconds Fraction (0-padded) | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ -uint64_t SNTPClient::ConvertNtpToStamp(uint64_t _ntpTs) +int64_t SNTPClient::ConvertNtpToStamp(uint64_t _ntpTs) { uint32_t second = (uint32_t)((_ntpTs >> RECEIVE_TIMESTAMP_OFFSET) & UINT32_MASK); uint32_t fraction = (uint32_t)(_ntpTs & UINT32_MASK); @@ -261,11 +243,8 @@ 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)); - if (ret != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret); - return; - } + (void)memcpy_s(value, sizeof(uint64_t), &_sntpMsg._originateTimestamp, sizeof(uint64_t)); + int numOfBit = sizeof(uint64_t) - 1; int ofssetEnd = ORIGINATE_TIMESTAMP_OFFSET + sizeof(uint64_t); for (int loop = ORIGINATE_TIMESTAMP_OFFSET; loop < ofssetEnd; loop++) { @@ -291,11 +270,8 @@ void SNTPClient::WriteTimeStamp(char* buffer, ntp_timestamp *ntp) _sntpMsg._mode = MODE_THREE; _sntpMsg._originateTimestamp = _ntpTs; char value[sizeof(uint64_t)]; - errno_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 = %d\n", ret); - return; - } + (void)memcpy_s(value, sizeof(uint64_t), &_sntpMsg._originateTimestamp, sizeof(uint64_t)); + int numOfBit = sizeof(uint64_t) - 1; int ofssetEnd = ORIGINATE_TIMESTAMP_OFFSET + sizeof(uint64_t); for (int loop = ORIGINATE_TIMESTAMP_OFFSET; loop < ofssetEnd; loop++) { @@ -343,16 +319,13 @@ void SNTPClient::ReceivedMessage(char* buffer) _tempOriginate = _sntpMsg._originateTimestamp; } - struct date_structure dataTs; - uint64_t _originClient = ConvertNtpToDate(_tempOriginate, &dataTs); - uint64_t _receiveServer = ConvertNtpToDate(_sntpMsg._receiveTimestamp, &dataTs); - uint64_t _transmitServer = ConvertNtpToDate(_sntpMsg._transmitTimestamp, &dataTs); - uint64_t _receiveClient = ConvertNtpToDate(_ntpTs, &dataTs); + int64_t _originClient = ConvertNtpToStamp(_tempOriginate); + int64_t _receiveServer = ConvertNtpToStamp(_sntpMsg._receiveTimestamp); + int64_t _transmitServer = ConvertNtpToStamp(_sntpMsg._transmitTimestamp); + int64_t _receiveClient = ConvertNtpToStamp(_ntpTs); - int _clockOffset = (((_receiveServer - _originClient) + (_transmitServer - _receiveClient)) / INDEX_TWO); - _clockOffset = _clockOffset / MICRO_TO_MILESECOND; - int _roundTripDelay = (_receiveClient - _originClient) - (_receiveServer - _transmitServer); - _roundTripDelay = _roundTripDelay / MICRO_TO_MILESECOND; + int64_t _clockOffset = (((_receiveServer - _originClient) + (_transmitServer - _receiveClient)) / INDEX_TWO); + int64_t _roundTripDelay = (_receiveClient - _originClient) - (_transmitServer - _receiveServer); mRoundTripTime = _roundTripDelay; mNtpTime = ConvertNtpToStamp(_ntpTs) + _clockOffset; mNtpTimeReference = std::chrono::duration_cast @@ -364,11 +337,8 @@ unsigned int SNTPClient::GetNtpField32(int offset, char* buffer) { const int _len = sizeof(int); char valueRx[_len]; - errno_t ret = memset_s(valueRx, _len, 0, _len); - if (ret != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", ret); - return false; - } + (void)memset_s(valueRx, _len, 0, _len); + int numOfBit = sizeof(int) - 1; for (int loop = offset; loop < offset + _len; loop++) { valueRx[numOfBit] = buffer[loop]; @@ -376,12 +346,8 @@ unsigned int SNTPClient::GetNtpField32(int offset, char* buffer) } unsigned int milliseconds; - errno_t retValue = memcpy_s(&milliseconds, sizeof(int), valueRx, sizeof(int)); - if (retValue != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed, err = %d\n", retValue); - milliseconds = 0; - return milliseconds; - } + (void)memcpy_s(&milliseconds, sizeof(int), valueRx, sizeof(int)); + TIME_HILOGD(TIME_MODULE_SERVICE, "end."); return milliseconds; } @@ -397,10 +363,9 @@ void SNTPClient::GetReferenceId(int offset, char* buffer, int* _outArray) void SNTPClient::SNTPMessage::clear() { - errno_t ret = memset_s(this, sizeof(*this), 0, sizeof(*this)); - if (ret != EOK) { - TIME_HILOGE(TIME_MODULE_SERVICE, "memcpy_s failed."); - } + TIME_HILOGD(TIME_MODULE_SERVICE, "start."); + (void)memset_s(this, sizeof(*this), 0, sizeof(*this)); + TIME_HILOGD(TIME_MODULE_SERVICE, "end."); } int64_t SNTPClient::getNtpTIme() diff --git a/services/time_manager/src/time_service_stub.cpp b/services/time_manager/src/time_service_stub.cpp index 4674291c247e815022eed6c7e4eaefeb6eeb9f1d..93e839228574a91381b7b4cc0db138854714fbc1 100644 --- a/services/time_manager/src/time_service_stub.cpp +++ b/services/time_manager/src/time_service_stub.cpp @@ -40,6 +40,8 @@ TimeServiceStub::TimeServiceStub() memberFuncMap_[START_TIMER] = &TimeServiceStub::OnStartTimer; memberFuncMap_[STOP_TIMER] = &TimeServiceStub::OnStopTimer; memberFuncMap_[DESTORY_TIMER] = &TimeServiceStub::OnDestoryTimer; + memberFuncMap_[NETWORK_TIME_ON] = &TimeServiceStub::OnNetworkTimeStatusOn; + memberFuncMap_[NETWORK_TIME_OFF] = &TimeServiceStub::OnNetworkTimeStatusOff; } TimeServiceStub::~TimeServiceStub() diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 66c1e695ec6f78cf543638ebcd79abcc09948baf..8c5681e5d3450cd0d86138eaa557b896f8172c02 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -314,12 +314,16 @@ HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level0) */ HWTEST_F(TimeServiceTest, NetworkTime001, TestSize.Level0) { + TimeServiceClient::GetInstance()->NetworkTimeStatusOn(); + sleep(5); auto UTCTimeMicro1 = system_clock::now().time_since_epoch().count(); auto UTCTimeMillis = (UTCTimeMicro1 / 1000) + 86400000; TimeServiceClient::GetInstance()->SetTime(UTCTimeMillis); - TimeServiceClient::GetInstance()->NetworkTimeStatusOn(); - sleep(5000); auto UTCTimeMicro2 = system_clock::now().time_since_epoch().count(); - EXPECT_TRUE(UTCTimeMicro2 < UTCTimeMicro1); + TimeServiceClient::GetInstance()->NetworkTimeStatusOff(); + TimeServiceClient::GetInstance()->NetworkTimeStatusOn(); + sleep(5); + auto UTCTimeMicro3 = system_clock::now().time_since_epoch().count(); + EXPECT_TRUE(UTCTimeMicro3 < UTCTimeMicro2); TimeServiceClient::GetInstance()->NetworkTimeStatusOff(); } diff --git a/utils/native/include/time_hilog_wreapper.h b/utils/native/include/time_hilog_wreapper.h index c320e6933d6f6737632120dd8faaa2682aebaddc..fb7d982b99226d164f3f976732012fa9f9768b24 100644 --- a/utils/native/include/time_hilog_wreapper.h +++ b/utils/native/include/time_hilog_wreapper.h @@ -56,16 +56,16 @@ static constexpr OHOS::HiviewDFX::HiLogLabel TIME_MODULE_LABEL[TIME_MODULE_BUTT] {LOG_CORE, TIME_JS_NAPI, "TimeJSNAPI"}, }; -#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) -#define __FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__ +#define R_FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define R_FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, R_FILENAME__, __FUNCTION__, ##__VA_ARGS__ // In order to improve performance, do not check the module range. // Besides, make sure module is less than TIME_MODULE_BUTT. -#define TIME_HILOGF(module, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(TIME_MODULE_LABEL[module], __FORMATED(__VA_ARGS__)) -#define TIME_HILOGE(module, ...) (void)OHOS::HiviewDFX::HiLog::Error(TIME_MODULE_LABEL[module], __FORMATED(__VA_ARGS__)) -#define TIME_HILOGW(module, ...) (void)OHOS::HiviewDFX::HiLog::Warn(TIME_MODULE_LABEL[module], __FORMATED(__VA_ARGS__)) -#define TIME_HILOGI(module, ...) (void)OHOS::HiviewDFX::HiLog::Info(TIME_MODULE_LABEL[module], __FORMATED(__VA_ARGS__)) -#define TIME_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(TIME_MODULE_LABEL[module], __FORMATED(__VA_ARGS__)) +#define TIME_HILOGF(module, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(TIME_MODULE_LABEL[module], R_FORMATED(__VA_ARGS__)) +#define TIME_HILOGE(module, ...) (void)OHOS::HiviewDFX::HiLog::Error(TIME_MODULE_LABEL[module], R_FORMATED(__VA_ARGS__)) +#define TIME_HILOGW(module, ...) (void)OHOS::HiviewDFX::HiLog::Warn(TIME_MODULE_LABEL[module], R_FORMATED(__VA_ARGS__)) +#define TIME_HILOGI(module, ...) (void)OHOS::HiviewDFX::HiLog::Info(TIME_MODULE_LABEL[module], R_FORMATED(__VA_ARGS__)) +#define TIME_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(TIME_MODULE_LABEL[module], R_FORMATED(__VA_ARGS__)) } // namespace MiscServices } // namespace OHOS #endif // TIME_HILOG_WRAPPER_H