1 /*
2 * Copyright (c) 2021-2022 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 #include "net_stats_service.h"
17
18 #include <cinttypes>
19 #include <initializer_list>
20 #include <sys/time.h>
21 #include <unistd.h>
22
23 #include "broadcast_manager.h"
24 #include "common_event_support.h"
25 #include "netmanager_base_permission.h"
26 #include "net_stats_constants.h"
27 #include "net_manager_center.h"
28 #include "net_mgr_log_wrapper.h"
29 #include "system_ability_definition.h"
30
31 namespace OHOS {
32 namespace NetManagerStandard {
33 namespace {
34 constexpr std::initializer_list<NetBearType> BEAR_TYPE_LIST = {
35 NetBearType::BEARER_CELLULAR,
36 NetBearType::BEARER_WIFI,
37 NetBearType::BEARER_BLUETOOTH,
38 NetBearType::BEARER_ETHERNET,
39 NetBearType::BEARER_VPN,
40 NetBearType::BEARER_WIFI_AWARE,
41 };
42
GetIfaceNamesFromManager(std::list<std::string> & ifaceNames)43 bool GetIfaceNamesFromManager(std::list<std::string> &ifaceNames)
44 {
45 int32_t ret = NetManagerCenter::GetInstance().GetIfaceNames(BEARER_CELLULAR, ifaceNames);
46 if (ret != 0 || ifaceNames.empty()) {
47 NETMGR_LOG_E("Iface list is empty, ret = %{public}d", ret);
48 return false;
49 }
50 return true;
51 }
52 } // namespace
53 const bool REGISTER_LOCAL_RESULT =
54 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetStatsService>::GetInstance().get());
55
NetStatsService()56 NetStatsService::NetStatsService()
57 : SystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID, true), registerToService_(false), state_(STATE_STOPPED)
58 {
59 netStatsCallback_ = new (std::nothrow) NetStatsCallback();
60 }
61
62 NetStatsService::~NetStatsService() = default;
63
OnStart()64 void NetStatsService::OnStart()
65 {
66 NETMGR_LOG_D("NetStatsService::OnStart begin");
67 if (state_ == STATE_RUNNING) {
68 NETMGR_LOG_D("the state is already running");
69 return;
70 }
71 if (!Init()) {
72 NETMGR_LOG_E("init failed");
73 return;
74 }
75 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
76 state_ = STATE_RUNNING;
77 NETMGR_LOG_D("NetStatsService::OnStart end");
78 }
79
OnStop()80 void NetStatsService::OnStop()
81 {
82 state_ = STATE_STOPPED;
83 registerToService_ = true;
84 }
85
Dump(int32_t fd,const std::vector<std::u16string> & args)86 int32_t NetStatsService::Dump(int32_t fd, const std::vector<std::u16string> &args)
87 {
88 NETMGR_LOG_D("Start Dump, fd: %{public}d", fd);
89 std::string result;
90 GetDumpMessage(result);
91 int32_t ret = dprintf(fd, "%s\n", result.c_str());
92 return ret < 0 ? static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR)
93 : static_cast<int32_t>(NetStatsResultCode::ERR_NONE);
94 }
95
GetDumpMessage(std::string & message)96 void NetStatsService::GetDumpMessage(std::string &message)
97 {
98 message.append("Net Stats Info:\n");
99 message.append("\tRxBytes: " +
100 std::to_string(NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_RX_BYTES)) +
101 "\n");
102 message.append("\tTxBytes: " +
103 std::to_string(NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_TX_BYTES)) +
104 "\n");
105 message.append("\tRxPackets: " +
106 std::to_string(NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_RX_PACKETS)) +
107 "\n");
108 message.append("\tTxPackets: " +
109 std::to_string(NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_TX_PACKETS)) +
110 "\n");
111 std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [&message, this](const auto &bearType) {
112 std::list<std::string> ifaceNames;
113 if (NetManagerCenter::GetInstance().GetIfaceNames(bearType, ifaceNames)) {
114 return;
115 }
116 for (const auto &name : ifaceNames) {
117 message.append("\t" + name + "-TxBytes: " + std::to_string(GetIfaceTxBytes(name)));
118 message.append("\t" + name + "-RxBytes: " + std::to_string(GetIfaceRxBytes(name)));
119 }
120 });
121 }
122
Init()123 bool NetStatsService::Init()
124 {
125 if (!REGISTER_LOCAL_RESULT) {
126 NETMGR_LOG_E("Register to local sa manager failed");
127 registerToService_ = false;
128 return false;
129 }
130 if (!registerToService_) {
131 if (!Publish(DelayedSingleton<NetStatsService>::GetInstance().get())) {
132 NETMGR_LOG_E("Register to sa manager failed");
133 return false;
134 }
135 registerToService_ = true;
136 }
137 return true;
138 }
139
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)140 int32_t NetStatsService::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
141 {
142 if (callback == nullptr) {
143 NETMGR_LOG_E("RegisterNetStatsCallback parameter callback is null");
144 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR);
145 }
146
147 netStatsCallback_->RegisterNetStatsCallback(callback);
148
149 return static_cast<int32_t>(NetStatsResultCode::ERR_NONE);
150 }
151
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)152 int32_t NetStatsService::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
153 {
154 if (callback == nullptr) {
155 NETMGR_LOG_E("UnregisterNetStatsCallback parameter callback is null");
156 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR);
157 }
158
159 netStatsCallback_->UnregisterNetStatsCallback(callback);
160
161 return static_cast<int32_t>(NetStatsResultCode::ERR_NONE);
162 }
163
GetIfaceRxBytes(const std::string & interfaceName)164 int64_t NetStatsService::GetIfaceRxBytes(const std::string &interfaceName)
165 {
166 return NetStatsWrapper::GetInstance().GetIfaceStats(StatsType::STATS_TYPE_RX_BYTES, interfaceName);
167 }
168
GetIfaceTxBytes(const std::string & interfaceName)169 int64_t NetStatsService::GetIfaceTxBytes(const std::string &interfaceName)
170 {
171 return NetStatsWrapper::GetInstance().GetIfaceStats(StatsType::STATS_TYPE_TX_BYTES, interfaceName);
172 }
173
GetCellularRxBytes()174 int64_t NetStatsService::GetCellularRxBytes()
175 {
176 std::list<std::string> ifaceNames;
177 int64_t err = -1;
178 if (!GetIfaceNamesFromManager(ifaceNames)) {
179 return err;
180 }
181 int64_t totalCellular = 0;
182 for (const auto &name : ifaceNames) {
183 totalCellular = totalCellular + NetStatsWrapper::GetInstance().GetIfaceStats(
184 StatsType::STATS_TYPE_RX_BYTES, name);
185 }
186 return totalCellular;
187 }
188
GetCellularTxBytes()189 int64_t NetStatsService::GetCellularTxBytes()
190 {
191 std::list<std::string> ifaceNames;
192 int64_t err = -1;
193 if (!GetIfaceNamesFromManager(ifaceNames)) {
194 return err;
195 }
196 int64_t totalCellular = 0;
197 for (const auto &name : ifaceNames) {
198 totalCellular = totalCellular + NetStatsWrapper::GetInstance().GetIfaceStats(
199 StatsType::STATS_TYPE_TX_BYTES, name);
200 }
201 return totalCellular;
202 }
203
GetAllRxBytes()204 int64_t NetStatsService::GetAllRxBytes()
205 {
206 return NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_RX_BYTES);
207 }
208
GetAllTxBytes()209 int64_t NetStatsService::GetAllTxBytes()
210 {
211 return NetStatsWrapper::GetInstance().GetTotalStats(StatsType::STATS_TYPE_TX_BYTES);
212 }
213
GetUidRxBytes(uint32_t uid)214 int64_t NetStatsService::GetUidRxBytes(uint32_t uid)
215 {
216 return NetStatsWrapper::GetInstance().GetUidStats(StatsType::STATS_TYPE_RX_BYTES, uid);
217 }
218
GetUidTxBytes(uint32_t uid)219 int64_t NetStatsService::GetUidTxBytes(uint32_t uid)
220 {
221 return NetStatsWrapper::GetInstance().GetUidStats(StatsType::STATS_TYPE_TX_BYTES, uid);
222 }
223 } // namespace NetManagerStandard
224 } // namespace OHOS
225