Commit 719b5a97 authored by lyong's avatar lyong

feat:新增指定长度授权方式

parent 1261225a
...@@ -381,63 +381,102 @@ function getpectedHexLen(seg) { ...@@ -381,63 +381,102 @@ function getpectedHexLen(seg) {
function licParce(recvBuf) { function licParce(recvBuf) {
var comallArrLicVal = '4954455F4C4943'; var comallArrLicVal = '4954455F4C4943'; // "AT+WRITE_LIC"
// var allArrLicVal = '3332'; // 外部可改,总段号(16 进制字符串) var equal = '3D'; // "="
var equal = '3D'; var slash = '2F'; // "/"
// console.log('licParce recv:' + recvBuf);
console.log('licParce recv:' + recvBuf);
var buf = recvBuf; var buf = recvBuf;
var titleBuf = recvBuf; var titleBuf = recvBuf;
/* 1. 以 comallArrLicVal 为分隔符劈成两段 */ /* 1. 以 comallArrLicVal 为分隔符劈成两段 */
const pos = buf.indexOf(comallArrLicVal); const pos = buf.indexOf(comallArrLicVal);
// console.log("pos:" + pos)
if (pos === -1) return 0; if (pos === -1) return 0;
let core = buf.slice(pos + comallArrLicVal.length + 12); // 后半段 let core = buf.slice(pos + comallArrLicVal.length + 12); // 后半段
// console.log("!!!!1_ core:" + core)
// console.log("1--core:" + core);
/* 2. 去尾 D0D0A0A */ /* 2. 去尾 D0D0A0A */
// if (core.endsWith('D0D0A0A'))
core = core.slice(0, -8); core = core.slice(0, -8);
console.log("recv:" + core); // console.log("recv:" + core);
/* 3. 解析帧头 */
const posT = titleBuf.indexOf(equal); const posT = titleBuf.indexOf(equal);
let coreT = titleBuf.slice(posT + equal.length); let coreT = titleBuf.slice(posT + equal.length);
// console.log("coreT:" + coreT); const slashPos = coreT.indexOf(slash);
const slashPos = coreT.indexOf('2F'); if (slashPos === -1) return 0;
if (slashPos === 1) { return 0; }
const segHex = coreT.slice(0, slashPos); // 例如 "31" const segHex = coreT.slice(0, slashPos); // 例如 "01"
const allArrLicVal = coreT.slice(slashPos + 2, slashPos + 6); // 总段数 const totSegHex = coreT.slice(slashPos + 2, slashPos + 6); // 总段数
console.log("allArrLicVal:" + allArrLicVal);
// Hex 字符串当成 ASCII 解码
// Hex → 字节 → ASCII → 数字
// const segChar = String.fromCharCode(parseInt(segHex, 16)); // '1'
const curSeg = hexToAscii(segHex); const curSeg = hexToAscii(segHex);
const totSeg = hexToAscii(allArrLicVal); const totSeg = hexToAscii(totSegHex);
console.log("recv index:" + curSeg); console.log("recv index:" + curSeg);
console.log("all index:" + totSeg); console.log("all index:" + totSeg);
//总长度数据4096 /* 4. 检查是否有指定长度的数据 */
var expectedHexLen = getpectedHexLen(totSeg); const optionHex = core.slice(0, 2); // "2F"
console.log("optionHex:" + optionHex);
if (optionHex == "2F") {
// 有指定长度的数据
const params = {
core: core,
curSeg: curSeg,
totSeg: totSeg
};
return licParceSpecifiedLength(params);
} else {
// 没有指定长度的数据,使用默认长度
const params = {
core: core,
curSeg: curSeg,
totSeg: totSeg
};
return licParceFixedLength(params);
}
}
if (core.length !== expectedHexLen) { function licParceFixedLength(params) {
console.log(`长度错误:期望 ${expectedHexLen},实际 ${core.length}`); const { core, curSeg, totSeg } = params;
var expectedHexLen = getpectedHexLen(totSeg);
if (core.length !== expectedHexLen) { //实际长度应该 / 2,为16进制长度
console.log(`!!Length error: Expected ${(expectedHexLen / 2)}, but got ${(core.length / 2)}`);
return 2; return 2;
} else { }else{
console.log(`长度正确:期望 ${expectedHexLen},实际 ${core.length}`); console.log(`!!Length is correct: Expected ${(expectedHexLen / 2)}, Actual ${(core.length / 2)}`);
} }
console.log("!!data:" + core);
/* 5. 格式化并缓存 */
const spaced = formatHexWithSpace(core);
licSegArray[curSeg - 1] = spaced;
/* 4. 格式化并缓存 */ console.log(`seg=${curSeg}/${totSeg} data=${spaced}`);
// const spaced = formatHexWithSpace(core); if (curSeg === totSeg) {
const spaced = core; return 3;
}
return 1;
}
function licParceSpecifiedLength(params) {
const { core, curSeg, totSeg } = params;
const lengthHex = core.slice(2, 10);
const lengthSeg = hexToAscii(lengthHex);
const dataLen = parseInt(lengthSeg, 10); // 转换为十进制数字
let data = core.slice(10);
licSegArray[curSeg - 1] = spaced; console.log("dataLen:" + dataLen);
console.log("!!data:" + data);
var dataLength = (data.length / 2)
if (dataLength !== dataLen) {
console.log(`!!Length error: Expected ${dataLen}, but got ${dataLength}`);
return 2;
}else{
console.log(`!!Length is correct: Expected ${dataLen}, Actual ${dataLength}`);
}
/* 5. 格式化并缓存 */
const spaced = formatHexWithSpace(data);
licSegArray[curSeg - 1] = spaced;
console.log(`seg=${curSeg}/${totSeg} data=${spaced}`); console.log(`seg=${curSeg}/${totSeg} data=${spaced}`);
if (curSeg === totSeg) { if (curSeg === totSeg) {
...@@ -726,7 +765,7 @@ function gotoWriteHostIdData(fullHex) { ...@@ -726,7 +765,7 @@ function gotoWriteHostIdData(fullHex) {
system(SECURE_APP_PATH + " -b " + fileName); system(SECURE_APP_PATH + " -b " + fileName);
// hostIdSegArray = []; // 清空缓存 // hostIdSegArray = []; // 清空缓存
// console.log('HostId 已全部写入:', recLic); // console.log('HostId 已全部写入:', recLic);
}, 100); }, 100);
} }
...@@ -816,7 +855,7 @@ function getTPVersionNew() { ...@@ -816,7 +855,7 @@ function getTPVersionNew() {
// 转换为10进制 // 转换为10进制
const decimalValue = parseInt(version, 16); const decimalValue = parseInt(version, 16);
return decimalValue.toString(); return decimalValue.toString();
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment