main.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. --[[
  2. IPv6服务端演示, 仅EC618系列支持, 例如Air780E/Air600E/Air780UG/Air700E
  3. ]]
  4. -- LuaTools需要PROJECT和VERSION这两个信息
  5. PROJECT = "ipv6_server"
  6. VERSION = "1.0.0"
  7. log.info("main", PROJECT, VERSION)
  8. -- 一定要添加sys.lua !!!!
  9. sys = require("sys")
  10. sysplus = require("sysplus")
  11. libnet = require "libnet"
  12. -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
  13. if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then
  14. pm.power(pm.PWK_MODE, false)
  15. end
  16. -- 处理未识别的网络消息
  17. local function netCB(msg)
  18. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  19. end
  20. LED = gpio.setup(27, 0)
  21. GPIO1 = gpio.setup(1, 0)
  22. GPIO24 = gpio.setup(24, 0)
  23. HTTP_200_EMTRY = "HTTP/1.0 200 OK\r\nServer: LuatOS\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
  24. -- 演示task
  25. function ipv6test()
  26. -- 启用IPv6, 默认关闭状态,必须在驻网前开启
  27. -- 注意, 启用IPv6, 联网速度会慢2~3秒
  28. mobile.ipv6(true)
  29. log.info("ipv6", "等待联网")
  30. sys.waitUntil("IP_READY")
  31. log.info("ipv6", "联网完成")
  32. sys.wait(1000)
  33. -- 打印一下本地ip, 一般只有ipv6才可能是公网ip, ipv4基本不可能
  34. -- 而且ipv6不一样是外网ip, 这是运营商决定的, 模块无能为力
  35. ip, mask, gw, ipv6 = socket.localIP()
  36. log.info("本地IP地址", ip, ipv6)
  37. if not ipv6 then
  38. log.info("没有IPV6地址,无法演示")
  39. -- return
  40. end
  41. log.info("shell", "telnet -6 " .. ipv6 .. " 14000")
  42. -- 开始正在的逻辑, 发起socket链接,等待数据/上报心跳
  43. local taskName = "ipv6server"
  44. local topic = taskName .. "_txrx"
  45. local txqueue = {}
  46. sysplus.taskInitEx(ipv6task, taskName, netCB, taskName, txqueue, topic)
  47. while 1 do
  48. local result, tp, data = sys.waitUntil(topic, 60000)
  49. if not result then
  50. -- 等很久了,没数据上传/下发, 发个日期心跳包吧
  51. --table.insert(txqueue, string.char(0))
  52. --sys_send(taskName, socket.EVENT, 0)
  53. elseif tp == "uplink" then
  54. -- 上行数据, 主动上报的数据,那就发送呀
  55. table.insert(txqueue, data)
  56. sys_send(taskName, socket.EVENT, 0)
  57. elseif tp == "downlink" then
  58. -- 下行数据,接收的数据, 从ipv6task来的
  59. -- 其他代码可以通过 sys.publish()
  60. log.info("socket", "收到下发的数据了", #data, data)
  61. -- 下面是模拟一个http服务, 因为httpsrv库还没好,先用着吧
  62. if data:startsWith("GET / ") then
  63. local httpresp = "HTTP/1.0 200 OK\r\n"
  64. httpresp = httpresp .. "Server: LuatOS\r\nContent-Type: text/html\r\nConnection: close\r\n"
  65. local fdata = io.readFile("/luadb/index.html")
  66. httpresp = httpresp .. string.format("Content-Length: %d\r\n\r\n", #fdata)
  67. httpresp = httpresp .. fdata
  68. table.insert(txqueue, httpresp)
  69. table.insert(txqueue, "close")
  70. sys_send(taskName, socket.EVENT, 0)
  71. elseif data:startsWith("GET /led/") then
  72. if data:startsWith("GET /led/1") then
  73. log.info("led", "亮起")
  74. LED(1)
  75. else
  76. log.info("led", "熄灭")
  77. LED(0)
  78. end
  79. table.insert(txqueue, HTTP_200_EMTRY)
  80. table.insert(txqueue, "close")
  81. sys_send(taskName, socket.EVENT, 0)
  82. elseif data:startsWith("GET /gpio24/") then
  83. if data:startsWith("GET /gpio24/1") then
  84. log.info("gpio24", "亮起")
  85. GPIO24(1)
  86. else
  87. log.info("gpio24", "熄灭")
  88. GPIO24(0)
  89. end
  90. table.insert(txqueue, HTTP_200_EMTRY)
  91. table.insert(txqueue, "close")
  92. sys_send(taskName, socket.EVENT, 0)
  93. elseif data:startsWith("GET /gpio1/") then
  94. if data:startsWith("GET /gpio1/1") then
  95. log.info("gpio1", "亮起")
  96. GPIO1(1)
  97. else
  98. log.info("gpio1", "熄灭")
  99. GPIO1(0)
  100. end
  101. table.insert(txqueue, HTTP_200_EMTRY)
  102. table.insert(txqueue, "close")
  103. sys_send(taskName, socket.EVENT, 0)
  104. elseif data:startsWith("GET ") or data:startsWith("POST ") or data:startsWith("HEAD ") then
  105. table.insert(txqueue, HTTP_200_EMTRY)
  106. table.insert(txqueue, "close")
  107. sys_send(taskName, socket.EVENT, 0)
  108. end
  109. end
  110. end
  111. end
  112. function ipv6task(d1Name, txqueue, rxtopic)
  113. -- 本地监听的端口
  114. local port = 14000
  115. local rx_buff = zbuff.create(1024)
  116. local tx_buff = zbuff.create(4 * 1024)
  117. local netc = socket.create(nil, d1Name)
  118. socket.config(netc, 14000)
  119. log.info("任务id", d1Name)
  120. while true do
  121. log.info("socket", "开始监控")
  122. local result = libnet.listen(d1Name, 0, netc)
  123. if result then
  124. log.info("socket", "监听成功")
  125. result = socket.accept(netc, nil) --只支持1对1
  126. if result then
  127. log.info("客户端连上了")
  128. end
  129. else
  130. log.info("socket", "监听失败!!")
  131. end
  132. while result do
  133. -- log.info("socket", "调用rx接收数据")
  134. local succ, param = socket.rx(netc, rx_buff)
  135. if not succ then
  136. log.info("客户端断开了", succ, param, ip, port)
  137. break
  138. end
  139. if rx_buff:used() > 0 then
  140. log.info("socket", "收到客户端数据,长度", rx_buff:used())
  141. local data = rx_buff:query() -- 获取数据
  142. sys.publish(rxtopic, "downlink", data)
  143. rx_buff:del()
  144. end
  145. -- log.info("libnet", "调用wait开始等待消息")
  146. result, param, param2 = libnet.wait(d1Name, 15000, netc)
  147. log.info("libnet", "wait", result, param, param2)
  148. if not result then
  149. -- 网络异常了
  150. log.info("socket", "客户端断开了", result, param)
  151. break
  152. elseif #txqueue > 0 then
  153. local force_close = false
  154. while #txqueue > 0 do
  155. local data = table.remove(txqueue, 1)
  156. if not data then
  157. break
  158. end
  159. log.info("socket", "上行数据长度", #data)
  160. if data == "close" then
  161. --sys.wait(1000)
  162. force_close = true
  163. break
  164. end
  165. tx_buff:del()
  166. tx_buff:copy(nil, data)
  167. result,param = libnet.tx(d1Name, 15000, netc, tx_buff)
  168. log.info("libnet", "发送数据的结果", result, param)
  169. if not result then
  170. log.info("socket", "数据发送异常", result, param)
  171. break
  172. end
  173. end
  174. if force_close then
  175. break
  176. end
  177. end
  178. end
  179. log.info("socket", "连接已断开,继续下一个循环")
  180. libnet.close(d1Name, 5000, netc)
  181. -- log.info(rtos.meminfo("sys"))
  182. sys.wait(50)
  183. end
  184. end
  185. sys.taskInit(ipv6test)
  186. -- 用户代码已结束---------------------------------------------
  187. -- 结尾总是这一句
  188. sys.run()
  189. -- sys.run()之后后面不要加任何语句!!!!!