• 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 "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.WriteInt32(readOnly ? 1 : 0);
53     ErrCode ret = EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
54     return ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED ? EdmReturnErrCode::SYSTEM_ABNORMALLY : ret;
55 }
56 
DisableUsb(const AppExecFwk::ElementName & admin,bool disable)57 int32_t UsbManagerProxy::DisableUsb(const AppExecFwk::ElementName &admin, bool disable)
58 {
59     EDMLOGD("UsbManagerProxy::DisableUsb");
60     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
61     if (proxy == nullptr) {
62         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
63         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
64     }
65     return proxy->SetPolicyDisabled(admin, disable, EdmInterfaceCode::DISABLE_USB);
66 }
67 
IsUsbDisabled(const AppExecFwk::ElementName * admin,bool & result)68 int32_t UsbManagerProxy::IsUsbDisabled(const AppExecFwk::ElementName *admin, bool &result)
69 {
70     EDMLOGD("UsbManagerProxy::IsUsbDisabled");
71     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
72     if (proxy == nullptr) {
73         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
74         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
75     }
76     return proxy->IsPolicyDisabled(admin, EdmInterfaceCode::DISABLE_USB, result);
77 }
78 
AddAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> usbDeviceIds)79 int32_t UsbManagerProxy::AddAllowedUsbDevices(const AppExecFwk::ElementName &admin,
80     std::vector<UsbDeviceId> usbDeviceIds)
81 {
82     EDMLOGD("UsbManagerProxy::AddAllowedUsbDevices");
83     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
84     if (proxy == nullptr) {
85         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
86         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
87     }
88     MessageParcel data;
89     std::uint32_t funcCode =
90         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ALLOWED_USB_DEVICES);
91     data.WriteInterfaceToken(DESCRIPTOR);
92     data.WriteInt32(WITHOUT_USERID);
93     data.WriteParcelable(&admin);
94     data.WriteInt32(usbDeviceIds.size());
95     std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), [&](const auto usbDeviceId) {
96         usbDeviceId.Marshalling(data);
97     });
98     return proxy->HandleDevicePolicy(funcCode, data);
99 }
100 
RemoveAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> usbDeviceIds)101 int32_t UsbManagerProxy::RemoveAllowedUsbDevices(const AppExecFwk::ElementName &admin,
102     std::vector<UsbDeviceId> usbDeviceIds)
103 {
104     EDMLOGD("UsbManagerProxy::RemoveAllowedUsbDevices");
105     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
106     if (proxy == nullptr) {
107         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
108         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
109     }
110     MessageParcel data;
111     std::uint32_t funcCode =
112         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::ALLOWED_USB_DEVICES);
113     data.WriteInterfaceToken(DESCRIPTOR);
114     data.WriteInt32(WITHOUT_USERID);
115     data.WriteParcelable(&admin);
116     data.WriteInt32(usbDeviceIds.size());
117     std::for_each(usbDeviceIds.begin(), usbDeviceIds.end(), [&](const auto usbDeviceId) {
118         usbDeviceId.Marshalling(data);
119     });
120     return proxy->HandleDevicePolicy(funcCode, data);
121 }
122 
GetAllowedUsbDevices(const AppExecFwk::ElementName & admin,std::vector<UsbDeviceId> & result)123 int32_t UsbManagerProxy::GetAllowedUsbDevices(const AppExecFwk::ElementName &admin, std::vector<UsbDeviceId> &result)
124 {
125     EDMLOGD("UsbManagerProxy::GetAllowedUsbDevices");
126     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
127     if (proxy == nullptr) {
128         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
129         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
130     }
131     MessageParcel data;
132     MessageParcel reply;
133     data.WriteInterfaceToken(DESCRIPTOR);
134     data.WriteInt32(WITHOUT_USERID);
135     data.WriteInt32(HAS_ADMIN);
136     data.WriteParcelable(&admin);
137     proxy->GetPolicy(EdmInterfaceCode::ALLOWED_USB_DEVICES, data, reply);
138     int32_t ret = ERR_INVALID_VALUE;
139     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
140     if (!blRes) {
141         EDMLOGW("UsbManagerProxy:GetAllowedUsbDevices fail. %{public}d", ret);
142         return ret;
143     }
144     int32_t size = reply.ReadInt32();
145     if (size > EdmConstants::ALLOWED_USB_DEVICES_MAX_SIZE) {
146         EDMLOGE("UsbManagerProxy:GetAllowedUsbDevices size=[%{public}d] is too large", size);
147         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
148     }
149     EDMLOGI("UsbManagerProxy:GetAllowedUsbDevices return size:%{public}d", size);
150     for (int i = 0; i < size; i++) {
151         UsbDeviceId usbDeviceId;
152         if (!UsbDeviceId::Unmarshalling(reply, usbDeviceId)) {
153             EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
154             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
155         }
156         result.emplace_back(usbDeviceId);
157     }
158     return ERR_OK;
159 }
160 
SetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName & admin,int32_t usbPolicy)161 int32_t UsbManagerProxy::SetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName &admin, int32_t usbPolicy)
162 {
163     EDMLOGD("UsbManagerProxy::SetUsbStorageDeviceAccessPolicy");
164     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
165     if (proxy == nullptr) {
166         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
167         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
168     }
169     MessageParcel data;
170     std::uint32_t funcCode =
171         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::USB_READ_ONLY);
172     data.WriteInterfaceToken(DESCRIPTOR);
173     data.WriteInt32(WITHOUT_USERID);
174     data.WriteParcelable(&admin);
175     data.WriteInt32(usbPolicy);
176     return proxy->HandleDevicePolicy(funcCode, data);
177 }
178 
GetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName & admin,int32_t & result)179 int32_t UsbManagerProxy::GetUsbStorageDeviceAccessPolicy(const AppExecFwk::ElementName &admin, int32_t &result)
180 {
181     EDMLOGD("UsbManagerProxy::GetUsbStorageDeviceAccessPolicy");
182     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
183     if (proxy == nullptr) {
184         EDMLOGE("can not get EnterpriseDeviceMgrProxy");
185         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
186     }
187     MessageParcel data;
188     MessageParcel reply;
189     data.WriteInterfaceToken(DESCRIPTOR);
190     data.WriteInt32(WITHOUT_USERID);
191     data.WriteInt32(HAS_ADMIN);
192     data.WriteParcelable(&admin);
193     proxy->GetPolicy(EdmInterfaceCode::USB_READ_ONLY, data, reply);
194     int32_t ret = ERR_INVALID_VALUE;
195     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
196     if (!blRes) {
197         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
198         return ret;
199     }
200     reply.ReadInt32(result);
201     return ERR_OK;
202 }
203 } // namespace EDM
204 } // namespace OHOS
205