1 /*
2 * Copyright (c) 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 "statistics_exec.h"
17
18 #include "napi_utils.h"
19 #include "netmanager_base_log.h"
20 #include "net_stats_client.h"
21 #include "net_stats_constants.h"
22 #include "statistics_observer_wrapper.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 const std::string RX_BYTES = "rxBytes";
28 const std::string TX_BYTES = "txBytes";
29 const std::string RX_PACKETS = "rxPackets";
30 const std::string TX_PACKETS = "txPackets";
31 } // namespace
32
ExecGetCellularRxBytes(GetCellularRxBytesContext * context)33 bool StatisticsExec::ExecGetCellularRxBytes(GetCellularRxBytesContext *context)
34 {
35 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularRxBytes());
36 return true;
37 }
38
ExecGetCellularTxBytes(GetCellularTxBytesContext * context)39 bool StatisticsExec::ExecGetCellularTxBytes(GetCellularTxBytesContext *context)
40 {
41 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetCellularTxBytes());
42 return true;
43 }
44
ExecGetAllRxBytes(GetAllRxBytesContext * context)45 bool StatisticsExec::ExecGetAllRxBytes(GetAllRxBytesContext *context)
46 {
47 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetAllRxBytes());
48 return true;
49 }
50
ExecGetAllTxBytes(GetAllTxBytesContext * context)51 bool StatisticsExec::ExecGetAllTxBytes(GetAllTxBytesContext *context)
52 {
53 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetAllTxBytes());
54 return true;
55 }
56
ExecGetUidRxBytes(GetUidRxBytesContext * context)57 bool StatisticsExec::ExecGetUidRxBytes(GetUidRxBytesContext *context)
58 {
59 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetUidRxBytes(context->GetUid()));
60 return true;
61 }
62
ExecGetUidTxBytes(GetUidTxBytesContext * context)63 bool StatisticsExec::ExecGetUidTxBytes(GetUidTxBytesContext *context)
64 {
65 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetUidTxBytes(context->GetUid()));
66 return true;
67 }
68
ExecGetIfaceRxBytes(GetIfaceRxBytesContext * context)69 bool StatisticsExec::ExecGetIfaceRxBytes(GetIfaceRxBytesContext *context)
70 {
71 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceRxBytes(context->GetNic()));
72 return true;
73 }
74
ExecGetIfaceTxBytes(GetIfaceTxBytesContext * context)75 bool StatisticsExec::ExecGetIfaceTxBytes(GetIfaceTxBytesContext *context)
76 {
77 context->SetBytes64(DelayedSingleton<NetStatsClient>::GetInstance()->GetIfaceTxBytes(context->GetNic()));
78 return true;
79 }
80
GetCellularRxBytesCallback(GetCellularRxBytesContext * context)81 napi_value StatisticsExec::GetCellularRxBytesCallback(GetCellularRxBytesContext *context)
82 {
83 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
84 }
85
GetCellularTxBytesCallback(GetCellularTxBytesContext * context)86 napi_value StatisticsExec::GetCellularTxBytesCallback(GetCellularTxBytesContext *context)
87 {
88 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
89 }
90
GetAllRxBytesCallback(GetAllRxBytesContext * context)91 napi_value StatisticsExec::GetAllRxBytesCallback(GetAllRxBytesContext *context)
92 {
93 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
94 }
95
GetAllTxBytesCallback(GetAllTxBytesContext * context)96 napi_value StatisticsExec::GetAllTxBytesCallback(GetAllTxBytesContext *context)
97 {
98 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
99 }
100
GetUidRxBytesCallback(GetUidRxBytesContext * context)101 napi_value StatisticsExec::GetUidRxBytesCallback(GetUidRxBytesContext *context)
102 {
103 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
104 }
105
GetUidTxBytesCallback(GetUidTxBytesContext * context)106 napi_value StatisticsExec::GetUidTxBytesCallback(GetUidTxBytesContext *context)
107 {
108 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
109 }
110
GetIfaceRxBytesCallback(GetIfaceRxBytesContext * context)111 napi_value StatisticsExec::GetIfaceRxBytesCallback(GetIfaceRxBytesContext *context)
112 {
113 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
114 }
115
GetIfaceTxBytesCallback(GetIfaceTxBytesContext * context)116 napi_value StatisticsExec::GetIfaceTxBytesCallback(GetIfaceTxBytesContext *context)
117 {
118 return NapiUtils::CreateInt64(context->GetEnv(), context->GetBytes64());
119 }
120 } // namespace NetManagerStandard
121 } // namespace OHOS
122