加入Claim Interface,看看内核是否还报错

This commit is contained in:
lumos 2025-06-06 15:30:10 +08:00
parent ea72309d89
commit dbd8a66778
5 changed files with 73 additions and 17 deletions

View File

@ -104,9 +104,20 @@ void MainWindow::on_pushButton_4_clicked()
QString endpointAddress = match.captured(0); // 获取第一个匹配的结果
uint8_t endpointValue = endpointAddress.mid(2).toUInt(NULL, 16); // 16 表示十六进制
uint8_t data=ui->textEdit->toPlainText().toUInt();
tmp = ui->comboBox_2->currentText();
QRegularExpression interfaceRegex("Interface #\\[(\\d+)\\]");
QRegularExpressionMatch interfaceMatch = interfaceRegex.match(tmp);
int interface = interfaceMatch.captured(1).toInt();
uint8_t* data =reinterpret_cast<uint8_t*>(ui->textEdit->toPlainText().toUtf8().data());
// uint8_t data=ui->textEdit->toPlainText().toUInt();
int len = ui->textEdit->toPlainText().length();
if(uh->sendBulkTransfer(endpointValue,&data,len)){
for(int i=0;i<len;i++){
LOG_DEBUG("Send data is 0x%x",data[i]);
}
if(uh->sendBulkTransfer(endpointValue,data,len,interface)){
LOG_DEBUG("Send endPoint 0x%x via BulkTransfer success.",endpointValue);
}
@ -121,9 +132,19 @@ void MainWindow::on_pushButton_3_clicked()
QString endpointAddress = match.captured(0); // 获取第一个匹配的结果
uint8_t endpointValue = endpointAddress.mid(2).toUInt(NULL, 16); // 16 表示十六进制
uint8_t data=ui->textEdit->toPlainText().toUInt();
tmp = ui->comboBox_2->currentText();
QRegularExpression interfaceRegex("Interface #\\[(\\d+)\\]");
QRegularExpressionMatch interfaceMatch = interfaceRegex.match(tmp);
int interface = interfaceMatch.captured(1).toInt();
uint8_t* data =reinterpret_cast<uint8_t*>(ui->textEdit->toPlainText().toUtf8().data());
int len = ui->textEdit->toPlainText().length();
if(uh->sendAsyncTransfer(endpointValue,&data,len)){
for(int i=0;i<len;i++){
LOG_DEBUG("Send data is 0x%x",data[i]);
}
if(uh->sendAsyncTransfer(endpointValue,data,len,interface)){
LOG_DEBUG("Send endPoint 0x%x via AsyncTransfer success.",endpointValue);
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 16.0.1, 2025-06-06T11:38:55. -->
<!-- Written by QtCreator 16.0.1, 2025-06-06T12:27:01. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@ -245,11 +245,14 @@
<value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">/home/leo/devCode/outer/USBFilter_30/uppper_LibUSB/upper.pro</value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">true</value>
<value type="bool" key="RunConfiguration.RunAsRoot">true</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/home/leo/devCode/outer/USBFilter_30/uppper_LibUSB/build/Desktop_Qt_5_15_2_GCC_64bit-Debug</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>

View File

@ -76,6 +76,8 @@ bool UsbHandler::sendControlTransfer(uint8_t bmRequestType, uint8_t bRequest, ui
emit errorOccurred("No device handle.");
return false;
}
int status = libusb_control_transfer(handle, bmRequestType, bRequest, wValue, wIndex, data, length, 1000);
if (status < 0) {
emit errorOccurred("Control transfer failed.");
@ -84,19 +86,34 @@ bool UsbHandler::sendControlTransfer(uint8_t bmRequestType, uint8_t bRequest, ui
return true;
}
bool UsbHandler::sendBulkTransfer(uint8_t endpoint, uint8_t *data, int length)
bool UsbHandler::sendBulkTransfer(uint8_t endpoint, uint8_t *data, int length,int interfaceNum)
{
int status;
if (handle == nullptr) {
emit errorOccurred("No device handle.");
return false;
}
int actualLength;
int status = libusb_bulk_transfer(handle, endpoint, data, length, &actualLength, 1000);
// 获取接口权限可选部分系统如Linux必须
if (libusb_kernel_driver_active(handle, interfaceNum)) {
libusb_detach_kernel_driver(handle, interfaceNum);
}
status = libusb_claim_interface(handle, interfaceNum);
if (status < 0) {
emit errorOccurred("Bulk transfer failed.");
LOG_ERROR("Failed to claim interface: %d",interfaceNum);
return false;
}
int actualLength;
status = libusb_bulk_transfer(handle, endpoint, data, length, &actualLength, 1000);
if (status < 0) {
emit errorOccurred("Bulk transfer failed.");
libusb_release_interface(handle, interfaceNum);
return false;
}
libusb_release_interface(handle, interfaceNum);
return true;
}
@ -118,8 +135,9 @@ bool UsbHandler::sendBulkTransfer(uint8_t endpoint, uint8_t *data, int length)
// }
bool UsbHandler::sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length)
bool UsbHandler::sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length,int interfaceNum)
{
int status;
if (handle == nullptr) {
emit errorOccurred("No device handle.");
return false;
@ -132,6 +150,18 @@ bool UsbHandler::sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length)
return false;
}
// 获取接口权限可选部分系统如Linux必须
if (libusb_kernel_driver_active(handle, interfaceNum)) {
libusb_detach_kernel_driver(handle, interfaceNum);
}
status = libusb_claim_interface(handle, interfaceNum);
if (status < 0) {
LOG_ERROR("Failed to claim interface: %d",interfaceNum);
return false;
}
// 2. 设置传输类型和参数
if (endpoint & LIBUSB_ENDPOINT_IN) { // 如果是输入端点 (IN)
libusb_fill_bulk_transfer(transfer, handle, endpoint, data, length,
@ -158,14 +188,16 @@ bool UsbHandler::sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length)
}
// 3. 提交传输
int status = libusb_submit_transfer(transfer);
status = libusb_submit_transfer(transfer);
if (status < 0) {
emit errorOccurred("Failed to submit async transfer.");
libusb_free_transfer(transfer);
libusb_release_interface(handle, interfaceNum);
return false;
}
libusb_handle_events(context); // 确保事件循环能处理异步传输
libusb_release_interface(handle, interfaceNum);
// libusb_handle_events(context); // 确保事件循环能处理异步传输
return true;
}

View File

@ -31,8 +31,8 @@ public:
);
bool sendBulkTransfer(uint8_t endpoint, uint8_t *data, int length);
bool sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length);
bool sendBulkTransfer(uint8_t endpoint, uint8_t *data, int length,int interfaceNum);
bool sendAsyncTransfer(uint8_t endpoint, uint8_t *data, int length,int interfaceNum);
void listInterfaces(int deviceIndex);
// bool sendInterruptTransfer(uint8_t endpoint, uint8_t *data, int length);