• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_listener.h"
23 #include "net_stats_callback.h"
24 #include "net_stats_service_stub.h"
25 #include "net_stats_service_iface.h"
26 #include "net_stats_csv.h"
27 #include "timer.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 constexpr uint32_t INTERVAL_UPDATE_STATS_TIME_MS = 1800000; // half an hour
32 class NetStatsService : public SystemAbility,
33     public NetStatsServiceStub,
34     public std::enable_shared_from_this<NetStatsService> {
35     DECLARE_DELAYED_SINGLETON(NetStatsService)
36     DECLARE_SYSTEM_ABILITY(NetStatsService)
37 
38 public:
39     void OnStart() override;
40     void OnStop() override;
41     int32_t RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
42     int32_t UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback) override;
43     NetStatsResultCode GetIfaceStatsDetail(const std::string &iface, uint32_t start, uint32_t end,
44         NetStatsInfo &statsInfo) override;
45     NetStatsResultCode GetUidStatsDetail(const std::string &iface, uint32_t uid,
46         uint32_t start, uint32_t end, NetStatsInfo &statsInfo) override;
47     NetStatsResultCode UpdateIfacesStats(const std::string &iface,
48         uint32_t start, uint32_t end, const NetStatsInfo &stats) override;
49     NetStatsResultCode UpdateStatsData() override;
50     NetStatsResultCode ResetFactory() override;
51 private:
52     bool Init();
53     void InitListener();
54 
55 private:
56     enum ServiceRunningState {
57         STATE_STOPPED = 0,
58         STATE_RUNNING,
59     };
60 
61     bool registerToService_;
62     ServiceRunningState state_;
63     Timer updateStatsTimer_;
64     sptr<NetStatsCallback> netStatsCallback_;
65     std::shared_ptr<NetStatsListener> subscriber_ = nullptr;
66     std::unique_ptr<NetStatsCsv> netStatsCsv_ = nullptr;
67     sptr<NetStatsServiceIface> serviceIface_ = nullptr;
68 };
69 } // namespace NetManagerStandard
70 } // namespace OHOS
71 #endif // NET_STATS_SERVICE_H
72