diff --git a/.github/scripts/check-glibc.sh b/.github/scripts/check-glibc.sh new file mode 100755 index 0000000..a98efd3 --- /dev/null +++ b/.github/scripts/check-glibc.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Fail if anything in a Linux bundle needs a newer glibc than we support. +# +# libserialport.so is loaded at the first port scan, not at startup, so a build +# that needs too new a glibc still launches and just finds no USB ports. +# +# Usage: check-glibc.sh +set -euo pipefail + +bundle="$1" +max="$2" +failed=0 + +for f in "$bundle"/OpenView "$bundle"/lib/*.so; do + # Some files, such as libapp.so, use no glibc symbols, so grep may match nothing. + need=$(objdump -T "$f" | { grep -oE 'GLIBC_[0-9]+(\.[0-9]+)+' || true; } | sed 's/GLIBC_//' | sort -Vu | tail -1) + [ -n "$need" ] || continue + echo "$(basename "$f"): glibc $need" + if [ "$(printf '%s\n%s\n' "$need" "$max" | sort -V | tail -1)" != "$max" ]; then + echo "::error::$(basename "$f") needs glibc $need, newer than the supported $max." + failed=1 + fi +done + +exit "$failed" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 11af442..c1b922b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -61,7 +61,10 @@ jobs: linux: name: Linux x64 needs: version - runs-on: ubuntu-latest + # Binaries need at least the glibc they were built against, so this sets the + # oldest Linux we can run on. Don't use ubuntu-latest: when it moved to a + # newer image, v3.3.1 stopped finding USB ports on Ubuntu 24.04. + runs-on: ubuntu-22.04 timeout-minutes: 60 steps: - name: Checkout @@ -93,6 +96,9 @@ jobs: # store builds must stay on the default `store` channel. run: flutter build linux --release --dart-define=OV_CHANNEL=github + - name: Check glibc requirement + run: .github/scripts/check-glibc.sh build/linux/x64/release/bundle 2.35 + - name: Package run: | cd build/linux/x64/release/bundle @@ -151,6 +157,9 @@ jobs: # store builds must stay on the default `store` channel. run: flutter build linux --release --dart-define=OV_CHANNEL=github + - name: Check glibc requirement + run: .github/scripts/check-glibc.sh build/linux/arm64/release/bundle 2.39 + - name: Package run: | cd build/linux/arm64/release/bundle diff --git a/lib/controllers/scan_controller.dart b/lib/controllers/scan_controller.dart index 5ed2df6..eb61ebe 100644 --- a/lib/controllers/scan_controller.dart +++ b/lib/controllers/scan_controller.dart @@ -47,6 +47,7 @@ class ScanController extends ChangeNotifier { notifyListeners(); try { if (includeUsb) { + _usbResults = const []; final targets = await usb.scan(); _usbResults = targets.map(_annotate).toList(growable: false); } else { diff --git a/lib/transport/usb_serial_service.dart b/lib/transport/usb_serial_service.dart index 75aa21a..8719d64 100644 --- a/lib/transport/usb_serial_service.dart +++ b/lib/transport/usb_serial_service.dart @@ -15,6 +15,11 @@ import 'transport_service.dart'; /// `transport_factory.dart`. The `dart:io` `Platform` check inside `scan` /// is a belt-and-braces guard. class UsbSerialService extends TransportService { + UsbSerialService({List Function()? listPorts}) + : _listPorts = listPorts ?? (() => SerialPort.availablePorts); + + final List Function() _listPorts; + final _bytesController = StreamController.broadcast(); final _eventsController = StreamController.broadcast(); @@ -98,7 +103,7 @@ class UsbSerialService extends TransportService { _setStatus(TransportStatus.scanning); final targets = []; try { - for (final name in SerialPort.availablePorts) { + for (final name in _listPorts()) { final p = SerialPort(name); String display = name; String? subtitle; @@ -121,6 +126,9 @@ class UsbSerialService extends TransportService { } catch (e) { _eventsController.add(TransportEvent(TransportStatus.error, message: 'scan failed', error: e)); + // Let the caller report it: an empty list would read as "nothing plugged + // in", which is misleading when the serial library itself failed to load. + rethrow; } finally { _setStatus(TransportStatus.idle); } diff --git a/lib/ui/screens/scan_screen.dart b/lib/ui/screens/scan_screen.dart index 1766667..2bb7915 100644 --- a/lib/ui/screens/scan_screen.dart +++ b/lib/ui/screens/scan_screen.dart @@ -530,7 +530,9 @@ class _Hints extends StatelessWidget { hints.add(_HintLine( icon: Icons.warning_amber, color: theme.colorScheme.error, - text: 'Scan: $scanError', + text: transport == TransportKind.usb + ? 'Could not list USB ports: $scanError' + : 'Scan: $scanError', )); } @@ -558,7 +560,9 @@ class _Hints extends StatelessWidget { )); } else { // USB - if (results.isEmpty && !scanning) { + if (results.isEmpty && !scanning && scanError != null) { + // The scan failed, so it can't say whether a board is plugged in. + } else if (results.isEmpty && !scanning) { hints.add(_HintLine( icon: Icons.info_outline, color: theme.colorScheme.onSurfaceVariant, diff --git a/pubspec.yaml b/pubspec.yaml index a990c71..12c3386 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: OpenView description: Companion app for ProtoCentral biosignal & sensor boards over USB, BLE, and Wi-Fi. -version: 3.3.1+137 +version: 3.3.2+139 environment: sdk: ^3.3.0 diff --git a/test/transport/usb_scan_test.dart b/test/transport/usb_scan_test.dart new file mode 100644 index 0000000..4335ffd --- /dev/null +++ b/test/transport/usb_scan_test.dart @@ -0,0 +1,63 @@ +// Copyright (c) 2024-2026 protocentral +// SPDX-License-Identifier: MIT + +import 'package:OpenView/controllers/scan_controller.dart'; +import 'package:OpenView/transport/ble_service.dart'; +import 'package:OpenView/transport/transport_service.dart'; +import 'package:OpenView/transport/usb_serial_service.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + const loadError = "Failed to load dynamic library 'libserialport.so': " + "version `GLIBC_2.42' not found"; + + ScanController controllerFor(List Function() listPorts) => + ScanController( + usb: UsbSerialService(listPorts: listPorts), + ble: BleService(), + ); + + test('a failed port listing reaches the caller and resets the status', + () async { + final usb = UsbSerialService(listPorts: () => throw ArgumentError(loadError)); + + await expectLater(usb.scan(), throwsArgumentError); + expect(usb.status, TransportStatus.idle); + }); + + test('the scan controller reports the failure instead of no devices', + () async { + final scan = controllerFor(() => throw ArgumentError(loadError)); + + await scan.refresh(includeBle: false); + + expect(scan.lastError, contains('GLIBC_2.42')); + expect(scan.usbResults, isEmpty); + expect(scan.scanning, isFalse); + }); + + test('a failed rescan drops ports from the previous scan', () async { + var fail = false; + final scan = controllerFor( + () => fail ? throw ArgumentError(loadError) : const []); + + await scan.refresh(includeBle: false); + expect(scan.lastError, isNull); + + fail = true; + await scan.refresh(includeBle: false); + expect(scan.lastError, isNotNull); + expect(scan.usbResults, isEmpty); + }); + + test('no ports and no error stays a clean empty result', () async { + final scan = controllerFor(() => const []); + + await scan.refresh(includeBle: false); + + expect(scan.lastError, isNull); + expect(scan.usbResults, isEmpty); + }); +}