1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#ifndef NET_POLICY_TRAFFIC_H 17b1b8bc3fSopenharmony_ci#define NET_POLICY_TRAFFIC_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include "singleton.h" 20b1b8bc3fSopenharmony_ci#include "system_ability.h" 21b1b8bc3fSopenharmony_ci 22b1b8bc3fSopenharmony_ci#include "net_policy_base.h" 23b1b8bc3fSopenharmony_ci#include "net_policy_callback.h" 24b1b8bc3fSopenharmony_ci#include "net_policy_service_stub.h" 25b1b8bc3fSopenharmony_ci#include "netsys_controller_callback.h" 26b1b8bc3fSopenharmony_ci 27b1b8bc3fSopenharmony_cinamespace OHOS { 28b1b8bc3fSopenharmony_cinamespace NetManagerStandard { 29b1b8bc3fSopenharmony_ciconstexpr int16_t NET_POLICY_LEAP_YEAR_ONE = 1; 30b1b8bc3fSopenharmony_ciconstexpr int16_t NET_POLICY_LEAP_YEAR_FOUR = 4; 31b1b8bc3fSopenharmony_ciconstexpr int16_t NET_POLICY_LEAP_YEAR_ONEHUNDRED = 100; 32b1b8bc3fSopenharmony_ciconstexpr int16_t NET_POLICY_LEAP_YEAR_FOURHUNDRED = 400; 33b1b8bc3fSopenharmony_ciconstexpr int16_t NET_POLICY_FEBRUARY = 1; 34b1b8bc3fSopenharmony_ciconstexpr int32_t NET_POLICY_ONEDAYTIME = 86400; 35b1b8bc3fSopenharmony_ciconstexpr int16_t MONTH_TWENTY_EIGHT = 28; 36b1b8bc3fSopenharmony_ciconstexpr int16_t MONTH_THIRTY = 30; 37b1b8bc3fSopenharmony_ciconstexpr int16_t MONTH_THIRTY_ONE = 31; 38b1b8bc3fSopenharmony_ciconstexpr int32_t NINETY_PERCENTAGE = 90; 39b1b8bc3fSopenharmony_ciconstexpr int32_t HUNDRED_PERCENTAGE = 100; 40b1b8bc3fSopenharmony_ciclass NetPolicyTraffic : public NetPolicyBase { 41b1b8bc3fSopenharmony_cipublic: 42b1b8bc3fSopenharmony_ci void Init(); 43b1b8bc3fSopenharmony_ci 44b1b8bc3fSopenharmony_ci /** 45b1b8bc3fSopenharmony_ci * Update quota policies. 46b1b8bc3fSopenharmony_ci * 47b1b8bc3fSopenharmony_ci * @param quotaPolicies The updated quota policies 48b1b8bc3fSopenharmony_ci * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode} 49b1b8bc3fSopenharmony_ci */ 50b1b8bc3fSopenharmony_ci int32_t UpdateQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies); 51b1b8bc3fSopenharmony_ci 52b1b8bc3fSopenharmony_ci /** 53b1b8bc3fSopenharmony_ci * Get network policies. 54b1b8bc3fSopenharmony_ci * 55b1b8bc3fSopenharmony_ci * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 56b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 57b1b8bc3fSopenharmony_ci */ 58b1b8bc3fSopenharmony_ci int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies); 59b1b8bc3fSopenharmony_ci 60b1b8bc3fSopenharmony_ci /** 61b1b8bc3fSopenharmony_ci * Update the limit or warning remind time of quota policy. 62b1b8bc3fSopenharmony_ci * 63b1b8bc3fSopenharmony_ci * @param netType {@link NetBearType}. 64b1b8bc3fSopenharmony_ci * @param simId Specify the matched simId of quota policy when netType is cellular. 65b1b8bc3fSopenharmony_ci * @param remindType {@link RemindType}. 66b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 67b1b8bc3fSopenharmony_ci */ 68b1b8bc3fSopenharmony_ci int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType); 69b1b8bc3fSopenharmony_ci 70b1b8bc3fSopenharmony_ci /** 71b1b8bc3fSopenharmony_ci * Handle the event from NetPolicyCore 72b1b8bc3fSopenharmony_ci * 73b1b8bc3fSopenharmony_ci * @param eventId The event id 74b1b8bc3fSopenharmony_ci * @param policyEvent The informations passed from other core 75b1b8bc3fSopenharmony_ci */ 76b1b8bc3fSopenharmony_ci void HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent); 77b1b8bc3fSopenharmony_ci 78b1b8bc3fSopenharmony_ci /** 79b1b8bc3fSopenharmony_ci * Get the metered ifaces. 80b1b8bc3fSopenharmony_ci * 81b1b8bc3fSopenharmony_ci * @return const std::vector<std::string>& The vector of metered ifaces 82b1b8bc3fSopenharmony_ci */ 83b1b8bc3fSopenharmony_ci const std::vector<std::string> &GetMeteredIfaces(); 84b1b8bc3fSopenharmony_ci 85b1b8bc3fSopenharmony_ci /** 86b1b8bc3fSopenharmony_ci * Reset network policies\rules\quota policies\firewall rules. 87b1b8bc3fSopenharmony_ci * 88b1b8bc3fSopenharmony_ci * @param simId Specify the matched simId of quota policy. 89b1b8bc3fSopenharmony_ci */ 90b1b8bc3fSopenharmony_ci int32_t ResetPolicies(const std::string &simId); 91b1b8bc3fSopenharmony_ci 92b1b8bc3fSopenharmony_ci /** 93b1b8bc3fSopenharmony_ci * Reset all network policies\rules\quota policies\firewall rules. 94b1b8bc3fSopenharmony_ci * 95b1b8bc3fSopenharmony_ci */ 96b1b8bc3fSopenharmony_ci int32_t ResetPolicies(); 97b1b8bc3fSopenharmony_ci 98b1b8bc3fSopenharmony_ci void ReachedLimit(const std::string &iface); 99b1b8bc3fSopenharmony_ci void UpdateNetPolicy(); 100b1b8bc3fSopenharmony_ci void GetDumpMessage(std::string &message); 101b1b8bc3fSopenharmony_ci 102b1b8bc3fSopenharmony_ciprivate: 103b1b8bc3fSopenharmony_ci class NetsysControllerCallbackImpl : public NetsysControllerCallback { 104b1b8bc3fSopenharmony_ci public: 105b1b8bc3fSopenharmony_ci NetsysControllerCallbackImpl(std::shared_ptr<NetPolicyTraffic> traffic) 106b1b8bc3fSopenharmony_ci { 107b1b8bc3fSopenharmony_ci traffic_ = traffic; 108b1b8bc3fSopenharmony_ci } 109b1b8bc3fSopenharmony_ci int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int32_t, int32_t) 110b1b8bc3fSopenharmony_ci { 111b1b8bc3fSopenharmony_ci return 0; 112b1b8bc3fSopenharmony_ci } 113b1b8bc3fSopenharmony_ci int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int32_t, int32_t) 114b1b8bc3fSopenharmony_ci { 115b1b8bc3fSopenharmony_ci return 0; 116b1b8bc3fSopenharmony_ci } 117b1b8bc3fSopenharmony_ci int32_t OnInterfaceAdded(const std::string &) 118b1b8bc3fSopenharmony_ci { 119b1b8bc3fSopenharmony_ci return 0; 120b1b8bc3fSopenharmony_ci } 121b1b8bc3fSopenharmony_ci int32_t OnInterfaceRemoved(const std::string &) 122b1b8bc3fSopenharmony_ci { 123b1b8bc3fSopenharmony_ci return 0; 124b1b8bc3fSopenharmony_ci } 125b1b8bc3fSopenharmony_ci int32_t OnInterfaceChanged(const std::string &, bool) 126b1b8bc3fSopenharmony_ci { 127b1b8bc3fSopenharmony_ci return 0; 128b1b8bc3fSopenharmony_ci } 129b1b8bc3fSopenharmony_ci int32_t OnInterfaceLinkStateChanged(const std::string &, bool) 130b1b8bc3fSopenharmony_ci { 131b1b8bc3fSopenharmony_ci return 0; 132b1b8bc3fSopenharmony_ci } 133b1b8bc3fSopenharmony_ci int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) 134b1b8bc3fSopenharmony_ci { 135b1b8bc3fSopenharmony_ci return 0; 136b1b8bc3fSopenharmony_ci } 137b1b8bc3fSopenharmony_ci int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) 138b1b8bc3fSopenharmony_ci { 139b1b8bc3fSopenharmony_ci return 0; 140b1b8bc3fSopenharmony_ci } 141b1b8bc3fSopenharmony_ci int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) 142b1b8bc3fSopenharmony_ci { 143b1b8bc3fSopenharmony_ci traffic_->ReachedLimit(iface); 144b1b8bc3fSopenharmony_ci return 0; 145b1b8bc3fSopenharmony_ci } 146b1b8bc3fSopenharmony_ci 147b1b8bc3fSopenharmony_ci private: 148b1b8bc3fSopenharmony_ci std::shared_ptr<NetPolicyTraffic> traffic_ = nullptr; 149b1b8bc3fSopenharmony_ci }; 150b1b8bc3fSopenharmony_ci 151b1b8bc3fSopenharmony_ci class ConnCallBack : public IRemoteStub<INetConnCallback> { 152b1b8bc3fSopenharmony_ci public: 153b1b8bc3fSopenharmony_ci ConnCallBack(std::shared_ptr<NetPolicyTraffic> connCallBack) 154b1b8bc3fSopenharmony_ci { 155b1b8bc3fSopenharmony_ci connCallBack_ = connCallBack; 156b1b8bc3fSopenharmony_ci } 157b1b8bc3fSopenharmony_ci int32_t NetAvailable(sptr<NetHandle> &netHandle) 158b1b8bc3fSopenharmony_ci { 159b1b8bc3fSopenharmony_ci if (connCallBack_ != nullptr) { 160b1b8bc3fSopenharmony_ci connCallBack_->UpdateNetPolicy(); 161b1b8bc3fSopenharmony_ci return 0; 162b1b8bc3fSopenharmony_ci } 163b1b8bc3fSopenharmony_ci return -1; 164b1b8bc3fSopenharmony_ci } 165b1b8bc3fSopenharmony_ci int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap) 166b1b8bc3fSopenharmony_ci { 167b1b8bc3fSopenharmony_ci return 0; 168b1b8bc3fSopenharmony_ci } 169b1b8bc3fSopenharmony_ci 170b1b8bc3fSopenharmony_ci int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) 171b1b8bc3fSopenharmony_ci { 172b1b8bc3fSopenharmony_ci return 0; 173b1b8bc3fSopenharmony_ci } 174b1b8bc3fSopenharmony_ci 175b1b8bc3fSopenharmony_ci int32_t NetLost(sptr<NetHandle> &netHandle) 176b1b8bc3fSopenharmony_ci { 177b1b8bc3fSopenharmony_ci return 0; 178b1b8bc3fSopenharmony_ci } 179b1b8bc3fSopenharmony_ci 180b1b8bc3fSopenharmony_ci int32_t NetUnavailable() 181b1b8bc3fSopenharmony_ci { 182b1b8bc3fSopenharmony_ci return 0; 183b1b8bc3fSopenharmony_ci } 184b1b8bc3fSopenharmony_ci 185b1b8bc3fSopenharmony_ci int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) 186b1b8bc3fSopenharmony_ci { 187b1b8bc3fSopenharmony_ci return 0; 188b1b8bc3fSopenharmony_ci } 189b1b8bc3fSopenharmony_ci 190b1b8bc3fSopenharmony_ci private: 191b1b8bc3fSopenharmony_ci std::shared_ptr<NetPolicyTraffic> connCallBack_ = nullptr; 192b1b8bc3fSopenharmony_ci }; 193b1b8bc3fSopenharmony_ci 194b1b8bc3fSopenharmony_ciprivate: 195b1b8bc3fSopenharmony_ci int32_t UpdateQuotaPoliciesInner(); 196b1b8bc3fSopenharmony_ci int64_t GetQuotaRemain(NetQuotaPolicy "aPolicy); 197b1b8bc3fSopenharmony_ci void UpdateQuotaNotify(); 198b1b8bc3fSopenharmony_ci void UpdateMeteredIfaces(std::vector<std::string> &newMeteredIfaces); 199b1b8bc3fSopenharmony_ci void UpdateNetEnableStatus(const NetQuotaPolicy "aPolicy); 200b1b8bc3fSopenharmony_ci void FormalizeQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies); 201b1b8bc3fSopenharmony_ci const std::vector<std::string> UpdateMeteredIfacesQuota(); 202b1b8bc3fSopenharmony_ci 203b1b8bc3fSopenharmony_ci bool IsValidQuotaPolicy(const NetQuotaPolicy "aPolicy); 204b1b8bc3fSopenharmony_ci int64_t GetTotalQuota(NetQuotaPolicy "aPolicy); 205b1b8bc3fSopenharmony_ci void SetNetworkEnableStatus(const NetQuotaPolicy "aPolicy, bool enable); 206b1b8bc3fSopenharmony_ci void NotifyQuotaWarning(int64_t totalQuota); 207b1b8bc3fSopenharmony_ci void NotifyQuotaLimit(int64_t totalQuota); 208b1b8bc3fSopenharmony_ci void NotifyQuotaLimitReminded(int64_t totalQuota); 209b1b8bc3fSopenharmony_ci void PublishQuotaEvent(const std::string &action, const std::string &describe, int64_t quota); 210b1b8bc3fSopenharmony_ci void ReadQuotaPolicies(); 211b1b8bc3fSopenharmony_ci bool WriteQuotaPolicies(); 212b1b8bc3fSopenharmony_ci const std::string GetMatchIfaces(const NetQuotaPolicy "aPolicy); 213b1b8bc3fSopenharmony_ci 214b1b8bc3fSopenharmony_ci bool IsValidNetType(int32_t netType); 215b1b8bc3fSopenharmony_ci bool IsValidPeriodDuration(const std::string &periodDuration); 216b1b8bc3fSopenharmony_ci bool IsQuotaPolicyExist(int32_t netType, const std::string &simId); 217b1b8bc3fSopenharmony_ci bool IsValidNetRemindType(uint32_t remindType); 218b1b8bc3fSopenharmony_ci 219b1b8bc3fSopenharmony_ciprivate: 220b1b8bc3fSopenharmony_ci std::vector<uint32_t> idleAllowedList_; 221b1b8bc3fSopenharmony_ci std::vector<NetQuotaPolicy> quotaPolicies_; 222b1b8bc3fSopenharmony_ci std::vector<std::string> meteredIfaces_; 223b1b8bc3fSopenharmony_ci sptr<NetsysControllerCallback> netsysCallback_ = nullptr; 224b1b8bc3fSopenharmony_ci sptr<INetConnCallback> netConnCallback_ = nullptr; 225b1b8bc3fSopenharmony_ci}; 226b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard 227b1b8bc3fSopenharmony_ci} // namespace OHOS 228b1b8bc3fSopenharmony_ci#endif // NET_POLICY_TRAFFIC_H 229