diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index f3331c38..17fbbe1e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -60,6 +60,8 @@ import '../utils/battery_utils.dart'; import '../utils/platform_info.dart'; import 'meshcore_uuids.dart'; import 'meshcore_protocol.dart'; +import '../helpers/region_key.dart'; +import '../storage/channel_region_scope_store.dart'; import 'offband_device_ui.dart'; import 'caplog_reassembler.dart'; @@ -552,6 +554,8 @@ class MeshCoreConnector extends ChangeNotifier { final MessageStore _messageStore = MessageStore(); final ChannelOrderStore _channelOrderStore = ChannelOrderStore(); final ChannelSettingsStore _channelSettingsStore = ChannelSettingsStore(); + final ChannelRegionScopeStore _channelRegionScopeStore = + ChannelRegionScopeStore(); final ContactSettingsStore _contactSettingsStore = ContactSettingsStore(); final ContactStore _contactStore = ContactStore(); final ContactDiscoveryStore _discoveryContactStore = ContactDiscoveryStore(); @@ -4687,7 +4691,8 @@ class MeshCoreConnector extends ChangeNotifier { _pendingChannelSentQueue.add(reactionQueueId); await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime); try { - await sendFrame( + await _sendChannelFrameScoped( + channel.index, buildSendChannelTextMsgFrame( channel.index, text, @@ -4729,7 +4734,8 @@ class MeshCoreConnector extends ChangeNotifier { final outboundText = prepareChannelOutboundText(channel.index, text); await _waitForRadioQuiet(lastInboundRxTime: _lastChannelMsgRxTime); try { - await sendFrame( + await _sendChannelFrameScoped( + channel.index, buildSendChannelTextMsgFrame( channel.index, outboundText, @@ -4758,6 +4764,66 @@ class MeshCoreConnector extends ChangeNotifier { } } + /// Send a channel frame, applying the channel's region scope on the wire if + /// one is configured (Feature #812, Epic #815). Fail-closed: a scoped channel + /// on firmware that cannot scope is blocked, never sent unscoped; the scope is + /// set before the send and cleared to the node default after. + Future _sendChannelFrameScoped( + int channelIndex, + Uint8List frame, { + String? channelSendQueueId, + bool expectsGenericAck = false, + }) async { + final regionName = await _channelRegionScopeStore.scopeFor(channelIndex); + if (regionName == null) { + await sendFrame( + frame, + channelSendQueueId: channelSendQueueId, + expectsGenericAck: expectsGenericAck, + ); + return; + } + if (!supportsRegionScope) { + throw StateError( + 'channel $channelIndex is region-scoped to "$regionName" but the ' + 'firmware does not support region scoping', + ); + } + final key = transportKeyForName(regionName); + if (key == null) { + throw StateError( + 'cannot derive a transport key for region "$regionName"', + ); + } + await setChannelSendScope(key); + try { + await sendFrame( + frame, + channelSendQueueId: channelSendQueueId, + expectsGenericAck: expectsGenericAck, + ); + } finally { + try { + await clearSendScope(); + } catch (e) { + appLogger.warn( + 'failed to clear region send-scope after a channel send: $e', + tag: 'Region', + ); + } + } + } + + @visibleForTesting + ChannelRegionScopeStore get channelRegionScopeStoreForTest => + _channelRegionScopeStore; + + @visibleForTesting + Future sendChannelFrameScopedForTest( + int channelIndex, + Uint8List frame, + ) => _sendChannelFrameScoped(channelIndex, frame); + Future removeContact(Contact contact) async { if (!isConnected) return; @@ -6590,6 +6656,7 @@ class MeshCoreConnector extends ChangeNotifier { _messageStore.setPublicKeyHex = selfPublicKeyHex; _channelOrderStore.setPublicKeyHex = selfPublicKeyHex; _channelSettingsStore.setPublicKeyHex = selfPublicKeyHex; + _channelRegionScopeStore.setPublicKeyHex = selfPublicKeyHex; _contactSettingsStore.setPublicKeyHex = selfPublicKeyHex; _contactStore.setPublicKeyHex = selfPublicKeyHex; _channelStore.setPublicKeyHex = selfPublicKeyHex; diff --git a/test/connector/channel_scope_send_test.dart b/test/connector/channel_scope_send_test.dart new file mode 100644 index 00000000..d1912c43 --- /dev/null +++ b/test/connector/channel_scope_send_test.dart @@ -0,0 +1,72 @@ +// Epic #815 T C3: per-send region scoping around a channel send. +// Drives the scoping helper via its test seam, capturing the frame sequence. + +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +// Known vector: first 16 bytes of SHA256("#oki"). +final _okiKey = Uint8List.fromList([ + 0x59, 0x9f, 0xe2, 0x54, 0x60, 0x02, 0x70, 0x9f, // + 0xa4, 0x93, 0xb1, 0x5d, 0xe5, 0x8b, 0xdd, 0x0d, +]); + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + late MeshCoreConnector connector; + final frame = Uint8List.fromList([cmdSendChannelTxtMsg, 0, 1, 2, 3]); + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + connector = MeshCoreConnector(); + connector.setConnectedForTest(); + connector.setFirmwareVerCodeForTest(22); + connector.channelRegionScopeStoreForTest.setPublicKeyHex = + 'aabbccddee0011223344'; + }); + + test('an unscoped channel sends the frame alone', () async { + final sent = []; + connector.sendFrameOverrideForTest = sent.add; + await connector.sendChannelFrameScopedForTest(0, frame); + expect(sent, hasLength(1)); + expect(sent.single, frame); + }); + + test('a scoped channel sets the key, sends, then clears', () async { + await connector.channelRegionScopeStoreForTest.setScope(0, 'oki'); + final sent = []; + connector.sendFrameOverrideForTest = sent.add; + await connector.sendChannelFrameScopedForTest(0, frame); + expect(sent, hasLength(3)); + // set scope: [54, 0, key16] + expect(sent[0][0], cmdSetFloodScopeKey); + expect(sent[0][1], floodScopeSubSetKey); + expect(sent[0].sublist(2), _okiKey); + // the message + expect(sent[1], frame); + // clear scope: [54, 0] + expect(sent[2], [cmdSetFloodScopeKey, floodScopeSubSetKey]); + }); + + test( + 'a scoped channel on unsupported firmware is blocked, nothing sent', + () async { + await connector.channelRegionScopeStoreForTest.setScope(0, 'oki'); + connector.setFirmwareVerCodeForTest(7); + final sent = []; + connector.sendFrameOverrideForTest = sent.add; + await expectLater( + connector.sendChannelFrameScopedForTest(0, frame), + throwsStateError, + ); + expect(sent, isEmpty); + }, + ); +}