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_mgr_log_wrapper.h" 22 23 namespace OHOS { 24 namespace NetManagerStandard { NetStatsClient()25NetStatsClient::NetStatsClient() : netStatsService_(nullptr), deathRecipient_(nullptr) {} 26 27 NetStatsClient::~NetStatsClient() = default; 28 RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)29int32_t NetStatsClient::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) 30 { 31 sptr<INetStatsService> proxy = GetProxy(); 32 if (proxy == nullptr) { 33 NETMGR_LOG_E("proxy is nullptr"); 34 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR); 35 } 36 37 return proxy->RegisterNetStatsCallback(callback); 38 } 39 UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)40int32_t NetStatsClient::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) 41 { 42 sptr<INetStatsService> proxy = GetProxy(); 43 if (proxy == nullptr) { 44 NETMGR_LOG_E("proxy is nullptr"); 45 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR); 46 } 47 48 return proxy->UnregisterNetStatsCallback(callback); 49 } 50 GetProxy()51sptr<INetStatsService> NetStatsClient::GetProxy() 52 { 53 std::lock_guard lock(mutex_); 54 55 if (netStatsService_ != nullptr) { 56 NETMGR_LOG_D("get proxy is ok"); 57 return netStatsService_; 58 } 59 60 NETMGR_LOG_D("execute GetSystemAbilityManager"); 61 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 62 if (sam == nullptr) { 63 NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed"); 64 return nullptr; 65 } 66 67 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID); 68 if (remote == nullptr) { 69 NETMGR_LOG_E("get Remote service failed"); 70 return nullptr; 71 } 72 73 deathRecipient_ = (std::make_unique<NetStatsDeathRecipient>(*this)).release(); 74 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) { 75 NETMGR_LOG_E("add death recipient failed"); 76 return nullptr; 77 } 78 79 netStatsService_ = iface_cast<INetStatsService>(remote); 80 if (netStatsService_ == nullptr) { 81 NETMGR_LOG_E("get Remote service proxy failed"); 82 return nullptr; 83 } 84 return netStatsService_; 85 } 86 OnRemoteDied(const wptr<IRemoteObject> & remote)87void NetStatsClient::OnRemoteDied(const wptr<IRemoteObject> &remote) 88 { 89 NETMGR_LOG_D("on remote died"); 90 if (remote == nullptr) { 91 NETMGR_LOG_E("remote object is nullptr"); 92 return; 93 } 94 95 std::lock_guard lock(mutex_); 96 if (netStatsService_ == nullptr) { 97 NETMGR_LOG_E("NetConnService_ is nullptr"); 98 return; 99 } 100 101 sptr<IRemoteObject> local = netStatsService_->AsObject(); 102 if (local != remote.promote()) { 103 NETMGR_LOG_E("proxy and stub is not same remote object"); 104 return; 105 } 106 107 local->RemoveDeathRecipient(deathRecipient_); 108 netStatsService_ = nullptr; 109 } 110 GetIfaceRxBytes(const std::string & interfaceName)111int64_t NetStatsClient::GetIfaceRxBytes(const std::string &interfaceName) 112 { 113 int64_t err = -1; 114 sptr<INetStatsService> proxy = GetProxy(); 115 if (proxy == nullptr) { 116 NETMGR_LOG_E("proxy is nullptr"); 117 return err; 118 } 119 return proxy->GetIfaceRxBytes(interfaceName); 120 } 121 GetIfaceTxBytes(const std::string & interfaceName)122int64_t NetStatsClient::GetIfaceTxBytes(const std::string &interfaceName) 123 { 124 int64_t err = -1; 125 sptr<INetStatsService> proxy = GetProxy(); 126 if (proxy == nullptr) { 127 NETMGR_LOG_E("proxy is nullptr"); 128 return err; 129 } 130 return proxy->GetIfaceTxBytes(interfaceName); 131 } 132 GetCellularRxBytes()133int64_t NetStatsClient::GetCellularRxBytes() 134 { 135 int64_t err = -1; 136 sptr<INetStatsService> proxy = GetProxy(); 137 if (proxy == nullptr) { 138 NETMGR_LOG_E("proxy is nullptr"); 139 return err; 140 } 141 return proxy->GetCellularRxBytes(); 142 } 143 GetCellularTxBytes()144int64_t NetStatsClient::GetCellularTxBytes() 145 { 146 int64_t err = -1; 147 sptr<INetStatsService> proxy = GetProxy(); 148 if (proxy == nullptr) { 149 NETMGR_LOG_E("proxy is nullptr"); 150 return err; 151 } 152 return proxy->GetCellularTxBytes(); 153 } 154 GetAllRxBytes()155int64_t NetStatsClient::GetAllRxBytes() 156 { 157 int64_t err = -1; 158 sptr<INetStatsService> proxy = GetProxy(); 159 if (proxy == nullptr) { 160 NETMGR_LOG_E("proxy is nullptr"); 161 return err; 162 } 163 return proxy->GetAllRxBytes(); 164 } 165 GetAllTxBytes()166int64_t NetStatsClient::GetAllTxBytes() 167 { 168 int64_t err = -1; 169 sptr<INetStatsService> proxy = GetProxy(); 170 if (proxy == nullptr) { 171 NETMGR_LOG_E("proxy is nullptr"); 172 return err; 173 } 174 return proxy->GetAllTxBytes(); 175 } 176 GetUidRxBytes(uint32_t uid)177int64_t NetStatsClient::GetUidRxBytes(uint32_t uid) 178 { 179 int64_t err = -1; 180 sptr<INetStatsService> proxy = GetProxy(); 181 if (proxy == nullptr) { 182 NETMGR_LOG_E("proxy is nullptr"); 183 return err; 184 } 185 return proxy->GetUidRxBytes(uid); 186 } 187 GetUidTxBytes(uint32_t uid)188int64_t NetStatsClient::GetUidTxBytes(uint32_t uid) 189 { 190 int64_t err = -1; 191 sptr<INetStatsService> proxy = GetProxy(); 192 if (proxy == nullptr) { 193 NETMGR_LOG_E("proxy is nullptr"); 194 return err; 195 } 196 return proxy->GetUidTxBytes(uid); 197 } 198 } // namespace NetManagerStandard 199 } // namespace OHOS