• 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 "i_net_stats_service.h"
25 #include "net_stats_constants.h"
26 #include "net_stats_info.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 class NetStatsClient : public Singleton<NetStatsClient> {
31 public:
32     NetStatsClient();
33     ~NetStatsClient();
34 
35     /**
36      * Register network card traffic monitoring
37      *
38      * @param callback callback function
39      * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
40      * @permission ohos.permission.CONNECTIVITY_INTERNAL
41      * @systemapi Hide this for inner system use.
42      */
43     int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback);
44 
45     /**
46      * Unregister network card traffic monitoring
47      *
48      * @param callback callback function
49      * @return Returns 0 success. Otherwise fail.
50      * @permission ohos.permission.CONNECTIVITY_INTERNAL
51      * @systemapi Hide this for inner system use.
52      */
53     int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback);
54 
55     /**
56      * Get the received traffic of the network card
57      *
58      * @param stats Traffic (bytes)
59      * @param interfaceName network card name
60      * @return Returns 0 success. Otherwise fail.
61      * @permission ohos.permission.CONNECTIVITY_INTERNAL
62      * @systemapi Hide this for inner system use.
63      */
64     int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName);
65 
66     /**
67      * Get the send traffic of the network card
68      *
69      * @param stats Traffic (bytes)
70      * @param interfaceName network card name
71      * @return Returns 0 success. Otherwise fail.
72      * @permission ohos.permission.CONNECTIVITY_INTERNAL
73      * @systemapi Hide this for inner system use.
74      */
75     int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName);
76 
77     /**
78      * Get received traffic from the cell
79      *
80      * @param stats Traffic (bytes)
81      * @return Returns 0 success. Otherwise fail.
82      * @permission ohos.permission.CONNECTIVITY_INTERNAL
83      * @systemapi Hide this for inner system use.
84      */
85     int32_t GetCellularRxBytes(uint64_t &stats);
86 
87     /**
88      * Get send traffic from the cell
89      *
90      * @param stats Traffic (bytes)
91      * @return Returns 0 success. Otherwise fail.
92      * @permission ohos.permission.CONNECTIVITY_INTERNAL
93      * @systemapi Hide this for inner system use.
94      */
95     int32_t GetCellularTxBytes(uint64_t &stats);
96 
97     /**
98      * Get all received traffic
99      *
100      * @param stats Traffic (bytes)
101      * @return Returns 0 success. Otherwise fail.
102      * @permission ohos.permission.CONNECTIVITY_INTERNAL
103      * @systemapi Hide this for inner system use.
104      */
105     int32_t GetAllRxBytes(uint64_t &stats);
106 
107     /**
108      * Get all send traffic
109      *
110      * @param stats Traffic (bytes)
111      * @return Returns 0 success. Otherwise fail.
112      * @permission ohos.permission.CONNECTIVITY_INTERNAL
113      * @systemapi Hide this for inner system use.
114      */
115     int32_t GetAllTxBytes(uint64_t &stats);
116 
117     /**
118      * Get the received traffic for the specified UID of application
119      *
120      * @param stats Traffic (bytes)
121      * @param uid The specified UID of application.
122      * @return Returns 0 success. Otherwise fail.
123      * @permission ohos.permission.CONNECTIVITY_INTERNAL
124      * @systemapi Hide this for inner system use.
125      */
126     int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid);
127 
128     /**
129      * Get the send traffic for the specified UID of application
130      *
131      * @param stats Traffic (bytes)
132      * @param uid The specified UID of application.
133      * @return Returns 0 success. Otherwise fail.
134      * @permission ohos.permission.CONNECTIVITY_INTERNAL
135      * @systemapi Hide this for inner system use.
136      */
137     int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid);
138 
139     /**
140      * Get traffic details for all network cards
141      *
142      * @param infos all network cards informations
143      * @return Returns 0 success. Otherwise fail.
144      * @permission ohos.permission.CONNECTIVITY_INTERNAL
145      * @systemapi Hide this for inner system use.
146      */
147     int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos);
148 
149     /**
150      * Get the historical traffic details of the specified network card
151      *
152      * @param iface network cards name
153      * @param start start time
154      * @param end end time
155      * @param statsInfo traffic information
156      * @return Returns 0 success. Otherwise fail.
157      * @permission ohos.permission.CONNECTIVITY_INTERNAL
158      * @systemapi Hide this for inner system use.
159      */
160     int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end, NetStatsInfo &statsInfo);
161 
162     /**
163      * Get the historical traffic details from UID of application.
164      *
165      * @param iface network cards name
166      * @param uid The specified UID of application.
167      * @param start start time
168      * @param end end time
169      * @param statsInfo traffic information
170      * @return Returns 0 success. Otherwise fail.
171      * @permission ohos.permission.CONNECTIVITY_INTERNAL
172      * @systemapi Hide this for inner system use.
173      */
174     int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
175                               NetStatsInfo &statsInfo);
176 
177     /**
178      * Update the traffic of the specified network card
179      *
180      * @param iface network cards name
181      * @param start start time
182      * @param end end time
183      * @param stats Traffic (bytes)
184      * @return Returns 0 success. Otherwise fail.
185      * @permission ohos.permission.CONNECTIVITY_INTERNAL
186      * @systemapi Hide this for inner system use.
187      */
188     int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end, const NetStatsInfo &stats);
189 
190     /**
191      * Update network card traffic data
192      *
193      * @return Returns 0 success. Otherwise fail.
194      * @permission ohos.permission.CONNECTIVITY_INTERNAL
195      * @systemapi Hide this for inner system use.
196      */
197     int32_t UpdateStatsData();
198 
199     /**
200      * Clear network card traffic
201      *
202      * @return Returns 0 success. Otherwise fail.
203      * @permission ohos.permission.CONNECTIVITY_INTERNAL
204      * @systemapi Hide this for inner system use.
205      */
206     int32_t ResetFactory();
207 
208     /**
209      * Get Sockfd RxBytes
210      *
211      * @param stats stats
212      * @param sockfd sockfd
213      * @return Returns 0 success. Otherwise fail.
214      * @permission ohos.permission.CONNECTIVITY_INTERNAL
215      * @systemapi Hide this for inner system use.
216      */
217     int32_t GetSockfdRxBytes(uint64_t &stats, int32_t sockfd);
218 
219     /**
220      * Get Sockfd TxBytes
221      *
222      * @param stats stats
223      * @param sockfd sockfd
224      * @return Returns 0 success. Otherwise fail.
225      * @permission ohos.permission.CONNECTIVITY_INTERNAL
226      * @systemapi Hide this for inner system use.
227      */
228     int32_t GetSockfdTxBytes(uint64_t &stats, int32_t sockfd);
229 
230 private:
231     class NetStatsDeathRecipient : public IRemoteObject::DeathRecipient {
232     public:
NetStatsDeathRecipient(NetStatsClient & client)233         explicit NetStatsDeathRecipient(NetStatsClient &client) : client_(client) {}
234         ~NetStatsDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)235         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
236         {
237             client_.OnRemoteDied(remote);
238         }
239 
240     private:
241         NetStatsClient &client_;
242     };
243 
244 private:
245     sptr<INetStatsService> GetProxy();
246     void RecoverCallback();
247     void OnRemoteDied(const wptr<IRemoteObject> &remote);
248 
249 private:
250     std::mutex mutex_;
251     sptr<INetStatsService> netStatsService_;
252     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
253     sptr<INetStatsCallback> callback_;
254 };
255 } // namespace NetManagerStandard
256 } // namespace OHOS
257 #endif // NET_STATS_CLIENT_H
258