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 void SetSystemTime(); 37 void RefreshNetworkTimeByTimer(uint64_t timerId); 38 void UpdateNITZSetTime(); 39 void Stop(); 40 void Init(); 41 int32_t MonitorNetwork(); 42 bool IsValidNITZTime(); 43 uint64_t GetNITZUpdateTime(); 44 45 private: 46 NtpUpdateTime(); 47 static bool GetNtpTimeInner(); 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