Moved all captive code into Configure.cpp

This commit is contained in:
Gregg 2020-09-03 21:48:08 -05:00
parent b957b540c0
commit 9a80b8fa41
5 changed files with 15 additions and 181 deletions

View File

@ -1,10 +1,13 @@
#include "Configure.h"
#include "Settings.h"
///////////////////////////////
void Configure::processRequest(WiFiClient &client, char *body, char *formData){
String s;
if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE
strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form
@ -13,8 +16,11 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
LOG1("In Post Configure...\n");
String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
s+="<html><head><title>Initiating</title><meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" /></head><body style=\"background-color:lightyellow;\">";
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
// s+="<html><head><title>Initiating</title><meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" /></head><body style=\"background-color:lightyellow;\">";
s+="<html><head><title>Initiating</title></head><body style=\"background-color:lightyellow;\">";
s+="<meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" />";
s+="<p style=\"font-size:300%;\">Initiating WiFi Connection...</p></body></html>";
} else
@ -23,7 +29,7 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
LOG1("In Get WiFi Status...\n");
String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\nRefresh: 5\r\n\r\n";
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\nRefresh: 5\r\n\r\n";
s+="<html><head><title>Connection Status</title></head><body style=\"background-color:lightyellow;\">";
s+="<p style=\"font-size:300%;\">Trying to Connect (";
s+=String(millis()/1000) + "sec)...</p></body></html>";
@ -34,7 +40,7 @@ void Configure::processRequest(WiFiClient &client, char *body, char *formData){
int n=WiFi.scanNetworks();
String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
s+="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
s+="<html><head><title>HomeSpan Configuration</title><style>p{font-size:300%; margin:25px}label{font-size:300%; margin:25px}input{font-size:250%; margin:25px}</style></head>";
s+="<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan_12_54_DD_E4_23_F5</b></p></center>";
s+="<p>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.</p>";

View File

@ -1,5 +1,5 @@
#include <HAP.h>
#include <WiFi.h>
///////////////////////////////

View File

@ -163,9 +163,9 @@ void HAPClient::processRequest(){
LOG2(body);
LOG2("\n------------ END BODY! ------------\n");
if(homeSpan.needsConfiguration){ // device not yet configured
content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based
configure.processRequest(client, body, (char *)content); // process request
if(homeSpan.needsConfiguration){ // device not yet configured
content[cLen]='\0'; // add a trailing null on end of any contents, which should always be text-based
configure.processRequest(client, body, (char *)content); // process request
return;
}
@ -338,102 +338,6 @@ 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+="<html><head><title>HomeSpan Configuration</title><style>p{font-size:300%; margin:25px}label{font-size:300%; margin:25px}input{font-size:250%; margin:25px}</style></head>";
s+="<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan_12_54_DD_E4_23_F5</b></p></center>";
s+="<p>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.</p>";
s+="<p>The LED on this device should be <em>double-blinking</em> during this configuration.<p>";
s+="<form action=\"/configure\" method=\"post\">";
s+="<label for=\"ssid\">WiFi Network:</label>";
s+="<input list=\"network\" name=\"network\" placeholder=\"Choose or Type\" required>";
s+="<datalist id=\"network\">";
for(int i=0;i<n;i++){
if(s.indexOf(WiFi.SSID(i))==-1) // first time this SSID founx
s+="<option value=\"" + WiFi.SSID(i) + "\">" + WiFi.SSID(i) + "</option>";
}
s+="</datalist><br><br>";
s+="<label for=\"pwd\">WiFi Password:</label>";
s+="<input type=\"password\" id=\"pwd\" name=\"pwd\">";
s+="<br><br>";
s+="<label for=\"code\">Setup Code:</label>";
s+="<input type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\">";
/*
apClient.print("<p><em>");
apClient.print("This device already has a Setup Code. You may leave Setup Code blank to retain the current one, or type a new Setup Code to change.");
apClient.print("This device does not yet have a Setup Code. You must create one above.");
apClient.print("</p>");
*/
s+="<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>";
s+="</form></body></html>";
LOG2("\n>>>>>>>>>> ");
LOG2(client.remoteIP());
LOG2(" >>>>>>>>>>\n");
LOG2(s);
LOG2("\n");
client.print(s);
LOG2("------------ SENT! --------------\n");
return(1);
}
//////////////////////////////////////
int HAPClient::postConfigureURL(char *formData){
LOG1("In Post Configure...\n");
String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
s+="<html><head><title>Initiating</title><meta http-equiv = \"refresh\" content = \"2; url = /wifi-status\" /></head><body style=\"background-color:lightyellow;\">";
s+="<p style=\"font-size:300%;\">Initiating WiFi Connection...</p></body></html>";
LOG2("\n>>>>>>>>>> ");
LOG2(client.remoteIP());
LOG2(" >>>>>>>>>>\n");
LOG2(s);
LOG2("\n");
client.print(s);
LOG2("------------ SENT! --------------\n");
return(1);
}
//////////////////////////////////////
int HAPClient::getWiFiStatusURL(){
LOG1("In Get WiFi Status...\n");
String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\nRefresh: 5\r\n\r\n";
s+="<html><head><title>Connection Status</title></head><body style=\"background-color:lightyellow;\">";
s+="<p style=\"font-size:300%;\">Trying to Connect (";
s+=String(millis()/1000) + "sec)...</p></body></html>";
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...");

View File

@ -82,11 +82,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 postConfigureURL(char *formData); // POST /configure (used to process captive access mode form)
int getWiFiStatusURL(); // GET /wifi-status (used to check status of connecting to WiFi network)
void processRequest(); // process HAP request
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)

View File

@ -387,78 +387,6 @@ void Span::configure(char *apName){
}
while(1);
while(!needsConfiguration){
dnsServer.processNextRequest();
WiFiClient apClient=apServer.available();
if(apClient){ // found a new HTTP client
Serial.print("Client Found: ");
Serial.println(apClient.remoteIP());
while(apClient.available()){ // data is available
int nBytes=apClient.read(HAPClient::httpBuf,HAPClient::MAX_HTTP); // read all available bytes up to maximum allowed
char *p=(char *)HAPClient::httpBuf;
p[nBytes]='\0';
Serial.print(p);
Serial.print("\n");
if(!strncmp(p,"GET /",5)){
apClient.println("HTTP/1.1 200 OK");
apClient.println("Content-type:text/html");
apClient.println();
apClient.print("<html><head><title>HomeSpan Configuration</title><style>p{font-size:300%; margin:25px}label{font-size:300%; margin:25px}input{font-size:250%; margin:25px}</style></head>");
apClient.print("<body style=\"background-color:lightyellow;\"><center><p><b>HomeSpan_12_54_DD_E4_23_F5</b></p></center>");
apClient.print("<p>Welcome to HomeSpan! This page allows you to configure the above HomeSpan device to connect to your WiFi network, and to create a Setup Code for pairing this device to HomeKit.</p>");
apClient.print("<p><em>WiFi Network</em> is a required field. If your network name is broadcast you can select it from the dropdown list. If you don't see your network name in the dropdown list you may type it into the text box.</p>");
apClient.print("<p><em>WiFi Password</em> is required only if your network is password-protected.</p>");
apClient.print("<p>Every HomeKit device requires an 8-digit <em>Setup Code</em>. If this device is being configured for the first time, or if the device has been factory reset, you must create a new <em>Setup Code</em>. ");
apClient.print("Otherwise, either leave this field blank to retain the current <em>Setup Code</em>, or type a new 8-digit <em>Setup Code</em> to replace the current one.</p>");
apClient.print("<p>Note that HomeSpan cannot display a previously saved <em>Setup Code</em>, so if you've forgotten what it is, this is the place to create a new one.</p>");
apClient.print("<p>The LED on this device should be <em>double-blinking</em> during this configuration.<p>");
apClient.print("<form method=\"post\">");
apClient.print("<label for=\"ssid\">WiFi Network:</label>");
apClient.print("<input list=\"network\" name=\"network\" placeholder=\"Type/Select\" required>");
apClient.print("<datalist id=\"network\">");
apClient.print("<option value=\"SEA-SHORE\">SEA-SHORE</option>");
apClient.print("<option value=\"NetworkA\">Network A</option>");
apClient.print("<option value=\"NetworkB\">Network B</option>");
apClient.print("<option value=\"MyNet\">My Network</option>");
apClient.print("</datalist><br><br>");
apClient.print("<label for=\"pwd\">WiFi Password:</label>");
apClient.print("<input type=\"password\" id=\"pwd\" name=\"pwd\">");
apClient.print("<br><br>");
apClient.print("<label for=\"code\">Setup Code:</label>");
apClient.print("<input type=\"tel\" id=\"code\" name=\"code\" placeholder=\"12345678\" pattern=\"[0-9]{8}\">");
apClient.print("<p><em>");
apClient.print("This device already has a Setup Code. You may leave Setup Code blank to retain the current one, or type a new Setup Code to change.");
apClient.print("This device does not yet have a Setup Code. You must create one above.");
apClient.print("</p>");
apClient.print("<center><input style=\"font-size:300%\" type=\"submit\" value=\"SUBMIT\"></center>");
apClient.print("</form></body></html>");
} // GET
if(!strncmp(p,"POST /",6)){
apClient.println("HTTP/1.1 200 OK");
apClient.println("Content-type:text/html");
apClient.println();
apClient.println("SUCCESS");
}
} // data available
apClient.stop();
}
}
}