1 /* 2 * Copyright (c) 2021 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_callback.h" 23 #include "net_stats_service_stub.h" 24 #include "net_stats_wrapper.h" 25 #include "timer.h" 26 27 namespace OHOS { 28 namespace NetManagerStandard { 29 class NetStatsService : public SystemAbility, 30 public NetStatsServiceStub, 31 public std::enable_shared_from_this<NetStatsService> { 32 DECLARE_DELAYED_SINGLETON(NetStatsService) 33 DECLARE_SYSTEM_ABILITY(NetStatsService) 34 35 public: 36 void OnStart() override; 37 void OnStop() override; 38 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 39 int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 40 int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override; 41 int64_t GetIfaceRxBytes(const std::string &interfaceName) override; 42 int64_t GetIfaceTxBytes(const std::string &interfaceName) override; 43 int64_t GetCellularRxBytes() override; 44 int64_t GetCellularTxBytes() override; 45 int64_t GetAllRxBytes() override; 46 int64_t GetAllTxBytes() override; 47 int64_t GetUidRxBytes(uint32_t uid) override; 48 int64_t GetUidTxBytes(uint32_t uid) override; 49 private: 50 bool Init(); 51 void GetDumpMessage(std::string &message); 52 53 private: 54 enum ServiceRunningState { 55 STATE_STOPPED = 0, 56 STATE_RUNNING, 57 }; 58 59 bool registerToService_; 60 ServiceRunningState state_; 61 sptr<NetStatsCallback> netStatsCallback_; 62 std::unique_ptr<NetStatsWrapper> netStatsWrapper_ = nullptr; 63 }; 64 } // namespace NetManagerStandard 65 } // namespace OHOS 66 #endif // NET_STATS_SERVICE_H 67