1 /*
2  * Copyright (c) 2023 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 #include "schedule_config.h"
17 
18 #include "file_utils.h"
19 #include "update_log.h"
20 #include "constant.h"
21 
22 namespace OHOS {
23 namespace UpdateEngine {
24 uint64_t ScheduleConfig::pullupInterval_ = Startup::PULLUP_INTERVAL;
25 uint64_t ScheduleConfig::idleCheckInterval_ = Startup::IDLE_CHECK_INTERVAL;
26 
InitConfig()27 void ScheduleConfig::InitConfig()
28 {
29     ENGINE_LOGI("InitConfig");
30     nlohmann::json root =
31         nlohmann::json::parse(FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH), nullptr, false);
32     if (root.is_discarded()) {
33         ENGINE_LOGI("InitConfig load fail");
34         return;
35     }
36     pullupInterval_ = ParseConfig(root, Startup::PULLUP_INTERVAL_CONFIG, Startup::PULLUP_INTERVAL);
37     idleCheckInterval_ = ParseConfig(root, Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL);
38     ENGINE_LOGI("InitConfig pullupInterval: %{public}s, idleCheckInterval: %{public}s",
39         std::to_string(pullupInterval_).c_str(), std::to_string(idleCheckInterval_).c_str());
40 }
41 
GetPullupInterval()42 uint64_t ScheduleConfig::GetPullupInterval()
43 {
44     return pullupInterval_;
45 }
46 
GetIdleCheckInterval()47 uint64_t ScheduleConfig::GetIdleCheckInterval()
48 {
49     return idleCheckInterval_;
50 }
51 
ParseConfig(nlohmann::json & root,const std::string & key,uint64_t defaultValue)52 uint64_t ScheduleConfig::ParseConfig(nlohmann::json &root, const std::string &key, uint64_t defaultValue)
53 {
54     uint64_t value = 0;
55     int32_t ret = JsonUtils::GetValueAndSetTo(root, key, value);
56     if ((ret == CAST_INT(JsonParseError::ERR_OK)) && (value > 0)) {
57         return value;
58     }
59     return defaultValue;
60 }
61 } // namespace UpdateEngine
62 } // namespace OHOS