mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-13 09:20:29 +00:00
Add CompassSarList widget and refactor DetailedCompassDialog
- Introduced CompassSarList widget to display filtered SAR markers with distance and bearing information. - Refactored DetailedCompassDialog to integrate CompassSarList and CompassContactList for better organization. - Removed the previous filter dialog implementation and replaced it with a more modular CompassFilters widget. - Simplified the handling of zoom and scale updates in the compass view. - Cleaned up unused code related to previous implementations of contact and SAR marker lists.
This commit is contained in:
131
lib/services/ble/ble_command_sender.dart
Normal file
131
lib/services/ble/ble_command_sender.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import '../meshcore_opcode_names.dart';
|
||||
import '../../models/ble_packet_log.dart';
|
||||
|
||||
/// Callback types for sender events
|
||||
typedef OnErrorCallback = void Function(String error);
|
||||
|
||||
/// Sends commands to the BLE device
|
||||
class BleCommandSender {
|
||||
BluetoothCharacteristic? _rxCharacteristic;
|
||||
int _txPacketCount = 0;
|
||||
final List<BlePacketLog> _packetLogs = [];
|
||||
static const int _maxLogSize = 1000;
|
||||
|
||||
// Callbacks
|
||||
OnErrorCallback? onError;
|
||||
VoidCallback? onTxActivity;
|
||||
|
||||
// Getters
|
||||
int get txPacketCount => _txPacketCount;
|
||||
List<BlePacketLog> get packetLogs => List.unmodifiable(_packetLogs);
|
||||
|
||||
/// Set the RX characteristic to write to
|
||||
void setRxCharacteristic(BluetoothCharacteristic? characteristic) {
|
||||
_rxCharacteristic = characteristic;
|
||||
}
|
||||
|
||||
/// Write data to RX characteristic
|
||||
Future<void> writeData(Uint8List data) async {
|
||||
if (_rxCharacteristic == null) {
|
||||
throw Exception('Not connected');
|
||||
}
|
||||
try {
|
||||
// Extract command code from first byte
|
||||
final commandCode = data.isNotEmpty ? data[0] : null;
|
||||
final opcodeName = commandCode != null
|
||||
? MeshCoreOpcodeNames.getCommandName(commandCode)
|
||||
: 'UNKNOWN';
|
||||
final opcodeHex = commandCode != null
|
||||
? '0x${commandCode.toRadixString(16).padLeft(2, '0').toUpperCase()}'
|
||||
: 'N/A';
|
||||
|
||||
print('📤 [TX] Sending command: $opcodeName ($opcodeHex)');
|
||||
print(' Data size: ${data.length} bytes');
|
||||
print(' Hex: ${data.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ')}');
|
||||
|
||||
// Check if the characteristic supports write without response
|
||||
final supportsWriteWithoutResponse = _rxCharacteristic!.properties.writeWithoutResponse;
|
||||
final supportsWrite = _rxCharacteristic!.properties.write;
|
||||
|
||||
if (supportsWriteWithoutResponse) {
|
||||
await _rxCharacteristic!.write(data, withoutResponse: true);
|
||||
} else if (supportsWrite) {
|
||||
await _rxCharacteristic!.write(data, withoutResponse: false);
|
||||
} else {
|
||||
throw Exception('Characteristic does not support write operations');
|
||||
}
|
||||
|
||||
// Log TX packet
|
||||
_logPacket(data, PacketDirection.tx, responseCode: commandCode);
|
||||
|
||||
// Increment TX packet counter and trigger activity indicator
|
||||
_txPacketCount++;
|
||||
onTxActivity?.call();
|
||||
|
||||
print('✅ [TX] Command sent successfully');
|
||||
} catch (e) {
|
||||
print('❌ [TX] Write error: $e');
|
||||
onError?.call('Write error: $e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a packet
|
||||
void _logPacket(Uint8List data, PacketDirection direction, {int? responseCode}) {
|
||||
// Add new packet
|
||||
_packetLogs.add(BlePacketLog(
|
||||
timestamp: DateTime.now(),
|
||||
rawData: data,
|
||||
direction: direction,
|
||||
responseCode: responseCode,
|
||||
description: _getPacketDescription(responseCode),
|
||||
));
|
||||
|
||||
// Limit log size to prevent memory issues
|
||||
if (_packetLogs.length > _maxLogSize) {
|
||||
_packetLogs.removeAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get human-readable description of packet
|
||||
String? _getPacketDescription(int? code) {
|
||||
// TX packets - command codes
|
||||
switch (code) {
|
||||
case 4: // cmdGetContacts
|
||||
return 'Get Contacts';
|
||||
case 2: // cmdSendTxtMsg
|
||||
return 'Send Text Message';
|
||||
case 3: // cmdSendChannelTxtMsg
|
||||
return 'Send Channel Message';
|
||||
case 39: // cmdSendTelemetryReq
|
||||
return 'Request Telemetry';
|
||||
case 22: // cmdDeviceQuery
|
||||
return 'Device Query';
|
||||
case 1: // cmdAppStart
|
||||
return 'App Start';
|
||||
case 27: // cmdSendStatusReq
|
||||
return 'Status Request';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset packet counter
|
||||
void resetCounter() {
|
||||
_txPacketCount = 0;
|
||||
}
|
||||
|
||||
/// Clear packet logs
|
||||
void clearPacketLogs() {
|
||||
_packetLogs.clear();
|
||||
}
|
||||
|
||||
/// Dispose resources
|
||||
void dispose() {
|
||||
_rxCharacteristic = null;
|
||||
_packetLogs.clear();
|
||||
}
|
||||
}
|
||||
176
lib/services/ble/ble_connection_manager.dart
Normal file
176
lib/services/ble/ble_connection_manager.dart
Normal file
@@ -0,0 +1,176 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import '../meshcore_constants.dart';
|
||||
|
||||
/// Callback types for connection events
|
||||
typedef OnConnectionStateCallback = void Function(bool isConnected);
|
||||
typedef OnErrorCallback = void Function(String error);
|
||||
|
||||
/// Manages BLE connection lifecycle
|
||||
class BleConnectionManager {
|
||||
BluetoothDevice? _device;
|
||||
BluetoothCharacteristic? _rxCharacteristic;
|
||||
BluetoothCharacteristic? _txCharacteristic;
|
||||
bool _isConnected = false;
|
||||
|
||||
// Callbacks
|
||||
OnConnectionStateCallback? onConnectionStateChanged;
|
||||
OnErrorCallback? onError;
|
||||
|
||||
// Getters
|
||||
bool get isConnected => _isConnected;
|
||||
BluetoothDevice? get device => _device;
|
||||
BluetoothCharacteristic? get rxCharacteristic => _rxCharacteristic;
|
||||
BluetoothCharacteristic? get txCharacteristic => _txCharacteristic;
|
||||
|
||||
/// Scan for MeshCore devices
|
||||
Stream<BluetoothDevice> scanForDevices({Duration timeout = const Duration(seconds: 10)}) async* {
|
||||
try {
|
||||
print('🔍 [BLE] Starting scan for MeshCore devices...');
|
||||
print(' Service UUID: ${MeshCoreConstants.bleServiceUuid}');
|
||||
print(' Timeout: ${timeout.inSeconds}s');
|
||||
|
||||
await FlutterBluePlus.startScan(
|
||||
timeout: timeout,
|
||||
withServices: [Guid(MeshCoreConstants.bleServiceUuid)],
|
||||
);
|
||||
print('✅ [BLE] Scan started successfully');
|
||||
|
||||
int deviceCount = 0;
|
||||
await for (final scanResult in FlutterBluePlus.scanResults) {
|
||||
print('📡 [BLE] Scan results batch received: ${scanResult.length} results');
|
||||
for (final result in scanResult) {
|
||||
print(' Device: ${result.device.platformName} (${result.device.remoteId})');
|
||||
print(' RSSI: ${result.rssi}');
|
||||
print(' Service UUIDs: ${result.advertisementData.serviceUuids}');
|
||||
|
||||
if (result.advertisementData.serviceUuids
|
||||
.contains(Guid(MeshCoreConstants.bleServiceUuid))) {
|
||||
deviceCount++;
|
||||
print(' ✅ MeshCore device found! Total: $deviceCount');
|
||||
yield result.device;
|
||||
} else {
|
||||
print(' ❌ Not a MeshCore device (service UUID mismatch)');
|
||||
}
|
||||
}
|
||||
}
|
||||
print('🏁 [BLE] Scan completed. Found $deviceCount MeshCore devices');
|
||||
} catch (e) {
|
||||
print('❌ [BLE] Scan error: $e');
|
||||
onError?.call('Scan error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Connect to a MeshCore device
|
||||
Future<bool> connect(BluetoothDevice device) async {
|
||||
try {
|
||||
print('🔵 [BLE] Starting connection to device: ${device.platformName} (${device.remoteId})');
|
||||
_device = device;
|
||||
|
||||
// Connect to device
|
||||
print('🔵 [BLE] Calling device.connect() with 15s timeout...');
|
||||
await device.connect(
|
||||
license: License.free,
|
||||
timeout: const Duration(seconds: 15),
|
||||
mtu: 512,
|
||||
);
|
||||
print('✅ [BLE] Device connected successfully');
|
||||
|
||||
// Discover services
|
||||
print('🔵 [BLE] Discovering services...');
|
||||
final services = await device.discoverServices();
|
||||
print('✅ [BLE] Found ${services.length} services');
|
||||
|
||||
// Log all discovered services for debugging
|
||||
for (final service in services) {
|
||||
print(' 📋 Service: ${service.uuid}');
|
||||
for (final char in service.characteristics) {
|
||||
print(' - Characteristic: ${char.uuid}');
|
||||
}
|
||||
}
|
||||
|
||||
// Find MeshCore service
|
||||
print('🔵 [BLE] Looking for MeshCore service: ${MeshCoreConstants.bleServiceUuid}');
|
||||
BluetoothService? meshCoreService;
|
||||
for (final service in services) {
|
||||
if (service.uuid.toString().toLowerCase() ==
|
||||
MeshCoreConstants.bleServiceUuid.toLowerCase()) {
|
||||
meshCoreService = service;
|
||||
print('✅ [BLE] Found MeshCore service');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (meshCoreService == null) {
|
||||
print('❌ [BLE] MeshCore service not found!');
|
||||
throw Exception('MeshCore service not found');
|
||||
}
|
||||
|
||||
// Find RX and TX characteristics
|
||||
print('🔵 [BLE] Looking for RX and TX characteristics...');
|
||||
print(' RX UUID: ${MeshCoreConstants.bleCharacteristicRxUuid}');
|
||||
print(' TX UUID: ${MeshCoreConstants.bleCharacteristicTxUuid}');
|
||||
|
||||
for (final characteristic in meshCoreService.characteristics) {
|
||||
final uuid = characteristic.uuid.toString().toLowerCase();
|
||||
print(' 📋 Checking characteristic: $uuid');
|
||||
|
||||
if (uuid == MeshCoreConstants.bleCharacteristicRxUuid.toLowerCase()) {
|
||||
_rxCharacteristic = characteristic;
|
||||
print(' ✅ Found RX characteristic');
|
||||
} else if (uuid ==
|
||||
MeshCoreConstants.bleCharacteristicTxUuid.toLowerCase()) {
|
||||
_txCharacteristic = characteristic;
|
||||
print(' ✅ Found TX characteristic');
|
||||
}
|
||||
}
|
||||
|
||||
if (_rxCharacteristic == null || _txCharacteristic == null) {
|
||||
print('❌ [BLE] Required characteristics not found!');
|
||||
print(' RX found: ${_rxCharacteristic != null}');
|
||||
print(' TX found: ${_txCharacteristic != null}');
|
||||
throw Exception('Required characteristics not found');
|
||||
}
|
||||
|
||||
// Enable notifications on TX characteristic
|
||||
print('🔵 [BLE] Enabling notifications on TX characteristic...');
|
||||
await _txCharacteristic!.setNotifyValue(true);
|
||||
print('✅ [BLE] Notifications enabled');
|
||||
|
||||
_isConnected = true;
|
||||
print('🔵 [BLE] Notifying connection state change: connected');
|
||||
onConnectionStateChanged?.call(true);
|
||||
|
||||
print('✅✅✅ [BLE] Connection completed successfully!');
|
||||
return true;
|
||||
} catch (e) {
|
||||
print('❌❌❌ [BLE] Connection failed: $e');
|
||||
print('Stack trace: ${StackTrace.current}');
|
||||
onError?.call('Connection error: $e');
|
||||
_isConnected = false;
|
||||
onConnectionStateChanged?.call(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Disconnect from device
|
||||
Future<void> disconnect() async {
|
||||
try {
|
||||
await _device?.disconnect();
|
||||
_isConnected = false;
|
||||
_device = null;
|
||||
_rxCharacteristic = null;
|
||||
_txCharacteristic = null;
|
||||
onConnectionStateChanged?.call(false);
|
||||
} catch (e) {
|
||||
onError?.call('Disconnect error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Dispose resources
|
||||
void dispose() {
|
||||
_device = null;
|
||||
_rxCharacteristic = null;
|
||||
_txCharacteristic = null;
|
||||
}
|
||||
}
|
||||
656
lib/services/ble/ble_response_handler.dart
Normal file
656
lib/services/ble/ble_response_handler.dart
Normal file
@@ -0,0 +1,656 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import '../../models/contact.dart';
|
||||
import '../../models/message.dart';
|
||||
import '../../models/ble_packet_log.dart';
|
||||
import '../buffer_reader.dart';
|
||||
import '../meshcore_constants.dart';
|
||||
import '../meshcore_opcode_names.dart';
|
||||
import '../protocol/frame_parser.dart';
|
||||
|
||||
/// Callback types for response events
|
||||
typedef OnContactCallback = void Function(Contact contact);
|
||||
typedef OnContactsCompleteCallback = void Function(List<Contact> contacts);
|
||||
typedef OnMessageCallback = void Function(Message message);
|
||||
typedef OnTelemetryCallback = void Function(Uint8List publicKey, Uint8List lppData);
|
||||
typedef OnSelfInfoCallback = void Function(Map<String, dynamic> selfInfo);
|
||||
typedef OnDeviceInfoCallback = void Function(Map<String, dynamic> deviceInfo);
|
||||
typedef OnNoMoreMessagesCallback = void Function();
|
||||
typedef OnMessageWaitingCallback = void Function();
|
||||
typedef OnLoginSuccessCallback = void Function(Uint8List publicKeyPrefix, int permissions, bool isAdmin, int tag);
|
||||
typedef OnLoginFailCallback = void Function(Uint8List publicKeyPrefix);
|
||||
typedef OnAdvertReceivedCallback = void Function(Uint8List publicKey);
|
||||
typedef OnPathUpdatedCallback = void Function(Uint8List publicKey);
|
||||
typedef OnMessageSentCallback = void Function(int expectedAckTag, int suggestedTimeoutMs, bool isFloodMode);
|
||||
typedef OnMessageDeliveredCallback = void Function(int ackCode, int roundTripTimeMs);
|
||||
typedef OnStatusResponseCallback = void Function(Uint8List publicKeyPrefix, Uint8List statusData);
|
||||
typedef OnBinaryResponseCallback = void Function(Uint8List publicKeyPrefix, int tag, Uint8List responseData);
|
||||
typedef OnBatteryAndStorageCallback = void Function(int millivolts, int? usedKb, int? totalKb);
|
||||
typedef OnErrorCallback = void Function(String error);
|
||||
|
||||
/// Processes incoming responses from the BLE device
|
||||
class BleResponseHandler {
|
||||
StreamSubscription? _txSubscription;
|
||||
final List<Contact> _pendingContacts = [];
|
||||
int _rxPacketCount = 0;
|
||||
final List<BlePacketLog> _packetLogs = [];
|
||||
static const int _maxLogSize = 1000;
|
||||
|
||||
// Callbacks
|
||||
OnContactCallback? onContactReceived;
|
||||
OnContactsCompleteCallback? onContactsComplete;
|
||||
OnMessageCallback? onMessageReceived;
|
||||
OnTelemetryCallback? onTelemetryReceived;
|
||||
OnSelfInfoCallback? onSelfInfoReceived;
|
||||
OnDeviceInfoCallback? onDeviceInfoReceived;
|
||||
OnNoMoreMessagesCallback? onNoMoreMessages;
|
||||
OnMessageWaitingCallback? onMessageWaiting;
|
||||
OnLoginSuccessCallback? onLoginSuccess;
|
||||
OnLoginFailCallback? onLoginFail;
|
||||
OnAdvertReceivedCallback? onAdvertReceived;
|
||||
OnPathUpdatedCallback? onPathUpdated;
|
||||
OnMessageSentCallback? onMessageSent;
|
||||
OnMessageDeliveredCallback? onMessageDelivered;
|
||||
OnStatusResponseCallback? onStatusResponse;
|
||||
OnBinaryResponseCallback? onBinaryResponse;
|
||||
OnBatteryAndStorageCallback? onBatteryAndStorage;
|
||||
OnErrorCallback? onError;
|
||||
VoidCallback? onRxActivity;
|
||||
|
||||
// Getters
|
||||
int get rxPacketCount => _rxPacketCount;
|
||||
List<BlePacketLog> get packetLogs => List.unmodifiable(_packetLogs);
|
||||
|
||||
/// Subscribe to TX characteristic notifications
|
||||
void subscribeToNotifications(BluetoothCharacteristic txCharacteristic) {
|
||||
_txSubscription = txCharacteristic.lastValueStream.listen(
|
||||
_onDataReceived,
|
||||
onError: (error) {
|
||||
print('❌ [BLE] TX notification error: $error');
|
||||
onError?.call('TX notification error: $error');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Handle incoming data from TX characteristic
|
||||
void _onDataReceived(List<int> data) {
|
||||
try {
|
||||
// Handle empty data
|
||||
if (data.isEmpty) {
|
||||
print('⚠️ [RX] Empty data received, ignoring');
|
||||
return;
|
||||
}
|
||||
|
||||
final dataBytes = Uint8List.fromList(data);
|
||||
|
||||
// Increment RX packet counter and trigger activity indicator
|
||||
_rxPacketCount++;
|
||||
onRxActivity?.call();
|
||||
|
||||
final reader = BufferReader(dataBytes);
|
||||
final responseCode = reader.readByte();
|
||||
|
||||
// Get opcode name for logging
|
||||
final opcodeName = MeshCoreOpcodeNames.getOpcodeName(responseCode, isTx: false);
|
||||
final opcodeHex = '0x${responseCode.toRadixString(16).padLeft(2, '0').toUpperCase()}';
|
||||
|
||||
print('📥 [RX] Received: $opcodeName ($opcodeHex)');
|
||||
print(' Data size: ${data.length} bytes');
|
||||
print(' Hex: ${data.map((b) => b.toRadixString(16).padLeft(2, '0')).join(' ')}');
|
||||
print(' Payload: ${reader.remainingBytesCount} bytes');
|
||||
|
||||
// Log RX packet (before processing so we capture everything)
|
||||
_logPacket(dataBytes, PacketDirection.rx, responseCode: responseCode);
|
||||
|
||||
switch (responseCode) {
|
||||
case MeshCoreConstants.respContactsStart:
|
||||
print(' → Handling ContactsStart');
|
||||
_handleContactsStart(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respContact:
|
||||
print(' → Handling Contact');
|
||||
_handleContact(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respEndOfContacts:
|
||||
print(' → Handling EndOfContacts');
|
||||
_handleEndOfContacts(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respSent:
|
||||
print(' → Handling Sent confirmation');
|
||||
_handleSentConfirmation(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respContactMsgRecv:
|
||||
print(' → Handling ContactMessage');
|
||||
_handleContactMessage(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respChannelMsgRecv:
|
||||
print(' → Handling ChannelMessage');
|
||||
_handleChannelMessage(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushTelemetryResponse:
|
||||
print(' → Handling TelemetryResponse');
|
||||
_handleTelemetryResponse(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushBinaryResponse:
|
||||
print(' → Handling BinaryResponse');
|
||||
_handleBinaryResponse(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respDeviceInfo:
|
||||
print(' → Handling DeviceInfo');
|
||||
_handleDeviceInfo(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respSelfInfo:
|
||||
print(' → Handling SelfInfo');
|
||||
_handleSelfInfo(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushAdvert:
|
||||
print(' → Handling Advert push');
|
||||
_handleAdvert(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushPathUpdated:
|
||||
print(' → Handling PathUpdated push');
|
||||
_handlePathUpdated(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushLogRxData:
|
||||
print(' → Handling LogRxData push');
|
||||
_handleLogRxData(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushNewAdvert:
|
||||
print(' → Handling NewAdvert push');
|
||||
_handleNewAdvert(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushSendConfirmed:
|
||||
print(' → Handling SendConfirmed push');
|
||||
_handleSendConfirmed(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushMsgWaiting:
|
||||
print(' → Handling MsgWaiting push');
|
||||
_handleMsgWaiting(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushLoginSuccess:
|
||||
print(' → Handling LoginSuccess push');
|
||||
_handleLoginSuccess(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushLoginFail:
|
||||
print(' → Handling LoginFail push');
|
||||
_handleLoginFail(reader);
|
||||
break;
|
||||
case MeshCoreConstants.pushStatusResponse:
|
||||
print(' → Handling StatusResponse push');
|
||||
_handleStatusResponse(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respCurrTime:
|
||||
print(' → Handling CurrentTime');
|
||||
_handleCurrentTime(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respBatteryVoltage:
|
||||
print(' → Handling BatteryAndStorage');
|
||||
_handleBatteryAndStorage(reader);
|
||||
break;
|
||||
case MeshCoreConstants.respNoMoreMessages:
|
||||
print(' → Response: No More Messages');
|
||||
onNoMoreMessages?.call();
|
||||
break;
|
||||
case MeshCoreConstants.respOk:
|
||||
print(' → Response: OK');
|
||||
break;
|
||||
case MeshCoreConstants.respErr:
|
||||
print(' → Response: ERROR');
|
||||
_handleError(reader);
|
||||
break;
|
||||
default:
|
||||
print(' ⚠️ Unknown response code: $responseCode');
|
||||
break;
|
||||
}
|
||||
print('✅ [BLE] Data parsed successfully');
|
||||
} catch (e, stackTrace) {
|
||||
print('❌ [BLE] Data parsing error: $e');
|
||||
print(' Stack trace: $stackTrace');
|
||||
onError?.call('Data parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle ContactsStart response
|
||||
void _handleContactsStart(BufferReader reader) {
|
||||
_pendingContacts.clear();
|
||||
FrameParser.parseContactsStart(reader);
|
||||
}
|
||||
|
||||
/// Handle Contact response
|
||||
void _handleContact(BufferReader reader) {
|
||||
try {
|
||||
final contact = FrameParser.parseContact(reader);
|
||||
print(' ✅ [Contact] Parsed successfully');
|
||||
_pendingContacts.add(contact);
|
||||
onContactReceived?.call(contact);
|
||||
} catch (e) {
|
||||
print(' ❌ [Contact] Parsing error: $e');
|
||||
onError?.call('Contact parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle EndOfContacts response
|
||||
void _handleEndOfContacts(BufferReader reader) {
|
||||
onContactsComplete?.call(List.from(_pendingContacts));
|
||||
_pendingContacts.clear();
|
||||
}
|
||||
|
||||
/// Handle Sent confirmation response
|
||||
void _handleSentConfirmation(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseSentConfirmation(reader);
|
||||
if (result.isNotEmpty) {
|
||||
print(' ✅ [Sent] Message sent successfully');
|
||||
onMessageSent?.call(
|
||||
result['expectedAckTag'] as int,
|
||||
result['suggestedTimeout'] as int,
|
||||
result['isFloodMode'] as bool,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [Sent] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle ContactMessage response
|
||||
void _handleContactMessage(BufferReader reader) {
|
||||
try {
|
||||
final message = FrameParser.parseContactMessage(reader);
|
||||
print(' ✅ [ContactMessage] Parsed successfully');
|
||||
onMessageReceived?.call(message);
|
||||
} catch (e) {
|
||||
print(' ❌ [ContactMessage] Parsing error: $e');
|
||||
onError?.call('Contact message parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle ChannelMessage response
|
||||
void _handleChannelMessage(BufferReader reader) {
|
||||
try {
|
||||
final message = FrameParser.parseChannelMessage(reader);
|
||||
print(' ✅ [ChannelMessage] Parsed successfully');
|
||||
onMessageReceived?.call(message);
|
||||
} catch (e) {
|
||||
print(' ❌ [ChannelMessage] Parsing error: $e');
|
||||
onError?.call('Channel message parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle TelemetryResponse push
|
||||
void _handleTelemetryResponse(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseTelemetryResponse(reader);
|
||||
print(' ✅ [Telemetry] Parsed successfully');
|
||||
onTelemetryReceived?.call(
|
||||
result['publicKeyPrefix'] as Uint8List,
|
||||
result['lppSensorData'] as Uint8List,
|
||||
);
|
||||
} catch (e) {
|
||||
print(' ❌ [Telemetry] Parsing error: $e');
|
||||
onError?.call('Telemetry parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle BinaryResponse push
|
||||
void _handleBinaryResponse(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseBinaryResponse(reader);
|
||||
print(' ✅ [BinaryResponse] Parsed successfully');
|
||||
onBinaryResponse?.call(
|
||||
result['publicKeyPrefix'] as Uint8List,
|
||||
result['tag'] as int,
|
||||
result['responseData'] as Uint8List,
|
||||
);
|
||||
} catch (e) {
|
||||
print(' ❌ [BinaryResponse] Parsing error: $e');
|
||||
onError?.call('Binary response parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle DeviceInfo response
|
||||
void _handleDeviceInfo(BufferReader reader) {
|
||||
try {
|
||||
final info = FrameParser.parseDeviceInfo(reader);
|
||||
onDeviceInfoReceived?.call(info);
|
||||
print(' ✅ [DeviceInfo] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [DeviceInfo] Parsing error: $e');
|
||||
onError?.call('DeviceInfo parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle SelfInfo response
|
||||
void _handleSelfInfo(BufferReader reader) {
|
||||
try {
|
||||
final info = FrameParser.parseSelfInfo(reader);
|
||||
if (info.isNotEmpty) {
|
||||
onSelfInfoReceived?.call(info);
|
||||
}
|
||||
print(' ✅ [SelfInfo] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [SelfInfo] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle Advert push
|
||||
void _handleAdvert(BufferReader reader) {
|
||||
try {
|
||||
final publicKey = FrameParser.parseAdvert(reader);
|
||||
if (publicKey != null) {
|
||||
onAdvertReceived?.call(publicKey);
|
||||
}
|
||||
print(' ✅ [Advert] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [Advert] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle PathUpdated push
|
||||
void _handlePathUpdated(BufferReader reader) {
|
||||
try {
|
||||
final publicKey = FrameParser.parsePathUpdated(reader);
|
||||
if (publicKey != null) {
|
||||
onPathUpdated?.call(publicKey);
|
||||
}
|
||||
print(' ✅ [PathUpdated] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [PathUpdated] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle LogRxData push - includes extensive decoding logic
|
||||
void _handleLogRxData(BufferReader reader) {
|
||||
try {
|
||||
print(' [LogRxData] Parsing log rx data from over-the-air packet...');
|
||||
final data = reader.readRemainingBytes();
|
||||
|
||||
if (data.length < 2) {
|
||||
print(' ⚠️ [LogRxData] Insufficient data');
|
||||
return;
|
||||
}
|
||||
|
||||
final snrRaw = data[0];
|
||||
final snrDb = (snrRaw.toSigned(8)) / 4.0;
|
||||
print(' SNR: ${snrDb.toStringAsFixed(2)} dB');
|
||||
|
||||
final rssiDbm = data[1].toSigned(8);
|
||||
print(' RSSI: $rssiDbm dBm');
|
||||
|
||||
if (data.length <= 2) {
|
||||
print(' ⚠️ [LogRxData] No raw packet data');
|
||||
return;
|
||||
}
|
||||
|
||||
final rawPacketData = data.sublist(2);
|
||||
print(' Raw packet data: ${rawPacketData.length} bytes');
|
||||
|
||||
// Calculate entropy
|
||||
final uniqueBytes = rawPacketData.toSet().length;
|
||||
final entropy = uniqueBytes / rawPacketData.length;
|
||||
final isLikelyEncrypted = entropy > 0.7;
|
||||
|
||||
// Create decoded info for packet log
|
||||
final logRxDataInfo = LogRxDataInfo(
|
||||
entropy: entropy,
|
||||
isLikelyEncrypted: isLikelyEncrypted,
|
||||
);
|
||||
|
||||
// Update the most recent packet log entry
|
||||
if (_packetLogs.isNotEmpty) {
|
||||
final lastLog = _packetLogs.last;
|
||||
if (lastLog.responseCode == MeshCoreConstants.pushLogRxData) {
|
||||
_packetLogs[_packetLogs.length - 1] = BlePacketLog(
|
||||
timestamp: lastLog.timestamp,
|
||||
rawData: lastLog.rawData,
|
||||
direction: lastLog.direction,
|
||||
responseCode: lastLog.responseCode,
|
||||
description: lastLog.description,
|
||||
logRxDataInfo: logRxDataInfo,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
print(' ✅ [LogRxData] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [LogRxData] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle NewAdvert push
|
||||
void _handleNewAdvert(BufferReader reader) {
|
||||
try {
|
||||
final contact = FrameParser.parseContact(reader);
|
||||
print(' ✅ [NewAdvert] Parsed successfully');
|
||||
onContactReceived?.call(contact);
|
||||
} catch (e) {
|
||||
print(' ❌ [NewAdvert] Parsing error: $e');
|
||||
onError?.call('NewAdvert parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle SendConfirmed push
|
||||
void _handleSendConfirmed(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseSendConfirmed(reader);
|
||||
if (result.isNotEmpty) {
|
||||
print(' ✅ [SendConfirmed] Message delivery confirmed');
|
||||
onMessageDelivered?.call(
|
||||
result['ackCode'] as int,
|
||||
result['roundTripTime'] as int,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [SendConfirmed] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle MsgWaiting push
|
||||
void _handleMsgWaiting(BufferReader reader) {
|
||||
try {
|
||||
print(' [MsgWaiting] New message(s) waiting in queue');
|
||||
onMessageWaiting?.call();
|
||||
} catch (e) {
|
||||
print(' ❌ [MsgWaiting] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle LoginSuccess push
|
||||
void _handleLoginSuccess(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseLoginSuccess(reader);
|
||||
if (result.isNotEmpty) {
|
||||
print(' ✅ [LoginSuccess] Successfully logged into room');
|
||||
onLoginSuccess?.call(
|
||||
result['publicKeyPrefix'] as Uint8List,
|
||||
result['permissions'] as int,
|
||||
result['isAdmin'] as bool,
|
||||
result['tag'] as int,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [LoginSuccess] Parsing error: $e');
|
||||
onError?.call('Login success parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle LoginFail push
|
||||
void _handleLoginFail(BufferReader reader) {
|
||||
try {
|
||||
final publicKeyPrefix = FrameParser.parseLoginFail(reader);
|
||||
if (publicKeyPrefix != null) {
|
||||
print(' ❌ [LoginFail] Failed to login to room');
|
||||
onLoginFail?.call(publicKeyPrefix);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [LoginFail] Parsing error: $e');
|
||||
onError?.call('Login fail parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle StatusResponse push
|
||||
void _handleStatusResponse(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseStatusResponse(reader);
|
||||
if (result.isNotEmpty) {
|
||||
// Try to decode as ASCII text if printable
|
||||
try {
|
||||
final statusData = result['statusData'] as Uint8List;
|
||||
final statusText = utf8.decode(statusData, allowMalformed: true);
|
||||
if (statusText.isNotEmpty && _isPrintableAscii(statusText)) {
|
||||
print(' Status data (text): $statusText');
|
||||
}
|
||||
} catch (e) {
|
||||
// Not text data
|
||||
}
|
||||
|
||||
print(' ✅ [StatusResponse] Received status response');
|
||||
onStatusResponse?.call(
|
||||
result['publicKeyPrefix'] as Uint8List,
|
||||
result['statusData'] as Uint8List,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [StatusResponse] Parsing error: $e');
|
||||
onError?.call('Status response parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a string contains only printable ASCII characters
|
||||
bool _isPrintableAscii(String text) {
|
||||
for (int i = 0; i < text.length; i++) {
|
||||
final code = text.codeUnitAt(i);
|
||||
if (code < 32 || code > 126) {
|
||||
if (code != 10 && code != 13 && code != 9) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Handle CurrentTime response
|
||||
void _handleCurrentTime(BufferReader reader) {
|
||||
try {
|
||||
final deviceTime = FrameParser.parseCurrentTime(reader);
|
||||
if (deviceTime != null) {
|
||||
final appTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
final drift = appTime - deviceTime;
|
||||
print(' Clock drift: $drift seconds');
|
||||
}
|
||||
print(' ✅ [CurrentTime] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [CurrentTime] Parsing error: $e');
|
||||
onError?.call('CurrentTime parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle BatteryAndStorage response
|
||||
void _handleBatteryAndStorage(BufferReader reader) {
|
||||
try {
|
||||
final result = FrameParser.parseBatteryAndStorage(reader);
|
||||
if (result.isNotEmpty) {
|
||||
onBatteryAndStorage?.call(
|
||||
result['millivolts'] as int,
|
||||
result['usedKb'] as int?,
|
||||
result['totalKb'] as int?,
|
||||
);
|
||||
}
|
||||
print(' ✅ [BatteryAndStorage] Parsed successfully');
|
||||
} catch (e) {
|
||||
print(' ❌ [BatteryAndStorage] Parsing error: $e');
|
||||
onError?.call('BatteryAndStorage parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle Error response
|
||||
void _handleError(BufferReader reader) {
|
||||
try {
|
||||
final errorCode = FrameParser.parseError(reader);
|
||||
if (errorCode != null) {
|
||||
final errorMsg = FrameParser.getErrorMessage(errorCode);
|
||||
print(' ❌ [Error] $errorMsg');
|
||||
onError?.call(errorMsg);
|
||||
}
|
||||
} catch (e) {
|
||||
print(' ❌ [Error] Parsing error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Log a packet
|
||||
void _logPacket(Uint8List data, PacketDirection direction, {int? responseCode}) {
|
||||
_packetLogs.add(BlePacketLog(
|
||||
timestamp: DateTime.now(),
|
||||
rawData: data,
|
||||
direction: direction,
|
||||
responseCode: responseCode,
|
||||
description: _getPacketDescription(responseCode),
|
||||
));
|
||||
|
||||
if (_packetLogs.length > _maxLogSize) {
|
||||
_packetLogs.removeAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get human-readable description of packet
|
||||
String? _getPacketDescription(int? code) {
|
||||
// RX packets - response codes
|
||||
switch (code) {
|
||||
case 2: // respContactsStart
|
||||
return 'Contacts Start';
|
||||
case 3: // respContact
|
||||
return 'Contact Info';
|
||||
case 4: // respEndOfContacts
|
||||
return 'End of Contacts';
|
||||
case 6: // respSent
|
||||
return 'Message Sent';
|
||||
case 7: // respContactMsgRecv
|
||||
return 'Contact Message';
|
||||
case 8: // respChannelMsgRecv
|
||||
return 'Channel Message';
|
||||
case 0x8B: // pushTelemetryResponse
|
||||
return 'Telemetry Data';
|
||||
case 13: // respDeviceInfo
|
||||
return 'Device Info';
|
||||
case 5: // respSelfInfo
|
||||
return 'Self Info';
|
||||
case 0x80: // pushAdvert
|
||||
return 'Advertisement';
|
||||
case 0x81: // pushPathUpdated
|
||||
return 'Path Updated';
|
||||
case 0x88: // pushLogRxData
|
||||
return 'Log RX Data';
|
||||
case 0x8A: // pushNewAdvert
|
||||
return 'New Advertisement';
|
||||
case 0x87: // pushStatusResponse
|
||||
return 'Status Response';
|
||||
case 10: // respNoMoreMessages
|
||||
return 'No More Messages';
|
||||
case 0: // respOk
|
||||
return 'OK';
|
||||
case 1: // respErr
|
||||
return 'ERROR';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset packet counter
|
||||
void resetCounter() {
|
||||
_rxPacketCount = 0;
|
||||
}
|
||||
|
||||
/// Clear packet logs
|
||||
void clearPacketLogs() {
|
||||
_packetLogs.clear();
|
||||
}
|
||||
|
||||
/// Dispose resources
|
||||
Future<void> dispose() async {
|
||||
await _txSubscription?.cancel();
|
||||
_pendingContacts.clear();
|
||||
_packetLogs.clear();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
238
lib/services/protocol/frame_builder.dart
Normal file
238
lib/services/protocol/frame_builder.dart
Normal file
@@ -0,0 +1,238 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import '../../models/contact.dart';
|
||||
import '../buffer_writer.dart';
|
||||
import '../meshcore_constants.dart';
|
||||
|
||||
/// Builds outgoing BLE frames for the MeshCore device
|
||||
class FrameBuilder {
|
||||
/// Build DeviceQuery command
|
||||
static Uint8List buildDeviceQuery() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdDeviceQuery);
|
||||
writer.writeByte(MeshCoreConstants.supportedCompanionProtocolVersion);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build AppStart command
|
||||
static Uint8List buildAppStart() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdAppStart);
|
||||
writer.writeByte(1); // appVer
|
||||
writer.writeBytes(Uint8List(6)); // reserved
|
||||
writer.writeString('MeshCore SAR'); // appName
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build GetContacts command
|
||||
static Uint8List buildGetContacts() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdGetContacts);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build AddUpdateContact command
|
||||
static Uint8List buildAddUpdateContact(Contact contact) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdAddUpdateContact); // 0x09
|
||||
writer.writeBytes(contact.publicKey); // 32 bytes
|
||||
writer.writeByte(contact.type.value); // ADV_TYPE_*
|
||||
writer.writeByte(contact.flags); // flags
|
||||
writer.writeInt8(contact.outPathLen); // path length (signed byte)
|
||||
writer.writeBytes(contact.outPath); // 64 bytes
|
||||
|
||||
// Write name as null-terminated string in 32-byte field
|
||||
final nameBytes = Uint8List(32);
|
||||
final encoded = utf8.encode(contact.advName);
|
||||
final copyLen = encoded.length > 31 ? 31 : encoded.length;
|
||||
nameBytes.setRange(0, copyLen, encoded);
|
||||
writer.writeBytes(nameBytes);
|
||||
|
||||
writer.writeUInt32LE(contact.lastAdvert); // timestamp
|
||||
writer.writeInt32LE(contact.advLat); // latitude * 1E6
|
||||
writer.writeInt32LE(contact.advLon); // longitude * 1E6
|
||||
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendTxtMsg command
|
||||
static Uint8List buildSendTxtMsg({
|
||||
required Uint8List contactPublicKey,
|
||||
required String text,
|
||||
int textType = 0,
|
||||
int attempt = 0,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendTxtMsg); // 0x02
|
||||
writer.writeByte(textType); // TXT_TYPE_*
|
||||
writer.writeByte(attempt); // 0-3
|
||||
writer.writeUInt32LE(DateTime.now().millisecondsSinceEpoch ~/ 1000);
|
||||
writer.writeBytes(contactPublicKey.sublist(0, 6));
|
||||
writer.writeString(text);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendChannelTxtMsg command
|
||||
static Uint8List buildSendChannelTxtMsg({
|
||||
required int channelIdx,
|
||||
required String text,
|
||||
int textType = 0,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendChannelTxtMsg); // 0x03
|
||||
writer.writeByte(textType); // TXT_TYPE_*
|
||||
writer.writeByte(channelIdx); // 0 for 'public' channel
|
||||
writer.writeUInt32LE(DateTime.now().millisecondsSinceEpoch ~/ 1000);
|
||||
writer.writeString(text);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendTelemetryReq command (deprecated)
|
||||
@Deprecated('Use buildSendBinaryReq() instead')
|
||||
static Uint8List buildSendTelemetryReq(Uint8List contactPublicKey, {bool zeroHop = false}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendTelemetryReq);
|
||||
writer.writeByte(zeroHop ? 0 : 255);
|
||||
writer.writeByte(0); // reserved
|
||||
writer.writeByte(0); // reserved
|
||||
writer.writeBytes(contactPublicKey);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendBinaryReq command
|
||||
static Uint8List buildSendBinaryReq({
|
||||
required Uint8List contactPublicKey,
|
||||
required Uint8List requestData,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendBinaryReq); // 0x32 (50)
|
||||
writer.writeBytes(contactPublicKey); // 32 bytes
|
||||
writer.writeBytes(requestData); // request code + params
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build GetBatteryVoltage command
|
||||
static Uint8List buildGetBatteryAndStorage() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdGetBatteryVoltage);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SyncNextMessage command
|
||||
static Uint8List buildSyncNextMessage() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSyncNextMessage);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build GetDeviceTime command
|
||||
static Uint8List buildGetDeviceTime() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdGetDeviceTime);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetDeviceTime command
|
||||
static Uint8List buildSetDeviceTime() {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetDeviceTime);
|
||||
writer.writeUInt32LE(DateTime.now().millisecondsSinceEpoch ~/ 1000);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendSelfAdvert command
|
||||
static Uint8List buildSendSelfAdvert({bool floodMode = true}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendSelfAdvert);
|
||||
writer.writeByte(floodMode ? MeshCoreConstants.selfAdvertFlood : MeshCoreConstants.selfAdvertZeroHop);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetAdvertName command
|
||||
static Uint8List buildSetAdvertName(String name) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetAdvertName);
|
||||
writer.writeString(name);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetAdvertLatLon command
|
||||
static Uint8List buildSetAdvertLatLon({
|
||||
required double latitude,
|
||||
required double longitude,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetAdvertLatLon);
|
||||
writer.writeInt32LE((latitude * 1000000).round());
|
||||
writer.writeInt32LE((longitude * 1000000).round());
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetRadioParams command
|
||||
static Uint8List buildSetRadioParams({
|
||||
required int frequency,
|
||||
required int bandwidth,
|
||||
required int spreadingFactor,
|
||||
required int codingRate,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetRadioParams);
|
||||
writer.writeUInt32LE(frequency);
|
||||
writer.writeUInt16LE(bandwidth);
|
||||
writer.writeByte(spreadingFactor);
|
||||
writer.writeByte(codingRate);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetTxPower command
|
||||
static Uint8List buildSetTxPower(int powerDbm) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetTxPower);
|
||||
writer.writeByte(powerDbm);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SetOtherParams command
|
||||
static Uint8List buildSetOtherParams({
|
||||
required int manualAddContacts,
|
||||
required int telemetryModes,
|
||||
required int advertLocationPolicy,
|
||||
int multiAcks = 0,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSetOtherParams);
|
||||
writer.writeByte(manualAddContacts);
|
||||
writer.writeByte(telemetryModes);
|
||||
writer.writeByte(advertLocationPolicy);
|
||||
writer.writeByte(multiAcks);
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendLogin command
|
||||
static Uint8List buildSendLogin({
|
||||
required Uint8List roomPublicKey,
|
||||
required String password,
|
||||
}) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendLogin); // 0x1A
|
||||
writer.writeBytes(roomPublicKey); // 32 bytes
|
||||
writer.writeString(password); // Max 15 bytes, null-terminated
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build SendStatusReq command
|
||||
static Uint8List buildSendStatusReq(Uint8List contactPublicKey) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdSendStatusReq); // 0x1B
|
||||
writer.writeBytes(contactPublicKey); // 32 bytes
|
||||
return writer.toBytes();
|
||||
}
|
||||
|
||||
/// Build ResetPath command - clears learned path for a contact
|
||||
static Uint8List buildResetPath(Uint8List contactPublicKey) {
|
||||
final writer = BufferWriter();
|
||||
writer.writeByte(MeshCoreConstants.cmdResetPath); // 0x0D (13)
|
||||
writer.writeBytes(contactPublicKey); // 32 bytes
|
||||
return writer.toBytes();
|
||||
}
|
||||
}
|
||||
398
lib/services/protocol/frame_parser.dart
Normal file
398
lib/services/protocol/frame_parser.dart
Normal file
@@ -0,0 +1,398 @@
|
||||
import 'dart:typed_data';
|
||||
import '../../models/contact.dart';
|
||||
import '../../models/message.dart';
|
||||
import '../buffer_reader.dart';
|
||||
import '../meshcore_constants.dart';
|
||||
|
||||
/// Parses incoming BLE frames from the MeshCore device
|
||||
class FrameParser {
|
||||
/// Parse ContactsStart response
|
||||
static int parseContactsStart(BufferReader reader) {
|
||||
return reader.readUInt32LE();
|
||||
}
|
||||
|
||||
/// Parse Contact response
|
||||
static Contact parseContact(BufferReader reader) {
|
||||
final publicKey = reader.readBytes(32);
|
||||
final typeByte = reader.readByte();
|
||||
final type = ContactType.fromValue(typeByte);
|
||||
final flags = reader.readByte();
|
||||
final outPathLen = reader.readInt8();
|
||||
final outPath = reader.readBytes(64);
|
||||
final advName = reader.readCString(32);
|
||||
final lastAdvert = reader.readUInt32LE();
|
||||
final advLat = reader.readInt32LE();
|
||||
final advLon = reader.readInt32LE();
|
||||
final lastMod = reader.readUInt32LE();
|
||||
|
||||
return Contact(
|
||||
publicKey: publicKey,
|
||||
type: type,
|
||||
flags: flags,
|
||||
outPathLen: outPathLen,
|
||||
outPath: outPath,
|
||||
advName: advName,
|
||||
lastAdvert: lastAdvert,
|
||||
advLat: advLat,
|
||||
advLon: advLon,
|
||||
lastMod: lastMod,
|
||||
);
|
||||
}
|
||||
|
||||
/// Parse Sent confirmation response
|
||||
static Map<String, dynamic> parseSentConfirmation(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 9) {
|
||||
final sendType = reader.readByte();
|
||||
final isFloodMode = sendType == 1;
|
||||
final expectedAckOrTagBytes = reader.readBytes(4);
|
||||
final expectedAckTag = ByteData.sublistView(Uint8List.fromList(expectedAckOrTagBytes))
|
||||
.getUint32(0, Endian.little);
|
||||
final suggestedTimeout = reader.readUInt32LE();
|
||||
|
||||
return {
|
||||
'expectedAckTag': expectedAckTag,
|
||||
'suggestedTimeout': suggestedTimeout,
|
||||
'isFloodMode': isFloodMode,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Parse ContactMessage response
|
||||
static Message parseContactMessage(BufferReader reader) {
|
||||
final pubKeyPrefix = reader.readBytes(6);
|
||||
final pathLen = reader.readByte();
|
||||
final txtTypeByte = reader.readByte();
|
||||
final txtType = MessageTextType.fromValue(txtTypeByte);
|
||||
final senderTimestamp = reader.readUInt32LE();
|
||||
|
||||
String text;
|
||||
if (txtType == MessageTextType.signedPlain) {
|
||||
// Signed message format: [4-byte sender prefix][UTF-8 text]
|
||||
if (reader.remainingBytesCount >= 4) {
|
||||
reader.readBytes(4); // Skip extra sender prefix
|
||||
text = reader.hasRemaining ? reader.readString() : '';
|
||||
} else {
|
||||
text = reader.readString();
|
||||
}
|
||||
} else {
|
||||
text = reader.readString();
|
||||
}
|
||||
|
||||
return Message(
|
||||
id: '${DateTime.now().millisecondsSinceEpoch}_${pubKeyPrefix.map((b) => b.toRadixString(16)).join()}',
|
||||
messageType: MessageType.contact,
|
||||
senderPublicKeyPrefix: pubKeyPrefix,
|
||||
pathLen: pathLen,
|
||||
textType: txtType,
|
||||
senderTimestamp: senderTimestamp,
|
||||
text: text,
|
||||
receivedAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Parse ChannelMessage response
|
||||
static Message parseChannelMessage(BufferReader reader) {
|
||||
final channelIdx = reader.readInt8();
|
||||
final pathLen = reader.readByte();
|
||||
final txtTypeByte = reader.readByte();
|
||||
final txtType = MessageTextType.fromValue(txtTypeByte);
|
||||
final senderTimestamp = reader.readUInt32LE();
|
||||
|
||||
String text;
|
||||
if (txtType == MessageTextType.signedPlain) {
|
||||
if (reader.remainingBytesCount >= 4) {
|
||||
reader.readBytes(4); // Skip extra sender prefix
|
||||
text = reader.hasRemaining ? reader.readString() : '';
|
||||
} else {
|
||||
text = reader.readString();
|
||||
}
|
||||
} else {
|
||||
text = reader.readString();
|
||||
}
|
||||
|
||||
return Message(
|
||||
id: '${DateTime.now().millisecondsSinceEpoch}_ch$channelIdx',
|
||||
messageType: MessageType.channel,
|
||||
channelIdx: channelIdx,
|
||||
pathLen: pathLen,
|
||||
textType: txtType,
|
||||
senderTimestamp: senderTimestamp,
|
||||
text: text,
|
||||
receivedAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Parse TelemetryResponse push
|
||||
static Map<String, dynamic> parseTelemetryResponse(BufferReader reader) {
|
||||
reader.readByte(); // reserved
|
||||
final pubKeyPrefix = reader.readBytes(6);
|
||||
final lppSensorData = reader.readRemainingBytes();
|
||||
|
||||
return {
|
||||
'publicKeyPrefix': pubKeyPrefix,
|
||||
'lppSensorData': lppSensorData,
|
||||
};
|
||||
}
|
||||
|
||||
/// Parse BinaryResponse push
|
||||
static Map<String, dynamic> parseBinaryResponse(BufferReader reader) {
|
||||
reader.readByte(); // reserved
|
||||
final tag = reader.readUInt32LE();
|
||||
final responseData = reader.readRemainingBytes();
|
||||
|
||||
return {
|
||||
'publicKeyPrefix': Uint8List(6), // Empty prefix
|
||||
'tag': tag,
|
||||
'responseData': responseData,
|
||||
};
|
||||
}
|
||||
|
||||
/// Parse DeviceInfo response
|
||||
static Map<String, dynamic> parseDeviceInfo(BufferReader reader) {
|
||||
if (reader.remainingBytesCount < 1) {
|
||||
return {};
|
||||
}
|
||||
|
||||
final firmwareVersion = reader.readByte();
|
||||
|
||||
int? maxContacts;
|
||||
int? maxChannels;
|
||||
int? blePin;
|
||||
if (reader.remainingBytesCount >= 6) {
|
||||
final maxContactsDiv2 = reader.readByte();
|
||||
maxContacts = maxContactsDiv2 * 2;
|
||||
maxChannels = reader.readByte();
|
||||
blePin = reader.readUInt32LE();
|
||||
}
|
||||
|
||||
String? firmwareBuildDate;
|
||||
if (reader.remainingBytesCount >= 12) {
|
||||
final buildDateBytes = reader.readBytes(12);
|
||||
firmwareBuildDate =
|
||||
String.fromCharCodes(buildDateBytes.takeWhile((b) => b != 0));
|
||||
}
|
||||
|
||||
String? manufacturerModel;
|
||||
if (reader.remainingBytesCount >= 40) {
|
||||
final modelBytes = reader.readBytes(40);
|
||||
manufacturerModel =
|
||||
String.fromCharCodes(modelBytes.takeWhile((b) => b != 0));
|
||||
}
|
||||
|
||||
String? semanticVersion;
|
||||
if (reader.remainingBytesCount >= 20) {
|
||||
final versionBytes = reader.readBytes(20);
|
||||
semanticVersion =
|
||||
String.fromCharCodes(versionBytes.takeWhile((b) => b != 0));
|
||||
}
|
||||
|
||||
return {
|
||||
'firmwareVersion': firmwareVersion,
|
||||
'maxContacts': maxContacts,
|
||||
'maxChannels': maxChannels,
|
||||
'blePin': blePin,
|
||||
'firmwareBuildDate': firmwareBuildDate,
|
||||
'manufacturerModel': manufacturerModel,
|
||||
'semanticVersion': semanticVersion,
|
||||
};
|
||||
}
|
||||
|
||||
/// Parse SelfInfo response
|
||||
static Map<String, dynamic> parseSelfInfo(BufferReader reader) {
|
||||
if (reader.remainingBytesCount < 54) {
|
||||
reader.readRemainingBytes();
|
||||
return {};
|
||||
}
|
||||
|
||||
final deviceType = reader.readByte();
|
||||
final txPower = reader.readByte();
|
||||
final maxTxPower = reader.readByte();
|
||||
final publicKey = reader.readBytes(32);
|
||||
|
||||
final advLatBytes = reader.readBytes(4);
|
||||
final advLat = ByteData.sublistView(Uint8List.fromList(advLatBytes))
|
||||
.getInt32(0, Endian.little);
|
||||
|
||||
final advLonBytes = reader.readBytes(4);
|
||||
final advLon = ByteData.sublistView(Uint8List.fromList(advLonBytes))
|
||||
.getInt32(0, Endian.little);
|
||||
|
||||
final multiAcks = reader.readByte();
|
||||
final advertLocPolicy = reader.readByte();
|
||||
final telemetryModes = reader.readByte();
|
||||
final manualAddContacts = reader.readByte();
|
||||
|
||||
final radioFreqBytes = reader.readBytes(4);
|
||||
final radioFreq = ByteData.sublistView(Uint8List.fromList(radioFreqBytes))
|
||||
.getUint32(0, Endian.little);
|
||||
|
||||
final radioBwBytes = reader.readBytes(4);
|
||||
final radioBw = ByteData.sublistView(Uint8List.fromList(radioBwBytes))
|
||||
.getUint32(0, Endian.little);
|
||||
|
||||
final radioSf = reader.readByte();
|
||||
final radioCr = reader.readByte();
|
||||
|
||||
String? selfName;
|
||||
if (reader.hasRemaining) {
|
||||
final nameBytes = reader.readRemainingBytes();
|
||||
selfName = String.fromCharCodes(nameBytes.takeWhile((b) => b != 0));
|
||||
}
|
||||
|
||||
return {
|
||||
'deviceType': deviceType,
|
||||
'txPower': txPower,
|
||||
'maxTxPower': maxTxPower,
|
||||
'publicKey': publicKey,
|
||||
'advLat': advLat,
|
||||
'advLon': advLon,
|
||||
'manualAddContacts': manualAddContacts == 1,
|
||||
'radioFreq': radioFreq,
|
||||
'radioBw': radioBw,
|
||||
'radioSf': radioSf,
|
||||
'radioCr': radioCr,
|
||||
'selfName': selfName,
|
||||
};
|
||||
}
|
||||
|
||||
/// Parse Advert push
|
||||
static Uint8List? parseAdvert(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 32) {
|
||||
return reader.readBytes(32);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Parse PathUpdated push
|
||||
static Uint8List? parsePathUpdated(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 32) {
|
||||
return reader.readBytes(32);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Parse SendConfirmed push
|
||||
static Map<String, dynamic> parseSendConfirmed(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 8) {
|
||||
final ackCodeBytes = reader.readBytes(4);
|
||||
final ackCode = ByteData.sublistView(Uint8List.fromList(ackCodeBytes))
|
||||
.getUint32(0, Endian.little);
|
||||
final roundTripTime = reader.readUInt32LE();
|
||||
|
||||
return {
|
||||
'ackCode': ackCode,
|
||||
'roundTripTime': roundTripTime,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Parse LoginSuccess push
|
||||
static Map<String, dynamic> parseLoginSuccess(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 11) {
|
||||
final permissions = reader.readByte();
|
||||
final isAdmin = (permissions & 0x01) != 0;
|
||||
final publicKeyPrefix = reader.readBytes(6);
|
||||
final tag = reader.readInt32LE();
|
||||
|
||||
int? newPermissions;
|
||||
if (reader.hasRemaining) {
|
||||
newPermissions = reader.readByte();
|
||||
}
|
||||
|
||||
return {
|
||||
'publicKeyPrefix': publicKeyPrefix,
|
||||
'permissions': permissions,
|
||||
'isAdmin': isAdmin,
|
||||
'tag': tag,
|
||||
'newPermissions': newPermissions,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Parse LoginFail push
|
||||
static Uint8List? parseLoginFail(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 7) {
|
||||
reader.readByte(); // reserved
|
||||
return reader.readBytes(6);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Parse StatusResponse push
|
||||
static Map<String, dynamic> parseStatusResponse(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 7) {
|
||||
reader.readByte(); // reserved
|
||||
final publicKeyPrefix = reader.readBytes(6);
|
||||
final statusData = reader.readRemainingBytes();
|
||||
|
||||
return {
|
||||
'publicKeyPrefix': publicKeyPrefix,
|
||||
'statusData': statusData,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Parse CurrentTime response
|
||||
static int? parseCurrentTime(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 4) {
|
||||
return reader.readUInt32LE();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Parse BatteryAndStorage response
|
||||
static Map<String, dynamic> parseBatteryAndStorage(BufferReader reader) {
|
||||
if (reader.remainingBytesCount >= 2) {
|
||||
final millivolts = reader.readUInt16LE();
|
||||
|
||||
int? usedKb;
|
||||
int? totalKb;
|
||||
|
||||
if (reader.remainingBytesCount >= 8) {
|
||||
usedKb = reader.readUInt32LE();
|
||||
totalKb = reader.readUInt32LE();
|
||||
} else if (reader.remainingBytesCount >= 4) {
|
||||
usedKb = reader.readUInt32LE();
|
||||
}
|
||||
|
||||
return {
|
||||
'millivolts': millivolts,
|
||||
'usedKb': usedKb,
|
||||
'totalKb': totalKb,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Parse Error response
|
||||
static int? parseError(BufferReader reader) {
|
||||
if (reader.hasRemaining) {
|
||||
return reader.readByte();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Get error message from error code
|
||||
static String getErrorMessage(int errorCode) {
|
||||
switch (errorCode) {
|
||||
case MeshCoreConstants.errUnsupportedCmd:
|
||||
return 'Unsupported command';
|
||||
case MeshCoreConstants.errNotFound:
|
||||
return 'Not found';
|
||||
case MeshCoreConstants.errTableFull:
|
||||
return 'Table full';
|
||||
case MeshCoreConstants.errBadState:
|
||||
return 'Bad state';
|
||||
case MeshCoreConstants.errFileIoError:
|
||||
return 'File I/O error';
|
||||
case MeshCoreConstants.errIllegalArg:
|
||||
return 'Illegal argument';
|
||||
default:
|
||||
return 'Error code: $errorCode';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user