|
| 1 | +// *************************************************************************** |
| 2 | +// net_info module for ESP8266 with nodeMCU |
| 3 | +// |
| 4 | +// Written by Lukas Voborsky (@voborsky) with great help by TerryE |
| 5 | +// *************************************************************************** |
| 6 | + |
| 7 | +// #define NODE_DEBUG |
| 8 | +#define LUA_USE_MODULES_NET_INFO |
| 9 | + |
| 10 | +#include "module.h" |
| 11 | +#include "lauxlib.h" |
| 12 | + |
| 13 | +#include "lwip/ip_addr.h" |
| 14 | +#include "espconn.h" |
| 15 | +#include "lwip/dns.h" |
| 16 | +#include "lwip/app/ping.h" |
| 17 | + |
| 18 | +/* |
| 19 | +ping_opt needs to be the first element of the structure. It is a workaround to pass the |
| 20 | +callback reference and self_ref to the ping_received function. Pointer the ping_option |
| 21 | +structure is equal to the pointer to net_info_ping_t structure. |
| 22 | +*/ |
| 23 | +typedef struct { |
| 24 | + struct ping_option ping_opt; |
| 25 | + uint32_t ping_callback_ref; |
| 26 | + } net_info_ping_t; |
| 27 | +typedef net_info_ping_t* ping_t; |
| 28 | + |
| 29 | +/* |
| 30 | + * ping_received_sent(pingresp) |
| 31 | +*/ |
| 32 | +#define LuaCBfunc lua_upvalueindex(1) |
| 33 | +#define nipUD lua_upvalueindex(2) |
| 34 | + |
| 35 | +static int ping_received_sent(lua_State *L) { |
| 36 | + struct ping_resp *resp = (struct ping_resp *) lua_touserdata (L, 1); |
| 37 | + ping_t nip = (ping_t) lua_touserdata (L, nipUD); |
| 38 | + lua_pushvalue(L, LuaCBfunc); |
| 39 | + |
| 40 | + NODE_DBG("[net_info ping_received_sent] nip = %p\n", nip); |
| 41 | + |
| 42 | + if (resp == NULL) { /* resolution failed so call the CB with 0 byte count to flag this */ |
| 43 | + luaL_unref(L, LUA_REGISTRYINDEX, nip->ping_callback_ref); |
| 44 | + lua_pushinteger(L, 0); |
| 45 | + luaL_pcallx(L, 1, 0); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + char ipaddrstr[16]; |
| 49 | + ipaddr_ntoa_r((ip_addr_t *) &nip->ping_opt.ip, ipaddrstr, sizeof(ipaddrstr)); |
| 50 | + |
| 51 | + if (resp->total_count == 0) { /* processing receive response */ |
| 52 | + NODE_DBG("[ping_received] %s: resp_time=%d seqno=%d bytes=%d ping_err=%d\n", |
| 53 | + ipaddrstr, resp->resp_time, resp->seqno, resp->bytes, resp->ping_err); |
| 54 | + lua_pushinteger(L, resp->bytes); |
| 55 | + lua_pushstring(L, ipaddrstr); |
| 56 | + lua_pushinteger(L, resp->seqno); |
| 57 | + lua_pushinteger(L, resp->ping_err == 0 ? resp->resp_time: -1); |
| 58 | + luaL_pcallx(L, 4, 0); |
| 59 | + } else { /* sent completion is currently a No-op */ |
| 60 | + NODE_DBG("[ping_sent] %s: total_count=%d timeout_count=%d " |
| 61 | + "total_bytes=%d total_time=%d\n", |
| 62 | + ipaddrstr, resp->total_count, resp->timeout_count, |
| 63 | + resp->total_bytes, resp->total_time); |
| 64 | + luaL_unref(L, LUA_REGISTRYINDEX, nip->ping_callback_ref); |
| 65 | + } |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +/* |
| 71 | + * Wrapper to call ping_received_sent(pingresp) |
| 72 | + */ |
| 73 | +static void ping_CB(net_info_ping_t *nip, struct ping_resp *pingresp) { |
| 74 | + NODE_DBG("[net_info ping_CB] nip = %p, nip->ping_callback_ref = %p, pingresp= %p\n", nip, nip->ping_callback_ref, pingresp); |
| 75 | + lua_State *L = lua_getstate(); |
| 76 | + lua_rawgeti(L, LUA_REGISTRYINDEX, nip->ping_callback_ref); |
| 77 | + lua_pushlightuserdata(L, pingresp); |
| 78 | + lua_call(L, 1, 0); // call the closure (ping_received_sent) |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * Wrapper to call ping_start using fully resolve IP4 address |
| 83 | + */ |
| 84 | +static void net_info_ping_raw(const char *name, ip_addr_t *ipaddr, ping_t nip) { |
| 85 | + NODE_DBG("[net_info_ping_raw] name = %s, ipaddr= %x\n", name, ipaddr); |
| 86 | + if (ipaddr) { |
| 87 | + char ipaddrstr[16]; |
| 88 | + ipaddr_ntoa_r(ipaddr, ipaddrstr, sizeof(ipaddrstr)); |
| 89 | + NODE_DBG("[net_info_ping_raw] ip: %s\n", ipaddrstr); |
| 90 | + } |
| 91 | + lua_State *L = lua_getstate(); |
| 92 | + |
| 93 | + if (!ipaddr || ipaddr->addr == 0xFFFFFFFF) { |
| 94 | + ping_CB(nip, NULL); /* A NULL pinresp flags DNS resolution failure */ |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + nip->ping_opt.ip = ipaddr->addr; |
| 99 | + NODE_DBG("[net_info_ping_raw] calling ping_start\n"); |
| 100 | + if (!ping_start(&(nip->ping_opt))) { |
| 101 | + luaL_unref(L, LUA_REGISTRYINDEX, nip->ping_callback_ref); |
| 102 | + luaL_error(L, "memory allocation error: cannot start ping"); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Lua: net_info.ping(target, [count], responseCB) |
| 107 | +static int net_info_ping(lua_State *L) |
| 108 | +{ |
| 109 | + ip_addr_t addr; |
| 110 | + |
| 111 | + // retrieve function parameters |
| 112 | + const char *ping_target = luaL_checkstring(L, 1); |
| 113 | + bool isf2 = lua_isfunction(L, 2); |
| 114 | + lua_Integer l_count = isf2 ? 0: luaL_optinteger(L, 2, 0); /* use ping_start() default */ |
| 115 | + lua_settop(L, isf2 ? 2 : 3); |
| 116 | + luaL_argcheck(L, lua_isfunction(L, -1), -1, "no callback specified"); |
| 117 | + |
| 118 | + ping_t nip = (ping_t) memset(lua_newuserdata(L, sizeof(*nip)), 0, sizeof(*nip)); |
| 119 | + |
| 120 | + /* Register C closure with 2 Upvals: (1) Lua CB function; (2) nip Userdata */ |
| 121 | + lua_pushcclosure(L, ping_received_sent, 2); // stack has nip UD, responseCB; [-n, +1, m] |
| 122 | + |
| 123 | + nip->ping_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); // registers the closure to registry [-1, +0, m] |
| 124 | + nip->ping_opt.count = l_count; |
| 125 | + nip->ping_opt.coarse_time = 0; |
| 126 | + nip->ping_opt.recv_function = (ping_recv_function) &ping_CB; |
| 127 | + nip->ping_opt.sent_function = (ping_sent_function) &ping_CB; |
| 128 | + |
| 129 | + NODE_DBG("[net_info_ping] nip = %p, nip->ping_callback_ref = %p\n", nip, nip->ping_callback_ref); |
| 130 | + |
| 131 | + err_t err = dns_gethostbyname(ping_target, &addr, (dns_found_callback) net_info_ping_raw, nip); |
| 132 | + if (err != ERR_OK && err != ERR_INPROGRESS) { |
| 133 | + luaL_unref(L, LUA_REGISTRYINDEX, nip->ping_callback_ref); |
| 134 | + return luaL_error(L, "lwip error %d", err); |
| 135 | + } |
| 136 | + if (err == ERR_OK) { |
| 137 | + NODE_DBG("[net_info_ping] No DNS resolution needed\n"); |
| 138 | + net_info_ping_raw(ping_target, &addr, nip); |
| 139 | + } |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +LROT_BEGIN(net_info, NULL, 0) |
| 144 | + LROT_FUNCENTRY( ping, net_info_ping ) |
| 145 | +LROT_END( net_info, NULL, 0 ) |
| 146 | + |
| 147 | +NODEMCU_MODULE(NET_INFO, "net_info", net_info, NULL); |
0 commit comments