• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "net_policy_traffic.h"
17 
18 #include "broadcast_manager.h"
19 #include "common_event_data.h"
20 #include "common_event_manager.h"
21 #include "common_event_publish_info.h"
22 #include "common_event_support.h"
23 #include "system_ability_definition.h"
24 
25 #include "net_manager_center.h"
26 #include "net_mgr_log_wrapper.h"
27 #include "net_policy_constants.h"
28 #include "net_policy_file.h"
29 #include "net_policy_inner_define.h"
30 #include "net_quota_policy.h"
31 #include "net_specifier.h"
32 #include "net_stats_info.h"
33 #include "netmanager_base_common_utils.h"
34 #include "netmanager_base_permission.h"
35 
36 namespace OHOS {
37 namespace NetManagerStandard {
38 namespace {
39 constexpr const char *BROADCAST_QUOTA_WARNING = "Net Policy Quota Warning";
40 constexpr const char *BROADCAST_QUOTA_LIMIT_REMIND = "Net Policy Quota Limit Remind";
41 constexpr const char *BROADCAST_QUOTA_LIMIT = "Net Policy Quota Limit";
42 } // namespace
43 
Init()44 void NetPolicyTraffic::Init()
45 {
46     netsysCallback_ = new (std::nothrow)
47         NetsysControllerCallbackImpl((std::static_pointer_cast<NetPolicyTraffic>(shared_from_this())));
48     if (netsysCallback_ != nullptr) {
49         GetNetsysInst()->RegisterNetsysCallback(netsysCallback_);
50     }
51 
52     netConnCallback_ =
53         new (std::nothrow) ConnCallBack((std::static_pointer_cast<NetPolicyTraffic>(shared_from_this())));
54     if (netConnCallback_ != nullptr) {
55         GetNetCenterInst().RegisterNetConnCallback(netConnCallback_);
56     }
57     ReadQuotaPolicies();
58 }
59 
IsValidQuotaPolicy(const NetQuotaPolicy & quotaPolicy)60 bool NetPolicyTraffic::IsValidQuotaPolicy(const NetQuotaPolicy &quotaPolicy)
61 {
62     int32_t netType = quotaPolicy.networkmatchrule.netType;
63     if (!IsValidNetType(netType)) {
64         NETMGR_LOG_E("NetPolicyType is invalid policy[%{public}d]", netType);
65         return false;
66     }
67 
68     if (!IsValidPeriodDuration(quotaPolicy.quotapolicy.periodDuration)) {
69         NETMGR_LOG_E("periodDuration [%{public}s] must Mx", quotaPolicy.quotapolicy.periodDuration.c_str());
70         return false;
71     }
72     return true;
73 }
74 
IsValidNetType(int32_t netType)75 bool NetPolicyTraffic::IsValidNetType(int32_t netType)
76 {
77     switch (netType) {
78         case NetBearType::BEARER_CELLULAR:
79         case NetBearType::BEARER_WIFI:
80         case NetBearType::BEARER_BLUETOOTH:
81         case NetBearType::BEARER_ETHERNET:
82         case NetBearType::BEARER_VPN:
83         case NetBearType::BEARER_WIFI_AWARE: {
84             return true;
85         }
86         default: {
87             NETMGR_LOG_E("Invalid netType [%{public}d]", netType);
88             return false;
89         }
90     }
91 }
92 
IsValidNetRemindType(uint32_t remindType)93 bool NetPolicyTraffic::IsValidNetRemindType(uint32_t remindType)
94 {
95     switch (remindType) {
96         case RemindType::REMIND_TYPE_WARNING:
97         case RemindType::REMIND_TYPE_LIMIT: {
98             return true;
99         }
100         default: {
101             NETMGR_LOG_E("Invalid remindType [%{public}d]", remindType);
102             return false;
103         }
104     }
105 }
106 
UpdateQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)107 int32_t NetPolicyTraffic::UpdateQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
108 {
109     if (quotaPolicies.empty()) {
110         NETMGR_LOG_E("SetNetQuotaPolicies size is empty");
111         return POLICY_ERR_INVALID_QUOTA_POLICY;
112     }
113     // formalize the quota policy
114     NetmanagerHiTrace::NetmanagerStartSyncTrace("FormalizeQuotaPolicies quotaPolicies start");
115     FormalizeQuotaPolicies(quotaPolicies);
116     NetmanagerHiTrace::NetmanagerFinishSyncTrace("FormalizeQuotaPolicies quotaPolicies end");
117     return UpdateQuotaPoliciesInner();
118 }
119 
UpdateQuotaPoliciesInner()120 int32_t NetPolicyTraffic::UpdateQuotaPoliciesInner()
121 {
122     // calculate the quota remain and get the metered ifaces
123     NetmanagerHiTrace::NetmanagerStartSyncTrace("UpdateMeteredIfacesQuota start");
124     auto meteredIfaces = UpdateMeteredIfacesQuota();
125     NetmanagerHiTrace::NetmanagerFinishSyncTrace("UpdateMeteredIfacesQuota end");
126 
127     // update the metered ifaces and notify the changes.
128     NetmanagerHiTrace::NetmanagerStartSyncTrace("UpdateMeteredIfaces meteredIfaces start");
129     UpdateMeteredIfaces(meteredIfaces);
130     NetmanagerHiTrace::NetmanagerFinishSyncTrace("UpdateMeteredIfaces meteredIfaces end");
131 
132     // notify quota limit or warning.
133     NetmanagerHiTrace::NetmanagerStartSyncTrace("UpdateQuotaNotify start");
134     UpdateQuotaNotify();
135     NetmanagerHiTrace::NetmanagerFinishSyncTrace("UpdateQuotaNotify end");
136     // write quota policies to file.
137     if (!WriteQuotaPolicies()) {
138         NETMGR_LOG_E("UpdateQuotaPolicies WriteFile failed");
139         return NETMANAGER_ERR_WRITE_DATA_FAIL;
140     }
141     // notify the the quota policy change.
142     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
143     GetCbInst()->NotifyNetQuotaPolicyChangeAsync(quotaPolicies_);
144     NETMGR_LOG_I("End UpdateQuotaPoliciesInner.");
145     return NETMANAGER_SUCCESS;
146 }
147 
FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)148 void NetPolicyTraffic::FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
149 {
150     std::unique_lock<std::shared_mutex> lock(quotaMutex_);
151     quotaPolicies_.clear();
152     for (auto quotaPolicy : quotaPolicies) {
153         if (!IsValidQuotaPolicy(quotaPolicy)) {
154             NETMGR_LOG_E("UpdateQuotaPolicies invalid quota policy netType[%{public}d], periodDuration[%{public}s]",
155                          quotaPolicy.networkmatchrule.netType, quotaPolicy.quotapolicy.periodDuration.c_str());
156             continue;
157         }
158         if (quotaPolicy.quotapolicy.limitBytes == DATA_USAGE_UNKNOWN) {
159             quotaPolicy.quotapolicy.limitAction = LIMIT_ACTION_ALERT_ONLY;
160         } else if (quotaPolicy.quotapolicy.warningBytes == DATA_USAGE_UNKNOWN) {
161             quotaPolicy.quotapolicy.warningBytes =
162                 quotaPolicy.quotapolicy.limitBytes * NINETY_PERCENTAGE / HUNDRED_PERCENTAGE;
163         }
164         if (quotaPolicy.quotapolicy.limitAction == LIMIT_ACTION_ALERT_ONLY) {
165             quotaPolicy.quotapolicy.limitBytes = DATA_USAGE_UNLIMITED;
166         }
167         if (quotaPolicy.quotapolicy.warningBytes > quotaPolicy.quotapolicy.limitBytes) {
168             quotaPolicy.quotapolicy.warningBytes = DATA_USAGE_UNLIMITED;
169         }
170         if (quotaPolicy.quotapolicy.limitBytes == DATA_USAGE_UNLIMITED) {
171             quotaPolicy.quotapolicy.limitAction = LIMIT_ACTION_ALERT_ONLY;
172         }
173         quotaPolicies_.push_back(quotaPolicy);
174     }
175 }
176 
UpdateMeteredIfacesQuota()177 const std::vector<std::string> NetPolicyTraffic::UpdateMeteredIfacesQuota()
178 {
179     std::vector<std::string> newMeteredIfaces;
180     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
181     std::vector<NetQuotaPolicy> quotaTmp = quotaPolicies_;
182     lock.unlock();
183     for (auto &quotaPolicy : quotaTmp) {
184         std::string iface = GetMatchIfaces(quotaPolicy);
185         // set quota for metered iface.
186         if (iface == UNKNOW_IFACE || !quotaPolicy.quotapolicy.metered) {
187             continue;
188         }
189         newMeteredIfaces.push_back(iface);
190         int64_t quotaRemain = GetQuotaRemain(quotaPolicy);
191         if (quotaRemain >= 0) {
192             GetNetsysInst()->BandwidthSetIfaceQuota(iface, quotaRemain);
193         }
194     }
195     // remove the iface quota that not metered.
196     for (uint32_t i = 0; i < meteredIfaces_.size(); ++i) {
197         if (!std::count(newMeteredIfaces.begin(), newMeteredIfaces.end(), meteredIfaces_[i])) {
198             GetNetsysInst()->BandwidthRemoveIfaceQuota(meteredIfaces_[i]);
199         }
200     }
201     return newMeteredIfaces;
202 }
203 
UpdateMeteredIfaces(std::vector<std::string> & newMeteredIfaces)204 void NetPolicyTraffic::UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces)
205 {
206     NETMGR_LOG_D("UpdateMeteredIfaces size[%{public}zu]", newMeteredIfaces.size());
207     meteredIfaces_.clear();
208     meteredIfaces_.reserve(newMeteredIfaces.size());
209     for (auto &iface : newMeteredIfaces) {
210         meteredIfaces_.push_back(iface);
211     }
212     // notify the callback of metered ifaces changed.
213     GetCbInst()->NotifyNetMeteredIfacesChangeAsync(meteredIfaces_);
214 }
215 
UpdateQuotaNotify()216 void NetPolicyTraffic::UpdateQuotaNotify()
217 {
218     NetmanagerHiTrace::NetmanagerStartSyncTrace("Traverse cellular network start");
219     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
220     std::vector<NetQuotaPolicy> quotaPoliciesTmp = quotaPolicies_;
221     lock.unlock();
222     for (auto &quotaPolicy : quotaPoliciesTmp) {
223         NetmanagerHiTrace::NetmanagerStartSyncTrace("Get the start time of the metering cycle start");
224         int64_t start = quotaPolicy.GetPeriodStart();
225         NetmanagerHiTrace::NetmanagerFinishSyncTrace("Get the start time of the metering cycle end");
226 
227         NetmanagerHiTrace::NetmanagerStartSyncTrace("Get the usage of traffic start");
228         int64_t totalQuota = GetTotalQuota(quotaPolicy);
229         NetmanagerHiTrace::NetmanagerFinishSyncTrace("Get the usage of traffic end");
230         // check if the quota is over the limit
231         if (quotaPolicy.IsOverLimit(totalQuota)) {
232             if (quotaPolicy.quotapolicy.lastLimitRemind > start) {
233                 // notify the quota reach limit and has reminded before.
234                 NetmanagerHiTrace::NetmanagerStartSyncTrace("Notify quota limit reminded start");
235                 NotifyQuotaLimitReminded(totalQuota);
236                 NetmanagerHiTrace::NetmanagerFinishSyncTrace("Notify quota limit reminded end");
237                 continue;
238             }
239             NetmanagerHiTrace::NetmanagerStartSyncTrace("Update net enable status start");
240             UpdateNetEnableStatus(quotaPolicy);
241             NetmanagerHiTrace::NetmanagerFinishSyncTrace("Update net enable status end");
242             // notify the quota reach limit
243             NotifyQuotaLimit(totalQuota);
244             continue;
245         }
246         // check if the quota is over the warning
247         if (quotaPolicy.IsOverWarning(totalQuota) && quotaPolicy.quotapolicy.lastWarningRemind < start) {
248             NetmanagerHiTrace::NetmanagerStartSyncTrace("Notify quota warning remind start");
249             NotifyQuotaWarning(totalQuota);
250             NetmanagerHiTrace::NetmanagerFinishSyncTrace("Notify quota warning remind end");
251         }
252     }
253     NetmanagerHiTrace::NetmanagerFinishSyncTrace("Traverse cellular network end");
254 }
255 
GetQuotaRemain(NetQuotaPolicy & quotaPolicy)256 int64_t NetPolicyTraffic::GetQuotaRemain(NetQuotaPolicy &quotaPolicy)
257 {
258     int64_t start = quotaPolicy.GetPeriodStart();
259     int64_t totalQuota = GetTotalQuota(quotaPolicy);
260     NETMGR_LOG_D("GetQuotaRemain totalQuota[%{public}s] limit[%{public}s] start[%{public}s]",
261                  std::to_string(totalQuota).c_str(), std::to_string(quotaPolicy.quotapolicy.limitBytes).c_str(),
262                  ctime(&start));
263     // calculate the quota for each policy.
264     bool hasLimit = quotaPolicy.quotapolicy.limitBytes != DATA_USAGE_UNKNOWN;
265     int64_t quota = LONG_MAX;
266     if (hasLimit || quotaPolicy.quotapolicy.metered) {
267         if (hasLimit && quotaPolicy.quotapolicy.periodDuration != QUOTA_POLICY_NO_PERIOD) {
268             if (quotaPolicy.quotapolicy.lastLimitRemind >= start) {
269                 return LONG_MAX;
270             }
271             quota = quotaPolicy.quotapolicy.limitBytes - totalQuota;
272         }
273     }
274     return quota < 0 ? 0 : quota;
275 }
276 
UpdateNetEnableStatus(const NetQuotaPolicy & quotaPolicy)277 void NetPolicyTraffic::UpdateNetEnableStatus(const NetQuotaPolicy &quotaPolicy)
278 {
279     NETMGR_LOG_D("UpdateNetEnableStatus metered[%{public}d] quotapolicy.limitAction[%{public}d]",
280                  quotaPolicy.quotapolicy.metered, quotaPolicy.quotapolicy.limitAction);
281     if (quotaPolicy.quotapolicy.metered || quotaPolicy.quotapolicy.limitAction == LIMIT_ACTION_ACCESS_DISABLED) {
282         SetNetworkEnableStatus(quotaPolicy, false);
283     }
284 }
285 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)286 int32_t NetPolicyTraffic::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
287 {
288     std::unique_lock<std::shared_mutex> lock(quotaMutex_);
289     quotaPolicies.clear();
290     quotaPolicies = quotaPolicies_;
291     NETMGR_LOG_D("GetNetQuotaPolicies quotaPolicies end size[%{public}zu]", quotaPolicies.size());
292     return NETMANAGER_SUCCESS;
293 }
294 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)295 int32_t NetPolicyTraffic::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
296 {
297     if (!IsValidNetType(netType)) {
298         NETMGR_LOG_E("NetPolicyType is invalid policy[%{public}d]", netType);
299         return NETMANAGER_ERR_PARAMETER_ERROR;
300     }
301 
302     if (!IsValidNetRemindType(remindType)) {
303         return NETMANAGER_ERR_PARAMETER_ERROR;
304     }
305     std::unique_lock<std::shared_mutex> lock(quotaMutex_);
306     for (uint32_t i = 0; i < quotaPolicies_.size(); ++i) {
307         NetQuotaPolicy &quotaPolicy = quotaPolicies_[i];
308         int32_t netTypeTemp = quotaPolicy.networkmatchrule.netType;
309         std::string iccidTemp = quotaPolicy.networkmatchrule.simId;
310         if (netTypeTemp == netType && iccidTemp == simId) {
311             switch (remindType) {
312                 case REMIND_TYPE_WARNING:
313                     quotaPolicy.quotapolicy.lastWarningRemind = time(nullptr);
314                     break;
315                 case REMIND_TYPE_LIMIT:
316                     quotaPolicy.quotapolicy.lastLimitRemind = time(nullptr);
317                     break;
318                 default:
319                     return NETMANAGER_ERR_PARAMETER_ERROR;
320             }
321         }
322     }
323     lock.unlock();
324     UpdateQuotaPoliciesInner();
325     NETMGR_LOG_I("NetPolicyTraffic::UpdateRemindPolicy end.");
326     return NETMANAGER_SUCCESS;
327 }
328 
GetMeteredIfaces()329 const std::vector<std::string> &NetPolicyTraffic::GetMeteredIfaces()
330 {
331     return meteredIfaces_;
332 }
333 
ResetPolicies(const std::string & simId)334 int32_t NetPolicyTraffic::ResetPolicies(const std::string &simId)
335 {
336     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
337     for (auto &quotaPolicy : quotaPolicies_) {
338         if (quotaPolicy.networkmatchrule.simId == simId) {
339             quotaPolicy.Reset();
340         }
341     }
342     lock.unlock();
343     return UpdateQuotaPoliciesInner();
344 }
345 
ResetPolicies()346 int32_t NetPolicyTraffic::ResetPolicies()
347 {
348     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
349     for (auto &quotaPolicy : quotaPolicies_) {
350         NETMGR_LOG_I("NetPolicyTraffic::ResetPolicies [%{public}s.", quotaPolicy.networkmatchrule.simId.c_str());
351         quotaPolicy.Reset();
352     }
353     lock.unlock();
354     return UpdateQuotaPoliciesInner();
355 }
356 
ReachedLimit(const std::string & iface)357 void NetPolicyTraffic::ReachedLimit(const std::string &iface)
358 {
359     NETMGR_LOG_D("ReachedLimit iface:%{public}s.", iface.c_str());
360     auto &ifaces = GetMeteredIfaces();
361     if (std::find(ifaces.begin(), ifaces.end(), iface) != ifaces.end()) {
362         UpdateQuotaPoliciesInner();
363     }
364 }
365 
UpdateNetPolicy()366 void NetPolicyTraffic::UpdateNetPolicy()
367 {
368     UpdateQuotaPoliciesInner();
369 }
370 
GetTotalQuota(NetQuotaPolicy & quotaPolicy)371 int64_t NetPolicyTraffic::GetTotalQuota(NetQuotaPolicy &quotaPolicy)
372 {
373     std::string iface = GetMatchIfaces(quotaPolicy);
374     NetStatsInfo info;
375     int64_t start = quotaPolicy.GetPeriodStart();
376     int64_t end = static_cast<int64_t>(time(nullptr));
377     if (end < 0) {
378         return 0;
379     }
380     GetNetCenterInst().GetIfaceStatsDetail(iface, start, end, info);
381     int64_t quota = static_cast<int64_t>(info.rxBytes_ + info.txBytes_);
382 
383     return quota < 0 ? 0 : quota;
384 }
385 
ReadQuotaPolicies()386 void NetPolicyTraffic::ReadQuotaPolicies()
387 {
388     std::unique_lock<std::shared_mutex> lock(quotaMutex_);
389     GetFileInst()->ReadQuotaPolicies(quotaPolicies_);
390     lock.unlock();
391     UpdateQuotaPoliciesInner();
392 }
393 
WriteQuotaPolicies()394 bool NetPolicyTraffic::WriteQuotaPolicies()
395 {
396     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
397     return GetFileInst()->WriteQuotaPolicies(quotaPolicies_);
398 }
399 
GetMatchIfaces(const NetQuotaPolicy & quotaPolicy)400 const std::string NetPolicyTraffic::GetMatchIfaces(const NetQuotaPolicy &quotaPolicy)
401 {
402     std::string ident = "";
403     if (quotaPolicy.networkmatchrule.netType == BEARER_CELLULAR) {
404         ident = IDENT_PREFIX_CELLULAR + quotaPolicy.networkmatchrule.simId;
405     } else if (quotaPolicy.networkmatchrule.netType == BEARER_WIFI) {
406         ident = quotaPolicy.networkmatchrule.ident;
407     } else if (quotaPolicy.networkmatchrule.netType == BEARER_ETHERNET) {
408         ident = quotaPolicy.networkmatchrule.ident;
409     }
410     std::string iface;
411     if (quotaPolicy.networkmatchrule.netType >= BEARER_DEFAULT) {
412         return iface;
413     }
414     GetNetCenterInst().GetIfaceNameByType(static_cast<NetBearType>(quotaPolicy.networkmatchrule.netType), ident, iface);
415     NETMGR_LOG_D("GetMatchIfaces netType: %{public}d ident: %{public}s iface: %{public}s.",
416                  quotaPolicy.networkmatchrule.netType, ident.c_str(), iface.c_str());
417     return iface;
418 }
419 
SetNetworkEnableStatus(const NetQuotaPolicy & quotaPolicy,bool enable)420 void NetPolicyTraffic::SetNetworkEnableStatus(const NetQuotaPolicy &quotaPolicy, bool enable)
421 {
422     NETMGR_LOG_D("SetNetworkEnableStatus enable: %{public}d ", enable);
423 }
424 
NotifyQuotaWarning(int64_t totalQuota)425 void NetPolicyTraffic::NotifyQuotaWarning(int64_t totalQuota)
426 {
427     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_WARNING, BROADCAST_QUOTA_WARNING, totalQuota);
428 }
429 
NotifyQuotaLimitReminded(int64_t totalQuota)430 void NetPolicyTraffic::NotifyQuotaLimitReminded(int64_t totalQuota)
431 {
432     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_LIMIT_REMINDED, BROADCAST_QUOTA_LIMIT_REMIND, totalQuota);
433 }
434 
NotifyQuotaLimit(int64_t totalQuota)435 void NetPolicyTraffic::NotifyQuotaLimit(int64_t totalQuota)
436 {
437     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_LIMIT, BROADCAST_QUOTA_LIMIT, totalQuota);
438 }
439 
PublishQuotaEvent(const std::string & action,const std::string & describe,int64_t quota)440 void NetPolicyTraffic::PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota)
441 {
442     BroadcastInfo info;
443     info.action = action;
444     info.data = describe;
445     info.permission = Permission::CONNECTIVITY_INTERNAL;
446     std::map<std::string, int64_t> param = {{"totalQuota", quota}};
447     BroadcastManager::GetInstance().SendBroadcast(info, param);
448 }
449 
IsValidPeriodDuration(const std::string & periodDuration)450 bool NetPolicyTraffic::IsValidPeriodDuration(const std::string &periodDuration)
451 {
452     if (periodDuration.empty() || periodDuration.size() < PERIOD_DURATION_SIZE) {
453         NETMGR_LOG_E("periodDuration is illegal");
454         return false;
455     }
456 
457     std::string cycle = periodDuration.substr(0, 1);
458     NETMGR_LOG_D("PeriodDuration [%{public}s].", periodDuration.c_str());
459     int32_t start = CommonUtils::StrToInt(periodDuration.substr(1, periodDuration.size()));
460 
461     if (cycle == PERIOD_DAY) {
462         if (start >= PERIOD_START && start <= DAY_MAX) {
463             return true;
464         }
465     }
466 
467     if (cycle == PERIOD_MONTH) {
468         if (start >= PERIOD_START && start <= MONTH_MAX) {
469             return true;
470         }
471     }
472 
473     if (cycle == PERIOD_YEAR) {
474         if (start >= PERIOD_START && start <= YEAR_MAX) {
475             return true;
476         }
477     }
478     NETMGR_LOG_E("Invalid periodDuration start [%{public}d],Invalid periodDuration cycle [%{public}s]", start,
479                  cycle.c_str());
480     return false;
481 }
482 
IsQuotaPolicyExist(int32_t netType,const std::string & simId)483 bool NetPolicyTraffic::IsQuotaPolicyExist(int32_t netType, const std::string &simId)
484 {
485     std::vector<NetQuotaPolicy> quotaPolicies;
486     GetFileInst()->ReadQuotaPolicies(quotaPolicies);
487 
488     if (quotaPolicies.empty()) {
489         NETMGR_LOG_E("quotaPolicies is empty");
490         return false;
491     }
492 
493     for (uint32_t i = 0; i < quotaPolicies.size(); i++) {
494         if (netType == quotaPolicies[i].networkmatchrule.netType && simId == quotaPolicies[i].networkmatchrule.simId) {
495             return true;
496         }
497     }
498 
499     return false;
500 }
501 
HandleEvent(int32_t eventId,const std::shared_ptr<PolicyEvent> & policyEvent)502 void NetPolicyTraffic::HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent)
503 {
504     NETMGR_LOG_D("NetPolicyTraffic HandleEvent");
505 }
506 
GetDumpMessage(std::string & message)507 void NetPolicyTraffic::GetDumpMessage(std::string &message)
508 {
509     static const std::string TAB = "    ";
510     message.append(TAB + "MeteredIfaces: {");
511     std::for_each(meteredIfaces_.begin(), meteredIfaces_.end(),
512                   [&message](const std::string &item) { message.append(item + ", "); });
513     message.append("}\n");
514     message.append(TAB + "QuotaPolicies:\n");
515     std::shared_lock<std::shared_mutex> lock(quotaMutex_);
516     std::vector<NetQuotaPolicy> quotaPoliciesTmp = quotaPolicies_;
517     lock.unlock();
518     std::for_each(quotaPoliciesTmp.begin(), quotaPoliciesTmp.end(), [&message](const auto &item) {
519         message.append(TAB + TAB + "NetType: " + std::to_string(item.networkmatchrule.netType) + "\n" + TAB + TAB +
520                        "simId: " + item.networkmatchrule.simId + "\n" + TAB + TAB +
521                        "Ident: " + item.networkmatchrule.ident + "\n");
522         message.append(TAB + TAB + "PeriodStartTime: " + std::to_string(item.quotapolicy.periodStartTime) + "\n");
523         message.append(TAB + TAB + "PeriodDuration: " + item.quotapolicy.periodDuration + "\n");
524         message.append(TAB + TAB + "Title: " + item.quotapolicy.title + "\n" + TAB + TAB +
525                        "Summary: " + item.quotapolicy.summary + "\n");
526         message.append(TAB + TAB + "quotapolicy.warningBytes: " + std::to_string(item.quotapolicy.warningBytes) + "\n");
527         message.append(TAB + TAB + "quotapolicy.limitBytes: " + std::to_string(item.quotapolicy.limitBytes) + "\n");
528         message.append(TAB + TAB +
529                        "quotapolicy.lastWarningRemind: " + std::to_string(item.quotapolicy.lastWarningRemind) + "\n");
530         message.append(TAB + TAB + "quotapolicy.lastLimitRemind: " + std::to_string(item.quotapolicy.lastLimitRemind) +
531                        "\n");
532         message.append(TAB + TAB + "Metered: " + std::to_string(item.quotapolicy.metered) + "\n" + TAB + TAB +
533                        "Ident: " + item.networkmatchrule.ident + "\n");
534         message.append(TAB + TAB + "quotapolicy.limitAction: " + std::to_string(item.quotapolicy.limitAction) + "\n" +
535                        TAB + TAB + "UsedBytes: " + std::to_string(item.quotapolicy.usedBytes) + "\n");
536         message.append(TAB + TAB + "UsedTimeDuration: " + std::to_string(item.quotapolicy.usedTimeDuration) + "\n");
537         message.append(TAB + TAB + "Possessor: " + item.quotapolicy.possessor + "\n\n");
538     });
539 }
540 } // namespace NetManagerStandard
541 } // namespace OHOS
542