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 "iplugin_manager.h"
25
26 namespace OHOS {
27 namespace EDM {
28
29 const bool REGISTER_RESULT = IPluginManager::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, PolicyName::POLICY_OPERATE_DEVICE,
35 EdmPermission::PERMISSION_ENTERPRISE_OPERATE_DEVICE, 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 ¶m, MessageParcel &reply)
41 {
42 EDMLOGD("OperateDevicePlugin OnSetPolicy operate = %{public}s", param.operate.c_str());
43 if (param.operate == EdmConstants::DeviceControl::LOCK_SCREEN) {
44 int32_t ret = ScreenLock::ScreenLockManager::GetInstance()->Lock(param.userId);
45 if (ret != ScreenLock::E_SCREENLOCK_OK) {
46 EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", ret);
47 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
48 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
49 }
50 reply.WriteInt32(ERR_OK);
51 return ERR_OK;
52 }
53 if (param.operate == EdmConstants::DeviceControl::SHUT_DOWN ||
54 param.operate == EdmConstants::DeviceControl::REBOOT) {
55 auto& powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
56 PowerMgr::PowerErrors ret;
57 if (param.operate == EdmConstants::DeviceControl::SHUT_DOWN) {
58 ret = powerMgrClient.ShutDownDevice("edm_Shutdown");
59 } else {
60 ret = powerMgrClient.RebootDevice("edm_Reboot");
61 }
62 if (ret != PowerMgr::PowerErrors::ERR_OK) {
63 EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", int32_t(ret));
64 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
65 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
66 }
67 reply.WriteInt32(ERR_OK);
68 return ERR_OK;
69 }
70 if (param.operate == EdmConstants::DeviceControl::RESET_FACTORY) {
71 UpdateService::BusinessError businessError;
72 int32_t ret = UpdateService::UpdateServiceKits::GetInstance().FactoryReset(businessError);
73 if (FAILED(ret)) {
74 EDMLOGE("OperateDevicePlugin:OnSetPolicy send request fail. %{public}d", ret);
75 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
76 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
77 }
78 reply.WriteInt32(ERR_OK);
79 return ERR_OK;
80 }
81 reply.WriteInt32(EdmReturnErrCode::INTERFACE_UNSUPPORTED);
82 OperateDeviceParamSerializer::GetInstance()->WritePolicy(reply, param);
83 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
84 }
85 } // namespace EDM
86 } // namespace OHOS
87