• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
17 
18 #include <napi/native_api.h>
19 
20 #include "constant.h"
21 #include "get_cellular_rxbytes_context.h"
22 #include "get_iface_rxbytes_context.h"
23 #include "get_iface_stats_context.h"
24 #include "get_iface_uid_stats_context.h"
25 #include "get_uid_rxbytes_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 *STATISTICS_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_GET_IFACE_STATS = "getTrafficStatsByIface";
48 constexpr const char *FUNCTION_GET_IFACE_UID_STATS = "getTrafficStatsByUid";
49 constexpr const char *FUNCTION_UPDATE_IFACE_STATS = "updateIfacesStats";
50 constexpr const char *FUNCTION_UPDATE_STATS_DATA = "updateStatsData";
51 constexpr const char *FUNCTION_ON = "on";
52 constexpr const char *FUNCTION_OFF = "off";
53 } // namespace
54 
GetCellularRxBytes(napi_env env,napi_callback_info info)55 napi_value GetCellularRxBytes(napi_env env, napi_callback_info info)
56 {
57     return ModuleTemplate::Interface<GetCellularRxBytesContext>(env, info, FUNCTION_GET_CELLULAR_RXBYTES, nullptr,
58                                                                 StatisticsAsyncWork::ExecGetCellularRxBytes,
59                                                                 StatisticsAsyncWork::GetCellularRxBytesCallback);
60 }
61 
GetCellularTxBytes(napi_env env,napi_callback_info info)62 napi_value GetCellularTxBytes(napi_env env, napi_callback_info info)
63 {
64     return ModuleTemplate::Interface<GetCellularTxBytesContext>(env, info, FUNCTION_GET_CELLULAR_TXBYTES, nullptr,
65                                                                 StatisticsAsyncWork::ExecGetCellularTxBytes,
66                                                                 StatisticsAsyncWork::GetCellularTxBytesCallback);
67 }
68 
GetAllRxBytes(napi_env env,napi_callback_info info)69 napi_value GetAllRxBytes(napi_env env, napi_callback_info info)
70 {
71     return ModuleTemplate::Interface<GetAllRxBytesContext>(env, info, FUNCTION_GET_ALL_RXBYTES, nullptr,
72                                                            StatisticsAsyncWork::ExecGetAllRxBytes,
73                                                            StatisticsAsyncWork::GetAllRxBytesCallback);
74 }
75 
GetAllTxBytes(napi_env env,napi_callback_info info)76 napi_value GetAllTxBytes(napi_env env, napi_callback_info info)
77 {
78     return ModuleTemplate::Interface<GetAllTxBytesContext>(env, info, FUNCTION_GET_ALL_TXBYTES, nullptr,
79                                                            StatisticsAsyncWork::ExecGetAllTxBytes,
80                                                            StatisticsAsyncWork::GetAllTxBytesCallback);
81 }
82 
GetUidRxBytes(napi_env env,napi_callback_info info)83 napi_value GetUidRxBytes(napi_env env, napi_callback_info info)
84 {
85     return ModuleTemplate::Interface<GetUidRxBytesContext>(env, info, FUNCTION_GET_UID_RXBYTES, nullptr,
86                                                            StatisticsAsyncWork::ExecGetUidRxBytes,
87                                                            StatisticsAsyncWork::GetUidRxBytesCallback);
88 }
89 
GetUidTxBytes(napi_env env,napi_callback_info info)90 napi_value GetUidTxBytes(napi_env env, napi_callback_info info)
91 {
92     return ModuleTemplate::Interface<GetUidTxBytesContext>(env, info, FUNCTION_GET_UID_TXBYTES, nullptr,
93                                                            StatisticsAsyncWork::ExecGetUidTxBytes,
94                                                            StatisticsAsyncWork::GetUidTxBytesCallback);
95 }
96 
GetIfaceRxBytes(napi_env env,napi_callback_info info)97 napi_value GetIfaceRxBytes(napi_env env, napi_callback_info info)
98 {
99     return ModuleTemplate::Interface<GetIfaceRxBytesContext>(env, info, FUNCTION_GET_IFACE_RXBYTES, nullptr,
100                                                              StatisticsAsyncWork::ExecGetIfaceRxBytes,
101                                                              StatisticsAsyncWork::GetIfaceRxBytesCallback);
102 }
103 
GetIfaceTxBytes(napi_env env,napi_callback_info info)104 napi_value GetIfaceTxBytes(napi_env env, napi_callback_info info)
105 {
106     return ModuleTemplate::Interface<GetIfaceTxBytesContext>(env, info, FUNCTION_GET_IFACE_TXBYTES, nullptr,
107                                                              StatisticsAsyncWork::ExecGetIfaceTxBytes,
108                                                              StatisticsAsyncWork::GetIfaceTxBytesCallback);
109 }
110 
GetIfaceStats(napi_env env,napi_callback_info info)111 napi_value GetIfaceStats(napi_env env, napi_callback_info info)
112 {
113     return ModuleTemplate::Interface<GetIfaceStatsContext>(env, info, FUNCTION_GET_IFACE_STATS, nullptr,
114                                                            StatisticsAsyncWork::ExecGetIfaceStats,
115                                                            StatisticsAsyncWork::GetIfaceStatsCallback);
116 }
117 
GetIfaceUidStats(napi_env env,napi_callback_info info)118 napi_value GetIfaceUidStats(napi_env env, napi_callback_info info)
119 {
120     return ModuleTemplate::Interface<GetIfaceUidStatsContext>(env, info, FUNCTION_GET_IFACE_UID_STATS, nullptr,
121                                                               StatisticsAsyncWork::ExecGetIfaceUidStats,
122                                                               StatisticsAsyncWork::GetIfaceUidStatsCallback);
123 }
124 
UpdateIfacesStats(napi_env env,napi_callback_info info)125 napi_value UpdateIfacesStats(napi_env env, napi_callback_info info)
126 {
127     return ModuleTemplate::Interface<UpdateIfacesStatsContext>(env, info, FUNCTION_UPDATE_IFACE_STATS, nullptr,
128                                                                StatisticsAsyncWork::ExecUpdateIfacesStats,
129                                                                StatisticsAsyncWork::UpdateIfacesStatsCallback);
130 }
131 
UpdateStatsData(napi_env env,napi_callback_info info)132 napi_value UpdateStatsData(napi_env env, napi_callback_info info)
133 {
134     return ModuleTemplate::Interface<UpdateStatsDataContext>(env, info, FUNCTION_UPDATE_STATS_DATA, nullptr,
135                                                              StatisticsAsyncWork::ExecUpdateStatsData,
136                                                              StatisticsAsyncWork::UpdateStatsDataCallback);
137 }
138 
On(napi_env env,napi_callback_info info)139 napi_value On(napi_env env, napi_callback_info info)
140 {
141     return StatisticsObserverWrapper::GetInstance().On(env, info, {EVENT_STATS_CHANGE}, false);
142 }
143 
Off(napi_env env,napi_callback_info info)144 napi_value Off(napi_env env, napi_callback_info info)
145 {
146     return StatisticsObserverWrapper::GetInstance().Off(env, info, {EVENT_STATS_CHANGE}, false);
147 }
148 
InitStatisticsModule(napi_env env,napi_value exports)149 napi_value InitStatisticsModule(napi_env env, napi_value exports)
150 {
151     NapiUtils::DefineProperties(env, exports,
152                                 {
153                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_CELLULAR_RXBYTES, GetCellularRxBytes),
154                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_CELLULAR_TXBYTES, GetCellularTxBytes),
155                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_RXBYTES, GetAllRxBytes),
156                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_TXBYTES, GetAllTxBytes),
157                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_UID_RXBYTES, GetUidRxBytes),
158                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_UID_TXBYTES, GetUidTxBytes),
159                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_RXBYTES, GetIfaceRxBytes),
160                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_TXBYTES, GetIfaceTxBytes),
161                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_STATS, GetIfaceStats),
162                                     DECLARE_NAPI_FUNCTION(FUNCTION_GET_IFACE_UID_STATS, GetIfaceUidStats),
163                                     DECLARE_NAPI_FUNCTION(FUNCTION_UPDATE_IFACE_STATS, UpdateIfacesStats),
164                                     DECLARE_NAPI_FUNCTION(FUNCTION_UPDATE_STATS_DATA, UpdateStatsData),
165                                     DECLARE_NAPI_FUNCTION(FUNCTION_ON, On),
166                                     DECLARE_NAPI_FUNCTION(FUNCTION_OFF, Off),
167                                 });
168     return exports;
169 }
170 
171 static napi_module g_statisticsModule = {
172     .nm_version = 1,
173     .nm_flags = 0,
174     .nm_filename = nullptr,
175     .nm_register_func = InitStatisticsModule,
176     .nm_modname = STATISTICS_MODULE_NAME,
177     .nm_priv = nullptr,
178     .reserved = {nullptr},
179 };
180 
RegisterNetStatsModule(void)181 extern "C" __attribute__((constructor)) void RegisterNetStatsModule(void)
182 {
183     napi_module_register(&g_statisticsModule);
184 }
185 } // namespace NetManagerStandard
186 } // namespace OHOS
187