1 /*
2 * Copyright (C) 2022-2024 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 <napi/native_api.h>
17 #include <napi/native_common.h>
18
19 #include "ethernet_async_work.h"
20 #include "get_all_active_ifaces_context.h"
21 #include "get_device_infomation.h"
22 #include "get_mac_address_context.h"
23 #include "get_iface_config_context.h"
24 #include "mac_address_info.h"
25 #include "interface_configuration.h"
26 #include "interface_state_observer_wrapper.h"
27 #include "is_iface_active_context.h"
28 #include "set_iface_config_context.h"
29 #include "module_template.h"
30 #include "napi_utils.h"
31
32 static constexpr const char *EVENT_STATS_CHANGE = "interfaceStateChange";
33
34 namespace OHOS {
35 namespace NetManagerStandard {
36 namespace {
37 constexpr const char *GET_MAC_ADDR = "getMacAddress";
38 constexpr const char *GET_IFACE = "getIfaceConfig";
39 constexpr const char *SET_IFACE = "setIfaceConfig";
40 constexpr const char *IS_IFACE = "isIfaceActive";
41 constexpr const char *GET_ALL_IFACES = "getAllActiveIfaces";
42 constexpr const char *STATIC_NAME = "STATIC";
43 constexpr const char *DHCP_NAME = "DHCP";
44 constexpr const char *IP_SET_MODE = "IPSetMode";
45 constexpr const char *FUNCTION_ON = "on";
46 constexpr const char *FUNCTION_OFF = "off";
47 constexpr const char *GET_DEVICE_INFO = "getEthernetDeviceInfos";
48
GetMacAddress(napi_env env,napi_callback_info info)49 napi_value GetMacAddress(napi_env env, napi_callback_info info)
50 {
51 return ModuleTemplate::Interface<GetMacAddressContext>(env, info, GET_MAC_ADDR, nullptr,
52 EthernetAsyncWork::ExecGetMacAddress, EthernetAsyncWork::GetMacAddressCallback);
53 }
54
GetIfaceConfig(napi_env env,napi_callback_info info)55 napi_value GetIfaceConfig(napi_env env, napi_callback_info info)
56 {
57 return ModuleTemplate::Interface<GetIfaceConfigContext>(env, info, GET_IFACE, nullptr,
58 EthernetAsyncWork::ExecGetIfaceConfig, EthernetAsyncWork::GetIfaceConfigCallback);
59 }
60
SetIfaceConfig(napi_env env,napi_callback_info info)61 napi_value SetIfaceConfig(napi_env env, napi_callback_info info)
62 {
63 return ModuleTemplate::Interface<SetIfaceConfigContext>(env, info, SET_IFACE, nullptr,
64 EthernetAsyncWork::ExecSetIfaceConfig, EthernetAsyncWork::SetIfaceConfigCallback);
65 }
66
IsIfaceActive(napi_env env,napi_callback_info info)67 napi_value IsIfaceActive(napi_env env, napi_callback_info info)
68 {
69 return ModuleTemplate::Interface<IsIfaceActiveContext>(env, info, IS_IFACE, nullptr,
70 EthernetAsyncWork::ExecIsIfaceActive, EthernetAsyncWork::IsIfaceActiveCallback);
71 }
72
GetAllActiveIfaces(napi_env env,napi_callback_info info)73 napi_value GetAllActiveIfaces(napi_env env, napi_callback_info info)
74 {
75 return ModuleTemplate::Interface<GetAllActiveIfacesContext>(env, info, GET_ALL_IFACES, nullptr,
76 EthernetAsyncWork::ExecGetAllActiveIfaces, EthernetAsyncWork::GetAllActiveIfacesCallback);
77 }
78 } // namespace
79
DeclareEthernetData(napi_env env,napi_value exports)80 static napi_value DeclareEthernetData(napi_env env, napi_value exports)
81 {
82 NapiUtils::DefineProperties(env, exports, {
83 DECLARE_NAPI_STATIC_PROPERTY(STATIC_NAME, NapiUtils::CreateInt32(env, static_cast<int32_t>(STATIC))),
84 DECLARE_NAPI_STATIC_PROPERTY(DHCP_NAME, NapiUtils::CreateInt32(env, static_cast<int32_t>(DHCP))),
85 });
86 return exports;
87 }
88
On(napi_env env,napi_callback_info info)89 napi_value On(napi_env env, napi_callback_info info)
90 {
91 return InterfaceStateObserverWrapper::GetInstance().On(env, info, {EVENT_STATS_CHANGE}, false);
92 }
93
Off(napi_env env,napi_callback_info info)94 napi_value Off(napi_env env, napi_callback_info info)
95 {
96 return InterfaceStateObserverWrapper::GetInstance().Off(env, info, {EVENT_STATS_CHANGE}, false);
97 }
98
GetDeviceInformation(napi_env env,napi_callback_info info)99 napi_value GetDeviceInformation(napi_env env, napi_callback_info info)
100 {
101 return ModuleTemplate::Interface<GetDeviceInformationContext>(env, info, GET_DEVICE_INFO, nullptr,
102 EthernetAsyncWork::ExecGetDeviceInformation, EthernetAsyncWork::GetDeviceInformationCallback);
103 }
104
DeclareEthernetInterface(napi_env env,napi_value exports)105 static napi_value DeclareEthernetInterface(napi_env env, napi_value exports)
106 {
107 NapiUtils::DefineProperties(env, exports, {
108 DECLARE_NAPI_FUNCTION(GET_MAC_ADDR, GetMacAddress),
109 DECLARE_NAPI_FUNCTION(GET_IFACE, GetIfaceConfig),
110 DECLARE_NAPI_FUNCTION(SET_IFACE, SetIfaceConfig),
111 DECLARE_NAPI_FUNCTION(IS_IFACE, IsIfaceActive),
112 DECLARE_NAPI_FUNCTION(GET_ALL_IFACES, GetAllActiveIfaces),
113 DECLARE_NAPI_FUNCTION(FUNCTION_ON, On),
114 DECLARE_NAPI_FUNCTION(FUNCTION_OFF, Off),
115 DECLARE_NAPI_FUNCTION(GET_DEVICE_INFO, GetDeviceInformation),
116 });
117 return exports;
118 }
119
AddCleanupHook(napi_env env)120 static void AddCleanupHook(napi_env env)
121 {
122 NapiUtils::SetEnvValid(env);
123 auto envWrapper = new (std::nothrow) napi_env;
124 if (envWrapper == nullptr) {
125 NETMANAGER_BASE_LOGE("EnvWrapper create fail!");
126 return;
127 }
128 *envWrapper = env;
129 napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper);
130 }
131
RegisterEthernetInterface(napi_env env,napi_value exports)132 napi_value RegisterEthernetInterface(napi_env env, napi_value exports)
133 {
134 DeclareEthernetInterface(env, exports);
135 DeclareEthernetData(env, exports);
136
137 std::initializer_list<napi_property_descriptor> ipSetMode = {
138 DECLARE_NAPI_STATIC_PROPERTY(STATIC_NAME,
139 NapiUtils::CreateUint32(env, static_cast<uint32_t>(IPSetMode::STATIC))),
140 DECLARE_NAPI_STATIC_PROPERTY(DHCP_NAME, NapiUtils::CreateUint32(env, static_cast<uint32_t>(IPSetMode::DHCP))),
141 };
142 napi_value ipSetMOdes = NapiUtils::CreateObject(env);
143 NapiUtils::DefineProperties(env, ipSetMOdes, ipSetMode);
144 NapiUtils::SetNamedProperty(env, exports, IP_SET_MODE, ipSetMOdes);
145 AddCleanupHook(env);
146 return exports;
147 }
148
149 static napi_module g_ethernetModule = {
150 .nm_version = 1,
151 .nm_flags = 0,
152 .nm_filename = nullptr,
153 .nm_register_func = RegisterEthernetInterface,
154 .nm_modname = "net.ethernet",
155 .nm_priv = (0),
156 .reserved = {0},
157 };
158
RegisterEthernetModule(void)159 extern "C" __attribute__((constructor)) void RegisterEthernetModule(void)
160 {
161 napi_module_register(&g_ethernetModule);
162 }
163 } // namespace NetManagerStandard
164 } // namespace OHOS
165