1 2 /* 3 * Copyright (C) 2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SNTP_CLIENT_NTP_TRUSTED_TIME_H 18 #define SNTP_CLIENT_NTP_TRUSTED_TIME_H 19 20 #include "time_common.h" 21 #include <mutex> 22 23 namespace OHOS { 24 namespace MiscServices { 25 class NtpTrustedTime { 26 public: 27 static NtpTrustedTime &GetInstance(); 28 bool ForceRefresh(const std::string &ntpServer); 29 int64_t GetCacheAge(); 30 int64_t CurrentTimeMillis(); 31 int64_t ElapsedRealtimeMillis(); 32 std::chrono::steady_clock::time_point GetBootTimeNs(); 33 class TimeResult : std::enable_shared_from_this<TimeResult> { 34 public: 35 TimeResult(); 36 TimeResult(int64_t mTimeMillis, int64_t mElapsedRealtimeMills, int64_t mCertaintyMillis); 37 ~TimeResult(); 38 int64_t GetTimeMillis(); 39 int64_t GetElapsedRealtimeMillis(); 40 int64_t CurrentTimeMillis(); 41 int64_t GetAgeMillis(); 42 void Clear(); 43 44 private: 45 // Calculated network time from NTP server. 46 int64_t mTimeMillis; 47 // Boot time when getting time from NTP server. 48 int64_t mElapsedRealtimeMillis; 49 int64_t mCertaintyMillis; 50 }; 51 52 private: 53 std::shared_ptr<TimeResult> mTimeResult {}; 54 static std::mutex mTimeResultMutex_; 55 }; 56 } // namespace MiscServices 57 } // namespace OHOS 58 59 #endif // SNTP_CLIENT_NTP_TRUSTED_TIME_H