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     GetCbInst()->NotifyNetQuotaPolicyChangeAsync(quotaPolicies_);
143     NETMGR_LOG_I("End UpdateQuotaPoliciesInner.");
144     return NETMANAGER_SUCCESS;
145 }
146 
FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)147 void NetPolicyTraffic::FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
148 {
149     quotaPolicies_.clear();
150     for (auto quotaPolicy : quotaPolicies) {
151         if (!IsValidQuotaPolicy(quotaPolicy)) {
152             NETMGR_LOG_E("UpdateQuotaPolicies invalid quota policy netType[%{public}d], periodDuration[%{public}s]",
153                          quotaPolicy.networkmatchrule.netType, quotaPolicy.quotapolicy.periodDuration.c_str());
154             continue;
155         }
156         if (quotaPolicy.quotapolicy.limitBytes == DATA_USAGE_UNKNOWN) {
157             quotaPolicy.quotapolicy.limitAction = LIMIT_ACTION_ALERT_ONLY;
158         } else if (quotaPolicy.quotapolicy.warningBytes == DATA_USAGE_UNKNOWN) {
159             quotaPolicy.quotapolicy.warningBytes =
160                 quotaPolicy.quotapolicy.limitBytes * NINETY_PERCENTAGE / HUNDRED_PERCENTAGE;
161         }
162         if (quotaPolicy.quotapolicy.limitAction == LIMIT_ACTION_ALERT_ONLY) {
163             quotaPolicy.quotapolicy.limitBytes = DATA_USAGE_UNLIMITED;
164         }
165         if (quotaPolicy.quotapolicy.warningBytes > quotaPolicy.quotapolicy.limitBytes) {
166             quotaPolicy.quotapolicy.warningBytes = DATA_USAGE_UNLIMITED;
167         }
168         if (quotaPolicy.quotapolicy.limitBytes == DATA_USAGE_UNLIMITED) {
169             quotaPolicy.quotapolicy.limitAction = LIMIT_ACTION_ALERT_ONLY;
170         }
171         quotaPolicies_.push_back(quotaPolicy);
172     }
173 }
174 
UpdateMeteredIfacesQuota()175 const std::vector<std::string> NetPolicyTraffic::UpdateMeteredIfacesQuota()
176 {
177     std::vector<std::string> newMeteredIfaces;
178     for (auto &quotaPolicy : quotaPolicies_) {
179         std::string iface = GetMatchIfaces(quotaPolicy);
180         // set quota for metered iface.
181         if (iface == UNKNOW_IFACE || !quotaPolicy.quotapolicy.metered) {
182             continue;
183         }
184         newMeteredIfaces.push_back(iface);
185         int64_t quotaRemain = GetQuotaRemain(quotaPolicy);
186         if (quotaRemain >= 0) {
187             GetNetsysInst()->BandwidthSetIfaceQuota(iface, quotaRemain);
188         }
189     }
190     // remove the iface quota that not metered.
191     for (uint32_t i = 0; i < meteredIfaces_.size(); ++i) {
192         if (!std::count(newMeteredIfaces.begin(), newMeteredIfaces.end(), meteredIfaces_[i])) {
193             GetNetsysInst()->BandwidthRemoveIfaceQuota(meteredIfaces_[i]);
194         }
195     }
196     return newMeteredIfaces;
197 }
198 
UpdateMeteredIfaces(std::vector<std::string> & newMeteredIfaces)199 void NetPolicyTraffic::UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces)
200 {
201     NETMGR_LOG_D("UpdateMeteredIfaces size[%{public}zu]", newMeteredIfaces.size());
202     meteredIfaces_.clear();
203     meteredIfaces_.reserve(newMeteredIfaces.size());
204     for (auto &iface : newMeteredIfaces) {
205         meteredIfaces_.push_back(iface);
206     }
207     // notify the callback of metered ifaces changed.
208     GetCbInst()->NotifyNetMeteredIfacesChangeAsync(meteredIfaces_);
209 }
210 
UpdateQuotaNotify()211 void NetPolicyTraffic::UpdateQuotaNotify()
212 {
213     NetmanagerHiTrace::NetmanagerStartSyncTrace("Traverse cellular network start");
214     for (auto &quotaPolicy : quotaPolicies_) {
215         NetmanagerHiTrace::NetmanagerStartSyncTrace("Get the start time of the metering cycle start");
216         int64_t start = quotaPolicy.GetPeriodStart();
217         NetmanagerHiTrace::NetmanagerFinishSyncTrace("Get the start time of the metering cycle end");
218 
219         NetmanagerHiTrace::NetmanagerStartSyncTrace("Get the usage of traffic start");
220         int64_t totalQuota = GetTotalQuota(quotaPolicy);
221         NetmanagerHiTrace::NetmanagerFinishSyncTrace("Get the usage of traffic end");
222         // check if the quota is over the limit
223         if (quotaPolicy.IsOverLimit(totalQuota)) {
224             if (quotaPolicy.quotapolicy.lastLimitRemind > start) {
225                 // notify the quota reach limit and has reminded before.
226                 NetmanagerHiTrace::NetmanagerStartSyncTrace("Notify quota limit reminded start");
227                 NotifyQuotaLimitReminded(totalQuota);
228                 NetmanagerHiTrace::NetmanagerFinishSyncTrace("Notify quota limit reminded end");
229                 continue;
230             }
231             NetmanagerHiTrace::NetmanagerStartSyncTrace("Update net enable status start");
232             UpdateNetEnableStatus(quotaPolicy);
233             NetmanagerHiTrace::NetmanagerFinishSyncTrace("Update net enable status end");
234             // notify the quota reach limit
235             NotifyQuotaLimit(totalQuota);
236             continue;
237         }
238         // check if the quota is over the warning
239         if (quotaPolicy.IsOverWarning(totalQuota) && quotaPolicy.quotapolicy.lastWarningRemind < start) {
240             NetmanagerHiTrace::NetmanagerStartSyncTrace("Notify quota warning remind start");
241             NotifyQuotaWarning(totalQuota);
242             NetmanagerHiTrace::NetmanagerFinishSyncTrace("Notify quota warning remind end");
243         }
244     }
245     NetmanagerHiTrace::NetmanagerFinishSyncTrace("Traverse cellular network end");
246 }
247 
GetQuotaRemain(NetQuotaPolicy & quotaPolicy)248 int64_t NetPolicyTraffic::GetQuotaRemain(NetQuotaPolicy &quotaPolicy)
249 {
250     int64_t start = quotaPolicy.GetPeriodStart();
251     int64_t totalQuota = GetTotalQuota(quotaPolicy);
252     NETMGR_LOG_D("GetQuotaRemain totalQuota[%{public}s] limit[%{public}s] start[%{public}s]",
253                  std::to_string(totalQuota).c_str(), std::to_string(quotaPolicy.quotapolicy.limitBytes).c_str(),
254                  ctime(&start));
255     // calculate the quota for each policy.
256     bool hasLimit = quotaPolicy.quotapolicy.limitBytes != DATA_USAGE_UNKNOWN;
257     int64_t quota = LONG_MAX;
258     if (hasLimit || quotaPolicy.quotapolicy.metered) {
259         if (hasLimit && quotaPolicy.quotapolicy.periodDuration != QUOTA_POLICY_NO_PERIOD) {
260             if (quotaPolicy.quotapolicy.lastLimitRemind >= start) {
261                 return LONG_MAX;
262             }
263             quota = quotaPolicy.quotapolicy.limitBytes - totalQuota;
264         }
265     }
266     return quota < 0 ? 0 : quota;
267 }
268 
UpdateNetEnableStatus(const NetQuotaPolicy & quotaPolicy)269 void NetPolicyTraffic::UpdateNetEnableStatus(const NetQuotaPolicy &quotaPolicy)
270 {
271     NETMGR_LOG_D("UpdateNetEnableStatus metered[%{public}d] quotapolicy.limitAction[%{public}d]",
272                  quotaPolicy.quotapolicy.metered, quotaPolicy.quotapolicy.limitAction);
273     if (quotaPolicy.quotapolicy.metered || quotaPolicy.quotapolicy.limitAction == LIMIT_ACTION_ACCESS_DISABLED) {
274         SetNetworkEnableStatus(quotaPolicy, false);
275     }
276 }
277 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)278 int32_t NetPolicyTraffic::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
279 {
280     quotaPolicies.clear();
281     quotaPolicies = quotaPolicies_;
282     NETMGR_LOG_D("GetNetQuotaPolicies quotaPolicies end size[%{public}zu]", quotaPolicies.size());
283     return NETMANAGER_SUCCESS;
284 }
285 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)286 int32_t NetPolicyTraffic::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
287 {
288     if (!IsValidNetType(netType)) {
289         NETMGR_LOG_E("NetPolicyType is invalid policy[%{public}d]", netType);
290         return NETMANAGER_ERR_PARAMETER_ERROR;
291     }
292 
293     if (!IsValidNetRemindType(remindType)) {
294         return NETMANAGER_ERR_PARAMETER_ERROR;
295     }
296 
297     for (uint32_t i = 0; i < quotaPolicies_.size(); ++i) {
298         NetQuotaPolicy &quotaPolicy = quotaPolicies_[i];
299         int32_t netTypeTemp = quotaPolicy.networkmatchrule.netType;
300         std::string iccidTemp = quotaPolicy.networkmatchrule.simId;
301         if (netTypeTemp == netType && iccidTemp == simId) {
302             switch (remindType) {
303                 case REMIND_TYPE_WARNING:
304                     quotaPolicy.quotapolicy.lastWarningRemind = time(nullptr);
305                     break;
306                 case REMIND_TYPE_LIMIT:
307                     quotaPolicy.quotapolicy.lastLimitRemind = time(nullptr);
308                     break;
309                 default:
310                     return NETMANAGER_ERR_PARAMETER_ERROR;
311             }
312         }
313     }
314     UpdateQuotaPoliciesInner();
315     NETMGR_LOG_I("NetPolicyTraffic::UpdateRemindPolicy end.");
316     return NETMANAGER_SUCCESS;
317 }
318 
GetMeteredIfaces()319 const std::vector<std::string> &NetPolicyTraffic::GetMeteredIfaces()
320 {
321     return meteredIfaces_;
322 }
323 
ResetPolicies(const std::string & simId)324 int32_t NetPolicyTraffic::ResetPolicies(const std::string &simId)
325 {
326     for (auto &quotaPolicy : quotaPolicies_) {
327         if (quotaPolicy.networkmatchrule.simId == simId) {
328             quotaPolicy.Reset();
329         }
330     }
331     return UpdateQuotaPoliciesInner();
332 }
333 
ResetPolicies()334 int32_t NetPolicyTraffic::ResetPolicies()
335 {
336     for (auto &quotaPolicy : quotaPolicies_) {
337         NETMGR_LOG_I("NetPolicyTraffic::ResetPolicies [%{public}s.", quotaPolicy.networkmatchrule.simId.c_str());
338         quotaPolicy.Reset();
339     }
340     return UpdateQuotaPoliciesInner();
341 }
342 
ReachedLimit(const std::string & iface)343 void NetPolicyTraffic::ReachedLimit(const std::string &iface)
344 {
345     NETMGR_LOG_D("ReachedLimit iface:%{public}s.", iface.c_str());
346     auto &ifaces = GetMeteredIfaces();
347     if (std::find(ifaces.begin(), ifaces.end(), iface) != ifaces.end()) {
348         UpdateQuotaPoliciesInner();
349     }
350 }
351 
UpdateNetPolicy()352 void NetPolicyTraffic::UpdateNetPolicy()
353 {
354     UpdateQuotaPoliciesInner();
355 }
356 
GetTotalQuota(NetQuotaPolicy & quotaPolicy)357 int64_t NetPolicyTraffic::GetTotalQuota(NetQuotaPolicy &quotaPolicy)
358 {
359     std::string iface = GetMatchIfaces(quotaPolicy);
360     NetStatsInfo info;
361     int64_t start = quotaPolicy.GetPeriodStart();
362     int64_t end = static_cast<int64_t>(time(nullptr));
363     if (end < 0) {
364         return 0;
365     }
366     GetNetCenterInst().GetIfaceStatsDetail(iface, start, end, info);
367     int64_t quota = static_cast<int64_t>(info.rxBytes_ + info.txBytes_);
368 
369     return quota < 0 ? 0 : quota;
370 }
371 
ReadQuotaPolicies()372 void NetPolicyTraffic::ReadQuotaPolicies()
373 {
374     GetFileInst()->ReadQuotaPolicies(quotaPolicies_);
375     UpdateQuotaPoliciesInner();
376 }
377 
WriteQuotaPolicies()378 bool NetPolicyTraffic::WriteQuotaPolicies()
379 {
380     return GetFileInst()->WriteQuotaPolicies(quotaPolicies_);
381 }
382 
GetMatchIfaces(const NetQuotaPolicy & quotaPolicy)383 const std::string NetPolicyTraffic::GetMatchIfaces(const NetQuotaPolicy &quotaPolicy)
384 {
385     std::string ident = "";
386     if (quotaPolicy.networkmatchrule.netType == BEARER_CELLULAR) {
387         ident = IDENT_PREFIX_CELLULAR + quotaPolicy.networkmatchrule.simId;
388     } else if (quotaPolicy.networkmatchrule.netType == BEARER_WIFI) {
389         ident = quotaPolicy.networkmatchrule.ident;
390     } else if (quotaPolicy.networkmatchrule.netType == BEARER_ETHERNET) {
391         ident = quotaPolicy.networkmatchrule.ident;
392     }
393     std::string iface;
394     if (quotaPolicy.networkmatchrule.netType >= BEARER_DEFAULT) {
395         return iface;
396     }
397     GetNetCenterInst().GetIfaceNameByType(static_cast<NetBearType>(quotaPolicy.networkmatchrule.netType), ident, iface);
398     NETMGR_LOG_D("GetMatchIfaces netType: %{public}d ident: %{public}s iface: %{public}s.",
399                  quotaPolicy.networkmatchrule.netType, ident.c_str(), iface.c_str());
400     return iface;
401 }
402 
SetNetworkEnableStatus(const NetQuotaPolicy & quotaPolicy,bool enable)403 void NetPolicyTraffic::SetNetworkEnableStatus(const NetQuotaPolicy &quotaPolicy, bool enable)
404 {
405     NETMGR_LOG_D("SetNetworkEnableStatus enable: %{public}d ", enable);
406 }
407 
NotifyQuotaWarning(int64_t totalQuota)408 void NetPolicyTraffic::NotifyQuotaWarning(int64_t totalQuota)
409 {
410     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_WARNING, BROADCAST_QUOTA_WARNING, totalQuota);
411 }
412 
NotifyQuotaLimitReminded(int64_t totalQuota)413 void NetPolicyTraffic::NotifyQuotaLimitReminded(int64_t totalQuota)
414 {
415     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_LIMIT_REMINDED, BROADCAST_QUOTA_LIMIT_REMIND, totalQuota);
416 }
417 
NotifyQuotaLimit(int64_t totalQuota)418 void NetPolicyTraffic::NotifyQuotaLimit(int64_t totalQuota)
419 {
420     PublishQuotaEvent(COMMON_EVENT_NET_QUOTA_LIMIT, BROADCAST_QUOTA_LIMIT, totalQuota);
421 }
422 
PublishQuotaEvent(const std::string & action,const std::string & describe,int64_t quota)423 void NetPolicyTraffic::PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota)
424 {
425     BroadcastInfo info;
426     info.action = action;
427     info.data = describe;
428     info.permission = Permission::CONNECTIVITY_INTERNAL;
429     std::map<std::string, int64_t> param = {{"totalQuota", quota}};
430     BroadcastManager::GetInstance().SendBroadcast(info, param);
431 }
432 
IsValidPeriodDuration(const std::string & periodDuration)433 bool NetPolicyTraffic::IsValidPeriodDuration(const std::string &periodDuration)
434 {
435     if (periodDuration.empty() || periodDuration.size() < PERIOD_DURATION_SIZE) {
436         NETMGR_LOG_E("periodDuration is illegal");
437         return false;
438     }
439 
440     std::string cycle = periodDuration.substr(0, 1);
441     NETMGR_LOG_D("PeriodDuration [%{public}s].", periodDuration.c_str());
442     int32_t start = CommonUtils::StrToInt(periodDuration.substr(1, periodDuration.size()));
443 
444     if (cycle == PERIOD_DAY) {
445         if (start >= PERIOD_START && start <= DAY_MAX) {
446             return true;
447         }
448     }
449 
450     if (cycle == PERIOD_MONTH) {
451         if (start >= PERIOD_START && start <= MONTH_MAX) {
452             return true;
453         }
454     }
455 
456     if (cycle == PERIOD_YEAR) {
457         if (start >= PERIOD_START && start <= YEAR_MAX) {
458             return true;
459         }
460     }
461     NETMGR_LOG_E("Invalid periodDuration start [%{public}d],Invalid periodDuration cycle [%{public}s]", start,
462                  cycle.c_str());
463     return false;
464 }
465 
IsQuotaPolicyExist(int32_t netType,const std::string & simId)466 bool NetPolicyTraffic::IsQuotaPolicyExist(int32_t netType, const std::string &simId)
467 {
468     std::vector<NetQuotaPolicy> quotaPolicies;
469     GetFileInst()->ReadQuotaPolicies(quotaPolicies);
470 
471     if (quotaPolicies.empty()) {
472         NETMGR_LOG_E("quotaPolicies is empty");
473         return false;
474     }
475 
476     for (uint32_t i = 0; i < quotaPolicies.size(); i++) {
477         if (netType == quotaPolicies[i].networkmatchrule.netType && simId == quotaPolicies[i].networkmatchrule.simId) {
478             return true;
479         }
480     }
481 
482     return false;
483 }
484 
HandleEvent(int32_t eventId,const std::shared_ptr<PolicyEvent> & policyEvent)485 void NetPolicyTraffic::HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent)
486 {
487     NETMGR_LOG_D("NetPolicyTraffic HandleEvent");
488 }
489 
GetDumpMessage(std::string & message)490 void NetPolicyTraffic::GetDumpMessage(std::string &message)
491 {
492     static const std::string TAB = "    ";
493     message.append(TAB + "MeteredIfaces: {");
494     std::for_each(meteredIfaces_.begin(), meteredIfaces_.end(),
495                   [&message](const std::string &item) { message.append(item + ", "); });
496     message.append("}\n");
497     message.append(TAB + "QuotaPolicies:\n");
498     std::for_each(quotaPolicies_.begin(), quotaPolicies_.end(), [&message](const auto &item) {
499         message.append(TAB + TAB + "NetType: " + std::to_string(item.networkmatchrule.netType) + "\n" + TAB + TAB +
500                        "simId: " + item.networkmatchrule.simId + "\n" + TAB + TAB +
501                        "Ident: " + item.networkmatchrule.ident + "\n");
502         message.append(TAB + TAB + "PeriodStartTime: " + std::to_string(item.quotapolicy.periodStartTime) + "\n");
503         message.append(TAB + TAB + "PeriodDuration: " + item.quotapolicy.periodDuration + "\n");
504         message.append(TAB + TAB + "Title: " + item.quotapolicy.title + "\n" + TAB + TAB +
505                        "Summary: " + item.quotapolicy.summary + "\n");
506         message.append(TAB + TAB + "quotapolicy.warningBytes: " + std::to_string(item.quotapolicy.warningBytes) + "\n");
507         message.append(TAB + TAB + "quotapolicy.limitBytes: " + std::to_string(item.quotapolicy.limitBytes) + "\n");
508         message.append(TAB + TAB +
509                        "quotapolicy.lastWarningRemind: " + std::to_string(item.quotapolicy.lastWarningRemind) + "\n");
510         message.append(TAB + TAB + "quotapolicy.lastLimitRemind: " + std::to_string(item.quotapolicy.lastLimitRemind) +
511                        "\n");
512         message.append(TAB + TAB + "Metered: " + std::to_string(item.quotapolicy.metered) + "\n" + TAB + TAB +
513                        "Ident: " + item.networkmatchrule.ident + "\n");
514         message.append(TAB + TAB + "quotapolicy.limitAction: " + std::to_string(item.quotapolicy.limitAction) + "\n" +
515                        TAB + TAB + "UsedBytes: " + std::to_string(item.quotapolicy.usedBytes) + "\n");
516         message.append(TAB + TAB + "UsedTimeDuration: " + std::to_string(item.quotapolicy.usedTimeDuration) + "\n");
517         message.append(TAB + TAB + "Possessor: " + item.quotapolicy.possessor + "\n\n");
518     });
519 }
520 } // namespace NetManagerStandard
521 } // namespace OHOS
522