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