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