NordicSemiconductor / pc-nrf-dfu-js

Javascript implementation of Nordic's DFU protocol over serial (or NoBLE).
BSD 3-Clause "New" or "Revised" License
18 stars 21 forks source link

Wait until port is closed before resolving after serial DFU #24

Closed mrodem closed 6 years ago

mrodem commented 6 years ago

The promise returned by DfuOperation.start() is currently resolved right after the init packet and firmware have been transferred. Attempting to open/connect to the target device at this time will cause problems, because the device is still in DFU mode and is scheduled to be reset. The SDK documentation, describes the DFU process in more detail.

This causes problems for consumers of this library, as the device cannot be reliably used right after DFU. Consumers have to add delays to wait for the device to reset. To improve this, we can make sure that the DFU process is fully complete before resolving from DfuOperation.start(). As per the SDK documentation, the DFU process ends with the device being reset, so I think we should wait until this happens before resolving.

This PR only adds waitForClose to DfuSerialTransport. The other transports have been kept unchanged. A similar implementation should probably be added to DfuNobleTransport, but I have not been able to test that one.

mrodem commented 6 years ago

After testing with more devices, I see that this approach will not work. The "reset" step that is documented in the SDK does not necessarily imply closing the serial port at the end of the DFU. Some bootloader implementations close the port, while others keep it open.

I have not been able to find a reliable way to detect the reset from the host side, so we may just have to keep the setTimeout after DFU for now.