• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ethernet_exec.h"
17 
18 #include <cstdint>
19 #include <new>
20 #include <numeric>
21 #include <string>
22 
23 #include "mac_address_info.h"
24 #include "ethernet_client.h"
25 #include "napi_utils.h"
26 #include "net_manager_constants.h"
27 #include "netmanager_ext_log.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace EthernetExec {
32 namespace {
33 constexpr const char *MODE = "mode";
34 constexpr const char *IP_ADDR = "ipAddr";
35 constexpr const char *ROUTE = "route";
36 constexpr const char *NET_MASK = "netMask";
37 constexpr const char *GATEWAY = "gateway";
38 constexpr const char *DNS_SERVERS = "dnsServers";
39 constexpr const char *DOMAIN = "domain";
40 constexpr const char *DEFAULT_SEPARATOR = ",";
41 constexpr const char *MAC_ADDR = "macAddress";
42 constexpr const char *IFACE = "iface";
43 constexpr const char *IFACE_NAME = "ifaceName";
44 constexpr const char *DEVICE_NAME = "deviceName";
45 constexpr const char *CONNECTION_MODE = "connectionMode";
46 constexpr const char *SUPPLIER_NAME = "supplierName";
47 constexpr const char *SUPPLIER_ID = "supplierId";
48 constexpr const char *PRODUCT_NAME = "productName";
49 constexpr const char *MAX_RATE = "maximumRate";
50 
AccumulateNetAddress(const std::vector<INetAddr> & netAddrList)51 std::string AccumulateNetAddress(const std::vector<INetAddr> &netAddrList)
52 {
53     return std::accumulate(
54         netAddrList.begin(), netAddrList.end(), std::string(), [](const std::string &addr, const INetAddr &iter) {
55             return addr.empty() ? (addr + iter.address_) : (addr + DEFAULT_SEPARATOR + iter.address_);
56         });
57 }
58 } // namespace
ExecGetMacAddress(GetMacAddressContext * context)59 bool ExecGetMacAddress(GetMacAddressContext *context)
60 {
61     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetMacAddress(context->macAddrInfo_);
62     if (result != NETMANAGER_EXT_SUCCESS) {
63         NETMANAGER_EXT_LOGE("ExecGetMacAddress error, errorCode: %{public}d", result);
64         context->SetErrorCode(result);
65         return false;
66     }
67     return true;
68 }
69 
GetMacAddressCallback(GetMacAddressContext * context)70 napi_value GetMacAddressCallback(GetMacAddressContext *context)
71 {
72     napi_value macAddressList = NapiUtils::CreateArray(context->GetEnv(), context->macAddrInfo_.size());
73     uint32_t index = 0;
74     for (auto &eachInfo : context->macAddrInfo_) {
75         napi_value macAddrInfo = NapiUtils::CreateObject(context->GetEnv());
76         NapiUtils::SetStringPropertyUtf8(
77             context->GetEnv(), macAddrInfo, IFACE, eachInfo.iface_);
78         NapiUtils::SetStringPropertyUtf8(
79             context->GetEnv(), macAddrInfo, MAC_ADDR, eachInfo.macAddress_);
80         NapiUtils::SetArrayElement(context->GetEnv(), macAddressList, index++, macAddrInfo);
81     }
82     return macAddressList;
83 }
84 
ExecGetIfaceConfig(GetIfaceConfigContext * context)85 bool ExecGetIfaceConfig(GetIfaceConfigContext *context)
86 {
87     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetIfaceConfig(context->iface_, context->config_);
88     if (context->config_ == nullptr || result != NETMANAGER_EXT_SUCCESS) {
89         NETMANAGER_EXT_LOGE("ExecGetIfaceConfig error, errorCode: %{public}d", result);
90         context->SetErrorCode(result);
91         return false;
92     }
93     return true;
94 }
95 
GetIfaceConfigCallback(GetIfaceConfigContext * context)96 napi_value GetIfaceConfigCallback(GetIfaceConfigContext *context)
97 {
98     napi_value interfaceConfiguration = NapiUtils::CreateObject(context->GetEnv());
99     NapiUtils::SetInt32Property(context->GetEnv(), interfaceConfiguration, MODE, context->config_->mode_);
100 
101     std::string ipAddresses = AccumulateNetAddress(context->config_->ipStatic_.ipAddrList_);
102     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, IP_ADDR, ipAddresses);
103 
104     std::string routeAddresses = AccumulateNetAddress(context->config_->ipStatic_.routeList_);
105     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, ROUTE, routeAddresses);
106 
107     std::string gatewayAddresses = AccumulateNetAddress(context->config_->ipStatic_.gatewayList_);
108     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, GATEWAY, gatewayAddresses);
109 
110     std::string maskAddresses = AccumulateNetAddress(context->config_->ipStatic_.netMaskList_);
111     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, NET_MASK, maskAddresses);
112 
113     std::string dnsServers = AccumulateNetAddress(context->config_->ipStatic_.dnsServers_);
114     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, DNS_SERVERS, dnsServers);
115 
116     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, DOMAIN,
117                                      context->config_->ipStatic_.domain_);
118     return interfaceConfiguration;
119 }
120 
ExecSetIfaceConfig(SetIfaceConfigContext * context)121 bool ExecSetIfaceConfig(SetIfaceConfigContext *context)
122 {
123     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->SetIfaceConfig(context->iface_, context->config_);
124     if (result != NETMANAGER_EXT_SUCCESS) {
125         NETMANAGER_EXT_LOGE("ExecSetIfaceConfig error, errorCode: %{public}d", result);
126         context->SetErrorCode(result);
127         return false;
128     }
129     return true;
130 }
131 
SetIfaceConfigCallback(SetIfaceConfigContext * context)132 napi_value SetIfaceConfigCallback(SetIfaceConfigContext *context)
133 {
134     return NapiUtils::GetUndefined(context->GetEnv());
135 }
136 
ExecIsIfaceActive(IsIfaceActiveContext * context)137 bool ExecIsIfaceActive(IsIfaceActiveContext *context)
138 {
139     int32_t result =
140         DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(context->iface_, context->ifActivate_);
141     if (result != NETMANAGER_EXT_SUCCESS) {
142         context->SetErrorCode(result);
143         NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
144         return false;
145     }
146     return true;
147 }
148 
IsIfaceActiveCallback(IsIfaceActiveContext * context)149 napi_value IsIfaceActiveCallback(IsIfaceActiveContext *context)
150 {
151     return NapiUtils::CreateInt32(context->GetEnv(), context->ifActivate_);
152 }
153 
ExecGetAllActiveIfaces(GetAllActiveIfacesContext * context)154 bool ExecGetAllActiveIfaces(GetAllActiveIfacesContext *context)
155 {
156     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetAllActiveIfaces(context->ethernetNameList_);
157     if (result != NETMANAGER_EXT_SUCCESS) {
158         context->SetErrorCode(result);
159         NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
160         return false;
161     }
162     return true;
163 }
164 
GetAllActiveIfacesCallback(GetAllActiveIfacesContext * context)165 napi_value GetAllActiveIfacesCallback(GetAllActiveIfacesContext *context)
166 {
167     napi_value ifaces = NapiUtils::CreateArray(context->GetEnv(), context->ethernetNameList_.size());
168     uint32_t index = 0;
169     for (const auto &iface : context->ethernetNameList_) {
170         napi_value ifaceStr = NapiUtils::CreateStringUtf8(context->GetEnv(), iface);
171         NapiUtils::SetArrayElement(context->GetEnv(), ifaces, index++, ifaceStr);
172     }
173     return ifaces;
174 }
175 
ExecGetDeviceInformation(GetDeviceInformationContext * context)176 bool ExecGetDeviceInformation(GetDeviceInformationContext *context)
177 {
178     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetDeviceInformation(context->deviceInfo_);
179     if (result != NETMANAGER_EXT_SUCCESS) {
180         NETMANAGER_EXT_LOGE("ExecGetDeviceInformation, err: %{public}d", result);
181         context->SetErrorCode(result);
182         return false;
183     }
184     return true;
185 }
186 
GetDeviceInformationCallback(GetDeviceInformationContext * context)187 napi_value GetDeviceInformationCallback(GetDeviceInformationContext *context)
188 {
189     napi_value deviceInfoList = NapiUtils::CreateArray(context->GetEnv(), context->deviceInfo_.size());
190     uint32_t index = 0;
191     for (auto &eachInfo : context->deviceInfo_) {
192         napi_value deviceInfo = NapiUtils::CreateObject(context->GetEnv());
193         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, IFACE_NAME, eachInfo.ifaceName_);
194         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, DEVICE_NAME, eachInfo.deviceName_);
195         NapiUtils::SetInt32Property(context->GetEnv(), deviceInfo, CONNECTION_MODE, eachInfo.connectionMode_);
196         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, SUPPLIER_NAME, eachInfo.supplierName_);
197         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, SUPPLIER_ID, eachInfo.supplierId_);
198         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, PRODUCT_NAME, eachInfo.productName_);
199         NapiUtils::SetStringPropertyUtf8(context->GetEnv(), deviceInfo, MAX_RATE, eachInfo.maximumRate_);
200         NapiUtils::SetArrayElement(context->GetEnv(), deviceInfoList, index++, deviceInfo);
201     }
202     return deviceInfoList;
203 }
204 } // namespace EthernetExec
205 } // namespace NetManagerStandard
206 } // namespace OHOS
207