Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DFU support for PWA #3949

Merged
merged 6 commits into from
May 15, 2024
Merged

Add DFU support for PWA #3949

merged 6 commits into from
May 15, 2024

Conversation

haslinghuis
Copy link
Member

@haslinghuis haslinghuis commented May 12, 2024

  • Adds supports for flashing in DFU mode.
  • We need porthandler support to jump into DFU mode when using serial web API.

Testing using yarn dev

  • netlify preview build provided does not work (with secure context).
  • when connected first disconnect before going to firmware flasher tab
  • after building a config press the flash button and put the device in DFU mode manually
  • after granting port permission the device will be flashed

image

On Ubuntu Firefox does not support these web APIs, as snap install of Chromium does not work either.
Need to install package like:

sudo add-apt-repository ppa:xtradeb/apps -y
sudo apt update
sudo apt install chromium

Tested flashing on:

MATEKF411
IFLIGHT_F411_PRO
AOCODARCF405V3
AIRBOTG4AIO
FLYWOOF722PRO
SDMODELH7
NEUTRONF435

@haslinghuis haslinghuis added this to the 11.0 milestone May 12, 2024
@haslinghuis haslinghuis self-assigned this May 12, 2024
Copy link

netlify bot commented May 12, 2024

Deploy Preview for origin-betaflight-app ready!

Name Link
🔨 Latest commit bbdfc16
🔍 Latest deploy log https://app.netlify.com/sites/origin-betaflight-app/deploys/6643cfde5de8e000080abbd0
😎 Deploy Preview https://deploy-preview-3949.dev.app.betaflight.com
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

This comment has been minimized.

@McGiverGim
Copy link
Member

Good work @haslinghuis
I suppose this can't be tested "easilly"? I can't see a way to "add/connect" the device. I suppose that maybe works if it has been authorized before?
I can modify the code to test it, but I want to know exactly what is the expectative with that ;)

@chmelevskij
Copy link
Member

Will grab a look tomorrow morning. Have few ideas about the webdfu parts

Comment on lines +191 to +199
.then(result => {
if (result.status === 'ok') {
const _length = result.data.getUint8(0);
let _descriptor = '';
for (let i = 2; i < _length; i += 2) {
const charCode = result.data.getUint16(i, true);
_descriptor += String.fromCharCode(charCode);
}
callback(_descriptor, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with usb so have a noob question probably.

What populates result.data? Like do we write into that thing, are first 8 bits which represent the length something we write or something standard?

Asking because we could potentially avoid the whole for loop here for string parsing and use TextDecoder instead.

Even if we just cut off the length and pass in the rest text decoder should be able to handle that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controlTransferIn returns a DataView object containing the data received from the USB device, if any.

image

This comment has been minimized.

This comment has been minimized.

Copy link
Member

@chmelevskij chmelevskij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to request this, but I feel like since it's a new file we should aim to get rid of at least the most obvious problems with the old code.

Something to start with:

  1. Removing commented out blocks of code in webdfu.
  2. Removing self = this pattern, it's not relevant anymore with classes and arrow functions.

There is defo more but these would at least put us up for better coding conventions in new code.

@haslinghuis
Copy link
Member Author

haslinghuis commented May 13, 2024

  • Tried to remove remaining self in unload_procedure. But then it wont' work.
  • Could the switch case blocks prevent this to work ? { }
  • Have implemented and tested commented code

EDIT: refactored whole function and now it works.

This comment has been minimized.

This comment has been minimized.

@chmelevskij
Copy link
Member

  • Tried to remove remaining self in unload_procedure. But then it wont' work.
  • Could the switch case blocks prevent this to work ? { }
  • Have implemented and tested commented code

EDIT: refactored whole function and now it works.

Don't know what was the culprit, but it might be that there was function keyword used somewhere in the code. Solution to that is to replace all of the callbacks etc. with arrow functions instead.

Main reason for that pattern in the past was because function binds this to itself. With arrow functions it binds this to closest class/function it's used in.

Copy link
Member

@chmelevskij chmelevskij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@haslinghuis haslinghuis changed the title Add webusbdfu support for PWA Add DFU support for PWA May 13, 2024
@haslinghuis haslinghuis mentioned this pull request May 13, 2024
52 tasks
@nerdCopter
Copy link
Member

nerdCopter commented May 14, 2024

Flash button never enables.
image

https://build.betaflight.com/api/builds/220ec26ced03bc9893d4d0f48ee9f9ac/log & /hex valid.

Chrome Version 124.0.6367.201 (Official Build) (64-bit) / Debian 12

@haslinghuis
Copy link
Member Author

@nerdCopter how did you test?

Works locally (yarn dev). Have not checked on Netlify. This code needs to run in secure context.

Screencast.from.14-05-24.18.18.48.webm

@nerdCopter
Copy link
Member

@nerdCopter how did you test?

Netlify

@nerdCopter
Copy link
Member

Okay, i get the DFU via your described process. it does not erase/flash, but the DFU is there.

Copy link

sonarcloud bot commented May 14, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
14.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@haslinghuis
Copy link
Member Author

@nerdCopter should work now. Removed flash protection as it does not work on F4 or F7

@nerdCopter
Copy link
Member

nerdCopter commented May 15, 2024

bbdfc167 worked. Chrome connector said STM32 BOOTLOADER. pilots might expect the term DFU.

@haslinghuis
Copy link
Member Author

haslinghuis commented May 15, 2024

@nerdCopter this is to be expected as PortHandler did all the hard work but currently is disabled for PWA.

@haslinghuis haslinghuis merged commit b91698d into betaflight:master May 15, 2024
6 of 7 checks passed
@haslinghuis haslinghuis deleted the webdfu branch May 15, 2024 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

5 participants