mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
feat: Configure default public channel with well-known secret and enhance channel message handling
This commit is contained in:
@@ -315,6 +315,13 @@ class MeshCoreBleService {
|
||||
_responseHandler.trackSentMessage(messageId, null);
|
||||
}
|
||||
|
||||
/// Send a text message to a channel (flood-mode broadcast)
|
||||
///
|
||||
/// Channel messages are ephemeral and use flood routing (no ACKs).
|
||||
/// Use channel 0 for the default public channel.
|
||||
///
|
||||
/// Note: Uses fire-and-forget mode since channel messages don't return
|
||||
/// delivery confirmation (they're broadcast to all nodes).
|
||||
Future<void> sendChannelMessage({
|
||||
required int channelIdx,
|
||||
required String text,
|
||||
@@ -324,6 +331,8 @@ class MeshCoreBleService {
|
||||
throw ArgumentError('Channel message too long (max ~160 characters)');
|
||||
}
|
||||
|
||||
// Channel messages use fire-and-forget (no ACK expected)
|
||||
// The firmware responds with RESP_CODE_OK but we don't wait for it
|
||||
await _commandSender.writeData(FrameBuilder.buildSendChannelTxtMsg(
|
||||
channelIdx: channelIdx,
|
||||
text: text,
|
||||
@@ -483,20 +492,26 @@ class MeshCoreBleService {
|
||||
await _commandSender.writeData(FrameBuilder.buildGetChannel(channelIdx));
|
||||
}
|
||||
|
||||
/// Set the name for a specific channel
|
||||
/// Set the name and secret for a specific channel
|
||||
///
|
||||
/// The secret must be exactly 16 bytes (128-bit encryption key).
|
||||
/// For the default public channel (channel 0), use [MeshCoreConstants.defaultPublicChannelSecret].
|
||||
Future<void> setChannel({
|
||||
required int channelIdx,
|
||||
required String channelName,
|
||||
required List<int> secret,
|
||||
}) async {
|
||||
debugPrint('📻 [BLE] Setting channel name:');
|
||||
debugPrint('📻 [BLE] Setting channel:');
|
||||
debugPrint(' Channel index: $channelIdx');
|
||||
debugPrint(' Channel name: $channelName');
|
||||
debugPrint(' Secret length: ${secret.length} bytes');
|
||||
|
||||
await _commandSender.writeDataAndWaitForAck(FrameBuilder.buildSetChannel(
|
||||
channelIdx: channelIdx,
|
||||
channelName: channelName,
|
||||
secret: secret,
|
||||
));
|
||||
debugPrint('✅ [BLE] CMD_SET_CHANNEL sent');
|
||||
debugPrint('✅ [BLE] CMD_SET_CHANNEL sent successfully');
|
||||
}
|
||||
|
||||
/// Sync all channels from the device (typically 0-39)
|
||||
|
||||
@@ -117,6 +117,16 @@ class MeshCoreConstants {
|
||||
static const int binaryReqGetAccessList = 0x05;
|
||||
static const int binaryReqGetNeighbours = 0x06;
|
||||
|
||||
// Default Public Channel Secret (128-bit)
|
||||
// This is the well-known pre-shared key for the public channel (channel 0)
|
||||
// Hex: 8b3387e9c5cdea6ac9e5edbaa115cd72
|
||||
// Base64: izOH6cXN6mrJ5e26oRXNcg==
|
||||
// Source: https://github.com/meshcore-dev/MeshCore/blob/main/docs/faq.md
|
||||
static const List<int> defaultPublicChannelSecret = [
|
||||
0x8b, 0x33, 0x87, 0xe9, 0xc5, 0xcd, 0xea, 0x6a,
|
||||
0xc9, 0xe5, 0xed, 0xba, 0xa1, 0x15, 0xcd, 0x72,
|
||||
];
|
||||
|
||||
// Cayenne LPP Data Types
|
||||
static const int lppDigitalInput = 0;
|
||||
static const int lppDigitalOutput = 1;
|
||||
|
||||
@@ -252,11 +252,19 @@ class FrameBuilder {
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetChannel command - sets the name for a specific channel
|
||||
/// Build SetChannel command - sets the name and secret for a specific channel
|
||||
///
|
||||
/// Format: [cmd(1)][channel_idx(1)][name(32)][secret(16)]
|
||||
/// Secret must be exactly 16 bytes (128-bit key)
|
||||
static Uint8List buildSetChannel({
|
||||
required int channelIdx,
|
||||
required String channelName,
|
||||
required List<int> secret,
|
||||
}) {
|
||||
if (secret.length != 16) {
|
||||
throw ArgumentError('Channel secret must be exactly 16 bytes (got ${secret.length})');
|
||||
}
|
||||
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetChannel); // 0x20 (32)
|
||||
writer.writeByte(channelIdx); // 0-39 typically
|
||||
@@ -268,6 +276,9 @@ class FrameBuilder {
|
||||
nameBytes.setRange(0, copyLen, encoded);
|
||||
writer.writeBytes(nameBytes);
|
||||
|
||||
// Write 16-byte secret
|
||||
writer.writeBytes(Uint8List.fromList(secret));
|
||||
|
||||
return writer.toBytes();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user