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 <string>
17
18 #include <napi/native_api.h>
19
20 #include "constant.h"
21 #include "get_cellular_rxbeytes_contex.h"
22 #include "get_iface_rxbeytes_context.h"
23 #include "get_iface_stats_context.h"
24 #include "get_iface_uid_stats_context.h"
25 #include "get_uid_rxbeytes_context.h"
26 #include "module_template.h"
27 #include "napi_utils.h"
28 #include "statistics_async_work.h"
29 #include "statistics_callback_observer.h"
30 #include "statistics_exec.h"
31 #include "statistics_observer_wrapper.h"
32 #include "update_iface_stats_context.h"
33
34 namespace OHOS {
35 namespace NetManagerStandard {
36 namespace {
37 constexpr const char *STATISTCS_MODULE_NAME = "net.statistics";
38
39 constexpr const char *FUNCTION_GET_CELLULAR_RXBYTES = "getCellularRxBytes";
40 constexpr const char *FUNCTION_GET_CELLULAR_TXBYTES = "getCellularTxBytes";
41 constexpr const char *FUNCTION_GET_ALL_RXBYTES = "getAllRxBytes";
42 constexpr const char *FUNCTION_GET_ALL_TXBYTES = "getAllTxBytes";
43 constexpr const char *FUNCTION_GET_UID_RXBYTES = "getUidRxBytes";
44 constexpr const char *FUNCTION_GET_UID_TXBYTES = "getUidTxBytes";
45 constexpr const char *FUNCTION_GET_IFACE_RXBYTES = "getIfaceRxBytes";
46 constexpr const char *FUNCTION_GET_IFACE_TXBYTES = "getIfaceTxBytes";
47 constexpr const char *FUNCTION_ON = "on";
48 constexpr const char *FUNCTION_OFF = "off";
49 } // namespace
50
GetCellularRxBytes(napi_env env,napi_callback_info info)51 napi_value GetCellularRxBytes(napi_env env, napi_callback_info info)
52 {
53 return ModuleTemplate::Interface<GetCellularRxBytesContext>(env, info, FUNCTION_GET_CELLULAR_RXBYTES, nullptr,
54 StatisticsAsyncWork::ExecGetCellularRxBytes,
55 StatisticsAsyncWork::GetCellularRxBytesCallback);
56 }
57
GetCellularTxBytes(napi_env env,napi_callback_info info)58 napi_value GetCellularTxBytes(napi_env env, napi_callback_info info)
59 {
60 return ModuleTemplate::Interface<GetCellularTxBytesContext>(env, info, FUNCTION_GET_CELLULAR_TXBYTES, nullptr,
61 StatisticsAsyncWork::ExecGetCellularTxBytes,
62 StatisticsAsyncWork::GetCellularTxBytesCallback);
63 }
64
GetAllRxBytes(napi_env env,napi_callback_info info)65 napi_value GetAllRxBytes(napi_env env, napi_callback_info info)
66 {
67 return ModuleTemplate::Interface<GetAllRxBytesContext>(env, info, FUNCTION_GET_ALL_RXBYTES, nullptr,
68 StatisticsAsyncWork::ExecGetAllRxBytes,
69 StatisticsAsyncWork::GetAllRxBytesCallback);
70 }
71
GetAllTxBytes(napi_env env,napi_callback_info info)72 napi_value GetAllTxBytes(napi_env env, napi_callback_info info)
73 {
74 return ModuleTemplate::Interface<GetAllTxBytesContext>(env, info, FUNCTION_GET_ALL_TXBYTES, nullptr,
75 StatisticsAsyncWork::ExecGetAllTxBytes,
76 StatisticsAsyncWork::GetAllTxBytesCallback);
77 }
78
GetUidRxBytes(napi_env env,napi_callback_info info)79 napi_value GetUidRxBytes(napi_env env, napi_callback_info info)
80 {
81 return ModuleTemplate::Interface<GetUidRxBytesContext>(env, info, FUNCTION_GET_UID_RXBYTES, nullptr,
82 StatisticsAsyncWork::ExecGetUidRxBytes,
83 StatisticsAsyncWork::GetUidRxBytesCallback);
84 }
85
GetUidTxBytes(napi_env env,napi_callback_info info)86 napi_value GetUidTxBytes(napi_env env, napi_callback_info info)
87 {
88 return ModuleTemplate::Interface<GetUidTxBytesContext>(env, info, FUNCTION_GET_UID_TXBYTES, nullptr,
89 StatisticsAsyncWork::ExecGetUidTxBytes,
90 StatisticsAsyncWork::GetUidTxBytesCallback);
91 }
92
GetIfaceRxBytes(napi_env env,napi_callback_info info)93 napi_value GetIfaceRxBytes(napi_env env, napi_callback_info info)
94 {
95 return ModuleTemplate::Interface<GetIfaceRxBytesContext>(env, info, FUNCTION_GET_IFACE_RXBYTES, nullptr,
96 StatisticsAsyncWork::ExecGetIfaceRxBytes,
97 StatisticsAsyncWork::GetIfaceRxBytesCallback);
98 }
99
GetIfaceTxBytes(napi_env env,napi_callback_info info)100 napi_value GetIfaceTxBytes(napi_env env, napi_callback_info info)
101 {
102 return ModuleTemplate::Interface<GetIfaceTxBytesContext>(env, info, FUNCTION_GET_IFACE_TXBYTES, nullptr,
103 StatisticsAsyncWork::ExecGetIfaceTxBytes,
104 StatisticsAsyncWork::GetIfaceTxBytesCallback);
105 }
106
On(napi_env env,napi_callback_info info)107 napi_value On(napi_env env, napi_callback_info info)
108 {
109 return DelayedSingleton<StatisticsObserverWrapper>::GetInstance()->On(env, info, {EVENT_STATS_CHANGE}, false);
110 }
111
Off(napi_env env,napi_callback_info info)112 napi_value Off(napi_env env, napi_callback_info info)
113 {
114 return DelayedSingleton<StatisticsObserverWrapper>::GetInstance()->Off(env, info, {EVENT_STATS_CHANGE}, false);
115 }
116
InitStatisticsModule(napi_env env,napi_value exports)117 napi_value InitStatisticsModule(napi_env env, napi_value exports)
118 {
119 NapiUtils::DefineProperties(env, exports,
120 {
121 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CELLULAR_RXBYTES, GetCellularRxBytes),
122 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CELLULAR_TXBYTES, GetCellularTxBytes),
123 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_RXBYTES, GetAllRxBytes),
124 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_TXBYTES, GetAllTxBytes),
125 DECLARE_NAPI_FUNCTION(FUNCTION_GET_UID_RXBYTES, GetUidRxBytes),
126 DECLARE_NAPI_FUNCTION(FUNCTION_GET_UID_TXBYTES, GetUidTxBytes),
127 DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_RXBYTES, GetIfaceRxBytes),
128 DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_TXBYTES, GetIfaceTxBytes),
129 DECLARE_NAPI_FUNCTION(FUNCTION_ON, On),
130 DECLARE_NAPI_FUNCTION(FUNCTION_OFF, Off),
131 });
132 return exports;
133 }
134
135 static napi_module g_statisticsModule = {
136 .nm_version = 1,
137 .nm_flags = 0,
138 .nm_filename = nullptr,
139 .nm_register_func = InitStatisticsModule,
140 .nm_modname = STATISTCS_MODULE_NAME,
141 .nm_priv = nullptr,
142 .reserved = {nullptr},
143 };
144
RegisterNetStatsModule(void)145 extern "C" __attribute__((constructor)) void RegisterNetStatsModule(void)
146 {
147 napi_module_register(&g_statisticsModule);
148 }
149 } // namespace NetManagerStandard
150 } // namespace OHOS
151