Files
meshcore-open/lib/services/ota/ota_release.dart
T
tthandClaude Opus 5.5 5edb1f3ab7
build / ci-image (push) Successful in 12m58s
build / build (push) Successful in 11m54s
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 <noreply@anthropic.com>
2026-11-16 23:45:48 +01:00

104 lines
3.6 KiB
Dart

/// 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<http.Response> _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<OtaReleaseMatch> find(String target, String base) async {
final rel = jsonDecode((await _get(kOtaReleaseApi)).body) as Map;
final tag = rel['tag_name'] as String;
final urls = <String, String>{
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 = <String, String>{
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));
}
}