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