2024-12-04 17:24:58 +08:00
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
2025-01-23 14:30:31 +08:00
|
|
|
|
import 'package:star_lock/talk/starChart/constant/message_type_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/constant/payload_type_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/constant/protocol_flag_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/constant/udp_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/entity/scp_message.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/ble_message.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/gateway_reset.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/generic.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/rbcu.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_accept.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_data.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_hangup.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_ping.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_reject.pb.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/starChart/proto/talk_request.pb.dart';
|
2024-11-28 14:57:49 +08:00
|
|
|
|
|
|
|
|
|
|
class MessageCommand {
|
2024-12-19 14:48:19 +08:00
|
|
|
|
// 全局字典,用于存储每个 ToPeerId 对应的当前 messageId
|
2024-12-24 15:38:36 +08:00
|
|
|
|
// 单个会话的messageId需要递增
|
2024-12-19 14:48:19 +08:00
|
|
|
|
static Map<String, int> _messageIdMap = {};
|
|
|
|
|
|
static int _maxIntValue = 9223372036854775807; // Dart 中 int 的最大值
|
|
|
|
|
|
|
|
|
|
|
|
// 获取并递增消息ID
|
2024-12-24 14:20:30 +08:00
|
|
|
|
static int getNextMessageId(String toPeerId, {bool increment = true}) {
|
2024-12-19 14:48:19 +08:00
|
|
|
|
if (_messageIdMap.containsKey(toPeerId)) {
|
|
|
|
|
|
if (increment) {
|
|
|
|
|
|
_messageIdMap[toPeerId] = _messageIdMap[toPeerId]! + 1;
|
|
|
|
|
|
// 如果 messageId 超过 int 的最大值,则重置为 1
|
|
|
|
|
|
if (_messageIdMap[toPeerId]! > _maxIntValue) {
|
|
|
|
|
|
_messageIdMap[toPeerId] = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
_messageIdMap[toPeerId] = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return _messageIdMap[toPeerId]!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
/// 客户端去中继上线命令
|
2024-12-02 15:43:59 +08:00
|
|
|
|
static List<int> goOnlineRelay({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-03 14:44:29 +08:00
|
|
|
|
}) {
|
2024-11-28 14:57:49 +08:00
|
|
|
|
String serializedBytesString = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-02 15:43:59 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-12-20 14:38:52 +08:00
|
|
|
|
PayloadCRC: 0,
|
|
|
|
|
|
PayloadLength: 0,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.goOnline,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
).serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 回声测试消息
|
|
|
|
|
|
static List<int> echoMessage({
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required String FromPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
List<int>? payload,
|
|
|
|
|
|
int? SpTotal,
|
|
|
|
|
|
int? SpIndex,
|
|
|
|
|
|
int? MessageId,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
2024-11-30 15:39:06 +08:00
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: SpTotal,
|
|
|
|
|
|
SpIndex: SpIndex,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-12-04 17:24:58 +08:00
|
|
|
|
Payload: payload,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
PayloadCRC: calculationCrcFromIntList(payload ?? []),
|
|
|
|
|
|
PayloadLength: payload?.length ?? 0,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.echoTest,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 心跳消息
|
2024-12-02 15:43:59 +08:00
|
|
|
|
static List<int> heartbeatMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-02 15:43:59 +08:00
|
|
|
|
}) {
|
2024-12-04 17:24:58 +08:00
|
|
|
|
String payload = 'hello';
|
2024-11-28 14:57:49 +08:00
|
|
|
|
ScpMessage message = ScpMessage(
|
2024-11-30 15:39:06 +08:00
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-02 15:43:59 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-12-04 17:24:58 +08:00
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(_stringToUint8List(payload)),
|
|
|
|
|
|
PayloadLength: payload.length,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.heartbeat,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-03 17:55:33 +08:00
|
|
|
|
// 网关初始化
|
2024-12-04 15:10:51 +08:00
|
|
|
|
static List<int> gatewayResetMessage({
|
2024-12-03 17:55:33 +08:00
|
|
|
|
required int gatewayId,
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required int time,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-03 17:55:33 +08:00
|
|
|
|
}) {
|
2024-12-04 10:52:05 +08:00
|
|
|
|
// 构建荷载
|
2024-12-03 17:55:33 +08:00
|
|
|
|
final gatewayResetReq = GatewayResetReq(
|
|
|
|
|
|
iD: gatewayId,
|
|
|
|
|
|
time: time,
|
|
|
|
|
|
);
|
2024-12-04 15:00:56 +08:00
|
|
|
|
// 构建荷载长度
|
2024-12-04 10:52:05 +08:00
|
|
|
|
final payload = gatewayResetReq.writeToBuffer();
|
2024-12-03 17:55:33 +08:00
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-03 17:55:33 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-12-04 10:52:05 +08:00
|
|
|
|
Payload: payload,
|
2024-12-04 17:24:58 +08:00
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
2024-12-04 10:52:05 +08:00
|
|
|
|
PayloadLength: payload.length,
|
2024-12-03 17:55:33 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.gatewayReset,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-05 13:50:20 +08:00
|
|
|
|
// 同意接听消息
|
|
|
|
|
|
static List<int> talkAcceptMessage({
|
2024-12-04 15:10:51 +08:00
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-04 15:10:51 +08:00
|
|
|
|
}) {
|
2024-12-05 13:50:20 +08:00
|
|
|
|
final talkAcceptReq = TalkAcceptReq();
|
|
|
|
|
|
final payload = talkAcceptReq.writeToBuffer();
|
2024-12-04 15:10:51 +08:00
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-04 15:10:51 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-12-04 17:24:58 +08:00
|
|
|
|
Payload: payload,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.talkAccept,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 14:21:51 +08:00
|
|
|
|
// 发送对话请求消息
|
|
|
|
|
|
static List<int> talkRequestMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-06 14:21:51 +08:00
|
|
|
|
}) {
|
2025-07-16 18:01:07 +08:00
|
|
|
|
final talkReq = TalkReq();
|
2024-12-06 14:21:51 +08:00
|
|
|
|
final payload = talkReq.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-06 14:21:51 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.callRequest,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-05 13:50:20 +08:00
|
|
|
|
// 拒绝接听消息
|
|
|
|
|
|
static List<int> talkRejectMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final talkReject = TalkReject();
|
|
|
|
|
|
final payload = talkReject.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.talkReject,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 呼叫保持消息
|
|
|
|
|
|
static List<int> talkPingMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final talkPing = TalkPing();
|
|
|
|
|
|
final payload = talkPing.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
2024-12-10 16:31:41 +08:00
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
2024-12-04 17:24:58 +08:00
|
|
|
|
PayloadLength: payload.length,
|
2024-12-04 15:10:51 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.talkPing,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-10 17:27:37 +08:00
|
|
|
|
// 通话中挂断
|
|
|
|
|
|
static List<int> talkHangupMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-10 17:27:37 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final talkHangup = TalkHangup();
|
|
|
|
|
|
final payload = talkHangup.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-10 17:27:37 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.talkHangup,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-05 13:50:20 +08:00
|
|
|
|
// 音视频数据
|
|
|
|
|
|
static List<int> talkDataMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2025-01-15 15:43:53 +08:00
|
|
|
|
required TalkData talkData,
|
2024-12-30 11:53:42 +08:00
|
|
|
|
int? SpTotal,
|
|
|
|
|
|
int? SpIndex,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
}) {
|
2025-01-15 15:43:53 +08:00
|
|
|
|
final payload = talkData.writeToBuffer();
|
2024-12-05 13:50:20 +08:00
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.RealTimeData,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
2025-01-15 15:43:53 +08:00
|
|
|
|
PayloadCRC: calculationCrcFromIntList(payload),
|
2024-12-05 13:50:20 +08:00
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.talkData,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 13:38:34 +08:00
|
|
|
|
// 预期接收消息
|
|
|
|
|
|
static List<int> talkExpectMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
2024-12-27 13:35:56 +08:00
|
|
|
|
required TalkExpectReq talkExpect,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-06 13:38:34 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final payload = talkExpect.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-06 13:38:34 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.talkExpect,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-05 13:50:20 +08:00
|
|
|
|
// GenericRespSuccess 消息
|
|
|
|
|
|
static List<int> genericRespSuccessMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required int PayloadType,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final genericResp = GenericResp();
|
2024-12-24 15:38:36 +08:00
|
|
|
|
genericResp.message = UdpConstant.genericRespSuccessMsg;
|
|
|
|
|
|
genericResp.code = UdpConstant.genericRespSuccessCode;
|
2024-12-05 13:50:20 +08:00
|
|
|
|
final payload = genericResp.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Resp,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadType,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GenericRespError 消息
|
|
|
|
|
|
static List<int> genericRespErrorMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required int PayloadType,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
int? MessageId,
|
2024-12-24 15:38:36 +08:00
|
|
|
|
String? errorMessageText,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
final genericResp = GenericResp();
|
2024-12-24 15:38:36 +08:00
|
|
|
|
genericResp.message = errorMessageText ?? UdpConstant.genericRespErrorMsg;
|
|
|
|
|
|
genericResp.code = UdpConstant.genericRespErrorCode;
|
2024-12-05 13:50:20 +08:00
|
|
|
|
final payload = genericResp.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Resp,
|
2024-12-19 14:48:19 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
2024-12-05 13:50:20 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadType,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-06 09:52:14 +08:00
|
|
|
|
// RbcuInfo 地址交换消息
|
|
|
|
|
|
static List<int> genericRbcuInfoMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required RbcuInfo rbcuInfo,
|
|
|
|
|
|
int? MessageId,
|
|
|
|
|
|
String? errorMessageText,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
final payload = rbcuInfo.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
2025-01-08 09:14:29 +08:00
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2025-01-06 09:52:14 +08:00
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
|
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
PayloadCRC: calculationCrc(payload),
|
|
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.RbcuInfo,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-08 09:14:29 +08:00
|
|
|
|
// 蓝牙消息
|
|
|
|
|
|
static List<int> bleMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required String bluetoothDeviceName,
|
|
|
|
|
|
required List<int> bleStructData,
|
|
|
|
|
|
int? MessageId,
|
|
|
|
|
|
int? timeout = 30,
|
|
|
|
|
|
int? IdleTimeout = 0,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
final bleReq = BleReq(
|
|
|
|
|
|
bluetoothDeviceName: bluetoothDeviceName,
|
|
|
|
|
|
structData: bleStructData,
|
|
|
|
|
|
timeout: timeout,
|
|
|
|
|
|
idleTimeout: 0,
|
|
|
|
|
|
);
|
|
|
|
|
|
final payload = bleReq.writeToBuffer();
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
|
|
|
|
|
MessageId: MessageId,
|
|
|
|
|
|
SpTotal: 1,
|
|
|
|
|
|
SpIndex: 1,
|
|
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: payload,
|
2025-01-14 17:57:33 +08:00
|
|
|
|
PayloadCRC: calculationCrcFromIntList(payload),
|
2025-01-08 09:14:29 +08:00
|
|
|
|
PayloadLength: payload.length,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.blePassthrough,
|
|
|
|
|
|
);
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
// 辅助方法:将16进制字符串转换为字节列表
|
|
|
|
|
|
static List<int> _hexToBytes(String hex) {
|
2025-01-14 17:57:33 +08:00
|
|
|
|
// 清理输入字符串,移除所有非十六进制字符
|
|
|
|
|
|
hex = hex.replaceAll(RegExp(r'[^0-9a-fA-F]'), '');
|
|
|
|
|
|
|
|
|
|
|
|
// 如果字符串长度为奇数,在前面补 0
|
|
|
|
|
|
if (hex.length % 2 != 0) {
|
|
|
|
|
|
hex = '0$hex';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
final bytes = <int>[];
|
2025-01-14 17:57:33 +08:00
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
for (int i = 0; i < hex.length; i += 2) {
|
2025-01-14 17:57:33 +08:00
|
|
|
|
// 确保 i + 2 不超过 hex 的长度
|
|
|
|
|
|
final end = i + 2 <= hex.length ? i + 2 : hex.length;
|
|
|
|
|
|
final hexByte = hex.substring(i, end);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析十六进制字符串为整数
|
|
|
|
|
|
try {
|
|
|
|
|
|
bytes.add(int.parse(hexByte, radix: 16));
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
// 如果解析失败,抛出异常或记录日志
|
|
|
|
|
|
throw FormatException('Invalid hex byte: $hexByte');
|
|
|
|
|
|
}
|
2024-11-28 14:57:49 +08:00
|
|
|
|
}
|
2025-01-14 17:57:33 +08:00
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
2024-12-02 15:43:59 +08:00
|
|
|
|
|
2024-12-04 17:24:58 +08:00
|
|
|
|
static int calculationCrc(Uint8List uint8Payload) {
|
|
|
|
|
|
// 使用自定义 CRC32 算法
|
|
|
|
|
|
final int crc32Value = _crc32Uint16(uint8Payload);
|
|
|
|
|
|
return crc32Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 14:48:19 +08:00
|
|
|
|
// 重载的方法,接受 List<int>
|
|
|
|
|
|
static int calculationCrcFromIntList(List<int> intList) {
|
|
|
|
|
|
// 将 List<int> 转换为 Uint8List
|
|
|
|
|
|
final Uint8List uint8Payload = Uint8List.fromList(intList);
|
|
|
|
|
|
|
|
|
|
|
|
// 调用现有的 calculationCrc 方法
|
|
|
|
|
|
return calculationCrc(uint8Payload);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-04 17:24:58 +08:00
|
|
|
|
// 将字符串转换为 Uint8List
|
|
|
|
|
|
static Uint8List _stringToUint8List(String input) {
|
|
|
|
|
|
return Uint8List.fromList(utf8.encode(input));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义 CRC32 实现
|
|
|
|
|
|
static int _crc32Uint16(Uint8List data) {
|
|
|
|
|
|
const int polynomial = 0xD5828281;
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 CRC32 表
|
|
|
|
|
|
final List<int> table = List<int>.generate(256, (i) {
|
|
|
|
|
|
int crc = i;
|
|
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
|
|
if ((crc & 1) != 0) {
|
|
|
|
|
|
crc = (crc >> 1) ^ polynomial;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
crc >>= 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return crc;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 计算 CRC32 校验值
|
|
|
|
|
|
int crc = 0xFFFFFFFF;
|
|
|
|
|
|
for (final int byte in data) {
|
|
|
|
|
|
crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
crc ^= 0xFFFFFFFF;
|
|
|
|
|
|
|
|
|
|
|
|
// 返回 CRC32 的低 16 位
|
|
|
|
|
|
return crc & 0xFFFF;
|
2024-12-02 15:43:59 +08:00
|
|
|
|
}
|
2024-11-28 14:57:49 +08:00
|
|
|
|
}
|