Skip to content

Commit 38cc0ee

Browse files
committed
add wifi_secure_mode, boots in insecure mode
1 parent 2573d86 commit 38cc0ee

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

boardesp/proxy.c

+45-35
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
_a > _b ? _a : _b; })
2121

2222
char ssid[32];
23+
char password[] = "testing123";
24+
int wifi_secure_mode = 0;
25+
2326
static const int pin = 2;
2427

2528
// Structure holding the TCP connection information.
@@ -218,11 +221,50 @@ void ICACHE_FLASH_ATTR inter_recv_cb(void *arg, char *pusrdata, unsigned short l
218221
}
219222
}
220223

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+
221264
void ICACHE_FLASH_ATTR wifi_init() {
222265
// default ssid and password
223266
memset(ssid, 0, 32);
224267
os_sprintf(ssid, "panda-%08x-BROKEN", system_get_chip_id());
225-
char password[] = "testing123";
226268

227269
// fetch secure ssid and password
228270
// update, try 20 times, for 1 second
@@ -242,19 +284,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
242284
os_delay_us(50000);
243285
}
244286

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
258288
wifi_softap_dhcps_stop(); //stop DHCP before setting static IP
259289
struct ip_info ip_config;
260290
IP4_ADDR(&ip_config.ip, 192, 168, 0, 10);
@@ -265,27 +295,7 @@ void ICACHE_FLASH_ATTR wifi_init() {
265295
wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &stupid_gateway);
266296
wifi_softap_dhcps_start();
267297

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);
289299
}
290300

291301
#define LOOP_PRIO 2

boardesp/webserver.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ char OK_header[] = "HTTP/1.0 200 OK\nContent-Type: text/html\n\n";
4040
static struct espconn web_conn;
4141
static esp_tcp web_proto;
4242
extern char ssid[];
43+
extern int wifi_secure_mode;
4344

4445
char *st_firmware;
4546
int real_content_length, content_length = 0;
@@ -215,8 +216,9 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {
215216

216217
espconn_send_string(&web_conn, resp);
217218
espconn_disconnect(conn);
218-
219-
} else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0) {
219+
} else if (memcmp(data, "GET /secure", 11) == 0 && !wifi_secure_mode) {
220+
wifi_configure(1);
221+
} else if (memcmp(data, "GET /set_property?usb_mode=", 27) == 0 && wifi_secure_mode) {
220222
char mode_value = data[27] - '0';
221223
if (mode_value >= '\x00' && mode_value <= '\x02') {
222224
memset(resp, 0, MAX_RESP);
@@ -228,7 +230,7 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {
228230
espconn_send_string(&web_conn, resp);
229231
espconn_disconnect(conn);
230232
}
231-
} else if (memcmp(data, "PUT /stupdate ", 14) == 0) {
233+
} else if (memcmp(data, "PUT /stupdate ", 14) == 0 && wifi_secure_mode) {
232234
os_printf("init st firmware\n");
233235
char *cl = strstr(data, "Content-Length: ");
234236
if (cl != NULL) {
@@ -244,8 +246,8 @@ static void ICACHE_FLASH_ATTR web_rx_cb(void *arg, char *data, uint16_t len) {
244246
state = RECEIVING_ST_FIRMWARE;
245247
}
246248

247-
} else if ((memcmp(data, "PUT /espupdate1 ", 16) == 0) ||
248-
(memcmp(data, "PUT /espupdate2 ", 16) == 0)) {
249+
} else if (((memcmp(data, "PUT /espupdate1 ", 16) == 0) ||
250+
(memcmp(data, "PUT /espupdate2 ", 16) == 0)) && wifi_secure_mode) {
249251
// 0x1000 = user1.bin
250252
// 0x81000 = user2.bin
251253
// 0x3FE000 = blank.bin

0 commit comments

Comments
 (0)