Skip to content

Serial refactor part 2 #4404

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

Merged
merged 7 commits into from
Mar 31, 2025
Merged

Serial refactor part 2 #4404

merged 7 commits into from
Mar 31, 2025

Conversation

haslinghuis
Copy link
Member

@haslinghuis haslinghuis commented Mar 30, 2025

This pull request introduces significant refactoring and enhancements to the port handling logic, aiming to simplify the codebase and improve device management. The key changes include consolidating device handling methods, adding new messages, and enhancing the Serial class for better protocol management.

Refactoring and Simplification:

  • Consolidated device handling methods by replacing addedSerialDevice, addedBluetoothDevice, and addedUsbDevice with handleDeviceAdded, and similarly for removal methods. This reduces redundancy and centralizes device management logic (src/js/port_handler.js). [1] [2] [3]
  • Introduced updateDeviceList method to handle device list updates for different types (serial, bluetooth, usb) in a unified manner (src/js/port_handler.js).

Protocol Management Enhancements:

  • Added _getProtocolByType and _getProtocolType methods in the Serial class to safely access and identify protocols, improving the flexibility and maintainability of protocol management (src/js/serial.js). [1] [2]
  • Updated getDevices and requestPermissionDevice methods in the Serial class to support specific protocol types, allowing for more granular control over device interactions (src/js/serial.js).

UI and Messaging Improvements:

  • Added new localized messages for rebooting the flight controller to the locales/en/messages.json file, enhancing user feedback during device operations (locales/en/messages.json).

Code Cleanup:

  • Removed unused imports and redundant methods from port_handler.js, streamlining the code and improving readability (src/js/port_handler.js). [1] [2]

These changes collectively enhance the robustness and maintainability of the port handling and protocol management logic, providing a more streamlined and user-friendly experience.

Copy link

netlify bot commented Mar 30, 2025

Deploy Preview for origin-betaflight-app ready!

Name Link
🔨 Latest commit bff4b8e
🔍 Latest deploy log https://app.netlify.com/sites/origin-betaflight-app/deploys/67eb005bea3c360008315cd1
😎 Deploy Preview https://deploy-preview-4404.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.

@haslinghuis haslinghuis requested a review from Copilot March 30, 2025 19:52
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the device handling and protocol management logic across several modules while enhancing error handling and consistency. Key changes include:

  • Consolidating connection reinitialization and reboot handling in serial_backend.js.
  • Introducing a unified protocol map and helper methods in serial.js.
  • Adjusting the reader lock release in WebSerial.js to fix a DFU mode bug.
  • Streamlining device permission and management flows in port_handler.js.

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
src/js/serial_backend.js Reworked connection reinitialization and reboot dialog handling.
src/js/serial.js Added a unified protocol map and helper functions for protocol access.
src/js/protocols/WebSerial.js Modified reader lock handling to address DFU mode issues.
src/js/port_handler.js Updated device permission and management flows with unified methods.
Files not reviewed (1)
  • locales/en/messages.json: Language not supported
Comments suppressed due to low confidence (3)

src/js/serial_backend.js:835

  • The callback is invoked both here and again inside the setTimeout in showRebootDialog, which may lead to unexpected double invocation. Consider consolidating the callback so it is only called once after the reboot process completes.
if (callback && typeof callback === "function") { callback(); }

src/js/serial.js:27

  • For consistency with the _getProtocolType method, update the protocol map key to 'webserial' (and similarly update 'bluetooth' to 'webbluetooth') so that the naming aligns across the code.
serial: this._webSerial, // TODO: should be 'webserial'

src/js/protocols/WebSerial.js:277

  • Ensure that releasing the reader lock without calling cancel fully cleans up the reader, as this change is critical for DFU mode functionality.
await this.reader.releaseLock();

@haslinghuis haslinghuis requested a review from Copilot March 30, 2025 20:12
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the device handling logic and improves protocol management and error handling across serial communication modules. Key changes include:

  • Consolidation and simplification of device addition/removal logic in port_handler.js.
  • Enhanced protocol mapping and safer device access methods in serial.js.
  • UI improvements for connection reinitialization and reboot progress in serial_backend.js, along with a bug fix in WebSerial.js to ensure proper reader lock release.

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/js/serial_backend.js Added reboot progress modal and refactored connection reinit.
src/js/serial.js Updated protocol mapping/handling and error logging.
src/js/protocols/WebSerial.js Fixed bug by releasing reader lock correctly.
src/js/port_handler.js Unified device addition/removal and updated device list logic.
Files not reviewed (1)
  • locales/en/messages.json: Language not supported
