feat(core): web usb mini下增加对endpoint 0的异常处理防止PC端使用异常

This commit is contained in:
王立帮
2025-10-23 12:43:29 +08:00
parent 9e70ce9661
commit e47f2f7026

View File

@@ -116,7 +116,8 @@ class USBMini extends Serial {
#defaultConfiguration_ = 1;
#endpointIn_ = null;
#endpointOut_ = null;
#interfaceNumber_ = 0;
#ctrlInterfaceNumber_ = -1;
#dataInterfaceNumber_ = -1;
#dataLength_ = 64;
constructor(port) {
super(port);
@@ -149,7 +150,7 @@ class USBMini extends Serial {
recipient: 'interface',
request: 0x01,
value: 0x100,
index: this.#interfaceNumber_
index: this.#dataInterfaceNumber_
}, 64);
}
return result?.data;
@@ -164,7 +165,7 @@ class USBMini extends Serial {
recipient: 'interface',
request: 0x09,
value: 0x200,
index: this.#interfaceNumber_
index: this.#dataInterfaceNumber_
}, data);
}
}
@@ -203,7 +204,7 @@ class USBMini extends Serial {
if (!selectedInterface) {
selectedInterface = interfaces[0];
}
this.#interfaceNumber_ = selectedInterface.interfaceNumber;
this.#dataInterfaceNumber_ = selectedInterface.interfaceNumber;
const { endpoints } = selectedInterface.alternates[0];
for (const endpoint of endpoints) {
if (endpoint.direction === 'in') {
@@ -212,8 +213,13 @@ class USBMini extends Serial {
this.#endpointOut_ = endpoint.endpointNumber;
}
}
await this.#device_.claimInterface(0);
await this.#device_.claimInterface(this.#interfaceNumber_);
try {
await this.#device_.claimInterface(0);
this.#ctrlInterfaceNumber_ = 0;
} catch (_) {
this.#ctrlInterfaceNumber_ = -1;
}
await this.#device_.claimInterface(this.#dataInterfaceNumber_);
super.open(baud);
await this.setBaudRate(baud);
this.onOpen();
@@ -238,28 +244,30 @@ class USBMini extends Serial {
if (!this.isOpened() || this.getRawBaudRate() === baud) {
return;
}
const dwDTERate = new Uint8Array([
baud & 0xFF,
(baud >> 8) & 0xFF,
(baud >> 16) & 0xFF,
(baud >> 24) & 0xFF
]);
const bCharFormat = 0x00;
const bParityType = 0x00;
const bDataBits = 0x08;
const lineCoding = new Uint8Array([
...dwDTERate,
bCharFormat,
bParityType,
bDataBits
]);
await this.#device_.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x20,
value: 0x0000,
index: 0
}, lineCoding);
if (this.#ctrlInterfaceNumber_ !== -1) {
const dwDTERate = new Uint8Array([
baud & 0xFF,
(baud >> 8) & 0xFF,
(baud >> 16) & 0xFF,
(baud >> 24) & 0xFF
]);
const bCharFormat = 0x00;
const bParityType = 0x00;
const bDataBits = 0x08;
const lineCoding = new Uint8Array([
...dwDTERate,
bCharFormat,
bParityType,
bDataBits
]);
await this.#device_.controlTransferOut({
requestType: 'class',
recipient: 'interface',
request: 0x20,
value: 0x0000,
index: this.#ctrlInterfaceNumber_
}, lineCoding);
}
await super.setBaudRate(baud);
}