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