luat_lib_netdrv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 "luat_netdrv_event.h"
  21. #include "net_lwip2.h"
  22. #include "lwip/ip.h"
  23. #include "lwip/ip4.h"
  24. #define LUAT_LOG_TAG "netdrv"
  25. #include "luat_log.h"
  26. /*
  27. 初始化指定netdrv设备
  28. @api netdrv.setup(id, tp, opts)
  29. @int 网络适配器编号, 例如 socket.LWIP_ETH
  30. @int 实现方式,如果是设备自带的硬件,那就不需要传, 外挂设备需要传,当前支持CH390H/D
  31. @int 外挂方式,需要额外的参数,参考示例
  32. @return boolean 初始化成功与否
  33. @usage
  34. -- Air8101初始化内部以太网控制器
  35. netdrv.setup(socket.LWIP_ETH)
  36. -- Air8000/Air780EPM初始化CH390H/D作为LAN/WAN
  37. -- 支持多个CH390H, 使用不同的CS脚区分不同网口
  38. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8})
  39. netdrv.dhcp(socket.LWIP_ETH, true)
  40. -- 支持CH390H的中断模式, 能提供响应速度, 但是需要外接中断引脚
  41. -- 实测对总网速没有帮助, 轻负载时能降低功耗, 让模组能进入低功耗模式
  42. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {spi=0,cs=8,irq=20})
  43. */
  44. static int l_netdrv_setup(lua_State *L) {
  45. luat_netdrv_conf_t conf = {0};
  46. size_t len = 0;
  47. conf.id = luaL_checkinteger(L, 1);
  48. conf.impl = luaL_optinteger(L, 2, 0);
  49. conf.irqpin = 255; // 默认无效
  50. if (lua_istable(L, 3)) {
  51. if (lua_getfield(L, 3, "spi") == LUA_TNUMBER) {
  52. conf.spiid = luaL_checkinteger(L, -1);
  53. };
  54. lua_pop(L, 1);
  55. if (lua_getfield(L, 3, "cs") == LUA_TNUMBER) {
  56. conf.cspin = luaL_checkinteger(L, -1);
  57. };
  58. lua_pop(L, 1);
  59. if (lua_getfield(L, 3, "irq") == LUA_TNUMBER) {
  60. conf.irqpin = luaL_checkinteger(L, -1);
  61. };
  62. lua_pop(L, 1);
  63. if (lua_getfield(L, 3, "mtu") == LUA_TNUMBER) {
  64. conf.mtu = luaL_checkinteger(L, -1);
  65. };
  66. lua_pop(L, 1);
  67. if (lua_getfield(L, 3, "flags") == LUA_TNUMBER) {
  68. conf.flags = luaL_checkinteger(L, -1);
  69. };
  70. lua_pop(L, 1);
  71. #ifdef LUAT_USE_NETDRV_WG
  72. // WG的配置参数比较多, 放在这里面传递
  73. // 需要的参数有, private_key, public_key, endpoint, port, address, dns, mtu
  74. if (lua_getfield(L, 3, "wg_private_key") == LUA_TSTRING) {
  75. conf.wg_private_key = luaL_checklstring(L, -1, &len);
  76. };
  77. lua_pop(L, 1);
  78. // 本地端口
  79. if (lua_getfield(L, 3, "wg_listen_port") == LUA_TNUMBER) {
  80. conf.wg_listen_port = luaL_checkinteger(L, -1);
  81. };
  82. lua_pop(L, 1);
  83. // keepalive时长
  84. if (lua_getfield(L, 3, "wg_keepalive") == LUA_TNUMBER) {
  85. conf.wg_keepalive = luaL_checkinteger(L, -1);
  86. };
  87. lua_pop(L, 1);
  88. // 预分享密钥
  89. if (lua_getfield(L, 3, "wg_preshared_key") == LUA_TSTRING) {
  90. conf.wg_preshared_key = luaL_checklstring(L, -1, &len);
  91. };
  92. lua_pop(L, 1);
  93. // 对端信息, 公钥, IP地址, 端口
  94. if (lua_getfield(L, 3, "wg_endpoint_key") == LUA_TSTRING) {
  95. conf.wg_endpoint_key = luaL_checklstring(L, -1, &len);
  96. };
  97. lua_pop(L, 1);
  98. if (lua_getfield(L, 3, "wg_endpoint_ip") == LUA_TSTRING) {
  99. conf.wg_endpoint_ip = luaL_checklstring(L, -1, &len);
  100. };
  101. lua_pop(L, 1);
  102. if (lua_getfield(L, 3, "wg_endpoint_port") == LUA_TNUMBER) {
  103. conf.wg_endpoint_port = luaL_checkinteger(L, -1);
  104. };
  105. lua_pop(L, 1);
  106. #endif
  107. }
  108. luat_netdrv_t* ret = luat_netdrv_setup(&conf);
  109. lua_pushboolean(L, ret != NULL);
  110. return 1;
  111. }
  112. /*
  113. 开启或关闭DHCP
  114. @api netdrv.dhcp(id, enable, name)
  115. @int 网络适配器编号, 例如 socket.LWIP_ETH
  116. @boolean 开启或者关闭
  117. @string dhcp主机名称, 可选, 最长31字节,填""清除
  118. @return boolean 成功与否
  119. @usgae
  120. -- 注意, 并非所有网络设备都支持关闭DHCP, 例如4G Cat.1自带的netdrv就不支持关闭DHCP
  121. -- name参数于2025.9.23添加
  122. netdrv.dhcp(socket.LWIP_ETH, true)
  123. netdrv.dhcp(socket.LWIP_ETH, true, "LuatOS")
  124. */
  125. static int l_netdrv_dhcp(lua_State *L) {
  126. int id = luaL_checkinteger(L, 1);
  127. int enable = lua_toboolean(L, 2);
  128. if (lua_isstring(L, 3)) {
  129. size_t len = 0;
  130. const char* data = NULL;
  131. luat_netdrv_t *drv = NULL;
  132. data = luaL_checklstring(L, 3, &len);
  133. drv = luat_netdrv_get(id);
  134. if(((len + 1) > 32) || (drv == NULL) || (drv->ulwip == NULL)) {
  135. LLOGD("adapter %d dhcp name set fail", id);
  136. lua_pushboolean(L, 0);
  137. return 1;
  138. }
  139. if(0 == len){
  140. memset(drv->ulwip->dhcp_client.name, 0x00, 32);
  141. } else {
  142. memcpy(drv->ulwip->dhcp_client.name, data, len + 1);
  143. }
  144. }
  145. int ret = luat_netdrv_dhcp(id, enable);
  146. lua_pushboolean(L, ret == 0);
  147. return 1;
  148. }
  149. /*
  150. 设置或获取设备MAC
  151. @api netdrv.mac(id, new_mac, raw_string)
  152. @int 网络适配器编号, 例如 socket.LWIP_ETH
  153. @string 新的MAC地址,可选, 必须是6个字节
  154. @boolean 是否返回6字节原始数据, 默认是否, 返回HEX字符串
  155. @return boolean 成功与否
  156. @usage
  157. -- 获取MAC地址
  158. log.info("netdrv", "mac addr", netdrv.mac(socket.LWIP_ETH))
  159. -- 暂不支持设置
  160. */
  161. static int l_netdrv_mac(lua_State *L) {
  162. int id = luaL_checkinteger(L, 1);
  163. uint8_t buff[6] = {0};
  164. char tmpbuff[13] = {0};
  165. size_t len = 0;
  166. if (lua_type(L, 2) == LUA_TSTRING) {
  167. const char* tmp = luaL_checklstring(L, 2, &len);
  168. if (len != 6) {
  169. return 0;
  170. }
  171. luat_netdrv_mac(id, tmp, (char*)buff);
  172. }
  173. else {
  174. luat_netdrv_mac(id, NULL, (char*)buff);
  175. }
  176. if (lua_isboolean(L, 3) && !lua_toboolean(L, 3)) {
  177. lua_pushlstring(L, (const char*)buff, 6);
  178. }
  179. else {
  180. sprintf_(tmpbuff, "%02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  181. lua_pushstring(L, tmpbuff);
  182. }
  183. return 1;
  184. }
  185. /*
  186. 设置或读取ipv4地址
  187. @api netdrv.ipv4(id, addr, mark, gw)
  188. @int 网络适配器编号, 例如 socket.LWIP_ETH
  189. @string ipv4地址,如果是读取就不需要传
  190. @string 掩码
  191. @string 网关
  192. @return string ipv4地址
  193. @return string 掩码
  194. @return string 网关
  195. @usage
  196. -- 注意, 不是所有netdrv都支持设置的, 尤其4G Cat.1自带的netdrv就不能设置ipv4
  197. -- 注意, 设置ipv4时, DHCP要处于关闭状态!!
  198. -- 当前设置ip但ip值非法, 不返回任何东西
  199. -- 如果设置ip且ip值合法, 会返回ip, mask, gw
  200. */
  201. static int l_netdrv_ipv4(lua_State *L) {
  202. int id = luaL_checkinteger(L, 1);
  203. const char* tmp = NULL;
  204. luat_ip_addr_t ip;
  205. luat_ip_addr_t netmask;
  206. luat_ip_addr_t gw;
  207. int ret = 0;
  208. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  209. if (netdrv == NULL || netdrv->netif == NULL) {
  210. LLOGW("对应的netdrv不存在或未就绪 %d %p", id, netdrv);
  211. return 0;
  212. }
  213. if (lua_isstring(L, 2) && lua_isstring(L, 3) && lua_isstring(L, 4)) {
  214. luat_netdrv_dhcp(id, 0); // 自动关闭DHCP
  215. tmp = luaL_checkstring(L, 2);
  216. ret = ipaddr_aton(tmp, &ip);
  217. if (!ret) {
  218. LLOGW("非法IP[%d] %s %d", id, tmp, ret);
  219. return 0;
  220. }
  221. tmp = luaL_checkstring(L, 3);
  222. ret = ipaddr_aton(tmp, &netmask);
  223. if (!ret) {
  224. LLOGW("非法MARK[%d] %s %d", id, tmp, ret);
  225. return 0;
  226. }
  227. tmp = luaL_checkstring(L, 4);
  228. ret = ipaddr_aton(tmp, &gw);
  229. if (ret == 0) {
  230. LLOGW("非法GW[%d] %s %d", id, tmp, ret);
  231. return 0;
  232. }
  233. network_set_static_ip_info(id, &ip, &netmask, &gw, NULL);
  234. }
  235. char buff[16] = {0};
  236. char buff2[16] = {0};
  237. char buff3[16] = {0};
  238. ipaddr_ntoa_r(&netdrv->netif->ip_addr, buff, 16);
  239. ipaddr_ntoa_r(&netdrv->netif->netmask, buff2, 16);
  240. ipaddr_ntoa_r(&netdrv->netif->gw, buff3, 16);
  241. lua_pushstring(L, buff);
  242. lua_pushstring(L, buff2);
  243. lua_pushstring(L, buff3);
  244. return 3;
  245. }
  246. /*
  247. 开启或关闭NAPT
  248. @api netdrv.napt(id)
  249. @int 网关适配器的id
  250. @return bool 合法值就返回true, 否则返回nil
  251. @usage
  252. -- 使用4G网络作为主网关出口
  253. netdrv.napt(socket.LWIP_GP)
  254. -- 关闭napt功能
  255. netdrv.napt(-1)
  256. */
  257. static int l_netdrv_napt(lua_State *L) {
  258. int id = luaL_checkinteger(L, 1);
  259. if (id < 0) {
  260. LLOGD("NAPT is disabled");
  261. luat_netdrv_napt_enable(id);
  262. lua_pushboolean(L, 1);
  263. return 1;
  264. }
  265. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  266. if (netdrv == NULL || netdrv->netif == NULL) {
  267. LLOGE("对应的网关netdrv不存在或未就绪 %d", id);
  268. return 0;
  269. }
  270. LLOGD("NAPT is enabled gw %d", id);
  271. luat_netdrv_napt_enable(id);
  272. lua_pushboolean(L, 1);
  273. return 1;
  274. }
  275. /*
  276. 获取netdrv的物理连接状态
  277. @api netdrv.link(id)
  278. @int netdrv的id, 例如 socket.LWIP_ETH
  279. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  280. @usage
  281. -- 注意, 本函数仅支持读取, 而且不能ip状态, 即是否能联网
  282. */
  283. static int l_netdrv_link(lua_State *L) {
  284. int id = luaL_checkinteger(L, 1);
  285. if (id < 0) {
  286. return 0; // 非法id
  287. }
  288. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  289. if (netdrv == NULL || netdrv->netif == NULL) {
  290. return 0;
  291. }
  292. lua_pushboolean(L, netif_is_link_up(netdrv->netif));
  293. return 1;
  294. }
  295. /*
  296. 获取netdrv的网络状态
  297. @api netdrv.ready(id)
  298. @int netdrv的id, 例如 socket.LWIP_ETH
  299. @return bool 已连接返回true, 否则返回false. 如果id对应的netdrv不存在,返回nil
  300. @usage
  301. -- 注意, 本函数仅支持读取, 即判断是否能通信, 不代表IP状态
  302. */
  303. static int l_netdrv_ready(lua_State *L) {
  304. int id = luaL_checkinteger(L, 1);
  305. if (id < 0) {
  306. return 0; // 非法id
  307. }
  308. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  309. if (netdrv == NULL || netdrv->netif == NULL) {
  310. return 0;
  311. }
  312. lua_pushboolean(L, netif_is_link_up(netdrv->netif) && !ip_addr_isany(&netdrv->netif->ip_addr));
  313. return 1;
  314. }
  315. /*
  316. 给具体的驱动发送控制指令
  317. @api netdrv.ctrl(id, cmd, arg)
  318. @int 网络适配器编号, 例如 socket.LWIP_ETH
  319. @int 指令, 例如 netdrv.CTRL_RESET
  320. @int 参数, 例如 netdrv.RESET_HARD
  321. @return boolean 成功与否
  322. @usage
  323. -- 重启网卡, 仅CH390H支持, 其他网络设备暂不支持
  324. -- 本函数于 2025.4.14 新增
  325. netdrv.ctrl(socket.LWIP_ETH, netdrv.CTRL_RESET, netdrv.RESET_HARD)
  326. */
  327. static int l_netdrv_ctrl(lua_State *L) {
  328. int id = luaL_checkinteger(L, 1);
  329. int cmd = luaL_checkinteger(L, 2);
  330. int arg = luaL_checkinteger(L, 3);
  331. luat_netdrv_t* drv = luat_netdrv_get(id);
  332. if (drv == NULL) {
  333. LLOGW("not such netdrv %d", id);
  334. return 0;
  335. }
  336. if (drv->ctrl == NULL) {
  337. LLOGW("netdrv %d not support ctrl", id);
  338. return 0;
  339. }
  340. int ret = drv->ctrl(drv, drv->userdata, cmd, arg);
  341. lua_pushboolean(L, ret == 0);
  342. lua_pushinteger(L, ret);
  343. return 2;
  344. }
  345. /*
  346. 设置调试信息输出
  347. @api netdrv.debug(id, enable)
  348. @int 网络适配器编号, 例如 socket.LWIP_ETH, 如果传0就是全局调试开关
  349. @boolean 是否开启调试信息输出
  350. @return boolean 成功与否
  351. @usage
  352. -- 打开netdrv全局调试开关
  353. netdrv.debug(0, true)
  354. */
  355. static int l_netdrv_debug(lua_State *L) {
  356. int id = luaL_checkinteger(L, 1);
  357. int enable = lua_toboolean(L, 2);
  358. luat_netdrv_debug_set(id, enable);
  359. return 0;
  360. }
  361. /*
  362. 设置遥测功能,开启后,会自动上报设备信息,2025/9/25启用
  363. @api netdrv.mreport(config, value)
  364. @string 配置项
  365. @boolean 设置功能开关
  366. @return boolean 成功与否
  367. @usage
  368. -- 设置开启与关闭
  369. netdrv.mreport("enable", true)
  370. netdrv.mreport("enable", false)
  371. -- 设置使用的网络适配器,2025/10/30启用
  372. netdrv.mreport("adapter_id", socket.LWIP_GP)
  373. netdrv.mreport("adapter_id", socket.LWIP_STA)
  374. netdrv.mreport("adapter_id", socket.LWIP_ETH)
  375. -- 立即上报一次, 无参数的方式调用
  376. netdrv.mreport()
  377. */
  378. extern int l_mreport_config(lua_State* L);
  379. /*
  380. 发起ping(异步的)
  381. @api netdrv.ping(id, ip, len)
  382. @int 网络适配器的id
  383. @string 目标ip地址,不支持域名!!
  384. @int ping包大小,默认128字节,可以不传
  385. @return bool 成功与否, 仅代表发送与否,不代表服务器已经响应
  386. @usage
  387. -- 本功能在2025.9.3新增
  388. sys.taskInit(function()
  389. -- 要等联网了才能ping
  390. sys.waitUntil("IP_READY")
  391. sys.wait(1000)
  392. while 1 do
  393. -- 必须指定使用哪个网卡
  394. netdrv.ping(socket.LWIP_GP, "121.14.77.221")
  395. sys.waitUntil("PING_RESULT", 3000)
  396. sys.wait(3000)
  397. end
  398. end)
  399. sys.subscribe("PING_RESULT", function(id, time, dst)
  400. log.info("ping", id, time, dst);
  401. end)
  402. */
  403. extern int l_icmp_ping(lua_State *L);
  404. static int s_socket_evt_ref[NW_ADAPTER_QTY] = {0};
  405. static int l_socket_evt_cb(lua_State *L, void* ptr) {
  406. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  407. netdrv_tcp_evt_t* evt = (netdrv_tcp_evt_t*)ptr;
  408. int ref = s_socket_evt_ref[evt->id];
  409. if (ref == 0) {
  410. LLOGW("socket evt cb no lua ref");
  411. luat_heap_free(ptr);
  412. return 0;
  413. }
  414. // LLOGD("socket evt cb %d %d lua function %d", evt->id, evt->flags, ref);
  415. // 取出函数
  416. lua_geti(L, LUA_REGISTRYINDEX, ref);
  417. if (!lua_isfunction(L, -1)) {
  418. LLOGW("socket evt cb ref not function");
  419. lua_pop(L, 1);
  420. luat_heap_free(ptr);
  421. return 0;
  422. }
  423. lua_pushinteger(L, evt->id);
  424. switch (evt->flags)
  425. {
  426. case 0x81:
  427. lua_pushstring(L, "create");
  428. break;
  429. case 0x82:
  430. lua_pushstring(L, "release");
  431. break;
  432. case 0x83:
  433. lua_pushstring(L, "connecting");
  434. break;
  435. case EV_NW_TIMEOUT - EV_NW_RESET:
  436. lua_pushstring(L, "timeout");
  437. break;
  438. case EV_NW_SOCKET_CLOSE_OK - EV_NW_RESET:
  439. lua_pushstring(L, "closed");
  440. break;
  441. case EV_NW_SOCKET_CONNECT_OK - EV_NW_RESET:
  442. lua_pushstring(L, "connected");
  443. break;
  444. case EV_NW_SOCKET_REMOTE_CLOSE - EV_NW_RESET:
  445. lua_pushstring(L, "remote_close");
  446. break;
  447. case EV_NW_SOCKET_ERROR - EV_NW_RESET:
  448. lua_pushstring(L, "error");
  449. break;
  450. case EV_NW_DNS_RESULT - EV_NW_RESET:
  451. lua_pushstring(L, "dns_result");
  452. break;
  453. default:
  454. lua_pushstring(L, "unknown");
  455. break;
  456. }
  457. lua_newtable(L);
  458. // 填充参数表 远端ip, 远端端口, 本地ip, 本地端口
  459. char buff[32] = {0};
  460. if (!ip_addr_isany(&evt->remote_ip)) {
  461. ipaddr_ntoa_r(&evt->remote_ip, buff, 32);
  462. lua_pushstring(L, buff);
  463. lua_setfield(L, -2, "remote_ip");
  464. }
  465. if (!ip_addr_isany(&evt->online_ip)) {
  466. ipaddr_ntoa_r(&evt->online_ip, buff, 32);
  467. lua_pushstring(L, buff);
  468. lua_setfield(L, -2, "online_ip");
  469. }
  470. lua_pushinteger(L, evt->remote_port);
  471. lua_setfield(L, -2, "remote_port");
  472. switch (evt->proto)
  473. {
  474. case 1:
  475. lua_pushstring(L, "tcp");
  476. break;
  477. case 2:
  478. lua_pushstring(L, "udp");
  479. break;
  480. case 3:
  481. lua_pushstring(L, "http");
  482. break;
  483. case 4:
  484. lua_pushstring(L, "mqtt");
  485. break;
  486. case 5:
  487. lua_pushstring(L, "websocket");
  488. break;
  489. default:
  490. lua_pushstring(L, "unknown");
  491. break;
  492. }
  493. lua_setfield(L, -2, "proto");
  494. // p = ipaddr_ntoa_r(&evt->local_ip, buff, 32);
  495. // lua_pushstring(L, p);
  496. // lua_setfield(L, -2, "local_ip");
  497. // lua_pushinteger(L, evt->local_port);
  498. // lua_setfield(L, -2, "local_port");
  499. if (evt->domain_name[0]) {
  500. lua_pushstring(L, evt->domain_name);
  501. lua_setfield(L, -2, "domain_name");
  502. }
  503. lua_call(L, 3, 0);
  504. // 释放内存
  505. luat_heap_free(ptr);
  506. return 0;
  507. }
  508. static void luat_socket_evt_cb(netdrv_tcp_evt_t* evt, void* userdata) {
  509. rtos_msg_t msg = {0};
  510. msg.handler = l_socket_evt_cb;
  511. msg.ptr = luat_heap_malloc(sizeof(netdrv_tcp_evt_t));
  512. if (msg.ptr == NULL) {
  513. LLOGE("socket evt cb no mem");
  514. return;
  515. }
  516. memcpy(msg.ptr, evt, sizeof(netdrv_tcp_evt_t));
  517. luat_msgbus_put(&msg, 0);
  518. }
  519. // 监听socket事件
  520. /*
  521. 订阅网络事件
  522. @api netdrv.on(adapter_id, event_type, callback)
  523. @int 网络适配器的id
  524. @int 事件总类型, 当前支持 netdrv.EVT_SOCKET
  525. @function 回调函数 function(id, event, params)
  526. @return bool 成功与否,成功返回true,否则返回nil
  527. @usage
  528. -- 订阅socket连接状态变化事件
  529. netdrv.on(socket.LWIP_ETH, netdrv.EVT_SOCKET, function(id, event, params)
  530. -- id 是网络适配器id
  531. -- event是事件id, 字符串类型,
  532. - create 创建socket对象
  533. - release 释放socket对象
  534. - connecting 正在连接, 域名解析成功后出现
  535. - connected 连接成功, TCP三次握手成功后出现
  536. - closed 连接关闭
  537. - remote_close 远程关闭, 网络中断,或者服务器主动断开
  538. - timeout dns解析超时,或者tcp连接超时
  539. - error 错误,包括一切异常错误
  540. -- params是参数表
  541. - remote_ip 远端ip地址,未必存在
  542. - remote_port 远端端口,未必存在
  543. - online_ip 实际连接的ip地址,未必存在
  544. - domain_name 远端域名,如果是通过域名连接的话, release时没有这个值, create时也没有
  545. log.info("netdrv", "socket event", id, event, json.encode(params or {}))
  546. if params then
  547. -- params里会有remote_ip, remote_port等信息, 可按需获取
  548. local remote_ip = params.remote_ip
  549. local remote_port = params.remote_port
  550. local domain_name = params.domain_name
  551. log.info("netdrv", "socket event", "remote_ip", remote_ip, "remote_port", remote_port, "domain_name", domain_name)
  552. end
  553. end)
  554. */
  555. static int l_netdrv_on(lua_State *L) {
  556. int id = luaL_checkinteger(L, 1);
  557. if (id < 0) {
  558. return 0; // 非法id
  559. }
  560. luat_netdrv_t* netdrv = luat_netdrv_get(id);
  561. if (netdrv == NULL || netdrv->netif == NULL) {
  562. return 0;
  563. }
  564. int event_id = luaL_checkinteger(L, 2);
  565. if (event_id == 0) {
  566. if (s_socket_evt_ref[id]) {
  567. luaL_unref(L, LUA_REGISTRYINDEX, s_socket_evt_ref[id]);
  568. s_socket_evt_ref[id] = 0;
  569. }
  570. luat_netdrv_register_socket_event_cb(id, 0, NULL, NULL);
  571. lua_pushboolean(L, 1);
  572. return 1;
  573. }
  574. else if (event_id == 1) {
  575. if (!lua_isfunction(L, 3)) {
  576. return 0;
  577. }
  578. lua_pushvalue(L, 3);
  579. s_socket_evt_ref[id] = luaL_ref(L, LUA_REGISTRYINDEX);
  580. // LLOGD("register socket event cb %d", s_socket_evt_ref[id]);
  581. luat_netdrv_register_socket_event_cb(id, 0xFF, luat_socket_evt_cb, NULL);
  582. lua_pushboolean(L, 1);
  583. return 1;
  584. }
  585. LLOGW("not support event type %d", event_id);
  586. return 0;
  587. }
  588. #include "rotable2.h"
  589. static const rotable_Reg_t reg_netdrv[] =
  590. {
  591. { "setup" , ROREG_FUNC(l_netdrv_setup )},
  592. { "dhcp", ROREG_FUNC(l_netdrv_dhcp)},
  593. { "mac", ROREG_FUNC(l_netdrv_mac)},
  594. { "ipv4", ROREG_FUNC(l_netdrv_ipv4)},
  595. { "napt", ROREG_FUNC(l_netdrv_napt)},
  596. { "link", ROREG_FUNC(l_netdrv_link)},
  597. { "ready", ROREG_FUNC(l_netdrv_ready)},
  598. { "ctrl", ROREG_FUNC(l_netdrv_ctrl)},
  599. { "debug", ROREG_FUNC(l_netdrv_debug)},
  600. { "on",ROREG_FUNC(l_netdrv_on)},
  601. #ifdef LUAT_USE_MREPORT
  602. { "mreport", ROREG_FUNC(l_mreport_config)},
  603. #endif
  604. #ifdef LUAT_USE_ICMP
  605. { "ping", ROREG_FUNC(l_icmp_ping)},
  606. #endif
  607. //@const CH390 number 南京沁恒CH390系列,支持CH390D/CH390H, SPI通信
  608. { "CH390", ROREG_INT(1)},
  609. { "UART", ROREG_INT(16)}, // UART形式的网卡, 不带MAC, 直接IP包
  610. #ifdef LUAT_USE_NETDRV_WG
  611. { "WG", ROREG_INT(32)}, // Wireguard VPN网卡
  612. #endif
  613. //@const WHALE number 虚拟网卡
  614. { "WHALE", ROREG_INT(64)}, // 通用WHALE设备
  615. //@const CTRL_RESET number 控制类型-复位,当前仅支持CH390H
  616. { "CTRL_RESET", ROREG_INT(LUAT_NETDRV_CTRL_RESET)},
  617. //@const RESET_HARD number 请求对网卡硬复位,当前仅支持CH390H
  618. { "RESET_HARD", ROREG_INT(0x101)},
  619. //@const RESET_SOFT number 请求对网卡软复位,当前仅支持CH390H
  620. { "RESET_SOFT", ROREG_INT(0x102)},
  621. //@const EVT_SOCKET number 事件类型-socket事件
  622. { "EVT_SOCKET", ROREG_INT(1)}, // socket事件
  623. { NULL, ROREG_INT(0) }
  624. };
  625. LUAMOD_API int luaopen_netdrv( lua_State *L ) {
  626. luat_newlib2(L, reg_netdrv);
  627. return 1;
  628. }