From b957b540c0c7ba52a3c33d75258e6a1dbd76a64f Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 3 Sep 2020 21:24:27 -0500 Subject: [PATCH] created struct Configure --- src/Configure.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++ src/Configure.h | 9 +++++ src/HAP.cpp | 29 ++++------------ src/HAP.h | 2 ++ 4 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 src/Configure.cpp create mode 100644 src/Configure.h diff --git a/src/Configure.cpp b/src/Configure.cpp new file mode 100644 index 0000000..5fa0375 --- /dev/null +++ b/src/Configure.cpp @@ -0,0 +1,84 @@ + +#include "Configure.h" + +/////////////////////////////// + +void Configure::processRequest(WiFiClient &client, char *body, char *formData){ + + if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE + strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form + + LOG2(formData); // print form data + LOG2("\n------------ END DATA! ------------\n"); + + LOG1("In Post Configure...\n"); + + String s="HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; + s+="Initiating"; + s+="

Initiating WiFi Connection...

"; + + } else + + if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS + + 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+="Connection Status"; + s+="

Trying to Connect ("; + s+=String(millis()/1000) + "sec)...

"; + + } else { // LOGIN PAGE + + 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 Configuration"; + s+="

HomeSpan_12_54_DD_E4_23_F5

"; + s+="

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+="

"; + s+=""; + s+=""; + s+=""; + + for(int i=0;i" + WiFi.SSID(i) + ""; + } + s+="

"; + + s+=""; + s+=""; + s+="

"; + + s+=""; + s+=""; + + /* + apClient.print("

"); + 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("

"); +*/ + + s+="
"; + s+=""; + + + } + + LOG2("\n>>>>>>>>>> "); + LOG2(client.remoteIP()); + LOG2(" >>>>>>>>>>\n"); + LOG2(s); + LOG2("\n"); + client.print(s); + LOG2("------------ SENT! --------------\n"); + +} // processRequest + +////////////////////////////////////// diff --git a/src/Configure.h b/src/Configure.h new file mode 100644 index 0000000..bf121b5 --- /dev/null +++ b/src/Configure.h @@ -0,0 +1,9 @@ + +#include + +/////////////////////////////// + +struct Configure { + + void processRequest(WiFiClient &client, char *body, char *formData); +}; diff --git a/src/HAP.cpp b/src/HAP.cpp index 321e126..e3a4321 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -163,28 +163,11 @@ void HAPClient::processRequest(){ LOG2(body); LOG2("\n------------ END BODY! ------------\n"); - if(homeSpan.needsConfiguration){ // device not yet configured - only allow certain URLs - - if(!strncmp(body,"POST /configure ",16) && // POST CONFIGURE - strstr(body,"Content-Type: application/x-www-form-urlencoded")){ // check that content is from a form - - content[cLen]='\0'; // add a trailing null on end of POST data - LOG2((char *)content); // print data - LOG2("\n------------ END DATA! ------------\n"); - - postConfigureURL((char *)content); // process URL - return; - } - - if(!strncmp(body,"GET /wifi-status ",17)){ // GET WIFI-STATUS - getWiFiStatusURL(); - return; - } - - captiveAccessURL(); // default action for all other URLs when in captive Access Point mode - return; - - } // captive access point URLs only - everything below is for normal HAP requests + 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; + } if(!strncmp(body,"POST ",5)){ // this is a POST request @@ -1684,5 +1667,5 @@ Accessory HAPClient::accessory; Controller HAPClient::controllers[MAX_CONTROLLERS]; SRP6A HAPClient::srp; int HAPClient::conNum; - +Configure HAPClient::configure; diff --git a/src/HAP.h b/src/HAP.h index 9716c4e..1a0a8df 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -7,6 +7,7 @@ #include "HKDF.h" #include "SRP.h" #include "HomeSpan.h" +#include "Configure.h" ///////////////////////////////////////////////// // NONCE Structure (HAP used last 64 of 96 bits) @@ -58,6 +59,7 @@ struct HAPClient { static Accessory accessory; // Accessory ID and Ed25519 public and secret keys- permanently stored static Controller controllers[MAX_CONTROLLERS]; // Paired Controller IDs and ED25519 long-term public keys - permanently stored static int conNum; // connection number - used to keep track of per-connection EV notifications + static Configure configure; // initial configuration of WiFi credentials and Setup Code // individual structures and data defined for each Hap Client connection