@@ -21,6 +21,9 @@ import { EventBus } from "../../components/eventBus";
2121import { ispConnected } from "../utils/connection.js" ;
2222import FC from "../fc" ;
2323
24+ const PORT_CHANGE_DEBOUNCE_MS = 500 ;
25+ const AUTO_DETECT_DELAY_MS = 1000 ;
26+
2427const firmware_flasher = {
2528 targets : null ,
2629 buildApi : new BuildApi ( ) ,
@@ -40,6 +43,55 @@ const firmware_flasher = {
4043 // Properties to preserve firmware state during flashing
4144 preFlashingMessage : null ,
4245 preFlashingMessageType : null ,
46+ // Minimal module-scoped handler references for cleanup to use
47+ // Module-level handler implementations so tests and early callers can
48+ // invoke them before `initialize()` binds the full DOM-aware versions.
49+ detectedUsbDevice : function ( device ) {
50+ // If a reboot-resume or flash-on-connect flow is active, we may need to
51+ // resume flashing. Otherwise USB attaches don't auto-detect a board.
52+ const isFlashOnConnect = $ ( "input.flash_on_connect" ) . is ( ":checked" ) || false ;
53+ if ( STM32 . rebootMode || isFlashOnConnect ) {
54+ STM32 . rebootMode = 0 ;
55+ GUI . connect_lock = false ;
56+ firmware_flasher . startFlashing ( ) ;
57+ }
58+ } ,
59+ detectedSerialDevice : function ( device ) {
60+ // Serial device connect should trigger auto-detect
61+ AutoDetect . verifyBoard ( ) ;
62+ } ,
63+ onPortChange : function ( port ) {
64+ // Debounce rapid port events
65+ if ( firmware_flasher . portChangeTimer ) {
66+ clearTimeout ( firmware_flasher . portChangeTimer ) ;
67+ firmware_flasher . portChangeTimer = null ;
68+ }
69+
70+ if ( GUI . connect_lock ) return ;
71+
72+ if ( port && port !== "0" && ! ( STM32 && STM32 . rebootMode ) ) {
73+ firmware_flasher . portChangeTimer = setTimeout ( ( ) => {
74+ firmware_flasher . portChangeTimer = null ;
75+ if ( ! GUI . connect_lock ) {
76+ AutoDetect . verifyBoard ( ) ;
77+ }
78+ } , PORT_CHANGE_DEBOUNCE_MS ) ;
79+ } else if ( ! port || port === "0" ) {
80+ if ( ! GUI . connect_lock ) {
81+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
82+ }
83+ }
84+ } ,
85+ onDeviceRemoved : function ( devicePath ) {
86+ // Avoid clearing when removal is expected during flashing/reboot
87+ if ( GUI . connect_lock || STM32 . rebootMode ) {
88+ return ;
89+ }
90+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
91+ firmware_flasher . clearBufferedFirmware ( ) ;
92+ } ,
93+ // Single debounce timer shared across instances
94+ portChangeTimer : null ,
4395} ;
4496
4597firmware_flasher . initialize = async function ( callback ) {
@@ -742,20 +794,91 @@ firmware_flasher.initialize = async function (callback) {
742794 return output . join ( "" ) . split ( "\n" ) ;
743795 }
744796
745- function detectedUsbDevice ( device ) {
797+ firmware_flasher . detectedUsbDevice = function ( device ) {
746798 const isFlashOnConnect = $ ( "input.flash_on_connect" ) . is ( ":checked" ) ;
747799
748800 console . log ( `${ self . logHead } Detected USB device:` , device ) ;
749801 console . log ( `${ self . logHead } Reboot mode: %s, flash on connect` , STM32 . rebootMode , isFlashOnConnect ) ;
750802
803+ // If another operation is in progress, ignore port events (unless we're resuming from a reboot)
804+ if ( GUI . connect_lock && ! STM32 . rebootMode ) {
805+ console . log ( `${ self . logHead } Port event ignored due to active operation (connect_lock)` ) ;
806+ return ;
807+ }
808+
809+ // Proceed if we're resuming a reboot sequence or if flash-on-connect is enabled and no operation is active
751810 if ( STM32 . rebootMode || isFlashOnConnect ) {
811+ const wasReboot = ! ! STM32 . rebootMode ;
752812 STM32 . rebootMode = 0 ;
753- GUI . connect_lock = false ;
813+ // Only clear the global connect lock when we are resuming from a reboot
814+ // so we don't accidentally interrupt another active operation.
815+ if ( wasReboot ) {
816+ GUI . connect_lock = false ;
817+ }
754818 startFlashing ( ) ;
755819 }
756- }
820+ } ;
821+
822+ firmware_flasher . detectedSerialDevice = function ( device ) {
823+ AutoDetect . verifyBoard ( ) ;
824+ } ;
825+
826+ firmware_flasher . onPortChange = function ( port ) {
827+ // Clear any pending debounce timer so rapid port events don't re-enter the handler.
828+ if ( firmware_flasher . portChangeTimer ) {
829+ clearTimeout ( firmware_flasher . portChangeTimer ) ;
830+ firmware_flasher . portChangeTimer = null ;
831+ }
832+
833+ console . log ( `${ self . logHead } Port changed to:` , port ) ;
834+
835+ if ( GUI . connect_lock ) {
836+ console . log ( `${ self . logHead } Port change ignored during active operation (connect_lock set)` ) ;
837+ return ;
838+ }
839+
840+ // Auto-detect board when port changes and we're on firmware flasher tab
841+ if ( port && port !== "0" && $ ( "input.flash_on_connect" ) . is ( ":checked" ) === false && ! STM32 . rebootMode ) {
842+ console . log ( `${ self . logHead } Auto-detecting board for port change (debounced)` ) ;
843+
844+ // Debounced verification: re-check connect lock when the timeout fires
845+ firmware_flasher . portChangeTimer = setTimeout ( ( ) => {
846+ firmware_flasher . portChangeTimer = null ;
847+ if ( GUI . connect_lock ) {
848+ console . log ( `${ self . logHead } Skipping auto-detect due to active operation at timeout` ) ;
849+ return ;
850+ }
851+ AutoDetect . verifyBoard ( ) ;
852+ } , PORT_CHANGE_DEBOUNCE_MS ) ; // Small delay to ensure port is ready
853+ } else if ( ! port || port === "0" ) {
854+ if ( ! GUI . connect_lock ) {
855+ console . log ( `${ self . logHead } Clearing board selection - no port selected` ) ;
856+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
857+ } else {
858+ console . log ( `${ self . logHead } Not clearing board selection because operation in progress` ) ;
859+ }
860+ }
861+ } ;
862+
863+ firmware_flasher . onDeviceRemoved = function ( devicePath ) {
864+ console . log ( `${ self . logHead } Device removed:` , devicePath ) ;
865+
866+ // If a flashing operation or reboot is in progress, the device may be
867+ // removed intentionally (it reboots into DFU). Don't clear selection or
868+ // buffered firmware in that case as it would prevent the flashing flow.
869+ if ( GUI . connect_lock || STM32 . rebootMode ) {
870+ console . log ( `${ self . logHead } Device removal during active operation/reboot; skipping clear` ) ;
871+ return ;
872+ }
873+
874+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
875+ clearBufferedFirmware ( ) ;
876+ } ;
757877
758- EventBus . $on ( "port-handler:auto-select-usb-device" , detectedUsbDevice ) ;
878+ EventBus . $on ( "port-handler:auto-select-usb-device" , firmware_flasher . detectedUsbDevice ) ;
879+ EventBus . $on ( "port-handler:auto-select-serial-device" , firmware_flasher . detectedSerialDevice ) ;
880+ EventBus . $on ( "ports-input:change" , firmware_flasher . onPortChange ) ;
881+ EventBus . $on ( "port-handler:device-removed" , firmware_flasher . onDeviceRemoved ) ;
759882
760883 async function saveFirmware ( ) {
761884 const fileType = self . firmware_type ;
@@ -1472,6 +1595,17 @@ firmware_flasher.initialize = async function (callback) {
14721595 $ ( "a.exit_dfu" ) . removeClass ( "disabled" ) ;
14731596 }
14741597
1598+ // Auto-detect board if drone is already connected when tab becomes active
1599+ if (
1600+ ( PortHandler . portAvailable && ! $ ( 'select[name="board"]' ) . val ( ) ) ||
1601+ $ ( 'select[name="board"]' ) . val ( ) === "0"
1602+ ) {
1603+ console . log ( `${ self . logHead } Auto-detecting board for already connected device` ) ;
1604+ setTimeout ( ( ) => {
1605+ AutoDetect . verifyBoard ( ) ;
1606+ } , AUTO_DETECT_DELAY_MS ) ; // Small delay to ensure tab is fully loaded
1607+ }
1608+
14751609 GUI . content_ready ( callback ) ;
14761610 }
14771611
@@ -1490,6 +1624,24 @@ firmware_flasher.cleanup = function (callback) {
14901624 $ ( document ) . unbind ( "keypress" ) ;
14911625 $ ( document ) . off ( "click" , "span.progressLabel a" ) ;
14921626
1627+ const cleanupHandler = ( evt , property ) => {
1628+ const handler = firmware_flasher [ property ] ;
1629+ if ( handler ) {
1630+ EventBus . $off ( evt , handler ) ;
1631+ }
1632+ } ;
1633+
1634+ cleanupHandler ( "port-handler:auto-select-usb-device" , "detectedUsbDevice" ) ;
1635+ cleanupHandler ( "port-handler:auto-select-serial-device" , "detectedSerialDevice" ) ;
1636+ cleanupHandler ( "ports-input:change" , "onPortChange" ) ;
1637+ cleanupHandler ( "port-handler:device-removed" , "onDeviceRemoved" ) ;
1638+
1639+ // Clear any pending debounce timer so it cannot fire after cleanup
1640+ if ( firmware_flasher . portChangeTimer ) {
1641+ clearTimeout ( firmware_flasher . portChangeTimer ) ;
1642+ firmware_flasher . portChangeTimer = null ;
1643+ }
1644+
14931645 if ( callback ) callback ( ) ;
14941646} ;
14951647
0 commit comments