--[[
ģƣͨ
ģ鹦ܣԺ
ģ޸ʱ䣺2017.02.23
]]

module(...,package.seeall)
require"cc"
require"audio"
require"common"
require"record"
require"audiocore"


local RCD_READ_UNIT = 512
local rcdoffset,rcdsize,rcdcnt,rcdcur
local typ="incoming"
local total,cur

--[[
print
  ӡӿڣļедӡtestǰ׺
  
ֵ
]]
local function print(...)
	_G.print("ljd test",...)
end

--[[
connected
  ͨѽϢ
  
ֵ
]]
local function connected()
	print("connected")
	--5󲥷TTSԶˣײ֧TTS
	--sys.timer_start(audio.play,5000,0,"TTSCC",common.binstohexs(common.gb2312toucs2("ͨвTTS")),audiocore.VOL7)
	--50֮ͨ
	sys.timer_start(cc.hangup,50000,"AUTO_DISCONNECT")

    if typ=="outgoing" then
        print("connected play")
        audio.play(0,"FILE","/ldata/alarm.amr",audiocore.VOL3,playcb)
    end
end

--[[
disconnected
  ͨѽϢ
  
		paraͨԭֵ
			  "LOCAL_HANG_UP"ûcc.hangupӿڹҶͨ
			  "CALL_FAILED"ûcc.dialӿںatִʧ
			  "NO CARRIER"Ӧ
			  "BUSY"ռ
			  "NO ANSWER"Ӧ
ֵ
]]
local function disconnected(para)
	print("disconnected:"..(para or "nil"))
	sys.timer_stop(cc.hangup,"AUTO_DISCONNECT")
    if typ=="incoming" then
    sys.timer_start(openrcd,3000)
    end
    typ=nil
end

function playcb(r)
	print("playcb",r)
	--ɾ¼ļ
	--record.delete()
end

--[[
getdata
  ȡ¼ļָλָ
  
		offsetnumberָͣλãȡֵΧǡ0  ļ-1
        lennumberָͣȣõĳȴļʣĳȣֻܶȡʣĳ
ֵָ¼ݣȡʧܣؿַ""
]]
function getdata(offset,len)
	local f,rt = io.open("/CallRec/rec001.wav","rb")
    --ļʧܣΪա
	if not f then print("getdata erropen") return "" end
	if not f:seek("set",offset) then print("getdata errseek") return "" end
    --ȡָȵ
	rt = f:read(len)
	f:close()
	
	return rt or ""
end

--[[
getsize
  ȡǰ¼ļܳ
  
ֵǰ¼ļܳȣλֽ
]]
local function getsize()
	local f = io.open("/CallRec/rec001.wav","rb")
	if not f then print("getsize erropen") return 0 end
	local size = f:seek("end")
	if not size or size == 0 then print("getsize errseek") return 0 end
	f:close()
    return size
end

function sndrcd()
    local data = getdata(rcdoffset,RCD_READ_UNIT)
    sys.dispatch("CMD_RCD_SEND",data)
end

function rcdsndcnf()
    print("rcdsndcnf:","rcdcur:",rcdcur,"rcdcnt:",rcdcnt)
    if rcdcur < rcdcnt then
        rcdcur = rcdcur+1
        rcdoffset = rcdoffset+(rcdcur-1)*RCD_READ_UNIT
        sndrcd()
    else
        print("rcdsnd finish")
        audio.play(0,"FILE","/CallRec/rec001.wav",audiocore.VOL3,playcb)
    end
end

function openrcd()
	print("openrcd:")
	
	rcdsize = getsize()
    rcdcnt = (rcdsize-1)/RCD_READ_UNIT+1
    rcdoffset,rcdcur = 0,1
    sndrcd()

    print("openrcd:","rcdsize:",rcdsize,"rcdcnt",rcdcnt)
    return 
end


--[[
incoming
  硱Ϣ
  
		numstringͣ
ֵ
]]
local function incoming(num)
	print("incoming:"..num)
	--
    typ="incoming"
	cc.accept()
    record.start(5)
end

local function ready()
	print("ready")
    ril.request("AT*EXASSERT=1")
    sys.timer_start(testdial,15000)
end

function testdial()
    typ="outgoing"
    cc.dial("18126324568")
end

local procer = {
	RCD_SEND_CNF = rcdsndcnf,
}
sys.regapp(procer)

--עϢûص
cc.regcb("INCOMING",incoming,"CONNECTED",connected,"READY",ready,"DISCONNECTED",disconnected)--"READY",ready,


