115 lines
3.2 KiB
Lua
115 lines
3.2 KiB
Lua
-- cmd.lua
|
||
-- 功能:解析串口帧,调用 linksocket 接口,执行系统命令
|
||
|
||
local log = require "log"
|
||
local sys = require "sys"
|
||
local uart = require "uart"
|
||
local linksocket = require "linksocket"
|
||
|
||
local uartID = 1
|
||
|
||
-- 辅助函数:字节转十六进制(调试用)
|
||
local function toHexcode(str)
|
||
if not str then return "" end
|
||
local hex = ""
|
||
for i = 1, #str do
|
||
hex = hex .. string.format("%02X", str:byte(i))
|
||
end
|
||
return hex
|
||
end
|
||
|
||
-- 发送通用响应帧:55 AA 83 subCmd socketId status AA 55
|
||
local function sendResponse(subCmd, socketId, status)
|
||
local frame = string.char(0x55, 0xAA, 0x83, subCmd, socketId, status, 0xAA, 0x55)
|
||
uart.write(uartID, frame)
|
||
log.debug("Response: subCmd=", subCmd, "socketId=", socketId, "status=", status)
|
||
end
|
||
|
||
-- 命令处理函数
|
||
local function handleCmd01(subCmd, payload)
|
||
local socketId = subCmd
|
||
if socketId < 1 or socketId > 6 then
|
||
log.error("Invalid socket id in cmd 0x01:", socketId)
|
||
sendResponse(0x01, socketId, 1)
|
||
return
|
||
end
|
||
local success = linksocket.sendToSocket(socketId, payload)
|
||
if not success then
|
||
log.warn("Send failed, socket", socketId, "not connected")
|
||
sendResponse(0x01, socketId, 1)
|
||
else
|
||
log.info("Data sent to socket", socketId, "len=", #payload)
|
||
end
|
||
end
|
||
|
||
local function handleCmd02()
|
||
log.info("System restart command received")
|
||
sys.restart()
|
||
end
|
||
|
||
local function handleCmd03(subCmd, payload)
|
||
if #payload < 1 then
|
||
log.error("Cmd 0x03 missing socket id")
|
||
return
|
||
end
|
||
local socketId = payload:byte(1)
|
||
if socketId < 1 or socketId > 6 then
|
||
log.error("Invalid socket id in cmd 0x03:", socketId)
|
||
sendResponse(subCmd, socketId, 1)
|
||
return
|
||
end
|
||
|
||
if subCmd == 0x01 then
|
||
local ok = linksocket.setKeepConnecting(socketId, true)
|
||
if ok then
|
||
sendResponse(subCmd, socketId, 3)
|
||
else
|
||
sendResponse(subCmd, socketId, 1)
|
||
end
|
||
elseif subCmd == 0x02 then
|
||
local ok = linksocket.setKeepConnecting(socketId, false)
|
||
if ok then
|
||
sendResponse(subCmd, socketId, 0)
|
||
else
|
||
sendResponse(subCmd, socketId, 1)
|
||
end
|
||
else
|
||
log.error("Unknown subCmd for 0x03:", subCmd)
|
||
sendResponse(subCmd, socketId, 1)
|
||
end
|
||
end
|
||
|
||
-- 对外接口:处理串口接收到的原始数据
|
||
local function process(rawData)
|
||
if not rawData or #rawData < 5 then
|
||
log.warn("Short data received, ignore")
|
||
return
|
||
end
|
||
|
||
log.info("CMD raw data:", toHexcode(rawData))
|
||
|
||
if rawData:byte(1) == 0x55 and rawData:byte(2) == 0xAA
|
||
and rawData:byte(-2) == 0xAA and rawData:byte(-1) == 0x55 then
|
||
local mainCmd = rawData:byte(3)
|
||
local subCmd = rawData:byte(4)
|
||
local payload = rawData:sub(5, -3)
|
||
|
||
if mainCmd == 0x01 then
|
||
handleCmd01(subCmd, payload)
|
||
elseif mainCmd == 0x02 then
|
||
handleCmd02()
|
||
elseif mainCmd == 0x03 then
|
||
handleCmd03(subCmd, payload)
|
||
else
|
||
log.warn("Unknown main command:", mainCmd)
|
||
end
|
||
else
|
||
log.warn("Invalid frame header/footer")
|
||
end
|
||
end
|
||
|
||
-- 模块接口
|
||
local M = {
|
||
process = process,
|
||
}
|
||
return M |