sys.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. --- 模块功能:luat协程调度框架
  2. -- @module sys
  3. -- @author 稀饭放姜 小强
  4. -- @license MIT
  5. -- @copyright openLuat.com
  6. -- @release 2017.9.13
  7. require "patch"
  8. require "errdump"
  9. require "log"
  10. module(..., package.seeall)
  11. -- lib脚本版本号,只要lib中的任何一个脚本做了修改,都需要更新此版本号
  12. SCRIPT_LIB_VER = "2.0.0"
  13. -- 脚本发布时的最新core软件版本号
  14. CORE_MIN_VER = "Luat_V0010_8955"
  15. -- TaskID最大值
  16. local TASK_TIMER_ID_MAX = 0x1FFFFFFF
  17. -- msgId 最大值(请勿修改否则会发生msgId碰撞的危险)
  18. local MSG_TIMER_ID_MAX = 0x7FFFFFFF
  19. -- 任务定时器id
  20. local taskTimerId = 0
  21. -- 消息定时器id
  22. local msgId = TASK_TIMER_ID_MAX
  23. -- 定时器id表
  24. local timerPool = {}
  25. local taskTimerPool = {}
  26. --消息定时器参数表
  27. local para = {}
  28. --定时器是否循环表
  29. local loop = {}
  30. --- 启动GSM协议栈。例如在充电开机未启动GSM协议栈状态下,如果用户长按键正常开机,此时调用此接口启动GSM协议栈即可
  31. -- @return 无
  32. -- @usage sys.powerOn()
  33. function powerOn()
  34. rtos.poweron(1)
  35. end
  36. --- 软件重启
  37. -- @string r 重启原因,用户自定义,一般是string类型,重启后的trace中会打印出此重启原因
  38. -- @return 无
  39. -- @usage sys.restart('程序超时软件重启')
  40. function restart(r)
  41. assert(r and r ~= "", "sys.restart cause null")
  42. errdump.appendErr("restart[" .. r .. "];")
  43. rtos.restart()
  44. end
  45. --- Task任务延时函数,只能用于任务函数中
  46. -- @number ms 整数,最大等待126322567毫秒
  47. -- @return number 正常返回1,失败返回nil
  48. -- @usage sys.wait(30)
  49. function wait(ms)
  50. -- 参数检测,参数不能为负值
  51. assert(ms > 0, "The wait time cannot be negative!")
  52. -- 选一个未使用的定时器ID给该任务线程
  53. if taskTimerId >= TASK_TIMER_ID_MAX then taskTimerId = 0 end
  54. taskTimerId = taskTimerId + 1
  55. local timerid = taskTimerId
  56. taskTimerPool[coroutine.running()] = timerid
  57. timerPool[timerid] = coroutine.running()
  58. -- 调用core的rtos定时器
  59. if 1 ~= rtos.timer_start(timerid, ms) then log.debug("rtos.timer_start error") return end
  60. -- 挂起调用的任务线程
  61. local message = {coroutine.yield()}
  62. if #message ~= 0 then
  63. rtos.timer_stop(timerid)
  64. taskTimerPool[coroutine.running()] = nil
  65. timerPool[timerid] = nil
  66. return unpack(message)
  67. end
  68. end
  69. --- Task任务的条件等待函数(包括事件消息和定时器消息等条件),只能用于任务函数中。
  70. -- @param id 消息ID
  71. -- @number ms 等待超时时间,单位ms,最大等待126322567毫秒
  72. -- @return result 接收到消息返回true,超时返回false
  73. -- @return data 接收到消息返回消息参数
  74. -- @usage result, data = sys.waitUntil("SIM_IND", 120000)
  75. function waitUntil(id, ms)
  76. subscribe(id, coroutine.running())
  77. local message = ms and {wait(ms)} or {coroutine.yield()}
  78. unsubscribe(id, coroutine.running())
  79. return message[1] ~= nil, unpack(message, 2, #message)
  80. end
  81. --- 创建一个任务线程,在模块最末行调用该函数并注册模块中的任务函数,main.lua导入该模块即可
  82. -- @param fun 任务函数名,用于resume唤醒时调用
  83. -- @param ... 任务函数fun的可变参数
  84. -- @return co 返回该任务的线程号
  85. -- @usage sys.taskInit(task1,'a','b')
  86. function taskInit(fun, ...)
  87. local co = coroutine.create(fun)
  88. coroutine.resume(co, unpack(arg))
  89. return co
  90. end
  91. --[[
  92. 检查底层软件版本号和lib脚本需要的最小底层软件版本号是否匹配
  93. ]]
  94. local function checkCoreVer()
  95. local realver = rtos.get_version()
  96. --如果没有获取到底层软件版本号
  97. if not realver or realver == "" then
  98. errdump.appendErr("checkCoreVer[no core ver error];")
  99. return
  100. end
  101. local buildver = string.match(realver, "Luat_V(%d+)_")
  102. --如果底层软件版本号格式错误
  103. if not buildver then
  104. errdump.appendErr("checkCoreVer[core ver format error]" .. realver .. ";")
  105. return
  106. end
  107. --lib脚本需要的底层软件版本号大于底层软件的实际版本号
  108. if tonumber(string.match(CORE_MIN_VER, "Luat_V(%d+)_")) > tonumber(buildver) then
  109. log.info("ril.checkCoreVer" .. realver .. "," .. CORE_MIN_VER .. ";")
  110. end
  111. end
  112. --- Luat平台初始化
  113. -- @param mode 充电开机是否启动GSM协议栈,1不启动,否则启动
  114. -- @param lprfnc 用户应用脚本中定义的“低电关机处理函数”,如果有函数名,则低电时,本文件中的run接口不会执行任何动作,否则,会延时1分钟自动关机
  115. -- @return 无
  116. -- @usage sys.init(1,0)
  117. function init(mode, lprfnc)
  118. -- 用户应用脚本中必须定义PROJECT和VERSION两个全局变量,否则会死机重启,如何定义请参考各个demo中的main.lua
  119. assert(PROJECT and PROJECT ~= "" and VERSION and VERSION ~= "", "Undefine PROJECT or VERSION")
  120. collectgarbage("setpause", 80)
  121. -- 设置AT命令的虚拟串口
  122. uart.setup(uart.ATC, 0, 0, uart.PAR_NONE, uart.STOP_1)
  123. log.info("poweron reason:", rtos.poweron_reason(), PROJECT, VERSION, SCRIPT_LIB_VER, rtos.get_version())
  124. if mode == 1 then
  125. -- 充电开机
  126. if rtos.poweron_reason() == rtos.POWERON_CHARGER then
  127. -- 关闭GSM协议栈
  128. rtos.poweron(0)
  129. end
  130. end
  131. -- 如果存在脚本运行错误文件,打开文件,打印错误信息
  132. local f = io.open("/luaerrinfo.txt", "r")
  133. if f then
  134. log.error(f:read("*a") or "")
  135. f:close()
  136. end
  137. -- 打印LIB_ERR_FILE文件中的错误信息
  138. errdump.initErr()
  139. checkCoreVer()
  140. end
  141. ------------------------------------------ rtos消息回调处理部分 ------------------------------------------
  142. --[[
  143. 函数名:cmpTable
  144. 功能 :比较两个table的内容是否相同,注意:table中不能再包含table
  145. 参数 :
  146. t1:第一个table
  147. t2:第二个table
  148. 返回值:相同返回true,否则false
  149. ]]
  150. local function cmpTable(t1, t2)
  151. if not t2 then return #t1 == 0 end
  152. if #t1 == #t2 then
  153. for i = 1, #t1 do
  154. if unpack(t1, i, i) ~= unpack(t2, i, i) then
  155. return false
  156. end
  157. end
  158. return true
  159. end
  160. return false
  161. end
  162. --- 关闭定时器
  163. -- @param val 值为number时,识别为定时器ID,值为回调函数时,需要传参数
  164. -- @param ... val值为函数时,函数的可变参数
  165. -- @return 无
  166. -- @usage timer_stop(1)
  167. function timer_stop(val, ...)
  168. -- val 为定时器ID
  169. if type(val) == 'number' then
  170. timerPool[val], para[val], loop[val] = nil
  171. rtos.timer_stop(val)
  172. else
  173. for k, v in pairs(timerPool) do
  174. -- 回调函数相同
  175. if type(v) == 'table' and v.cb == val or v == val then
  176. -- 可变参数相同
  177. if cmpTable(arg, para[k]) then
  178. rtos.timer_stop(k)
  179. timerPool[k], para[k], loop[val] = nil
  180. break
  181. end
  182. end
  183. end
  184. end
  185. end
  186. --- 函数功能--开启一个定时器
  187. -- @param fnc 定时器回调函数
  188. -- @number ms 整数,最大定时126322567毫秒
  189. -- @param ... 可变参数 fnc的参数
  190. -- @return number 定时器ID,如果失败,返回nil
  191. function timer_start(fnc, ms, ...)
  192. --回调函数和时长检测
  193. assert(fnc ~= nil, "sys.timer_start(first param) is nil !")
  194. assert(ms > 0, "sys.timer_start(Second parameter) is <= zero !")
  195. -- 关闭完全相同的定时器
  196. if arg.n == 0 then
  197. timer_stop(fnc)
  198. else
  199. timer_stop(fnc, unpack(arg))
  200. end
  201. -- 为定时器申请ID,ID值 1-20 留给任务,20-30留给消息专用定时器
  202. while true do
  203. if msgId >= MSG_TIMER_ID_MAX then msgId = TASK_TIMER_ID_MAX end
  204. msgId = msgId + 1
  205. if timerPool[msgId] == nil then
  206. timerPool[msgId] = fnc
  207. break
  208. end
  209. end
  210. --调用底层接口启动定时器
  211. if rtos.timer_start(msgId, ms) ~= 1 then log.debug("rtos.timer_start error") return end
  212. --如果存在可变参数,在定时器参数表中保存参数
  213. if arg.n ~= 0 then
  214. para[msgId] = arg
  215. end
  216. --返回定时器id
  217. return msgId
  218. end
  219. --- 函数功能--开启一个循环定时器
  220. -- @param fnc 定时器回调函数
  221. -- @number ms 整数,最大定时126322567毫秒
  222. -- @param ... 可变参数 fnc的参数
  223. -- @return number 定时器ID,如果失败,返回nil
  224. function timer_loop_start(fnc,ms,...)
  225. local tid = timer_start(fnc,ms,unpack(arg))
  226. if tid then loop[tid] = ms end
  227. return tid
  228. end
  229. --- 函数功能--判断某个定时器是否处于开启状态
  230. -- @param val 有两种形式
  231. --一种是开启定时器时返回的定时器id,此形式时不需要再传入可变参数...就能唯一标记一个定时器
  232. --另一种是开启定时器时的回调函数,此形式时必须再传入可变参数...才能唯一标记一个定时器
  233. -- @param ... 可变参数
  234. -- @return number 开启状态返回true,否则nil
  235. function timer_is_active(val,...)
  236. if type(val) == "number" then
  237. return timerPool[val]
  238. else
  239. for k,v in pairs(timerPool) do
  240. if v == val then
  241. if cmpTable(arg,para[k]) then return true end
  242. end
  243. end
  244. end
  245. end
  246. ------------------------------------------ LUA应用消息订阅/发布接口 ------------------------------------------
  247. -- 订阅者列表
  248. local subscribers = {}
  249. --内部消息队列
  250. local messageQueue = {}
  251. --- 订阅消息
  252. -- @param id 消息id
  253. -- @param callback 消息回调处理
  254. -- @usage subscribe("NET_STATUS_IND", callback)
  255. function subscribe(id, callback)
  256. if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
  257. log.warn("warning: sys.subscribe invalid parameter", id, callback)
  258. return
  259. end
  260. if not subscribers[id] then subscribers[id] = {} end
  261. subscribers[id][callback] = true
  262. end
  263. --- 取消订阅消息
  264. -- @param id 消息id
  265. -- @param callback 消息回调处理
  266. -- @usage unsubscribe("NET_STATUS_IND", callback)
  267. function unsubscribe(id, callback)
  268. if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
  269. log.warn("warning: sys.unsubscribe invalid parameter", id, callback)
  270. return
  271. end
  272. if subscribers[id] then subscribers[id][callback] = nil end
  273. end
  274. --- 发布内部消息,存储在内部消息队列中
  275. -- @param ... 可变参数,用户自定义
  276. -- @return 无
  277. -- @usage publish("NET_STATUS_IND")
  278. function publish(...)
  279. table.insert(messageQueue, arg)
  280. end
  281. -- 分发消息
  282. local function dispatch()
  283. while true do
  284. if #messageQueue == 0 then
  285. break
  286. end
  287. local message = table.remove(messageQueue, 1)
  288. if subscribers[message[1]] then
  289. for callback, _ in pairs(subscribers[message[1]]) do
  290. if type(callback) == "function" then
  291. callback(unpack(message, 2, #message))
  292. elseif type(callback) == "thread" then
  293. coroutine.resume(callback, unpack(message))
  294. end
  295. end
  296. end
  297. end
  298. end
  299. -- rtos消息回调
  300. local handlers = {}
  301. setmetatable(handlers, {__index = function() return function() end end, })
  302. --- 注册rtos消息回调处理函数
  303. -- @number id 消息类型id
  304. -- @param handler 消息处理函数
  305. -- @return 无
  306. -- @usage rtos.on(rtos.MSG_KEYPAD, function(param) handle keypad message end)
  307. rtos.on = function(id, handler)
  308. handlers[id] = handler
  309. end
  310. ------------------------------------------ Luat 主调度框架 ------------------------------------------
  311. --- run()从底层获取core消息并及时处理相关消息,查询定时器并调度各注册成功的任务线程运行和挂起
  312. -- @return 无
  313. -- @usage sys.run()
  314. function run()
  315. while true do
  316. -- 分发内部消息
  317. dispatch()
  318. -- 阻塞读取外部消息
  319. local msg, param = rtos.receive(rtos.INF_TIMEOUT)
  320. -- 判断是否为定时器消息,并且消息是否注册
  321. if msg == rtos.MSG_TIMER and timerPool[param] then
  322. if param < TASK_TIMER_ID_MAX then
  323. local taskId = timerPool[param]
  324. timerPool[param] = nil
  325. if taskTimerPool[taskId] == param then
  326. taskTimerPool[taskId] = nil
  327. coroutine.resume(taskId)
  328. end
  329. else
  330. local cb = timerPool[param]
  331. --如果不是循环定时器,从定时器id表中删除此定时器
  332. if not loop[param] then timerPool[param] = nil end
  333. if para[param] ~= nil then
  334. cb(unpack(para[param]))
  335. if not loop[param] then para[param] = nil end
  336. else
  337. cb()
  338. end
  339. --如果是循环定时器,继续启动此定时器
  340. if loop[param] then rtos.timer_start(param,loop[param]) end
  341. end
  342. --其他消息(音频消息、充电管理消息、按键消息等)
  343. elseif type(msg) == "number" then
  344. handlers[msg](param)
  345. else
  346. handlers[msg.id](msg)
  347. end
  348. end
  349. end
  350. require "clib"