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::once_flag UsbManagerProxy::flag_;
29 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
30
GetUsbManagerProxy()31 std::shared_ptr<UsbManagerProxy> UsbManagerProxy::GetUsbManagerProxy()
32 {
33 std::call_once(flag_, []() {
34 if (instance_ == nullptr) {
35 instance_ = std::make_shared<UsbManagerProxy>();
36 }
37 });
38 return instance_;
39 }
40
SetUsbReadOnly(MessageParcel & data)41 int32_t UsbManagerProxy::SetUsbReadOnly(MessageParcel &data)
42 {
43 EDMLOGD("UsbManagerProxy::SetUsbReadOnly");
44 std::uint32_t funcCode =
45 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::USB_READ_ONLY);
46 ErrCode ret = EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
47 return ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED ? EdmReturnErrCode::SYSTEM_ABNORMALLY : ret;
48 }
49
DisableUsb(MessageParcel & data)50 int32_t UsbManagerProxy::DisableUsb(MessageParcel &data)
51 {
52 EDMLOGD("UsbManagerProxy::DisableUsb");
53 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
54 return proxy->SetPolicyDisabled(data, EdmInterfaceCode::DISABLE_USB);
55 }
56
IsUsbDisabled(MessageParcel & data,bool & result)57 int32_t UsbManagerProxy::IsUsbDisabled(MessageParcel &data, bool &result)
58 {
59 EDMLOGD("UsbManagerProxy::IsUsbDisabled");
60 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
61 return proxy->IsPolicyDisabled(data, EdmInterfaceCode::DISABLE_USB, result);
62 }
63
AddAllowedUsbDevices(MessageParcel & data)64 int32_t UsbManagerProxy::AddAllowedUsbDevices(MessageParcel &data)
65 {
66 EDMLOGD("UsbManagerProxy::AddAllowedUsbDevices");
67 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
68 std::uint32_t funcCode =
69 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ALLOWED_USB_DEVICES);
70 return proxy->HandleDevicePolicy(funcCode, data);
71 }
72
RemoveAllowedUsbDevices(MessageParcel & data)73 int32_t UsbManagerProxy::RemoveAllowedUsbDevices(MessageParcel &data)
74 {
75 EDMLOGD("UsbManagerProxy::RemoveAllowedUsbDevices");
76 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
77 std::uint32_t funcCode =
78 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::ALLOWED_USB_DEVICES);
79 return proxy->HandleDevicePolicy(funcCode, data);
80 }
81
GetAllowedUsbDevices(MessageParcel & data,std::vector<UsbDeviceId> & result)82 int32_t UsbManagerProxy::GetAllowedUsbDevices(MessageParcel &data, std::vector<UsbDeviceId> &result)
83 {
84 EDMLOGD("UsbManagerProxy::GetAllowedUsbDevices");
85 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
86 MessageParcel reply;
87 proxy->GetPolicy(EdmInterfaceCode::ALLOWED_USB_DEVICES, data, reply);
88 int32_t ret = ERR_INVALID_VALUE;
89 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
90 if (!blRes) {
91 EDMLOGW("UsbManagerProxy:GetAllowedUsbDevices fail. %{public}d", ret);
92 return ret;
93 }
94 uint32_t size = reply.ReadUint32();
95 if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
96 EDMLOGE("UsbManagerProxy:GetAllowedUsbDevices size=[%{public}u] is too large", size);
97 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
98 }
99 EDMLOGI("UsbManagerProxy:GetAllowedUsbDevices return size:%{public}u", size);
100 for (uint32_t i = 0; i < size; i++) {
101 UsbDeviceId usbDeviceId;
102 if (!UsbDeviceId::Unmarshalling(reply, usbDeviceId)) {
103 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
104 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
105 }
106 result.emplace_back(usbDeviceId);
107 }
108 return ERR_OK;
109 }
110
SetUsbStorageDeviceAccessPolicy(MessageParcel & data)111 int32_t UsbManagerProxy::SetUsbStorageDeviceAccessPolicy(MessageParcel &data)
112 {
113 EDMLOGD("UsbManagerProxy::SetUsbStorageDeviceAccessPolicy");
114 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
115 std::uint32_t funcCode =
116 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::USB_READ_ONLY);
117 return proxy->HandleDevicePolicy(funcCode, data);
118 }
119
120
GetUsbStorageDeviceAccessPolicy(MessageParcel & data,int32_t & result)121 int32_t UsbManagerProxy::GetUsbStorageDeviceAccessPolicy(MessageParcel &data, int32_t &result)
122 {
123 EDMLOGD("UsbManagerProxy::GetUsbStorageDeviceAccessPolicy");
124 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
125 MessageParcel reply;
126 proxy->GetPolicy(EdmInterfaceCode::USB_READ_ONLY, data, reply);
127 int32_t ret = ERR_INVALID_VALUE;
128 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
129 if (!blRes) {
130 EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
131 return ret;
132 }
133 reply.ReadInt32(result);
134 return ERR_OK;
135 }
136 #ifdef USB_EDM_ENABLE
AddOrRemoveDisallowedUsbDevices(MessageParcel & data,bool isAdd)137 int32_t UsbManagerProxy::AddOrRemoveDisallowedUsbDevices(MessageParcel &data, bool isAdd)
138 {
139 EDMLOGD("UsbManagerProxy::AddOrRemoveDisallowedUsbDevices");
140 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
141 if (proxy == nullptr) {
142 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
143 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
144 }
145 std::uint32_t funcCode = 0;
146 std::uint32_t funcOperateType = static_cast<std::uint32_t>(isAdd ?
147 FuncOperateType::SET : FuncOperateType::REMOVE);
148 funcCode = POLICY_FUNC_CODE(funcOperateType, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
149 return proxy->HandleDevicePolicy(funcCode, data);
150 }
151
GetDisallowedUsbDevices(MessageParcel & data,std::vector<OHOS::USB::UsbDeviceType> & result)152 int32_t UsbManagerProxy::GetDisallowedUsbDevices(MessageParcel &data,
153 std::vector<OHOS::USB::UsbDeviceType> &result)
154 {
155 EDMLOGD("UsbManagerProxy::GetDisallowedUsbDevices");
156 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
157 if (proxy == nullptr) {
158 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
159 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
160 }
161 MessageParcel reply;
162 proxy->GetPolicy(EdmInterfaceCode::DISALLOWED_USB_DEVICES, data, reply);
163 int32_t ret = ERR_INVALID_VALUE;
164 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
165 if (!blRes) {
166 EDMLOGW("UsbManagerProxy:GetDisallowedUsbDevices fail. %{public}d", ret);
167 return ret;
168 }
169 uint32_t size = reply.ReadUint32();
170 if (size > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
171 EDMLOGE("UsbManagerProxy:GetDisallowedUsbDevices size=[%{public}u] is too large", size);
172 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
173 }
174 EDMLOGI("UsbManagerProxy:GetDisallowedUsbDevices return size:%{public}u", size);
175 for (uint32_t i = 0; i < size; i++) {
176 OHOS::USB::UsbDeviceType usbDeviceType;
177 if (!OHOS::USB::UsbDeviceType::Unmarshalling(reply, usbDeviceType)) {
178 EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
179 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
180 }
181 result.emplace_back(usbDeviceType);
182 }
183 return ERR_OK;
184 }
185 #endif
186 } // namespace EDM
187 } // namespace OHOS
188