1 /*
2 * Copyright (c) 2021-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 "net_quota_policy.h"
17
18 #include <ctime>
19
20 #include "parcel.h"
21
22 #include "net_mgr_log_wrapper.h"
23 #include "netmanager_base_common_utils.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 static constexpr uint32_t MAX_POLICY_SIZE = 100;
28
Marshalling(Parcel & parcel) const29 bool NetQuotaPolicy::Marshalling(Parcel &parcel) const
30 {
31 if (!parcel.WriteInt32(networkmatchrule.netType)) {
32 return false;
33 }
34 if (!parcel.WriteString(networkmatchrule.simId)) {
35 return false;
36 }
37 if (!parcel.WriteInt64(quotapolicy.periodStartTime)) {
38 return false;
39 }
40 if (!parcel.WriteString(quotapolicy.periodDuration)) {
41 return false;
42 }
43 if (!parcel.WriteInt64(quotapolicy.warningBytes)) {
44 return false;
45 }
46 if (!parcel.WriteInt64(quotapolicy.limitBytes)) {
47 return false;
48 }
49 if (!parcel.WriteInt64(quotapolicy.lastLimitRemind)) {
50 return false;
51 }
52 if (!parcel.WriteBool(quotapolicy.metered)) {
53 return false;
54 }
55 if (!parcel.WriteInt32(quotapolicy.source)) {
56 return false;
57 }
58 if (!parcel.WriteInt32(quotapolicy.limitAction)) {
59 return false;
60 }
61 if (!parcel.WriteString(networkmatchrule.ident)) {
62 return false;
63 }
64
65 return true;
66 }
67
Marshalling(Parcel & parcel,const NetQuotaPolicy & quotaPolicy)68 bool NetQuotaPolicy::Marshalling(Parcel &parcel, const NetQuotaPolicy "aPolicy)
69 {
70 quotaPolicy.Marshalling(parcel);
71 return true;
72 }
73
Marshalling(Parcel & parcel,const std::vector<NetQuotaPolicy> & quotaPolicies)74 bool NetQuotaPolicy::Marshalling(Parcel &parcel, const std::vector<NetQuotaPolicy> "aPolicies)
75 {
76 uint32_t vsize = static_cast<uint32_t>(quotaPolicies.size());
77 if (!parcel.WriteUint32(vsize)) {
78 return false;
79 }
80
81 for (uint32_t i = 0; i < vsize; ++i) {
82 quotaPolicies[i].Marshalling(parcel);
83 }
84
85 return true;
86 }
87
Unmarshalling(Parcel & parcel,NetQuotaPolicy & quotaPolicy)88 bool NetQuotaPolicy::Unmarshalling(Parcel &parcel, NetQuotaPolicy "aPolicy)
89 {
90 if (!parcel.ReadInt32(quotaPolicy.networkmatchrule.netType)) {
91 return false;
92 }
93 if (!parcel.ReadString(quotaPolicy.networkmatchrule.simId)) {
94 return false;
95 }
96 if (!parcel.ReadInt64(quotaPolicy.quotapolicy.periodStartTime)) {
97 return false;
98 }
99 if (!parcel.ReadString(quotaPolicy.quotapolicy.periodDuration)) {
100 return false;
101 }
102 if (!parcel.ReadInt64(quotaPolicy.quotapolicy.warningBytes)) {
103 return false;
104 }
105 if (!parcel.ReadInt64(quotaPolicy.quotapolicy.limitBytes)) {
106 return false;
107 }
108 if (!parcel.ReadInt64(quotaPolicy.quotapolicy.lastLimitRemind)) {
109 return false;
110 }
111 if (!parcel.ReadBool(quotaPolicy.quotapolicy.metered)) {
112 return false;
113 }
114 if (!parcel.ReadInt32(quotaPolicy.quotapolicy.source)) {
115 return false;
116 }
117 if (!parcel.ReadInt32(quotaPolicy.quotapolicy.limitAction)) {
118 return false;
119 }
120 if (!parcel.ReadString(quotaPolicy.networkmatchrule.ident)) {
121 return false;
122 }
123
124 return true;
125 }
126
Unmarshalling(Parcel & parcel,std::vector<NetQuotaPolicy> & quotaPolicies)127 bool NetQuotaPolicy::Unmarshalling(Parcel &parcel, std::vector<NetQuotaPolicy> "aPolicies)
128 {
129 uint32_t vSize = 0;
130 if (!parcel.ReadUint32(vSize)) {
131 return false;
132 }
133 vSize = vSize > MAX_POLICY_SIZE ? MAX_POLICY_SIZE : vSize;
134
135 NetQuotaPolicy quotaPolicyTmp;
136 for (uint32_t i = 0; i < vSize; i++) {
137 if (!parcel.ReadInt32(quotaPolicyTmp.networkmatchrule.netType)) {
138 return false;
139 }
140 if (!parcel.ReadString(quotaPolicyTmp.networkmatchrule.simId)) {
141 return false;
142 }
143 if (!parcel.ReadInt64(quotaPolicyTmp.quotapolicy.periodStartTime)) {
144 return false;
145 }
146 if (!parcel.ReadString(quotaPolicyTmp.quotapolicy.periodDuration)) {
147 return false;
148 }
149 if (!parcel.ReadInt64(quotaPolicyTmp.quotapolicy.warningBytes)) {
150 return false;
151 }
152 if (!parcel.ReadInt64(quotaPolicyTmp.quotapolicy.limitBytes)) {
153 return false;
154 }
155 if (!parcel.ReadInt64(quotaPolicyTmp.quotapolicy.lastLimitRemind)) {
156 return false;
157 }
158 if (!parcel.ReadBool(quotaPolicyTmp.quotapolicy.metered)) {
159 return false;
160 }
161 if (!parcel.ReadInt32(quotaPolicyTmp.quotapolicy.source)) {
162 return false;
163 }
164 if (!parcel.ReadInt32(quotaPolicyTmp.quotapolicy.limitAction)) {
165 return false;
166 }
167 if (!parcel.ReadString(quotaPolicyTmp.networkmatchrule.ident)) {
168 return false;
169 }
170 quotaPolicies.push_back(quotaPolicyTmp);
171 }
172
173 return true;
174 }
175
IsOverWarning(int64_t totalQuota) const176 bool NetQuotaPolicy::IsOverWarning(int64_t totalQuota) const
177 {
178 return totalQuota > quotapolicy.warningBytes;
179 }
180
IsOverLimit(int64_t totalQuota) const181 bool NetQuotaPolicy::IsOverLimit(int64_t totalQuota) const
182 {
183 return totalQuota > quotapolicy.limitBytes;
184 }
185
GetPeriodStart()186 int64_t NetQuotaPolicy::GetPeriodStart()
187 {
188 if (quotapolicy.periodDuration.size() < PERIOD_DURATION_SIZE) {
189 quotapolicy.periodDuration = PERIOD_MONTH;
190 }
191 time_t timeNow;
192 time(&timeNow);
193 struct tm tm;
194 localtime_r(&timeNow, &tm);
195 std::string cycle = quotapolicy.periodDuration.substr(0, 1);
196 int32_t start = CommonUtils::StrToInt(quotapolicy.periodDuration.substr(1, quotapolicy.periodDuration.size()));
197
198 if (cycle == PERIOD_DAY) {
199 tm.tm_hour = start;
200 tm.tm_min = 0;
201 tm.tm_sec = 0;
202 } else if (cycle == PERIOD_YEAR) {
203 tm.tm_hour = 0;
204 tm.tm_min = 0;
205 tm.tm_sec = 0;
206 tm.tm_yday = start - 1;
207 } else {
208 tm.tm_hour = 0;
209 tm.tm_min = 0;
210 tm.tm_sec = 0;
211 tm.tm_mday = start;
212 }
213 time_t start_time = mktime(&tm);
214 return start_time;
215 }
216
Reset()217 void NetQuotaPolicy::Reset()
218 {
219 quotapolicy.periodDuration = PERIOD_MONTH + std::to_string(PERIOD_START);
220 quotapolicy.warningBytes = DATA_USAGE_UNKNOWN;
221 quotapolicy.limitBytes = DATA_USAGE_UNKNOWN;
222 quotapolicy.lastWarningRemind = REMIND_NEVER;
223 quotapolicy.lastLimitRemind = REMIND_NEVER;
224 quotapolicy.metered = false;
225 quotapolicy.limitAction = LimitAction::LIMIT_ACTION_NONE;
226 }
227 } // namespace NetManagerStandard
228 } // namespace OHOS
229