20
20
_a > _b ? _a : _b; })
21
21
22
22
char ssid [32 ];
23
+ char password [] = "testing123" ;
24
+ int wifi_secure_mode = 0 ;
25
+
23
26
static const int pin = 2 ;
24
27
25
28
// Structure holding the TCP connection information.
@@ -218,11 +221,50 @@ void ICACHE_FLASH_ATTR inter_recv_cb(void *arg, char *pusrdata, unsigned short l
218
221
}
219
222
}
220
223
224
+ void ICACHE_FLASH_ATTR wifi_configure (int secure ) {
225
+ wifi_secure_mode = secure ;
226
+
227
+ // start wifi AP
228
+ wifi_set_opmode (SOFTAP_MODE );
229
+ struct softap_config config = {0 };
230
+ wifi_softap_get_config (& config );
231
+ strcpy (config .ssid , ssid );
232
+ if (wifi_secure_mode == 0 ) strcat (config .ssid , "-pair" );
233
+ strcpy (config .password , password );
234
+ config .ssid_len = strlen (config .ssid );
235
+ config .authmode = wifi_secure_mode ? AUTH_WPA2_PSK : AUTH_OPEN ;
236
+ config .beacon_interval = 100 ;
237
+ config .max_connection = 4 ;
238
+ wifi_softap_set_config (& config );
239
+
240
+ if (wifi_secure_mode ) {
241
+ // setup tcp server
242
+ tcp_proto .local_port = 1337 ;
243
+ tcp_conn .type = ESPCONN_TCP ;
244
+ tcp_conn .state = ESPCONN_NONE ;
245
+ tcp_conn .proto .tcp = & tcp_proto ;
246
+ espconn_regist_connectcb (& tcp_conn , tcp_connect_cb );
247
+ espconn_accept (& tcp_conn );
248
+ espconn_regist_time (& tcp_conn , 60 , 0 ); // 60s timeout for all connections
249
+
250
+ // setup inter server
251
+ inter_proto .local_port = 1338 ;
252
+ const char udp_remote_ip [4 ] = {255 , 255 , 255 , 255 };
253
+ os_memcpy (inter_proto .remote_ip , udp_remote_ip , 4 );
254
+ inter_proto .remote_port = 1338 ;
255
+
256
+ inter_conn .type = ESPCONN_UDP ;
257
+ inter_conn .proto .udp = & inter_proto ;
258
+
259
+ espconn_regist_recvcb (& inter_conn , inter_recv_cb );
260
+ espconn_create (& inter_conn );
261
+ }
262
+ }
263
+
221
264
void ICACHE_FLASH_ATTR wifi_init () {
222
265
// default ssid and password
223
266
memset (ssid , 0 , 32 );
224
267
os_sprintf (ssid , "panda-%08x-BROKEN" , system_get_chip_id ());
225
- char password [] = "testing123" ;
226
268
227
269
// fetch secure ssid and password
228
270
// update, try 20 times, for 1 second
@@ -242,19 +284,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
242
284
os_delay_us (50000 );
243
285
}
244
286
245
- // start wifi AP
246
- wifi_set_opmode (SOFTAP_MODE );
247
- struct softap_config config ;
248
- wifi_softap_get_config (& config );
249
- strcpy (config .ssid , ssid );
250
- strcpy (config .password , password );
251
- config .ssid_len = strlen (ssid );
252
- config .authmode = AUTH_WPA2_PSK ;
253
- config .beacon_interval = 100 ;
254
- config .max_connection = 4 ;
255
- wifi_softap_set_config (& config );
256
-
257
- //set IP
287
+ // set IP
258
288
wifi_softap_dhcps_stop (); //stop DHCP before setting static IP
259
289
struct ip_info ip_config ;
260
290
IP4_ADDR (& ip_config .ip , 192 , 168 , 0 , 10 );
@@ -265,27 +295,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
265
295
wifi_softap_set_dhcps_offer_option (OFFER_ROUTER , & stupid_gateway );
266
296
wifi_softap_dhcps_start ();
267
297
268
- // setup tcp server
269
- tcp_proto .local_port = 1337 ;
270
- tcp_conn .type = ESPCONN_TCP ;
271
- tcp_conn .state = ESPCONN_NONE ;
272
- tcp_conn .proto .tcp = & tcp_proto ;
273
- espconn_regist_connectcb (& tcp_conn , tcp_connect_cb );
274
- espconn_accept (& tcp_conn );
275
- espconn_regist_time (& tcp_conn , 60 , 0 ); // 60s timeout for all connections
276
-
277
- // setup inter server
278
- inter_proto .local_port = 1338 ;
279
- const char udp_remote_ip [4 ] = {255 , 255 , 255 , 255 };
280
- os_memcpy (inter_proto .remote_ip , udp_remote_ip , 4 );
281
- inter_proto .remote_port = 1338 ;
282
-
283
- inter_conn .type = ESPCONN_UDP ;
284
- inter_conn .proto .udp = & inter_proto ;
285
-
286
- espconn_regist_recvcb (& inter_conn , inter_recv_cb );
287
-
288
- espconn_create (& inter_conn );
298
+ wifi_configure (0 );
289
299
}
290
300
291
301
#define LOOP_PRIO 2
0 commit comments