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 #include "update_service_cache.h"
17 
18 #include <cstdint>
19 
20 #include "update_define.h"
21 #include "update_log.h"
22 #include "upgrade_info.h"
23 #include "upgrade_interval.h"
24 
25 namespace OHOS {
26 namespace UpdateEngine {
27 UpgradeInfo UpdateServiceCache::upgradeInfo_{};
28 uint64_t UpdateServiceCache::checkInterval_ = 0;
29 uint64_t UpdateServiceCache::downloadInterval_ = 0;
30 UpgradeInterval UpdateServiceCache::upgradeInterval_{};
31 
32 UpgradeInfo UpdateServiceCache::paramUpgradeInfo_{};
33 uint64_t UpdateServiceCache::paramCheckInterval_ = 0;
34 uint64_t UpdateServiceCache::paramDownloadInterval_ = 0;
35 UpgradeInterval UpdateServiceCache::paramUpgradeInterval_{};
36 
IsTypelegal(BusinessSubType businessSubType)37 bool UpdateServiceCache::IsTypelegal(BusinessSubType businessSubType)
38 {
39     bool isLegal = businessSubType == BusinessSubType::FIRMWARE || businessSubType == BusinessSubType::PARAM;
40     if (!isLegal) {
41         ENGINE_LOGI("IsTypelegal type %{public}d", CAST_INT(businessSubType));
42     }
43     return isLegal;
44 }
45 
IsParamType(BusinessSubType businessSubType)46 bool UpdateServiceCache::IsParamType(BusinessSubType businessSubType)
47 {
48     return businessSubType == BusinessSubType::PARAM;
49 }
50 
GetUpgradeInfo(BusinessSubType businessSubType)51 UpgradeInfo UpdateServiceCache::GetUpgradeInfo(BusinessSubType businessSubType)
52 {
53     if (!IsTypelegal(businessSubType)) {
54         UpgradeInfo upgradeInfo;
55         return upgradeInfo;
56     }
57 
58     return IsParamType(businessSubType) ? paramUpgradeInfo_ : upgradeInfo_;
59 }
60 
SetUpgradeInfo(const UpgradeInfo & upgradeInfo)61 void UpdateServiceCache::SetUpgradeInfo(const UpgradeInfo &upgradeInfo)
62 {
63     if (!IsTypelegal(upgradeInfo.businessType.subType)) {
64         return;
65     }
66 
67     if (IsParamType(upgradeInfo.businessType.subType)) {
68         paramUpgradeInfo_ = upgradeInfo;
69     } else {
70         upgradeInfo_ = upgradeInfo;
71     }
72 }
73 
GetCheckInterval(BusinessSubType businessSubType)74 uint64_t UpdateServiceCache::GetCheckInterval(BusinessSubType businessSubType)
75 {
76     if (!IsTypelegal(businessSubType)) {
77         return 0;
78     }
79 
80     return IsParamType(businessSubType) ? paramCheckInterval_ : checkInterval_;
81 }
82 
SetCheckInterval(BusinessSubType businessSubType,uint64_t interval)83 void UpdateServiceCache::SetCheckInterval(BusinessSubType businessSubType, uint64_t interval)
84 {
85     if (!IsTypelegal(businessSubType)) {
86         return;
87     }
88 
89     if (IsParamType(businessSubType)) {
90         paramCheckInterval_ = interval;
91     } else {
92         checkInterval_ = interval;
93     }
94 }
95 
GetDownloadInterval(BusinessSubType businessSubType)96 uint64_t UpdateServiceCache::GetDownloadInterval(BusinessSubType businessSubType)
97 {
98     if (!IsTypelegal(businessSubType)) {
99         return 0;
100     }
101 
102     return IsParamType(businessSubType) ? paramDownloadInterval_ : downloadInterval_;
103 }
104 
SetDownloadInterval(BusinessSubType businessSubType,uint64_t interval)105 void UpdateServiceCache::SetDownloadInterval(BusinessSubType businessSubType, uint64_t interval)
106 {
107     if (!IsTypelegal(businessSubType)) {
108         return;
109     }
110 
111     if (IsParamType(businessSubType)) {
112         paramDownloadInterval_ = interval;
113     } else {
114         downloadInterval_ = interval;
115     }
116 }
117 
GetUpgradeInterval(BusinessSubType businessSubType)118 UpgradeInterval UpdateServiceCache::GetUpgradeInterval(BusinessSubType businessSubType)
119 {
120     if (!IsTypelegal(businessSubType)) {
121         UpgradeInterval upgradeInterval;
122         return upgradeInterval;
123     }
124 
125     return IsParamType(businessSubType) ? paramUpgradeInterval_ : upgradeInterval_;
126 }
127 
SetUpgradeStartTime(BusinessSubType businessSubType,uint64_t time)128 void UpdateServiceCache::SetUpgradeStartTime(BusinessSubType businessSubType, uint64_t time)
129 {
130     if (!IsTypelegal(businessSubType)) {
131         return;
132     }
133 
134     if (IsParamType(businessSubType)) {
135         paramUpgradeInterval_.timeStart = time;
136     } else {
137         upgradeInterval_.timeStart = time;
138     }
139 }
140 
SetUpgradeEndTime(BusinessSubType businessSubType,uint64_t time)141 void UpdateServiceCache::SetUpgradeEndTime(BusinessSubType businessSubType, uint64_t time)
142 {
143     if (!IsTypelegal(businessSubType)) {
144         return;
145     }
146 
147     if (IsParamType(businessSubType)) {
148         paramUpgradeInterval_.timeEnd = time;
149     } else {
150         upgradeInterval_.timeEnd = time;
151     }
152 }
153 } // namespace UpdateEngine
154 } // namespace OHOS
155