Skip to content

Conversation

@AsCress
Copy link
Collaborator

@AsCress AsCress commented Aug 17, 2025

Adds firmware checks for the legacy firmware and a warning dialog if the user is running the legacy firmware.

Screenshots / Recordings

  1. With legacy firmware:
Screen_recording_20250817_223553.mp4
  1. With new firmware:
Screen_recording_20250817_224100.mp4

@marcnause Could you please verify that this works with the V5 board ? Thanks.

Summary by Sourcery

Add firmware version detection and user warning for legacy firmware support.

New Features:

  • Detect and store the device firmware version via a new getFirmwareVersion API in the packet handler
  • Display a warning dialog on the Instruments screen when legacy firmware (version <3) is detected

Enhancements:

  • Extend BoardStateProvider to fetch firmware version after device connection and notify listeners of legacy firmware
  • Add localizable titles and messages for the legacy firmware alert

@AsCress AsCress self-assigned this Aug 17, 2025
@AsCress AsCress added Feature New addition to the existing app flutter labels Aug 17, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 17, 2025

Reviewer's Guide

This PR implements firmware version checks by extending the low-level packet handler to retrieve firmware versions, integrating the check into the board state provider (triggering a notifier when legacy firmware is detected), and displaying a warning dialog in the instruments screen, alongside updating localization resources for the new alert messages.

Sequence diagram for firmware version check and legacy warning

sequenceDiagram
    participant App as InstrumentsScreen
    participant Board as BoardStateProvider
    participant SciLab as ScienceLab
    participant Packet as PacketHandler
    participant User as actor User

    App->>Board: initialize()
    Board->>SciLab: initialize()
    Board->>SciLab: openDevice()
    Board->>Board: setPSLabVersionIDs()
    Board->>Board: fetchFirmwareVersion()
    Board->>SciLab: getFirmwareVersion()
    SciLab->>Packet: mPacketHandler.getFirmwareVersion()
    Packet->>Packet: sendByte(common)
    Packet->>Packet: sendByte(getFwVersion)
    Packet->>Packet: _commonRead(fwVersionLength)
    Packet-->>SciLab: firmwareVersion
    SciLab-->>Board: firmwareVersion
    Board->>Board: Check firmwareVersion < 3
    Board->>App: legacyFirmwareNotifier triggers
    App->>User: Show legacy firmware warning dialog
Loading

File-Level Changes

Change Details Files
Refactor and add localization keys for firmware alerts
  • Removed duplicate sharingMessage and delete keys
  • Moved sharingMessage and delete definitions to a single location
  • Added legacyFirmwareAlertTitle and legacyFirmwareAlertMessage entries
  • Updated English translations and ARB resources
lib/l10n/app_localizations.dart
lib/l10n/app_localizations_en.dart
lib/l10n/app_en.arb
Retrieve firmware version in PacketHandler
  • Introduced fwVersionLength constant
  • Added getFirmwareVersion() method with fallback logic
  • Logged errors and returned default on failure
  • Updated protocol command set with getFwVersion
lib/communication/packet_handler.dart
lib/communication/commands_proto.dart
Integrate firmware check into BoardStateProvider
  • Added pslabFirmwareVersion field and legacyFirmwareNotifier
  • Implemented fetchFirmwareVersion() to set notifier when version < 3
  • Called fetchFirmwareVersion() in initialize, USB and Wi-Fi connection flows
  • Ensured notifyListeners() after firmware check
lib/providers/board_state_provider.dart
Display legacy firmware warning dialog in UI
  • Subscribed to legacyFirmwareNotifier in InstrumentsScreen initState
  • Presented AlertDialog with localized title, message and OK button
  • Used post-frame callback to avoid build conflicts
lib/view/instruments_screen.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `lib/providers/board_state_provider.dart:102` </location>
<code_context>
+      pslabFirmwareVersion =
+          await getIt.get<ScienceLab>().mPacketHandler.getFirmwareVersion();
+    }
+    if (pslabFirmwareVersion < 3 && pslabFirmwareVersion != 0) {
+      legacyFirmwareNotifier.value = "LegacyFirmwareDetected";
+    }
</code_context>

<issue_to_address>
Edge case: firmware version 0 may indicate an error, but is excluded from legacy detection.

Version 0 may represent a failed read or uninitialized state. Please handle this explicitly to prevent silent failures or missed alerts.
</issue_to_address>

### Comment 2
<location> `lib/communication/packet_handler.dart:142` </location>
<code_context>
+      sendByte(_mCommandsProto.common);
+      sendByte(_mCommandsProto.getFwVersion);
+      int numBytesRead = await _commonRead(fwVersionLength);
+      if (numBytesRead == 1) {
+        return 2;
+      } else {
+        return _buffer[0];
</code_context>

<issue_to_address>
Returning a hardcoded value (2) for numBytesRead == 1 may be unclear.

Please add a comment explaining why 2 is returned when numBytesRead is 1, or refactor to make the logic clearer.

Suggested implementation:

```
      if (numBytesRead == 1) {
        // If only one byte is read, it indicates a legacy device or error state.
        // Returning 2 as a sentinel value to signal this special case.
        return 2;
      } else {
        return _buffer[0];

```

For even greater clarity, you could define a named constant at the top of the file or class, e.g.:
```dart
static const int legacyDeviceSentinel = 2;
```
and use `return legacyDeviceSentinel;` instead of `return 2;`.
</issue_to_address>

### Comment 3
<location> `lib/communication/packet_handler.dart:147` </location>
<code_context>
+      } else {
+        return _buffer[0];
+      }
+    } catch (e) {
+      logger.e(e);
+    }
+    return 0;
</code_context>

<issue_to_address>
Consider providing more context in error logging for firmware version fetch.

Include details about the operation and relevant parameters in the log to aid debugging and traceability.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    } catch (e) {
      logger.e(e);
    }
    return 0;
=======
    } catch (e) {
      logger.e(
        'Error fetching firmware version: '
        'command=${_mCommandsProto.getFwVersion}, '
        'fwVersionLength=$fwVersionLength, '
        'exception=$e'
      );
    }
    return 0;
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 17, 2025

Build Status

Build successful. APKs to test: https://github.com/fossasia/pslab-app/actions/runs/17189400070/artifacts/3837871033.

Screenshots

Android Screenshots
iPhone Screenshots
iPad Screenshots

@marcnause
Copy link
Contributor

@marcnause Could you please verify that this works with the V5 board ? Thanks.

@AsCress Yes, I can do that, just need to find my PICKit... 🙈

Copy link
Contributor

@marcnause marcnause left a comment

Choose a reason for hiding this comment

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

I was able to test successfully with both the v5 board and the v6 board.

@marcnause marcnause enabled auto-merge (squash) August 24, 2025 13:36
@marcnause marcnause merged commit 013c2d4 into fossasia:flutter Aug 24, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New addition to the existing app flutter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants