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;
} }
} }
await this.#device_.claimInterface(0); try {
await this.#device_.claimInterface(this.#interfaceNumber_); await this.#device_.claimInterface(0);
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,28 +244,30 @@ class USBMini extends Serial {
if (!this.isOpened() || this.getRawBaudRate() === baud) { if (!this.isOpened() || this.getRawBaudRate() === baud) {
return; return;
} }
const dwDTERate = new Uint8Array([ if (this.#ctrlInterfaceNumber_ !== -1) {
baud & 0xFF, const dwDTERate = new Uint8Array([
(baud >> 8) & 0xFF, baud & 0xFF,
(baud >> 16) & 0xFF, (baud >> 8) & 0xFF,
(baud >> 24) & 0xFF (baud >> 16) & 0xFF,
]); (baud >> 24) & 0xFF
const bCharFormat = 0x00; ]);
const bParityType = 0x00; const bCharFormat = 0x00;
const bDataBits = 0x08; const bParityType = 0x00;
const lineCoding = new Uint8Array([ const bDataBits = 0x08;
...dwDTERate, const lineCoding = new Uint8Array([
bCharFormat, ...dwDTERate,
bParityType, bCharFormat,
bDataBits bParityType,
]); bDataBits
await this.#device_.controlTransferOut({ ]);
requestType: 'class', await this.#device_.controlTransferOut({
recipient: 'interface', requestType: 'class',
request: 0x20, recipient: 'interface',
value: 0x0000, request: 0x20,
index: 0 value: 0x0000,
}, lineCoding); index: this.#ctrlInterfaceNumber_
}, lineCoding);
}
await super.setBaudRate(baud); await super.setBaudRate(baud);
} }