• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NTP_UPDATE_TIME_H
17 #define NTP_UPDATE_TIME_H
18 
19 #include <mutex>
20 
21 namespace OHOS {
22 namespace MiscServices {
23 struct AutoTimeInfo {
24     std::string ntpServer;
25     std::string ntpServerSpec;
26     std::string status;
27     int64_t lastUpdateTime;
28 };
29 enum NtpRefreshCode {
30     NO_NEED_REFRESH,
31     REFRESH_SUCCESS,
32     REFRESH_FAILED
33 };
34 
35 class NtpUpdateTime {
36 public:
37     static NtpUpdateTime &GetInstance();
38     static bool GetNtpTime(int64_t &time);
39     static bool GetRealTime(int64_t &time);
40     static void SetSystemTime();
41     static bool IsInUpdateInterval();
42     void RefreshNetworkTimeByTimer(uint64_t timerId);
43     void UpdateNITZSetTime();
44     void Stop();
45     void Init();
46     bool IsValidNITZTime();
47     uint64_t GetNITZUpdateTime();
48 
49 private:
50     NtpUpdateTime();
51     static NtpRefreshCode GetNtpTimeInner();
52     static bool CheckNeedSetTime(NtpRefreshCode code, int64_t time);
53     static bool GetRealTimeInner(int64_t &time);
54     static void ChangeNtpServerCallback(const char *key, const char *value, void *context);
55     static std::vector<std::string> SplitNtpAddrs(const std::string &ntpStr);
56     void StartTimer();
57     void RefreshNextTriggerTime();
58     bool CheckStatus();
59     void RegisterSystemParameterListener();
60     static void ChangeAutoTimeCallback(const char *key, const char *value, void *context);
61 
62     static AutoTimeInfo autoTimeInfo_;
63     uint64_t timerId_;
64     uint64_t nitzUpdateTimeMilli_;
65     uint64_t nextTriggerTime_;
66     static std::mutex requestMutex_;
67     int64_t lastNITZUpdateTime_;
68 };
69 } // namespace MiscServices
70 } // namespace OHOS
71 #endif