luat_lib_netdrv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. if (lua_getfield(L, 3, "mtu") == LUA_TNUMBER) {
  57. conf.mtu = luaL_checkinteger(L, -1);
  58. };
  59. lua_pop(L, 1);
  60. if (lua_getfield(L, 3, "flags") == LUA_TNUMBER) {
  61. conf.flags = luaL_checkinteger(L, -1);
  62. };
  63. lua_pop(L, 1);
  64. }
  65. luat_netdrv_t* ret = luat_netdrv_setup(&conf);
  66. lua_pushboolean(L, ret != NULL);
  67. return 1;
  68. }
  69. /*
  70. 开启或关闭DHCP
  71. @api netdrv.dhcp(id, enable)
  72. @int 网络适配器编号, 例如 socket.LWIP_ETH
  73. @boolean 开启或者关闭
  74. @return boolean 成功与否
  75. @usgae
  76. -- 注意, 并非所有网络设备都支持关闭DHCP, 例如4G Cat.1
  77. netdrv.dhcp(socket.LWIP_ETH, true)
  78. */
  79. static int l_netdrv_dhcp(lua_State *L) {
  80. int id = luaL_checkinteger(L, 1);
  81. int enable = lua_toboolean(L, 2);
  82. int ret = luat_netdrv_dhcp(id, enable);
  83. lua_pushboolean(L, ret == 0);
  84. return 1;
  85. }
  86. /*
  87. 设置或获取设备MAC
  88. @api netdrv.mac(id, new_mac, raw_string)
  89. @int 网络适配器编号, 例如 socket.LWIP_ETH
  90. @string 新的MAC地址,可选, 必须是6个字节
  91. @boolean 是否返回6字节原始数据, 默认是否, 返回HEX字符串
  92. @return boolean 成功与否
  93. @usage
  94. -- 获取MAC地址
  95. log.info("netdrv", "mac addr", netdrv.mac(socket.LWIP_ETH))
  96. -- 暂不支持设置
  97. */
  98. static int l_netdrv_mac(lua_State *L) {
  99. int id = luaL_checkinteger(L, 1);
  100. char buff[6] = {0};
  101. char tmpbuff[13] = {0};
  102. size_t len = 0;
  103. if (lua_type(L, 2) == LUA_TSTRING) {
  104. const char* tmp = luaL_checklstring(L, 2, &len);
  105. if (len != 6) {
  106. return 0;
  107. }
  108. luat_netdrv_mac(id, tmp, buff);
  109. }
  110. else {
  111. luat_netdrv_mac(id, NULL, buff);
  112. }
  113. if (lua_isboolean(L, 3) && !lua_toboolean(L, 3)) {
  114. lua_pushlstring(L, (const char*)buff, 6);
  115. }
  116. else {
  117. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  118. lua_pushstring(L, tmpbuff);
  119. }
  120. return 1;
  121. }
  122. /*
  123. 设置或读取ipv4地址
  124. @api netdrv.ipv4(id, addr, mark, gw)
  125. @int 网络适配器编号, 例如 socket.LWIP_ETH
  126. @string ipv4地址,如果是读取就不需要传
  127. @string 掩码
  128. @string 网关
  129. @return string ipv4地址
  130. @return string 掩码
  131. @return string 网关
  132. @usage
  133. -- 注意, 不是所有netdrv都支持设置的, 尤其4G Cat.1自带的netdrv就不能设置ipv4
  134. -- 注意, 设置ipv4时, DHCP要处于关闭状态!!
  135. */
  136. static int l_netdrv_ipv4(lua_State *L) {
  137. int id = luaL_checkinteger(L, 1);
  138. const char* tmp = NULL;
  139. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  140. if (netdrv == NULL || netdrv->netif == NULL) {
  141. return 0;
  142. }
  143. if (lua_isstring(L, 2) && lua_isstring(L, 3) && lua_isstring(L, 4)) {
  144. tmp = luaL_checkstring(L, 2);
  145. ipaddr_aton(tmp, &netdrv->netif->ip_addr);
  146. tmp = luaL_checkstring(L, 3);
  147. ipaddr_aton(tmp, &netdrv->netif->netmask);
  148. tmp = luaL_checkstring(L, 4);
  149. ipaddr_aton(tmp, &netdrv->netif->gw);
  150. net_lwip2_set_link_state(id, 1);
  151. }
  152. char buff[16] = {0};
  153. char buff2[16] = {0};
  154. char buff3[16] = {0};
  155. ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff, 16);
  156. ipaddr_ntoa_r(&netdrv->netif->netmask, buff2, 16);
  157. ipaddr_ntoa_r(&netdrv->netif->gw, buff3, 16);
  158. lua_pushstring(L, buff);
  159. lua_pushstring(L, buff2);
  160. lua_pushstring(L, buff3);
  161. return 3;
  162. }
  163. /*
  164. 开启或关闭NAPT
  165. @api netdrv.napt(id)
  166. @int 网关适配器的id
  167. @return bool 合法值就返回true, 否则返回nil
  168. @usage
  169. -- 使用4G网络作为主网关出口
  170. netdrv.napt(socket.LWIP_GP)
  171. -- 关闭napt功能
  172. netdrv.napt(-1)
  173. */
  174. extern int luat_netdrv_gw_adapter_id;
  175. static int l_netdrv_napt(lua_State *L) {
  176. int id = luaL_checkinteger(L, 1);
  177. if (id < 0) {
  178. LLOGD("NAPT is disabled");
  179. luat_netdrv_gw_adapter_id = id;
  180. lua_pushboolean(L, 1);
  181. return 1;
  182. }
  183. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  184. if (netdrv == NULL || netdrv->netif == NULL) {
  185. return 0;
  186. }
  187. LLOGD("NAPT is enabled gw %d", id);
  188. luat_netdrv_gw_adapter_id = id;
  189. lua_pushboolean(L, 1);
  190. return 1;
  191. }
  192. /*
  193. 获取netdrv的物理连接状态
  194. @api netdrv.link(id)
  195. @int netdrv的id, 例如 socket.LWIP_ETH
  196. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  197. @usage
  198. -- 注意, 本函数仅支持读取, 而且不能ip状态, 即是否能联网
  199. */
  200. static int l_netdrv_link(lua_State *L) {
  201. int id = luaL_checkinteger(L, 1);
  202. if (id < 0) {
  203. return 0; // 非法id
  204. }
  205. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  206. if (netdrv == NULL || netdrv->netif == NULL) {
  207. return 0;
  208. }
  209. lua_pushboolean(L, netif_is_link_up(netdrv->netif));
  210. return 1;
  211. }
  212. /*
  213. 获取netdrv的网络状态
  214. @api netdrv.ready(id)
  215. @int netdrv的id, 例如 socket.LWIP_ETH
  216. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  217. @usage
  218. -- 注意, 本函数仅支持读取, 即判断是否能联网
  219. */
  220. static int l_netdrv_ready(lua_State *L) {
  221. int id = luaL_checkinteger(L, 1);
  222. if (id < 0) {
  223. return 0; // 非法id
  224. }
  225. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  226. if (netdrv == NULL || netdrv->netif == NULL) {
  227. return 0;
  228. }
  229. lua_pushboolean(L, netif_is_link_up(netdrv->netif) && !ip_addr_isany(&netdrv->netif->ip_addr));
  230. return 1;
  231. }
  232. #include "rotable2.h"
  233. static const rotable_Reg_t reg_netdrv[] =
  234. {
  235. { "setup" , ROREG_FUNC(l_netdrv_setup )},
  236. { "dhcp", ROREG_FUNC(l_netdrv_dhcp)},
  237. { "mac", ROREG_FUNC(l_netdrv_mac)},
  238. { "ipv4", ROREG_FUNC(l_netdrv_ipv4)},
  239. { "napt", ROREG_FUNC(l_netdrv_napt)},
  240. { "link", ROREG_FUNC(l_netdrv_link)},
  241. { "ready", ROREG_FUNC(l_netdrv_ready)},
  242. //@const CH390 number 南京沁恒CH390系列,支持CH390D/CH390H, SPI通信
  243. { "CH390", ROREG_INT(1)},
  244. { "CH395", ROREG_INT(2)}, // 考虑兼容Air724UG/Air820UG的老客户
  245. { "W5500", ROREG_INT(3)}, // 考虑兼容Air780EXXX/Air105的老客户
  246. { "UART", ROREG_INT(16)}, // UART形式的网卡, 不带MAC, 直接IP包
  247. { "SPINET", ROREG_INT(32)}, // SPI形式的网卡, 可以带MAC, 也可以不带
  248. { "WHALE", ROREG_INT(64)}, // 通用WHALE设备
  249. { NULL, ROREG_INT(0) }
  250. };
  251. LUAMOD_API int luaopen_netdrv( lua_State *L ) {
  252. luat_newlib2(L, reg_netdrv);
  253. return 1;
  254. }