Commit d1d27272 authored by lyong's avatar lyong

增加EVM授权

parent 25754d9e
......@@ -28,11 +28,16 @@ const cmdTable = [
// { kw: 'WRITE_LIC', fn: doWriteLic },
{ kw: 'GET_CRC', fn: doGetCrc },
{ kw: 'TEST_END', fn: doEnd },
{ kw: 'RTC', fn: doGetRtc },
{ kw: 'AT+RTC', fn: doGetRtc },
{ kw: 'WRITE_RTC', fn: doSetRtc },
{ kw: 'READ_HOSTID', fn: doReadHostId },
// { kw: 'WRITE_HOSTID', fn: doWriteHostId },
{ kw: 'GET_HOSTID', fn: dogetHostIdSumCrc },
];
const cmdTableHex = [
{ kw: '57524954455F4C4943', fn: doWriteLic }, //WRITE_LIC
{ kw: '41542B57524954455F484F53544944', fn: doWriteHostId }, //WRITE_LIC
];
/* ================= 2. 统一入口:模糊匹配================= */
......@@ -60,11 +65,9 @@ function doStart(fd) {
function doGetRtc(fd) {
var time = "+" + autoUtil.getRtcTimer()
console.log('RTC:' + time);
send(fd, time);
}
function doSetRtc(fd, recvBuf) {
console.log("doSetRtc " + recvBuf);
autoUtil.setRtc(recvBuf);
if (doSetRtc._rtcTimerOut === undefined) {
......@@ -227,6 +230,38 @@ function doKeyBz(fd, recvBuf) {
touchFlag = 1;
// send(fd, '+OK');
}
function doReadHostId(fd) {
var ret = "+" + autoUtil.getHostid();
send(fd, ret);
}
function doWriteHostId(fd, recvBuf) {
const rcv = autoUtil.readHostIdData(recvBuf);
console.log("!!rvc.code:"+ rcv.code+" rcv.msg:"+rcv.msg+" rcv.data:"+rcv.data)
switch (rcv.code) {
case 1: // 已缓存
send(fd, '+OK');
console.log('readHostId msg:'+ rcv.msg);
break;
case 3: // 末段完成
send(fd, '+OK');
autoUtil.gotoWriteHostIdData(rcv.data); // 写入 /tmp/hostid.txt
break;
case 0: // 解析失败
default:
console.log('readHostId msg:'+ rcv.msg);
send(fd, '+FAIL');
break;
}
}
function dogetHostIdSumCrc(fd) {
var ret = "+" + autoUtil.verifyHostIDChecksum();
send(fd, ret);
}
// 想加 TOUCH4、5…直接往后写
// 3 键就是 0b111
let collectMask = 0;
......
......@@ -166,13 +166,14 @@ function setBright(vlaue) {
}
function getRtcTimer() {
var partFilePath = "../../../../../dev/rtc1";
if (!fs.exists(partFilePath)) {
return "NA";
var partFilePath = "/dev/rtc1";
if (!fs.exists("../../../../.." + partFilePath)) {
return "FAIL";
}
system('hwclock -f /dev/rtc1 > /tmp/hwRtc.txt');
var str = "hwclock -f " + partFilePath + " > /tmp/hwRtc.txt"
system(str);
const raw = fs.read('/tmp/hwRtc.txt');
if (!raw) return 'NA';
if (!raw) return 'FAIL';
/* 典型行:Thu Jan 1 18:22:21 1970 0.000000 seconds
只处理前 4 段:星期 月 日 时间 年 */
......@@ -406,7 +407,7 @@ function licParce(recvBuf) {
let coreT = titleBuf.slice(posT + equal.length);
// console.log("coreT:" + coreT);
const slashPos = coreT.indexOf('2F');
if (slashPos === -1) { return 0; }
if (slashPos === 1) { return 0; }
const segHex = coreT.slice(0, slashPos); // 例如 "31"
const allArrLicVal = coreT.slice(slashPos + 2, slashPos + 6); // 总段数
console.log("allArrLicVal:" + allArrLicVal);
......@@ -423,7 +424,7 @@ function licParce(recvBuf) {
if (core.length !== expectedHexLen) {
console.log(`长度错误:期望 ${expectedHexLen},实际 ${core.length}`);
return -2;
return 2;
} else {
console.log(`长度正确:期望 ${expectedHexLen},实际 ${core.length}`);
}
......@@ -596,13 +597,14 @@ function getMacAddressFromLicenseFile() {
function setRtc(iso) {
console.log("setRtc:" + iso);
console.log("!!setRtc:" + iso);
// const iso = "1999-10-03 19:50:00";
const pure = iso.replace(/^AT\+WRITE_RTC=/, '');
// 一次性解析
const [datePart, timePart] = iso.split(' ');
const [datePart, timePart] = pure.split(' ');
const [year, month, day] = datePart.split('-').map(Number);
const [hour, minute, sec] = timePart.split(':').map(Number);
console.log("!!-----" + year +"," +month +","+ day +","+ hour +","+ minute +","+ sec);
console.log("!!-----:" + year + "," + month + "," + day + "," + hour + "," + minute + "," + sec);
// 写入 RTC
var librtc = require("rtc.js");
librtc.ctrl("set", {
......@@ -614,6 +616,160 @@ function setRtc(iso) {
second: sec // 这里用解析出来的秒(示例 00)
});
}
function getHostid() {
// root@TinaLinux:/# cat ./tmp/hostid.txt
// 3469984A8F071DE627757D3CBAE131C1root@TinaLinux:/#
var filePath1 = "/tmp/hostid.txt";
var filePath2 = "/mnt/extsd/hostid.txt";
var readfile;
if (!fs.exists("../../../../.." + filePath1)) {
if (!fs.exists("../../../../.." + filePath2)) {
return "FAIL";
}
readfile = filePath2;
} else {
readfile = filePath1;
}
console.log("getHostid readfile: " + readfile);
const raw = fs.read(readfile, "r");
if (!raw) return 'FAIL';
// 去掉首尾空白、换行、回车
return raw.trim() || 'FAIL';
}
/* ========== 全局缓存 ========== */
var hostIdSegArray = []; // 下标 = 段号-1
/**
* 解析 HostId 分段数据
* @param {string} recvBuf 原始 HEX 字符串
* @returns {Object} {code:0|1|3, data:'', msg:''}
* code=0 格式错误
* 1 已缓存(非末段)
* 3 末段已齐并返回完整 HEX
*/
function readHostIdData(recvBuf) {
const HDR_HEX = '41542B57524954455F484F53544944'; // "AT+WRITE_HOSTID"
const EQUAL = '3D';
const TAIL = '0D0D0A0A';
if (!recvBuf) return { code: 0, data: '', msg: 'empty buffer' };
/* 1. 头 */
const hdrPos = recvBuf.indexOf(HDR_HEX);
if (hdrPos === -1) return { code: 0, data: '', msg: 'header not found' };
let core = recvBuf.slice(hdrPos + HDR_HEX.length);
/* 2. = */
const eqPos = core.indexOf(EQUAL);
if (eqPos === -1) return { code: 0, data: '', msg: 'equal not found' };
core = core.slice(eqPos + EQUAL.length);
/* 3. 去尾 */
if (core.endsWith(TAIL)) core = core.slice(0, -TAIL.length);
/* 4. 段号 01/08 */
const slashPos = core.indexOf('2F');
if (slashPos === -1) return { code: 0, data: '', msg: 'slash not found' };
const curSegHex = core.slice(0, slashPos);
const totSegHex = core.slice(slashPos + 2, slashPos + 6);
const curSeg = hexToAscii(curSegHex);
const totSeg = hexToAscii(totSegHex);
if (isNaN(curSeg) || isNaN(totSeg)) return { code: 0, data: '', msg: 'seg parse fail' };
/* 5. 数据 */
const hexData = core.slice(slashPos + 6).replace(/\s+/g, '');
if (!hexData) return { code: 0, data: '', msg: 'no data' };
/* 6. 缓存 */
hostIdSegArray[curSeg - 1] = hexData;
/* 7. 末段判断 */
if (curSeg === totSeg) {
const fullHex = hostIdSegArray.join('');
hostIdSegArray = []; // 清空
return { code: 3, data: fullHex, msg: 'completed' };
}
return { code: 1, data: '', msg: `segment ${curSeg}/${totSeg} cached` };
}
function gotoWriteHostIdData(fullHex) {
console.log("fullHex: " + fullHex);
if (gotoWriteHostIdData._timeOutId == undefined) {
gotoWriteHostIdData._timeOutId = null;
}
// 清理之前的定时器
if (gotoWriteHostIdData._timeOutId) {
clearTimeout(gotoWriteHostIdData._timeOutId);
gotoWriteHostIdData._timeOutId = null;
}
// // 设置新的定时器
gotoWriteHostIdData._timeOutId = setTimeout(() => {
const hostid = getHostid();
const fileName = "/tmp/" + hostid + ".lic";
var hexLen = fullHex.length;
var result = uart.writeData(fullHex, hexLen, fileName);
if (result == 1) {
console.log("Data written successfully.");
} else {
console.log("Failed to write data.");
}
system("sync");
system(SECURE_APP_PATH + " -b " + fileName);
// hostIdSegArray = []; // 清空缓存
// console.log('HostId 已全部写入:', recLic);
}, 100);
}
/**
* 计算 HostID 校验和
* @param {string} s 输入字符串(已 trim)
* @returns {string} 返回 "0xXX" 格式校验和
*/
function verifyHostIDChecksum() {
var hostid = getHostid();
var fileName = hostid + ".lic"
console.log("HostId fileName: " + fileName);
const filePath1 = "/tmp/" + fileName;
const filePath2 = "/mnt/extsd/" + fileName;
let readFile = null;
if (fs.exists("../../../../.." + filePath1)) readFile = filePath1;
else if (fs.exists("../../../../.." + filePath2)) readFile = filePath2;
if (!readFile) return "0X00"; // 统一错误格式
console.log("verifyHostIDChecksum readFile: " + readFile);
const raw = fs.read(readFile, "r");
console.log("verifyHostIDChecksum raw: " + raw);
return raw ? calculateSumChecksum(raw) : "0X00";
}
/**
* 计算字符串校验和(跳过 '-',取低 8 位)
* @param {string} s 输入字符串
* @returns {string} 返回 "0xXX" 格式校验和
*/
function calculateSumChecksum(s) {
if (!s) return "0X00";
let sum = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] !== '-') sum += s.charCodeAt(i) & 0xFF;
}
return "0X" + (sum & 0xFF).toString(16).padStart(2, '0').toUpperCase();
}
function system(cmd) {
systemCtrl.executeSystemCommand(cmd)
}
......@@ -650,5 +806,9 @@ module.exports = {
getWifiInfo: getWifiInfo,
gotoWriteLic: gotoWriteLic,
getMacAddressFromLicenseFile: getMacAddressFromLicenseFile,
setRtc: setRtc
setRtc: setRtc,
getHostid: getHostid,
readHostIdData: readHostIdData,
gotoWriteHostIdData: gotoWriteHostIdData,
verifyHostIDChecksum: verifyHostIDChecksum
}
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