123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- --- 模块功能:参数存储功能测试.
- -- @author openLuat
- -- @module nvm.testNvm
- -- @license MIT
- -- @copyright openLuat
- -- @release 2018.03.27
- module(...,package.seeall)
- require"config"
- require"nvm"
- require"sim"
- require"misc"
- require"pmd"
- require"lmath"
- local device_config_init_timer_id = nil
- local init_sim_config_result = false
- local init_module_config_result = false
- local device_config_init_loop_max_times = 1000
- local device_config_init_loop_times = 0
- local device_config_init_loop_interval = 100
- nvm.init("config.lua")
- local moduleModel = nvm.get("moduleModel")
- --开机次数+1
- nvm.set("bootNum", nvm.get("bootNum") + 1)
- local devStart = nvm.get("devStart")
- if devStart == false then
- ----log.info("configNvm","第一次上电开机")
- nvm.set("devStart", true)
- ----log.info("configNvm","开机信息已经记录")
- ----sys.timerStart(rtos.restart, 5000)
- else
- ----log.info("configNvm","第 "..nvm.get("bootNum").." 次上电开机")
- ----nvm.restore()
- ----log.info("configNvm","已经恢复出厂设置")
- ----sys.timerStart(rtos.restart, 5000)
- end
- sys.timerLoopStart(function()
- if devStart == false then
- log.info("configNvm","第一次上电开机")
- --nvm.set("devStart", true)
- if nvm.get("devStart") then
- log.info("configNvm","开机信息已经记录")
- end
- --sys.timerStart(rtos.restart, 5000)
- else
- log.info("configNvm","第 "..nvm.get("bootNum").." 次上电开机", "poweron_reason:", rtos.poweron_reason())
- --nvm.restore()
- --log.info("configNvm","已经恢复出厂设置")
- --sys.timerStart(rtos.restart, 5000)
- end
- end, 5000)
- --nvm使用方法示例
- --nvm.set("numPara",2)
- --nvm.set("boolPara",false)
- --nvm.set("tablePara",{"item2-1","item2-2","item2-3"})
- --log.info("configNvm.numPara",nvm.get("numPara"))
- --log.info("configNvm.boolPara",nvm.get("boolPara"))
- --local tableValue = nvm.get("tablePara")
- --log.info("configNvm.tablePara",tableValue[1],tableValue[2],tableValue[3])
- --[[
- --运行指示灯测试
- sys.taskInit(function()
- --蜂鸣器io初始化
- pmd.ldoset(2,pmd.LDO_VMMC)
- pins.setup(pio.P0_26, 0)
- pmd.ldoset(2,pmd.LDO_VLCD)
- while true do
- log.info("configNvm.led","-----> start")
- sys.wait(1000)
- --pins.setup(pio.P0_14, 0)
- pins.setup(pio.P0_0, 0) --ctl 灯
- pins.setup(pio.P0_18, 1) --net
- pins.setup(pio.P0_19, 1) --485
- sys.wait(1000)
- --pins.setup(pio.P0_14, 1)
- pins.setup(pio.P0_0, 1)
- pins.setup(pio.P0_18, 0)
- pins.setup(pio.P0_19, 0)
- end
- end)
- ]]
- --[[
- 有些GPIO需要打开对应的ldo电压域之后,才能正常配置工作,电压域和对应的GPIO关系如下
- pmd.ldoset(x,pmd.LDO_VSIM1) -- GPIO 29、30、31
- pmd.ldoset(x,pmd.LDO_VLCD) -- GPIO 0、1、2、3、4
- pmd.ldoset(x,pmd.LDO_VMMC) -- GPIO 24、25、26、27、28
- x=0时:关闭LDO
- x=1时:LDO输出1.716V
- x=2时:LDO输出1.828V
- x=3时:LDO输出1.939V
- x=4时:LDO输出2.051V
- x=5时:LDO输出2.162V
- x=6时:LDO输出2.271V
- x=7时:LDO输出2.375V
- x=8时:LDO输出2.493V
- x=9时:LDO输出2.607V
- x=10时:LDO输出2.719V
- x=11时:LDO输出2.831V
- x=12时:LDO输出2.942V
- x=13时:LDO输出3.054V
- x=14时:LDO输出3.165V
- x=15时:LDO输出3.177V
- -- 除了上面列举出的GPIO外,其余的GPIO不需要打开特定的电压域,可以直接配置工作
- ]]
- --三色灯测试
- --[[
- sys.taskInit(function()
- --打开ldo电压域
- pmd.ldoset(2,pmd.LDO_VMMC)
- while true do
- log.info("------> p15 start <---------")
- pins.setup(pio.P0_15, 0)
- --pins.setup(pio.P0_26, 1)
- --pins.setup(pio.P0_25, 1)
- sys.wait(2000)
- pins.setup(pio.P0_15, 1)
- --pins.setup(pio.P0_26, 1)
- --pins.setup(pio.P0_25, 1)
- sys.wait(2000)
- log.info("------> p26 start <---------")
- pins.setup(pio.P0_26, 0)
- --pins.setup(pio.P0_15, 1)
- --pins.setup(pio.P0_25, 1)
- sys.wait(2000)
- pins.setup(pio.P0_26, 1)
- ---pins.setup(pio.P0_15, 1)
- --pins.setup(pio.P0_25, 1)
- sys.wait(2000)
- log.info("------> p25 start <---------")
- pins.setup(pio.P0_25, 0)
- --pins.setup(pio.P0_15, 1)
- --pins.setup(pio.P0_26, 1)
- sys.wait(2000)
- pins.setup(pio.P0_25, 1)
- --pins.setup(pio.P0_15, 1)
- --pins.setup(pio.P0_26, 1)
- sys.wait(2000)
- end
- end)
- ]]
- --切换sim卡
- sys.taskInit(function()
- while not sim.getId() do
- log.info("sim.getId","wait getId ...")
- sys.wait(1000)
- end
- log.info("sim.getId","------>>> simid:", sim.getId())
- --[[
- while true do
- log.info("sim.getId","------>>> simid:", sim.getId())
- sys.wait(2000)
- end
-
- if sim.getId() ~=0 then
- sim.setId(0, function(result)
- if result then
- --ril.request("AT+SIMCROSS=1")
- log.info("sim.setId","setId ok, restart simcross:",sim.getId())
- sys.restart("simcross")
- end
- end)
- end
- ]]
- end)
- --[[
- --获取设备名称at指令测试
- sys.taskInit(function()
-
- while true do
- if not moduleModel then
- ril.request("AT+CGMM", nil, function(...)
-
- log.info("configNvm","at AT+CGMM resp,data:",...)
- end)
- end
- sys.wait(1000)
- end
- end)
- ]]
- --初始化sim卡配置参数
- local function init_sim_config()
- if not sim.getStatus() then
- log.info("configNvm.init_sim_config", "sim.getStatus() fail!")
- return false
- end
- local simIccid = sim.getIccid()
- local simImsi = sim.getImsi()
- local simMnc = sim.getMnc()
- local simMcc = sim.getMcc()
- if not simIccid then
- log.info("configNvm.init_sim_config", "sim.getIccid() fail!")
- return false
- end
- if not simImsi then
- log.info("configNvm.init_sim_config", "sim.getImsi() fail")
- return false
- end
- if not simMnc then
- log.info("configNvm.init_sim_config", "sim.getMnc() fail")
- return false
- end
- if not simMcc then
- log.info("configNvm.init_sim_config", "sim.getMcc() fail")
- return false
- end
- if nvm.get("simIccid") ~= simIccid then
- if not nvm.set("simIccid",simIccid) then
- log.info("configNvm.init_sim_config", "nvm.set simIccid fail")
- return false
- end
- end
- if nvm.get("simImsi") ~= simImsi then
- if not nvm.set("simImsi",simImsi) then
- log.info("configNvm.init_sim_config", "nvm.set simImsi fail")
- return false
- end
- end
- if nvm.get("simMnc") ~= simMnc then
- if not nvm.set("simMnc",simMnc) then
- log.info("configNvm.init_sim_config", "nvm.set simMnc fail")
- return false
- end
- end
- if nvm.get("simMcc") ~= simMcc then
- if not nvm.set("simMcc",simMcc) then
- log.info("configNvm.init_sim_config", "nvm.set simMcc fail")
- return false
- end
- end
- log.info("configNvm.init_sim_config", "simIccid=", simIccid, "simImsi=", simImsi, "simMnc=", simMnc, "simMcc=", simMcc)
- return true
- end
- --初始化模块配置参数
- local function init_module_config()
- local moduleImei = misc.getImei()
- local moduleSn = misc.getSn()
- local moduleMuid = misc.getMuid()
- local moduleSoftVersion = misc.getVersion()
- if not moduleImei then
- log.info("configNvm.init_module_config", "misc.getImei() fail")
- return false
- end
- if not moduleSn then
- log.info("configNvm.init_module_config", "misc.getSn() fail")
- return false
- end
- if not moduleMuid then
- log.info("configNvm.init_module_config", "misc.getMuid() fail")
- return false
- end
- if not moduleSoftVersion then
- log.info("configNvm.init_module_config", "misc.getVersion() fail")
- return false
- end
- if nvm.get("moduleImei") ~= moduleImei then
- if not nvm.set("moduleImei",moduleImei) then
- log.info("configNvm.init_module_config", "nvm.set moduleImei fail")
- return false
- end
- end
- if nvm.get("moduleSn") ~= moduleSn then
- if not nvm.set("moduleImei",moduleImei) then
- log.info("configNvm.init_module_config", "nvm.set moduleImei fail")
- return false
- end
- end
- if nvm.get("moduleMuid") ~= moduleMuid then
- if not nvm.set("moduleMuid",moduleMuid) then
- log.info("configNvm.init_module_config", "nvm.set moduleMuid fail")
- return false
- end
- end
- if nvm.get("moduleSoftVersion") ~= moduleSoftVersion then
- if not nvm.set("moduleSoftVersion",moduleSoftVersion) then
- log.info("configNvm.init_module_config", "nvm.set moduleSoftVersion fail")
- return false
- end
- end
- log.info("configNvm.init_module_config", "moduleImei=", moduleImei, "moduleSn=", moduleSn, "moduleMuid=", moduleMuid, "moduleSoftVersion=", moduleSoftVersion)
- return true
- end
- --初始化设备配置参数
- device_config_init_timer_id = sys.timerLoopStart(function ()
-
- if not device_config_init_timer_id then
- log.info("configNvm.device_config_init_timer_id", "device_config_init_timer_id empty!")
- return false
- end
-
- device_config_init_loop_times = device_config_init_loop_times + 1
- log.info("configNvm.device_config_init_timer_id","device_config_init_loop_times = ",device_config_init_loop_times)
- if device_config_init_loop_times >= device_config_init_loop_max_times then
- 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...!! ")
- --sys.publish("device_config_init_result",false)
- sys.timerStart(rtos.restart, 5000)
- sys.timerStop(device_config_init_timer_id)
- return false
- end
- if not init_module_config_result then
- log.info("configNvm.device_config_init_timer_id", "init_module_config_result start")
- init_module_config_result = init_module_config()
- log.info("configNvm.device_config_init_timer_id", "init_module_config_result = ",init_module_config_result)
- if not init_module_config_result then
- return false
- end
- end
- if not init_sim_config_result then
- log.info("configNvm.device_config_init_timer_id", "init_sim_config start")
- init_sim_config_result = init_sim_config()
- log.info("configNvm.device_config_init_timer_id", "init_sim_config_result = ",init_sim_config_result)
- if not init_sim_config_result then
- return false
- end
- end
- log.info("configNvm.device_config_init_timer_id", "device_config_init_timer loop stop, device_config_init_timer_id:",device_config_init_timer_id)
- sys.publish("device_config_init_result",true)
- sys.timerStop(device_config_init_timer_id)
- end, device_config_init_loop_interval)
- function getRebootTimestamp(devRebootTime)
- if not devRebootTime then
- log.error("configNvm.getRebootTimestamp","devRebootTime empty!")
- return false
- end
- local _, __, h, m, s = string.find(devRebootTime, "(%d+):(%d+):(%d+)")
- if not _ then
- log.error("configNvm.getRebootTimestamp","devRebootTime format error! devRebootTime:", devRebootTime)
- return false
- end
- local tm = os.date("*t")
- local rebootTimestamp = os.time({year = tm.year, month = tm.month, day = tm.day, hour = h, min = m, sec = s})
- return rebootTimestamp
- end
- --设备定时自动重启任务
- sys.taskInit(function()
- if not sys.waitUntil("TIMER_SYNC_SUCCESS", 3600*1000) then
- log.error("configNvm.auto_reboot_task","wait timer sync timeout,reboot now ...")
- sys.restart("configNvm wait timer sync timeout")
- end
- while true do
- local now = os.time()
- local devRebootTime = nvm.get("devRebootTime")
- --今天是否重启
- local isReboot = false
- local devRebootTimestamp = getRebootTimestamp(devRebootTime)
- if not devRebootTimestamp then
- log.error("configNvm.auto_reboot_task","getRebootTimestamp failed!!")
- else
- --到重启时间时,重启设备
- if (now - devRebootTimestamp >= 0) and (now - devRebootTimestamp < 60) then
- local randVal = lmath.random( 1, 3600 )
- log.info("configNvm.auto_reboot_task", "wait "..randVal.."s, reboot device ...", now)
- --等待一个随机时间重启设备
- sys.wait(randVal*1000)
- sys.restart("configNvm auto restart")
- break
- else
- log.info("configNvm.auto_reboot_task","have not reached restart time ,skip")
- end
- end
- sys.wait(10*1000)
- end
- end)
|