luat_lib_netdrv.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. @module netdrv
  3. @summary 网络设备管理
  4. @catalog 外设API
  5. @version 1.0
  6. @date 2025.01.07
  7. @demo netdrv
  8. @tag LUAT_USE_NETDRV
  9. */
  10. #include "luat_base.h"
  11. #include "luat_gpio.h"
  12. #include "luat_mem.h"
  13. #include "luat_mcu.h"
  14. #include "luat_msgbus.h"
  15. #include "luat_timer.h"
  16. #include "luat_rtos.h"
  17. #include "luat_netdrv.h"
  18. #include "lwip/ip.h"
  19. #include "lwip/ip4.h"
  20. #include "luat_network_adapter.h"
  21. #include "net_lwip2.h"
  22. #define LUAT_LOG_TAG "netdrv"
  23. #include "luat_log.h"
  24. /*
  25. 初始化指定netdrv设备
  26. @api netdrv.setup(id, tp, opts)
  27. @int 网络适配器编号, 例如 socket.LWIP_ETH
  28. @int 实现方式,如果是设备自带的硬件,那就不需要传, 外挂设备需要传,当前支持CH390H/D
  29. @int 外挂方式,需要额外的参数,参考示例
  30. @return boolean 初始化成功与否
  31. @usage
  32. -- Air8101初始化内部以太网控制器
  33. netdrv.setup(socket.LWIP_ETH)
  34. -- Air8000/Air780EPM初始化CH390H/D作为LAN口, 单一使用.不含WAN.
  35. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8})
  36. netdrv.dhcp(socket.LWIP_ETH, true)
  37. */
  38. static int l_netdrv_setup(lua_State *L) {
  39. luat_netdrv_conf_t conf = {0};
  40. conf.id = luaL_checkinteger(L, 1);
  41. conf.impl = luaL_optinteger(L, 2, 0);
  42. conf.irqpin = 255; // 默认无效
  43. if (lua_istable(L, 3)) {
  44. if (lua_getfield(L, 3, "spi") == LUA_TNUMBER) {
  45. conf.spiid = luaL_checkinteger(L, -1);
  46. };
  47. lua_pop(L, 1);
  48. if (lua_getfield(L, 3, "cs") == LUA_TNUMBER) {
  49. conf.cspin = luaL_checkinteger(L, -1);
  50. };
  51. lua_pop(L, 1);
  52. if (lua_getfield(L, 3, "irq") == LUA_TNUMBER) {
  53. conf.irqpin = luaL_checkinteger(L, -1);
  54. };
  55. lua_pop(L, 1);
  56. }
  57. luat_netdrv_t* ret = luat_netdrv_setup(&conf);
  58. lua_pushboolean(L, ret != NULL);
  59. return 1;
  60. }
  61. /*
  62. 开启或关闭DHCP
  63. @api netdrv.dhcp(id, enable)
  64. @int 网络适配器编号, 例如 socket.LWIP_ETH
  65. @boolean 开启或者关闭
  66. @return boolean 成功与否
  67. @usgae
  68. -- 注意, 并非所有网络设备都支持关闭DHCP, 例如4G Cat.1
  69. netdrv.dhcp(socket.LWIP_ETH, true)
  70. */
  71. static int l_netdrv_dhcp(lua_State *L) {
  72. int id = luaL_checkinteger(L, 1);
  73. int enable = lua_toboolean(L, 2);
  74. int ret = luat_netdrv_dhcp(id, enable);
  75. lua_pushboolean(L, ret == 0);
  76. return 1;
  77. }
  78. /*
  79. 设置或获取设备MAC
  80. @api netdrv.mac(id, new_mac, raw_string)
  81. @int 网络适配器编号, 例如 socket.LWIP_ETH
  82. @string 新的MAC地址,可选, 必须是6个字节
  83. @boolean 是否返回6字节原始数据, 默认是否, 返回HEX字符串
  84. @return boolean 成功与否
  85. @usage
  86. -- 获取MAC地址
  87. log.info("netdrv", "mac addr", netdrv.mac(socket.LWIP_ETH))
  88. -- 暂不支持设置
  89. */
  90. static int l_netdrv_mac(lua_State *L) {
  91. int id = luaL_checkinteger(L, 1);
  92. char buff[6] = {0};
  93. char tmpbuff[13] = {0};
  94. size_t len = 0;
  95. if (lua_isstring(L, 2)) {
  96. const char* tmp = luaL_checklstring(L, 2, &len);
  97. if (len != 6) {
  98. return 0;
  99. }
  100. luat_netdrv_mac(id, tmp, buff);
  101. }
  102. else {
  103. luat_netdrv_mac(id, NULL, buff);
  104. }
  105. if (lua_isboolean(L, 3) && !lua_toboolean(L, 3)) {
  106. lua_pushlstring(L, (const char*)buff, 6);
  107. }
  108. else {
  109. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  110. lua_pushstring(L, tmpbuff);
  111. }
  112. return 1;
  113. }
  114. /*
  115. 设置或读取ipv4地址
  116. @api netdrv.ipv4(id, addr, mark, gw)
  117. @int 网络适配器编号, 例如 socket.LWIP_ETH
  118. @string ipv4地址,如果是读取就不需要传
  119. @string 掩码
  120. @string 网关
  121. @return string ipv4地址
  122. @return string 掩码
  123. @return string 网关
  124. @usage
  125. -- 注意, 不是所有netdrv都支持设置的, 尤其4G Cat.1自带的netdrv就不能设置ipv4
  126. -- 注意, 设置ipv4时, DHCP要处于关闭状态!!
  127. */
  128. static int l_netdrv_ipv4(lua_State *L) {
  129. int id = luaL_checkinteger(L, 1);
  130. const char* tmp = NULL;
  131. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  132. if (netdrv == NULL || netdrv->netif == NULL) {
  133. return 0;
  134. }
  135. if (lua_isstring(L, 2) && lua_isstring(L, 3) && lua_isstring(L, 4)) {
  136. tmp = luaL_checkstring(L, 2);
  137. ipaddr_aton(tmp, &netdrv->netif->ip_addr);
  138. tmp = luaL_checkstring(L, 3);
  139. ipaddr_aton(tmp, &netdrv->netif->netmask);
  140. tmp = luaL_checkstring(L, 4);
  141. ipaddr_aton(tmp, &netdrv->netif->gw);
  142. net_lwip2_set_link_state(id, 1);
  143. }
  144. char buff[16] = {0};
  145. char buff2[16] = {0};
  146. char buff3[16] = {0};
  147. ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff, 16);
  148. ipaddr_ntoa_r(&netdrv->netif->netmask, buff2, 16);
  149. ipaddr_ntoa_r(&netdrv->netif->gw, buff3, 16);
  150. lua_pushstring(L, buff);
  151. lua_pushstring(L, buff2);
  152. lua_pushstring(L, buff3);
  153. return 3;
  154. }
  155. /*
  156. 开启或关闭NAPT
  157. @api netdrv.napt(id)
  158. @int 网关适配器的id
  159. @return bool 合法值就返回true, 否则返回nil
  160. @usage
  161. -- 使用4G网络作为主网关出口
  162. netdrv.napt(socket.LWIP_GP)
  163. -- 关闭napt功能
  164. netdrv.napt(socket.LWIP_GP)
  165. */
  166. extern int luat_netdrv_gw_adapter_id;
  167. static int l_netdrv_napt(lua_State *L) {
  168. int id = luaL_checkinteger(L, 1);
  169. if (id < 0) {
  170. LLOGD("NAPT is disabled");
  171. luat_netdrv_gw_adapter_id = id;
  172. lua_pushboolean(L, 1);
  173. return 1;
  174. }
  175. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  176. if (netdrv == NULL || netdrv->netif == NULL) {
  177. return 0;
  178. }
  179. LLOGD("NAPT is enabled gw %d", id);
  180. luat_netdrv_gw_adapter_id = id;
  181. lua_pushboolean(L, 1);
  182. return 1;
  183. }
  184. /*
  185. 获取netdrv的物理连接状态
  186. @api netdrv.link(id)
  187. @int netdrv的id, 例如 socket.LWIP_ETH
  188. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  189. @usage
  190. -- 注意, 本函数仅支持读取, 而且不能ip状态, 即是否能联网
  191. */
  192. static int l_netdrv_link(lua_State *L) {
  193. int id = luaL_checkinteger(L, 1);
  194. if (id < 0) {
  195. return 0; // 非法id
  196. }
  197. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  198. if (netdrv == NULL || netdrv->netif == NULL) {
  199. return 0;
  200. }
  201. lua_pushboolean(L, netif_is_link_up(netdrv->netif));
  202. return 1;
  203. }
  204. /*
  205. 获取netdrv的网络状态
  206. @api netdrv.ready(id)
  207. @int netdrv的id, 例如 socket.LWIP_ETH
  208. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  209. @usage
  210. -- 注意, 本函数仅支持读取, 而且不能ip状态, 即是否能联网
  211. */
  212. static int l_netdrv_ready(lua_State *L) {
  213. int id = luaL_checkinteger(L, 1);
  214. if (id < 0) {
  215. return 0; // 非法id
  216. }
  217. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  218. if (netdrv == NULL || netdrv->netif == NULL) {
  219. return 0;
  220. }
  221. lua_pushboolean(L, netif_is_link_up(netdrv->netif) && !ip_addr_isany(&netdrv->netif->ip_addr));
  222. return 1;
  223. }
  224. #include "rotable2.h"
  225. static const rotable_Reg_t reg_netdrv[] =
  226. {
  227. { "setup" , ROREG_FUNC(l_netdrv_setup )},
  228. { "dhcp", ROREG_FUNC(l_netdrv_dhcp)},
  229. { "mac", ROREG_FUNC(l_netdrv_mac)},
  230. { "ipv4", ROREG_FUNC(l_netdrv_ipv4)},
  231. { "napt", ROREG_FUNC(l_netdrv_napt)},
  232. { "link", ROREG_FUNC(l_netdrv_link)},
  233. { "ready", ROREG_FUNC(l_netdrv_ready)},
  234. //@const CH390 number 南京沁恒CH390系列,支持CH390D/CH390H, SPI通信
  235. { "CH390", ROREG_INT(1)},
  236. { "CH395", ROREG_INT(2)}, // 考虑兼容Air724UG/Air820UG的老客户
  237. { "W5500", ROREG_INT(3)}, // 考虑兼容Air780E/Air105的老客户
  238. { "UART", ROREG_INT(16)}, // UART形式的网卡, 不带MAC, 直接IP包
  239. { "SPINET", ROREG_INT(32)}, // SPI形式的网卡, 可以带MAC, 也可以不带
  240. { NULL, ROREG_INT(0) }
  241. };
  242. LUAMOD_API int luaopen_netdrv( lua_State *L ) {
  243. luat_newlib2(L, reg_netdrv);
  244. return 1;
  245. }