1 /* 2 * Copyright (c) 2021-2023 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_SERVICE_H 17 #define NET_STATS_SERVICE_H 18 19 #include "singleton.h" 20 #include "system_ability.h" 21 22 #include "net_push_stats_info.h" 23 #include "net_stats_cached.h" 24 #include "net_stats_callback.h" 25 #include "net_stats_history.h" 26 #include "net_stats_info_sequence.h" 27 #include "net_stats_listener.h" 28 #include "net_stats_network.h" 29 #include "net_stats_service_stub.h" 30 #include "netlink_manager.h" 31 32 namespace OHOS { 33 namespace NetManagerStandard { 34 class NetStatsService : public SystemAbility, 35 public NetStatsServiceStub, 36 public std::enable_shared_from_this<NetStatsService> { 37 DECLARE_DELAYED_SINGLETON(NetStatsService) 38 DECLARE_SYSTEM_ABILITY(NetStatsService) 39 40 public: 41 void OnStart() override; 42 void OnStop() override; 43 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 44 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 45 int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override; 46 int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override; 47 int32_t GetCellularRxBytes(uint64_t &stats) override; 48 int32_t GetCellularTxBytes(uint64_t &stats) override; 49 int32_t GetAllRxBytes(uint64_t &stats) override; 50 int32_t GetAllTxBytes(uint64_t &stats) override; 51 int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override; 52 int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override; 53 int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos) override; 54 int32_t GetAllSimStatsInfo(std::vector<NetStatsInfo> &infos) override; 55 int32_t GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos, 56 const sptr<NetStatsNetwork> &network) override; 57 int32_t GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid, 58 const sptr<NetStatsNetwork> &network) override; 59 int32_t SetAppStats(const PushStatsInfo &info) override; 60 int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 61 int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 62 int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end, 63 NetStatsInfo &statsInfo) override; 64 int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end, 65 NetStatsInfo &statsInfo) override; 66 int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end, 67 const NetStatsInfo &stats) override; 68 int32_t UpdateStatsData() override; 69 int32_t ResetFactory() override; 70 int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override; 71 int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override; 72 73 private: 74 bool Init(); 75 void GetDumpMessage(std::string &message); 76 void MergeTrafficStats(std::vector<NetStatsInfoSequence> &statsInfoSequences, const NetStatsInfo &info, 77 uint32_t currentTime); 78 bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames); 79 80 private: 81 enum ServiceRunningState { 82 STATE_STOPPED = 0, 83 STATE_RUNNING, 84 }; 85 86 bool registerToService_; 87 ServiceRunningState state_; 88 std::shared_ptr<NetStatsCallback> netStatsCallback_ = nullptr; 89 std::shared_ptr<NetStatsListener> subscriber_ = nullptr; 90 std::unique_ptr<NetStatsCached> netStatsCached_ = nullptr; 91 }; 92 } // namespace NetManagerStandard 93 } // namespace OHOS 94 #endif // NET_STATS_SERVICE_H 95