diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2975413..28a9067 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -270,7 +270,8 @@ void Span::initWifi(){ } else if(digitalRead(resetPin)){ resetPressed=0; } else if(millis()>resetTime){ - status=network.apConfigure(hostName)?1:-1; + network.apConfigure(hostName); + status=1; } if(Serial.available() && *readSerial(key,1)=='W'){ diff --git a/src/Network.cpp b/src/Network.cpp index 206b640..28d740d 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -139,7 +139,7 @@ boolean Network::allowedCode(char *s){ /////////////////////////////// -boolean Network::apConfigure(char *apName){ +void Network::apConfigure(char *apName){ Serial.print("Starting Access Point: "); Serial.print(apName); @@ -163,16 +163,27 @@ boolean Network::apConfigure(char *apName){ dnsServer.start(DNS_PORT, "*", apIP); // start DNS server that resolves every request to the address of this device apServer.begin(); - int status=0; // initialize status alarmTimeOut=millis()+lifetime; // Access Point will shut down when alarmTimeOut is reached + apStatus=0; // status will be "timed out" unless changed - while(status==0){ + while(1){ // loop until we get timed out (which will be accelerated if save/cancel selected) if(millis()>alarmTimeOut){ - Serial.print("\n*** WARNING: Access Point timed Out ("); - Serial.print(lifetime/1000); - Serial.print(" seconds)\n\n"); - ESP.restart(); + WiFi.softAPdisconnect(true); // terminate connections and shut down captive access point + delay(100); + if(apStatus==1){ + Serial.print("\n*** Access Point: Exiting and Saving Settings\n\n"); + return; + } else { + if(apStatus==0){ + Serial.print("\n*** Access Point: Timed Out ("); + Serial.print(lifetime/1000); + Serial.print(" seconds)."); + } else + Serial.print("\n*** Access Point: Configuration Canceled."); + Serial.print(" Restarting...\n\n"); + ESP.restart(); + } } dnsServer.processNextRequest(); @@ -228,27 +239,19 @@ boolean Network::apConfigure(char *apName){ content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based - status=processRequest(body, (char *)content); // process request; returns 0=continue, 1=exit and save settings, -1=cancel changes and restart + processRequest(body, (char *)content); // process request LOG2("\n"); } // process HAP Client - } // while status==0 + } // while 1 - delay(2000); - - if(status==-1) - ESP.restart(); - - return(1); } /////////////////////////////// -int Network::processRequest(char *body, char *formData){ - - int returnCode=0; +void Network::processRequest(char *body, char *formData){ String responseHead="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n"; @@ -280,16 +283,25 @@ int Network::processRequest(char *body, char *formData){ } else - if(!strncmp(body,"POST /confirm-restart ",21)){ // GET CONFIRM-RESTART - responseBody+="

Settings saved!

Restarting HomeSpan in 2 seconds...

"; + if(!strncmp(body,"POST /save ",11)){ // GET SAVE getFormValue(formData,"code",setupCode,8); - // insert code to check whether setup Code allowed or not - returnCode=1; + + if(allowedCode(setupCode)){ + responseBody+="

Settings saved!

Restarting HomeSpan.

Please close this window...

"; + alarmTimeOut=millis()+2000; + apStatus=1; + + } else { + responseBody+="" + "

Disallowed Setup Code - too simple!

Returning to configuration page...

"; + } + } else - if(!strncmp(body,"GET /cancel-restart ",20)){ // GET CANCEL-RESTART - responseBody+="

Update canceled!

Restarting HomeSpan in 2 seconds...

"; - returnCode=-1; + if(!strncmp(body,"GET /cancel ",12)){ // GET CANCEL + responseBody+="

Configuration Canceled!

Restarting HomeSpan.

Please close this window...

"; + alarmTimeOut=millis()+2000; + apStatus=-1; } else if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS @@ -309,13 +321,13 @@ int Network::processRequest(char *body, char *formData){ responseBody+="

SUCCESS! Connected to:

" + String(ssid) + "

"; responseBody+="

You may enter new 8-digit Setup Code below, or leave blank to retain existing code.

"; - responseBody+="
" + responseBody+="" "" "
" "
" "
"; - responseBody+="
"; + responseBody+="
"; } } else @@ -345,11 +357,10 @@ int Network::processRequest(char *body, char *formData){ responseBody+="
" ""; - } else { + } else - if(!landingPage) - responseHead="HTTP/1.1 307 Temporary Redirect\r\nLocation: /landing-page\r\n"; - } + if(!landingPage) + responseHead="HTTP/1.1 307 Temporary Redirect\r\nLocation: /landing-page\r\n"; responseHead+="\r\n"; // add blank line between reponse header and body responseBody+=""; // close out body and html tags @@ -363,8 +374,6 @@ int Network::processRequest(char *body, char *formData){ client.print(responseHead); client.print(responseBody); LOG2("------------ SENT! --------------\n"); - - return(returnCode); } // processRequest diff --git a/src/Network.h b/src/Network.h index d41d09f..c87f557 100644 --- a/src/Network.h +++ b/src/Network.h @@ -19,7 +19,8 @@ struct Network { WiFiClient client=NULL; // client used for HTTP calls unsigned long timer; // length of time of trying to connect to WiFi unsigned long alarmTimeOut; // alarm time after which access point is shut down and HomeSpan is re-started - boolean landingPage=false; // check to see if captive accerss point landing page is accessed + boolean landingPage=false; // check to see if captive access point landing page is accessed + int apStatus; // tracks access point status (0=timed-out, -1=cancel, 1=save) char ssid[MAX_SSID+1]; char pwd[MAX_PWD+1]; @@ -28,8 +29,8 @@ struct Network { void scan(); // scan for WiFi networks and save only those with unique SSIDs boolean serialConfigure(); // configure homeSpan WiFi and Setup Code from Serial Monitor; return 1=save settings, 0=cancel settings boolean allowedCode(char *s); // checks if Setup Code is allowed (HAP defines a list of disallowed codes) - boolean apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; return 1=save settinmgs, 0=cancel settings - int processRequest(char *body, char *formData); // process the HTTP request; return 0=continue, 1=save connection, -1=cancel and re-start + void apConfigure(char *hostName); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point 'hostName'; only returns if sucessful, else ESP restarts + void processRequest(char *body, char *formData); // process the HTTP request int getFormValue(char *formData, char *tag, char *value, int maxSize); // search for 'tag' in 'formData' and copy result into 'value' up to 'maxSize' characters; returns number of characters, else -1 if 'tag' not found int badRequestError(); // return 400 error