• 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 #include "net_stats_client.h"
17 #include <thread>
18 
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 #include "net_all_capabilities.h"
23 #include "net_manager_constants.h"
24 #include "net_mgr_log_wrapper.h"
25 #include "sys/socket.h"
26 #include "net_stats_service_proxy.h"
27 
28 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
29 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
NetStatsClient()33 NetStatsClient::NetStatsClient() : netStatsService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
34 
~NetStatsClient()35 NetStatsClient::~NetStatsClient()
36 {
37     NETMGR_LOG_I("~NetStatsClient : Destroy NetStatsClient");
38     sptr<INetStatsService> proxy = GetProxy();
39     if (proxy == nullptr) {
40         return;
41     }
42 
43     auto serviceRemote = proxy->AsObject();
44     if (serviceRemote == nullptr) {
45         return;
46     }
47 
48     serviceRemote->RemoveDeathRecipient(deathRecipient_);
49 }
50 
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)51 int32_t NetStatsClient::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
52 {
53     NETMGR_LOG_D("RegisterNetStatsCallback client in");
54     sptr<INetStatsService> proxy = GetProxy();
55     if (proxy == nullptr) {
56         NETMGR_LOG_E("proxy is nullptr");
57         return NETMANAGER_ERR_GET_PROXY_FAIL;
58     }
59     int32_t ret = proxy->RegisterNetStatsCallback(callback);
60     if (ret == NETMANAGER_SUCCESS) {
61         NETMGR_LOG_D("RegisterNetStatsCallback success, save callback");
62         callback_ = callback;
63     }
64 
65     return ret;
66 }
67 
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)68 int32_t NetStatsClient::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
69 {
70     sptr<INetStatsService> proxy = GetProxy();
71     if (proxy == nullptr) {
72         NETMGR_LOG_E("proxy is nullptr");
73         return NETMANAGER_ERR_GET_PROXY_FAIL;
74     }
75     int32_t ret = proxy->UnregisterNetStatsCallback(callback);
76     if (ret == NETMANAGER_SUCCESS) {
77         NETMGR_LOG_D("UnRegisterNetStatsCallback success, delete callback");
78         callback_ = nullptr;
79     }
80 
81     return ret;
82 }
83 
GetProxy()84 sptr<INetStatsService> NetStatsClient::GetProxy()
85 {
86     std::lock_guard lock(mutex_);
87 
88     if (netStatsService_ != nullptr) {
89         NETMGR_LOG_D("get proxy is ok");
90         return netStatsService_;
91     }
92 
93     NETMGR_LOG_D("execute GetSystemAbilityManager");
94     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95     if (sam == nullptr) {
96         NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed");
97         return nullptr;
98     }
99 
100     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID);
101     if (remote == nullptr) {
102         NETMGR_LOG_E("get Remote service failed");
103         return nullptr;
104     }
105 
106     deathRecipient_ = new (std::nothrow) NetStatsDeathRecipient(*this);
107     if (deathRecipient_ == nullptr) {
108         NETMGR_LOG_E("get deathRecipient_ failed");
109         return nullptr;
110     }
111     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
112         NETMGR_LOG_E("add death recipient failed");
113         return nullptr;
114     }
115 
116     netStatsService_ = iface_cast<INetStatsService>(remote);
117     if (netStatsService_ == nullptr) {
118         NETMGR_LOG_E("get Remote service proxy failed");
119         return nullptr;
120     }
121     return netStatsService_;
122 }
123 
RecoverCallback()124 void NetStatsClient::RecoverCallback()
125 {
126     uint32_t count = 0;
127     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
128         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
129         count++;
130     }
131     auto proxy = GetProxy();
132     NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
133     if (proxy != nullptr && callback_ != nullptr) {
134         int32_t ret = proxy->RegisterNetStatsCallback(callback_);
135         NETMGR_LOG_D("Register result %{public}d", ret);
136     }
137 }
138 
OnRemoteDied(const wptr<IRemoteObject> & remote)139 void NetStatsClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
140 {
141     NETMGR_LOG_D("on remote died");
142     if (remote == nullptr) {
143         NETMGR_LOG_E("remote object is nullptr");
144         return;
145     }
146 
147     {
148         std::lock_guard lock(mutex_);
149         if (netStatsService_ == nullptr) {
150             NETMGR_LOG_E("NetConnService_ is nullptr");
151             return;
152         }
153 
154         sptr<IRemoteObject> local = netStatsService_->AsObject();
155         if (local != remote.promote()) {
156             NETMGR_LOG_E("proxy and stub is not same remote object");
157             return;
158         }
159 
160         local->RemoveDeathRecipient(deathRecipient_);
161         netStatsService_ = nullptr;
162     }
163 
164     if (callback_ != nullptr) {
165         NETMGR_LOG_D("on remote died recover callback");
166         std::thread t([sp = shared_from_this()]() { sp->RecoverCallback(); });
167         std::string threadName = "nestatsRecoverCallback";
168         pthread_setname_np(t.native_handle(), threadName.c_str());
169         t.detach();
170     }
171 }
172 
GetIfaceRxBytes(uint64_t & stats,const std::string & interfaceName)173 int32_t NetStatsClient::GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName)
174 {
175     sptr<INetStatsService> proxy = GetProxy();
176     if (proxy == nullptr) {
177         NETMGR_LOG_E("proxy is nullptr");
178         return NETMANAGER_ERR_GET_PROXY_FAIL;
179     }
180     return proxy->GetIfaceRxBytes(stats, interfaceName);
181 }
182 
GetIfaceTxBytes(uint64_t & stats,const std::string & interfaceName)183 int32_t NetStatsClient::GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName)
184 {
185     sptr<INetStatsService> proxy = GetProxy();
186     if (proxy == nullptr) {
187         NETMGR_LOG_E("proxy is nullptr");
188         return NETMANAGER_ERR_GET_PROXY_FAIL;
189     }
190     return proxy->GetIfaceTxBytes(stats, interfaceName);
191 }
192 
GetCellularRxBytes(uint64_t & stats)193 int32_t NetStatsClient::GetCellularRxBytes(uint64_t &stats)
194 {
195     sptr<INetStatsService> proxy = GetProxy();
196     if (proxy == nullptr) {
197         NETMGR_LOG_E("proxy is nullptr");
198         return NETMANAGER_ERR_GET_PROXY_FAIL;
199     }
200     return proxy->GetCellularRxBytes(stats);
201 }
202 
GetCellularTxBytes(uint64_t & stats)203 int32_t NetStatsClient::GetCellularTxBytes(uint64_t &stats)
204 {
205     sptr<INetStatsService> proxy = GetProxy();
206     if (proxy == nullptr) {
207         NETMGR_LOG_E("proxy is nullptr");
208         return NETMANAGER_ERR_GET_PROXY_FAIL;
209     }
210     return proxy->GetCellularTxBytes(stats);
211 }
212 
GetAllRxBytes(uint64_t & stats)213 int32_t NetStatsClient::GetAllRxBytes(uint64_t &stats)
214 {
215     sptr<INetStatsService> proxy = GetProxy();
216     if (proxy == nullptr) {
217         NETMGR_LOG_E("proxy is nullptr");
218         return NETMANAGER_ERR_GET_PROXY_FAIL;
219     }
220     return proxy->GetAllRxBytes(stats);
221 }
222 
GetAllTxBytes(uint64_t & stats)223 int32_t NetStatsClient::GetAllTxBytes(uint64_t &stats)
224 {
225     sptr<INetStatsService> proxy = GetProxy();
226     if (proxy == nullptr) {
227         NETMGR_LOG_E("proxy is nullptr");
228         return NETMANAGER_ERR_GET_PROXY_FAIL;
229     }
230     return proxy->GetAllTxBytes(stats);
231 }
232 
GetUidRxBytes(uint64_t & stats,uint32_t uid)233 int32_t NetStatsClient::GetUidRxBytes(uint64_t &stats, uint32_t uid)
234 {
235     sptr<INetStatsService> proxy = GetProxy();
236     if (proxy == nullptr) {
237         NETMGR_LOG_E("proxy is nullptr");
238         return NETMANAGER_ERR_GET_PROXY_FAIL;
239     }
240     return proxy->GetUidRxBytes(stats, uid);
241 }
242 
GetUidTxBytes(uint64_t & stats,uint32_t uid)243 int32_t NetStatsClient::GetUidTxBytes(uint64_t &stats, uint32_t uid)
244 {
245     sptr<INetStatsService> proxy = GetProxy();
246     if (proxy == nullptr) {
247         NETMGR_LOG_E("proxy is nullptr");
248         return NETMANAGER_ERR_GET_PROXY_FAIL;
249     }
250     return proxy->GetUidTxBytes(stats, uid);
251 }
252 
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)253 int32_t NetStatsClient::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
254                                             NetStatsInfo &statsInfo)
255 {
256     sptr<INetStatsService> proxy = GetProxy();
257     if (proxy == nullptr) {
258         NETMGR_LOG_E("proxy is nullptr");
259         return NETMANAGER_ERR_GET_PROXY_FAIL;
260     }
261     return proxy->GetIfaceStatsDetail(iface, start, end, statsInfo);
262 }
263 
GetUidStatsDetail(const std::string & iface,uint32_t uid,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)264 int32_t NetStatsClient::GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
265                                           NetStatsInfo &statsInfo)
266 {
267     sptr<INetStatsService> proxy = GetProxy();
268     if (proxy == nullptr) {
269         NETMGR_LOG_E("proxy is nullptr");
270         return NETMANAGER_ERR_GET_PROXY_FAIL;
271     }
272     return proxy->GetUidStatsDetail(iface, uid, start, end, statsInfo);
273 }
274 
UpdateIfacesStats(const std::string & iface,uint64_t start,uint64_t end,const NetStatsInfo & stats)275 int32_t NetStatsClient::UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
276                                           const NetStatsInfo &stats)
277 {
278     sptr<INetStatsService> proxy = GetProxy();
279     if (proxy == nullptr) {
280         NETMGR_LOG_E("proxy is nullptr");
281         return NETMANAGER_ERR_GET_PROXY_FAIL;
282     }
283     return proxy->UpdateIfacesStats(iface, start, end, stats);
284 }
285 
UpdateStatsData()286 int32_t NetStatsClient::UpdateStatsData()
287 {
288     sptr<INetStatsService> proxy = GetProxy();
289     if (proxy == nullptr) {
290         NETMGR_LOG_E("proxy is nullptr");
291         return NETMANAGER_ERR_GET_PROXY_FAIL;
292     }
293     return proxy->UpdateStatsData();
294 }
295 
ResetFactory()296 int32_t NetStatsClient::ResetFactory()
297 {
298     sptr<INetStatsService> proxy = GetProxy();
299     if (proxy == nullptr) {
300         NETMGR_LOG_E("proxy is nullptr");
301         return NETMANAGER_ERR_GET_PROXY_FAIL;
302     }
303     return proxy->ResetFactory();
304 }
305 
GetAllStatsInfo(std::vector<NetStatsInfo> & infos)306 int32_t NetStatsClient::GetAllStatsInfo(std::vector<NetStatsInfo> &infos)
307 {
308     sptr<INetStatsService> proxy = GetProxy();
309     if (proxy == nullptr) {
310         NETMGR_LOG_E("proxy is nullptr");
311         return NETMANAGER_ERR_GET_PROXY_FAIL;
312     }
313     return proxy->GetAllStatsInfo(infos);
314 }
315 
GetAllContainerStatsInfo(std::vector<NetStatsInfo> & infos)316 int32_t NetStatsClient::GetAllContainerStatsInfo(std::vector<NetStatsInfo> &infos)
317 {
318     sptr<INetStatsService> proxy = GetProxy();
319     if (proxy == nullptr) {
320         NETMGR_LOG_E("proxy is nullptr");
321         return NETMANAGER_ERR_GET_PROXY_FAIL;
322     }
323     return proxy->GetAllSimStatsInfo(infos);
324 }
325 
GetTrafficStatsByNetwork(std::unordered_map<uint32_t,NetStatsInfo> & infos,const sptr<NetStatsNetwork> & network)326 int32_t NetStatsClient::GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos,
327                                                  const sptr<NetStatsNetwork> &network)
328 {
329     sptr<INetStatsService> proxy = GetProxy();
330     if (proxy == nullptr) {
331         NETMGR_LOG_E("proxy is nullptr");
332         return NETMANAGER_ERR_GET_PROXY_FAIL;
333     }
334     if (network == nullptr) {
335         NETMGR_LOG_E("network is nullptr");
336         return NETMANAGER_ERR_INVALID_PARAMETER;
337     }
338     if (network->startTime_ > network->endTime_) {
339         NETMGR_LOG_E("network is invalid");
340         return NETMANAGER_ERR_INVALID_PARAMETER;
341     }
342     if (network->type_ > static_cast<uint32_t>(BEARER_DEFAULT)) {
343         NETMGR_LOG_E("network is invalid");
344         return NETMANAGER_ERR_INVALID_PARAMETER;
345     }
346     return proxy->GetTrafficStatsByNetwork(infos, *network);
347 }
348 
GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> & infos,uint32_t uid,const sptr<NetStatsNetwork> & network)349 int32_t NetStatsClient::GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid,
350                                                     const sptr<NetStatsNetwork> &network)
351 {
352     sptr<INetStatsService> proxy = GetProxy();
353     if (proxy == nullptr) {
354         NETMGR_LOG_E("proxy is nullptr");
355         return NETMANAGER_ERR_GET_PROXY_FAIL;
356     }
357     if (network == nullptr) {
358         NETMGR_LOG_E("network is nullptr");
359         return NETMANAGER_ERR_INVALID_PARAMETER;
360     }
361     if (network->startTime_ > network->endTime_) {
362         NETMGR_LOG_E("network is invalid");
363         return NETMANAGER_ERR_INVALID_PARAMETER;
364     }
365     if (network->type_ > static_cast<uint32_t>(BEARER_DEFAULT)) {
366         NETMGR_LOG_E("network is invalid");
367         return NETMANAGER_ERR_INVALID_PARAMETER;
368     }
369     return proxy->GetTrafficStatsByUidNetwork(infos, uid, *network);
370 }
371 
SetAppStats(const PushStatsInfo & info)372 int32_t NetStatsClient::SetAppStats(const PushStatsInfo &info)
373 {
374     sptr<INetStatsService> proxy = GetProxy();
375     if (proxy == nullptr) {
376         NETMGR_LOG_E("proxy is nullptr");
377         return NETMANAGER_ERR_GET_PROXY_FAIL;
378     }
379     return proxy->SetAppStats(info);
380 }
381 
SaveSharingTraffic(const NetStatsInfo & infos)382 int32_t NetStatsClient::SaveSharingTraffic(const NetStatsInfo &infos)
383 {
384     sptr<INetStatsService> proxy = GetProxy();
385     if (proxy == nullptr) {
386         NETMGR_LOG_E("proxy is nullptr");
387         return NETMANAGER_ERR_GET_PROXY_FAIL;
388     }
389     return proxy->SaveSharingTraffic(infos);
390 }
391 
GetSockfdRxBytes(uint64_t & stats,int32_t sockfd)392 int32_t NetStatsClient::GetSockfdRxBytes(uint64_t &stats, int32_t sockfd)
393 {
394     if (sockfd <= 0) {
395         NETMGR_LOG_E("sockfd is invalid");
396         return NETMANAGER_ERR_INVALID_PARAMETER;
397     }
398 
399     sptr<INetStatsService> proxy = GetProxy();
400     if (proxy == nullptr) {
401         NETMGR_LOG_E("proxy is nullptr");
402         return NETMANAGER_ERR_GET_PROXY_FAIL;
403     }
404 
405     uint64_t optrval = 0;
406     uint32_t optlen = sizeof(optrval);
407     if (getsockopt(sockfd, SOL_SOCKET, SO_COOKIE, &optrval, &optlen) == -1) {
408         NETMGR_LOG_E("getsockopt error");
409         return NETMANAGER_ERR_OPERATION_FAILED;
410     }
411 
412     return proxy->GetCookieRxBytes(stats, optrval);
413 }
414 
GetSockfdTxBytes(uint64_t & stats,int32_t sockfd)415 int32_t NetStatsClient::GetSockfdTxBytes(uint64_t &stats, int32_t sockfd)
416 {
417     if (sockfd <= 0) {
418         NETMGR_LOG_E("sockfd is invalid");
419         return NETMANAGER_ERR_INVALID_PARAMETER;
420     }
421 
422     sptr<INetStatsService> proxy = GetProxy();
423     if (proxy == nullptr) {
424         NETMGR_LOG_E("proxy is nullptr");
425         return NETMANAGER_ERR_GET_PROXY_FAIL;
426     }
427 
428     uint64_t optrval = 0;
429     uint32_t optlen = sizeof(optrval);
430     if (getsockopt(sockfd, SOL_SOCKET, SO_COOKIE, &optrval, &optlen) == -1) {
431         NETMGR_LOG_E("getsockopt error");
432         return NETMANAGER_ERR_OPERATION_FAILED;
433     }
434 
435     return proxy->GetCookieTxBytes(stats, optrval);
436 }
437 
GetUidTxBytesEx(uint64_t * stats,uint32_t uid)438 extern "C" int32_t GetUidTxBytesEx(uint64_t *stats, uint32_t uid)
439 {
440     NETMGR_LOG_D("GetUidTxBytesEx in");
441     if (!stats) {
442         NETMGR_LOG_E("stats is null");
443         return -1;
444     }
445     return DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidTxBytes(*stats, uid);
446 }
447 
GetUidRxBytesEx(uint64_t * stats,uint32_t uid)448 extern "C" int32_t GetUidRxBytesEx(uint64_t *stats, uint32_t uid)
449 {
450     NETMGR_LOG_D("GetUidRxBytesEx in");
451     if (!stats) {
452         NETMGR_LOG_E("stats is null");
453         return -1;
454     }
455     return DelayedSingleton<NetManagerStandard::NetStatsClient>::GetInstance()->GetUidRxBytes(*stats, uid);
456 }
457 } // namespace NetManagerStandard
458 } // namespace OHOS
459