Browse Source

add:临时的network的demo,8850的

alienwalker 3 years ago
parent
commit
624322c3d1

+ 89 - 0
demo/network/UIS8850/dtu_demo.lua

@@ -0,0 +1,89 @@
+local libnet = require "libnet"
+
+--下面演示用阻塞方式做串口透传远程服务器,简单的串口DTU
+local d1Online = false
+local com_buff = zbuff.create(4096)
+local d1Name = "D1_TASK"
+local function netCB(msg)
+	log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
+end
+
+local function dtuTask(uart_id, ip, port)
+	d1Online = false
+	local tx_buff = zbuff.create(1024)
+	local rx_buff = zbuff.create(1024)
+	local netc 
+	local result, param, is_err, rIP, rPort
+	-- result = uart.setup(uart_id,115200,8,1)
+	-- uart.on(uart_id, "receive", function(id, len)
+	--     uart.rx(id, com_buff)
+	--     if d1Online then
+	--     	sys_send(d1Name, network.EVENT, 0)
+	--     end
+	-- end)
+	netc = network.create(nil, d1Name)
+	network.debug(netc, true)
+	network.config(netc, nil, nil, nil)
+	-- network.config(netc, nil, true)
+	while true do
+
+		log.info(rtos.meminfo("sys"))
+		result = libnet.waitLink(d1Name, 0, netc)
+		result = libnet.connect(d1Name, 15000, netc, ip, port)
+		-- result = libnet.connect(d1Name, 5000, netc, "112.125.89.8",34607)
+		d1Online = result
+		if result then
+			log.info("服务器连上了")
+			libnet.tx(d1Name, 0, netc, "helloworld")
+		end
+		while result do
+			is_err, param, rIP, rPort = network.rx(netc, rx_buff)
+			if is_err then
+				log.info("服务器断开了", is_err, param, ip, port)
+				break
+			end
+			if rx_buff:used() > 0 then
+				log.info("收到服务器数据,长度", rx_buff:used())
+				-- uart.tx(uart_id, rx_buff)
+				tx_buff:copy(nil, rx_buff)
+				rx_buff:del()
+			else
+				libnet.tx(d1Name, 15000, netc, "heart!")
+			end
+			tx_buff:copy(nil, com_buff)
+			com_buff:del()
+			if tx_buff:used() > 0 then
+				result, param = libnet.tx(d1Name, 15000, netc, tx_buff)
+			end
+			if not result then
+				log.info("发送失败了", result, param)
+				break
+			end
+			tx_buff:del()
+			if com_buff:len() > 4096 then
+				com_buff:resize(4096)
+			end
+			if tx_buff:len() > 1024 then
+				tx_buff:resize(1024)
+			end
+			if rx_buff:len() > 1024 then
+				rx_buff:resize(1024)
+			end
+			log.info(rtos.meminfo("sys"))
+			result, param = libnet.wait(d1Name, 15000, netc)
+			if not result then
+				log.info("服务器断开了", result, param)
+				break
+			end
+
+		end
+		d1Online = false
+		libnet.close(d1Name, 5000, netc)
+		log.info(rtos.meminfo("sys"))
+		sys.wait(1000)
+	end
+end
+
+function dtuDemo(uart_id, ip, port)
+	sysplus.taskInitEx(dtuTask, d1Name, netCB, uart_id, ip, port)
+end

+ 144 - 0
demo/network/UIS8850/libnet.lua

@@ -0,0 +1,144 @@
+local libnet = {}
+
+--- 阻塞等待网卡的网络连接上,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.linkup一致
+-- @return 失败或者超时返回false 成功返回true
+function libnet.waitLink(taskName, timeout, ...)
+	local is_err, result = network.linkup(...)
+	if is_err then
+		return false
+	end
+	if not result then
+		result = sys_wait(taskName, network.LINK, timeout)
+	else
+		return true
+	end
+	if type(result) == 'table' and result[2] == 0 then
+		return true
+	else
+		return false
+	end
+end
+
+--- 阻塞等待IP或者域名连接上,如果加密连接还要等握手完成,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.connect一致
+-- @return 失败或者超时返回false 成功返回true
+function libnet.connect(taskName,timeout, ... )
+	local is_err, result = network.connect(...)
+	if is_err then
+		return false
+	end
+	if not result then
+		result = sys_wait(taskName, network.ON_LINE, timeout)
+	else
+		return true
+	end
+	if type(result) == 'table' and result[2] == 0 then
+		return true
+	else
+		return false
+	end
+end
+
+--- 阻塞等待客户端连接上,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.listen一致
+-- @return 失败或者超时返回false 成功返回true
+function libnet.listen(taskName,timeout, ... )
+	local is_err, result = network.listen(...)
+	if is_err then
+		return false
+	end
+	if not result then
+		result = sys_wait(taskName, network.ON_LINE, timeout)
+	else
+		return true
+	end
+	if type(result) == 'table' and result[2] == 0 then
+		return true
+	else
+		return false
+	end
+end
+
+--- 阻塞等待数据发送完成,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.tx一致
+-- @return 
+-- @boolean 失败或者超时返回false,缓冲区满了或者成功返回true
+-- @boolean 缓存区是否满了
+function libnet.tx(taskName,timeout, ...)
+	local is_err, is_full, result = network.tx(...)
+	if is_err then
+		return false, is_full
+	end
+	if is_full then
+		return true, true
+	end
+	if not result then
+		result = sys_wait(taskName, network.TX_OK, timeout)
+	else
+		return true, is_full
+	end
+	if type(result) == 'table' and result[2] == 0 then
+		return true, false
+	else
+		return false, is_full
+	end
+end
+
+--- 阻塞等待新的网络事件或者特定事件,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.wait一致
+-- @return 
+-- @boolean 网络异常返回false,其他返回true
+-- @table or boolean 超时返回false,有新的数据到返回true,被其他事件退出的,返回接收到的事件
+function libnet.wait(taskName,timeout, netc)
+	local is_err, result = network.wait(netc)
+	if is_err then
+		return false
+	end
+	if not result then
+		result = sys_wait(taskName, network.EVENT, timeout)
+	else
+		return true
+	end
+	if type(result) == 'table' then
+		if result[2] == 0 then
+			return true, true
+		else
+			return false, false
+		end
+	else
+		return true, false
+	end
+end
+
+--- 阻塞等待网络断开连接,只能用于任务函数中
+-- @string 任务标志
+-- @int 超时时间,如果==0或者空,则没有超时一致等待
+-- @... 其他参数和network.close一致
+-- @return 无
+function libnet.close(taskName,timeout, netc)
+	local is_err, result = network.discon(netc)
+	if is_err then
+		network.close(netc)
+		return
+	end
+	if not result then
+		result = sys_wait(taskName, network.CLOSED, timeout)
+	else
+		network.close(netc)
+		return
+	end
+	network.close(netc)
+end
+
+return libnet

