1 /*
2  * Copyright (c) 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_policy_utils.h"
17 
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "usb_device.h"
21 #include "usb_srv_client.h"
22 
23 namespace OHOS {
24 namespace EDM {
SetUsbDisabled(bool data)25 ErrCode UsbPolicyUtils::SetUsbDisabled(bool data)
26 {
27     EDMLOGI("UsbPolicyUtils SetUsbDisabled...disable = %{public}d", data);
28     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
29     int32_t usbRet = srvClient.ManageGlobalInterface(data);
30     if (usbRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
31         EDMLOGW("UsbPolicyUtils ManageGlobalInterface failed! USB interface not init!");
32     }
33     if (usbRet != ERR_OK && usbRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
34         EDMLOGE("UsbPolicyUtils ManageGlobalInterface failed! ret: %{public}d", usbRet);
35         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
36     }
37     return ERR_OK;
38 }
39 
AddAllowedUsbDevices(std::vector<UsbDeviceId> data)40 ErrCode UsbPolicyUtils::AddAllowedUsbDevices(std::vector<UsbDeviceId> data)
41 {
42     EDMLOGI("UsbPolicyUtils AddAllowedUsbDevices....data size = %{public}zu", data.size());
43     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
44     std::vector<OHOS::USB::UsbDevice> allDevices;
45     int32_t getRet = srvClient.GetDevices(allDevices);
46     if (getRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
47         EDMLOGW("UsbPolicyUtils getDevices failed! USB interface not init!");
48     }
49     if (getRet != ERR_OK && getRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
50         EDMLOGE("UsbPolicyUtils getDevices failed: %{public}d", getRet);
51         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
52     }
53     EDMLOGI("UsbPolicyUtils AddAllowedUsbDevices getDevices size: %{public}zu", allDevices.size());
54     for (const auto &item : allDevices) {
55         bool isAllowed = (std::find_if(data.begin(), data.end(), [item](UsbDeviceId trustItem) {
56             return item.GetVendorId() == trustItem.GetVendorId() && item.GetProductId() == trustItem.GetProductId();
57         }) != data.end());
58         if (srvClient.ManageDevice(item.GetVendorId(), item.GetProductId(), !isAllowed) != ERR_OK) {
59             EDMLOGW("UsbPolicyUtils ManageDevice: vid:%{public}d pid:%{public}d, %{public}d failed!",
60                 item.GetVendorId(), item.GetProductId(), isAllowed);
61         }
62     }
63     return ERR_OK;
64 }
65 
SetDisallowedUsbDevices(std::vector<USB::UsbDeviceType> data)66 ErrCode UsbPolicyUtils::SetDisallowedUsbDevices(std::vector<USB::UsbDeviceType> data)
67 {
68     EDMLOGI("UsbPolicyUtils SetDisallowedUsbDevices...data size = %{public}zu", data.size());
69     auto &srvClient = OHOS::USB::UsbSrvClient::GetInstance();
70     int32_t usbRet = srvClient.ManageInterfaceType(data, true);
71     if (usbRet == EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
72         EDMLOGW("UsbPolicyUtils ManageInterfaceType failed! USB interface not init!");
73     }
74     if (usbRet != ERR_OK && usbRet != EdmConstants::USB_ERRCODE_INTERFACE_NO_INIT) {
75         EDMLOGE("UsbPolicyUtils ManageInterfaceType failed! ret:%{public}d", usbRet);
76         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
77     }
78     return ERR_OK;
79 }
80 } // namespace EDM
81 } // namespace OHOS