• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "device_control_proxy.h"
17 
18 #include "edm_log.h"
19 #include "func_code.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 std::shared_ptr<DeviceControlProxy> DeviceControlProxy::instance_ = nullptr;
24 std::mutex DeviceControlProxy::mutexLock_;
25 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
26 
DeviceControlProxy()27 DeviceControlProxy::DeviceControlProxy() {}
28 
~DeviceControlProxy()29 DeviceControlProxy::~DeviceControlProxy() {}
30 
GetDeviceControlProxy()31 std::shared_ptr<DeviceControlProxy> DeviceControlProxy::GetDeviceControlProxy()
32 {
33     if (instance_ == nullptr) {
34         std::lock_guard<std::mutex> lock(mutexLock_);
35         if (instance_ == nullptr) {
36             std::shared_ptr<DeviceControlProxy> temp = std::make_shared<DeviceControlProxy>();
37             instance_ = temp;
38         }
39     }
40     return instance_;
41 }
42 
ResetFactory(AppExecFwk::ElementName & admin)43 int32_t DeviceControlProxy::ResetFactory(AppExecFwk::ElementName &admin)
44 {
45     EDMLOGD("DeviceControlProxy::ResetFactory");
46     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
47     if (proxy == nullptr) {
48         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
49         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
50     }
51     MessageParcel data;
52     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::RESET_FACTORY);
53     data.WriteInterfaceToken(DESCRIPTOR);
54     data.WriteInt32(WITHOUT_USERID);
55     data.WriteParcelable(&admin);
56     return proxy->HandleDevicePolicy(funcCode, data);
57 }
58 
Shutdown(AppExecFwk::ElementName & admin)59 int32_t DeviceControlProxy::Shutdown(AppExecFwk::ElementName &admin)
60 {
61     EDMLOGD("DeviceControlProxy::Shutdown");
62     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
63     if (proxy == nullptr) {
64         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
65         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
66     }
67     MessageParcel data;
68     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SHUTDOWN);
69     data.WriteInterfaceToken(DESCRIPTOR);
70     data.WriteInt32(WITHOUT_USERID);
71     data.WriteParcelable(&admin);
72     return proxy->HandleDevicePolicy(funcCode, data);
73 }
74 
Reboot(AppExecFwk::ElementName & admin)75 int32_t DeviceControlProxy::Reboot(AppExecFwk::ElementName &admin)
76 {
77     EDMLOGD("DeviceControlProxy::Reboot");
78     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
79     if (proxy == nullptr) {
80         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
81         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
82     }
83     MessageParcel data;
84     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::REBOOT);
85     data.WriteInterfaceToken(DESCRIPTOR);
86     data.WriteInt32(WITHOUT_USERID);
87     data.WriteParcelable(&admin);
88     return proxy->HandleDevicePolicy(funcCode, data);
89 }
90 
LockScreen(AppExecFwk::ElementName & admin,int32_t userId)91 int32_t DeviceControlProxy::LockScreen(AppExecFwk::ElementName &admin, int32_t userId)
92 {
93     EDMLOGD("DeviceControlProxy::LockScreen");
94     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
95     if (proxy == nullptr) {
96         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
97         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
98     }
99     MessageParcel data;
100     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::LOCK_SCREEN);
101     data.WriteInterfaceToken(DESCRIPTOR);
102     data.WriteInt32(WITHOUT_USERID);
103     data.WriteParcelable(&admin);
104     data.WriteInt32(userId);
105     EDMLOGD("DeviceControlProxy LockScreen userId = %{public}d.", userId);
106     return proxy->HandleDevicePolicy(funcCode, data);
107 }
108 } // namespace EDM
109 } // namespace OHOS