完成添加用户蓝牙协议调试,添加其他逻辑
This commit is contained in:
parent
d91f59db48
commit
796cd3a0f6
@ -4,6 +4,7 @@ import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||||
import 'package:star_lock/blue/sender_manage.dart';
|
||||
|
||||
import '../app_settings/app_settings.dart';
|
||||
import 'io_tool/io_manager.dart';
|
||||
import 'io_tool/io_tool.dart';
|
||||
import 'io_tool/manager_event_bus.dart';
|
||||
import 'reciver_data.dart';
|
||||
@ -52,7 +53,6 @@ class BlueManage{
|
||||
} else {
|
||||
_scanDevices.add(device);
|
||||
}
|
||||
// EventBusManager().eventBusFir(_scanDevices);
|
||||
scanResultCallBack(_scanDevices);
|
||||
}
|
||||
// _pushState();
|
||||
@ -62,13 +62,15 @@ class BlueManage{
|
||||
}
|
||||
|
||||
/// 连接监听状态
|
||||
Future<void> connect(String deviceMAC) async {
|
||||
Future<void> connect(String deviceMAC, String deviceName) async {
|
||||
print("connect:$deviceMAC");
|
||||
_flutterReactiveBle!.connectToDevice(id: deviceMAC).listen((connectionStateUpdate) {
|
||||
print('ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}');
|
||||
// EventBusManager().eventBusFir(connectionStateUpdate);
|
||||
if(connectionStateUpdate.connectionState == DeviceConnectionState.connected){
|
||||
// getPublicKey(update.deviceId);
|
||||
// 先配置lockId
|
||||
IoManager().configCurrentDeviceLockId(deviceName);
|
||||
// 如果状态是连接的开始发现服务
|
||||
discoverServices(deviceMAC);
|
||||
}
|
||||
@ -137,7 +139,7 @@ class BlueManage{
|
||||
_flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen((data) {
|
||||
// code to handle incoming data
|
||||
print("subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data");
|
||||
CommandReciverManager.appDataReceive(data, "");
|
||||
CommandReciverManager.appDataReceive(data);
|
||||
}, onError: (dynamic error) {
|
||||
print("subscribeToCharacteristic error:$error");
|
||||
});
|
||||
@ -145,7 +147,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 \nhexStr:${radixString(value)}');
|
||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value \nhexStr:${radixHex16String(value)}');
|
||||
|
||||
try {
|
||||
List<int> valueList = value;
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../tools/storage.dart';
|
||||
import '../io_tool/io_manager.dart';
|
||||
import '../io_tool/io_tool.dart';
|
||||
import '../sm4Encipher/sm4.dart';
|
||||
import 'io_reply.dart';
|
||||
@ -22,8 +24,9 @@ class AddUserCommand extends SenderProtocol {
|
||||
int? role;
|
||||
String? password;
|
||||
int? needAuthor;
|
||||
List<int>? userPperationToken;
|
||||
List<int>? publicKeyData;
|
||||
List<int>? publicKey;
|
||||
List<int>? privateKey;
|
||||
List<int>? token;
|
||||
AddUserCommand({
|
||||
this.lockID,
|
||||
this.authUserID,
|
||||
@ -36,8 +39,9 @@ class AddUserCommand extends SenderProtocol {
|
||||
this.role,
|
||||
this.password,
|
||||
this.needAuthor,
|
||||
this.userPperationToken,
|
||||
this.publicKeyData
|
||||
this.publicKey,
|
||||
this.privateKey,
|
||||
this.token
|
||||
}) : super(CommandType.addUser);
|
||||
|
||||
@override
|
||||
@ -111,13 +115,13 @@ class AddUserCommand extends SenderProtocol {
|
||||
if(needAuthor == 0){
|
||||
// 当token失效或者第一次发送的时候token为0
|
||||
//Token 4
|
||||
data.addAll([0, 0, 0, 0]);
|
||||
data.addAll(token!);
|
||||
|
||||
//AuthCodeLen 1
|
||||
data.add(0);
|
||||
} else {
|
||||
// token 长度4 首次请求 Token 填 0,如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。
|
||||
data.addAll(userPperationToken!);
|
||||
data.addAll(token!);
|
||||
|
||||
List<int> authCodeData = [];
|
||||
|
||||
@ -127,22 +131,13 @@ class AddUserCommand extends SenderProtocol {
|
||||
//authUserID
|
||||
authCodeData.addAll(utf8.encode(authUserID!));
|
||||
|
||||
//token 4
|
||||
authCodeData.addAll(userPperationToken!);
|
||||
//token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。
|
||||
data.addAll([0, 0, 0, 0]);
|
||||
|
||||
// String pubKey = "";
|
||||
// Storage.getData("bluePublicKey").then((res) => {
|
||||
// pubKey = res
|
||||
// });
|
||||
// var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
|
||||
// print("pubKey:$pubKey");
|
||||
// List<int> pubKeyData = base64.decode(pubKey.toString());
|
||||
// print("pubKeyData:$pubKeyData");
|
||||
authCodeData.addAll(publicKeyData!);
|
||||
authCodeData.addAll(publicKey!);
|
||||
|
||||
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
||||
var authCode = crypto.md5.convert(authCodeData);
|
||||
// print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
|
||||
|
||||
data.add(authCode.bytes.length);
|
||||
data.addAll(authCode.bytes);
|
||||
@ -154,22 +149,16 @@ class AddUserCommand extends SenderProtocol {
|
||||
data.add(0);
|
||||
}
|
||||
}
|
||||
print("SM4Data:$data");
|
||||
print("addUserSM4Data:$data");
|
||||
// 拿到数据之后通过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");
|
||||
|
||||
return data;
|
||||
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
|
||||
return ebcData;
|
||||
}
|
||||
}
|
||||
|
||||
class AddUserReply extends Reply {
|
||||
AddUserReply.parseData(CommandType commandType, List<int> dataDetail)
|
||||
: super.parseData(commandType, dataDetail) {
|
||||
int index = 0;
|
||||
|
||||
data = dataDetail;
|
||||
}
|
||||
}
|
||||
@ -3,13 +3,17 @@ import 'dart:convert';
|
||||
import 'package:star_lock/blue/sm4Encipher/sm4.dart';
|
||||
|
||||
import '../../tools/storage.dart';
|
||||
import '../io_tool/io_manager.dart';
|
||||
import '../io_tool/io_tool.dart';
|
||||
import '../sender_manage.dart';
|
||||
import 'io_reply.dart';
|
||||
import 'io_sender.dart';
|
||||
import 'io_type.dart';
|
||||
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
|
||||
List<int>publicKeyDataList = [];
|
||||
|
||||
class GetPrivateKeyCommand extends SenderProtocol {
|
||||
String? lockID;
|
||||
String? keyID; // 钥匙ID
|
||||
@ -28,6 +32,7 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
|
||||
@override
|
||||
List<int> messageDetail() {
|
||||
publicKeyDataList = publicKeyData!;
|
||||
List<int> data = [];
|
||||
List<int> ebcData = [];
|
||||
// print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
||||
@ -87,22 +92,10 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
authCodeData.add((d1 & 0xff00) >> 8);
|
||||
authCodeData.add((d1 & 0xff));
|
||||
|
||||
List<int> getIntList;
|
||||
Storage.getStringList("bluePublicKey").then((res) {
|
||||
print("getStrList:$res");
|
||||
getIntList = changeStringListToIntList(res!);
|
||||
print("getIntList:$getIntList");
|
||||
});
|
||||
|
||||
// List<String> getStrList = Storage.getLocalStorage("bluePublicKey") as List<String>;
|
||||
// List<int> getIntList = changeStringListToIntList(getStrList);
|
||||
|
||||
// print("getIntList:$getIntList");
|
||||
authCodeData.addAll(publicKeyData!);
|
||||
|
||||
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
||||
var authCode = crypto.md5.convert(authCodeData);
|
||||
// print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
|
||||
|
||||
data.add(authCode.bytes.length);
|
||||
data.addAll(authCode.bytes);
|
||||
@ -114,13 +107,10 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
data.add(0);
|
||||
}
|
||||
}
|
||||
print("SM4Data:$data");
|
||||
// print("SM4Data:$data");
|
||||
// 拿到数据之后通过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");
|
||||
ebcData = SM4.encrypt(data, key: utf8.encode(IoManager().getCurrentDeviceLockId), mode: SM4CryptoMode.ECB);
|
||||
|
||||
return ebcData;
|
||||
}
|
||||
}
|
||||
@ -128,32 +118,8 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
class GetPrivateKeyReply extends Reply {
|
||||
GetPrivateKeyReply.parseData(CommandType commandType, List<int> dataDetail)
|
||||
: super.parseData(commandType, dataDetail) {
|
||||
var getData = dataDetail.sublist(2);
|
||||
data = dataDetail.sublist(2);
|
||||
status = data[0];
|
||||
// print("getData:$getData");
|
||||
switch(getData[0]){
|
||||
case 0x00:
|
||||
//成功
|
||||
print('获取私钥成功');
|
||||
getData.removeAt(0);
|
||||
List commKey = getData.sublist(0, 16);
|
||||
List signKey = getData.sublist(16);
|
||||
print("commKey:$commKey signKey:$signKey");
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
print('获取私钥无权限');
|
||||
|
||||
break;
|
||||
case 0x0f:
|
||||
//用户已存在
|
||||
print('获取私钥:用户已存在');
|
||||
|
||||
break;
|
||||
default:
|
||||
//失败
|
||||
print('获取私钥失败');
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import '../../tools/storage.dart';
|
||||
import '../io_tool/io_manager.dart';
|
||||
import '../io_tool/io_tool.dart';
|
||||
import '../sender_manage.dart';
|
||||
import 'io_reply.dart';
|
||||
@ -42,33 +43,7 @@ class GetPublicKeyCommand extends SenderProtocol {
|
||||
class GetPublicKeyReply extends Reply {
|
||||
GetPublicKeyReply.parseData(CommandType commandType, List<int> dataDetail)
|
||||
: super.parseData(commandType, dataDetail) {
|
||||
var tokenData = dataDetail.sublist(3);
|
||||
print('获取公钥:dataDetail:$dataDetail tokenData:$tokenData');
|
||||
switch(dataDetail[2]){
|
||||
case 0x00:
|
||||
//成功
|
||||
String stringEncoded = base64.encode(tokenData);
|
||||
print('获取公钥成功 publickey:$stringEncoded');
|
||||
// 储存公钥
|
||||
var saveStrList = changeIntListToStringList(tokenData);
|
||||
// Storage.setStringList("bluePublicKey", saveStrList);
|
||||
Storage.setStringList("bluePublicKey", saveStrList);
|
||||
// List<String> getStrList = Storage.getLocalStorage("bluePublicKey") as List<String>;
|
||||
// print('获取储存的公钥 getStrList:$getStrList');
|
||||
IoSenderManage.getPrivateKey("TMH_c3570480da8d", "1", "1", 1, tokenData, 1);
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
|
||||
break;
|
||||
case 0x0f:
|
||||
//用户已存在
|
||||
|
||||
break;
|
||||
default:
|
||||
//失败
|
||||
|
||||
break;
|
||||
}
|
||||
status = dataDetail[2];
|
||||
data = dataDetail;
|
||||
}
|
||||
}
|
||||
@ -1,22 +1,13 @@
|
||||
import 'io_type.dart';
|
||||
|
||||
abstract class Reply{
|
||||
//value字节长度
|
||||
int? packetHeader = 4; // 包头4个字节
|
||||
int? packetType = 1; // 包类型1个字节
|
||||
int? packetNumber = 1; // 包序号
|
||||
int? packetIdentifier = 1; // 包标识
|
||||
int? packetLength = 4; // 包标识
|
||||
|
||||
CommandType? commandType;
|
||||
int? packetMcrc = 2; // 校验位
|
||||
|
||||
//command key flag
|
||||
int commandKey = 0;
|
||||
|
||||
int status = 0;
|
||||
List<int> data = [];
|
||||
Reply.parseData(this.commandType, List<int> dataDetail);
|
||||
// Reply({this.result});
|
||||
|
||||
|
||||
// bool get isSuccessfully => packetHeader == EF01EE02;
|
||||
|
||||
}
|
||||
@ -48,17 +48,9 @@ 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");
|
||||
// 指令类型 高 4 位表示包版本,低 4 位用来指示后面数据的加密类型,长度为 1 字节,加密类型取值说明,0:明文,1:AES128,2:SM4(事先约定密钥),3:SM4(设备指定密钥)
|
||||
commandList.add(commandType!.identifierValue);
|
||||
print("commandType!.identifierValue:${commandType!.identifierValue}");
|
||||
|
||||
// 数据长度
|
||||
int dataLen = dataSourceLength();
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
//TODO:发送指令类型
|
||||
import 'package:get/get.dart';
|
||||
|
||||
enum CommandType {
|
||||
addUser, //增加用户 = 0x3001
|
||||
deletUser , //删除用户 = 0x3002
|
||||
@ -164,6 +166,22 @@ extension ExtensionCommandType on CommandType {
|
||||
return type;
|
||||
}
|
||||
|
||||
int get identifierValue {
|
||||
int type = 0x23;
|
||||
switch(this){
|
||||
case CommandType.getLockPublicKey:
|
||||
type = 0x20;
|
||||
break;
|
||||
case CommandType.getLockPrivateKey:
|
||||
type = 0x22;
|
||||
break;
|
||||
default:
|
||||
type = 0x23;
|
||||
break;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
String get typeName {
|
||||
String t = '';
|
||||
switch(typeValue){
|
||||
|
||||
@ -3,6 +3,11 @@ enum DataTransmissionMode {
|
||||
ble,
|
||||
}
|
||||
|
||||
const saveBluePublicKey = "BluePublicKey";
|
||||
const saveBluePrivateKey = "BluePrivateKey";
|
||||
const saveBlueSignKey = "BlueSignKey";
|
||||
const saveBlueToken = "BlueToken";
|
||||
|
||||
class IoManager {
|
||||
|
||||
static IoManager? _ioManager;
|
||||
@ -21,18 +26,20 @@ class IoManager {
|
||||
///蓝牙传输协议
|
||||
void bleTransmission() => _dataTransmissionMode = DataTransmissionMode.ble;
|
||||
|
||||
int _commandIndex = 1; //割草机协议帧序号
|
||||
///割草机协议帧序号
|
||||
int _commandIndex = 1;
|
||||
configCommandIdx(int idx) => _commandIndex = idx;
|
||||
Future<void> increaseCommandIndex() async {
|
||||
_commandIndex < 255 ? _commandIndex++ : _commandIndex = 0;
|
||||
}
|
||||
|
||||
void resetCommandIndex(){
|
||||
_commandIndex = 0;
|
||||
}
|
||||
|
||||
void resetCommandIndex() => _commandIndex = 0;
|
||||
int get commandIndex => _commandIndex;
|
||||
|
||||
/// 当前设备连接的lockId
|
||||
String _currentDeviceLockId = "";
|
||||
configCurrentDeviceLockId(String lockId) => _currentDeviceLockId = lockId;
|
||||
String get getCurrentDeviceLockId => _currentDeviceLockId;
|
||||
|
||||
void resetAllFlags() {
|
||||
resetCommandIndex();
|
||||
}
|
||||
|
||||
@ -254,7 +254,7 @@ List<List<T>> splitList<T>(List<T> list, int len) {
|
||||
//TODO:int->两个字节 List 高字节在前,低字节在后(小端存储) 本工程只有配置 WiFi用到的!!!!
|
||||
List<int> intToByte2ListLow(int value) => [value >> 8, value];
|
||||
|
||||
String radixString(List<int> codeUnits) {
|
||||
String radixHex16String(List<int> codeUnits) {
|
||||
String result = '';
|
||||
codeUnits.forEach((value) {
|
||||
result += value.toRadixString(16).padLeft(2, '0');
|
||||
|
||||
@ -1,18 +1,22 @@
|
||||
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../tools/storage.dart';
|
||||
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_manager.dart';
|
||||
import 'io_tool/io_tool.dart';
|
||||
import 'io_tool/manager_event_bus.dart';
|
||||
import 'sm4Encipher/sm4.dart';
|
||||
|
||||
class CommandReciverManager {
|
||||
|
||||
static void appDataReceive(List<int> data, String lockId) async {
|
||||
static void appDataReceive(List<int> data) async {
|
||||
///解析数据
|
||||
if(data.isEmpty){
|
||||
return;
|
||||
@ -46,28 +50,26 @@ class CommandReciverManager {
|
||||
// SM4(事先约定密钥)
|
||||
// 获取的加密数组
|
||||
var getDataList = data.sublist(12, 12 + dataLen);
|
||||
String key = SM4.createHexKey(key: 'TMH_c3570480da8d');
|
||||
|
||||
// 解密
|
||||
oriDataList = SM4.decrypt(getDataList, key: key, mode: SM4CryptoMode.ECB);
|
||||
// String key = SM4.createHexKey(key: IoManager().getCurrentDeviceLockId);
|
||||
oriDataList = SM4.decrypt(getDataList, key: utf8.encode(IoManager().getCurrentDeviceLockId), mode: SM4CryptoMode.ECB);
|
||||
oriDataList = oriDataList.sublist(0, oriLen);
|
||||
print("SM4 oriDataList:$oriDataList");
|
||||
break;
|
||||
case 3:
|
||||
//SM4(设备指定密钥)
|
||||
// for (var i = 0; i < dataLen ; i++) {
|
||||
// dataView[i] = uint8Recv[12 + i];
|
||||
// }
|
||||
// console.log("dataView.length = ", dataView.length);
|
||||
// console.log("currCommStru.pairCommKey = ", currCommStru.pairCommKey);
|
||||
// var commKey1 = base64js.toByteArray(currCommStru.pairCommKey);
|
||||
// console.log("commKey1 = ", commKey1);
|
||||
// var d_cbc = cbc.decrypt_ecb(dataView, commKey1, false, "nobase64");
|
||||
// console.log("d_cbc = ", d_cbc);
|
||||
// console.log("oriLen = ", oriLen);
|
||||
// for (var i = 0; i < oriLen ; i++) {
|
||||
// oriDataView[i] = d_cbc[i];
|
||||
// }
|
||||
// console.log("oriDataView = ", oriDataView);
|
||||
// 获取的加密数组
|
||||
var getDataList = data.sublist(12, 12 + dataLen);
|
||||
|
||||
var res = await Storage.getStringList(saveBluePrivateKey);
|
||||
List<int> getPrivateKeyList = changeStringListToIntList(res!);
|
||||
|
||||
// 解密
|
||||
// String key = SM4.createHexKey(key: radixHex16String(getPrivateKeyList!));
|
||||
oriDataList = SM4.decrypt(getDataList, key: getPrivateKeyList, mode: SM4CryptoMode.ECB);
|
||||
oriDataList = oriDataList.sublist(0, oriLen);
|
||||
print("SM4 oriDataList:$oriDataList");
|
||||
break;
|
||||
|
||||
}
|
||||
@ -82,6 +84,7 @@ class CommandReciverManager {
|
||||
if(data.isNotEmpty){
|
||||
var cmd = data[0] * 256 + data[1];
|
||||
CommandType commandType = ExtensionCommandType.getCommandType(cmd);
|
||||
await IoManager().increaseCommandIndex();
|
||||
// data.removeRange(0, 2);
|
||||
print("111111data cmd:$cmd commandType:$commandType data:$data");
|
||||
var reply;
|
||||
|
||||
@ -24,9 +24,9 @@ class CommandSenderManager {
|
||||
bool canSendControlCommand = false;
|
||||
|
||||
//TODO:发送常规数据
|
||||
void managerSendData ({
|
||||
Future<void> managerSendData ({
|
||||
required SenderProtocol command,
|
||||
CommandSendCallBack? callBack}) {
|
||||
CommandSendCallBack? callBack}) async {
|
||||
if (callBack != null) {
|
||||
// if (!BluetoothManager().connected) {
|
||||
print('❌ 蓝牙断开了');
|
||||
@ -38,7 +38,7 @@ class CommandSenderManager {
|
||||
return;
|
||||
}
|
||||
|
||||
List<int> value = command.packageData();
|
||||
List<int> value = await command.packageData();
|
||||
// print("sendData:${value}");
|
||||
_sendNormalData(value);
|
||||
}
|
||||
|
||||
@ -8,50 +8,66 @@ import 'sender_data.dart';
|
||||
class IoSenderManage {
|
||||
|
||||
//todo:获取公钥
|
||||
static void getPublicKey(String lockId ,{CommandSendCallBack? callBack}) {
|
||||
static void getPublicKey({String? lockId ,CommandSendCallBack? callBack}) {
|
||||
CommandSenderManager().managerSendData(command: GetPublicKeyCommand(
|
||||
lockID: lockId,
|
||||
), callBack:callBack);
|
||||
}
|
||||
|
||||
//todo:获取私钥
|
||||
static void getPrivateKey(
|
||||
String? lockId,
|
||||
String? keyID, // 钥匙ID
|
||||
String? authUserID,// 钥匙授权人ID
|
||||
int? nowTime,
|
||||
List<int>? publicKeyData,
|
||||
int? needAuthor, {CommandSendCallBack? callBack}) {
|
||||
static void getPrivateKey({
|
||||
String? lockId,
|
||||
String? keyID, // 钥匙ID
|
||||
String? authUserID,// 钥匙授权人ID
|
||||
int? nowTime,
|
||||
List<int>? publicKeyData,
|
||||
int? needAuthor,
|
||||
CommandSendCallBack? callBack}) {
|
||||
CommandSenderManager().managerSendData(command: GetPrivateKeyCommand(
|
||||
lockID: lockId,
|
||||
keyID: keyID,
|
||||
authUserID: authUserID,
|
||||
nowTime: nowTime,
|
||||
publicKeyData:publicKeyData,
|
||||
needAuthor: needAuthor,
|
||||
keyID: keyID,
|
||||
authUserID: authUserID,
|
||||
nowTime: nowTime,
|
||||
publicKeyData:publicKeyData,
|
||||
needAuthor: needAuthor,
|
||||
), callBack:callBack);
|
||||
}
|
||||
|
||||
//todo:添加用户
|
||||
// static void senderAddUser({CommandSendCallBack? callBack}) {
|
||||
// CommandSenderManager().managerSendData(
|
||||
// command: AddUserCommand(
|
||||
// cmdID: 0,
|
||||
// lockID: "",
|
||||
// authUserID: "",
|
||||
// keyID: "",
|
||||
// userID: "",
|
||||
// openMode: 0,
|
||||
// keyType: 0,
|
||||
// startDate: 0,
|
||||
// expireDate: 0,
|
||||
// role: 0,
|
||||
// password: "",
|
||||
// token: 0,
|
||||
// authCodeLen: 0,
|
||||
// authCode: "",
|
||||
// ), callBack:callBack);
|
||||
// }
|
||||
static void senderAddUser({
|
||||
String? lockID,
|
||||
String? authUserID,
|
||||
String? keyID,
|
||||
String? userID,
|
||||
int? openMode,
|
||||
int? keyType,
|
||||
int? startDate,
|
||||
int? expireDate,
|
||||
int? role,
|
||||
String? password,
|
||||
int? needAuthor,
|
||||
List<int>? publicKey,
|
||||
List<int>? privateKey,
|
||||
List<int>? token,
|
||||
CommandSendCallBack? callBack}) {
|
||||
CommandSenderManager().managerSendData(
|
||||
command: AddUserCommand(
|
||||
lockID: lockID,
|
||||
authUserID: authUserID,
|
||||
keyID: keyID,
|
||||
userID: userID,
|
||||
openMode: openMode,
|
||||
keyType: keyType,
|
||||
startDate: startDate,
|
||||
expireDate: expireDate,
|
||||
role: role,
|
||||
password: password,
|
||||
needAuthor: needAuthor,
|
||||
publicKey: publicKey,
|
||||
privateKey: privateKey,
|
||||
token: token
|
||||
), callBack:callBack);
|
||||
}
|
||||
|
||||
//todo:开锁
|
||||
// static void senderOpenDoor({CommandSendCallBack? callBack}) {
|
||||
|
||||
@ -359,8 +359,8 @@ class SM4 {
|
||||
return roundKey;
|
||||
}
|
||||
|
||||
static void setKey(String key) {
|
||||
List<int> keyBytes = SMUtils.hexStringToBytes(key);
|
||||
static void setKey(List<int> key) {
|
||||
List<int> keyBytes = key;
|
||||
List<int> intermediateKeys = List<int>.filled(36, 0);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
intermediateKeys[i] = _readUint32BE(keyBytes, i * 4) ^ FK[i];
|
||||
@ -520,7 +520,7 @@ class SM4 {
|
||||
}
|
||||
|
||||
static List<int> encrypt(List<int> data,
|
||||
{String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
|
||||
{List<int>? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
|
||||
if (key != null) setKey(key);
|
||||
List<int> input = data;
|
||||
List<int> output = _crypto(input, SM4_ENCRYPT, mode, iv);
|
||||
@ -536,7 +536,7 @@ class SM4 {
|
||||
// }
|
||||
|
||||
static decrypt(List<int> data,
|
||||
{String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
|
||||
{List<int>? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) {
|
||||
if (key != null) setKey(key);
|
||||
List<int> input = data;
|
||||
List<int> output = _crypto(input, SM4_DECRYPT, mode, iv);
|
||||
|
||||
@ -2,12 +2,20 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:star_lock/blue/io_protocol/io_getPrivateKey.dart';
|
||||
import 'package:star_lock/blue/io_protocol/io_getPublicKey.dart';
|
||||
import 'package:star_lock/tools/baseGetXController.dart';
|
||||
|
||||
import '../../../blue/blue_manage.dart';
|
||||
import '../../../blue/io_protocol/io_addUser.dart';
|
||||
import '../../../blue/io_protocol/io_reply.dart';
|
||||
import '../../../blue/io_tool/io_manager.dart';
|
||||
import '../../../blue/io_tool/io_model.dart';
|
||||
import '../../../blue/io_tool/io_tool.dart';
|
||||
import '../../../blue/io_tool/manager_event_bus.dart';
|
||||
import '../../../blue/sender_manage.dart';
|
||||
import '../../../tools/storage.dart';
|
||||
import 'nearbyLock_state.dart';
|
||||
|
||||
class NearbyLockLogic extends BaseGetXController{
|
||||
@ -15,26 +23,18 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
final NearbyLockState state = NearbyLockState();
|
||||
|
||||
// 点击复合要求的设备之后 连接
|
||||
void connect(String lockId){
|
||||
BlueManage().connect(lockId);
|
||||
void connect(String lockId, String deviceName){
|
||||
BlueManage().connect(lockId, deviceName);
|
||||
}
|
||||
|
||||
// 监听蓝牙连接的状态
|
||||
late StreamSubscription _streamSubscription;
|
||||
void _startListenIO(){
|
||||
_streamSubscription = EventBusManager().eventBus!.on<QualifiedCharacteristic>().listen((event) async {
|
||||
IoSenderManage.getPublicKey(state.seletLockName.value);
|
||||
IoSenderManage.getPublicKey(lockId:state.seletLockName.value);
|
||||
});
|
||||
}
|
||||
|
||||
// 状态在线之后发现服务
|
||||
// Future<void> scanDiscoverServices(String lockId) async {
|
||||
// // 获取特征值 处理特征值
|
||||
// List<DiscoveredService> list = await BlueManage().discoverServices(lockId);
|
||||
// // 发送获取公钥
|
||||
// IoSenderManage.getPublicKey(state.seletLockName.value);
|
||||
// }
|
||||
|
||||
// 获取公钥
|
||||
// void getPublicKey(String lockId){
|
||||
// // print("seletGetPublicKey:${lockId}");
|
||||
@ -57,6 +57,146 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
});
|
||||
}
|
||||
|
||||
late StreamSubscription<Reply> _replySubscription;
|
||||
void _initReplySubscription() {
|
||||
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
||||
if(reply is GetPublicKeyReply) {
|
||||
_replyGetPublicKey(reply);
|
||||
}
|
||||
|
||||
if(reply is GetPrivateKeyReply) {
|
||||
_replyGetPrivateKeyKey(reply);
|
||||
}
|
||||
|
||||
if(reply is AddUserReply) {
|
||||
_replyAddUserKey(reply);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _replyGetPublicKey(Reply reply){
|
||||
// 获取公钥
|
||||
switch(reply.status){
|
||||
case 0x00:
|
||||
//成功
|
||||
// 储存公钥
|
||||
var tokenData = reply.data.sublist(3);
|
||||
var saveStrList = changeIntListToStringList(tokenData);
|
||||
Storage.setStringList(saveBluePublicKey, saveStrList);
|
||||
IoSenderManage.getPrivateKey(
|
||||
lockId:IoManager().getCurrentDeviceLockId,
|
||||
keyID:"1",
|
||||
authUserID:"1",
|
||||
nowTime:1,
|
||||
publicKeyData:tokenData,
|
||||
needAuthor:1);
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
|
||||
break;
|
||||
case 0x0f:
|
||||
//用户已存在
|
||||
|
||||
break;
|
||||
default:
|
||||
//失败
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _replyGetPrivateKeyKey(Reply reply){
|
||||
switch(reply.status){
|
||||
case 0x00:
|
||||
//成功
|
||||
print('获取私钥成功');
|
||||
reply.data.removeAt(0);
|
||||
|
||||
// 私钥
|
||||
List<int> privateKey = reply.data.sublist(0, 16);
|
||||
var savePrivateKeyList = changeIntListToStringList(privateKey);
|
||||
Storage.setStringList(saveBluePrivateKey, savePrivateKeyList);
|
||||
|
||||
// signKey
|
||||
List<int> signKey = reply.data.sublist(16);
|
||||
var savesignKeyList = changeIntListToStringList(signKey);
|
||||
Storage.setStringList(saveBlueSignKey, savesignKeyList);
|
||||
|
||||
print("privateKey:$privateKey signKey:$signKey");
|
||||
IoSenderManage.senderAddUser(
|
||||
lockID:IoManager().getCurrentDeviceLockId,
|
||||
authUserID:"100001",
|
||||
keyID:"1",
|
||||
userID:"100001",
|
||||
openMode:1,
|
||||
keyType:1,
|
||||
startDate:0x11223344,
|
||||
expireDate:0x11223344,
|
||||
role:255,
|
||||
password:"123456",
|
||||
needAuthor:1,
|
||||
publicKey:publicKeyDataList,
|
||||
privateKey:privateKey,
|
||||
token: [0,0,0,0]
|
||||
);
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
print('获取私钥无权限');
|
||||
|
||||
break;
|
||||
case 0x0f:
|
||||
//用户已存在
|
||||
print('获取私钥:用户已存在');
|
||||
|
||||
break;
|
||||
default:
|
||||
//失败
|
||||
print('获取私钥失败');
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _replyAddUserKey(Reply reply){
|
||||
var lockId = reply.data.sublist(2, 42);
|
||||
print("lockId:$lockId");
|
||||
var token = reply.data.sublist(42, 46);
|
||||
List<String> strTokenList = changeIntListToStringList(token);
|
||||
Storage.setStringList(saveBlueToken, strTokenList);
|
||||
print("token:$token");
|
||||
int status = reply.data[46];
|
||||
print("status:$status");
|
||||
switch(status){
|
||||
case 0x00:
|
||||
//成功
|
||||
print("添加用户数据解析成功");
|
||||
|
||||
break;
|
||||
case 0x06:
|
||||
//无权限
|
||||
print("需要鉴权");
|
||||
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
print("用户无权限");
|
||||
|
||||
break;
|
||||
case 0x09:
|
||||
// 权限校验错误
|
||||
print("权限校验错误");
|
||||
|
||||
break;
|
||||
default:
|
||||
//失败
|
||||
print("领锁失败");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {
|
||||
// TODO: implement onReady
|
||||
@ -65,6 +205,7 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
|
||||
_startListenIO();
|
||||
_initSendStreamSubscription();
|
||||
_initReplySubscription();
|
||||
}
|
||||
|
||||
@override
|
||||
@ -75,10 +216,8 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
|
||||
// 进来第一步开始扫描
|
||||
BlueManage().startScan((v){
|
||||
bool isHave = state.devices.any((element) => element.id == v[0].id);
|
||||
if (isHave == false){
|
||||
state.devices.addAll(v);
|
||||
}
|
||||
state.devices.clear();
|
||||
state.devices.addAll(v);
|
||||
});
|
||||
}
|
||||
|
||||
@ -88,6 +227,7 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
super.onClose();
|
||||
_streamSubscription.cancel();
|
||||
_sendStreamSubscription.cancel();
|
||||
_replySubscription.cancel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ class _NearbyLockPageState extends State<NearbyLockPage> {
|
||||
// Navigator.pushNamed(context, Routers.lockAddressPage);
|
||||
// logic.getPublicKey(state.devices[index].serviceUuids[0].toString());
|
||||
state.seletLockName.value = state.devices[index].name;
|
||||
logic.connect(state.devices[index].id);
|
||||
logic.connect(state.devices[index].id, state.devices[index].name);
|
||||
});
|
||||
},
|
||||
separatorBuilder: (BuildContext context, int index) {
|
||||
|
||||
@ -6,6 +6,6 @@ import 'package:get/get.dart';
|
||||
|
||||
class NearbyLockState {
|
||||
|
||||
var devices = <DiscoveredDevice>[].obs;
|
||||
RxList<DiscoveredDevice> devices = <DiscoveredDevice>[].obs;
|
||||
var seletLockName = "".obs;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user