Comments suppressed due to low confidence (2)

src/js/serial.js:27

  • [nitpick] Consider updating the key from 'serial' to 'webserial' as noted in the TODO comment to maintain consistent protocol naming.
serial: this._webSerial, // TODO: should be 'webserial'

src/js/serial_backend.js:840

  • [nitpick] It might be beneficial to clear the progress update interval once progress reaches 100% rather than waiting for the overall timeout, to avoid redundant style updates.
progress += 5;

// Get device path safely
const devicePath = device?.path || (typeof device === "string" ? device : null);

if (!devicePath) {
Copy link
Preview

Copilot AI Mar 30, 2025

Choose a reason for hiding this comment

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

After logging a warning for a missing devicePath, exit the function immediately to prevent calling 'devicePath.startsWith' on a null value.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Copy link
Member Author

Choose a reason for hiding this comment

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

See line 101 😕

@haslinghuis haslinghuis requested a review from Copilot March 30, 2025 22:04
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request refactors and simplifies device handling while enhancing protocol management and adding UI improvements for device reboot feedback. Key updates include consolidating device handling methods, enhancing the Serial class with a unified protocol map and error-safe getters, and introducing a progress dialog for reboot operations.

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
src/js/serial_backend.js Added reboot progress dialog UI and improved connection reinitialization flow.
src/js/serial.js Updated protocol mapping and event forwarding to support consistent protocol types.
src/js/protocols/WebSerial.js Fixed asynchronous release of the reader lock by awaiting the releaseLock call.
src/js/port_handler.js Unified device handling methods with a common updateDeviceList approach.
Files not reviewed (1)
  • locales/en/messages.json: Language not supported
Comments suppressed due to low confidence (2)

src/js/serial.js:27

  • [nitpick] Consider renaming the 'serial' key in _protocolMap to 'webserial' for consistency with the protocol naming conventions suggested in the comment.
serial: this._webSerial, // TODO: should be 'webserial'

src/js/serial.js:28

  • [nitpick] Consider renaming the 'bluetooth' key in _protocolMap to 'webbluetooth' to align with the suggested naming style and improve clarity.
bluetooth: this._bluetooth, // TODO: should be 'webbluetooth'

@mituritsyn
Copy link
Contributor

it tries to reboot on disconnect while the FC in gpspassthrough mode.
P.S. it not succeed so passthrough works fine
image

@mituritsyn
Copy link
Contributor

Should a new reboot status be shown when the preset is applied?

@nerdCopter nerdCopter self-requested a review March 31, 2025 14:57
@nerdCopter
Copy link
Member

nerdCopter commented Mar 31, 2025

c7276c1b is not detecting OSD Protocol with Detect. (or maybe just not setting)

@haslinghuis
Copy link
Member Author

@nerdCopter will be fixed in #4405

@nerdCopter
Copy link
Member

c7276c1b

  • flashed FOXEERF405, made backup, applied backup via CLI (with save). no dialog. no issues.
  • all tabs with save & reboot shows dialog, then reconnects.
  • exiting CLI tab (click some other tab) shows dialog, then reconnects.
  • did not really do any configuring with tabs
  • what else needs testing?

@haslinghuis
Copy link
Member Author

This PR specifically updates bluetooth connection to use new serial facade. Tested with SPBE F405 MINI.

  • One thing not working with bluetooth is autodetect, but as we cannot flash over bluetooth at the moment, this is less important for now.
  • Auto-reconnect does not work in master now, so prefer to fix that separate.

Copy link
Member

@VitroidFPV VitroidFPV left a comment

Choose a reason for hiding this comment

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

I think it'd be better to have a single message along the lines of "Rebooting flight controller, reconnect when ready" instead of a separate message that blocks interaction. But let's see what others think.

Otherwise works great, approving

@haslinghuis
Copy link
Member Author

Rebased on master

@haslinghuis haslinghuis merged commit 2d36e62 into betaflight:master Mar 31, 2025
9 checks passed
@haslinghuis haslinghuis deleted the serialv2 branch March 31, 2025 20:54
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.

4 participants