Add OTA firmware updates of meshcore-tth nodes, with a side channel

A "Firmware update (OTA)" tile in the repeater hub (admins, tth fork
switch kOtaUpdate) pushes a delta of a meshcore-tth -ota image over the
mesh: the package that starts from the node's image is picked from the
chosen .mcfw files, binary chunks with text as the fallback, an airtime
budget, verify and apply. Optionally on a side channel: the node goes to
another preset by tempradio, the companion follows (checked), the chunks
go zero-hop and both come back at the end.

A port of the reference engine of the tth.meshcore Ansible collection
(module_utils/meshcore_ota_engine.py); test/services/ota/ota_vectors.json
is its test-vector file. Bench 16. 11. 2026: the connector over TCP to a
USB companion, side channel 866.0/SF7, applied a 2.7 kB delta, 20 s
airtime, companion back on its preset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-16 23:32:04 +01:00
1 parent 5a53e45e9a
commit 1b2e0ccdfc
7 files changed
+1711

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
// The OTA protocol against the test vectors of its reference implementation,
// module_utils/meshcore_ota.py of git.rfa.cz/tth-pub/meshcore-ansible
// (tests/vectors/ota_vectors.json there; copy it over when it changes).
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/services/ota/ota_protocol.dart';
Uint8List _bytes(String hex) => Uint8List.fromList([
for (var i = 0; i < hex.length; i += 2)
int.parse(hex.substring(i, i + 2), radix: 16),
]);
String _hex(List<int> b) =>
b.map((x) => x.toRadixString(16).padLeft(2, '0')).join();
void main() {
final v =
jsonDecode(File('test/services/ota/ota_vectors.json').readAsStringSync())
as Map<String, dynamic>;
final packages = [
for (final p in v['packages'] as List)
OtaPackage(p['name'] as String, _bytes(p['hex'] as String)),
];
test('package manifest', () {
for (final (i, p) in (v['packages'] as List).indexed) {
expect(packages[i].format, p['format']);
expect(packages[i].imageSha, p['image_sha']);
expect(packages[i].baseSha, p['base_sha']);
expect(packages[i].version, p['version']);
}
expect(() => OtaPackage('x', Uint8List(300)), throwsFormatException);
});
test('pick', () {
for (final c in v['pick'] as List) {
final r = otaPick(
packages,
c['base'] as String,
fullOk: c['full_ok'] as bool,
);
expect(r.kind.name, c['kind'], reason: '${c['base']}');
expect(r.package?.name, c['package']);
}
});
test('crc16 and the binary write payload', () {
for (final c in v['crc16'] as List) {
expect(otaCrc16(_bytes(c['hex'] as String)), c['crc']);
}
for (final c in v['w_payload'] as List) {
expect(
_hex(
otaWritePayload(
c['sid'] as int,
c['off'] as int,
_bytes(c['chunk'] as String),
),
),
c['hex'],
);
}
});
test('replies', () {
for (final c in v['parse_status'] as List) {
final r = otaParseStatus(c['text'] as String);
expect(r == null ? null : [r.$1, r.$2], c['base']);
}
for (final c in v['parse_base'] as List) {
expect(otaParseBase(c['text'] as String), c['sha8']);
}
for (final c in v['parse_missing'] as List) {
final r = otaParseMissing(_bytes(c['hex'] as String));
expect(r?.$1, c['state']);
expect(r?.$2, c['units']);
}
for (final c in v['text_missing'] as List) {
expect(otaTextMissing(c['text'] as String), c['offsets']);
}
});
test('presets', () {
for (final c in v['preset'] as List) {
final p = OtaPreset.parse(c['text'] as String);
expect([p.freq, p.bw, p.sf, p.cr], c['preset']);
expect(p.toString(), c['fmt']);
}
for (final bad in v['preset_bad'] as List) {
expect(() => OtaPreset.parse(bad as String), throwsFormatException);
}
for (final c in v['parse_radio'] as List) {
final p = OtaPreset.fromReply(c['text'] as String);
expect(p == null ? null : [p.freq, p.bw, p.sf, p.cr], c['preset']);
}
final p = OtaPreset.parse('869.525,62.5,7,5');
expect(p.freqField, 869525); // the companion frame: kHz
expect(p.bwField, 62500); // and Hz
});
test('airtime and frame lengths', () {
for (final c in v['airtime'] as List) {
expect(
otaAirtime(
c['n'] as int,
c['sf'] as int,
(c['bw'] as num).toDouble(),
c['cr'] as int,
),
closeTo((c['secs'] as num).toDouble(), 1e-9),
);
}
final f = v['frame_len'] as Map<String, dynamic>;
for (final c in f['req'] as List) {
expect(otaReqFrameLen(c['payload'] as int), c['len']);
}
for (final c in f['text'] as List) {
expect(otaTextFrameLen(c['text'] as String), c['len']);
}
});
test('airtime bucket', () {
var now = DateTime(2026, 11, 16);
final b = OtaBucket(10, clock: () => now);
expect(b.waitFor(360), 0); // a full hour's budget at once
expect(b.waitFor(1), closeTo(10, 1e-6)); // then 0.1 s per second
now = now.add(const Duration(seconds: 10));
expect(b.waitFor(1), 0);
});
}
+296
View File
@@ -0,0 +1,296 @@
{
"airtime": [
{
"bw": 62.5,
"cr": 1,
"n": 48,
"secs": 0.381952,
"sf": 8
},
{
"bw": 62.5,
"cr": 1,
"n": 172,
"secs": 0.569856,
"sf": 7
},
{
"bw": 62.5,
"cr": 4,
"n": 150,
"secs": 0.778752,
"sf": 7
},
{
"bw": 125.0,
"cr": 1,
"n": 255,
"secs": 9.281536,
"sf": 12
}
],
"crc16": [
{
"crc": 65535,
"hex": ""
},
{
"crc": 57840,
"hex": "00"
},
{
"crc": 10673,
"hex": "313233343536373839"
},
{
"crc": 57070,
"hex": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c"
}
],
"frame_len": {
"req": [
{
"len": 22,
"payload": 2
},
{
"len": 150,
"payload": 139
},
{
"len": 182,
"payload": 160
}
],
"text": [
{
"len": 22,
"text": "fw status"
},
{
"len": 54,
"text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
]
},
"packages": [
{
"base_sha": null,
"format": 1,
"hex": "4d434657010200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d747468636330353938620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
"image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb",
"name": "x-v3.mcfw",
"version": "v1.18.0-tthcc0598b"
},
{
"base_sha": "fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a3",
"format": 2,
"hex": "4d434657020200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d747468636330353938620000000000000000000000000000fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
"image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb",
"name": "x-v3.from-v1.mcfw",
"version": "v1.18.0-tthcc0598b"
},
{
"base_sha": "43a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a",
"format": 2,
"hex": "4d434657020200000000000000000000490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076312e31382e302d74746863633035393862000000000000000000000000000043a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
"image_sha": "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb",
"name": "x-v3.from-v2.mcfw",
"version": "v1.18.0-tthcc0598b"
}
],
"parse_base": [
{
"sha8": "490418bc",
"text": "> 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb"
},
{
"sha8": "490418bc",
"text": "> 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb fresh 490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb"
},
{
"sha8": null,
"text": "ERR: fw status|begin"
}
],
"parse_missing": [
{
"hex": "0100000000",
"state": 1,
"units": []
},
{
"hex": "01000003000500090010000300",
"state": 1,
"units": [
5,
9,
16
]
},
{
"hex": "040000",
"state": null,
"units": null
},
{
"hex": "03000000000000",
"state": 3,
"units": []
}
],
"parse_radio": [
{
"preset": [
869.432,
62.5,
7,
5
],
"text": "> 869.432,62.5,7,5"
},
{
"preset": [
866.5,
62.5,
8,
5
],
"text": "> 866.5,62.5,8,5"
},
{
"preset": null,
"text": "Unknown command"
}
],
"parse_status": [
{
"base": [
456492,
"fe748410"
],
"text": "> idle, batt 4137mV (min 3800), free 352256 B, base 456492 fe748410, delta"
},
{
"base": null,
"text": "> failed: not newer than this image (pkg 1794864486, node 1794864486) [b2 d0 x0 t0]"
},
{
"base": null,
"text": "> rx 12/86 units, batt 4100mV [b12 d0 x0 t0]"
},
{
"base": null,
"text": "Unknown command"
}
],
"pick": [
{
"base": "fe748410",
"full_ok": false,
"kind": "delta",
"package": "x-v3.from-v1.mcfw"
},
{
"base": "43a84e01",
"full_ok": false,
"kind": "delta",
"package": "x-v3.from-v2.mcfw"
},
{
"base": "490418bc",
"full_ok": false,
"kind": "current",
"package": "x-v3.mcfw"
},
{
"base": "00000000",
"full_ok": false,
"kind": "none",
"package": null
},
{
"base": "00000000",
"full_ok": true,
"kind": "full",
"package": "x-v3.mcfw"
}
],
"preset": [
{
"fmt": "869.525,62.5,7,5",
"preset": [
869.525,
62.5,
7,
5
],
"text": "869.525,62.5,7,5"
},
{
"fmt": "866,62.5,7,5",
"preset": [
866.0,
62.5,
7,
5
],
"text": "866,62.5,7,5"
},
{
"fmt": "869.432,62.5,8,8",
"preset": [
869.432,
62.5,
8,
8
],
"text": "869.432,62.5,8,8"
}
],
"preset_bad": [
"869.525,62.5,7",
"869.525,62.5,13,5",
"869.525,62.5,7,4",
"x,62.5,7,5",
"869.525,600,7,5"
],
"text_missing": [
{
"offsets": [],
"text": "> none"
},
{
"offsets": [
0,
96,
2688
],
"text": "> 0 96 2688"
},
{
"offsets": null,
"text": "ERR: not receiving"
}
],
"w_payload": [
{
"chunk": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c",
"hex": "f077010000000000806028030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c",
"off": 0,
"sid": 1
},
{
"chunk": "030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b7279",
"hex": "f077efbe000b00005bc363030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b7279",
"off": 2816,
"sid": 48879
},
{
"chunk": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"hex": "f07768c78000000080e1ee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"off": 128,
"sid": 51048
}
]
}