app-starlock/lib/blue/io_protocol/io_otaUpgrade.dart
2024-05-18 09:37:50 +08:00

189 lines
5.0 KiB
Dart
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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