From cc5e20c005d21e2b1506ae223f8a157179ac2dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=ADguel=20=C3=81ngel=20Mulero=20Mart=C3=ADnez?= Date: Thu, 20 Jun 2024 22:19:27 +0200 Subject: [PATCH] Request permission before connect in USB flashing (#4046) --- src/js/protocols/webstm32.js | 5 ++++- src/js/protocols/webusbdfu.js | 23 ++++++++--------------- src/js/tabs/firmware_flasher.js | 10 ++++++++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/js/protocols/webstm32.js b/src/js/protocols/webstm32.js index 83baa3b4eb..05be769a0e 100644 --- a/src/js/protocols/webstm32.js +++ b/src/js/protocols/webstm32.js @@ -100,7 +100,10 @@ class STM32Protocol { serial.removeEventListener('disconnect', (event) => this.handleDisconnect(event.detail)); if (disconnectionResult && this.rebootMode) { - DFU.connect(this.hex, this.serialOptions); + DFU.requestPermission() + .then((device) => { + DFU.connect(device.path, this.hex, this.serialOptions); + }); } else { GUI.connect_lock = false; } diff --git a/src/js/protocols/webusbdfu.js b/src/js/protocols/webusbdfu.js index 357d89279b..24e069d44d 100644 --- a/src/js/protocols/webusbdfu.js +++ b/src/js/protocols/webusbdfu.js @@ -119,7 +119,7 @@ class WEBUSBDFU_protocol extends EventTarget { getConnectedPort() { return this.usbDevice ? `usb_${this.usbDevice.serialNumber}` : null; } - connect(hex, options, callback) { + async connect(devicePath, hex, options, callback) { this.hex = hex; this.callback = callback; @@ -141,19 +141,10 @@ class WEBUSBDFU_protocol extends EventTarget { // reset progress bar to initial state TABS.firmware_flasher.flashingMessage(null, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL).flashProgress(0); - navigator.usb - .requestDevice(usbDevices) - .then(selectedDevice => { - console.log(`Product name: ${selectedDevice.productName}`); - console.log(`USB DFU detected with ID: ${selectedDevice}`); - this.usbDevice = selectedDevice; - console.log(`WebUSB Version: ${this.usbDevice.deviceVersionMajor}.${this.usbDevice.deviceVersionMinor}.${this.usbDevice.deviceVersionSubminor}`); - return this.openDevice(); - }) - .catch((e) => { - console.error(`There is no device. ${e}`); - gui_log(i18n.getMessage('stm32UsbDfuNotFound')); - }); + const devices = await this.getDevices(); + const deviceFound = devices.find(device => device.path === devicePath); + this.usbDevice = deviceFound.port; + return this.openDevice(); } openDevice() { this.usbDevice @@ -161,7 +152,9 @@ class WEBUSBDFU_protocol extends EventTarget { .then(async () => { // show key values for the device console.log(`USB Device opened: ${this.usbDevice.productName}`); - if (this.usbDevice.configuration === null) await this.usbDevice.selectConfiguration(1); + if (this.usbDevice.configuration === null) { + await this.usbDevice.selectConfiguration(1); + } this.claimInterface(0); }) .catch(error => { diff --git a/src/js/tabs/firmware_flasher.js b/src/js/tabs/firmware_flasher.js index d66a918f9d..48ef6e9270 100644 --- a/src/js/tabs/firmware_flasher.js +++ b/src/js/tabs/firmware_flasher.js @@ -578,7 +578,10 @@ firmware_flasher.initialize = function (callback) { if (isDFU) { tracking.sendEvent(tracking.EVENT_CATEGORIES.FLASHING, 'DFU Flashing', { filename: self.filename || null }); - DFU.connect(firmware, options); + DFU.requestPermission() + .then((device) => { + DFU.connect(device.path, firmware, options); + }); } else if (isSerial) { if ($('input.updating').is(':checked')) { options.no_reboot = true; @@ -923,7 +926,10 @@ firmware_flasher.initialize = function (callback) { tracking.sendEvent(tracking.EVENT_CATEGORIES.FLASHING, 'ExitDfu', null); try { console.log('Closing DFU'); - DFU.connect(self.parsed_hex, { exitDfu: true }); + DFU.requestPermission() + .then((device) => { + DFU.connect(device.path, self.parsed_hex, { exitDfu: true }); + }); } catch (e) { console.log(`Exiting DFU failed: ${e.message}`); }