devanlai / webdfu

WebUSB Device Firmware Upgrade example
https://devanlai.github.io/webdfu/dfu-util/
ISC License
277 stars 95 forks source link

Running dfu-util demo locally #6

Closed ankoornoisee closed 4 years ago

ankoornoisee commented 4 years ago

I'm pretty new to the concepts of WebUSB and webdfu, and web development in general. I have the demo working with my STM32F0 device - I am able to flash the device with a .bin file using your tool on chrome. However, if I download the repo and try to run the demo locally (opening dfu-util/index.html on chrome) the webpage is no longer able to connect to my STM32 device. When I click on the 'connect' button, I see the STM32 device listed in a chrome popup dialog but when I select it the 'status' text on the webpage changes to "NotFoundError: No device selected." I have tried running the demo locally, then immediately tried it online and it always works online. Is there something obvious I am missing here? Why does the dfu-util demo not work when run locally?

devanlai commented 4 years ago

I'm not yet sure if this is a bug in the webdfu code, or in Chrome's security policies. In theory, WebUSB is restricted to Javascript running in a secure context, which normally means it has to be served over HTTPS, but for development purposes, localhost and file:// URLs are also treated as secure.

I can see similar failures on my Windows machine when running webdfu from a local file URL. I'm pretty sure local files are treated as secure contexts, since running some Web Bluetooth code from a local file URL works fine and window.isSecureContext evaluates to true.

As as a workaround, you can run a local webserver and access it from localhost, which does work in Chrome. If you have Python 2 installed, this can be as simple as either:

python -m SimpleHTTPServer

or for Python 3:

python -m http.server