luat_lib_netdrv.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. -- 支持多个CH390H, 使用不同的CS脚区分不同网口
  36. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8})
  37. netdrv.dhcp(socket.LWIP_ETH, true)
  38. -- 支持CH390H的中断模式, 能提供响应速度, 但是需要外接中断引脚
  39. -- 实测对总网速没有帮助, 轻负载时能降低功耗, 让模组能进入低功耗模式
  40. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8,irq=20})
  41. */
  42. static int l_netdrv_setup(lua_State *L) {
  43. luat_netdrv_conf_t conf = {0};
  44. conf.id = luaL_checkinteger(L, 1);
  45. conf.impl = luaL_optinteger(L, 2, 0);
  46. conf.irqpin = 255; // 默认无效
  47. if (lua_istable(L, 3)) {
  48. if (lua_getfield(L, 3, "spi") == LUA_TNUMBER) {
  49. conf.spiid = luaL_checkinteger(L, -1);
  50. };
  51. lua_pop(L, 1);
  52. if (lua_getfield(L, 3, "cs") == LUA_TNUMBER) {
  53. conf.cspin = luaL_checkinteger(L, -1);
  54. };
  55. lua_pop(L, 1);
  56. if (lua_getfield(L, 3, "irq") == LUA_TNUMBER) {
  57. conf.irqpin = luaL_checkinteger(L, -1);
  58. };
  59. lua_pop(L, 1);
  60. if (lua_getfield(L, 3, "mtu") == LUA_TNUMBER) {
  61. conf.mtu = luaL_checkinteger(L, -1);
  62. };
  63. lua_pop(L, 1);
  64. if (lua_getfield(L, 3, "flags") == LUA_TNUMBER) {
  65. conf.flags = luaL_checkinteger(L, -1);
  66. };
  67. lua_pop(L, 1);
  68. }
  69. luat_netdrv_t* ret = luat_netdrv_setup(&conf);
  70. lua_pushboolean(L, ret != NULL);
  71. return 1;
  72. }
  73. /*
  74. 开启或关闭DHCP
  75. @api netdrv.dhcp(id, enable)
  76. @int 网络适配器编号, 例如 socket.LWIP_ETH
  77. @boolean 开启或者关闭
  78. @return boolean 成功与否
  79. @usgae
  80. -- 注意, 并非所有网络设备都支持关闭DHCP, 例如4G Cat.1
  81. netdrv.dhcp(socket.LWIP_ETH, true)
  82. */
  83. static int l_netdrv_dhcp(lua_State *L) {
  84. int id = luaL_checkinteger(L, 1);
  85. int enable = lua_toboolean(L, 2);
  86. int ret = luat_netdrv_dhcp(id, enable);
  87. lua_pushboolean(L, ret == 0);
  88. return 1;
  89. }
  90. /*
  91. 设置或获取设备MAC
  92. @api netdrv.mac(id, new_mac, raw_string)
  93. @int 网络适配器编号, 例如 socket.LWIP_ETH
  94. @string 新的MAC地址,可选, 必须是6个字节
  95. @boolean 是否返回6字节原始数据, 默认是否, 返回HEX字符串
  96. @return boolean 成功与否
  97. @usage
  98. -- 获取MAC地址
  99. log.info("netdrv", "mac addr", netdrv.mac(socket.LWIP_ETH))
  100. -- 暂不支持设置
  101. */
  102. static int l_netdrv_mac(lua_State *L) {
  103. int id = luaL_checkinteger(L, 1);
  104. char buff[6] = {0};
  105. char tmpbuff[13] = {0};
  106. size_t len = 0;
  107. if (lua_type(L, 2) == LUA_TSTRING) {
  108. const char* tmp = luaL_checklstring(L, 2, &len);
  109. if (len != 6) {
  110. return 0;
  111. }
  112. luat_netdrv_mac(id, tmp, buff);
  113. }
  114. else {
  115. luat_netdrv_mac(id, NULL, buff);
  116. }
  117. if (lua_isboolean(L, 3) && !lua_toboolean(L, 3)) {
  118. lua_pushlstring(L, (const char*)buff, 6);
  119. }
  120. else {
  121. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  122. lua_pushstring(L, tmpbuff);
  123. }
  124. return 1;
  125. }
  126. /*
  127. 设置或读取ipv4地址
  128. @api netdrv.ipv4(id, addr, mark, gw)
  129. @int 网络适配器编号, 例如 socket.LWIP_ETH
  130. @string ipv4地址,如果是读取就不需要传
  131. @string 掩码
  132. @string 网关
  133. @return string ipv4地址
  134. @return string 掩码
  135. @return string 网关
  136. @usage
  137. -- 注意, 不是所有netdrv都支持设置的, 尤其4G Cat.1自带的netdrv就不能设置ipv4
  138. -- 注意, 设置ipv4时, DHCP要处于关闭状态!!
  139. -- 当前设置ip但ip值非法, 不返回任何东西
  140. -- 如果设置ip且ip值合法, 会返回ip, mask, gw
  141. */
  142. static int l_netdrv_ipv4(lua_State *L) {
  143. int id = luaL_checkinteger(L, 1);
  144. const char* tmp = NULL;
  145. luat_ip_addr_t ip;
  146. luat_ip_addr_t netmask;
  147. luat_ip_addr_t gw;
  148. int ret = 0;
  149. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  150. if (netdrv == NULL || netdrv->netif == NULL) {
  151. return 0;
  152. }
  153. if (lua_isstring(L, 2) && lua_isstring(L, 3) && lua_isstring(L, 4)) {
  154. luat_netdrv_dhcp(id, 0); // 自动关闭DHCP
  155. tmp = luaL_checkstring(L, 2);
  156. ret = ipaddr_aton(tmp, &ip);
  157. if (!ret) {
  158. LLOGW("非法IP[%d] %s %d", id, tmp, ret);
  159. return 0;
  160. }
  161. tmp = luaL_checkstring(L, 3);
  162. ret = ipaddr_aton(tmp, &netmask);
  163. if (!ret) {
  164. LLOGW("非法MARK[%d] %s %d", id, tmp, ret);
  165. return 0;
  166. }
  167. tmp = luaL_checkstring(L, 4);
  168. ret = ipaddr_aton(tmp, &gw);
  169. if (ret == 0) {
  170. LLOGW("非法GW[%d] %s %d", id, tmp, ret);
  171. return 0;
  172. }
  173. network_set_static_ip_info(id, &ip, &netmask, &gw, NULL);
  174. }
  175. char buff[16] = {0};
  176. char buff2[16] = {0};
  177. char buff3[16] = {0};
  178. ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff, 16);
  179. ipaddr_ntoa_r(&netdrv->netif->netmask, buff2, 16);
  180. ipaddr_ntoa_r(&netdrv->netif->gw, buff3, 16);
  181. lua_pushstring(L, buff);
  182. lua_pushstring(L, buff2);
  183. lua_pushstring(L, buff3);
  184. return 3;
  185. }
  186. /*
  187. 开启或关闭NAPT
  188. @api netdrv.napt(id)
  189. @int 网关适配器的id
  190. @return bool 合法值就返回true, 否则返回nil
  191. @usage
  192. -- 使用4G网络作为主网关出口
  193. netdrv.napt(socket.LWIP_GP)
  194. -- 关闭napt功能
  195. netdrv.napt(-1)
  196. */
  197. extern int luat_netdrv_gw_adapter_id;
  198. static int l_netdrv_napt(lua_State *L) {
  199. int id = luaL_checkinteger(L, 1);
  200. if (id < 0) {
  201. LLOGD("NAPT is disabled");
  202. luat_netdrv_gw_adapter_id = id;
  203. lua_pushboolean(L, 1);
  204. return 1;
  205. }
  206. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  207. if (netdrv == NULL || netdrv->netif == NULL) {
  208. return 0;
  209. }
  210. LLOGD("NAPT is enabled gw %d", id);
  211. luat_netdrv_gw_adapter_id = id;
  212. lua_pushboolean(L, 1);
  213. return 1;
  214. }
  215. /*
  216. 获取netdrv的物理连接状态
  217. @api netdrv.link(id)
  218. @int netdrv的id, 例如 socket.LWIP_ETH
  219. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  220. @usage
  221. -- 注意, 本函数仅支持读取, 而且不能ip状态, 即是否能联网
  222. */
  223. static int l_netdrv_link(lua_State *L) {
  224. int id = luaL_checkinteger(L, 1);
  225. if (id < 0) {
  226. return 0; // 非法id
  227. }
  228. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  229. if (netdrv == NULL || netdrv->netif == NULL) {
  230. return 0;
  231. }
  232. lua_pushboolean(L, netif_is_link_up(netdrv->netif));
  233. return 1;
  234. }
  235. /*
  236. 获取netdrv的网络状态
  237. @api netdrv.ready(id)
  238. @int netdrv的id, 例如 socket.LWIP_ETH
  239. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  240. @usage
  241. -- 注意, 本函数仅支持读取, 即判断是否能通信, 不代表IP状态
  242. */
  243. static int l_netdrv_ready(lua_State *L) {
  244. int id = luaL_checkinteger(L, 1);
  245. if (id < 0) {
  246. return 0; // 非法id
  247. }
  248. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  249. if (netdrv == NULL || netdrv->netif == NULL) {
  250. return 0;
  251. }
  252. lua_pushboolean(L, netif_is_link_up(netdrv->netif) && !ip_addr_isany(&netdrv->netif->ip_addr));
  253. return 1;
  254. }
  255. /*
  256. 给具体的驱动发送控制指令
  257. @api netdrv.ctrl(id, cmd, arg)
  258. @int 网络适配器编号, 例如 socket.LWIP_ETH
  259. @int 指令, 例如 netdrv.CTRL_RESET
  260. @int 参数, 例如 netdrv.RESET_HARD
  261. @return boolean 成功与否
  262. @usage
  263. -- 重启网卡, 仅CH390H支持, 其他网络设备暂不支持
  264. -- 本函数于 2025.4.14 新增
  265. netdrv.ctrl(socket.LWIP_ETH, netdrv.CTRL_RESET, netdrv.RESET_HARD)
  266. */
  267. static int l_netdrv_ctrl(lua_State *L) {
  268. int id = luaL_checkinteger(L, 1);
  269. int cmd = luaL_checkinteger(L, 2);
  270. int arg = luaL_checkinteger(L, 3);
  271. luat_netdrv_t* drv = luat_netdrv_get(id);
  272. if (drv == NULL) {
  273. LLOGW("not such netdrv %d", id);
  274. return 0;
  275. }
  276. if (drv->ctrl == NULL) {
  277. LLOGW("netdrv %d not support ctrl", id);
  278. return 0;
  279. }
  280. int ret = drv->ctrl(drv, drv->userdata, cmd, arg);
  281. lua_pushboolean(L, ret == 0);
  282. lua_pushinteger(L, ret);
  283. return 2;
  284. }
  285. #include "rotable2.h"
  286. static const rotable_Reg_t reg_netdrv[] =
  287. {
  288. { "setup" , ROREG_FUNC(l_netdrv_setup )},
  289. { "dhcp", ROREG_FUNC(l_netdrv_dhcp)},
  290. { "mac", ROREG_FUNC(l_netdrv_mac)},
  291. { "ipv4", ROREG_FUNC(l_netdrv_ipv4)},
  292. { "napt", ROREG_FUNC(l_netdrv_napt)},
  293. { "link", ROREG_FUNC(l_netdrv_link)},
  294. { "ready", ROREG_FUNC(l_netdrv_ready)},
  295. { "ctrl", ROREG_FUNC(l_netdrv_ctrl)},
  296. //@const CH390 number 南京沁恒CH390系列,支持CH390D/CH390H, SPI通信
  297. { "CH390", ROREG_INT(1)},
  298. { "UART", ROREG_INT(16)}, // UART形式的网卡, 不带MAC, 直接IP包
  299. //@const WHALE number 虚拟网卡
  300. { "WHALE", ROREG_INT(64)}, // 通用WHALE设备
  301. //@const CTRL_RESET number 控制类型-复位,当前仅支持CH390H
  302. { "CTRL_RESET", ROREG_INT(LUAT_NETDRV_CTRL_RESET)},
  303. //@const CTRL_RESET number 请求对网卡硬复位,当前仅支持CH390H
  304. { "RESET_HARD", ROREG_INT(0x101)},
  305. //@const CTRL_RESET number 请求对网卡硬复位,当前仅支持CH390H
  306. { "RESET_SOFT", ROREG_INT(0x102)},
  307. { NULL, ROREG_INT(0) }
  308. };
  309. LUAMOD_API int luaopen_netdrv( lua_State *L ) {
  310. luat_newlib2(L, reg_netdrv);
  311. return 1;
  312. }