1 /*
2 * Copyright (c) 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 "driver_report_sys_event.h"
17 #include "hilog_wrapper.h"
18 #include "hisysevent.h"
19 #include "edm_errors.h"
20 #include "ext_object.h"
21 #include "usb_device_info.h"
22 #include "usb_driver_info.h"
23 #include "pkg_tables.h"
24
25 using namespace OHOS::HiviewDFX;
26
27 namespace OHOS {
28 namespace ExternalDeviceManager {
29 constexpr int32_t LAST_FIVE = 5;
30
31 const std::map<ExtDevReportSysEvent::EventErrCode, std::string> ExtDevReportSysEvent::ErrMsgs = {
32 {ExtDevReportSysEvent::EventErrCode::SUCCESS, "Success"},
33 {ExtDevReportSysEvent::EventErrCode::BIND_JS_CALLBACK_FAILED, "Failed to register JS callback"},
34 {ExtDevReportSysEvent::EventErrCode::CONNECT_DRIVER_EXTENSION_FAILED,
35 "Failed to connect DriverExtensionAbility"},
36 {ExtDevReportSysEvent::EventErrCode::BIND_ACCESS_NOT_ALLOWED, "Bind access is not allowed"},
37 {ExtDevReportSysEvent::EventErrCode::UNBIND_DRIVER_EMPTY, "No driver matched for the device"},
38 {ExtDevReportSysEvent::EventErrCode::UNBIND_RELATION_NOT_FOUND,
39 "Binding relationship between client and driver not found"},
40 {ExtDevReportSysEvent::EventErrCode::DISCONNECT_DRIVER_EXTENSION_FAILED,
41 "Failed to disconnect DriverExtensionAbility"},
42 {ExtDevReportSysEvent::EventErrCode::QUERY_DRIVER_EXTENSION_FAILED,
43 "Failed to query DriverExtensionAbility"},
44 {ExtDevReportSysEvent::EventErrCode::UPDATE_DATABASE_FAILED, "Failed to update database"},
45 {ExtDevReportSysEvent::EventErrCode::LIFECYCLE_FUNCTION_FAILED,
46 "Lifecycle function execution failed"},
47 {ExtDevReportSysEvent::EventErrCode::OPEN_DEVICE_FAILED, "Failed to open device"},
48 {ExtDevReportSysEvent::EventErrCode::GET_DEVICE_DESCRIPTOR_FAILED,
49 "Failed to get device descriptor"},
50 {ExtDevReportSysEvent::EventErrCode::DEVICE_DESCRIPTOR_LENGTH_INVALID,
51 "Device descriptor length is invalid"},
52 {ExtDevReportSysEvent::EventErrCode::GET_INTERFACE_DESCRIPTOR_FAILED,
53 "Failed to get interface descriptor"},
54 {ExtDevReportSysEvent::EventErrCode::STOP_DRIVER_EXTENSION_FAILED,
55 "Failed to stop DriverExtensionAbility"},
56 {ExtDevReportSysEvent::EventErrCode::QUERY_DRIVER_INFO_FAILED,
57 "Failed to query driver information"},
58 {ExtDevReportSysEvent::EventErrCode::NO_MATCHING_DRIVER_FOUND,
59 "No matching driver found for the device"}
60 };
61
ReportExternalDeviceEvent(const std::shared_ptr<ExtDevEvent> & extDevEvent)62 void ExtDevReportSysEvent::ReportExternalDeviceEvent(const std::shared_ptr<ExtDevEvent> &extDevEvent)
63 {
64 EDM_LOGI(MODULE_PKG_MGR, "report external device event");
65 if (extDevEvent == nullptr) {
66 EDM_LOGI(MODULE_PKG_MGR, "%{public}s, extDevEvent is null", __func__);
67 return;
68 }
69 std::string snNum = "";
70 if (extDevEvent->snNum.length() > LAST_FIVE) {
71 snNum = extDevEvent->snNum.substr(extDevEvent->snNum.length() - LAST_FIVE);
72 }
73
74 int32_t hiRet = HiSysEventWrite(HiSysEvent::Domain::EXTERNAL_DEVICE, "EXT_DEVICE_EVENT",
75 HiSysEvent::EventType::STATISTIC, "DEVICE_CLASS", extDevEvent->deviceClass, "DEVICE_SUBCLASS",
76 extDevEvent->deviceSubClass, "DEVICE_PROTOCOL", extDevEvent->deviceProtocol, "SN_NUM", snNum,
77 "VENDOR_ID", extDevEvent->vendorId, "PRODUCT_ID", extDevEvent->productId,
78 "DEVICE_ID", extDevEvent->deviceId, "DRIVER_UID", extDevEvent->driverUid, "DRIVER_NAME",
79 extDevEvent->driverName, "VERSION_CODE", extDevEvent->versionCode, "VIDS", extDevEvent->vids,
80 "PIDS", extDevEvent->pids, "USER_ID", extDevEvent->userId, "BUNDLE_NAME", extDevEvent->bundleName,
81 "OPERAT_TYPE", extDevEvent->operatType, "INTERFACE_NAME", extDevEvent->interfaceName, "MESSAGE",
82 extDevEvent->message, "ERR_CODE", extDevEvent->errCode);
83 if (hiRet != EDM_OK) {
84 EDM_LOGI(MODULE_PKG_MGR, "HiSysEventWrite ret: %{public}d", hiRet);
85 }
86 }
87
ReportExternalDeviceEvent(const std::shared_ptr<ExtDevEvent> & extDevEvent,const ExtDevReportSysEvent::EventErrCode errCode)88 void ExtDevReportSysEvent::ReportExternalDeviceEvent(const std::shared_ptr<ExtDevEvent> &extDevEvent,
89 const ExtDevReportSysEvent::EventErrCode errCode)
90 {
91 EDM_LOGI(MODULE_PKG_MGR, "report external device event with error code");
92 if (extDevEvent == nullptr) {
93 EDM_LOGI(MODULE_PKG_MGR, "%{public}s, extDevEvent is null", __func__);
94 return;
95 }
96 extDevEvent->errCode = static_cast<int32_t>(errCode);
97 auto it = ExtDevReportSysEvent::ErrMsgs.find(errCode);
98 if (it != ExtDevReportSysEvent::ErrMsgs.end()) {
99 extDevEvent->message = it->second;
100 }
101 ReportExternalDeviceEvent(extDevEvent);
102 }
103
ParseToExtDevEvent(const std::shared_ptr<DeviceInfo> & deviceInfo,const std::shared_ptr<ExtDevEvent> & eventObj)104 void ExtDevReportSysEvent::ParseToExtDevEvent(const std::shared_ptr<DeviceInfo> &deviceInfo,
105 const std::shared_ptr<ExtDevEvent> &eventObj)
106 {
107 if (deviceInfo == nullptr || eventObj == nullptr) {
108 return;
109 }
110 switch (deviceInfo->GetBusType()) {
111 case BusType::BUS_TYPE_USB:{
112 std::shared_ptr<UsbDeviceInfo> usbDeviceInfo = std::static_pointer_cast<UsbDeviceInfo>(deviceInfo);
113 eventObj->deviceId = usbDeviceInfo->GetDeviceId();
114 eventObj->deviceClass = usbDeviceInfo->GetDeviceClass();
115 eventObj->deviceSubClass = usbDeviceInfo->GetDeviceSubClass();
116 eventObj->deviceProtocol = usbDeviceInfo->GetDeviceProtocol();
117 eventObj->snNum = usbDeviceInfo->GetSnNum();
118 eventObj->vendorId = usbDeviceInfo->GetVendorId();
119 eventObj->productId = usbDeviceInfo->GetProductId();
120 break;
121 }
122 default:
123 break;
124 }
125 }
126
ParseToExtDevEvent(const std::shared_ptr<DriverInfo> & driverInfo,const std::shared_ptr<ExtDevEvent> & eventObj)127 void ExtDevReportSysEvent::ParseToExtDevEvent(const std::shared_ptr<DriverInfo> &driverInfo,
128 const std::shared_ptr<ExtDevEvent> &eventObj)
129 {
130 if (driverInfo == nullptr || eventObj == nullptr) {
131 return;
132 }
133 switch (driverInfo->GetBusType()) {
134 case BusType::BUS_TYPE_USB:{
135 std::shared_ptr<UsbDriverInfo> usbDriverInfo =
136 std::static_pointer_cast<UsbDriverInfo>(driverInfo->GetInfoExt());
137 std::vector<uint16_t> productIds = usbDriverInfo->GetProductIds();
138 std::vector<uint16_t> vendorIds = usbDriverInfo->GetVendorIds();
139 eventObj->vids = ParseIdVector(vendorIds);
140 eventObj->pids = ParseIdVector(productIds);
141 eventObj->driverUid = driverInfo->GetDriverUid();
142 eventObj->userId = driverInfo->GetUserId();
143 eventObj->driverName = driverInfo->GetDriverName();
144 eventObj->versionCode = driverInfo->GetVersion();
145 eventObj->bundleName = driverInfo->GetBundleName();
146 break;
147 }
148 default:
149 break;
150 }
151 }
152
ParseToExtDevEvent(const std::shared_ptr<DeviceInfo> & deviceInfo,const std::shared_ptr<DriverInfo> & driverInfo,const std::shared_ptr<ExtDevEvent> & eventObj)153 void ExtDevReportSysEvent::ParseToExtDevEvent(const std::shared_ptr<DeviceInfo> &deviceInfo,
154 const std::shared_ptr<DriverInfo> &driverInfo, const std::shared_ptr<ExtDevEvent> &eventObj)
155 {
156 ExtDevReportSysEvent::ParseToExtDevEvent(deviceInfo, eventObj);
157 ExtDevReportSysEvent::ParseToExtDevEvent(driverInfo, eventObj);
158 }
159
ParseIdVector(std::vector<uint16_t> ids)160 std::string ExtDevReportSysEvent::ParseIdVector(std::vector<uint16_t> ids)
161 {
162 if (ids.size() < 1) {
163 return "";
164 }
165 std::string str = "";
166 auto it = ids.begin();
167 for (uint16_t id : ids) {
168 if (it + 1 == ids.end()) {
169 std::string copy = std::to_string(id);
170 str.append(copy);
171 } else {
172 std::string copy = std::to_string(id);
173 str.append(copy);
174 str.append(",");
175 }
176 }
177 return str;
178 }
179 }
180 }