1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef NET_STATS_SERVICE_H
17b1b8bc3fSopenharmony_ci#define NET_STATS_SERVICE_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include "singleton.h"
20b1b8bc3fSopenharmony_ci#include "system_ability.h"
21b1b8bc3fSopenharmony_ci
22b1b8bc3fSopenharmony_ci#include "net_push_stats_info.h"
23b1b8bc3fSopenharmony_ci#include "net_stats_cached.h"
24b1b8bc3fSopenharmony_ci#include "net_stats_callback.h"
25b1b8bc3fSopenharmony_ci#include "net_stats_history.h"
26b1b8bc3fSopenharmony_ci#include "net_stats_info_sequence.h"
27b1b8bc3fSopenharmony_ci#include "net_stats_listener.h"
28b1b8bc3fSopenharmony_ci#include "net_stats_network.h"
29b1b8bc3fSopenharmony_ci#include "net_stats_service_stub.h"
30b1b8bc3fSopenharmony_ci#include "netlink_manager.h"
31b1b8bc3fSopenharmony_ci
32b1b8bc3fSopenharmony_cinamespace OHOS {
33b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
34b1b8bc3fSopenharmony_ciclass NetStatsService : public SystemAbility,
35b1b8bc3fSopenharmony_ci                        public NetStatsServiceStub,
36b1b8bc3fSopenharmony_ci                        public std::enable_shared_from_this<NetStatsService> {
37b1b8bc3fSopenharmony_ci    DECLARE_DELAYED_SINGLETON(NetStatsService)
38b1b8bc3fSopenharmony_ci    DECLARE_SYSTEM_ABILITY(NetStatsService)
39b1b8bc3fSopenharmony_ci
40b1b8bc3fSopenharmony_cipublic:
41b1b8bc3fSopenharmony_ci    void OnStart() override;
42b1b8bc3fSopenharmony_ci    void OnStop() override;
43b1b8bc3fSopenharmony_ci    void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
44b1b8bc3fSopenharmony_ci    int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
45b1b8bc3fSopenharmony_ci    int32_t GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName) override;
46b1b8bc3fSopenharmony_ci    int32_t GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName) override;
47b1b8bc3fSopenharmony_ci    int32_t GetCellularRxBytes(uint64_t &stats) override;
48b1b8bc3fSopenharmony_ci    int32_t GetCellularTxBytes(uint64_t &stats) override;
49b1b8bc3fSopenharmony_ci    int32_t GetAllRxBytes(uint64_t &stats) override;
50b1b8bc3fSopenharmony_ci    int32_t GetAllTxBytes(uint64_t &stats) override;
51b1b8bc3fSopenharmony_ci    int32_t GetUidRxBytes(uint64_t &stats, uint32_t uid) override;
52b1b8bc3fSopenharmony_ci    int32_t GetUidTxBytes(uint64_t &stats, uint32_t uid) override;
53b1b8bc3fSopenharmony_ci    int32_t GetAllStatsInfo(std::vector<NetStatsInfo> &infos) override;
54b1b8bc3fSopenharmony_ci    int32_t GetAllSimStatsInfo(std::vector<NetStatsInfo> &infos) override;
55b1b8bc3fSopenharmony_ci    int32_t GetTrafficStatsByNetwork(std::unordered_map<uint32_t, NetStatsInfo> &infos,
56b1b8bc3fSopenharmony_ci                                     const sptr<NetStatsNetwork> &network) override;
57b1b8bc3fSopenharmony_ci    int32_t GetTrafficStatsByUidNetwork(std::vector<NetStatsInfoSequence> &infos, uint32_t uid,
58b1b8bc3fSopenharmony_ci                                        const sptr<NetStatsNetwork> &network) override;
59b1b8bc3fSopenharmony_ci    int32_t SetAppStats(const PushStatsInfo &info) override;
60b1b8bc3fSopenharmony_ci    int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
61b1b8bc3fSopenharmony_ci    int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
62b1b8bc3fSopenharmony_ci    int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
63b1b8bc3fSopenharmony_ci                                NetStatsInfo &statsInfo) override;
64b1b8bc3fSopenharmony_ci    int32_t GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
65b1b8bc3fSopenharmony_ci                              NetStatsInfo &statsInfo) override;
66b1b8bc3fSopenharmony_ci    int32_t UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
67b1b8bc3fSopenharmony_ci                              const NetStatsInfo &stats) override;
68b1b8bc3fSopenharmony_ci    int32_t UpdateStatsData() override;
69b1b8bc3fSopenharmony_ci    int32_t ResetFactory() override;
70b1b8bc3fSopenharmony_ci    int32_t GetCookieRxBytes(uint64_t &stats, uint64_t cookie) override;
71b1b8bc3fSopenharmony_ci    int32_t GetCookieTxBytes(uint64_t &stats, uint64_t cookie) override;
72b1b8bc3fSopenharmony_ci
73b1b8bc3fSopenharmony_ciprivate:
74b1b8bc3fSopenharmony_ci    bool Init();
75b1b8bc3fSopenharmony_ci    void GetDumpMessage(std::string &message);
76b1b8bc3fSopenharmony_ci    void MergeTrafficStats(std::vector<NetStatsInfoSequence> &statsInfoSequences, const NetStatsInfo &info,
77b1b8bc3fSopenharmony_ci                           uint32_t currentTime);
78b1b8bc3fSopenharmony_ci    bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames);
79b1b8bc3fSopenharmony_ci
80b1b8bc3fSopenharmony_ciprivate:
81b1b8bc3fSopenharmony_ci    enum ServiceRunningState {
82b1b8bc3fSopenharmony_ci        STATE_STOPPED = 0,
83b1b8bc3fSopenharmony_ci        STATE_RUNNING,
84b1b8bc3fSopenharmony_ci    };
85b1b8bc3fSopenharmony_ci
86b1b8bc3fSopenharmony_ci    bool registerToService_;
87b1b8bc3fSopenharmony_ci    ServiceRunningState state_;
88b1b8bc3fSopenharmony_ci    std::shared_ptr<NetStatsCallback> netStatsCallback_ = nullptr;
89b1b8bc3fSopenharmony_ci    std::shared_ptr<NetStatsListener> subscriber_ = nullptr;
90b1b8bc3fSopenharmony_ci    std::unique_ptr<NetStatsCached> netStatsCached_ = nullptr;
91b1b8bc3fSopenharmony_ci};
92b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
93b1b8bc3fSopenharmony_ci} // namespace OHOS
94b1b8bc3fSopenharmony_ci#endif // NET_STATS_SERVICE_H
95