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 #ifndef NET_STATS_SERVICE_H 17 #define NET_STATS_SERVICE_H 18 19 #include "singleton.h" 20 #include "system_ability.h" 21 22 #include "net_push_stats_info.h" 23 #include "net_stats_cached.h" 24 #include "net_stats_callback.h" 25 #include "net_stats_history.h" 26 #include "net_stats_info_sequence.h" 27 #include "net_stats_listener.h" 28 #include "net_stats_network.h" 29 #include "net_stats_service_stub.h" 30 #include "netlink_manager.h" 31 #include "net_bundle.h" 32 #ifdef SUPPORT_TRAFFIC_STATISTIC 33 #include "ffrt_timer.h" 34 #include "net_stats_settings_observer.h" 35 #include "netsys_traffic_callback_stub.h" 36 #include "net_info_observer.h" 37 #include "netsys_controller_callback.h" 38 #include "net_stats_trafficLimit_dialog.h" 39 #endif // SUPPORT_TRAFFIC_STATISTIC 40 #include "network_sharing.h" 41 42 namespace OHOS { 43 namespace NetManagerStandard { 44 45 #ifdef SUPPORT_TRAFFIC_STATISTIC 46 using ObserverPtr = std::shared_ptr<TrafficDataObserver>; 47 using SettingsInfoPtr = std::shared_ptr<TrafficSettingsInfo>; 48 class TrafficObserver; 49 class NetsysControllerObserver; 50 #endif // SUPPORT_TRAFFIC_STATISTIC 51 class NetStatsService : public SystemAbility, 52 public NetStatsServiceStub, 53 public std::enable_shared_from_this<NetStatsService> { 54 DECLARE_DELAYED_SINGLETON(NetStatsService) 55 DECLARE_SYSTEM_ABILITY(NetStatsService) 56 57 public: 58 void OnStart() override; 59 void OnStop() override; 60 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 61 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 62 int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override; 63 int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override; 64 int32_t GetCellularRxBytes(uint64_t &stats) override; 65 int32_t GetCellularTxBytes(uint64_t &stats) override; 66 int32_t GetAllRxBytes(uint64_t &stats) override; 67 int32_t GetAllTxBytes(uint64_t &stats) override; 68 int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override; 69 int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override; 70 int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos) override; 71 int32_t GetAllSimStatsInfo(std::vector<NetStatsInfo> &infos) override; 72 int32_t GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos, 73 const sptr<NetStatsNetwork> &network) override; 74 int32_t GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid, 75 const sptr<NetStatsNetwork> &network) override; 76 int32_t SetAppStats(const PushStatsInfo &info) override; 77 int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 78 int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 79 int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end, 80 NetStatsInfo &statsInfo) override; 81 int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end, 82 NetStatsInfo &statsInfo) override; 83 int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end, 84 const NetStatsInfo &stats) override; 85 int32_t UpdateStatsData() override; 86 int32_t ResetFactory() override; 87 int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override; 88 int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override; 89 int32_t SaveSharingTraffic(const NetStatsInfo &infos) override; 90 91 #ifdef SUPPORT_TRAFFIC_STATISTIC 92 void UpdataSettingsdata(int32_t simId, uint8_t flag, uint64_t value); 93 bool ProcessNetConnectionPropertiesChange(int32_t simId, uint64_t ifIndex); 94 int32_t GetCurActiviteSimId(); 95 std::map<int32_t, std::pair<ObserverPtr, SettingsInfoPtr>> GetSettingsObserverMap(); 96 int32_t NotifyTrafficAlert(uint8_t flag); 97 #endif // SUPPORT_TRAFFIC_STATISTIC 98 99 private: 100 bool Init(); 101 void GetDumpMessage(std::string &message); 102 void MergeTrafficStats(std::vector<NetStatsInfoSequence> &statsInfoSequences, const NetStatsInfo &info, 103 uint32_t currentTime); 104 bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames); 105 std::unordered_map<uint32_t, SampleBundleInfo> GetSampleBundleInfosForActiveUser(); 106 SampleBundleInfo GetSampleBundleInfoForUid(uint32_t uid); 107 void RefreshUidStatsFlag(uint64_t delay); 108 void RegisterCommonEvent(); 109 bool CommonEventPackageAdded(uint32_t uid); 110 bool CommonEventPackageRemoved(uint32_t uid); 111 void FilterTrafficStatsByNetwork(std::vector<NetStatsInfo> &allInfo, 112 std::unordered_map<uint32_t, NetStatsInfo> &infos, 113 const std::string ident, uint32_t startTime, uint32_t endTime); 114 void FilterTrafficStatsByUidNetwork(std::vector<NetStatsInfo> &allInfo, std::vector<NetStatsInfoSequence> &infos, 115 const uint32_t uid, const std::string ident, uint32_t startTime, uint32_t endTime); 116 #ifdef SUPPORT_NETWORK_SHARE 117 bool IsSharingOn(); 118 void GetSharingStats(std::vector<NetStatsInfo> &sharingStats, uint32_t endtime); 119 #endif 120 #ifdef SUPPORT_TRAFFIC_STATISTIC 121 bool CommonEventSimStateChanged(int32_t slotId, int32_t simState); 122 bool CommonEventCellularDataStateChanged(int32_t slotId, int32_t dataState); 123 int32_t GetAllUsedTrafficStatsByNetwork(const sptr<NetStatsNetwork> &network, uint64_t &allUsedTraffic); 124 void UpdateBpfMap(); 125 void UpdateBeginDate(int32_t simId, int32_t beginDate); 126 void UpdateUnLimitedDataEnable(int32_t simId, int8_t unLimitedDataEnable); 127 void UpdateMonthlyLimitdNotifyType(int32_t simId, int8_t monthlyLimitdNotifyType); 128 void UpdateMonthlyLimit(int32_t simId, uint64_t monthlyLimit); 129 void UpdateMonthlyMark(int32_t simId, uint16_t monthlyMark); 130 void UpdateDailyMark(int32_t simId, uint16_t dailyMark); 131 void SetTrafficMapMaxValue(); 132 void StartNetObserver(); 133 void StartTrafficOvserver(); 134 void StopTrafficOvserver(); 135 bool GetNotifyStats(uint8_t flag); 136 bool GetMonAlertStatus(); 137 bool GetMonNotifyStatus(); 138 bool GetDayNotifyStatus(); 139 void DealMonAlert(bool isDaulCard); 140 void DealMonNotification(bool isDaulCard); 141 void DealDayNotification(bool isDaulCard); 142 void DealNotificaiton(uint8_t flag); 143 bool IsMobileDataEnabled(); 144 void UpdateTrafficLimitDate(int32_t simId); 145 void UpdateNetStatsToMapFromDB(int32_t simId); 146 void UpdateCurActiviteSimChanged(); 147 bool CalculateTrafficAvailable(int32_t simId, uint64_t &monthlyAvailable, 148 uint64_t &monthlyMarkAvailable, uint64_t &dailyMarkAvailable); 149 #endif // SUPPORT_TRAFFIC_STATISTIC 150 151 private: 152 enum ServiceRunningState { 153 STATE_STOPPED = 0, 154 STATE_RUNNING, 155 }; 156 157 bool registerToService_; 158 ServiceRunningState state_; 159 std::shared_ptr<NetStatsCallback> netStatsCallback_ = nullptr; 160 std::shared_ptr<NetStatsListener> subscriber_ = nullptr; 161 std::unique_ptr<NetStatsCached> netStatsCached_ = nullptr; 162 #ifdef SUPPORT_TRAFFIC_STATISTIC 163 int32_t curActiviteSimId_ = -1; 164 uint64_t curIfIndex_ = UINT64_MAX; 165 std::atomic_bool isWifiConnected_ = false; 166 std::map<int32_t, std::pair<ObserverPtr, SettingsInfoPtr>> settingsTrafficMap_; 167 std::unique_ptr<FfrtTimer> trafficTimer_ = nullptr; 168 sptr<NetInfoObserver> netconnCallback_ = nullptr; 169 sptr<TrafficObserver> trafficObserver_ = nullptr; 170 sptr<NetsysControllerCallback> netsysControllerObserver_ = nullptr; 171 SafeMap<std::string, std::string> ifaceNameIdentMap_; 172 std::shared_ptr<TrafficLimitDialog> dialog_ = nullptr; 173 #endif // SUPPORT_TRAFFIC_STATISTIC 174 }; 175 176 #ifdef SUPPORT_TRAFFIC_STATISTIC 177 class TrafficObserver : public NetsysNative::NetsysTrafficCallbackStub { 178 public: 179 TrafficObserver(); 180 ~TrafficObserver() override; 181 int32_t OnExceedTrafficLimits(int8_t &flag) override; 182 }; 183 #endif // SUPPORT_TRAFFIC_STATISTIC 184 } // namespace NetManagerStandard 185 } // namespace OHOS 186 #endif // NET_STATS_SERVICE_H 187