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 #include "net_stats_service.h"
16
17 #include <sys/time.h>
18 #include <unistd.h>
19 #include <cinttypes>
20
21 #include "broadcast_manager.h"
22 #include "system_ability_definition.h"
23 #include "common_event_support.h"
24 #include "net_stats_constants.h"
25 #include "net_stats_csv.h"
26 #include "net_manager_center.h"
27 #include "net_mgr_log_wrapper.h"
28
29 namespace OHOS {
30 namespace NetManagerStandard {
31 const bool REGISTER_LOCAL_RESULT =
32 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetStatsService>::GetInstance().get());
33
NetStatsService()34 NetStatsService::NetStatsService()
35 : SystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID, true), registerToService_(false), state_(STATE_STOPPED)
36 {
37 netStatsCsv_.reset(std::make_unique<NetStatsCsv>().release());
38 netStatsCallback_ = (std::make_unique<NetStatsCallback>()).release();
39 }
40
~NetStatsService()41 NetStatsService::~NetStatsService() {}
42
UpdateStatsTimer()43 static void UpdateStatsTimer()
44 {
45 sptr<NetStatsCsv> statsCsv = (std::make_unique<NetStatsCsv>()).release();
46 if (statsCsv == nullptr) {
47 NETMGR_LOG_E("statsCsv is nullptr");
48 return;
49 }
50 if (!statsCsv->UpdateIfaceStats()) {
51 NETMGR_LOG_E("UpdateIfaceStats failed");
52 }
53 if (!statsCsv->UpdateUidStats()) {
54 NETMGR_LOG_E("UpdateUidStats failed");
55 }
56 BroadcastInfo info;
57 info.action = EventFwk::CommonEventSupport::COMMON_EVENT_NETMANAGER_NETSTATES_UPDATED;
58 info.data = "Net Manager Iface and Uid States Updated";
59 info.ordered = true;
60 std::map<std::string, bool> param = {{"NetStatsUpdated", true}};
61 DelayedSingleton<BroadcastManager>::GetInstance()->SendBroadcast(info, param);
62 NETMGR_LOG_D("NetStatsService Update Iface and Uid Stats.");
63 }
64
OnStart()65 void NetStatsService::OnStart()
66 {
67 struct timeval tv;
68 gettimeofday(&tv, NULL);
69 NETMGR_LOG_D("NetStatsService::OnStart begin");
70 if (state_ == STATE_RUNNING) {
71 NETMGR_LOG_D("the state is already running");
72 return;
73 }
74 if (!Init()) {
75 NETMGR_LOG_E("init failed");
76 return;
77 }
78
79 InitListener();
80 updateStatsTimer_.Start(INTERVAL_UPDATE_STATS_TIME_MS, UpdateStatsTimer);
81
82 state_ = STATE_RUNNING;
83 gettimeofday(&tv, NULL);
84 NETMGR_LOG_D("NetStatsService::OnStart end");
85 }
86
OnStop()87 void NetStatsService::OnStop()
88 {
89 state_ = STATE_STOPPED;
90 registerToService_ = true;
91 }
92
Init()93 bool NetStatsService::Init()
94 {
95 if (!REGISTER_LOCAL_RESULT) {
96 NETMGR_LOG_E("Register to local sa manager failed");
97 registerToService_ = false;
98 return false;
99 }
100 if (!registerToService_) {
101 if (!Publish(DelayedSingleton<NetStatsService>::GetInstance().get())) {
102 NETMGR_LOG_E("Register to sa manager failed");
103 return false;
104 }
105 registerToService_ = true;
106 }
107 if (!netStatsCsv_->UpdateIfaceCsvInfo()) {
108 NETMGR_LOG_E("update iface.csv failed");
109 return false;
110 }
111 if (!netStatsCsv_->UpdateUidCsvInfo()) {
112 NETMGR_LOG_E("update uid.csv failed");
113 return false;
114 }
115 if (!netStatsCsv_->UpdateIfaceStats()) {
116 NETMGR_LOG_E("update iface_stats.csv failed");
117 return false;
118 }
119 if (!netStatsCsv_->UpdateUidStats()) {
120 NETMGR_LOG_E("update uid_stats.csv failed");
121 return false;
122 }
123 serviceIface_ = (std::make_unique<NetStatsServiceIface>()).release();
124 NetManagerCenter::GetInstance().RegisterStatsService(serviceIface_);
125 return true;
126 }
127
InitListener()128 void NetStatsService::InitListener()
129 {
130 EventFwk::MatchingSkills matchingSkills;
131 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_NETMANAGER_NETSTATES_LIMITED);
132 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_UID_REMOVED);
133 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
134 subscribeInfo.SetPriority(1);
135 subscriber_ = std::make_shared<NetStatsListener>(subscribeInfo);
136 subscriber_->SetStatsCallback(netStatsCallback_);
137 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
138 NETMGR_LOG_D("NetStatsService SubscribeCommonEvent"
139 " COMMON_EVENT_NETMANAGER_NETSTATES_LIMITED and COMMON_EVENT_UID_REMOVED");
140 }
141
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)142 int32_t NetStatsService::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
143 {
144 if (callback == nullptr) {
145 NETMGR_LOG_E("RegisterNetStatsCallback parameter callback is null");
146 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR);
147 }
148
149 netStatsCallback_->RegisterNetStatsCallback(callback);
150
151 return static_cast<int32_t>(NetStatsResultCode::ERR_NONE);
152 }
153
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)154 int32_t NetStatsService::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
155 {
156 if (callback == nullptr) {
157 NETMGR_LOG_E("UnregisterNetStatsCallback parameter callback is null");
158 return static_cast<int32_t>(NetStatsResultCode::ERR_INTERNAL_ERROR);
159 }
160
161 netStatsCallback_->UnregisterNetStatsCallback(callback);
162
163 return static_cast<int32_t>(NetStatsResultCode::ERR_NONE);
164 }
165
GetIfaceStatsDetail(const std::string & iface,uint32_t start,uint32_t end,NetStatsInfo & statsInfo)166 NetStatsResultCode NetStatsService::GetIfaceStatsDetail(const std::string &iface, uint32_t start, uint32_t end,
167 NetStatsInfo &statsInfo)
168 {
169 if (!netStatsCsv_->ExistsIface(iface)) {
170 NETMGR_LOG_E("iface not exist");
171 return NetStatsResultCode::ERR_INVALID_PARAMETER;
172 }
173 if (start >= end) {
174 NETMGR_LOG_E("the start time should be less than the end time.");
175 return NetStatsResultCode::ERR_INVALID_PARAMETER;
176 }
177 NetStatsResultCode result = netStatsCsv_->GetIfaceBytes(iface, start, end, statsInfo);
178 NETMGR_LOG_I("GetIfaceStatsDetail iface[%{public}s], statsInfo.rxBytes[%{public}" PRId64 "]"
179 "statsInfo.txBytes[%{public}" PRId64 "]", iface.c_str(), statsInfo.rxBytes_, statsInfo.txBytes_);
180 if (result == NetStatsResultCode::ERR_INVALID_TIME_PERIOD) {
181 NETMGR_LOG_E("GetIfaceBytes is error.");
182 }
183 return result;
184 }
185
GetUidStatsDetail(const std::string & iface,uint32_t uid,uint32_t start,uint32_t end,NetStatsInfo & statsInfo)186 NetStatsResultCode NetStatsService::GetUidStatsDetail(const std::string &iface, uint32_t uid,
187 uint32_t start, uint32_t end, NetStatsInfo &statsInfo)
188 {
189 if (!netStatsCsv_->ExistsIface(iface)) {
190 NETMGR_LOG_E("iface not exist");
191 return NetStatsResultCode::ERR_INVALID_PARAMETER;
192 }
193 if (!netStatsCsv_->ExistsUid(uid)) {
194 NETMGR_LOG_E("uid not exist");
195 return NetStatsResultCode::ERR_INVALID_PARAMETER;
196 }
197 if (start >= end) {
198 NETMGR_LOG_E("the start time should be less than the end time.");
199 return NetStatsResultCode::ERR_INVALID_PARAMETER;
200 }
201 NetStatsResultCode result = netStatsCsv_->GetUidBytes(iface, uid, start, end, statsInfo);
202 NETMGR_LOG_I("GetUidStatsDetail iface[%{public}s], uid[%{public}d] statsInfo.rxBytes[%{public}" PRId64 "] "
203 "statsInfo.txBytes[%{public}" PRId64 "]", iface.c_str(), uid, statsInfo.rxBytes_, statsInfo.txBytes_);
204 if (result == NetStatsResultCode::ERR_INVALID_TIME_PERIOD) {
205 NETMGR_LOG_E("GetUidBytes is error.");
206 }
207 return result;
208 }
209
UpdateIfacesStats(const std::string & iface,uint32_t start,uint32_t end,const NetStatsInfo & stats)210 NetStatsResultCode NetStatsService::UpdateIfacesStats(const std::string &iface,
211 uint32_t start, uint32_t end, const NetStatsInfo &stats)
212 {
213 NETMGR_LOG_I("UpdateIfacesStats info: iface[%{public}s],rxBytes_[%{public}" PRId64 "]"
214 "txBytes_[%{public}" PRId64 "]", iface.c_str(), stats.rxBytes_, stats.txBytes_);
215
216 if (!netStatsCsv_->ExistsIface(iface)) {
217 NETMGR_LOG_E("iface not exist");
218 return NetStatsResultCode::ERR_INVALID_PARAMETER;
219 }
220
221 if (start >= end) {
222 NETMGR_LOG_E("the start time should be less than the end time.");
223 return NetStatsResultCode::ERR_INVALID_PARAMETER;
224 }
225
226 if (stats.rxBytes_ < 0 || stats.txBytes_ < 0) {
227 NETMGR_LOG_E("the bytes cannot be negative.");
228 return NetStatsResultCode::ERR_INVALID_PARAMETER;
229 }
230
231 if (!netStatsCsv_->CorrectedIfacesStats(iface, start, end, stats)) {
232 NETMGR_LOG_E("UpdateIfacesStats failed");
233 return NetStatsResultCode::ERR_INTERNAL_ERROR;
234 }
235 return NetStatsResultCode::ERR_NONE;
236 }
237
UpdateStatsData()238 NetStatsResultCode NetStatsService::UpdateStatsData()
239 {
240 if (!netStatsCsv_->UpdateIfaceStats()) {
241 NETMGR_LOG_E("UpdateIfaceStats failed");
242 }
243 if (!netStatsCsv_->UpdateUidStats()) {
244 NETMGR_LOG_E("UpdateUidStats failed");
245 }
246 return NetStatsResultCode::ERR_NONE;
247 }
248
ResetFactory()249 NetStatsResultCode NetStatsService::ResetFactory()
250 {
251 NETMGR_LOG_I("ResetFactory begin");
252 return netStatsCsv_->ResetFactory();
253 }
254 } // namespace NetManagerStandard
255 } // namespace OHOS
256