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 30 class NtpUpdateTime { 31 public: 32 static NtpUpdateTime &GetInstance(); 33 static bool GetNtpTime(int64_t &time); 34 static bool GetRealTime(int64_t &time); 35 static void SetSystemTime(); 36 static bool IsInUpdateInterval(); 37 void RefreshNetworkTimeByTimer(uint64_t timerId); 38 void UpdateNITZSetTime(); 39 void Stop(); 40 void Init(); 41 bool IsValidNITZTime(); 42 uint64_t GetNITZUpdateTime(); 43 44 private: 45 NtpUpdateTime(); 46 static bool GetNtpTimeInner(); 47 static bool GetRealTimeInner(int64_t &time); 48 static void ChangeNtpServerCallback(const char *key, const char *value, void *context); 49 static std::vector<std::string> SplitNtpAddrs(const std::string &ntpStr); 50 void StartTimer(); 51 void RefreshNextTriggerTime(); 52 bool CheckStatus(); 53 void RegisterSystemParameterListener(); 54 static void ChangeAutoTimeCallback(const char *key, const char *value, void *context); 55 56 static AutoTimeInfo autoTimeInfo_; 57 uint64_t timerId_; 58 uint64_t nitzUpdateTimeMilli_; 59 uint64_t nextTriggerTime_; 60 static std::mutex requestMutex_; 61 int64_t lastNITZUpdateTime_; 62 }; 63 } // namespace MiscServices 64 } // namespace OHOS 65 #endif