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