configNvm.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. --- 模块功能:参数存储功能测试.
  2. -- @author openLuat
  3. -- @module nvm.testNvm
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.27
  7. module(...,package.seeall)
  8. require"config"
  9. require"nvm"
  10. require"sim"
  11. require"misc"
  12. require"pmd"
  13. require"lmath"
  14. local device_config_init_timer_id = nil
  15. local init_sim_config_result = false
  16. local init_module_config_result = false
  17. local device_config_init_loop_max_times = 1000
  18. local device_config_init_loop_times = 0
  19. local device_config_init_loop_interval = 100
  20. nvm.init("config.lua")
  21. local moduleModel = nvm.get("moduleModel")
  22. --开机次数+1
  23. nvm.set("bootNum", nvm.get("bootNum") + 1)
  24. local devStart = nvm.get("devStart")
  25. if devStart == false then
  26. ----log.info("configNvm","第一次上电开机")
  27. nvm.set("devStart", true)
  28. ----log.info("configNvm","开机信息已经记录")
  29. ----sys.timerStart(rtos.restart, 5000)
  30. else
  31. ----log.info("configNvm","第 "..nvm.get("bootNum").." 次上电开机")
  32. ----nvm.restore()
  33. ----log.info("configNvm","已经恢复出厂设置")
  34. ----sys.timerStart(rtos.restart, 5000)
  35. end
  36. sys.timerLoopStart(function()
  37. if devStart == false then
  38. log.info("configNvm","第一次上电开机")
  39. --nvm.set("devStart", true)
  40. if nvm.get("devStart") then
  41. log.info("configNvm","开机信息已经记录")
  42. end
  43. --sys.timerStart(rtos.restart, 5000)
  44. else
  45. log.info("configNvm","第 "..nvm.get("bootNum").." 次上电开机", "poweron_reason:", rtos.poweron_reason())
  46. --nvm.restore()
  47. --log.info("configNvm","已经恢复出厂设置")
  48. --sys.timerStart(rtos.restart, 5000)
  49. end
  50. end, 5000)
  51. --nvm使用方法示例
  52. --nvm.set("numPara",2)
  53. --nvm.set("boolPara",false)
  54. --nvm.set("tablePara",{"item2-1","item2-2","item2-3"})
  55. --log.info("configNvm.numPara",nvm.get("numPara"))
  56. --log.info("configNvm.boolPara",nvm.get("boolPara"))
  57. --local tableValue = nvm.get("tablePara")
  58. --log.info("configNvm.tablePara",tableValue[1],tableValue[2],tableValue[3])
  59. --[[
  60. --运行指示灯测试
  61. sys.taskInit(function()
  62. --蜂鸣器io初始化
  63. pmd.ldoset(2,pmd.LDO_VMMC)
  64. pins.setup(pio.P0_26, 0)
  65. pmd.ldoset(2,pmd.LDO_VLCD)
  66. while true do
  67. log.info("configNvm.led","-----> start")
  68. sys.wait(1000)
  69. --pins.setup(pio.P0_14, 0)
  70. pins.setup(pio.P0_0, 0) --ctl 灯
  71. pins.setup(pio.P0_18, 1) --net
  72. pins.setup(pio.P0_19, 1) --485
  73. sys.wait(1000)
  74. --pins.setup(pio.P0_14, 1)
  75. pins.setup(pio.P0_0, 1)
  76. pins.setup(pio.P0_18, 0)
  77. pins.setup(pio.P0_19, 0)
  78. end
  79. end)
  80. ]]
  81. --[[
  82. 有些GPIO需要打开对应的ldo电压域之后,才能正常配置工作,电压域和对应的GPIO关系如下
  83. pmd.ldoset(x,pmd.LDO_VSIM1) -- GPIO 29、30、31
  84. pmd.ldoset(x,pmd.LDO_VLCD) -- GPIO 0、1、2、3、4
  85. pmd.ldoset(x,pmd.LDO_VMMC) -- GPIO 24、25、26、27、28
  86. x=0时:关闭LDO
  87. x=1时:LDO输出1.716V
  88. x=2时:LDO输出1.828V
  89. x=3时:LDO输出1.939V
  90. x=4时:LDO输出2.051V
  91. x=5时:LDO输出2.162V
  92. x=6时:LDO输出2.271V
  93. x=7时:LDO输出2.375V
  94. x=8时:LDO输出2.493V
  95. x=9时:LDO输出2.607V
  96. x=10时:LDO输出2.719V
  97. x=11时:LDO输出2.831V
  98. x=12时:LDO输出2.942V
  99. x=13时:LDO输出3.054V
  100. x=14时:LDO输出3.165V
  101. x=15时:LDO输出3.177V
  102. -- 除了上面列举出的GPIO外,其余的GPIO不需要打开特定的电压域,可以直接配置工作
  103. ]]
  104. --三色灯测试
  105. --[[
  106. sys.taskInit(function()
  107. --打开ldo电压域
  108. pmd.ldoset(2,pmd.LDO_VMMC)
  109. while true do
  110. log.info("------> p15 start <---------")
  111. pins.setup(pio.P0_15, 0)
  112. --pins.setup(pio.P0_26, 1)
  113. --pins.setup(pio.P0_25, 1)
  114. sys.wait(2000)
  115. pins.setup(pio.P0_15, 1)
  116. --pins.setup(pio.P0_26, 1)
  117. --pins.setup(pio.P0_25, 1)
  118. sys.wait(2000)
  119. log.info("------> p26 start <---------")
  120. pins.setup(pio.P0_26, 0)
  121. --pins.setup(pio.P0_15, 1)
  122. --pins.setup(pio.P0_25, 1)
  123. sys.wait(2000)
  124. pins.setup(pio.P0_26, 1)
  125. ---pins.setup(pio.P0_15, 1)
  126. --pins.setup(pio.P0_25, 1)
  127. sys.wait(2000)
  128. log.info("------> p25 start <---------")
  129. pins.setup(pio.P0_25, 0)
  130. --pins.setup(pio.P0_15, 1)
  131. --pins.setup(pio.P0_26, 1)
  132. sys.wait(2000)
  133. pins.setup(pio.P0_25, 1)
  134. --pins.setup(pio.P0_15, 1)
  135. --pins.setup(pio.P0_26, 1)
  136. sys.wait(2000)
  137. end
  138. end)
  139. ]]
  140. --切换sim卡
  141. sys.taskInit(function()
  142. while not sim.getId() do
  143. log.info("sim.getId","wait getId ...")
  144. sys.wait(1000)
  145. end
  146. log.info("sim.getId","------>>> simid:", sim.getId())
  147. --[[
  148. while true do
  149. log.info("sim.getId","------>>> simid:", sim.getId())
  150. sys.wait(2000)
  151. end
  152. if sim.getId() ~=0 then
  153. sim.setId(0, function(result)
  154. if result then
  155. --ril.request("AT+SIMCROSS=1")
  156. log.info("sim.setId","setId ok, restart simcross:",sim.getId())
  157. sys.restart("simcross")
  158. end
  159. end)
  160. end
  161. ]]
  162. end)
  163. --[[
  164. --获取设备名称at指令测试
  165. sys.taskInit(function()
  166. while true do
  167. if not moduleModel then
  168. ril.request("AT+CGMM", nil, function(...)
  169. log.info("configNvm","at AT+CGMM resp,data:",...)
  170. end)
  171. end
  172. sys.wait(1000)
  173. end
  174. end)
  175. ]]
  176. --初始化sim卡配置参数
  177. local function init_sim_config()
  178. if not sim.getStatus() then
  179. log.info("configNvm.init_sim_config", "sim.getStatus() fail!")
  180. return false
  181. end
  182. local simIccid = sim.getIccid()
  183. local simImsi = sim.getImsi()
  184. local simMnc = sim.getMnc()
  185. local simMcc = sim.getMcc()
  186. if not simIccid then
  187. log.info("configNvm.init_sim_config", "sim.getIccid() fail!")
  188. return false
  189. end
  190. if not simImsi then
  191. log.info("configNvm.init_sim_config", "sim.getImsi() fail")
  192. return false
  193. end
  194. if not simMnc then
  195. log.info("configNvm.init_sim_config", "sim.getMnc() fail")
  196. return false
  197. end
  198. if not simMcc then
  199. log.info("configNvm.init_sim_config", "sim.getMcc() fail")
  200. return false
  201. end
  202. if nvm.get("simIccid") ~= simIccid then
  203. if not nvm.set("simIccid",simIccid) then
  204. log.info("configNvm.init_sim_config", "nvm.set simIccid fail")
  205. return false
  206. end
  207. end
  208. if nvm.get("simImsi") ~= simImsi then
  209. if not nvm.set("simImsi",simImsi) then
  210. log.info("configNvm.init_sim_config", "nvm.set simImsi fail")
  211. return false
  212. end
  213. end
  214. if nvm.get("simMnc") ~= simMnc then
  215. if not nvm.set("simMnc",simMnc) then
  216. log.info("configNvm.init_sim_config", "nvm.set simMnc fail")
  217. return false
  218. end
  219. end
  220. if nvm.get("simMcc") ~= simMcc then
  221. if not nvm.set("simMcc",simMcc) then
  222. log.info("configNvm.init_sim_config", "nvm.set simMcc fail")
  223. return false
  224. end
  225. end
  226. log.info("configNvm.init_sim_config", "simIccid=", simIccid, "simImsi=", simImsi, "simMnc=", simMnc, "simMcc=", simMcc)
  227. return true
  228. end
  229. --初始化模块配置参数
  230. local function init_module_config()
  231. local moduleImei = misc.getImei()
  232. local moduleSn = misc.getSn()
  233. local moduleMuid = misc.getMuid()
  234. local moduleSoftVersion = misc.getVersion()
  235. if not moduleImei then
  236. log.info("configNvm.init_module_config", "misc.getImei() fail")
  237. return false
  238. end
  239. if not moduleSn then
  240. log.info("configNvm.init_module_config", "misc.getSn() fail")
  241. return false
  242. end
  243. if not moduleMuid then
  244. log.info("configNvm.init_module_config", "misc.getMuid() fail")
  245. return false
  246. end
  247. if not moduleSoftVersion then
  248. log.info("configNvm.init_module_config", "misc.getVersion() fail")
  249. return false
  250. end
  251. if nvm.get("moduleImei") ~= moduleImei then
  252. if not nvm.set("moduleImei",moduleImei) then
  253. log.info("configNvm.init_module_config", "nvm.set moduleImei fail")
  254. return false
  255. end
  256. end
  257. if nvm.get("moduleSn") ~= moduleSn then
  258. if not nvm.set("moduleImei",moduleImei) then
  259. log.info("configNvm.init_module_config", "nvm.set moduleImei fail")
  260. return false
  261. end
  262. end
  263. if nvm.get("moduleMuid") ~= moduleMuid then
  264. if not nvm.set("moduleMuid",moduleMuid) then
  265. log.info("configNvm.init_module_config", "nvm.set moduleMuid fail")
  266. return false
  267. end
  268. end
  269. if nvm.get("moduleSoftVersion") ~= moduleSoftVersion then
  270. if not nvm.set("moduleSoftVersion",moduleSoftVersion) then
  271. log.info("configNvm.init_module_config", "nvm.set moduleSoftVersion fail")
  272. return false
  273. end
  274. end
  275. log.info("configNvm.init_module_config", "moduleImei=", moduleImei, "moduleSn=", moduleSn, "moduleMuid=", moduleMuid, "moduleSoftVersion=", moduleSoftVersion)
  276. return true
  277. end
  278. --初始化设备配置参数
  279. device_config_init_timer_id = sys.timerLoopStart(function ()
  280. if not device_config_init_timer_id then
  281. log.info("configNvm.device_config_init_timer_id", "device_config_init_timer_id empty!")
  282. return false
  283. end
  284. device_config_init_loop_times = device_config_init_loop_times + 1
  285. log.info("configNvm.device_config_init_timer_id","device_config_init_loop_times = ",device_config_init_loop_times)
  286. if device_config_init_loop_times >= device_config_init_loop_max_times then
  287. log.info("configNvm.device_config_init_timer_id", "device_config_init_loop_times >= device_config_init_loop_max_times, device_config_init failed!! 5s later restart device...!! ")
  288. --sys.publish("device_config_init_result",false)
  289. sys.timerStart(rtos.restart, 5000)
  290. sys.timerStop(device_config_init_timer_id)
  291. return false
  292. end
  293. if not init_module_config_result then
  294. log.info("configNvm.device_config_init_timer_id", "init_module_config_result start")
  295. init_module_config_result = init_module_config()
  296. log.info("configNvm.device_config_init_timer_id", "init_module_config_result = ",init_module_config_result)
  297. if not init_module_config_result then
  298. return false
  299. end
  300. end
  301. if not init_sim_config_result then
  302. log.info("configNvm.device_config_init_timer_id", "init_sim_config start")
  303. init_sim_config_result = init_sim_config()
  304. log.info("configNvm.device_config_init_timer_id", "init_sim_config_result = ",init_sim_config_result)
  305. if not init_sim_config_result then
  306. return false
  307. end
  308. end
  309. log.info("configNvm.device_config_init_timer_id", "device_config_init_timer loop stop, device_config_init_timer_id:",device_config_init_timer_id)
  310. sys.publish("device_config_init_result",true)
  311. sys.timerStop(device_config_init_timer_id)
  312. end, device_config_init_loop_interval)
  313. function getRebootTimestamp(devRebootTime)
  314. if not devRebootTime then
  315. log.error("configNvm.getRebootTimestamp","devRebootTime empty!")
  316. return false
  317. end
  318. local _, __, h, m, s = string.find(devRebootTime, "(%d+):(%d+):(%d+)")
  319. if not _ then
  320. log.error("configNvm.getRebootTimestamp","devRebootTime format error! devRebootTime:", devRebootTime)
  321. return false
  322. end
  323. local tm = os.date("*t")
  324. local rebootTimestamp = os.time({year = tm.year, month = tm.month, day = tm.day, hour = h, min = m, sec = s})
  325. return rebootTimestamp
  326. end
  327. --设备定时自动重启任务
  328. sys.taskInit(function()
  329. if not sys.waitUntil("TIMER_SYNC_SUCCESS", 3600*1000) then
  330. log.error("configNvm.auto_reboot_task","wait timer sync timeout,reboot now ...")
  331. sys.restart("configNvm wait timer sync timeout")
  332. end
  333. while true do
  334. local now = os.time()
  335. local devRebootTime = nvm.get("devRebootTime")
  336. --今天是否重启
  337. local isReboot = false
  338. local devRebootTimestamp = getRebootTimestamp(devRebootTime)
  339. if not devRebootTimestamp then
  340. log.error("configNvm.auto_reboot_task","getRebootTimestamp failed!!")
  341. else
  342. --到重启时间时,重启设备
  343. if (now - devRebootTimestamp >= 0) and (now - devRebootTimestamp < 60) then
  344. local randVal = lmath.random( 1, 3600 )
  345. log.info("configNvm.auto_reboot_task", "wait "..randVal.."s, reboot device ...", now)
  346. --等待一个随机时间重启设备
  347. sys.wait(randVal*1000)
  348. sys.restart("configNvm auto restart")
  349. break
  350. else
  351. log.info("configNvm.auto_reboot_task","have not reached restart time ,skip")
  352. end
  353. end
  354. sys.wait(10*1000)
  355. end
  356. end)