1 /*
2  * Copyright (c) 2023 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 "get_ip_address_plugin.h"
17 
18 #include "edm_constants.h"
19 #include "edm_ipc_interface_code.h"
20 #include "ethernet_client.h"
21 #include "interface_type.h"
22 #include "string_serializer.h"
23 #include "plugin_manager.h"
24 
25 namespace OHOS {
26 namespace EDM {
27 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(GetIpAddressPlugin::GetPlugin());
28 
InitPlugin(std::shared_ptr<IPluginTemplate<GetIpAddressPlugin,std::string>> ptr)29 void GetIpAddressPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<GetIpAddressPlugin, std::string>> ptr)
30 {
31     EDMLOGI("GetIpAddressPlugin InitPlugin...");
32     std::map<std::string, std::string> perms;
33     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_11,
34         "ohos.permission.ENTERPRISE_GET_NETWORK_INFO"));
35     perms.insert(std::make_pair(EdmConstants::PERMISSION_TAG_VERSION_12, "ohos.permission.ENTERPRISE_MANAGE_NETWORK"));
36     IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(perms,
37         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, IPlugin::ApiType::PUBLIC);
38     ptr->InitAttribute(EdmInterfaceCode::GET_IP_ADDRESS, "get_ip_address", config, false);
39     ptr->SetSerializer(StringSerializer::GetInstance());
40 }
41 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)42 ErrCode GetIpAddressPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
43     int32_t userId)
44 {
45     EDMLOGI("GetIpAddressPlugin OnGetPolicy.");
46     nmd::InterfaceConfigurationParcel config;
47     std::string networkInterface;
48     data.ReadString(networkInterface);
49     DelayedSingleton<NetManagerStandard::EthernetClient>::GetInstance()->GetInterfaceConfig(networkInterface, config);
50     reply.WriteInt32(ERR_OK);
51     reply.WriteString(config.ipv4Addr);
52     return ERR_OK;
53 }
54 } // namespace EDM
55 } // namespace OHOS
56