27
27
28
28
// https://github.com/nodemcu/nodemcu-firmware/wiki/[DRAFT]-How-to-write-a-C-module#debug-and-error-messages
29
29
30
+ #define NET_INFO_DEBUG_ON
31
+
30
32
#define log_prefix " ### DEBUG: net_info ### : "
31
33
32
34
#if defined(DEVELOP_VERSION )
44
46
#endif
45
47
46
48
47
-
48
49
#define NET_INFO_PRIORITY_OUTPUT TASK_PRIORITY_MEDIUM
49
50
#define NET_INFO_PRIORITY_ERROR TASK_PRIORITY_MEDIUM
50
51
51
-
52
52
// these statics should go away for reentrancy and maybe other reasons!
53
- static lua_State * gL = NULL ;
54
53
static int ping_callback_ref ;
55
54
static int ping_host_count ;
56
55
static ip_addr_t ping_host_ip ;
57
56
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
-
144
57
void ping_received (void * arg , void * data ) {
145
58
// this would require change of the interface
146
59
// struct ping_msg *pingmsg = (struct ping_msg*)arg;
@@ -157,25 +70,24 @@ void ping_received(void *arg, void *data) {
157
70
ipaddr_ntoa_r (& source_ip , ipaddrstr , sizeof (ipaddrstr ));
158
71
159
72
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 );
167
81
} else {
168
82
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 );
174
88
}
175
89
}
176
90
177
-
178
-
179
91
static void ping_by_hostname (const char * name , ip_addr_t * ipaddr , void * arg ) {
180
92
struct ping_option * ping_opt = (struct ping_option * )c_zalloc (sizeof (struct ping_option ));
181
93
@@ -185,7 +97,7 @@ static void ping_by_hostname(const char *name, ip_addr_t *ipaddr, void *arg) {
185
97
}
186
98
if (ipaddr -> addr == IPADDR_NONE ) {
187
99
c_printf ("problem resolving hostname - maybe nonexistent host?\n" );
188
- return ;
100
+ return ;
189
101
}
190
102
191
103
ping_opt -> count = ping_host_count ;
@@ -200,19 +112,19 @@ static void ping_by_hostname(const char *name, ip_addr_t *ipaddr, void *arg) {
200
112
/**
201
113
* test.ping()
202
114
* 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
204
116
*
205
117
* 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
207
119
* test.ping(address) -- send 4 ping requests to target address
208
120
* test.ping(address, n) -- send n ping requests to target address
209
121
* test.ping(address, n, callback) -- send n ping requests to target address
210
122
* 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:
214
126
* Returns:
215
- * Nothing.
127
+ * Nothing.
216
128
*
217
129
* Example:
218
130
* 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)
228
140
229
141
// retrieve address arg (mandatory)
230
142
if (lua_isstring (L , 1 )) {
231
- ping_target = luaL_checkstring (L , 1 );
143
+ ping_target = luaL_checkstring (L , 1 );
232
144
} else {
233
- return luaL_error (L , "no address specified" );
145
+ return luaL_error (L , "no address specified" );
234
146
}
235
147
236
148
// retrieve count arg (optional)
237
149
if (lua_isnumber (L , 2 )) {
238
- count = luaL_checkinteger (L , 2 );
150
+ count = luaL_checkinteger (L , 2 );
239
151
}
240
152
241
153
// retrieve callback arg (optional)
@@ -246,47 +158,35 @@ static int net_info_ping(lua_State *L)
246
158
if (lua_type (L , 3 ) == LUA_TFUNCTION || lua_type (L , 3 ) == LUA_TLIGHTFUNCTION )
247
159
ping_callback_ref = luaL_ref (L , LUA_REGISTRYINDEX );
248
160
249
- gL = L ; // global L
250
-
251
-
252
161
// attempt to parse ping target as IP
253
162
uint32 ip = ipaddr_addr (ping_target );
254
163
255
164
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
+
265
174
} else {
266
- ping_host_count = count ;
175
+ ping_host_count = count ;
267
176
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 );
271
180
}
272
181
273
182
return 0 ;
274
183
}
275
184
276
-
277
-
278
185
// Module function map
279
186
static 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 ) },
285
188
{ LNILKEY , LNILVAL }
286
189
};
287
190
288
-
289
191
// 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