Skip to content

Commit

Permalink
Request permission before connect in USB flashing (#4046)
Browse files Browse the repository at this point in the history
  • Loading branch information
McGiverGim authored Jun 20, 2024
1 parent 586dd58 commit cc5e20c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/js/protocols/webstm32.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 8 additions & 15 deletions src/js/protocols/webusbdfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -141,27 +141,20 @@ 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
.open()
.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 => {
Expand Down
10 changes: 8 additions & 2 deletions src/js/tabs/firmware_flasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
}
Expand Down

0 comments on commit cc5e20c

Please sign in to comment.