1 /*
2 * Copyright (c) 2022-2023 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 "statistics_exec.h"
17
18 #include "errorcode_convertor.h"
19 #include "napi_utils.h"
20 #include "net_stats_client.h"
21 #include "net_stats_constants.h"
22 #include "netmanager_base_log.h"
23 #include "statistics_observer_wrapper.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 const std::string RX_BYTES = "rxBytes";
29 const std::string TX_BYTES = "txBytes";
30 const std::string RX_PACKETS = "rxPackets";
31 const std::string TX_PACKETS = "txPackets";
32 } // namespace
33
ExecGetCellularRxBytes(GetCellularRxBytesContext * context)34 bool StatisticsExec::ExecGetCellularRxBytes(GetCellularRxBytesContext *context)
35 {
36 int32_t result = NetStatsClient::GetInstance().GetCellularRxBytes(context->bytes64_);
37 context->SetErrorCode(result);
38 return result == NETMANAGER_SUCCESS;
39 }
40
ExecGetCellularTxBytes(GetCellularTxBytesContext * context)41 bool StatisticsExec::ExecGetCellularTxBytes(GetCellularTxBytesContext *context)
42 {
43 int32_t result = NetStatsClient::GetInstance().GetCellularTxBytes(context->bytes64_);
44 context->SetErrorCode(result);
45 return result == NETMANAGER_SUCCESS;
46 }
47
ExecGetAllRxBytes(GetAllRxBytesContext * context)48 bool StatisticsExec::ExecGetAllRxBytes(GetAllRxBytesContext *context)
49 {
50 int32_t result = NetStatsClient::GetInstance().GetAllRxBytes(context->bytes64_);
51 context->SetErrorCode(result);
52 return result == NETMANAGER_SUCCESS;
53 }
54
ExecGetAllTxBytes(GetAllTxBytesContext * context)55 bool StatisticsExec::ExecGetAllTxBytes(GetAllTxBytesContext *context)
56 {
57 int32_t result = NetStatsClient::GetInstance().GetAllTxBytes(context->bytes64_);
58 context->SetErrorCode(result);
59 return result == NETMANAGER_SUCCESS;
60 }
61
ExecGetUidRxBytes(GetUidRxBytesContext * context)62 bool StatisticsExec::ExecGetUidRxBytes(GetUidRxBytesContext *context)
63 {
64 int32_t result = NetStatsClient::GetInstance().GetUidRxBytes(context->bytes64_, context->uid_);
65 context->SetErrorCode(result);
66 return result == NETMANAGER_SUCCESS;
67 }
68
ExecGetUidTxBytes(GetUidTxBytesContext * context)69 bool StatisticsExec::ExecGetUidTxBytes(GetUidTxBytesContext *context)
70 {
71 int32_t result = NetStatsClient::GetInstance().GetUidTxBytes(context->bytes64_, context->uid_);
72 context->SetErrorCode(result);
73 return result == NETMANAGER_SUCCESS;
74 }
75
ExecGetIfaceRxBytes(GetIfaceRxBytesContext * context)76 bool StatisticsExec::ExecGetIfaceRxBytes(GetIfaceRxBytesContext *context)
77 {
78 int32_t result = NetStatsClient::GetInstance().GetIfaceRxBytes(context->bytes64_, context->interfaceName_);
79 context->SetErrorCode(result);
80 return result == NETMANAGER_SUCCESS;
81 }
82
ExecGetIfaceTxBytes(GetIfaceTxBytesContext * context)83 bool StatisticsExec::ExecGetIfaceTxBytes(GetIfaceTxBytesContext *context)
84 {
85 int32_t result = NetStatsClient::GetInstance().GetIfaceTxBytes(context->bytes64_, context->interfaceName_);
86 context->SetErrorCode(result);
87 return result == NETMANAGER_SUCCESS;
88 }
89
ExecGetIfaceStats(GetIfaceStatsContext * context)90 bool StatisticsExec::ExecGetIfaceStats(GetIfaceStatsContext *context)
91 {
92 int32_t result = NetStatsClient::GetInstance().GetIfaceStatsDetail(context->GetInterfaceName(), context->GetStart(),
93 context->GetEnd(), context->GetStatsInfo());
94 context->SetErrorCode(result);
95 return result == NETMANAGER_SUCCESS;
96 }
97
ExecGetIfaceUidStats(GetIfaceUidStatsContext * context)98 bool StatisticsExec::ExecGetIfaceUidStats(GetIfaceUidStatsContext *context)
99 {
100 int32_t result = NetStatsClient::GetInstance().GetUidStatsDetail(context->GetInterfaceName(), context->GetUid(),
101 context->GetStart(), context->GetEnd(),
102 context->GetStatsInfo());
103 context->SetErrorCode(result);
104 return result == NETMANAGER_SUCCESS;
105 }
106
ExecUpdateIfacesStats(UpdateIfacesStatsContext * context)107 bool StatisticsExec::ExecUpdateIfacesStats(UpdateIfacesStatsContext *context)
108 {
109 int32_t result = NetStatsClient::GetInstance().UpdateIfacesStats(context->GetInterfaceName(), context->GetStart(),
110 context->GetEnd(), context->GetStatsInfo());
111 context->SetErrorCode(result);
112 return result == NETMANAGER_SUCCESS;
113 }
114
ExecUpdateStatsData(UpdateStatsDataContext * context)115 bool StatisticsExec::ExecUpdateStatsData(UpdateStatsDataContext *context)
116 {
117 int32_t result = NetStatsClient::GetInstance().UpdateStatsData();
118 context->SetErrorCode(result);
119 return result == NETMANAGER_SUCCESS;
120 }
121
GetCellularRxBytesCallback(GetCellularRxBytesContext * context)122 napi_value StatisticsExec::GetCellularRxBytesCallback(GetCellularRxBytesContext *context)
123 {
124 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
125 }
126
GetCellularTxBytesCallback(GetCellularTxBytesContext * context)127 napi_value StatisticsExec::GetCellularTxBytesCallback(GetCellularTxBytesContext *context)
128 {
129 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
130 }
131
GetAllRxBytesCallback(GetAllRxBytesContext * context)132 napi_value StatisticsExec::GetAllRxBytesCallback(GetAllRxBytesContext *context)
133 {
134 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
135 }
136
GetAllTxBytesCallback(GetAllTxBytesContext * context)137 napi_value StatisticsExec::GetAllTxBytesCallback(GetAllTxBytesContext *context)
138 {
139 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
140 }
141
GetUidRxBytesCallback(GetUidRxBytesContext * context)142 napi_value StatisticsExec::GetUidRxBytesCallback(GetUidRxBytesContext *context)
143 {
144 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
145 }
146
GetUidTxBytesCallback(GetUidTxBytesContext * context)147 napi_value StatisticsExec::GetUidTxBytesCallback(GetUidTxBytesContext *context)
148 {
149 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
150 }
151
GetIfaceRxBytesCallback(GetIfaceRxBytesContext * context)152 napi_value StatisticsExec::GetIfaceRxBytesCallback(GetIfaceRxBytesContext *context)
153 {
154 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
155 }
156
GetIfaceTxBytesCallback(GetIfaceTxBytesContext * context)157 napi_value StatisticsExec::GetIfaceTxBytesCallback(GetIfaceTxBytesContext *context)
158 {
159 return NapiUtils::CreateInt64(context->GetEnv(), context->bytes64_);
160 }
161
GetIfaceStatsCallback(GetIfaceStatsContext * context)162 napi_value StatisticsExec::GetIfaceStatsCallback(GetIfaceStatsContext *context)
163 {
164 napi_value netStatsInfo = NapiUtils::CreateObject(context->GetEnv());
165 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, RX_BYTES, context->GetStatsInfo().rxBytes_);
166 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, TX_BYTES, context->GetStatsInfo().txBytes_);
167 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, RX_PACKETS, context->GetStatsInfo().rxPackets_);
168 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, TX_PACKETS, context->GetStatsInfo().txPackets_);
169 return netStatsInfo;
170 }
171
GetIfaceUidStatsCallback(GetIfaceUidStatsContext * context)172 napi_value StatisticsExec::GetIfaceUidStatsCallback(GetIfaceUidStatsContext *context)
173 {
174 napi_value netStatsInfo = NapiUtils::CreateObject(context->GetEnv());
175 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, RX_BYTES, context->GetStatsInfo().rxBytes_);
176 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, TX_BYTES, context->GetStatsInfo().txBytes_);
177 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, RX_PACKETS, context->GetStatsInfo().rxPackets_);
178 NapiUtils::SetInt64Property(context->GetEnv(), netStatsInfo, TX_PACKETS, context->GetStatsInfo().txPackets_);
179 return netStatsInfo;
180 }
181
UpdateIfacesStatsCallback(UpdateIfacesStatsContext * context)182 napi_value StatisticsExec::UpdateIfacesStatsCallback(UpdateIfacesStatsContext *context)
183 {
184 return NapiUtils::GetUndefined(context->GetEnv());
185 }
186
UpdateStatsDataCallback(UpdateStatsDataContext * context)187 napi_value StatisticsExec::UpdateStatsDataCallback(UpdateStatsDataContext *context)
188 {
189 return NapiUtils::GetUndefined(context->GetEnv());
190 }
191 } // namespace NetManagerStandard
192 } // namespace OHOS
193