• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_report_sys_event.h"
17 
18 #include "hilog_wrapper.h"
19 #include "usb_config.h"
20 #include "usb_interface.h"
21 #include "usb_device.h"
22 #include "usb_host_manager.h"
23 #include "usb_endpoint.h"
24 #include "usb_errors.h"
25 #include "hisysevent.h"
26 
27 #ifdef USB_PERIPHERAL_FAULT_NOTIFY
28 #include "driver_ext_mgr_client.h"
29 #endif
30 
31 using namespace OHOS::HiviewDFX;
32 
33 namespace OHOS {
34 namespace USB {
35 constexpr int32_t ERR_CODE_TIMEOUT = -7;
36 
ReportTransferFaultSysEvent(const std::string transferType,const HDI::Usb::V1_0::UsbDev & tmpDev,const HDI::Usb::V1_0::UsbPipe & tmpPipe,int32_t ret,const std::string description,MAP_STR_DEVICE & devices)37 void UsbReportSysEvent::ReportTransferFaultSysEvent(const std::string transferType,
38     const HDI::Usb::V1_0::UsbDev &tmpDev, const HDI::Usb::V1_0::UsbPipe &tmpPipe,
39     int32_t ret, const std::string description, MAP_STR_DEVICE &devices)
40 {
41     UsbInterface itIF;
42     UsbDevice dev;
43     USB_HILOGI(MODULE_USBD, "report transfor fault sys event");
44     if (!GetUsbInterfaceId(tmpDev, tmpPipe, tmpPipe.intfId, devices, itIF, dev)) {
45         USB_HILOGE(MODULE_SERVICE, "GetUsbConfigs failed");
46         return;
47     }
48     int32_t hiRet = HiSysEventWrite(HiSysEvent::Domain::USB, "TRANSFOR_FAULT",
49         HiSysEvent::EventType::FAULT, "TRANSFER_TYPE", transferType,
50         "VENDOR_ID", dev.GetVendorId(), "PRODUCT_ID", dev.GetProductId(),
51         "INTERFACE_CLASS", itIF.GetClass(), "INTERFACE_SUBCLASS", itIF.GetSubClass(),
52         "INTERFACE_PROTOCOL", itIF.GetProtocol(),
53         "INFT_ID", tmpPipe.intfId, "ENDPOINT_ID", tmpPipe.endpointId,
54         "FAIL_REASON", ret, "FAIL_DESCRIPTION", description);
55     if (hiRet != UEC_OK) {
56         USB_HILOGE(MODULE_USBD, "HiSysEventWrite ret: %{public}d", hiRet);
57     }
58 
59 #ifdef USB_PERIPHERAL_FAULT_NOTIFY
60     ExternalDeviceManager::DriverExtMgrClient::GetInstance().NotifyUsbPeripheralFault(
61         HiSysEvent::Domain::USB, "TRANSFOR_FAULT");
62 #endif
63 }
64 
CheckAttributeReportTransferFaultSysEvent(const std::string transferType,const HDI::Usb::V1_0::UsbDev & tmpDev,const HDI::Usb::V1_0::UsbPipe & tmpPipe,const USBEndpoint & ep,int32_t ret,const std::string description,MAP_STR_DEVICE & devices)65 void UsbReportSysEvent::CheckAttributeReportTransferFaultSysEvent(const std::string transferType,
66     const HDI::Usb::V1_0::UsbDev &tmpDev, const HDI::Usb::V1_0::UsbPipe &tmpPipe, const USBEndpoint &ep,
67     int32_t ret, const std::string description, MAP_STR_DEVICE &devices)
68 {
69     UsbInterface itIF;
70     UsbDevice dev;
71     USB_HILOGI(MODULE_USBD, "report transfor fault sys event");
72     if (ep.GetAttributes() == 0x03 && ret == ERR_CODE_TIMEOUT) {
73         USB_HILOGE(MODULE_SERVICE, "submitTransfer is timeout");
74         return;
75     }
76     if (!GetUsbInterfaceId(tmpDev, tmpPipe, tmpPipe.intfId, devices, itIF, dev)) {
77         USB_HILOGE(MODULE_SERVICE, "GetUsbConfigs failed");
78         return;
79     }
80     int32_t hiRet = HiSysEventWrite(HiSysEvent::Domain::USB, "TRANSFOR_FAULT",
81         HiSysEvent::EventType::FAULT, "TRANSFER_TYPE", transferType,
82         "VENDOR_ID", dev.GetVendorId(), "PRODUCT_ID", dev.GetProductId(),
83         "INTERFACE_CLASS", itIF.GetClass(), "INTERFACE_SUBCLASS", itIF.GetSubClass(),
84         "INTERFACE_PROTOCOL", itIF.GetProtocol(),
85         "INFT_ID", tmpPipe.intfId, "ENDPOINT_ID", tmpPipe.endpointId,
86         "FAIL_REASON", ret, "FAIL_DESCRIPTION", description);
87     if (hiRet != UEC_OK) {
88         USB_HILOGE(MODULE_USBD, "HiSysEventWrite ret: %{public}d", hiRet);
89     }
90 
91 #ifdef USB_PERIPHERAL_FAULT_NOTIFY
92     ExternalDeviceManager::DriverExtMgrClient::GetInstance().NotifyUsbPeripheralFault(
93         HiSysEvent::Domain::USB, "TRANSFOR_FAULT");
94 #endif
95 }
96 
GetUsbInterfaceId(const HDI::Usb::V1_0::UsbDev & tmpDev,const HDI::Usb::V1_0::UsbPipe & tmpPipe,int32_t interfaceId,MAP_STR_DEVICE & devices,UsbInterface & itIF,UsbDevice & dev)97 bool UsbReportSysEvent::GetUsbInterfaceId(const HDI::Usb::V1_0::UsbDev &tmpDev, const HDI::Usb::V1_0::UsbPipe &tmpPipe,
98     int32_t interfaceId, MAP_STR_DEVICE &devices, UsbInterface &itIF, UsbDevice &dev)
99 {
100     std::string name = std::to_string(tmpDev.busNum) + "-" + std::to_string(tmpDev.devAddr);
101     MAP_STR_DEVICE::iterator iter = devices.find(name);
102     if (iter == devices.end()) {
103         USB_HILOGE(MODULE_SERVICE, "name:%{public}s bus:%{public}hhu dev:%{public}hhu not exist", name.c_str(),
104             tmpDev.busNum, tmpDev.devAddr);
105         return false;
106     }
107     if (iter->second == nullptr) {
108         USB_HILOGE(MODULE_SERVICE, "%{public}s: %{public}s device is nullptr.", __func__, name.c_str());
109         return false;
110     }
111     dev = *(iter->second);
112 
113     if (tmpPipe.intfId == 0 && tmpPipe.endpointId == 0) {
114         itIF.SetClass(dev.GetClass());
115         itIF.SetSubClass(dev.GetSubclass());
116         itIF.SetProtocol(dev.GetProtocol());
117         return true;
118     }
119 
120     auto configs = dev.GetConfigs();
121     for (auto &config : configs) {
122         std::vector<UsbInterface> interfaces = config.GetInterfaces();
123         for (auto &interface : interfaces) {
124             if (interface.GetId() == interfaceId) {
125                 itIF = interface;
126                 return true;
127             }
128         }
129     }
130     return false;
131 }
132 } // OHOS
133 } // USB