2024-10-15 14:24:35 +08:00
|
|
|
|
|
2024-04-24 16:04:07 +08:00
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
2024-10-15 14:24:35 +08:00
|
|
|
|
import 'package:crypto/crypto.dart' as crypto;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
import '../io_reply.dart';
|
|
|
|
|
|
import '../io_sender.dart';
|
2024-10-15 14:24:35 +08:00
|
|
|
|
import '../io_tool/io_tool.dart';
|
2024-04-24 16:04:07 +08:00
|
|
|
|
import '../io_type.dart';
|
2024-10-15 14:24:35 +08:00
|
|
|
|
import '../sm4Encipher/sm4.dart';
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
//oat升级
|
|
|
|
|
|
class OTAUpgradeCommand extends SenderProtocol {
|
|
|
|
|
|
|
|
|
|
|
|
OTAUpgradeCommand(
|
|
|
|
|
|
{this.lockID,
|
|
|
|
|
|
this.userID,
|
|
|
|
|
|
this.keyID,
|
|
|
|
|
|
this.platform,
|
|
|
|
|
|
this.product,
|
|
|
|
|
|
this.hwVersion,
|
|
|
|
|
|
this.fwVersion,
|
|
|
|
|
|
this.fwSize,
|
|
|
|
|
|
this.fwMD5,
|
|
|
|
|
|
this.needAuthor,
|
2024-04-25 09:39:31 +08:00
|
|
|
|
this.signKey,
|
2024-04-24 16:04:07 +08:00
|
|
|
|
this.privateKey,
|
2024-05-07 16:45:59 +08:00
|
|
|
|
this.token,
|
|
|
|
|
|
this.encrypt = true})
|
2024-04-24 16:04:07 +08:00
|
|
|
|
: super(CommandType.startOATUpgrade);
|
2024-10-15 14:24:35 +08:00
|
|
|
|
String? lockID;
|
|
|
|
|
|
String? userID;
|
|
|
|
|
|
String? keyID;
|
|
|
|
|
|
int? platform;
|
|
|
|
|
|
int? product;
|
|
|
|
|
|
String? hwVersion;
|
|
|
|
|
|
String? fwVersion;
|
|
|
|
|
|
int? fwSize;
|
|
|
|
|
|
String? fwMD5;
|
|
|
|
|
|
int? needAuthor;
|
|
|
|
|
|
List<int>? signKey;
|
|
|
|
|
|
List<int>? privateKey;
|
|
|
|
|
|
List<int>? token;
|
|
|
|
|
|
bool encrypt;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
2024-05-03 14:38:09 +08:00
|
|
|
|
@override
|
|
|
|
|
|
String toString() {
|
|
|
|
|
|
return 'OTAUpgradeCommand{lockID: $lockID, userID: $userID, '
|
|
|
|
|
|
'keyID: $keyID, platform: $platform, product: $product, '
|
|
|
|
|
|
'hwVersion: $hwVersion, fwVersion: $fwVersion, fwSize: $fwSize, '
|
|
|
|
|
|
'fwMD5: $fwMD5, needAuthor: $needAuthor, signKey: $signKey, '
|
|
|
|
|
|
'privateKey: $privateKey, token: $token}';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-07 16:45:59 +08:00
|
|
|
|
@override
|
|
|
|
|
|
int identifierValue() {
|
|
|
|
|
|
if (encrypt) {
|
|
|
|
|
|
return super.identifierValue();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return 0x20;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-24 16:04:07 +08:00
|
|
|
|
@override
|
|
|
|
|
|
List<int> messageDetail() {
|
2024-07-08 18:25:06 +08:00
|
|
|
|
List<int> data = <int>[];
|
|
|
|
|
|
List<int> ebcData = <int>[];
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
// 指令类型
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int type = commandType!.typeValue;
|
|
|
|
|
|
final double typeDouble = type / 256;
|
|
|
|
|
|
final int type1 = typeDouble.toInt();
|
|
|
|
|
|
final int type2 = type % 256;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.add(type1);
|
|
|
|
|
|
data.add(type2);
|
|
|
|
|
|
|
|
|
|
|
|
// 锁id 40
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int lockIDLength = utf8.encode(lockID!).length;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.addAll(utf8.encode(lockID!));
|
|
|
|
|
|
data = getFixedLengthList(data, 40 - lockIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//userID 20
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int userIDLength = utf8.encode(userID!).length;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.addAll(utf8.encode(userID!));
|
|
|
|
|
|
data = getFixedLengthList(data, 20 - userIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//platform 2
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int platform0 = (platform! & 0xFF00) >> 8;
|
|
|
|
|
|
final int platform1 = platform! & 0xFF;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.add(platform0);
|
|
|
|
|
|
data.add(platform1);
|
|
|
|
|
|
|
|
|
|
|
|
//product 2
|
2024-07-08 18:25:06 +08:00
|
|
|
|
data.addAll(<int>[0, 1]); //先默认是 01
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
//HwVersion 20
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int hwVersionLength = utf8.encode(hwVersion!).length;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.addAll(utf8.encode(hwVersion!));
|
|
|
|
|
|
data = getFixedLengthList(data, 20 - hwVersionLength);
|
|
|
|
|
|
|
|
|
|
|
|
//FwVersion 20
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int fwVersionLength = utf8.encode(fwVersion!).length;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.addAll(utf8.encode(fwVersion!));
|
|
|
|
|
|
data = getFixedLengthList(data, 20 - fwVersionLength);
|
|
|
|
|
|
|
|
|
|
|
|
//fwSize 4
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final ByteData bytes = ByteData(4); // 创建一个长度为4的字节数据
|
2024-04-24 16:04:07 +08:00
|
|
|
|
bytes.setInt32(0, fwSize!);
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final List<int> byteList = bytes.buffer.asUint8List();
|
2024-04-24 16:04:07 +08:00
|
|
|
|
data.addAll(byteList);
|
|
|
|
|
|
|
2024-04-25 14:23:05 +08:00
|
|
|
|
// 创建一个16字节的字节数组
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final Uint8List result = Uint8List(16);
|
2024-04-25 14:23:05 +08:00
|
|
|
|
// 将每个十六进制字符转换为4位二进制数据,并将其存储到结果字节数组中
|
|
|
|
|
|
for (int i = 0; i < fwMD5!.length; i += 2) {
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final String hex = fwMD5!.substring(i, i + 2);
|
|
|
|
|
|
final int byteValue = int.parse(hex, radix: 16);
|
2024-04-25 14:23:05 +08:00
|
|
|
|
result[i ~/ 2] = byteValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
data.addAll(result);
|
2024-04-24 16:04:07 +08:00
|
|
|
|
// token 长度4 首次请求 Token 填 0,如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 当token失效或者第一次发送的时候token为0
|
|
|
|
|
|
data.addAll(token!);
|
|
|
|
|
|
if (needAuthor == 0) {
|
|
|
|
|
|
//AuthCodeLen 1
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
} else {
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final List<int> authCodeData = <int>[];
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
//KeyID
|
|
|
|
|
|
authCodeData.addAll(utf8.encode(keyID!));
|
|
|
|
|
|
|
2024-04-25 09:39:31 +08:00
|
|
|
|
//UserID
|
|
|
|
|
|
authCodeData.addAll(utf8.encode(userID!));
|
|
|
|
|
|
|
2024-04-24 16:04:07 +08:00
|
|
|
|
//token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。
|
2024-07-08 18:25:06 +08:00
|
|
|
|
authCodeData.addAll(token ?? <int>[]);
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
2024-07-08 18:25:06 +08:00
|
|
|
|
authCodeData.addAll(signKey ?? <int>[]);
|
2024-04-25 14:23:05 +08:00
|
|
|
|
|
2024-04-24 16:04:07 +08:00
|
|
|
|
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final crypto.Digest authCode = crypto.md5.convert(authCodeData);
|
2024-04-24 16:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
data.add(authCode.bytes.length);
|
|
|
|
|
|
data.addAll(authCode.bytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((data.length % 16) != 0) {
|
2024-07-08 18:25:06 +08:00
|
|
|
|
final int add = 16 - data.length % 16;
|
2024-04-24 16:04:07 +08:00
|
|
|
|
for (int i = 0; i < add; i++) {
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-05-03 14:38:09 +08:00
|
|
|
|
printLog(data);
|
2024-05-07 16:45:59 +08:00
|
|
|
|
|
|
|
|
|
|
if (encrypt) {
|
|
|
|
|
|
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
|
|
|
|
|
|
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
|
|
|
|
|
|
return ebcData;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
2024-04-24 16:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OTAUpgradeReply extends Reply {
|
|
|
|
|
|
OTAUpgradeReply.parseData(CommandType commandType, List<int> dataDetail)
|
|
|
|
|
|
: super.parseData(commandType, dataDetail) {
|
|
|
|
|
|
data = dataDetail;
|
2024-04-25 09:39:31 +08:00
|
|
|
|
token = data.sublist(2, 6);
|
|
|
|
|
|
status = data[6];
|
2024-04-24 16:04:07 +08:00
|
|
|
|
errorWithStstus(status);
|
|
|
|
|
|
}
|
2024-10-15 14:24:35 +08:00
|
|
|
|
List<int> token = <int>[];
|
2024-04-24 16:04:07 +08:00
|
|
|
|
}
|