luat_lib_netdrv.c 20 KB

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