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