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 "usb_manager_proxy.h"
17
18 #include "edm_constants.h"
19 #include "edm_ipc_interface_code.h"
20 #include "edm_log.h"
21 #include "func_code.h"
22 #include "message_parcel_utils.h"
23 #include "usb_device_id.h"
24
25 namespace OHOS {
26 namespace EDM {
27 std::shared_ptr<UsbManagerProxy> UsbManagerProxy::instance_ = nullptr;
28 std::mutex UsbManagerProxy::mutexLock_;
29 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
30
GetUsbManagerProxy()31 std::shared_ptr<UsbManagerProxy> UsbManagerProxy::GetUsbManagerProxy()
32 {
33 if (instance_ == nullptr) {
34 std::lock_guard<std::mutex> lock(mutexLock_);
35 if (instance_ == nullptr) {
36 std::shared_ptr<UsbManagerProxy> temp = std::make_shared<UsbManagerProxy>();
37 instance_ = temp;
38 }
39 }
40 return instance_;
41 }
42
SetUsbReadOnly(const AppExecFwk::ElementName & admin,bool readOnly)43 int32_t UsbManagerProxy::SetUsbReadOnly(const AppExecFwk::ElementName &admin, bool readOnly)
44 {
45 EDMLOGD("UsbManagerProxy::SetUsbReadOnly");
46 MessageParcel data;
47 std::uint32_t funcCode =
48 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::USB_READ_ONLY);
49 data.WriteInterfaceToken(DESCRIPTOR);
50 data.WriteInt32(WITHOUT_USERID);
51 data.WriteParcelable(&admin);
52 data.WriteString(WITHOUT_PERMISSION_TAG);
53 data.WriteInt32(readOnly ? 1 : 0);
54 ErrCode ret = EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
55 return ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED ? EdmReturnErrCode::SYSTEM_ABNORMALLY : ret;
56 }
57
DisableUsb(const AppExecFwk::ElementName & admin,bool disable)58 int32_t UsbManagerProxy::DisableUsb(const AppExecFwk::ElementName &admin, bool disable)
59 {
60 EDMLOGD("UsbManagerProxy::DisableUsb");
61 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
62 return proxy->SetPolicyDisabled(admin, disable, EdmInterfaceCode::DISABLE_USB,
63 EdmConstants::PERMISSION_TAG_VERSION_11);
64 }
65
IsUsbDisabled(const AppExecFwk::ElementName * admin,bool & result)66 int32_t UsbManagerProxy::IsUsbDisabled(const AppExecFwk::ElementName *admin, bool &result)
67 {
68 EDMLOGD("UsbManagerProxy::IsUsbDisabled");
69 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
70 return proxy->IsPolicyDisabled(admin, EdmInterfaceCode::DISABLE_USB, result,
71 EdmConstants::PERMISSION_TAG_VERSION_11);
72 }
73
AddAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> usbDeviceIds)74 int32_t UsbManagerProxy::AddAllowedUsbDevices(const AppExecFwk::ElementName &admin,
75 std::vector<UsbDeviceId> usbDeviceIds)
76 {
77 EDMLOGD("UsbManagerProxy::AddAllowedUsbDevices");
78 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
79 MessageParcel data;
80 std::uint32_t funcCode =
81 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ALLOWED_USB_DEVICES);
82 data.WriteInterfaceToken(DESCRIPTOR);
83 data.WriteInt32(WITHOUT_USERID);
84 data.WriteParcelable(&admin);
85 data.WriteString(WITHOUT_PERMISSION_TAG);
86 data.WriteInt32(usbDeviceIds.size());
87 for (const auto &usbDeviceId : usbDeviceIds) {
88 if (!usbDeviceId.Marshalling(data)) {
89 EDMLOGE("UsbManagerProxy AddAllowedUsbDevices: write parcel failed!");
90 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
91 }
92 }
93 return proxy->HandleDevicePolicy(funcCode, data);
94 }
95
RemoveAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> usbDeviceIds)96 int32_t UsbManagerProxy::RemoveAllowedUsbDevices(const AppExecFwk::ElementName &admin,
97 std::vector<UsbDeviceId> usbDeviceIds)
98 {
99 EDMLOGD("UsbManagerProxy::RemoveAllowedUsbDevices");
100 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
101 MessageParcel data;
102 std::uint32_t funcCode =
103 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::ALLOWED_USB_DEVICES);
104 data.WriteInterfaceToken(DESCRIPTOR);
105 data.WriteInt32(WITHOUT_USERID);
106 data.WriteParcelable(&admin);
107 data.WriteString(WITHOUT_PERMISSION_TAG);
108 data.WriteInt32(usbDeviceIds.size());
109 for (const auto &usbDeviceId : usbDeviceIds) {
110 if (!usbDeviceId.Marshalling(data)) {
111 EDMLOGE("UsbManagerProxy RemoveAllowedUsbDevices: write parcel failed!");
112 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
113 }
114 }
115 return proxy->HandleDevicePolicy(funcCode, data);
116 }
117
GetAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> & result)118 int32_t UsbManagerProxy::GetAllowedUsbDevices(const AppExecFwk::ElementName &admin, std::vector<UsbDeviceId> &result)
119 {
120 EDMLOGD("UsbManagerProxy::GetAllowedUsbDevices");
121 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
122 MessageParcel data;
123 MessageParcel reply;
124 data.WriteInterfaceToken(DESCRIPTOR);
125 data.WriteInt32(WITHOUT_USERID);
126 data.WriteString(WITHOUT_PERMISSION_TAG);
127 data.WriteInt32(HAS_ADMIN);
128 data.WriteParcelable(&admin);
129 proxy->GetPolicy(EdmInterfaceCode::ALLOWED_USB_DEVICES, data, reply);
130 int32_t ret = ERR_INVALID_VALUE;
131 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
132 if (!blRes) {
133 EDMLOGW("UsbManagerProxy:GetAllowedUsbDevices fail. %{public}d", ret);
134 return ret;
135 }
136 int32_t size = reply.ReadInt32();
137 if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
138 EDMLOGE("UsbManagerProxy:GetAllowedUsbDevices size=[%{public}d] is too large", size);
139 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
140 }
141 EDMLOGI("UsbManagerProxy:GetAllowedUsbDevices return size:%{public}d", size);
142 for (int i = 0; i < size; i++) {
143 UsbDeviceId usbDeviceId;
144 if (!UsbDeviceId::Unmarshalling(reply, usbDeviceId)) {
145 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
146 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
147 }
148 result.emplace_back(usbDeviceId);
149 }
150 return ERR_OK;
151 }
152
SetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName & admin,int32_t usbPolicy)153 int32_t UsbManagerProxy::SetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName &admin, int32_t usbPolicy)
154 {
155 EDMLOGD("UsbManagerProxy::SetUsbStorageDeviceAccessPolicy");
156 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
157 MessageParcel data;
158 std::uint32_t funcCode =
159 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::USB_READ_ONLY);
160 data.WriteInterfaceToken(DESCRIPTOR);
161 data.WriteInt32(WITHOUT_USERID);
162 data.WriteParcelable(&admin);
163 data.WriteString(WITHOUT_PERMISSION_TAG);
164 data.WriteInt32(usbPolicy);
165 return proxy->HandleDevicePolicy(funcCode, data);
166 }
167
GetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName & admin,int32_t & result)168 int32_t UsbManagerProxy::GetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName &admin, int32_t &result)
169 {
170 EDMLOGD("UsbManagerProxy::GetUsbStorageDeviceAccessPolicy");
171 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
172 MessageParcel data;
173 MessageParcel reply;
174 data.WriteInterfaceToken(DESCRIPTOR);
175 data.WriteInt32(WITHOUT_USERID);
176 data.WriteString(WITHOUT_PERMISSION_TAG);
177 data.WriteInt32(HAS_ADMIN);
178 data.WriteParcelable(&admin);
179 proxy->GetPolicy(EdmInterfaceCode::USB_READ_ONLY, data, reply);
180 int32_t ret = ERR_INVALID_VALUE;
181 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
182 if (!blRes) {
183 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
184 return ret;
185 }
186 reply.ReadInt32(result);
187 return ERR_OK;
188 }
189 } // namespace EDM
190 } // namespace OHOS
191