From 5edb1f3ab7ae4c79baf5cebb85350959bc0b4173 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Mon, 16 Nov 2026 23:45:48 +0100 Subject: [PATCH] OTA: take the package from the latest meshcore-tth release by itself With no file chosen, the node's target (`fw ver`) and image (`fw status` / `fw base`) pick the delta from manifest.json of the latest public release on git.rfa.cz; it is checked against SHA256SUMS (the node checks the manifest signature itself). Choosing .mcfw files stays for own builds. Bench 16. 11. 2026: a node on v1.18.0+tth.20261115.2 got the delta to 20261116 found and downloaded by the app, over the side channel. Co-Authored-By: Claude Opus 5.5 --- lib/screens/repeater_ota_screen.dart | 25 ++++-- lib/services/ota/ota_release.dart | 103 +++++++++++++++++++++++ lib/services/ota/ota_transfer.dart | 37 ++++++++- test/services/ota/ota_release_test.dart | 104 ++++++++++++++++++++++++ 4 files changed, 260 insertions(+), 9 deletions(-) create mode 100644 lib/services/ota/ota_release.dart create mode 100644 test/services/ota/ota_release_test.dart diff --git a/lib/screens/repeater_ota_screen.dart b/lib/screens/repeater_ota_screen.dart index 6b17ed7d..6fb2b4b3 100644 --- a/lib/screens/repeater_ota_screen.dart +++ b/lib/screens/repeater_ota_screen.dart @@ -182,10 +182,10 @@ class _RepeaterOtaScreenState extends State { Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 0), child: Text( - 'For nodes that run an -ota image of meshcore-tth. Pick ' - 'the .mcfw packages of a release; the delta that starts ' - 'from the node\'s image is taken. Only deltas go over ' - 'the mesh. Keep the app open until it is done.', + 'For nodes that run an -ota image of meshcore-tth. The ' + 'delta that starts from the node\'s image is taken; only ' + 'deltas go over the mesh. Keep the app open until it is ' + 'done.', style: textTheme.bodySmall?.copyWith( color: scheme.onSurfaceVariant, ), @@ -197,7 +197,11 @@ class _RepeaterOtaScreenState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ if (_packages.isEmpty) - const Text('none chosen') + const Text( + 'The latest release from git.rfa.cz/tth-pub/' + 'meshcore-tth, the delta for the node\'s image. ' + 'Or choose .mcfw files (your own builds).', + ) else for (final p in _packages) Text( @@ -211,6 +215,13 @@ class _RepeaterOtaScreenState extends State { icon: const Icon(Icons.folder_open), label: const Text('Choose .mcfw files'), ), + if (_packages.isNotEmpty) + TextButton( + onPressed: _running + ? null + : () => setState(_packages.clear), + child: const Text('Use the latest release'), + ), ], ), ), @@ -311,9 +322,7 @@ class _RepeaterOtaScreenState extends State { children: [ Expanded( child: FilledButton.icon( - onPressed: _running || _packages.isEmpty - ? null - : _start, + onPressed: _running ? null : _start, icon: const Icon(Icons.system_update_alt), label: const Text('Update'), ), diff --git a/lib/services/ota/ota_release.dart b/lib/services/ota/ota_release.dart new file mode 100644 index 00000000..03e34314 --- /dev/null +++ b/lib/services/ota/ota_release.dart @@ -0,0 +1,103 @@ +/// The OTA package for a node, straight from the public meshcore-tth release +/// (git.rfa.cz/tth-pub/meshcore-tth) -- what `tools/ota_pack.py fetch` does. +/// +/// The node says which image it runs (`fw ver`: target, `fw status`/`fw base`: +/// sha256 of the image); the release's manifest.json maps the target to its +/// full package and the deltas by the sha256 of the image they start from. The +/// package is checked against the release's SHA256SUMS; the node checks the +/// Ed25519 signature of its manifest itself before the rest goes on air. +library; + +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; +import 'package:http/http.dart' as http; + +import 'ota_protocol.dart'; + +const String kOtaReleaseApi = + 'https://git.rfa.cz/api/v1/repos/tth-pub/meshcore-tth/releases/latest'; + +/// What the latest release holds for a node. +class OtaReleaseMatch { + OtaReleaseMatch(this.tag, this.package, {this.current = false}); + final String tag; + final OtaPackage? package; // null when [current] or no delta fits + final bool current; +} + +class OtaReleaseError implements Exception { + OtaReleaseError(this.message); + final String message; + @override + String toString() => message; +} + +/// `fw ver` -> the target ('> target=Xiao_nrf52_repeater-ota epoch=...'). +String? otaParseTarget(String reply) => + RegExp(r'target=(\S+)').firstMatch(reply)?.group(1); + +class OtaRelease { + OtaRelease({http.Client? client}) : _client = client ?? http.Client(); + + final http.Client _client; + + Future _get(String url) async { + final r = await _client + .get(Uri.parse(url)) + .timeout(const Duration(seconds: 60)); + if (r.statusCode != 200) { + throw OtaReleaseError('HTTP ${r.statusCode} for $url'); + } + return r; + } + + /// The delta of the latest release for [target] that starts from the image + /// whose sha256 begins with [base]. + Future find(String target, String base) async { + final rel = jsonDecode((await _get(kOtaReleaseApi)).body) as Map; + final tag = rel['tag_name'] as String; + final urls = { + for (final a in rel['assets'] as List) + a['name'] as String: a['browser_download_url'] as String, + }; + final manifestUrl = urls['manifest.json']; + final sumsUrl = urls['SHA256SUMS']; + if (manifestUrl == null || sumsUrl == null) { + throw OtaReleaseError('$tag: no manifest.json / SHA256SUMS'); + } + final manifest = jsonDecode((await _get(manifestUrl)).body) as Map; + final env = (manifest['envs'] as Map)[target] as Map?; + final ota = env?['ota'] as Map?; + if (ota == null) { + throw OtaReleaseError('$tag has no OTA packages for $target'); + } + if ((ota['image_sha256'] as String).startsWith(base)) { + return OtaReleaseMatch(tag, null, current: true); + } + String? file; + for (final d in (ota['deltas'] as Map).values) { + if ((d['base_sha256'] as String).startsWith(base)) { + file = d['file'] as String; + } + } + if (file == null) return OtaReleaseMatch(tag, null); + final url = urls[file]; + if (url == null) throw OtaReleaseError('$tag: $file is not attached'); + final sums = { + for (final line in const LineSplitter().convert( + (await _get(sumsUrl)).body, + )) + if (line.trim().isNotEmpty) + line.trim().split(RegExp(r'\s+\*?')).last: line + .trim() + .split(' ') + .first, + }; + final data = (await _get(url)).bodyBytes; + if (sha256.convert(data).toString() != sums[file]) { + throw OtaReleaseError('$file: sha256 does not match SHA256SUMS'); + } + return OtaReleaseMatch(tag, OtaPackage(file, data)); + } +} diff --git a/lib/services/ota/ota_transfer.dart b/lib/services/ota/ota_transfer.dart index 7bfe8127..0571b23d 100644 --- a/lib/services/ota/ota_transfer.dart +++ b/lib/services/ota/ota_transfer.dart @@ -27,6 +27,7 @@ import '../../connector/meshcore_protocol.dart'; import '../../models/contact.dart'; import '../repeater_command_service.dart'; import 'ota_protocol.dart'; +import 'ota_release.dart'; class OtaError implements Exception { OtaError(this.message); @@ -66,6 +67,7 @@ class OtaTransfer { this.window = 15, this.apply = true, this.applyWait = const Duration(seconds: 60), + this.release, }) : _bucket = OtaBucket(duty); final MeshCoreConnector connector; @@ -81,6 +83,10 @@ class OtaTransfer { final bool apply; final Duration applyWait; + /// Where the package comes from when [packages] is empty (default: the + /// latest public release). + final OtaRelease? release; + final OtaBucket _bucket; late final RepeaterCommandService _cli = RepeaterCommandService(connector); StreamSubscription? _frames; @@ -544,7 +550,36 @@ class OtaTransfer { log('no base image: not an -ota build'); return OtaResult(OtaState.noPackage, airtime: airtime); } - final pick = otaPick(packages, base); + var pkgs = packages; + if (pkgs.isEmpty) { + final target = otaParseTarget(await _ask('fw ver')); + if (target == null) { + log('`fw ver` names no target: not an -ota build'); + return OtaResult(OtaState.noPackage, airtime: airtime); + } + log('looking for $target from $base in the latest release'); + final OtaReleaseMatch m; + try { + m = await (release ?? OtaRelease()).find(target, base); + } on OtaReleaseError catch (e) { + throw OtaError('release: $e'); + } catch (e) { + throw OtaError('release: cannot download ($e)'); + } + if (m.current) { + log('the node already runs ${m.tag}'); + return OtaResult(OtaState.current, airtime: airtime); + } + if (m.package == null) { + log( + '${m.tag} has no delta from $base (too old: a full image over USB/BLE)', + ); + return OtaResult(OtaState.noPackage, airtime: airtime); + } + log('${m.tag}: ${m.package!.name}, sha256 ok'); + pkgs = [m.package!]; + } + final pick = otaPick(pkgs, base); if (pick.kind == OtaPickKind.current) { log('the node already runs ${pick.package!.name}'); return OtaResult( diff --git a/test/services/ota/ota_release_test.dart b/test/services/ota/ota_release_test.dart new file mode 100644 index 00000000..ddf2773f --- /dev/null +++ b/test/services/ota/ota_release_test.dart @@ -0,0 +1,104 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:crypto/crypto.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as http; +import 'package:http/testing.dart'; +import 'package:meshcore_open/services/ota/ota_release.dart'; + +const _target = 'Xiao_nrf52_repeater-ota'; +const _base = + 'fa1704ebb3f5a22d17914948a5e5252b8ab5d583d682ee0cda490b4f6b0fb6a1'; +const _image = + 'c7f1000f2ae05998a6b03c965929cd1ef6f6c6b1561d57fc491e6f5ef3d2ac84'; +const _file = '$_target-v2.from-v1.mcfw'; + +Uint8List _package() { + final b = Uint8List(320); + b.setAll(0, ascii.encode('MCFW')); + b[4] = 2; + return b; +} + +MockClient _server({Uint8List? served}) { + final pkg = _package(); + final body = served ?? pkg; + final sums = '${sha256.convert(pkg)} $_file\n${'0' * 64} other.mcfw\n'; + const dl = 'https://git.rfa.cz/dl/'; + return MockClient((req) async { + final u = req.url.toString(); + if (u == kOtaReleaseApi) { + return http.Response( + jsonEncode({ + 'tag_name': 'v2', + 'assets': [ + for (final n in ['manifest.json', 'SHA256SUMS', _file]) + {'name': n, 'browser_download_url': '$dl$n'}, + ], + }), + 200, + ); + } + if (u == '${dl}manifest.json') { + return http.Response( + jsonEncode({ + 'envs': { + _target: { + 'ota': { + 'image_sha256': _image, + 'deltas': { + 'v1': {'file': _file, 'base_sha256': _base}, + }, + }, + }, + }, + }), + 200, + ); + } + if (u == '${dl}SHA256SUMS') return http.Response(sums, 200); + if (u == '$dl$_file') return http.Response.bytes(body, 200); + return http.Response('', 404); + }); +} + +void main() { + test('target from fw ver', () { + expect(otaParseTarget('> target=$_target epoch=1794851405'), _target); + expect(otaParseTarget('Unknown command'), isNull); + }); + + test('the delta for the node image, checked against SHA256SUMS', () async { + final m = await OtaRelease(client: _server()).find(_target, 'fa1704eb'); + expect(m.tag, 'v2'); + expect(m.package!.name, _file); + expect(m.package!.delta, isTrue); + }); + + test('a node on the release image is current', () async { + final m = await OtaRelease(client: _server()).find(_target, 'c7f1000f'); + expect(m.current, isTrue); + }); + + test('no delta from an image too old', () async { + final m = await OtaRelease(client: _server()).find(_target, 'deadbeef'); + expect(m.current, isFalse); + expect(m.package, isNull); + }); + + test('a package that does not match SHA256SUMS is refused', () async { + final bad = _package()..[300] = 1; + expect( + OtaRelease(client: _server(served: bad)).find(_target, 'fa1704eb'), + throwsA(isA()), + ); + }); + + test('a target the release does not build', () async { + expect( + OtaRelease(client: _server()).find('Other-ota', 'fa1704eb'), + throwsA(isA()), + ); + }); +}