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 "operate_device_plugin.h"
17 
18 #include "power_mgr_client.h"
19 #include "screenlock_manager.h"
20 #include "update_service_kits.h"
21 
22 #include "edm_ipc_interface_code.h"
23 #include "operate_device_param_serializer.h"
24 #include "plugin_manager.h"
25 
26 namespace OHOS {
27 namespace EDM {
28 
29 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(OperateDevicePlugin::GetPlugin());
30 
InitPlugin(std::shared_ptr<IPluginTemplate<OperateDevicePlugin,OperateDeviceParam>> ptr)31 void OperateDevicePlugin::InitPlugin(std::shared_ptr<IPluginTemplate<OperateDevicePlugin, OperateDeviceParam>> ptr)
32 {
33     EDMLOGI("OperateDevicePlugin InitPlugin...");
34     ptr->InitAttribute(EdmInterfaceCode::OPERATE_DEVICE, "operate_device", "ohos.permission.ENTERPRISE_OPERATE_DEVICE",
35         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
36     ptr->SetSerializer(OperateDeviceParamSerializer::GetInstance());
37     ptr->SetOnHandlePolicyListener(&OperateDevicePlugin::OnSetPolicy, FuncOperateType::SET);
38 }
39 
OnSetPolicy(OperateDeviceParam & param,MessageParcel & reply)40 ErrCode OperateDevicePlugin::OnSetPolicy(OperateDeviceParam &param, MessageParcel &reply)
41 {
42     EDMLOGD("OperateDevicePlugin OnSetPolicy operate = %{public}s, userId = %{public}d", param.operate.c_str(),
43         param.userId);
44     if (param.operate == EdmConstants::DeviceControl::LOCK_SCREEN) {
45         int32_t ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(param.userId);
46         if (ret != ScreenLock::E_SCREENLOCK_OK) {
47             EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", ret);
48             reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
49             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
50         }
51         reply.WriteInt32(ERR_OK);
52         return ERR_OK;
53     }
54     if (param.operate == EdmConstants::DeviceControl::SHUT_DOWN ||
55         param.operate == EdmConstants::DeviceControl::REBOOT) {
56         auto& powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
57         PowerMgr::PowerErrors ret;
58         if (param.operate == EdmConstants::DeviceControl::SHUT_DOWN) {
59             ret = powerMgrClient.ShutDownDevice("edm_Shutdown");
60         } else {
61             ret = powerMgrClient.RebootDevice("edm_Reboot");
62         }
63         if (ret != PowerMgr::PowerErrors::ERR_OK) {
64             EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", int32_t(ret));
65             reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
66             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
67         }
68         reply.WriteInt32(ERR_OK);
69         return ERR_OK;
70     }
71     if (param.operate == EdmConstants::DeviceControl::RESET_FACTORY) {
72         UpdateEngine::BusinessError businessError;
73         int32_t ret = UpdateEngine::UpdateServiceKits::GetInstance().FactoryReset(businessError);
74         if (FAILED(ret)) {
75             EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", ret);
76             reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
77             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
78         }
79         reply.WriteInt32(ERR_OK);
80         return ERR_OK;
81     }
82     reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
83     OperateDeviceParamSerializer::GetInstance()->WritePolicy(reply, param);
84     return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
85 }
86 } // namespace EDM
87 } // namespace OHOS
88