luat_lib_socket.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. @module socket
  3. @summary 网络接口
  4. @version 1.0
  5. @date 2022.11.13
  6. @demo socket
  7. @tag LUAT_USE_NETWORK
  8. */
  9. #include "luat_base.h"
  10. #include "luat_malloc.h"
  11. // #ifdef LUAT_USE_NETWORK
  12. #include "luat_network_adapter.h"
  13. #include "luat_rtos.h"
  14. #include "luat_zbuff.h"
  15. #define LUAT_LOG_TAG "socket"
  16. #include "luat_log.h"
  17. //static const char NW_TYPE[] = "NWA*";
  18. #define LUAT_NW_CTRL_TYPE "NWCTRL*"
  19. typedef struct
  20. {
  21. network_ctrl_t *netc;
  22. int cb_ref; //回调函数
  23. char *task_name;
  24. uint8_t adapter_index;
  25. }luat_socket_ctrl_t;
  26. network_adapter_info* network_adapter_fetch(int id, void** userdata);
  27. /*
  28. 获取本地ip,当前仅支持IPV4的地址
  29. @api socket.localIP(adapter)
  30. @int 适配器序号, 只能是socket.ETH0(外置以太网),socket.LWIP_ETH(内置以太网),socket.LWIP_STA(内置WIFI的STA),socket.LWIP_AP(内置WIFI的AP),socket.LWIP_GP(内置蜂窝网络的GPRS),socket.USB(外置USB网卡),如果不填,优先选择soc平台自带能上外网的适配器,若仍然没有,选择最后一个注册的适配器
  31. @return string 通常是内网ip, 也可能是外网ip, 取决于运营商的分配
  32. @return string 网络掩码
  33. @return string 网关IP
  34. @usage
  35. sys.taskInit(function()
  36. while 1 do
  37. sys.wait(3000)
  38. log.info("socket", "ip", socket.localIP())
  39. -- 输出示例
  40. -- 62.39.244.10 255.255.255.255 0.0.0.0
  41. end
  42. end)
  43. */
  44. static int l_socket_local_ip(lua_State *L)
  45. {
  46. luat_ip_addr_t local_ip, net_mask, gate_way, ipv6;
  47. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  48. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  49. {
  50. return 0;
  51. }
  52. #ifdef LUAT_USE_LWIP
  53. ipv6.type = 0xff;
  54. int ret = network_get_full_local_ip_info(NULL, adapter_index, &local_ip, &net_mask, &gate_way, &ipv6);
  55. #else
  56. void* userdata = NULL;
  57. network_adapter_info* info = network_adapter_fetch(adapter_index, &userdata);
  58. if (info == NULL)
  59. return 0;
  60. int ret = info->get_local_ip_info(&local_ip, &net_mask, &gate_way, userdata);
  61. #endif
  62. if (ret == 0) {
  63. #ifdef LUAT_USE_LWIP
  64. lua_pushfstring(L, "%s", ipaddr_ntoa(&local_ip));
  65. lua_pushfstring(L, "%s", ipaddr_ntoa(&net_mask));
  66. lua_pushfstring(L, "%s", ipaddr_ntoa(&gate_way));
  67. if (IPADDR_TYPE_V6 == ipv6.type)
  68. {
  69. char *ipv6_string = ip6addr_ntoa(&ipv6.u_addr.ip6);
  70. lua_pushfstring(L, "%s", ipv6_string);
  71. }
  72. else
  73. {
  74. lua_pushnil(L);
  75. }
  76. return 4;
  77. #else
  78. lua_pushfstring(L, "%d.%d.%d.%d", (local_ip.ipv4 >> 24) & 0xFF, (local_ip.ipv4 >> 16) & 0xFF, (local_ip.ipv4 >> 8) & 0xFF, (local_ip.ipv4 >> 0) & 0xFF);
  79. lua_pushfstring(L, "%d.%d.%d.%d", (net_mask.ipv4 >> 24) & 0xFF, (net_mask.ipv4 >> 16) & 0xFF, (net_mask.ipv4 >> 8) & 0xFF, (net_mask.ipv4 >> 0) & 0xFF);
  80. lua_pushfstring(L, "%d.%d.%d.%d", (gate_way.ipv4 >> 24) & 0xFF, (gate_way.ipv4 >> 16) & 0xFF, (gate_way.ipv4 >> 8) & 0xFF, (gate_way.ipv4 >> 0) & 0xFF);
  81. return 3;
  82. #endif
  83. }
  84. return 0;
  85. }
  86. #ifdef LUAT_USE_LWIP
  87. static int32_t l_socket_callback(lua_State *L, void* ptr)
  88. {
  89. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  90. luat_socket_ctrl_t *l_ctrl =(luat_socket_ctrl_t *)msg->ptr;
  91. if (l_ctrl->netc)
  92. {
  93. if (l_ctrl->cb_ref)
  94. {
  95. lua_geti(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  96. if (lua_isfunction(L, -1)) {
  97. lua_pushlightuserdata(L, l_ctrl);
  98. lua_pushinteger(L, msg->arg1);
  99. lua_pushinteger(L, msg->arg2);
  100. lua_call(L, 3, 0);
  101. }
  102. }
  103. else if (l_ctrl->task_name)
  104. {
  105. lua_getglobal(L, "sys_send");
  106. if (lua_isfunction(L, -1)) {
  107. lua_pushstring(L, l_ctrl->task_name);
  108. lua_pushinteger(L, msg->arg1);
  109. lua_pushinteger(L, msg->arg2);
  110. lua_call(L, 3, 0);
  111. }
  112. }
  113. else
  114. {
  115. lua_getglobal(L, "sys_pub");
  116. if (lua_isfunction(L, -1)) {
  117. lua_pushstring(L, LUAT_NW_CTRL_TYPE);
  118. lua_pushinteger(L, l_ctrl->netc->adapter_index);
  119. lua_pushinteger(L, l_ctrl->netc->socket_id);
  120. lua_pushinteger(L, msg->arg1);
  121. lua_pushinteger(L, msg->arg2);
  122. lua_call(L, 5, 0);
  123. }
  124. }
  125. }
  126. lua_pushinteger(L, 0);
  127. return 1;
  128. }
  129. static int32_t luat_lib_socket_callback(void *data, void *param)
  130. {
  131. OS_EVENT *event = (OS_EVENT *)data;
  132. rtos_msg_t msg;
  133. msg.handler = l_socket_callback;
  134. msg.ptr = param;
  135. msg.arg1 = event->ID & 0x0fffffff;
  136. msg.arg2 = event->Param1;
  137. luat_msgbus_put(&msg, 0);
  138. return 0;
  139. }
  140. static luat_socket_ctrl_t * l_get_ctrl(lua_State *L, int index)
  141. {
  142. if (luaL_testudata(L, 1, LUAT_NW_CTRL_TYPE))
  143. {
  144. return ((luat_socket_ctrl_t *)luaL_checkudata(L, 1, LUAT_NW_CTRL_TYPE));
  145. }
  146. else
  147. {
  148. return ((luat_socket_ctrl_t *)lua_touserdata(L, 1));
  149. }
  150. }
  151. // __gc
  152. static int l_socket_gc(lua_State *L)
  153. {
  154. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  155. if (l_ctrl->netc)
  156. {
  157. network_force_close_socket(l_ctrl->netc);
  158. network_release_ctrl(l_ctrl->netc);
  159. l_ctrl->netc = NULL;
  160. }
  161. if (l_ctrl->cb_ref)
  162. {
  163. luaL_unref(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  164. l_ctrl->cb_ref = 0;
  165. }
  166. if (l_ctrl->task_name)
  167. {
  168. luat_heap_free(l_ctrl->task_name);
  169. l_ctrl->task_name = 0;
  170. }
  171. return 0;
  172. }
  173. /*
  174. 在某个适配的网卡上申请一个socket_ctrl
  175. @api socket.create(adapter, cb)
  176. @int 适配器序号, 只能是socket.ETH0(外置以太网),socket.LWIP_ETH(内置以太网),socket.LWIP_STA(内置WIFI的STA),socket.LWIP_AP(内置WIFI的AP),socket.LWIP_GP(内置蜂窝网络的GPRS),socket.USB(外置USB网卡),如果不填,优先选择soc平台自带能上外网的适配器,若仍然没有,选择最后一个注册的适配器
  177. @string or function string为消息通知的taskName,function则为回调函数,如果固件没有内置sys_wait,则必须是function
  178. 当通过回调函数回调消息时,输入给function一共3个参数:
  179. param1为申请的network_ctrl
  180. param2为具体的消息,只能是socket.RESET, socket.LINK, socket.ON_LINE, socket.TX_OK, socket.RX_NEW, socket.CLOSE等等
  181. param3为消息对应的参数
  182. @return userdata 成功返回network_ctrl,失败返回nil
  183. @usage local netc = socket.create(socket.ETH0, socket_cb_fun) --以太网网卡上申请一个network_ctrl,通过socket_cb_fun回调相关消息
  184. local netc = socket.create(socket.ETH0, "IOT_TASK") --以太网网卡上申请一个network_ctrl,通过sendMsg方式通知taskName为"IOT_TASK"回调相关消息
  185. */
  186. static int l_socket_create(lua_State *L)
  187. {
  188. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  189. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  190. {
  191. lua_pushnil(L);
  192. return 1;
  193. }
  194. luat_socket_ctrl_t *l_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  195. if (!l_ctrl)
  196. {
  197. lua_pushnil(L);
  198. return 1;
  199. }
  200. l_ctrl->adapter_index = adapter_index;
  201. l_ctrl->netc = network_alloc_ctrl(adapter_index);
  202. if (!l_ctrl->netc)
  203. {
  204. LLOGD("create fail");
  205. lua_pushnil(L);
  206. return 1;
  207. }
  208. network_init_ctrl(l_ctrl->netc, NULL, luat_lib_socket_callback, l_ctrl);
  209. if (lua_isfunction(L, 2))
  210. {
  211. lua_pushvalue(L, 2);
  212. l_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  213. l_ctrl->task_name = NULL;
  214. }
  215. else if (lua_isstring(L, 2))
  216. {
  217. l_ctrl->cb_ref = 0;
  218. size_t len;
  219. const char *buf;
  220. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  221. l_ctrl->task_name = luat_heap_malloc(len + 1);
  222. memset(l_ctrl->task_name, 0, len + 1);
  223. memcpy(l_ctrl->task_name, buf, len);
  224. }
  225. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  226. return 1;
  227. }
  228. /*
  229. 配置是否打开debug信息
  230. @api socket.debug(ctrl, onoff)
  231. @user_data socket.create得到的ctrl
  232. @boolean true 打开debug开关
  233. @return nil 无返回值
  234. @usage socket.debug(ctrl, true)
  235. */
  236. static int l_socket_set_debug(lua_State *L)
  237. {
  238. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  239. if (lua_isboolean(L, 2))
  240. {
  241. l_ctrl->netc->is_debug = lua_toboolean(L, 2);
  242. }
  243. return 0;
  244. }
  245. /*
  246. 配置network一些信息,
  247. @api socket.config(ctrl, local_port, is_udp, is_tls, keep_idle, keep_interval, keep_cnt, server_cert, client_cert, client_key, client_password)
  248. @user_data socket.create得到的ctrl
  249. @int 本地端口号,小端格式,如果不写,则自动分配一个,如果用户填了端口号则需要小于60000, 默认不写
  250. @boolean 是否是UDP,默认false
  251. @boolean 是否是加密传输,默认false
  252. @int tcp keep live模式下的idle时间(秒),如果留空则表示不启用,如果是不支持标准posix接口的网卡(比如W5500),则为心跳间隔
  253. @int tcp keep live模式下的探测间隔时间(秒)
  254. @int tcp keep live模式下的探测次数
  255. @string TCP模式下的服务器ca证书数据,UDP模式下的PSK,不需要加密传输写nil,后续参数也全部nil
  256. @string TCP模式下的客户端ca证书数据,UDP模式下的PSK-ID,TCP模式下如果不需要验证客户端证书时,忽略,一般不需要验证客户端证书
  257. @string TCP模式下的客户端私钥加密数据
  258. @string TCP模式下的客户端私钥口令数据
  259. @return nil 无返回值
  260. @usage socket.config(ctrl) --最普通的TCP传输
  261. socket.config(ctrl, nil, nil ,true) --最普通的加密TCP传输,证书都不用验证的那种
  262. */
  263. static int l_socket_config(lua_State *L)
  264. {
  265. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  266. uint8_t is_udp = 0;
  267. uint8_t is_tls = 0;
  268. int param_pos = 1;
  269. uint32_t keep_idle, keep_interval, keep_cnt;
  270. const char *server_cert = NULL;
  271. const char *client_cert = NULL;
  272. const char *client_key = NULL;
  273. const char *client_password = NULL;
  274. size_t server_cert_len, client_cert_len, client_key_len, client_password_len;
  275. uint16_t local_port = luaL_optinteger(L, ++param_pos, 0);
  276. if (lua_isboolean(L, ++param_pos))
  277. {
  278. is_udp = lua_toboolean(L, param_pos);
  279. }
  280. if (lua_isboolean(L, ++param_pos))
  281. {
  282. is_tls = lua_toboolean(L, param_pos);
  283. }
  284. keep_idle = luaL_optinteger(L, ++param_pos, 0);
  285. keep_interval = luaL_optinteger(L, ++param_pos, 0);
  286. keep_cnt = luaL_optinteger(L, ++param_pos, 0);
  287. if (lua_isstring(L, ++param_pos))
  288. {
  289. server_cert_len = 0;
  290. server_cert = luaL_checklstring(L, param_pos, &server_cert_len);
  291. }
  292. if (lua_isstring(L, ++param_pos))
  293. {
  294. client_cert_len = 0;
  295. client_cert = luaL_checklstring(L, param_pos, &client_cert_len);
  296. }
  297. if (lua_isstring(L, ++param_pos))
  298. {
  299. client_key_len = 0;
  300. client_key = luaL_checklstring(L, param_pos, &client_key_len);
  301. }
  302. if (lua_isstring(L, ++param_pos))
  303. {
  304. client_password_len = 0;
  305. client_password = luaL_checklstring(L, param_pos, &client_password_len);
  306. }
  307. network_set_base_mode(l_ctrl->netc, !is_udp, 10000, keep_idle, keep_idle, keep_interval, keep_cnt);
  308. network_set_local_port(l_ctrl->netc, local_port);
  309. if (is_tls)
  310. {
  311. network_init_tls(l_ctrl->netc, (server_cert || client_cert)?2:0);
  312. if (is_udp)
  313. {
  314. network_set_psk_info(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len, (const unsigned char *)client_key, client_key_len);
  315. }
  316. else
  317. {
  318. if (server_cert)
  319. {
  320. network_set_server_cert(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len + 1);
  321. }
  322. if (client_cert)
  323. {
  324. network_set_client_cert(l_ctrl->netc, (const unsigned char *)client_cert, client_cert_len + 1,
  325. (const unsigned char *)client_key, client_key_len + 1,
  326. (const unsigned char *)client_password, client_password_len + 1);
  327. }
  328. }
  329. }
  330. else
  331. {
  332. network_deinit_tls(l_ctrl->netc);
  333. }
  334. return 0;
  335. }
  336. /*
  337. 等待网卡linkup
  338. @api socket.linkup(ctrl)
  339. @user_data socket.create得到的ctrl
  340. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了
  341. @return boolean true已经linkup,false没有linkup,之后需要接收socket.LINK消息
  342. @usage local succ, result = socket.linkup(ctrl)
  343. */
  344. static int l_socket_linkup(lua_State *L)
  345. {
  346. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  347. int result = network_wait_link_up(l_ctrl->netc, 0);
  348. lua_pushboolean(L, (result < 0)?0:1);
  349. lua_pushboolean(L, result == 0);
  350. return 2;
  351. }
  352. /*
  353. 作为客户端连接服务器
  354. @api socket.connect(ctrl, ip, remote_port, need_ipv6_dns)
  355. @user_data socket.create得到的ctrl
  356. @string or int ip或者域名,如果是IPV4,可以是大端格式的int值
  357. @int 服务器端口号,小端格式
  358. @boolean 域名解析是否要IPV6,true要,false不要,默认false不要,只有支持IPV6的协议栈才有效果
  359. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果有异常,后续要close
  360. @return boolean true已经connect,false没有connect,之后需要接收socket.ON_LINE消息
  361. @usage local succ, result = socket.connect(ctrl, "xxx.xxx.xxx.xxx", xxxx)
  362. */
  363. static int l_socket_connect(lua_State *L)
  364. {
  365. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  366. luat_ip_addr_t ip_addr;
  367. const char *ip = NULL;
  368. size_t ip_len;
  369. ip_addr.type = 0xff;
  370. if (lua_isinteger(L, 2))
  371. {
  372. ip_addr.type = IPADDR_TYPE_V4;
  373. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 2);
  374. ip = NULL;
  375. ip_len = 0;
  376. }
  377. else
  378. {
  379. ip_len = 0;
  380. ip = luaL_checklstring(L, 2, &ip_len);
  381. }
  382. uint16_t remote_port = luaL_checkinteger(L, 3);
  383. LLOGD("connect to %s,%d", ip, remote_port);
  384. if (ip_addr.type != IPADDR_TYPE_V4)
  385. {
  386. if (LUA_TBOOLEAN == lua_type(L, 4))
  387. {
  388. network_connect_ipv6_domain(l_ctrl->netc, lua_toboolean(L, 4));
  389. }
  390. else
  391. {
  392. network_connect_ipv6_domain(l_ctrl->netc, 0);
  393. }
  394. }
  395. int result = network_connect(l_ctrl->netc, ip, ip_len, (ip_addr.type != IPADDR_TYPE_V4)?NULL:&ip_addr, remote_port, 0);
  396. lua_pushboolean(L, (result < 0)?0:1);
  397. lua_pushboolean(L, result == 0);
  398. return 2;
  399. }
  400. /*
  401. 作为客户端断开连接
  402. @api socket.discon(ctrl)
  403. @user_data socket.create得到的ctrl
  404. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了
  405. @return boolean true已经断开,false没有断开,之后需要接收socket.CLOSED消息
  406. @usage local succ, result = socket.discon(ctrl)
  407. */
  408. static int l_socket_disconnect(lua_State *L)
  409. {
  410. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  411. int result = network_close(l_ctrl->netc, 0);
  412. lua_pushboolean(L, (result < 0)?0:1);
  413. lua_pushboolean(L, result == 0);
  414. return 2;
  415. }
  416. /*
  417. 强制关闭socket
  418. @api socket.close(ctrl)
  419. @user_data socket.create得到的ctrl
  420. */
  421. static int l_socket_close(lua_State *L)
  422. {
  423. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  424. network_force_close_socket(l_ctrl->netc);
  425. return 0;
  426. }
  427. /*
  428. 发送数据给对端,UDP单次发送不要超过1460字节,否则很容易失败
  429. @api socket.tx(ctrl, data, ip, port, flag)
  430. @user_data socket.create得到的ctrl
  431. @string or user_data zbuff 要发送的数据
  432. @string or int 对端IP,如果是TCP应用则忽略,如果是UDP,如果留空则用connect时候的参数,如果是IPV4,可以是大端格式的int值
  433. @int 对端端口号,小端格式,如果是TCP应用则忽略,如果是UDP,如果留空则用connect时候的参数
  434. @int 发送参数,目前预留,不起作用
  435. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果false,后续要close
  436. @return boolean true缓冲区满了,false没有满,如果true,则需要等待一段时间或者等到socket.TX_OK消息后再尝试发送,同时忽略下一个返回值
  437. @return boolean true已经收到应答,false没有收到应答,之后需要接收socket.TX_OK消息, 也可以忽略继续发送,直到full==true
  438. @usage local succ, full, result = socket.tx(ctrl, "123456", "xxx.xxx.xxx.xxx", xxxx)
  439. */
  440. static int l_socket_tx(lua_State *L)
  441. {
  442. char ip_buf[68] = {0};
  443. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  444. luat_ip_addr_t ip_addr = {0};
  445. luat_zbuff_t *buff = NULL;
  446. const char *ip = NULL;
  447. const char *data = NULL;
  448. size_t ip_len = 0, data_len = 0;
  449. ip_addr.type = 0xff;
  450. if (lua_isstring(L, 2))
  451. {
  452. data_len = 0;
  453. data = luaL_checklstring(L, 2, &data_len);
  454. }
  455. else
  456. {
  457. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  458. data = (const char*)buff->addr;
  459. data_len = buff->used;
  460. }
  461. if (lua_isinteger(L, 3))
  462. {
  463. ip_addr.type = 0;
  464. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
  465. }
  466. else if (lua_isstring(L, 3))
  467. {
  468. ip_len = 0;
  469. ip = luaL_checklstring(L, 3, &ip_len);
  470. memcpy(ip_buf, ip, ip_len);
  471. ip_buf[ip_len] = 0;
  472. ipaddr_aton(ip_buf, &ip_addr);
  473. }
  474. uint32_t tx_len;
  475. int result = network_tx(l_ctrl->netc, (const uint8_t *)data, data_len, luaL_optinteger(L, 5, 0), (ip_addr.type != 0xff)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
  476. lua_pushboolean(L, (result < 0)?0:1);
  477. lua_pushboolean(L, tx_len != data_len);
  478. lua_pushboolean(L, result == 0);
  479. return 3;
  480. }
  481. /*
  482. 接收对端发出的数据,注意数据已经缓存在底层,使用本函数只是提取出来,UDP模式下一次只会取出一个数据包
  483. @api socket.rx(ctrl, buff, flag)
  484. @user_data socket.create得到的ctrl
  485. @user_data zbuff 存放接收的数据,如果缓冲区不够大会自动扩容
  486. @int 接收参数,目前预留,不起作用
  487. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果false,后续要close
  488. @return int 本次接收到数据长度
  489. @return string 对端IP,只有UDP模式下才有意义,TCP模式返回nil,注意返回的格式,如果是IPV4,1byte 0x00 + 4byte地址 如果是IPV6,1byte 0x01 + 16byte地址
  490. @return int 对端port,只有UDP模式下才有意义,TCP模式返回0
  491. @usage local succ, data_len, ip, port = socket.rx(ctrl, buff)
  492. */
  493. static int l_socket_rx(lua_State *L)
  494. {
  495. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  496. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  497. luat_ip_addr_t ip_addr;
  498. uint8_t ip[17];
  499. uint16_t port;
  500. uint8_t new_flag = 0;
  501. int rx_len;
  502. int total_len;
  503. int result = network_rx(l_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  504. if (result < 0)
  505. {
  506. lua_pushboolean(L, 0);
  507. lua_pushinteger(L, 0);
  508. lua_pushnil(L);
  509. lua_pushnil(L);
  510. }
  511. else if (!total_len)
  512. {
  513. lua_pushboolean(L, 1);
  514. lua_pushinteger(L, 0);
  515. lua_pushnil(L);
  516. lua_pushnil(L);
  517. }
  518. else
  519. {
  520. if ((buff->len - buff->used) < total_len)
  521. {
  522. __zbuff_resize(buff, total_len + buff->used);
  523. }
  524. result = network_rx(l_ctrl->netc, buff->addr + buff->used, total_len, 0, &ip_addr, &port, &rx_len);
  525. if (result < 0)
  526. {
  527. lua_pushboolean(L, 0);
  528. lua_pushinteger(L, 0);
  529. lua_pushnil(L);
  530. lua_pushnil(L);
  531. }
  532. else if (!rx_len)
  533. {
  534. lua_pushboolean(L, 1);
  535. lua_pushinteger(L, 0);
  536. lua_pushnil(L);
  537. lua_pushnil(L);
  538. }
  539. else
  540. {
  541. buff->used += rx_len;
  542. lua_pushboolean(L, 1);
  543. lua_pushinteger(L, rx_len);
  544. if (l_ctrl->netc->is_tcp)
  545. {
  546. lua_pushnil(L);
  547. lua_pushnil(L);
  548. }
  549. else
  550. {
  551. if (IPADDR_TYPE_V4 == ip_addr.type)
  552. {
  553. ip[0] = 0;
  554. memcpy(ip + 1, &ip_addr.u_addr.ip4.addr, 4);
  555. lua_pushlstring(L, (const char*)ip, 5);
  556. }
  557. else
  558. {
  559. ip[0] = 1;
  560. memcpy(ip + 1, ip_addr.u_addr.ip6.addr, 16);
  561. lua_pushlstring(L, (const char*)ip, 17);
  562. }
  563. lua_pushinteger(L, port);
  564. }
  565. }
  566. }
  567. return 4;
  568. }
  569. /*
  570. 等待新的socket消息,在连接成功和发送数据成功后,使用一次将network状态转换到接收新数据
  571. @api socket.wait(ctrl)
  572. @user_data socket.create得到的ctrl
  573. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果false,后续要close
  574. @return boolean true有新的数据需要接收,false没有数据,之后需要接收socket.EVENT消息
  575. @usage local succ, result = socket.wait(ctrl)
  576. */
  577. static int l_socket_wait(lua_State *L)
  578. {
  579. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  580. int result = network_wait_event(l_ctrl->netc, NULL, 0, NULL);
  581. lua_pushboolean(L, (result < 0)?0:1);
  582. lua_pushboolean(L, result == 0);
  583. return 2;
  584. }
  585. /*
  586. 作为服务端开始监听
  587. @api socket.listen(ctrl)
  588. @user_data socket.create得到的ctrl
  589. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果false,后续要close
  590. @return boolean true已经connect,false没有connect,之后需要接收socket.ON_LINE消息
  591. @usage local succ, result = socket.listen(ctrl)
  592. */
  593. static int l_socket_listen(lua_State *L)
  594. {
  595. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  596. int result = network_listen(l_ctrl->netc, 0);
  597. lua_pushboolean(L, (result < 0)?0:1);
  598. lua_pushboolean(L, result == 0);
  599. return 2;
  600. }
  601. /*
  602. 作为服务端接收到一个新的客户端,注意,如果是类似W5500的硬件协议栈不支持1对多,则不需要第二个参数
  603. @api socket.accept(ctrl)
  604. @user_data socket.create得到的ctrl,这里是服务器端
  605. @string or function or nil string为消息通知的taskName,function则为回调函数,和socket.create参数一致
  606. @return boolean true没有异常发生,false失败了,如果false则不需要看下一个返回值了,如果false,后续要close
  607. @return user_data or nil 如果支持1对多,则会返回新的ctrl,自动create,如果不支持则返回nil
  608. @usage local succ, new_netc = socket.listen(ctrl, cb)
  609. */
  610. static int l_socket_accept(lua_State *L)
  611. {
  612. luat_socket_ctrl_t *old_ctrl = l_get_ctrl(L, 1);
  613. if (network_accept_enable(old_ctrl->netc))
  614. {
  615. luat_socket_ctrl_t *new_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  616. if (!new_ctrl)
  617. {
  618. lua_pushboolean(L, 0);
  619. lua_pushnil(L);
  620. return 2;
  621. }
  622. new_ctrl->adapter_index = old_ctrl->adapter_index;
  623. new_ctrl->netc = network_alloc_ctrl(old_ctrl->adapter_index);
  624. if (!new_ctrl->netc)
  625. {
  626. LLOGD("create fail");
  627. lua_pushboolean(L, 0);
  628. lua_pushnil(L);
  629. return 2;
  630. }
  631. network_init_ctrl(new_ctrl->netc, NULL, luat_lib_socket_callback, new_ctrl);
  632. if (lua_isfunction(L, 2))
  633. {
  634. lua_pushvalue(L, 2);
  635. new_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  636. new_ctrl->task_name = NULL;
  637. }
  638. else if (lua_isstring(L, 2))
  639. {
  640. new_ctrl->cb_ref = 0;
  641. size_t len;
  642. const char *buf;
  643. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  644. new_ctrl->task_name = luat_heap_malloc(len + 1);
  645. memset(new_ctrl->task_name, 0, len + 1);
  646. memcpy(new_ctrl->task_name, buf, len);
  647. }
  648. if (network_socket_accept(old_ctrl, new_ctrl))
  649. {
  650. lua_pushboolean(L, 0);
  651. lua_pushnil(L);
  652. return 2;
  653. }
  654. else
  655. {
  656. lua_pushboolean(L, 1);
  657. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  658. return 2;
  659. }
  660. }
  661. else
  662. {
  663. lua_pushboolean(L, !network_socket_accept(old_ctrl->netc, NULL));
  664. lua_pushnil(L);
  665. return 2;
  666. }
  667. }
  668. /*
  669. 主动释放掉network_ctrl
  670. @api socket.release(ctrl)
  671. @user_data socket.create得到的ctrl
  672. @usage socket.release(ctrl)
  673. */
  674. static int l_socket_release(lua_State *L)
  675. {
  676. return l_socket_gc(L);
  677. }
  678. /*
  679. 设置DNS服务器
  680. @api socket.setDNS(adapter_index, dns_index, ip)
  681. @int 适配器序号, 只能是socket.ETH0,socket.STA,socket.AP,如果不填,会选择最后一个注册的适配器
  682. @int dns服务器序号,从1开始
  683. @string or int dns,如果是IPV4,可以是大端格式的int值
  684. @return boolean 成功返回true,失败返回false
  685. @usage socket.setDNS(socket.ETH0, 1, "114.114.114.114")
  686. */
  687. static int l_socket_set_dns(lua_State *L)
  688. {
  689. char ip_buf[68];
  690. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  691. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  692. {
  693. lua_pushboolean(L, 0);
  694. return 1;
  695. }
  696. int dns_index = luaL_optinteger(L, 2, 1);
  697. luat_ip_addr_t ip_addr;
  698. const char *ip;
  699. size_t ip_len;
  700. ip_addr.type = 0xff;
  701. if (lua_isinteger(L, 3))
  702. {
  703. ip_addr.type = 0;
  704. ip_addr.u_addr.ip4.addr = lua_tointeger(L, 3);
  705. ip = NULL;
  706. ip_len = 0;
  707. }
  708. else
  709. {
  710. ip_len = 0;
  711. ip = luaL_checklstring(L, 3, &ip_len);
  712. memcpy(ip_buf, ip, ip_len);
  713. ip_buf[ip_len] = 0;
  714. ipaddr_aton(ip_buf, &ip_addr);
  715. }
  716. network_set_dns_server(adapter_index, dns_index - 1, &ip_addr);
  717. lua_pushboolean(L, 1);
  718. return 1;
  719. }
  720. /*
  721. 设置SSL的log
  722. @api socket.sslLog(log_level)
  723. @int mbedtls log等级,0不打印,1只打印错误和警告,2大部分info,3及3以上详细的debug信息,过多的信息可能会造成内存碎片化
  724. @usage socket.sslLog(2)
  725. */
  726. static int l_socket_set_ssl_log(lua_State *L)
  727. {
  728. #if defined(MBEDTLS_DEBUG_C)
  729. mbedtls_debug_set_threshold(luaL_optinteger(L, 1, 1));
  730. #endif
  731. return 0;
  732. }
  733. #else
  734. static int32_t l_socket_callback(lua_State *L, void* ptr)
  735. {
  736. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  737. luat_socket_ctrl_t *l_ctrl =(luat_socket_ctrl_t *)msg->ptr;
  738. if (l_ctrl->netc)
  739. {
  740. if (l_ctrl->cb_ref)
  741. {
  742. lua_geti(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  743. if (lua_isfunction(L, -1)) {
  744. lua_pushlightuserdata(L, l_ctrl);
  745. lua_pushinteger(L, msg->arg1);
  746. lua_pushinteger(L, msg->arg2);
  747. lua_call(L, 3, 0);
  748. }
  749. }
  750. else if (l_ctrl->task_name)
  751. {
  752. lua_getglobal(L, "sys_send");
  753. if (lua_isfunction(L, -1)) {
  754. lua_pushstring(L, l_ctrl->task_name);
  755. lua_pushinteger(L, msg->arg1);
  756. lua_pushinteger(L, msg->arg2);
  757. lua_call(L, 3, 0);
  758. }
  759. }
  760. else
  761. {
  762. lua_getglobal(L, "sys_pub");
  763. if (lua_isfunction(L, -1)) {
  764. lua_pushstring(L, LUAT_NW_CTRL_TYPE);
  765. lua_pushinteger(L, l_ctrl->netc->adapter_index);
  766. lua_pushinteger(L, l_ctrl->netc->socket_id);
  767. lua_pushinteger(L, msg->arg1);
  768. lua_pushinteger(L, msg->arg2);
  769. lua_call(L, 5, 0);
  770. }
  771. }
  772. }
  773. lua_pushinteger(L, 0);
  774. return 1;
  775. }
  776. static int32_t luat_lib_socket_callback(void *data, void *param)
  777. {
  778. OS_EVENT *event = (OS_EVENT *)data;
  779. rtos_msg_t msg;
  780. msg.handler = l_socket_callback;
  781. msg.ptr = param;
  782. msg.arg1 = event->ID & 0x0fffffff;
  783. msg.arg2 = event->Param1;
  784. luat_msgbus_put(&msg, 0);
  785. return 0;
  786. }
  787. static luat_socket_ctrl_t * l_get_ctrl(lua_State *L, int index)
  788. {
  789. if (luaL_testudata(L, 1, LUAT_NW_CTRL_TYPE))
  790. {
  791. return ((luat_socket_ctrl_t *)luaL_checkudata(L, 1, LUAT_NW_CTRL_TYPE));
  792. }
  793. else
  794. {
  795. return ((luat_socket_ctrl_t *)lua_touserdata(L, 1));
  796. }
  797. }
  798. // __gc
  799. static int l_socket_gc(lua_State *L)
  800. {
  801. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  802. if (l_ctrl->netc)
  803. {
  804. network_force_close_socket(l_ctrl->netc);
  805. network_release_ctrl(l_ctrl->netc);
  806. l_ctrl->netc = NULL;
  807. }
  808. if (l_ctrl->cb_ref)
  809. {
  810. luaL_unref(L, LUA_REGISTRYINDEX, l_ctrl->cb_ref);
  811. l_ctrl->cb_ref = 0;
  812. }
  813. if (l_ctrl->task_name)
  814. {
  815. luat_heap_free(l_ctrl->task_name);
  816. l_ctrl->task_name = 0;
  817. }
  818. return 0;
  819. }
  820. static int l_socket_create(lua_State *L)
  821. {
  822. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  823. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  824. {
  825. lua_pushnil(L);
  826. return 1;
  827. }
  828. luat_socket_ctrl_t *l_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  829. if (!l_ctrl)
  830. {
  831. lua_pushnil(L);
  832. return 1;
  833. }
  834. l_ctrl->adapter_index = adapter_index;
  835. l_ctrl->netc = network_alloc_ctrl(adapter_index);
  836. if (!l_ctrl->netc)
  837. {
  838. LLOGD("create fail");
  839. lua_pushnil(L);
  840. return 1;
  841. }
  842. network_init_ctrl(l_ctrl->netc, NULL, luat_lib_socket_callback, l_ctrl);
  843. if (lua_isfunction(L, 2))
  844. {
  845. lua_pushvalue(L, 2);
  846. l_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  847. l_ctrl->task_name = NULL;
  848. }
  849. else if (lua_isstring(L, 2))
  850. {
  851. l_ctrl->cb_ref = 0;
  852. size_t len;
  853. const char *buf;
  854. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  855. l_ctrl->task_name = luat_heap_malloc(len + 1);
  856. memset(l_ctrl->task_name, 0, len + 1);
  857. memcpy(l_ctrl->task_name, buf, len);
  858. }
  859. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  860. return 1;
  861. }
  862. static int l_socket_set_debug(lua_State *L)
  863. {
  864. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  865. if (lua_isboolean(L, 2))
  866. {
  867. l_ctrl->netc->is_debug = lua_toboolean(L, 2);
  868. }
  869. return 0;
  870. }
  871. static int l_socket_config(lua_State *L)
  872. {
  873. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  874. uint8_t is_udp = 0;
  875. uint8_t is_tls = 0;
  876. int param_pos = 1;
  877. uint32_t keep_idle, keep_interval, keep_cnt;
  878. const char *server_cert = NULL;
  879. const char *client_cert = NULL;
  880. const char *client_key = NULL;
  881. const char *client_password = NULL;
  882. size_t server_cert_len, client_cert_len, client_key_len, client_password_len;
  883. uint16_t local_port = luaL_optinteger(L, ++param_pos, 0);
  884. if (lua_isboolean(L, ++param_pos))
  885. {
  886. is_udp = lua_toboolean(L, param_pos);
  887. }
  888. if (lua_isboolean(L, ++param_pos))
  889. {
  890. is_tls = lua_toboolean(L, param_pos);
  891. }
  892. keep_idle = luaL_optinteger(L, ++param_pos, 0);
  893. keep_interval = luaL_optinteger(L, ++param_pos, 0);
  894. keep_cnt = luaL_optinteger(L, ++param_pos, 0);
  895. if (lua_isstring(L, ++param_pos))
  896. {
  897. server_cert_len = 0;
  898. server_cert = luaL_checklstring(L, param_pos, &server_cert_len);
  899. }
  900. if (lua_isstring(L, ++param_pos))
  901. {
  902. client_cert_len = 0;
  903. client_cert = luaL_checklstring(L, param_pos, &client_cert_len);
  904. }
  905. if (lua_isstring(L, ++param_pos))
  906. {
  907. client_key_len = 0;
  908. client_key = luaL_checklstring(L, param_pos, &client_key_len);
  909. }
  910. if (lua_isstring(L, ++param_pos))
  911. {
  912. client_password_len = 0;
  913. client_password = luaL_checklstring(L, param_pos, &client_password_len);
  914. }
  915. network_set_base_mode(l_ctrl->netc, !is_udp, 10000, keep_idle, keep_idle, keep_interval, keep_cnt);
  916. network_set_local_port(l_ctrl->netc, local_port);
  917. if (is_tls)
  918. {
  919. network_init_tls(l_ctrl->netc, (server_cert || client_cert)?2:0);
  920. if (is_udp)
  921. {
  922. network_set_psk_info(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len, (const unsigned char *)client_key, client_key_len);
  923. }
  924. else
  925. {
  926. if (server_cert)
  927. {
  928. network_set_server_cert(l_ctrl->netc, (const unsigned char *)server_cert, server_cert_len);
  929. }
  930. if (client_cert)
  931. {
  932. network_set_client_cert(l_ctrl->netc, (const unsigned char *)client_cert, client_cert_len,
  933. (const unsigned char *)client_key, client_key_len,
  934. (const unsigned char *)client_password, client_password_len);
  935. }
  936. }
  937. }
  938. else
  939. {
  940. network_deinit_tls(l_ctrl->netc);
  941. }
  942. return 0;
  943. }
  944. static int l_socket_linkup(lua_State *L)
  945. {
  946. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  947. int result = network_wait_link_up(l_ctrl->netc, 0);
  948. lua_pushboolean(L, (result < 0)?0:1);
  949. lua_pushboolean(L, result == 0);
  950. return 2;
  951. }
  952. static int l_socket_connect(lua_State *L)
  953. {
  954. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  955. luat_ip_addr_t ip_addr;
  956. const char *ip = NULL;
  957. size_t ip_len;
  958. ip_addr.is_ipv6 = 0xff;
  959. if (lua_isinteger(L, 2))
  960. {
  961. ip_addr.is_ipv6 = 0;
  962. ip_addr.ipv4 = lua_tointeger(L, 2);
  963. ip = NULL;
  964. ip_len = 0;
  965. }
  966. else
  967. {
  968. ip_len = 0;
  969. ip = luaL_checklstring(L, 2, &ip_len);
  970. }
  971. uint16_t remote_port = luaL_checkinteger(L, 3);
  972. LLOGD("connect to %s,%d", ip, remote_port);
  973. int result = network_connect(l_ctrl->netc, ip, ip_len, ip_addr.is_ipv6?NULL:&ip_addr, remote_port, 0);
  974. lua_pushboolean(L, (result < 0)?0:1);
  975. lua_pushboolean(L, result == 0);
  976. return 2;
  977. }
  978. static int l_socket_disconnect(lua_State *L)
  979. {
  980. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  981. int result = network_close(l_ctrl->netc, 0);
  982. lua_pushboolean(L, (result < 0)?0:1);
  983. lua_pushboolean(L, result == 0);
  984. return 2;
  985. }
  986. static int l_socket_close(lua_State *L)
  987. {
  988. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  989. network_force_close_socket(l_ctrl->netc);
  990. return 0;
  991. }
  992. static int l_socket_tx(lua_State *L)
  993. {
  994. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  995. luat_ip_addr_t ip_addr = {0};
  996. luat_zbuff_t *buff = NULL;
  997. const char *ip = NULL;
  998. const char *data = NULL;
  999. size_t ip_len = 0, data_len = 0;
  1000. ip_addr.is_ipv6 = 0xff;
  1001. if (lua_isstring(L, 2))
  1002. {
  1003. data_len = 0;
  1004. data = luaL_checklstring(L, 2, &data_len);
  1005. }
  1006. else
  1007. {
  1008. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  1009. data = (const char *)buff->addr;
  1010. data_len = buff->used;
  1011. }
  1012. if (lua_isinteger(L, 3))
  1013. {
  1014. ip_addr.is_ipv6 = 0;
  1015. ip_addr.ipv4 = lua_tointeger(L, 3);
  1016. }
  1017. else if (lua_isstring(L, 3))
  1018. {
  1019. ip_len = 0;
  1020. ip = luaL_checklstring(L, 3, &ip_len);
  1021. if (network_string_is_ipv4(ip, ip_len))
  1022. {
  1023. ip_addr.is_ipv6 = 0;
  1024. ip_addr.ipv4 = network_string_to_ipv4(ip, ip_len);
  1025. }
  1026. else
  1027. {
  1028. char *name = luat_heap_malloc(ip_len + 1);
  1029. memcpy(name, ip, ip_len);
  1030. name[ip_len] = 0;
  1031. network_string_to_ipv6(name, &ip_addr);
  1032. free(name);
  1033. }
  1034. }
  1035. uint32_t tx_len;
  1036. int result = network_tx(l_ctrl->netc, (const uint8_t *)data, data_len, luaL_optinteger(L, 5, 0), (ip_addr.is_ipv6 != 0xff)?&ip_addr:NULL, luaL_optinteger(L, 4, 0), &tx_len, 0);
  1037. lua_pushboolean(L, (result < 0)?0:1);
  1038. lua_pushboolean(L, tx_len != data_len);
  1039. lua_pushboolean(L, result == 0);
  1040. return 3;
  1041. }
  1042. static int l_socket_rx(lua_State *L)
  1043. {
  1044. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1045. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  1046. luat_ip_addr_t ip_addr;
  1047. uint8_t ip[17];
  1048. uint16_t port;
  1049. uint8_t new_flag = 0;
  1050. int rx_len;
  1051. int total_len;
  1052. int result = network_rx(l_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  1053. if (result < 0)
  1054. {
  1055. lua_pushboolean(L, 0);
  1056. lua_pushinteger(L, 0);
  1057. lua_pushnil(L);
  1058. lua_pushnil(L);
  1059. }
  1060. else if (!total_len)
  1061. {
  1062. lua_pushboolean(L, 1);
  1063. lua_pushinteger(L, 0);
  1064. lua_pushnil(L);
  1065. lua_pushnil(L);
  1066. }
  1067. else
  1068. {
  1069. if ((buff->len - buff->used) < total_len)
  1070. {
  1071. __zbuff_resize(buff, total_len + buff->used);
  1072. }
  1073. result = network_rx(l_ctrl->netc, buff->addr + buff->used, total_len, 0, &ip_addr, &port, &rx_len);
  1074. if (result < 0)
  1075. {
  1076. lua_pushboolean(L, 0);
  1077. lua_pushinteger(L, 0);
  1078. lua_pushnil(L);
  1079. lua_pushnil(L);
  1080. }
  1081. else if (!rx_len)
  1082. {
  1083. lua_pushboolean(L, 1);
  1084. lua_pushinteger(L, 0);
  1085. lua_pushnil(L);
  1086. lua_pushnil(L);
  1087. }
  1088. else
  1089. {
  1090. buff->used += rx_len;
  1091. lua_pushboolean(L, 1);
  1092. lua_pushinteger(L, rx_len);
  1093. if (l_ctrl->netc->is_tcp)
  1094. {
  1095. lua_pushnil(L);
  1096. lua_pushnil(L);
  1097. }
  1098. else
  1099. {
  1100. if (!ip_addr.is_ipv6)
  1101. {
  1102. ip[0] = 0;
  1103. memcpy(ip + 1, &ip_addr.ipv4, 4);
  1104. lua_pushlstring(L, (const char*)ip, 5);
  1105. }
  1106. else
  1107. {
  1108. ip[0] = 1;
  1109. memcpy(ip + 1, &ip_addr.ipv6_u8_addr, 16);
  1110. lua_pushlstring(L, (const char*)ip, 17);
  1111. }
  1112. lua_pushinteger(L, port);
  1113. }
  1114. }
  1115. }
  1116. return 4;
  1117. }
  1118. static int l_socket_wait(lua_State *L)
  1119. {
  1120. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1121. int result = network_wait_event(l_ctrl->netc, NULL, 0, NULL);
  1122. lua_pushboolean(L, (result < 0)?0:1);
  1123. lua_pushboolean(L, result == 0);
  1124. return 2;
  1125. }
  1126. static int l_socket_listen(lua_State *L)
  1127. {
  1128. luat_socket_ctrl_t *l_ctrl = l_get_ctrl(L, 1);
  1129. int result = network_listen(l_ctrl->netc, 0);
  1130. lua_pushboolean(L, (result < 0)?0:1);
  1131. lua_pushboolean(L, result == 0);
  1132. return 2;
  1133. }
  1134. static int l_socket_accept(lua_State *L)
  1135. {
  1136. luat_socket_ctrl_t *old_ctrl = l_get_ctrl(L, 1);
  1137. if (network_accept_enable(old_ctrl->netc))
  1138. {
  1139. luat_socket_ctrl_t *new_ctrl = (luat_socket_ctrl_t *)lua_newuserdata(L, sizeof(luat_socket_ctrl_t));
  1140. if (!new_ctrl)
  1141. {
  1142. lua_pushboolean(L, 0);
  1143. lua_pushnil(L);
  1144. return 2;
  1145. }
  1146. new_ctrl->adapter_index = old_ctrl->adapter_index;
  1147. new_ctrl->netc = network_alloc_ctrl(old_ctrl->adapter_index);
  1148. if (!new_ctrl->netc)
  1149. {
  1150. LLOGD("create fail");
  1151. lua_pushboolean(L, 0);
  1152. lua_pushnil(L);
  1153. return 2;
  1154. }
  1155. network_init_ctrl(new_ctrl->netc, NULL, luat_lib_socket_callback, new_ctrl);
  1156. if (lua_isfunction(L, 2))
  1157. {
  1158. lua_pushvalue(L, 2);
  1159. new_ctrl->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1160. new_ctrl->task_name = NULL;
  1161. }
  1162. else if (lua_isstring(L, 2))
  1163. {
  1164. new_ctrl->cb_ref = 0;
  1165. size_t len;
  1166. const char *buf;
  1167. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  1168. new_ctrl->task_name = luat_heap_malloc(len + 1);
  1169. memset(new_ctrl->task_name, 0, len + 1);
  1170. memcpy(new_ctrl->task_name, buf, len);
  1171. }
  1172. if (network_socket_accept(old_ctrl, new_ctrl))
  1173. {
  1174. lua_pushboolean(L, 0);
  1175. lua_pushnil(L);
  1176. return 2;
  1177. }
  1178. else
  1179. {
  1180. lua_pushboolean(L, 1);
  1181. luaL_setmetatable(L, LUAT_NW_CTRL_TYPE);
  1182. return 2;
  1183. }
  1184. }
  1185. else
  1186. {
  1187. lua_pushboolean(L, !network_socket_accept(old_ctrl->netc, NULL));
  1188. lua_pushnil(L);
  1189. return 2;
  1190. }
  1191. }
  1192. static int l_socket_release(lua_State *L)
  1193. {
  1194. return l_socket_gc(L);
  1195. }
  1196. static int l_socket_set_dns(lua_State *L)
  1197. {
  1198. int adapter_index = luaL_optinteger(L, 1, network_get_last_register_adapter());
  1199. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY)
  1200. {
  1201. lua_pushboolean(L, 0);
  1202. return 1;
  1203. }
  1204. int dns_index = luaL_optinteger(L, 2, 1);
  1205. luat_ip_addr_t ip_addr;
  1206. const char *ip;
  1207. size_t ip_len;
  1208. ip_addr.is_ipv6 = 0xff;
  1209. if (lua_isinteger(L, 3))
  1210. {
  1211. ip_addr.is_ipv6 = 0;
  1212. ip_addr.ipv4 = lua_tointeger(L, 3);
  1213. ip = NULL;
  1214. ip_len = 0;
  1215. }
  1216. else
  1217. {
  1218. ip_len = 0;
  1219. ip = luaL_checklstring(L, 3, &ip_len);
  1220. ip_addr.is_ipv6 = !network_string_is_ipv4(ip, ip_len);
  1221. if (ip_addr.is_ipv6)
  1222. {
  1223. char *temp = luat_heap_malloc(ip_len + 1);
  1224. memcpy(temp, ip, ip_len);
  1225. temp[ip_len] = 0;
  1226. network_string_to_ipv6(temp, &ip_addr);
  1227. luat_heap_free(temp);
  1228. }
  1229. else
  1230. {
  1231. ip_addr.ipv4 = network_string_to_ipv4(ip, ip_len);
  1232. }
  1233. }
  1234. network_set_dns_server(adapter_index, dns_index - 1, &ip_addr);
  1235. lua_pushboolean(L, 1);
  1236. return 1;
  1237. }
  1238. static int l_socket_set_ssl_log(lua_State *L)
  1239. {
  1240. #if defined(MBEDTLS_DEBUG_C)
  1241. mbedtls_debug_set_threshold(luaL_optinteger(L, 1, 1));
  1242. #endif
  1243. return 0;
  1244. }
  1245. #endif
  1246. #ifdef LUAT_USE_SNTP
  1247. #include "luat_sntp.h"
  1248. #endif
  1249. /*
  1250. 查看网卡适配器的联网状态
  1251. @api socket.adapter(index)
  1252. @int 需要查看的适配器序号,可以留空会查看全部网卡,直到遇到IP READY的,如果指定网卡,只能是socket.ETH0(外置以太网),socket.LWIP_ETH(内置以太网),socket.LWIP_STA(内置WIFI的STA),socket.LWIP_AP(内置WIFI的AP),socket.LWIP_GP(内置蜂窝网络的GPRS),socket.USB(外置USB网卡)
  1253. @return boolean 被查看的适配器是否IP READY,true表示已经准备好可以联网了,false暂时不可以联网
  1254. @return int 最后一个被查看的适配器序号
  1255. @usage
  1256. -- 查看全部网卡,直到找到一个是IP READY的
  1257. local isReady,index = socket.adapter() --如果isReady为true,则index为IP READY的网卡适配器序号
  1258. --查看外置以太网(比如W5500)是否IP READY
  1259. local isReady,default = socket.adapter(socket.ETH0)
  1260. */
  1261. static int l_socket_adapter(lua_State *L)
  1262. {
  1263. int adapter_index = luaL_optinteger(L, 1, -1);
  1264. if (adapter_index > NW_ADAPTER_INDEX_LWIP_NONE && adapter_index < NW_ADAPTER_QTY)
  1265. {
  1266. lua_pushboolean(L, network_check_ready(NULL, adapter_index));
  1267. lua_pushinteger(L, adapter_index);
  1268. }
  1269. else
  1270. {
  1271. for(int i = NW_ADAPTER_INDEX_LWIP_GPRS; i < NW_ADAPTER_QTY; i++)
  1272. {
  1273. if (network_check_ready(NULL, i))
  1274. {
  1275. lua_pushboolean(L, 1);
  1276. lua_pushinteger(L, i);
  1277. return 2;
  1278. }
  1279. }
  1280. lua_pushboolean(L, 0);
  1281. lua_pushinteger(L, NW_ADAPTER_QTY - 1);
  1282. }
  1283. return 2;
  1284. }
  1285. #include "rotable2.h"
  1286. static const rotable_Reg_t reg_socket_adapter[] =
  1287. {
  1288. {"create", ROREG_FUNC(l_socket_create)},
  1289. {"debug", ROREG_FUNC(l_socket_set_debug)},
  1290. {"config", ROREG_FUNC(l_socket_config)},
  1291. {"linkup", ROREG_FUNC(l_socket_linkup)},
  1292. {"connect", ROREG_FUNC(l_socket_connect)},
  1293. {"listen", ROREG_FUNC(l_socket_listen)},
  1294. {"accept", ROREG_FUNC(l_socket_accept)},
  1295. {"discon", ROREG_FUNC(l_socket_disconnect)},
  1296. {"close", ROREG_FUNC(l_socket_close)},
  1297. {"tx", ROREG_FUNC(l_socket_tx)},
  1298. {"rx", ROREG_FUNC(l_socket_rx)},
  1299. {"wait", ROREG_FUNC(l_socket_wait)},
  1300. //{"listen", ROREG_FUNC(l_socket_listen)},
  1301. //{"accept", ROREG_FUNC(l_socket_accept)},
  1302. {"release", ROREG_FUNC(l_socket_release)},
  1303. { "setDNS", ROREG_FUNC(l_socket_set_dns)},
  1304. { "sslLog", ROREG_FUNC(l_socket_set_ssl_log)},
  1305. {"localIP", ROREG_FUNC(l_socket_local_ip)},
  1306. {"adapter", ROREG_FUNC(l_socket_adapter)},
  1307. #ifdef LUAT_USE_SNTP
  1308. {"sntp", ROREG_FUNC(l_sntp_get)},
  1309. #endif
  1310. //@const ETH0 number 带硬件协议栈的ETH0,值为5
  1311. { "ETH0", ROREG_INT(NW_ADAPTER_INDEX_ETH0)},
  1312. //@const LWIP_ETH number 使用LWIP协议栈的以太网卡,值为4
  1313. { "LWIP_ETH", ROREG_INT(NW_ADAPTER_INDEX_LWIP_ETH)},
  1314. //@const LWIP_STA number 使用LWIP协议栈的WIFI STA,值为2
  1315. { "LWIP_STA", ROREG_INT(NW_ADAPTER_INDEX_LWIP_WIFI_STA)},
  1316. //@const LWIP_AP number 使用LWIP协议栈的WIFI AP,值为3
  1317. { "LWIP_AP", ROREG_INT(NW_ADAPTER_INDEX_LWIP_WIFI_AP)},
  1318. //@const LWIP_GP number 使用LWIP协议栈的移动蜂窝模块,值为1
  1319. { "LWIP_GP", ROREG_INT(NW_ADAPTER_INDEX_LWIP_GPRS)},
  1320. //@const USB number 使用LWIP协议栈的USB网卡,值为6
  1321. { "USB", ROREG_INT(NW_ADAPTER_INDEX_USB)},
  1322. //@const LINK number LINK事件
  1323. { "LINK", ROREG_INT(EV_NW_RESULT_LINK & 0x0fffffff)},
  1324. //@const ON_LINE number ON_LINE事件
  1325. { "ON_LINE", ROREG_INT(EV_NW_RESULT_CONNECT & 0x0fffffff)},
  1326. //@const EVENT number EVENT事件
  1327. { "EVENT", ROREG_INT(EV_NW_RESULT_EVENT & 0x0fffffff)},
  1328. //@const TX_OK number TX_OK事件
  1329. { "TX_OK", ROREG_INT(EV_NW_RESULT_TX & 0x0fffffff)},
  1330. //@const CLOSED number CLOSED事件
  1331. { "CLOSED", ROREG_INT(EV_NW_RESULT_CLOSE & 0x0fffffff)},
  1332. { NULL, ROREG_INT(0)}
  1333. };
  1334. LUAMOD_API int luaopen_socket_adapter( lua_State *L ) {
  1335. luat_newlib2(L, reg_socket_adapter);
  1336. luaL_newmetatable(L, LUAT_NW_CTRL_TYPE); /* create metatable for file handles */
  1337. lua_pushcfunction(L, l_socket_gc);
  1338. lua_setfield(L, -2, "__gc");
  1339. lua_pop(L, 1); /* pop new metatable */
  1340. return 1;
  1341. }
  1342. // #endif