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 <fstream>
21 #include <mutex>
22 #include <string>
23 #include <sys/time.h>
24 
25 #include "refbase.h"
26 #include "time.h"
27 #include "time_common.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 class NtpTrustedTime {
32 public:
33     static NtpTrustedTime &GetInstance();
34     bool ForceRefresh(const std::string &ntpServer);
35     int64_t GetCacheAge();
36     int64_t CurrentTimeMillis();
37     int64_t ElapsedRealtimeMillis();
38     std::chrono::steady_clock::time_point GetBootTimeNs();
39     class TimeResult : std::enable_shared_from_this<TimeResult> {
40     public:
41         TimeResult();
42         TimeResult(int64_t mTimeMillis, int64_t mElapsedRealtimeMills, int64_t mCertaintyMillis);
43         ~TimeResult();
44         int64_t GetTimeMillis();
45         int64_t GetElapsedRealtimeMillis();
46         int64_t CurrentTimeMillis();
47         int64_t GetAgeMillis();
48         void Clear();
49 
50     private:
51         // Calculated network time from NTP server.
52         int64_t mTimeMillis;
53         // Boot time when getting time from NTP server.
54         int64_t mElapsedRealtimeMillis;
55         int64_t mCertaintyMillis;
56     };
57 
58 private:
59     std::shared_ptr<TimeResult> mTimeResult {};
60 };
61 } // namespace MiscServices
62 } // namespace OHOS
63 
64 #endif // SNTP_CLIENT_NTP_TRUSTED_TIME_H