• 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_STATS_CLIENT_H
17 #define NET_STATS_CLIENT_H
18 
19 #include <string>
20 
21 #include "parcel.h"
22 #include "singleton.h"
23 
24 #include "inet_stats_service.h"
25 #include "net_push_stats_info.h"
26 #include "net_stats_constants.h"
27 #include "net_stats_info.h"
28 #include "net_stats_info_sequence.h"
29 #include "net_stats_network.h"
30 #include "network_sharing.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 class NetStatsClient : public std::enable_shared_from_this<NetStatsClient> {
35 public:
36     NetStatsClient();
37     ~NetStatsClient();
GetInstance()38     static NetStatsClient& GetInstance()
39     {
40         static std::shared_ptr<NetStatsClient> instance = std::make_shared<NetStatsClient>();
41         return *instance;
42     }
43 
44     /**
45      * Register network card traffic monitoring
46      *
47      * @param callback callback function
48      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
49      * @permission ohos.permission.CONNECTIVITY_INTERNAL
50      * @systemapi Hide this for inner system use.
51      */
52     int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback);
53 
54     /**
55      * Unregister network card traffic monitoring
56      *
57      * @param callback callback function
58      * @return Returns 0 success. Otherwise fail.
59      * @permission ohos.permission.CONNECTIVITY_INTERNAL
60      * @systemapi Hide this for inner system use.
61      */
62     int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback);
63 
64     /**
65      * Get the received traffic of the network card
66      *
67      * @param stats Traffic (bytes)
68      * @param interfaceName network card name
69      * @return Returns 0 success. Otherwise fail.
70      * @permission ohos.permission.CONNECTIVITY_INTERNAL
71      * @systemapi Hide this for inner system use.
72      */
73     int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName);
74 
75     /**
76      * Get the send traffic of the network card
77      *
78      * @param stats Traffic (bytes)
79      * @param interfaceName network card name
80      * @return Returns 0 success. Otherwise fail.
81      * @permission ohos.permission.CONNECTIVITY_INTERNAL
82      * @systemapi Hide this for inner system use.
83      */
84     int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName);
85 
86     /**
87      * Get received traffic from the cell
88      *
89      * @param stats Traffic (bytes)
90      * @return Returns 0 success. Otherwise fail.
91      * @permission ohos.permission.CONNECTIVITY_INTERNAL
92      * @systemapi Hide this for inner system use.
93      */
94     int32_t GetCellularRxBytes(uint64_t &stats);
95 
96     /**
97      * Get send traffic from the cell
98      *
99      * @param stats Traffic (bytes)
100      * @return Returns 0 success. Otherwise fail.
101      * @permission ohos.permission.CONNECTIVITY_INTERNAL
102      * @systemapi Hide this for inner system use.
103      */
104     int32_t GetCellularTxBytes(uint64_t &stats);
105 
106     /**
107      * Get all received traffic
108      *
109      * @param stats Traffic (bytes)
110      * @return Returns 0 success. Otherwise fail.
111      * @permission ohos.permission.CONNECTIVITY_INTERNAL
112      * @systemapi Hide this for inner system use.
113      */
114     int32_t GetAllRxBytes(uint64_t &stats);
115 
116     /**
117      * Get all send traffic
118      *
119      * @param stats Traffic (bytes)
120      * @return Returns 0 success. Otherwise fail.
121      * @permission ohos.permission.CONNECTIVITY_INTERNAL
122      * @systemapi Hide this for inner system use.
123      */
124     int32_t GetAllTxBytes(uint64_t &stats);
125 
126     /**
127      * Get the received traffic for the specified UID of application
128      *
129      * @param stats Traffic (bytes)
130      * @param uid The specified UID of application.
131      * @return Returns 0 success. Otherwise fail.
132      * @permission ohos.permission.CONNECTIVITY_INTERNAL
133      * @systemapi Hide this for inner system use.
134      */
135     int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid);
136 
137     /**
138      * Get the send traffic for the specified UID of application
139      *
140      * @param stats Traffic (bytes)
141      * @param uid The specified UID of application.
142      * @return Returns 0 success. Otherwise fail.
143      * @permission ohos.permission.CONNECTIVITY_INTERNAL
144      * @systemapi Hide this for inner system use.
145      */
146     int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid);
147 
148     /**
149      * Get traffic details for all network cards
150      *
151      * @param infos all network cards informations
152      * @return Returns 0 success. Otherwise fail.
153      * @permission ohos.permission.CONNECTIVITY_INTERNAL
154      * @systemapi Hide this for inner system use.
155      */
156     int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos);
157 
158     /**
159      * Get traffic details for all network cards with container application
160      *
161      * @param infos all network cards informations
162      * @return Returns 0 success. Otherwise fail.
163      * @permission ohos.permission.CONNECTIVITY_INTERNAL
164      * @systemapi Hide this for inner system use.
165      */
166     int32_t GetAllContainerStatsInfo(std::vector<NetStatsInfo> &infos);
167 
168     /**
169      * Get traffic of all application with the specified network cards
170      *
171      * @param infos traffic of all application
172      * @param network the network of traffic stats
173      * @return Returns 0 success. Otherwise fail.
174      * @permission ohos.permission.CONNECTIVITY_INTERNAL
175      * @systemapi Hide this for inner system use.
176      */
177     int32_t GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos,
178                                      const sptr<NetStatsNetwork> &network);
179 
180     /**
181      * Get traffic of the specified application with the specified network cards
182      *
183      * @param infos traffic of all application
184      * @param uid the id of the specified application
185      * @param network the network of traffic stats
186      * @return Returns 0 success. Otherwise fail.
187      * @permission ohos.permission.CONNECTIVITY_INTERNAL
188      * @systemapi Hide this for inner system use.
189      */
190     int32_t GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid,
191                                         const sptr<NetStatsNetwork> &network);
192 
193     /**
194      * Set traffic stats of the specified application
195      *
196      * @param info traffic of the application
197      * @return Returns 0 success. Otherwise fail.
198      * @permission ohos.permission.CONNECTIVITY_INTERNAL
199      * @systemapi Hide this for inner system use.
200      */
201     int32_t SetAppStats(const PushStatsInfo &info);
202 
203     /**
204      * set sharing traffic before hotspot stop
205      *
206      * @param info traffic of the cellular
207      * @return Returns 0 success. Otherwise fail.
208      * @permission ohos.permission.CONNECTIVITY_INTERNAL
209      * @systemapi Hide this for inner system use.
210      */
211     int32_t SaveSharingTraffic(const NetStatsInfo &infos);
212 
213     /**
214      * Get the historical traffic details of the specified network card
215      *
216      * @param iface network cards name
217      * @param start start time
218      * @param end end time
219      * @param statsInfo traffic information
220      * @return Returns 0 success. Otherwise fail.
221      * @permission ohos.permission.CONNECTIVITY_INTERNAL
222      * @systemapi Hide this for inner system use.
223      */
224     int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end, NetStatsInfo &statsInfo);
225 
226     /**
227      * Get the historical traffic details from UID of application.
228      *
229      * @param iface network cards name
230      * @param uid The specified UID of application.
231      * @param start start time
232      * @param end end time
233      * @param statsInfo traffic information
234      * @return Returns 0 success. Otherwise fail.
235      * @permission ohos.permission.CONNECTIVITY_INTERNAL
236      * @systemapi Hide this for inner system use.
237      */
238     int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
239                               NetStatsInfo &statsInfo);
240 
241     /**
242      * Update the traffic of the specified network card
243      *
244      * @param iface network cards name
245      * @param start start time
246      * @param end end time
247      * @param stats Traffic (bytes)
248      * @return Returns 0 success. Otherwise fail.
249      * @permission ohos.permission.CONNECTIVITY_INTERNAL
250      * @systemapi Hide this for inner system use.
251      */
252     int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end, const NetStatsInfo &stats);
253 
254     /**
255      * Update network card traffic data
256      *
257      * @return Returns 0 success. Otherwise fail.
258      * @permission ohos.permission.CONNECTIVITY_INTERNAL
259      * @systemapi Hide this for inner system use.
260      */
261     int32_t UpdateStatsData();
262 
263     /**
264      * Clear network card traffic
265      *
266      * @return Returns 0 success. Otherwise fail.
267      * @permission ohos.permission.CONNECTIVITY_INTERNAL
268      * @systemapi Hide this for inner system use.
269      */
270     int32_t ResetFactory();
271 
272     /**
273      * Get Sockfd RxBytes
274      *
275      * @param stats stats
276      * @param sockfd sockfd
277      * @return Returns 0 success. Otherwise fail.
278      * @permission ohos.permission.CONNECTIVITY_INTERNAL
279      * @systemapi Hide this for inner system use.
280      */
281     int32_t GetSockfdRxBytes(uint64_t &stats, int32_t sockfd);
282 
283     /**
284      * Get Sockfd TxBytes
285      *
286      * @param stats stats
287      * @param sockfd sockfd
288      * @return Returns 0 success. Otherwise fail.
289      * @permission ohos.permission.CONNECTIVITY_INTERNAL
290      * @systemapi Hide this for inner system use.
291      */
292     int32_t GetSockfdTxBytes(uint64_t &stats, int32_t sockfd);
293 
294 private:
295     class NetStatsDeathRecipient : public IRemoteObject::DeathRecipient {
296     public:
NetStatsDeathRecipient(NetStatsClient & client)297         explicit NetStatsDeathRecipient(NetStatsClient &client) : client_(client) {}
298         ~NetStatsDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)299         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
300         {
301             client_.OnRemoteDied(remote);
302         }
303 
304     private:
305         NetStatsClient &client_;
306     };
307 
308 private:
309     sptr<INetStatsService> GetProxy();
310     void RecoverCallback();
311     void OnRemoteDied(const wptr<IRemoteObject> &remote);
312 
313 private:
314     std::mutex mutex_;
315     sptr<INetStatsService> netStatsService_;
316     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
317     sptr<INetStatsCallback> callback_;
318 };
319 } // namespace NetManagerStandard
320 } // namespace OHOS
321 #endif // NET_STATS_CLIENT_H
322