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