• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_POLICY_TRAFFIC_H
17 #define NET_POLICY_TRAFFIC_H
18 
19 #include "singleton.h"
20 #include "system_ability.h"
21 
22 #include "net_policy_base.h"
23 #include "net_policy_callback.h"
24 #include "net_policy_service_stub.h"
25 #include "netsys_controller_callback.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 constexpr int16_t NET_POLICY_LEAP_YEAR_ONE = 1;
30 constexpr int16_t NET_POLICY_LEAP_YEAR_FOUR = 4;
31 constexpr int16_t NET_POLICY_LEAP_YEAR_ONEHUNDRED = 100;
32 constexpr int16_t NET_POLICY_LEAP_YEAR_FOURHUNDRED = 400;
33 constexpr int16_t NET_POLICY_FEBRUARY = 1;
34 constexpr int32_t NET_POLICY_ONEDAYTIME = 86400;
35 constexpr int16_t MONTH_TWENTY_EIGHT = 28;
36 constexpr int16_t MONTH_THIRTY = 30;
37 constexpr int16_t MONTH_THIRTY_ONE = 31;
38 constexpr int32_t NINETY_PERCENTAGE = 90;
39 constexpr int32_t HUNDRED_PERCENTAGE = 100;
40 class NetPolicyTraffic : public NetPolicyBase {
41 public:
42     void Init();
43 
44     /**
45      * Update quota policies.
46      *
47      * @param quotaPolicies The updated quota policies
48      * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}
49      */
50     int32_t UpdateQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
51 
52     /**
53      * Get network policies.
54      *
55      * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}.
56      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
57      */
58     int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies);
59 
60     /**
61      * Update the limit or warning remind time of quota policy.
62      *
63      * @param netType {@link NetBearType}.
64      * @param iccid Specify the matched iccid of quota policy when netType is cellular.
65      * @param remindType {@link RemindType}.
66      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
67      */
68     int32_t UpdateRemindPolicy(int32_t netType, const std::string &iccid, uint32_t remindType);
69 
70     /**
71      * Handle the event from NetPolicyCore
72      *
73      * @param eventId The event id
74      * @param policyEvent The informations passed from other core
75      */
76     void HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent);
77 
78     /**
79      * Get the metered ifaces.
80      *
81      * @return const std::vector<std::string>& The vector of metered ifaces
82      */
83     const std::vector<std::string> &GetMeteredIfaces();
84 
85     /**
86      * Reset network policies\rules\quota policies\firewall rules.
87      *
88      * @param iccid Specify the matched iccid of quota policy.
89      */
90     int32_t ResetPolicies(const std::string &iccid);
91 
92     void ReachedLimit(const std::string &iface);
93     void GetDumpMessage(std::string &message);
94 
95 private:
96     class NetsysControllerCallbackImpl : public NetsysControllerCallback {
97     public:
NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)98         NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic)
99         {
100             traffic_ = traffic;
101         }
OnInterfaceAddressUpdated(const std::string &,const std::string &,int,int)102         virtual int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int)
103         {
104             return 0;
105         }
OnInterfaceAddressRemoved(const std::string &,const std::string &,int,int)106         virtual int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int)
107         {
108             return 0;
109         }
OnInterfaceAdded(const std::string &)110         virtual int32_t OnInterfaceAdded(const std::string &)
111         {
112             return 0;
113         }
OnInterfaceRemoved(const std::string &)114         virtual int32_t OnInterfaceRemoved(const std::string &)
115         {
116             return 0;
117         }
OnInterfaceChanged(const std::string &,bool)118         virtual int32_t OnInterfaceChanged(const std::string &, bool)
119         {
120             return 0;
121         }
OnInterfaceLinkStateChanged(const std::string &,bool)122         virtual int32_t OnInterfaceLinkStateChanged(const std::string &, bool)
123         {
124             return 0;
125         }
OnRouteChanged(bool,const std::string &,const std::string &,const std::string &)126         virtual int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &)
127         {
128             return 0;
129         }
OnDhcpSuccess(NetsysControllerCallback::DhcpResult & dhcpResult)130         virtual int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
131         {
132             return 0;
133         }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)134         virtual int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
135         {
136             traffic_->ReachedLimit(iface);
137             return 0;
138         }
139 
140     private:
141         std::shared_ptr<NetPolicyTraffic> traffic_ = nullptr;
142     };
143 
144     int32_t UpdateQuotaPoliciesInner();
145     int64_t GetQuotaRemain(NetQuotaPolicy &quotaPolicy);
146     void UpdateQuotaNotify();
147     void UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces);
148     void UpdateNetEnableStatus(const NetQuotaPolicy &quotaPolicy);
149     void FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies);
150     const std::vector<std::string> UpdateMeteredIfacesQuota();
151 
152     bool IsValidQuotaPolicy(const NetQuotaPolicy &quotaPolicy);
153     int64_t GetTotalQuota(NetQuotaPolicy &quotaPolicy);
154     void SetNetworkEnableStatus(const NetQuotaPolicy &quotaPolicy, bool enable);
155     void NotifyQuotaWarning(int64_t totalQuota);
156     void NotifyQuotaLimit(int64_t totalQuota);
157     void NotifyQuotaLimitReminded(int64_t totalQuota);
158     void PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota);
159     int32_t ReadQuotaPolicies();
160     bool WriteQuotaPolicies();
161     const std::string GetMatchIfaces(const NetQuotaPolicy &quotaPolicy);
162 
163     bool IsValidNetType(int32_t netType);
164     bool IsValidPeriodDuration(const std::string &periodDuration);
165     bool IsQuotaPolicyExist(int32_t netType, const std::string &iccid);
166     bool IsValidNetRemindType(uint32_t remindType);
167 
168 private:
169     std::vector<uint32_t> idleAllowedList_;
170     std::vector<NetQuotaPolicy> quotaPolicies_;
171     std::vector<std::string> meteredIfaces_;
172     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
173 };
174 } // namespace NetManagerStandard
175 } // namespace OHOS
176 #endif // NET_POLICY_TRAFFIC_H
177