luat_lib_netdrv.c 10 KB

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