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
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "net_manager_constants.h"
22 #include "net_mgr_log_wrapper.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
NetStatsClient()26 NetStatsClient::NetStatsClient() : netStatsService_(nullptr), deathRecipient_(nullptr) {}
27
28 NetStatsClient::~NetStatsClient() = default;
29
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)30 int32_t NetStatsClient::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
31 {
32 sptr<INetStatsService> proxy = GetProxy();
33 if (proxy == nullptr) {
34 NETMGR_LOG_E("proxy is nullptr");
35 return NETMANAGER_ERR_GET_PROXY_FAIL;
36 }
37
38 return proxy->RegisterNetStatsCallback(callback);
39 }
40
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)41 int32_t NetStatsClient::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
42 {
43 sptr<INetStatsService> proxy = GetProxy();
44 if (proxy == nullptr) {
45 NETMGR_LOG_E("proxy is nullptr");
46 return NETMANAGER_ERR_GET_PROXY_FAIL;
47 }
48
49 return proxy->UnregisterNetStatsCallback(callback);
50 }
51
GetProxy()52 sptr<INetStatsService> NetStatsClient::GetProxy()
53 {
54 std::lock_guard lock(mutex_);
55
56 if (netStatsService_ != nullptr) {
57 NETMGR_LOG_D("get proxy is ok");
58 return netStatsService_;
59 }
60
61 NETMGR_LOG_D("execute GetSystemAbilityManager");
62 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63 if (sam == nullptr) {
64 NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed");
65 return nullptr;
66 }
67
68 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID);
69 if (remote == nullptr) {
70 NETMGR_LOG_E("get Remote service failed");
71 return nullptr;
72 }
73
74 deathRecipient_ = new (std::nothrow) NetStatsDeathRecipient(*this);
75 if (deathRecipient_ == nullptr) {
76 NETMGR_LOG_E("get deathRecipient_ failed");
77 return nullptr;
78 }
79 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
80 NETMGR_LOG_E("add death recipient failed");
81 return nullptr;
82 }
83
84 netStatsService_ = iface_cast<INetStatsService>(remote);
85 if (netStatsService_ == nullptr) {
86 NETMGR_LOG_E("get Remote service proxy failed");
87 return nullptr;
88 }
89 return netStatsService_;
90 }
91
OnRemoteDied(const wptr<IRemoteObject> & remote)92 void NetStatsClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
93 {
94 NETMGR_LOG_D("on remote died");
95 if (remote == nullptr) {
96 NETMGR_LOG_E("remote object is nullptr");
97 return;
98 }
99
100 std::lock_guard lock(mutex_);
101 if (netStatsService_ == nullptr) {
102 NETMGR_LOG_E("NetConnService_ is nullptr");
103 return;
104 }
105
106 sptr<IRemoteObject> local = netStatsService_->AsObject();
107 if (local != remote.promote()) {
108 NETMGR_LOG_E("proxy and stub is not same remote object");
109 return;
110 }
111
112 local->RemoveDeathRecipient(deathRecipient_);
113 netStatsService_ = nullptr;
114 }
115
GetIfaceRxBytes(uint64_t & stats,const std::string & interfaceName)116 int32_t NetStatsClient::GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName)
117 {
118 sptr<INetStatsService> proxy = GetProxy();
119 if (proxy == nullptr) {
120 NETMGR_LOG_E("proxy is nullptr");
121 return NETMANAGER_ERR_GET_PROXY_FAIL;
122 }
123 return proxy->GetIfaceRxBytes(stats, interfaceName);
124 }
125
GetIfaceTxBytes(uint64_t & stats,const std::string & interfaceName)126 int32_t NetStatsClient::GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName)
127 {
128 sptr<INetStatsService> proxy = GetProxy();
129 if (proxy == nullptr) {
130 NETMGR_LOG_E("proxy is nullptr");
131 return NETMANAGER_ERR_GET_PROXY_FAIL;
132 }
133 return proxy->GetIfaceTxBytes(stats, interfaceName);
134 }
135
GetCellularRxBytes(uint64_t & stats)136 int32_t NetStatsClient::GetCellularRxBytes(uint64_t &stats)
137 {
138 sptr<INetStatsService> proxy = GetProxy();
139 if (proxy == nullptr) {
140 NETMGR_LOG_E("proxy is nullptr");
141 return NETMANAGER_ERR_GET_PROXY_FAIL;
142 }
143 return proxy->GetCellularRxBytes(stats);
144 }
145
GetCellularTxBytes(uint64_t & stats)146 int32_t NetStatsClient::GetCellularTxBytes(uint64_t &stats)
147 {
148 sptr<INetStatsService> proxy = GetProxy();
149 if (proxy == nullptr) {
150 NETMGR_LOG_E("proxy is nullptr");
151 return NETMANAGER_ERR_GET_PROXY_FAIL;
152 }
153 return proxy->GetCellularTxBytes(stats);
154 }
155
GetAllRxBytes(uint64_t & stats)156 int32_t NetStatsClient::GetAllRxBytes(uint64_t &stats)
157 {
158 sptr<INetStatsService> proxy = GetProxy();
159 if (proxy == nullptr) {
160 NETMGR_LOG_E("proxy is nullptr");
161 return NETMANAGER_ERR_GET_PROXY_FAIL;
162 }
163 return proxy->GetAllRxBytes(stats);
164 }
165
GetAllTxBytes(uint64_t & stats)166 int32_t NetStatsClient::GetAllTxBytes(uint64_t &stats)
167 {
168 sptr<INetStatsService> proxy = GetProxy();
169 if (proxy == nullptr) {
170 NETMGR_LOG_E("proxy is nullptr");
171 return NETMANAGER_ERR_GET_PROXY_FAIL;
172 }
173 return proxy->GetAllTxBytes(stats);
174 }
175
GetUidRxBytes(uint64_t & stats,uint32_t uid)176 int32_t NetStatsClient::GetUidRxBytes(uint64_t &stats, uint32_t uid)
177 {
178 sptr<INetStatsService> proxy = GetProxy();
179 if (proxy == nullptr) {
180 NETMGR_LOG_E("proxy is nullptr");
181 return NETMANAGER_ERR_GET_PROXY_FAIL;
182 }
183 return proxy->GetUidRxBytes(stats, uid);
184 }
185
GetUidTxBytes(uint64_t & stats,uint32_t uid)186 int32_t NetStatsClient::GetUidTxBytes(uint64_t &stats, uint32_t uid)
187 {
188 sptr<INetStatsService> proxy = GetProxy();
189 if (proxy == nullptr) {
190 NETMGR_LOG_E("proxy is nullptr");
191 return NETMANAGER_ERR_GET_PROXY_FAIL;
192 }
193 return proxy->GetUidTxBytes(stats, uid);
194 }
195
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)196 int32_t NetStatsClient::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
197 NetStatsInfo &statsInfo)
198 {
199 sptr<INetStatsService> proxy = GetProxy();
200 if (proxy == nullptr) {
201 NETMGR_LOG_E("proxy is nullptr");
202 return NETMANAGER_ERR_GET_PROXY_FAIL;
203 }
204 return proxy->GetIfaceStatsDetail(iface, start, end, statsInfo);
205 }
206
GetUidStatsDetail(const std::string & iface,uint32_t uid,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)207 int32_t NetStatsClient::GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
208 NetStatsInfo &statsInfo)
209 {
210 sptr<INetStatsService> proxy = GetProxy();
211 if (proxy == nullptr) {
212 NETMGR_LOG_E("proxy is nullptr");
213 return NETMANAGER_ERR_GET_PROXY_FAIL;
214 }
215 return proxy->GetUidStatsDetail(iface, uid, start, end, statsInfo);
216 }
217
UpdateIfacesStats(const std::string & iface,uint64_t start,uint64_t end,const NetStatsInfo & stats)218 int32_t NetStatsClient::UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
219 const NetStatsInfo &stats)
220 {
221 sptr<INetStatsService> proxy = GetProxy();
222 if (proxy == nullptr) {
223 NETMGR_LOG_E("proxy is nullptr");
224 return NETMANAGER_ERR_GET_PROXY_FAIL;
225 }
226 return proxy->UpdateIfacesStats(iface, start, end, stats);
227 }
228
UpdateStatsData()229 int32_t NetStatsClient::UpdateStatsData()
230 {
231 sptr<INetStatsService> proxy = GetProxy();
232 if (proxy == nullptr) {
233 NETMGR_LOG_E("proxy is nullptr");
234 return NETMANAGER_ERR_GET_PROXY_FAIL;
235 }
236 return proxy->UpdateStatsData();
237 }
238
ResetFactory()239 int32_t NetStatsClient::ResetFactory()
240 {
241 sptr<INetStatsService> proxy = GetProxy();
242 if (proxy == nullptr) {
243 NETMGR_LOG_E("proxy is nullptr");
244 return NETMANAGER_ERR_GET_PROXY_FAIL;
245 }
246 return proxy->ResetFactory();
247 }
248
GetAllStatsInfo(std::vector<NetStatsInfo> & infos)249 int32_t NetStatsClient::GetAllStatsInfo(std::vector<NetStatsInfo> &infos)
250 {
251 sptr<INetStatsService> proxy = GetProxy();
252 if (proxy == nullptr) {
253 NETMGR_LOG_E("proxy is nullptr");
254 return NETMANAGER_ERR_GET_PROXY_FAIL;
255 }
256 return proxy->GetAllStatsInfo(infos);
257 }
258 } // namespace NetManagerStandard
259 } // namespace OHOS
260