+ 35 - 0
demo/network/UIS8850/main.lua

@@ -0,0 +1,35 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "my_test"
+VERSION = "1.2"
+PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的
+-- sys库是标配
+_G.sys = require("sys")
+_G.sysplus = require("sysplus")
+log.style(1)
+--require "gpio_test"
+-- require "i2c_test"
+-- require "audio_test"
+require "net_test"
+-- require "evb_lcd"
+-- require "timer_test"
+-- require "rotary"
+-- rotary_start()
+--require "fs_test"
+--fs_demo()
+--require "video_demo"
+-- require "sd"
+-- require "xmodem"
+-- pwm.open(2, 1000, 300,1000,1000)
+-- sys.taskInit(sendFile,3, 115200, "/sd/fw/air101.fls")
+-- require "camera_test"
+-- require "camera_raw"
+-- camDemo("10.0.0.3", 12000)
+-- require "core_lcd"
+-- require "lvgl_test"
+-- require "pm_test"
+-- require "no_block_test"
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 10 - 0
demo/network/UIS8850/net_test.lua

@@ -0,0 +1,10 @@
+network.setDNS(nil,1,"223.5.5.5")
+network.setDNS(nil,2,"114.114.114.114")
+require "dtu_demo"
+dtuDemo(2, "112.125.89.8", 37553)
+require "ntp_demo"
+ntpDemo()
+--require "ota_demo"
+--otaDemo()
+--require "server_demo"
+--SerDemo(12000)

+ 76 - 0
demo/network/UIS8850/ntp_demo.lua

@@ -0,0 +1,76 @@
+-- 实现ntp校准时间
+-- 通过回调的方式进行演示
+local function netCB(netc, event, param)
+	if event == network.LINK and param == 0 then
+		sys.publish("NTP_ENABLE_LINK")
+	elseif event == network.ON_LINE and param == 0 then
+		network.tx(netc, "\xE3\x00\x06\xEC" .. string.rep("\x00", 44))
+		network.wait(netc)	-- 这样才会有接收消息的回调
+	elseif event == network.EVENT and param == 0 then
+		local rbuf = zbuff.create(512)
+		network.rx(netc, rbuf)
+		if rbuf:used() >= 48 then
+			local tamp = rbuf:query(40,4,true) 
+			if tamp - 0x83aa7e80 > 0 then
+				-- rtc.set(tamp - 0x83aa7e80 + 8 * 3600)
+			else
+				--2036年后,当前ntp协议会回滚
+				log.info("ntp 时间戳回滚")
+				-- rtc.set(0x7C558180 + tamp + 8 * 3600)
+			end
+			log.info(os.date())
+			sys.publish("NTP_FINISH")
+			rbuf:resize(4)
+		end
+	end
+end
+
+local function ntpTask()
+	local server = {
+		"ntp1.aliyun.com",
+		"ntp2.aliyun.com",
+		"ntp3.aliyun.com",
+		"ntp4.aliyun.com",
+		"ntp5.aliyun.com",
+		"ntp6.aliyun.com",
+		"ntp7.aliyun.com",
+		"194.109.22.18",
+		"210.72.145.44",}
+	local msg
+	local netc = network.create(nil, netCB)
+	network.debug(netc, true)
+	network.config(netc, nil, true)	-- 配置成UDP,自动分配本地端口
+	local isError, result = network.linkup(netc)
+	if isError then
+		log.info("unkown error")
+		return false
+	end
+	if not result then
+		result, msg = sys.waitUntil("NTP_ENABLE_LINK")
+	else
+		log.info("already online")
+	end
+	for k,v in pairs(server) do
+		log.info("尝试", v)
+		isError, result = network.connect(netc, v, 123)
+		if isError then
+			log.info("unkown error")
+		else
+			result, msg = sys.waitUntil("NTP_FINISH", 5000)
+			if result then
+				log.info("ntp 完成")
+				network.close(netc)
+				network.release(netc)
+				return
+			end
+		end
+		network.close(netc)
+	end
+	log.info("ntp 失败")
+	network.release(netc)
+	return
+end
+
+function ntpDemo()
+	sys.taskInit(ntpTask)
+end