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 "constant.h"
19 #include "file_utils.h"
20 #include "update_log.h"
21 #include "updateservice_json_utils.h"
22
23 namespace OHOS {
24 namespace UpdateService {
25 uint64_t ScheduleConfig::pullupInterval_ = Startup::PULLUP_INTERVAL;
26 uint64_t ScheduleConfig::idleCheckInterval_ = Startup::IDLE_CHECK_INTERVAL;
27
InitConfig()28 void ScheduleConfig::InitConfig()
29 {
30 ENGINE_LOGI("InitConfig");
31
32 std::string dataString = FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH);
33 if (dataString.empty()) {
34 ENGINE_LOGI("file content is null");
35 return;
36 }
37 auto root = UpdateServiceJsonUtils::ParseJson(dataString);
38 if (root == nullptr) {
39 ENGINE_LOGI("InitConfig load fail");
40 return;
41 }
42 pullupInterval_ = ParseConfig(root.get(), Startup::PULLUP_INTERVAL_CONFIG, Startup::PULLUP_INTERVAL);
43 idleCheckInterval_ = ParseConfig(root.get(), Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL);
44 ENGINE_LOGI("InitConfig pullupInterval: %{public}s, idleCheckInterval: %{public}s",
45 std::to_string(pullupInterval_).c_str(), std::to_string(idleCheckInterval_).c_str());
46 }
47
GetPullupInterval()48 uint64_t ScheduleConfig::GetPullupInterval()
49 {
50 return pullupInterval_;
51 }
52
GetIdleCheckInterval()53 uint64_t ScheduleConfig::GetIdleCheckInterval()
54 {
55 return idleCheckInterval_;
56 }
57
ParseConfig(cJSON * root,const std::string & key,uint64_t defaultValue)58 uint64_t ScheduleConfig::ParseConfig(cJSON *root, const std::string &key, uint64_t defaultValue)
59 {
60 uint64_t value = 0;
61 int32_t ret = UpdateServiceJsonUtils::GetValueAndSetTo(root, key, value);
62 if ((ret == CAST_INT(JsonParseError::ERR_OK)) && (value > 0)) {
63 return value;
64 }
65 return defaultValue;
66 }
67 } // namespace UpdateService
68 } // namespace OHOS