@@ -92,7 +92,7 @@ static const char http_html_gz_filename[] = "enduser_setup.html.gz";
92
92
static const char http_html_filename [] = "enduser_setup.html" ;
93
93
static const char http_header_200 [] = "HTTP/1.1 200 OK\r\nCache-control:no-cache\r\nConnection:close\r\nContent-Type:text/html; charset=utf-8\r\n" ; /* Note single \r\n here! */
94
94
static const char http_header_204 [] = "HTTP/1.1 204 No Content\r\nContent-Length:0\r\nConnection:close\r\n\r\n" ;
95
- static const char http_header_302 [] = "HTTP/1.1 302 Moved\r\nLocation: /\r\nContent-Length:0\r\nConnection:close\r\n\r\n" ;
95
+ static const char http_header_302 [] = "HTTP/1.1 302 Moved\r\nLocation: http://nodemcu.portal /\r\nContent-Length:0\r\nConnection:close\r\n\r\n" ;
96
96
static const char http_header_302_trying [] = "HTTP/1.1 302 Moved\r\nLocation: /?trying=true\r\nContent-Length:0\r\nConnection:close\r\n\r\n" ;
97
97
static const char http_header_400 [] = "HTTP/1.1 400 Bad request\r\nContent-Length:0\r\nConnection:close\r\n\r\n" ;
98
98
static const char http_header_404 [] = "HTTP/1.1 404 Not found\r\nContent-Length:10\r\nConnection:close\r\n\r\nNot found\n" ;
@@ -135,6 +135,7 @@ typedef struct
135
135
struct tcp_pcb * http_pcb ;
136
136
char * http_payload_data ;
137
137
uint32_t http_payload_len ;
138
+ char * ap_ssid ;
138
139
os_timer_t check_station_timer ;
139
140
os_timer_t shutdown_timer ;
140
141
int lua_connected_cb_ref ;
@@ -931,6 +932,8 @@ static err_t streamout_sent (void *arg, struct tcp_pcb *pcb, u16_t len)
931
932
{
932
933
tcp_sent (pcb , 0 );
933
934
deferred_close (pcb );
935
+ free (state -> http_payload_data );
936
+ state -> http_payload_data = NULL ;
934
937
}
935
938
else
936
939
tcp_arg (pcb , (void * )offs );
@@ -1123,9 +1126,9 @@ static void enduser_setup_handle_OPTIONS (struct tcp_pcb *http_client, char *dat
1123
1126
1124
1127
int type = 0 ;
1125
1128
1126
- if (strncmp (data , "GET " , 4 ) == 0 )
1129
+ if (strncmp (data , "OPTIONS " , 8 ) == 0 )
1127
1130
{
1128
- if (strncmp (data + 4 , "/aplist" , 7 ) == 0 || strncmp (data + 4 , "/setwifi?" , 9 ) == 0 || strncmp (data + 4 , "/status.json" , 12 ) == 0 )
1131
+ if (strncmp (data + 8 , "/aplist" , 7 ) == 0 || strncmp (data + 8 , "/setwifi?" , 9 ) == 0 || strncmp (data + 8 , "/status.json" , 12 ) == 0 )
1129
1132
{
1130
1133
enduser_setup_http_serve_header (http_client , json , strlen (json ));
1131
1134
return ;
@@ -1153,7 +1156,7 @@ static void enduser_setup_handle_POST(struct tcp_pcb *http_client, char* data, s
1153
1156
{
1154
1157
case 0 : {
1155
1158
// all went fine, extract all the form data into a file
1156
- enduser_setup_write_file_with_extra_configuration_data (body , bodylength );
1159
+ enduser_setup_write_file_with_extra_configuration_data (body , bodylength );
1157
1160
// redirect user to the base page with the trying flag
1158
1161
enduser_setup_http_serve_header (http_client , http_header_302_trying , LITLEN (http_header_302_trying ));
1159
1162
break ;
@@ -1283,7 +1286,7 @@ static void on_scan_done (void *arg, STATUS status)
1283
1286
const size_t hdr_sz = sizeof (header_fmt ) + 1 - 1 ; /* +expand %4d, -\0 */
1284
1287
1285
1288
/* To be able to safely escape a pathological SSID, we need 2*32 bytes */
1286
- const size_t max_entry_sz = 27 + 2 * 32 + 6 ; /* {"ssid":"","rssi":,"chan":} */
1289
+ const size_t max_entry_sz = 35 + 2 * 32 + 9 ; /* {"ssid":"","rssi":,"chan":,"auth ":} */
1287
1290
const size_t alloc_sz = hdr_sz + num_nets * max_entry_sz + 3 ;
1288
1291
char * http = calloc (1 , alloc_sz );
1289
1292
if (!http )
@@ -1319,6 +1322,12 @@ static void on_scan_done (void *arg, STATUS status)
1319
1322
1320
1323
p += sprintf (p , "%d" , wn -> channel );
1321
1324
1325
+ const char entry_auth [] = ",\"auth\":" ;
1326
+ strcpy (p , entry_auth );
1327
+ p += sizeof (entry_auth ) - 1 ;
1328
+
1329
+ p += sprintf (p , "%d" , wn -> authmode );
1330
+
1322
1331
* p ++ = '}' ;
1323
1332
}
1324
1333
* p ++ = ']' ;
@@ -1579,15 +1588,10 @@ static err_t enduser_setup_http_recvcb(void *arg, struct tcp_pcb *http_client, s
1579
1588
break ;
1580
1589
}
1581
1590
}
1582
- else if (strncmp (data + 4 , "/generate_204" , 13 ) == 0 )
1583
- {
1584
- /* Convince Android devices that they have internet access to avoid pesky dialogues. */
1585
- enduser_setup_http_serve_header (http_client , http_header_204 , LITLEN (http_header_204 ));
1586
- }
1587
1591
else
1588
1592
{
1589
- ENDUSER_SETUP_DEBUG ( "serving 404" );
1590
- enduser_setup_http_serve_header (http_client , http_header_404 , LITLEN (http_header_404 ));
1593
+ // All other URLs redirect to http://nodemcu.portal/ -- this triggers captive portal.
1594
+ enduser_setup_http_serve_header (http_client , http_header_302 , LITLEN (http_header_302 ));
1591
1595
}
1592
1596
}
1593
1597
else if (strncmp (data , "OPTIONS " , 8 ) == 0 )
@@ -1706,18 +1710,23 @@ static void enduser_setup_ap_start(void)
1706
1710
memset (& (cnf ), 0 , sizeof (struct softap_config ));
1707
1711
1708
1712
#ifndef ENDUSER_SETUP_AP_SSID
1709
- #define ENDUSER_SETUP_AP_SSID "SetupGadget "
1713
+ #define ENDUSER_SETUP_AP_SSID "NodeMCU "
1710
1714
#endif
1711
1715
1712
- char ssid [] = ENDUSER_SETUP_AP_SSID ;
1713
- int ssid_name_len = strlen (ssid );
1714
- memcpy (& (cnf .ssid ), ssid , ssid_name_len );
1716
+ if (state -> ap_ssid ) {
1717
+ strncpy (cnf .ssid , state -> ap_ssid , sizeof (cnf .ssid ));
1718
+ cnf .ssid_len = strlen (cnf .ssid );
1719
+ } else {
1720
+ char ssid [] = ENDUSER_SETUP_AP_SSID ;
1721
+ int ssid_name_len = strlen (ssid );
1722
+ memcpy (& (cnf .ssid ), ssid , ssid_name_len );
1715
1723
1716
- uint8_t mac [6 ];
1717
- wifi_get_macaddr (SOFTAP_IF , mac );
1718
- cnf .ssid [ssid_name_len ] = '_' ;
1719
- sprintf (cnf .ssid + ssid_name_len + 1 , "%02X%02X%02X" , mac [3 ], mac [4 ], mac [5 ]);
1720
- cnf .ssid_len = ssid_name_len + 7 ;
1724
+ uint8_t mac [6 ];
1725
+ wifi_get_macaddr (SOFTAP_IF , mac );
1726
+ cnf .ssid [ssid_name_len ] = '_' ;
1727
+ sprintf (cnf .ssid + ssid_name_len + 1 , "%02X%02X%02X" , mac [3 ], mac [4 ], mac [5 ]);
1728
+ cnf .ssid_len = ssid_name_len + 7 ;
1729
+ }
1721
1730
cnf .channel = state == NULL ? 1 : state -> softAPchannel ;
1722
1731
cnf .authmode = AUTH_OPEN ;
1723
1732
cnf .ssid_hidden = 0 ;
@@ -1895,6 +1904,8 @@ static void enduser_setup_free(void)
1895
1904
1896
1905
free_scan_listeners ();
1897
1906
1907
+ free (state -> ap_ssid );
1908
+
1898
1909
free (state );
1899
1910
state = NULL ;
1900
1911
}
@@ -1999,21 +2010,33 @@ static int enduser_setup_init(lua_State *L)
1999
2010
}
2000
2011
}
2001
2012
2002
- if (!lua_isnoneornil (L , 1 ))
2013
+ int argno = 1 ;
2014
+
2015
+ if (lua_isstring (L , argno )) {
2016
+ /* Get the SSID */
2017
+ state -> ap_ssid = strdup (lua_tostring (L , argno ));
2018
+ argno ++ ;
2019
+ }
2020
+
2021
+ if (!lua_isnoneornil (L , argno ))
2003
2022
{
2004
- lua_pushvalue (L , 1 );
2023
+ lua_pushvalue (L , argno );
2005
2024
state -> lua_connected_cb_ref = luaL_ref (L , LUA_REGISTRYINDEX );
2006
2025
}
2007
2026
2008
- if (!lua_isnoneornil (L , 2 ))
2027
+ argno ++ ;
2028
+
2029
+ if (!lua_isnoneornil (L , argno ))
2009
2030
{
2010
- lua_pushvalue (L , 2 );
2031
+ lua_pushvalue (L , argno );
2011
2032
state -> lua_err_cb_ref = luaL_ref (L , LUA_REGISTRYINDEX );
2012
2033
}
2013
2034
2014
- if (!lua_isnoneornil (L , 3 ))
2035
+ argno ++ ;
2036
+
2037
+ if (!lua_isnoneornil (L , argno ))
2015
2038
{
2016
- lua_pushvalue (L , 3 );
2039
+ lua_pushvalue (L , argno );
2017
2040
state -> lua_dbg_cb_ref = luaL_ref (L , LUA_REGISTRYINDEX );
2018
2041
ENDUSER_SETUP_DEBUG ("enduser_setup_init: Debug callback has been set" );
2019
2042
}
0 commit comments