diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index faff38fcc0735593b7bda190f5e3e1837a6b657e..6ecb10f4dc7a58c14c6cb6332faf4402b3f81287 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -1169,23 +1169,33 @@ int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &de USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr"); return UEC_SERVICE_INVALID_VALUE; } + int32_t res = UEC_OK; int32_t ret = usbd_->OpenDevice(uDev); if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_SERVICE, "OpenDevice failed ret=%{public}d", ret); + USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInfo OpenDevice failed ret=%{public}d", ret); return ret; } ret = GetDeviceInfoDescriptor(uDev, descriptor, dev); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInfoDescriptor ret=%{public}d", ret); + res = usbd_->CloseDevice(uDev); + if (res != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInfo CloseDevice failed res=%{public}d", res); + return res; + } + return ret; } - ret = GetConfigDescriptor(dev, descriptor); - if (ret != UEC_OK) { + res = GetConfigDescriptor(dev, descriptor); + if (res != UEC_OK) { USB_HILOGE(MODULE_USB_SERVICE, "GetConfigDescriptor ret=%{public}d", ret); + } + ret = usbd_->CloseDevice(uDev); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInfo CloseDevice failed ret=%{public}d", ret); return ret; } - - return UEC_OK; + return res; } // LCOV_EXCL_STOP @@ -2157,7 +2167,7 @@ int32_t UsbService::ManageInterfaceTypeImpl(InterfaceType interfaceType, bool di iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { ManageInterface(dev, interfaces[i].GetId(), disable); USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices.size(), (int32_t)interfaceType, disable); + devices.size(), static_cast(interfaceType), disable); std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); } } @@ -2177,14 +2187,14 @@ int32_t UsbService::ManageDeviceTypeImpl(InterfaceType interfaceType, bool disab std::map devices; usbHostManager_->GetDevices(devices); - USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices.size(), (int32_t)interfaceType, disable); for (auto it = devices.begin(); it != devices.end(); ++it) { if ((it->second->GetClass() == iterInterface->second[BASECLASS_INDEX]) && (it->second->GetSubclass() == iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && (it->second->GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), disable); + USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", + devices.size(), static_cast(interfaceType), disable); std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); } } @@ -2199,7 +2209,21 @@ int32_t UsbService::ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t i USB_HILOGE(MODULE_USB_SERVICE, "usbd_ is nullptr"); return UEC_SERVICE_INVALID_VALUE; } - return usbd_->ManageInterface(dev, interfaceId, disable); + int32_t ret = usbd_->OpenDevice(dev); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "ManageInterface OpenDevice failed ret=%{public}d", ret); + return ret; + } + int32_t res = usbd_->ManageInterface(dev, interfaceId, disable); + if (res != 0) { + USB_HILOGE(MODULE_USB_SERVICE, "ManageInterface failed ret=%{public}d", ret); + } + ret = usbd_->CloseDevice(dev); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USBD, "ManageInterface Close device failed ret = %{public}d", ret); + return ret; + } + return res; } // LCOV_EXCL_STOP