2727
2828// https://github.com/nodemcu/nodemcu-firmware/wiki/[DRAFT]-How-to-write-a-C-module#debug-and-error-messages
2929
30+ #define NET_INFO_DEBUG_ON
31+
3032#define log_prefix " ### DEBUG: net_info ### : "
3133
3234#if defined(DEVELOP_VERSION )
4446#endif
4547
4648
47-
4849#define NET_INFO_PRIORITY_OUTPUT TASK_PRIORITY_MEDIUM
4950#define NET_INFO_PRIORITY_ERROR TASK_PRIORITY_MEDIUM
5051
51-
5252// these statics should go away for reentrancy and maybe other reasons!
53- static lua_State * gL = NULL ;
5453static int ping_callback_ref ;
5554static int ping_host_count ;
5655static ip_addr_t ping_host_ip ;
5756
58-
59- // static task numbers for task API should be reentrant - I hope....
60- // do they contain a counter, right???
61- static task_handle_t taskno_generic ;
62- static task_handle_t taskno_error ;
63-
64- // task handling callbacks
65-
66- static void net_if_generic (task_param_t param , uint8_t prio ) {
67- (void ) param ;
68- (void ) prio ;
69-
70- NET_INFO_DEBUG (" entering net_if_generic \n" );
71- lua_State * L = lua_getstate ();
72- NET_INFO_DEBUG (" ... in generic got lua state: 0x%x\n" , L );
73- // do what has to be done
74-
75- }
76-
77- static void net_if_error (task_param_t param , uint8_t prio ) {
78- // (void) param;
79- // (void) prio;
80-
81- NET_INFO_DEBUG (" entering net_if_error \n" );
82- // get lua state (I'm supposed to be able in the task, right?)
83- lua_State * L = lua_getstate ();
84- NET_INFO_DEBUG (" ... in error got lua state: 0x%x\n" , L );
85-
86- // cast param to char*
87- char * errmsg = (char * ) param ;
88-
89- // char errmsg[] = "lets try it with a dummy message";
90-
91-
92- NET_INFO_DEBUG (" ... in error got message: %s - pointers were 0x%x from 0x%x \n" , errmsg , errmsg , param );
93-
94- // raise lua error with message
95- lua_pushstring (L , errmsg );
96- NET_INFO_DEBUG (" ... pushed my msg to the stack...\n" );
97-
98- lua_error (L );
99- NET_INFO_DEBUG (" ... and called a lua error (will I see this?)\n" );
100-
101- }
102-
103- // register task handlers
104- static int register_task_handlers (lua_State * L ) {
105- NET_INFO_DEBUG ("start registering task handlers\n" );
106-
107- taskno_generic = task_get_id (net_if_generic );
108- NET_INFO_DEBUG (" ...registered generic: 0x%x\n" , taskno_generic );
109-
110- taskno_error = task_get_id (net_if_error );
111- NET_INFO_DEBUG (" ...registered error 0x%x\n" , taskno_error );
112- return 0 ;
113- }
114-
115- // task handler test dummies
116- static int net_info_err_task_dummy (lua_State * L ) {
117- NET_INFO_DEBUG ("entering task error dummy\n" );
118- NET_INFO_DEBUG (" ... in dummy got lua state: 0x%x\n" , L );
119- char msg [] = "I am the stupid hello task dummy" ;
120-
121- NET_INFO_DEBUG (" and my message is %s\n" , msg );
122- task_param_t param = (task_param_t ) msg ;
123- NET_INFO_DEBUG (" have casted my param from 0x%x to 0x%x \n" , msg , param );
124-
125- task_post ( NET_INFO_PRIORITY_ERROR , taskno_error , param );
126- NET_INFO_DEBUG (" ... have postet my msg task now... \n" );
127- }
128-
129-
130- static int net_info_panic_dummy (lua_State * L ) {
131- char errmsg [] = "Does lua_error really produce a panic?" ;
132- NET_INFO_DEBUG (" ... this is my panicking message: %s \n" , errmsg );
133-
134- // raise lua error with message
135- lua_pushstring (L , errmsg );
136- NET_INFO_DEBUG (" ... pushed my msg to the stack...\n" );
137- lua_error (L );
138- NET_INFO_DEBUG (" ... and called a lua error (will I see this?)\n" );
139- }
140-
141-
142- // ================================ start of old ping stuff ===================================
143-
14457void ping_received (void * arg , void * data ) {
14558 // this would require change of the interface
14659 // struct ping_msg *pingmsg = (struct ping_msg*)arg;
@@ -157,25 +70,24 @@ void ping_received(void *arg, void *data) {
15770 ipaddr_ntoa_r (& source_ip , ipaddrstr , sizeof (ipaddrstr ));
15871
15972 if (ping_callback_ref != LUA_NOREF ) {
160- lua_rawgeti (gL , LUA_REGISTRYINDEX , ping_callback_ref );
161- lua_pushinteger (gL , pingresp -> bytes );
162- lua_pushstring (gL , ipaddrstr );
163- lua_pushinteger (gL , pingresp -> seqno );
164- lua_pushinteger (gL , pingresp -> ttl );
165- lua_pushinteger (gL , pingresp -> resp_time );
166- lua_call (gL , 5 , 0 );
73+ lua_State * L = lua_getstate ();
74+ lua_rawgeti (L , LUA_REGISTRYINDEX , ping_callback_ref );
75+ lua_pushinteger (L , pingresp -> bytes );
76+ lua_pushstring (L , ipaddrstr );
77+ lua_pushinteger (L , pingresp -> seqno );
78+ lua_pushinteger (L , pingresp -> ttl );
79+ lua_pushinteger (L , pingresp -> resp_time );
80+ lua_call (L , 5 , 0 );
16781 } else {
16882 c_printf ("%d bytes from %s, icmp_seq=%d ttl=%d time=%dms\n" ,
169- pingresp -> bytes ,
170- ipaddrstr ,
171- pingresp -> seqno ,
172- pingresp -> ttl ,
173- pingresp -> resp_time );
83+ pingresp -> bytes ,
84+ ipaddrstr ,
85+ pingresp -> seqno ,
86+ pingresp -> ttl ,
87+ pingresp -> resp_time );
17488 }
17589}
17690
177-
178-
17991static void ping_by_hostname (const char * name , ip_addr_t * ipaddr , void * arg ) {
18092 struct ping_option * ping_opt = (struct ping_option * )c_zalloc (sizeof (struct ping_option ));
18193
@@ -185,7 +97,7 @@ static void ping_by_hostname(const char *name, ip_addr_t *ipaddr, void *arg) {
18597 }
18698 if (ipaddr -> addr == IPADDR_NONE ) {
18799 c_printf ("problem resolving hostname - maybe nonexistent host?\n" );
188- return ;
100+ return ;
189101 }
190102
191103 ping_opt -> count = ping_host_count ;
@@ -200,19 +112,19 @@ static void ping_by_hostname(const char *name, ip_addr_t *ipaddr, void *arg) {
200112/**
201113 * test.ping()
202114 * Description:
203- * Send ICMP ping request to address, optionally call callback when response received
115+ * Send ICMP ping request to address, optionally call callback when response received
204116 *
205117 * Syntax:
206- * wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
118+ * wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
207119 * test.ping(address) -- send 4 ping requests to target address
208120 * test.ping(address, n) -- send n ping requests to target address
209121 * test.ping(address, n, callback) -- send n ping requests to target address
210122 * Parameters:
211- * address: string
212- * n: number of requests to send
213- * callback:
123+ * address: string
124+ * n: number of requests to send
125+ * callback:
214126 * Returns:
215- * Nothing.
127+ * Nothing.
216128 *
217129 * Example:
218130 * test.ping("192.168.0.1") -- send 4 pings to 192.168.0.1
@@ -228,14 +140,14 @@ static int net_info_ping(lua_State *L)
228140
229141 // retrieve address arg (mandatory)
230142 if (lua_isstring (L , 1 )) {
231- ping_target = luaL_checkstring (L , 1 );
143+ ping_target = luaL_checkstring (L , 1 );
232144 } else {
233- return luaL_error (L , "no address specified" );
145+ return luaL_error (L , "no address specified" );
234146 }
235147
236148 // retrieve count arg (optional)
237149 if (lua_isnumber (L , 2 )) {
238- count = luaL_checkinteger (L , 2 );
150+ count = luaL_checkinteger (L , 2 );
239151 }
240152
241153 // retrieve callback arg (optional)
@@ -246,47 +158,35 @@ static int net_info_ping(lua_State *L)
246158 if (lua_type (L , 3 ) == LUA_TFUNCTION || lua_type (L , 3 ) == LUA_TLIGHTFUNCTION )
247159 ping_callback_ref = luaL_ref (L , LUA_REGISTRYINDEX );
248160
249- gL = L ; // global L
250-
251-
252161 // attempt to parse ping target as IP
253162 uint32 ip = ipaddr_addr (ping_target );
254163
255164 if (ip != IPADDR_NONE ) {
256- struct ping_option * ping_opt = (struct ping_option * )c_zalloc (sizeof (struct ping_option ));
257-
258- ping_opt -> count = count ;
259- ping_opt -> ip = ip ;
260- ping_opt -> coarse_time = 0 ;
261- ping_opt -> recv_function = & ping_received ;
262-
263- ping_start (ping_opt );
264-
165+ struct ping_option * ping_opt = (struct ping_option * )c_zalloc (sizeof (struct ping_option ));
166+
167+ ping_opt -> count = count ;
168+ ping_opt -> ip = ip ;
169+ ping_opt -> coarse_time = 0 ;
170+ ping_opt -> recv_function = & ping_received ;
171+
172+ ping_start (ping_opt );
173+
265174 } else {
266- ping_host_count = count ;
175+ ping_host_count = count ;
267176
268- struct espconn * ping_dns_lookup ;
269- espconn_create (ping_dns_lookup );
270- espconn_gethostbyname (ping_dns_lookup , ping_target , & ping_host_ip , ping_by_hostname );
177+ struct espconn * ping_dns_lookup ;
178+ espconn_create (ping_dns_lookup );
179+ espconn_gethostbyname (ping_dns_lookup , ping_target , & ping_host_ip , ping_by_hostname );
271180 }
272181
273182 return 0 ;
274183}
275184
276-
277-
278185// Module function map
279186static const LUA_REG_TYPE net_info_map [] = {
280- { LSTRKEY ( "ping" ), LFUNCVAL ( net_info_ping ) },
281- { LSTRKEY ( "dummy" ), LFUNCVAL ( net_info_err_task_dummy ) },
282- { LSTRKEY ( "panic" ), LFUNCVAL (net_info_panic_dummy ) },
283-
284- { LSTRKEY ( "__metatable" ), LROVAL ( net_info_map ) },
187+ { LSTRKEY ( "ping" ), LFUNCVAL ( net_info_ping ) },
285188 { LNILKEY , LNILVAL }
286189};
287190
288-
289191// Register the module - NODEMCU_MODULE()
290- // NODEMCU_MODULE(NET_INFO, "net_info", net_info_map, NULL);
291- NODEMCU_MODULE (NET_INFO , "net_info" , net_info_map , register_task_handlers );
292-
192+ NODEMCU_MODULE (NET_INFO , "net_info" , net_info_map , NULL );
0 commit comments