luat_lib_netdrv.c 20 KB

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