• 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 #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 "netsys_controller_callback.h"
37 #include "net_stats_trafficLimit_dialog.h"
38 #endif // SUPPORT_TRAFFIC_STATISTIC
39 #include "network_sharing.h"
40 #include "net_stats_subscriber.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 
52 class NetStatsService : public SystemAbility,
53                         public NetStatsServiceStub,
54                         public std::enable_shared_from_this<NetStatsService> {
55     DECLARE_DELAYED_SINGLETON(NetStatsService)
56     DECLARE_SYSTEM_ABILITY(NetStatsService)
57 
58 public:
59     void OnStart() override;
60     void OnStop() override;
61     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
62     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
63     int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override;
64     int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override;
65     int32_t GetCellularRxBytes(uint64_t &stats) override;
66     int32_t GetCellularTxBytes(uint64_t &stats) override;
67     int32_t GetAllRxBytes(uint64_t &stats) override;
68     int32_t GetAllTxBytes(uint64_t &stats) override;
69     int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override;
70     int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override;
71     int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos) override;
72     int32_t GetAllSimStatsInfo(std::vector<NetStatsInfo> &infos) override;
73     int32_t GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos,
74                                      const NetStatsNetwork &networkIpc) override;
75     int32_t GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid,
76                                         const NetStatsNetwork &networkIpc) override;
77     int32_t SetAppStats(const PushStatsInfo &info) override;
78     int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
79     int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
80     int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
81                                 NetStatsInfo &statsInfo) override;
82     int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
83                               NetStatsInfo &statsInfo) override;
84     int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
85                               const NetStatsInfo &stats) override;
86     int32_t UpdateStatsData() override;
87     void StartAccountObserver();
88     int32_t ProcessOsAccountChanged(int32_t toId, AccountSA::OsAccountState state);
89     int32_t ResetFactory() override;
90     int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override;
91     int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override;
92     int32_t SaveSharingTraffic(const NetStatsInfo &infos) override;
93 
94 #ifdef SUPPORT_TRAFFIC_STATISTIC
95     void UpdataSettingsdata(int32_t simId, uint8_t flag, uint64_t value);
96     std::map<int32_t, std::pair<ObserverPtr, SettingsInfoPtr>> GetSettingsObserverMap();
97     int32_t NotifyTrafficAlert(int32_t simId, uint8_t flag);
98     bool GetMonthlyLimitBySimId(int32_t simId, uint64_t &monthlyLimit);
99     bool GetMonthlyMarkBySimId(int32_t simId, uint16_t &monthlyMark);
100     bool GetdailyMarkBySimId(int32_t simId, uint16_t &dailyMark);
101 #endif // SUPPORT_TRAFFIC_STATISTIC
102 
103 private:
104     bool Init();
105     void GetDumpMessage(std::string &message);
106     void MergeTrafficStats(std::vector<NetStatsInfoSequence> &statsInfoSequences, const NetStatsInfo &info,
107                            uint32_t currentTime);
108     bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames);
109     std::unordered_map<uint32_t, SampleBundleInfo> GetSampleBundleInfosForActiveUser();
110     SampleBundleInfo GetSampleBundleInfoForUid(uint32_t uid);
111     void RefreshUidStatsFlag(uint64_t delay);
112     void RegisterCommonEvent();
113     bool CommonEventPackageAdded(uint32_t uid);
114     bool CommonEventPackageRemoved(uint32_t uid);
115     void FilterTrafficStatsByNetwork(std::vector<NetStatsInfo> &allInfo,
116         std::unordered_map<uint32_t, NetStatsInfo> &infos,
117         const std::string ident, uint32_t startTime, uint32_t endTime);
118     void MergeTrafficStatsByAccount(std::vector<NetStatsInfo> &infos);
119     void FilterTrafficStatsByUidNetwork(std::vector<NetStatsInfo> &allInfo, std::vector<NetStatsInfoSequence> &infos,
120         const uint32_t uid, const std::string ident, uint32_t startTime, uint32_t endTime);
121     int32_t CheckNetManagerAvailable();
122 #ifdef SUPPORT_NETWORK_SHARE
123     bool IsSharingOn();
124     void GetSharingStats(std::vector<NetStatsInfo> &sharingStats, uint32_t endtime);
125 #endif
126 #ifdef SUPPORT_TRAFFIC_STATISTIC
127     void UpdateBpfMapTimer();
128     bool CommonEventSimStateChanged(int32_t slotId, int32_t simState);
129     bool CommonEventSimStateChangedFfrt(int32_t slotId, int32_t simState);
130     bool CellularDataStateChangedFfrt(int32_t slotId, int32_t dataState);
131     bool CommonEventCellularDataStateChanged(int32_t slotId, int32_t dataState);
132     int32_t GetAllUsedTrafficStatsByNetwork(const sptr<NetStatsNetwork> &network, uint64_t &allUsedTraffic);
133     void UpdateBpfMap(int32_t simId);
134     void SetTrafficMapMaxValue();
135     void SetTrafficMapMaxValue(int32_t slotId);
136     void StartTrafficOvserver();
137     void StopTrafficOvserver();
138     bool GetNotifyStats(int32_t simId, uint8_t flag);
139     bool GetMonAlertStatus(int32_t simId);
140     bool GetMonNotifyStatus(int32_t simId);
141     bool GetDayNotifyStatus(int32_t simId);
142     void DealMonAlert(int32_t simId, bool isDaulCard);
143     void DealMonNotification(int32_t simId, bool isDaulCard);
144     void DealDayNotification(int32_t simId, bool isDaulCard);
145     void DealNotificaiton(int32_t simId, uint8_t flag);
146     bool IsMobileDataEnabled();
147     void UpdateTrafficLimitDate(int32_t simId);
148     void UpdateNetStatsToMapFromDB(int32_t simId);
149     bool CalculateTrafficAvailable(int32_t simId, uint64_t &monthlyAvailable,
150                                    uint64_t &monthlyMarkAvailable, uint64_t &dailyMarkAvailable);
151     int32_t UpdataSettingsdataFfrt(int32_t simId, uint8_t flag, uint64_t value);
152     void ClearTrafficMapBySlotId(int32_t slotId, uint64_t ifIndex);
153     bool IsSameStateInTwoMap(int32_t simId);
154     void DeleteSimIdInTwoMap(int32_t simId);
155     void AddSimIdInTwoMap(int32_t simId, uint64_t ifIndex);
156     void PrintTrafficBpfMapInfo(int32_t slotId);
157     void PrintTrafficSettingsMapInfo(int32_t simId);
158     void UpdateCurActiviteSimChanged(int32_t simId, uint64_t ifIndex);
159 #endif // SUPPORT_TRAFFIC_STATISTIC
160     void StartSysTimer();
161     void StopSysTimer();
162     int32_t ModifySysTimer();
163     void RegisterCommonTelephonyEvent();
164     void RegisterCommonTimeEvent();
165     void RegisterCommonNetStatusEvent();
166     int32_t UpdateStatsDataInner();
167     int32_t GetHistoryData(std::vector<NetStatsInfo> &infos, std::string ident,
168                            uint32_t uid, uint32_t start, uint32_t end);
169     void DeleteTrafficStatsByAccount(std::vector<NetStatsInfoSequence> &infos, uint32_t uid);
170     void InitPrivateUserId();
171     bool UpdateNetStatusMap(uint8_t type, uint8_t value);
172     void UpdateNetStatusMapCellular(int32_t dataState);
173 
174 private:
175     enum ServiceRunningState {
176         STATE_STOPPED = 0,
177         STATE_RUNNING,
178     };
179 
180     bool registerToService_;
181     ServiceRunningState state_;
182     std::shared_ptr<NetStatsCallback> netStatsCallback_ = nullptr;
183     std::shared_ptr<NetStatsListener> subscriber_ = nullptr;
184     std::unique_ptr<NetStatsCached> netStatsCached_ = nullptr;
185     uint64_t netStatsSysTimerId_ = 0;
186     std::shared_ptr<NetStatsAccountSubscriber> accountSubscriber_ = nullptr;
187     int32_t defaultUserId_ = 0;
188 
189 #ifdef SUPPORT_TRAFFIC_STATISTIC
190     uint64_t curIfIndex_ = UINT64_MAX;
191     std::atomic_bool isWifiConnected_ = false;
192     std::map<int32_t, std::pair<ObserverPtr, SettingsInfoPtr>> settingsTrafficMap_;
193     std::map<int32_t, uint64_t> simIdToIfIndexMap_;
194     std::unique_ptr<FfrtTimer> trafficTimer_ = nullptr;
195     sptr<TrafficObserver> trafficObserver_ = nullptr;
196     sptr<NetsysControllerCallback> netsysControllerObserver_ = nullptr;
197     SafeMap<std::string, std::string> ifaceNameIdentMap_;
198     std::shared_ptr<TrafficLimitDialog> dialog_ = nullptr;
199     std::shared_ptr<ffrt::queue> trafficPlanFfrtQueue_ = nullptr;
200 #endif // SUPPORT_TRAFFIC_STATISTIC
201     std::mutex timerMutex_;
202 };
203 
204 #ifdef SUPPORT_TRAFFIC_STATISTIC
205 class TrafficObserver : public NetsysNative::NetsysTrafficCallbackStub {
206 public:
207     TrafficObserver();
208     ~TrafficObserver() override;
209     int32_t OnExceedTrafficLimits(int8_t &flag) override;
210 };
211 #endif // SUPPORT_TRAFFIC_STATISTIC
212 } // namespace NetManagerStandard
213 } // namespace OHOS
214 #endif // NET_STATS_SERVICE_H
215