require "ril" require "sys" module(..., package.seeall) local req = ril.request local imsi, iccid, status local sNumber,bQueryNumber = "" local simCross,setSimCrossCbFnc function getIccid() return iccid end function getImsi() return imsi end function getMcc() return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 1, 3) or "" end function getMnc() return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 4, 5) or "" end function getStatus() return status end function setQueryNumber(flag) bQueryNumber = flag end function getNumber() return sNumber or "" end local function rsp(cmd, success, response, intermediate) if cmd == "AT+ICCID" then if intermediate then iccid = string.match(intermediate, "%+ICCID: (.+)") end elseif cmd == "AT+SIMCROSS?" then if success then simCross = tonumber(intermediate:match("%+SIMCROSS:%s*(%d)")) end if setSimCrossCbFnc then setSimCrossCbFnc(success) end elseif cmd:match("AT%+SIMCROSS=") then if success then req("AT+SIMCROSS?") else if setSimCrossCbFnc then setSimCrossCbFnc(false) end end elseif cmd == "AT+CIMI" then imsi = intermediate sys.publish("IMSI_READY") elseif cmd == "AT+CNUM" then if success then if intermediate then sNumber = intermediate:match("%+CNUM:%s*\".-\",\"[%+]*(%d+)\",") end else sys.timerStart(ril.request,5000,"AT+CNUM") end end end local function urc(data, prefix) if prefix == "+CPIN" then status = false if data == "+CPIN: READY" then status = true ril.request("AT+ICCID") ril.request("AT+CIMI") if bQueryNumber then ril.request("AT+CNUM") end sys.publish("SIM_IND", "RDY") elseif data == "+CPIN: NOT INSERTED" then sys.publish("SIM_IND", "NIST") else if data == "+CPIN: SIM PIN" then sys.publish("SIM_IND","SIM_PIN") end sys.publish("SIM_IND", "NORDY") end end end function set2gSim() ril.request("AT+MEDCR=0,8,1") ril.request("AT+MEDCR=0,17,240") ril.request("AT+MEDCR=0,19,1") end function setId(id,cbFnc) if id ~= simCross then setSimCrossCbFnc = cbFnc ril.request("AT+SIMCROSS="..id) else if cbFnc then cbFnc(true) end end end function getId() return simCross end ril.regRsp("+ICCID", rsp) ril.regRsp("+CIMI", rsp) ril.regRsp("+CNUM", rsp) ril.regRsp("+SIMCROSS", rsp) ril.regUrc("+CPIN", urc) ril.request("AT+SIMCROSS?")