@@ -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,51 @@ 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 ( ) {
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 ( ) {
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 ?. rebootMode ) {
73+ firmware_flasher . portChangeTimer = setTimeout ( ( ) => {
74+ firmware_flasher . portChangeTimer = null ;
75+ AutoDetect . verifyBoard ( ) ;
76+ } , PORT_CHANGE_DEBOUNCE_MS ) ;
77+ } else if ( ! port || port === "0" ) {
78+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
79+ }
80+ } ,
81+ onDeviceRemoved : function ( ) {
82+ // Avoid clearing when removal is expected during flashing/reboot
83+ if ( GUI . connect_lock || STM32 . rebootMode ) {
84+ return ;
85+ }
86+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
87+ firmware_flasher ?. clearBufferedFirmware ( ) ;
88+ } ,
89+ // Single debounce timer shared across instances
90+ portChangeTimer : null ,
4391} ;
4492
4593firmware_flasher . initialize = async function ( callback ) {
@@ -615,6 +663,10 @@ firmware_flasher.initialize = async function (callback) {
615663 self . filename = null ;
616664 }
617665
666+ // Expose the implementation to the module so module-scoped handlers that
667+ // may run before initialize() completes can call it safely.
668+ firmware_flasher . clearBufferedFirmware = clearBufferedFirmware ;
669+
618670 $ ( 'select[name="board"]' ) . select2 ( ) ;
619671 $ ( 'select[name="osdProtocols"]' ) . select2 ( ) ;
620672 $ ( 'select[name="radioProtocols"]' ) . select2 ( ) ;
@@ -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 ( ) {
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+ }
757839
758- EventBus . $on ( "port-handler:auto-select-usb-device" , detectedUsbDevice ) ;
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+ } ;
877+
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 ;
@@ -1389,6 +1512,11 @@ firmware_flasher.initialize = async function (callback) {
13891512 }
13901513 }
13911514
1515+ // Expose the local startFlashing implementation to module callers/tests so
1516+ // module-scoped handlers can safely call firmware_flasher.startFlashing()
1517+ // even if those callers ran before initialize() completed.
1518+ firmware_flasher . startFlashing = startFlashing ;
1519+
13921520 $ ( "a.flash_firmware" ) . on ( "click" , async function ( ) {
13931521 if ( GUI . connect_lock ) {
13941522 return ;
@@ -1472,6 +1600,17 @@ firmware_flasher.initialize = async function (callback) {
14721600 $ ( "a.exit_dfu" ) . removeClass ( "disabled" ) ;
14731601 }
14741602
1603+ // Auto-detect board if drone is already connected when tab becomes active
1604+ if (
1605+ ( PortHandler . portAvailable && ! $ ( 'select[name="board"]' ) . val ( ) ) ||
1606+ $ ( 'select[name="board"]' ) . val ( ) === "0"
1607+ ) {
1608+ console . log ( `${ self . logHead } Auto-detecting board for already connected device` ) ;
1609+ setTimeout ( ( ) => {
1610+ AutoDetect . verifyBoard ( ) ;
1611+ } , AUTO_DETECT_DELAY_MS ) ; // Small delay to ensure tab is fully loaded
1612+ }
1613+
14751614 GUI . content_ready ( callback ) ;
14761615 }
14771616
@@ -1490,6 +1629,24 @@ firmware_flasher.cleanup = function (callback) {
14901629 $ ( document ) . unbind ( "keypress" ) ;
14911630 $ ( document ) . off ( "click" , "span.progressLabel a" ) ;
14921631
1632+ const cleanupHandler = ( evt , property ) => {
1633+ const handler = firmware_flasher [ property ] ;
1634+ if ( handler ) {
1635+ EventBus . $off ( evt , handler ) ;
1636+ }
1637+ } ;
1638+
1639+ cleanupHandler ( "port-handler:auto-select-usb-device" , "detectedUsbDevice" ) ;
1640+ cleanupHandler ( "port-handler:auto-select-serial-device" , "detectedSerialDevice" ) ;
1641+ cleanupHandler ( "ports-input:change" , "onPortChange" ) ;
1642+ cleanupHandler ( "port-handler:device-removed" , "onDeviceRemoved" ) ;
1643+
1644+ // Clear any pending debounce timer so it cannot fire after cleanup
1645+ if ( firmware_flasher . portChangeTimer ) {
1646+ clearTimeout ( firmware_flasher . portChangeTimer ) ;
1647+ firmware_flasher . portChangeTimer = null ;
1648+ }
1649+
14931650 if ( callback ) callback ( ) ;
14941651} ;
14951652
0 commit comments