1 /*
2 * Copyright (c) 2023-2024 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.WriteUint32(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.WriteUint32(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 uint32_t size = reply.ReadUint32();
137 if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
138 EDMLOGE("UsbManagerProxy:GetAllowedUsbDevices size=[%{public}u] is too large", size);
139 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
140 }
141 EDMLOGI("UsbManagerProxy:GetAllowedUsbDevices return size:%{public}u", size);
142 for (uint32_t 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
190 #ifdef USB_EDM_ENABLE
AddOrRemoveDisallowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<OHOS::USB::UsbDeviceType> usbDeviceTypes,bool isAdd)191 int32_t UsbManagerProxy::AddOrRemoveDisallowedUsbDevices(const AppExecFwk::ElementName &admin,
192 std::vector<OHOS::USB::UsbDeviceType> usbDeviceTypes, bool isAdd)
193 {
194 EDMLOGD("UsbManagerProxy::AddOrRemoveDisallowedUsbDevices");
195 size_t size = usbDeviceTypes.size();
196 if (size > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
197 EDMLOGE("UsbManagerProxy:AddOrRemoveDisallowedUsbDevices size=[%{public}zu] is too large", size);
198 return EdmReturnErrCode::PARAM_ERROR;
199 }
200 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
201 if (proxy == nullptr) {
202 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
203 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
204 }
205 MessageParcel data;
206 std::uint32_t funcCode = 0;
207 if (isAdd) {
208 funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
209 } else {
210 funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
211 }
212 data.WriteInterfaceToken(DESCRIPTOR);
213 data.WriteInt32(WITHOUT_USERID);
214 data.WriteParcelable(&admin);
215 data.WriteString(WITHOUT_PERMISSION_TAG);
216 data.WriteUint32(size);
217 for (const auto &usbDeviceType : usbDeviceTypes) {
218 if (!usbDeviceType.Marshalling(data)) {
219 EDMLOGE("UsbManagerProxy AddOrRemoveDisallowedUsbDevices: write parcel failed!");
220 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
221 }
222 }
223 EDMLOGI("UsbManagerProxy::AddOrRemoveDisallowedUsbDevices funcCode: %{public}u, usbDeviceTypes.size: %{public}zu",
224 funcCode, size);
225 return proxy->HandleDevicePolicy(funcCode, data);
226 }
227
GetDisallowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<OHOS::USB::UsbDeviceType> & result)228 int32_t UsbManagerProxy::GetDisallowedUsbDevices(const AppExecFwk::ElementName &admin,
229 std::vector<OHOS::USB::UsbDeviceType> &result)
230 {
231 EDMLOGD("UsbManagerProxy::GetDisallowedUsbDevices");
232 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
233 if (proxy == nullptr) {
234 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
235 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
236 }
237 MessageParcel data;
238 MessageParcel reply;
239 data.WriteInterfaceToken(DESCRIPTOR);
240 data.WriteInt32(WITHOUT_USERID);
241 data.WriteString(WITHOUT_PERMISSION_TAG);
242 data.WriteInt32(HAS_ADMIN);
243 data.WriteParcelable(&admin);
244 proxy->GetPolicy(EdmInterfaceCode::DISALLOWED_USB_DEVICES, data, reply);
245 int32_t ret = ERR_INVALID_VALUE;
246 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
247 if (!blRes) {
248 EDMLOGW("UsbManagerProxy:GetDisallowedUsbDevices fail. %{public}d", ret);
249 return ret;
250 }
251 uint32_t size = reply.ReadUint32();
252 if (size > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
253 EDMLOGE("UsbManagerProxy:GetDisallowedUsbDevices size=[%{public}u] is too large", size);
254 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
255 }
256 EDMLOGI("UsbManagerProxy:GetDisallowedUsbDevices return size:%{public}u", size);
257 for (uint32_t i = 0; i < size; i++) {
258 OHOS::USB::UsbDeviceType usbDeviceType;
259 if (!OHOS::USB::UsbDeviceType::Unmarshalling(reply, usbDeviceType)) {
260 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
261 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
262 }
263 result.emplace_back(usbDeviceType);
264 }
265 return ERR_OK;
266 }
267 #endif
268 } // namespace EDM
269 } // namespace OHOS
270