luat_lib_netdrv.c 21 KB

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