luat_lib_netdrv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #define LUAT_LOG_TAG "netdrv"
  19. #include "luat_log.h"
  20. /*
  21. 初始化指定netdrv设备
  22. @api netdrv.setup(id, tp, opts)
  23. @int 网络适配器编号, 例如 socket.LWIP_ETH
  24. @int 实现方式,如果是设备自带的硬件,那就不需要传, 外挂设备需要传,当前支持CH390H/D
  25. @int 外挂方式,需要额外的参数,参考示例
  26. @return boolean 初始化成功与否
  27. @usage
  28. -- Air8101初始化内部以太网控制器
  29. netdrv.setup(socket.LWIP_ETH)
  30. -- Air8000/Air780EPM初始化CH390H/D作为LAN口, 单一使用.不含WAN.
  31. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spiid=0,cs=8})
  32. netdrv.dhcp(socket.LWIP_ETH, true)
  33. */
  34. static int l_netdrv_setup(lua_State *L) {
  35. luat_netdrv_conf_t conf = {0};
  36. conf.id = luaL_checkinteger(L, 1);
  37. conf.impl = luaL_optinteger(L, 2, 0);
  38. if (lua_istable(L, 3)) {
  39. if (lua_getfield(L, 3, "spi") == LUA_TNUMBER) {
  40. conf.spiid = luaL_checkinteger(L, -1);
  41. };
  42. lua_pop(L, 1);
  43. if (lua_getfield(L, 3, "cs") == LUA_TNUMBER) {
  44. conf.cspin = luaL_checkinteger(L, -1);
  45. };
  46. lua_pop(L, 1);
  47. if (lua_getfield(L, 3, "irq") == LUA_TNUMBER) {
  48. conf.irqpin = luaL_checkinteger(L, -1);
  49. };
  50. lua_pop(L, 1);
  51. }
  52. luat_netdrv_t* ret = luat_netdrv_setup(&conf);
  53. lua_pushboolean(L, ret != NULL);
  54. return 1;
  55. }
  56. /*
  57. 开启或关闭DHCP
  58. @api netdrv.dhcp(id, enable)
  59. @int 网络适配器编号, 例如 socket.LWIP_ETH
  60. @boolean 开启或者关闭
  61. @return boolean 成功与否
  62. @usgae
  63. -- 注意, 并非所有网络设备都支持关闭DHCP, 例如4G Cat.1
  64. netdrv.dhcp(socket.LWIP_ETH, true)
  65. */
  66. static int l_netdrv_dhcp(lua_State *L) {
  67. int id = luaL_checkinteger(L, 1);
  68. int enable = lua_toboolean(L, 2);
  69. int ret = luat_netdrv_dhcp(id, enable);
  70. lua_pushboolean(L, ret == 0);
  71. return 1;
  72. }
  73. /*
  74. 设置或获取设备MAC
  75. @api netdrv.mac(id, new_mac, raw_string)
  76. @int 网络适配器编号, 例如 socket.LWIP_ETH
  77. @string 新的MAC地址,可选, 必须是6个字节
  78. @boolean 是否返回6字节原始数据, 默认是否, 返回HEX字符串
  79. @return boolean 成功与否
  80. @usage
  81. -- 获取MAC地址
  82. log.info("netdrv", "mac addr", netdrv.mac(socket.LWIP_ETH))
  83. -- 暂不支持设置
  84. */
  85. static int l_netdrv_mac(lua_State *L) {
  86. int id = luaL_checkinteger(L, 1);
  87. char buff[6] = {0};
  88. char tmpbuff[13] = {0};
  89. size_t len = 0;
  90. int ret = 0;
  91. if (lua_isstring(L, 2)) {
  92. const char* tmp = luaL_checklstring(L, 2, &len);
  93. if (len != 6) {
  94. return 0;
  95. }
  96. luat_netdrv_mac(id, tmp, buff);
  97. }
  98. else {
  99. luat_netdrv_mac(id, NULL, buff);
  100. }
  101. if (lua_isboolean(L, 3) && !lua_toboolean(L, 3)) {
  102. lua_pushlstring(L, (const char*)buff, 6);
  103. }
  104. else {
  105. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  106. lua_pushstring(L, tmpbuff);
  107. }
  108. return 1;
  109. }
  110. #include "rotable2.h"
  111. static const rotable_Reg_t reg_netdrv[] =
  112. {
  113. { "setup" , ROREG_FUNC(l_netdrv_setup )},
  114. { "dhcp", ROREG_FUNC(l_netdrv_dhcp)},
  115. { "mac", ROREG_FUNC(l_netdrv_mac)},
  116. { "CH390", ROREG_INT(1)},
  117. { "W5500", ROREG_INT(2)},
  118. { "W5100", ROREG_INT(3)},
  119. { NULL, ROREG_INT(0) }
  120. };
  121. LUAMOD_API int luaopen_netdrv( lua_State *L ) {
  122. luat_newlib2(L, reg_netdrv);
  123. return 1;
  124. }