调试蓝牙

This commit is contained in:
魏少阳 2023-08-11 14:50:42 +08:00
parent b6bafc9a47
commit a4648fcb26
6 changed files with 83 additions and 59 deletions

View File

@ -145,7 +145,7 @@ class BlueManage{
//
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
// print('Write with characteristicId${characteristic.characteristicId} serviceId${characteristic.serviceId} deviceId${characteristic.deviceId} value : $value hexStr:${radixString(value)}');
print('Write with characteristicId${characteristic.characteristicId} serviceId${characteristic.serviceId} deviceId${characteristic.deviceId} value : $value \nhexStr:${radixString(value)}');
try {
List<int> valueList = value;

View File

@ -29,7 +29,7 @@ class GetPrivateKeyCommand extends SenderProtocol {
List<int> messageDetail() {
List<int> data = [];
List<int> ebcData = [];
print("lockID${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
// print("lockID${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
//
int type = commandType!.typeValue;
@ -38,9 +38,9 @@ class GetPrivateKeyCommand extends SenderProtocol {
int type2 = type % 256;
data.add(type1);
data.add(type2);
print("type:$type");
print("type1:$type1");
print("type2:$type2");
// print("type:$type");
// print("type1:$type1");
// print("type2:$type2");
// id
int lockIDLength = utf8.encode(lockID!).length;
@ -71,12 +71,6 @@ class GetPrivateKeyCommand extends SenderProtocol {
} else {
List<int> authCodeData = [];
//KeyID
// authCodeData.addAll(utf8.encode(keyID!));
//
// //authUserID
// authCodeData.addAll(utf8.encode(authUserID!));
//KeyID
authCodeData.addAll(utf8.encode(keyID!));
@ -92,15 +86,16 @@ class GetPrivateKeyCommand extends SenderProtocol {
authCodeData.add((d1 & 0xff00) >> 8);
authCodeData.add((d1 & 0xff));
// var pubKey = Storage.getData("bluePublicKey");
var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
List<int> pubKeyData = base64.decode(pubKey);
var pubKey = Storage.getData("bluePublicKey");
// var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
print("pubKey:$pubKey");
List<int> pubKeyData = base64.decode(pubKey.toString());
print("pubKeyData:$pubKeyData");
authCodeData.addAll(pubKeyData);
// KeyIDauthUserIDmd5加密之后就是authCode
var authCode = a.md5.convert(authCodeData);
print(
"authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
// print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
data.add(authCode.bytes.length);
data.addAll(authCode.bytes);
@ -116,9 +111,9 @@ class GetPrivateKeyCommand extends SenderProtocol {
// LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
String key = SM4.createHexKey(key: 'TMH_c3570480da8d');
ebcData = SM4.encrypt(data, key: key, mode: SM4CryptoMode.ECB);
ebcData.removeRange(ebcData.length - 16, ebcData.length);
// ebcData = utf8.encode(getSM4Str(data, "TMH_c3570480da8d"));
print("ebcData:$ebcData");
// String cbcEncryptData = SM4.encrypt(data: data, key: "TMH_c3570480da8d", mode: SM4CryptoMode.CBC, iv: iv,);
return ebcData;
}
}
@ -127,13 +122,23 @@ class GetPrivateKeyReply extends Reply {
GetPrivateKeyReply.parseData(CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
print('获取私钥');
int index = 0;
// while(index < endIndex){
// commandKey = byteUInt8(dataDetail, index);
// index += offset_1;
// switch(commandKey){
//
// }
// }
switch(dataDetail[0]){
case 0x00:
//
break;
case 0x07:
//
break;
case 0x0f:
//
break;
default:
//
break;
}
}
}

View File

@ -42,12 +42,12 @@ class GetPublicKeyCommand extends SenderProtocol {
class GetPublicKeyReply extends Reply {
GetPublicKeyReply.parseData(CommandType commandType, List<int> dataDetail)
: super.parseData(commandType, dataDetail) {
var tokenData = dataDetail.sublist(1);
print('获取公钥:$tokenData');
switch(dataDetail[0]){
var tokenData = dataDetail.sublist(3);
print('获取公钥:dataDetail$dataDetail tokenData:$tokenData');
switch(dataDetail[2]){
case 0x00:
//
var stringEncoded = base64.encode(tokenData);
String stringEncoded = base64.encode(tokenData);
print('获取公钥成功 publickey:$stringEncoded');
//
Storage.setData("bluePublicKey", stringEncoded);

View File

@ -15,7 +15,8 @@ abstract class SenderProtocol extends IOData {
final List<int> header = [0XEF, 0X01, 0XEE, 0X02]; // 0XEF01EE02 4
final int ask = 0X01 ; // 0X01 0X11 1
int? _commandIndex = 1; //
final int identifier = 0x20; // 4 4 1 01AES1282SM43SM4
int identifier = 0x20; // 4 4 1 01AES1282SM43SM4
List<int>? commandData = []; //
// final int? tail = 0xFF; // CRC 2
@ -47,19 +48,28 @@ abstract class SenderProtocol extends IOData {
// print("_commandIndex:$_commandIndex commandIndexChang1$commandIndexChang1 commandIndexChang2:$commandIndexChang2");
//
//
int type = commandType!.typeValue;
if(type == 0x3090){
//
identifier = 0x20;
}else if(type == 0x3091){
//
identifier = 0x22;
}
commandList.add(identifier);
// print("identifier:$identifier");
//
// int dataLength = dataSourceLength();
int dataLen = dataSourceLength();
// commandList.add(dataLength);
// print("dataLength:$dataLength");
var dataLen = 42;
print("dataLen:$dataLen");
// var dataLen = 42;
double dataLength = dataLen/256;
commandList.add(dataLength.toInt());
commandList.add(42%256);
commandList.add(dataLen%256);
commandList.add(dataLength.toInt());
commandList.add(42%256);
commandList.add(dataLen%256);
//
commandList.addAll(commandData!); //

View File

@ -1,12 +1,14 @@
import 'io_protocol/io_addUser.dart';
import 'io_protocol/io_getPrivateKey.dart';
import 'io_protocol/io_getPublicKey.dart';
import 'io_protocol/io_openDoor.dart';
import 'io_protocol/io_reply.dart';
import 'io_protocol/io_type.dart';
import 'io_tool/io_tool.dart';
import 'io_tool/manager_event_bus.dart';
import 'sm4Encipher/sm4.dart';
class CommandReciverManager {
@ -26,31 +28,24 @@ class CommandReciverManager {
print("temType:$tmpType");
var dataLen = data[8] * 256 + data[9];// 16
var oriLen = data[10] * 256 + data[11];// 16
print("dataLen:$dataLen oriLen:$oriLen");
List<int> dataList = [];
List<int> oriDataList = [];
switch(tmpType){
case 0: //
for (var i = 0; i < oriLen ; i++) {
oriDataList.add(data[12 + i]);
}
parseData(oriDataList).then((value) {
EventBusManager().eventBusFir(value);
});
break;
case 1: //AES128
break;
case 2: //SM4
// for (var i = 0; i < dataLen ; i++) {
// dataList.add(data[12 + i]);
// }
//console.log("currCommStru.pairedName = ", currCommStru.pairedName);
// var d_cbc = cbc.decrypt_ecb(dataView, currCommStru.pairLockID, true, "nobase64");
// console.log("d_cbc = ", d_cbc);
// console.log("oriLen = ", oriLen);
// for (var i = 0; i < oriLen ; i++) {
// oriDataView[i] = d_cbc[i];
// oriDataList.add(data[12 + i]);
// }
// console.log("oriDataView = ", oriDataView);
oriDataList = data.sublist(12, 12 + oriLen);
print("oriDataList:$oriDataList");
break;
case 1:
//AES128
break;
case 2:
//SM4
String key = SM4.createHexKey(key: 'TMH_c3570480da8d');
oriDataList = SM4.encrypt(data, key: key, mode: SM4CryptoMode.ECB);
break;
case 3: //SM4
// for (var i = 0; i < dataLen ; i++) {
@ -70,8 +65,10 @@ class CommandReciverManager {
break;
}
parseData(oriDataList).then((value) {
EventBusManager().eventBusFir(value);
});
}
// print('✅ 执行结束 _buffer:${_buffer.length}');
}
@ -79,7 +76,7 @@ class CommandReciverManager {
if(data.isNotEmpty){
var cmd = data[0] * 256 + data[1];
CommandType commandType = ExtensionCommandType.getCommandType(cmd);
data.removeRange(0, 2);
// data.removeRange(0, 2);
print("111111data commandType:$commandType data:$data");
var reply;
switch(commandType) {
@ -88,6 +85,11 @@ class CommandReciverManager {
reply = GetPublicKeyReply.parseData(commandType, data);
}
break;
case CommandType.getLockPrivateKey:
{
reply = GetPrivateKeyReply.parseData(commandType, data);
}
break;
case CommandType.addUser:
{
reply = AddUserReply.parseData(commandType, data);
@ -101,6 +103,5 @@ class CommandReciverManager {
}
return reply;
}
}
}

View File

@ -525,10 +525,18 @@ class SM4 {
return output;
}
static decrypt(String cipherText,
// static decrypt(String cipherText,
// {String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
// if (key != null) setKey(key);
// List<int> input = SMUtils.hexStringToBytes(cipherText);
// List<int> output = _crypto(input, SM4_DECRYPT, mode, iv);
// return utf8.decode(output);
// }
static decrypt(List<int> data,
{String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
if (key != null) setKey(key);
List<int> input = SMUtils.hexStringToBytes(cipherText);
List<int> input = data;
List<int> output = _crypto(input, SM4_DECRYPT, mode, iv);
return utf8.decode(output);
}