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