diff --git a/src/HAP.cpp b/src/HAP.cpp index 781a25d..c82aeb7 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -163,6 +163,13 @@ void HAPClient::processRequest(){ LOG2(body); LOG2("\n------------ END BODY! ------------\n"); + if(homeSpan.needsConfiguration){ // device not yet configured - only allow certain URLs + + captiveAccessURL(); // default action for all other URLs when in captive Access Point mode + return; + + } // captive access point URLs only + if(!strncmp(body,"POST ",5)){ // this is a POST request if(cLen==0){ @@ -332,6 +339,59 @@ int HAPClient::unauthorizedError(){ ////////////////////////////////////// +int HAPClient::captiveAccessURL(){ + + LOG1("In Captive Access...\n"); + + int n=WiFi.scanNetworks(); + + String s="HTTP/1.1 200 OK\r\nContent-type:text/html\r\n\r\n"; + s+="
HomeSpan_12_54_DD_E4_23_F5
Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network, and (if needed) to create a Setup Code for pairing this device to HomeKit.
"; + s+="The LED on this device should be double-blinking during this configuration.
"; + + s+="
"; + + LOG2("\n>>>>>>>>>> "); + LOG2(client.remoteIP()); + LOG2(" >>>>>>>>>>\n"); + LOG2(s); + LOG2("\n"); + client.print(s); + LOG2("------------ SENT! --------------\n"); + return(1); + +} + +////////////////////////////////////// + int HAPClient::postPairSetupURL(){ LOG1("In Pair Setup..."); diff --git a/src/HAP.h b/src/HAP.h index cb309cd..e94d70a 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -81,6 +81,7 @@ struct HAPClient { // define member methods void processRequest(); // process HAP request + int captiveAccessURL(); // default process for requests made when in captive access point mode int postPairSetupURL(); // POST /pair-setup (HAP Section 5.6) int postPairVerifyURL(); // POST /pair-verify (HAP Section 5.7) int getAccessoriesURL(); // GET /accessories (HAP Section 6.6) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 6b347c9..11431d0 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -353,15 +353,9 @@ void Span::configure(char *apName){ dnsServer.start(DNS_PORT, "*", apIP); apServer.begin(); - boolean configured=false; + needsConfiguration=true; - TODO: Eliminate these two lines and replace with a new generic CaptiveAP flag to trackAP mode - ALSO CHANGE HAP[0] to POINTER REFERENCES - - hap[0].cPair=NULL; // reset pointer to verified ID - HAPClient::pairStatus=pairState_M1; // reset starting PAIR STATE (which may be needed if Accessory failed in middle of pair-setup) - - while(!configured){ + while(needsConfiguration){ dnsServer.processNextRequest(); @@ -395,7 +389,7 @@ void Span::configure(char *apName){ while(1); - while(!configured){ + while(!needsConfiguration){ dnsServer.processNextRequest(); WiFiClient apClient=apServer.available(); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 16ae87a..c14e742 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -54,7 +54,9 @@ struct Span{ unsigned long resetTime; // tracks time once reset button is pressed Blinker statusLED=Blinker(LED_BUILTIN); // indicates HomeSpan status - + + boolean needsConfiguration=false; // tracks whether device needs to be configured with WiFi credentials and a Setup Code + SpanConfig hapConfig; // track configuration changes to the HAP Accessory database; used to increment the configuration number (c#) when changes found vector