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_stats_cached.h" 23 #include "net_stats_callback.h" 24 #include "net_stats_history.h" 25 #include "net_stats_listener.h" 26 #include "net_stats_service_stub.h" 27 #include "netlink_manager.h" 28 29 namespace OHOS { 30 namespace NetManagerStandard { 31 class NetStatsService : public SystemAbility, 32 public NetStatsServiceStub, 33 public std::enable_shared_from_this<NetStatsService> { 34 DECLARE_DELAYED_SINGLETON(NetStatsService) 35 DECLARE_SYSTEM_ABILITY(NetStatsService) 36 37 public: 38 void OnStart() override; 39 void OnStop() override; 40 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 41 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 42 int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override; 43 int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override; 44 int32_t GetCellularRxBytes(uint64_t &stats) override; 45 int32_t GetCellularTxBytes(uint64_t &stats) override; 46 int32_t GetAllRxBytes(uint64_t &stats) override; 47 int32_t GetAllTxBytes(uint64_t &stats) override; 48 int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override; 49 int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override; 50 int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos) override; 51 int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 52 int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 53 int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end, 54 NetStatsInfo &statsInfo) override; 55 int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end, 56 NetStatsInfo &statsInfo) override; 57 int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end, 58 const NetStatsInfo &stats) override; 59 int32_t UpdateStatsData() override; 60 int32_t ResetFactory() override; 61 int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override; 62 int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override; 63 64 private: 65 bool Init(); 66 void GetDumpMessage(std::string &message); 67 bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames); 68 69 private: 70 enum ServiceRunningState { 71 STATE_STOPPED = 0, 72 STATE_RUNNING, 73 }; 74 75 bool registerToService_; 76 ServiceRunningState state_; 77 std::shared_ptr<NetStatsCallback> netStatsCallback_ = nullptr; 78 std::shared_ptr<NetStatsListener> subscriber_ = nullptr; 79 std::unique_ptr<NetStatsCached> netStatsCached_ = nullptr; 80 }; 81 } // namespace NetManagerStandard 82 } // namespace OHOS 83 #endif // NET_STATS_SERVICE_H 84