From dbfad7d2225d3264b5ea5ea54d846f0640593f37 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 12 May 2024 07:17:21 -0500 Subject: [PATCH] initial change of hap[i] to linked-list from fixed array --- src/HAP.cpp | 19 +++++------ src/HAP.h | 1 - src/HomeSpan.cpp | 85 +++++++++++++++++++++--------------------------- 3 files changed, 46 insertions(+), 59 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index c63d95f..cf10f51 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -324,7 +324,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){ iosTLV.print(); LOG2("------------ END TLVS! ------------\n"); - LOG1("In Pair Setup #%d (%s)...",conNum,client.remoteIP().toString().c_str()); + LOG1("In Pair Setup #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); auto itState=iosTLV.find(kTLVType_State); @@ -344,7 +344,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){ return(0); }; - LOG2("Found . Expected .\n",tlvState,pairStatus); + LOG1("Found . Expected .\n",tlvState,pairStatus); if(tlvState!=pairStatus){ // error: Device is not yet paired, but out-of-sequence pair-setup STATE was received LOG0("\n*** ERROR: Out-of-Sequence Pair-Setup request!\n\n"); @@ -574,7 +574,7 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){ iosTLV.print(); LOG2("------------ END TLVS! ------------\n"); - LOG1("In Pair Verify #%d (%s)...",conNum,client.remoteIP().toString().c_str()); + LOG1("In Pair Verify #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); auto itState=iosTLV.find(kTLVType_State); @@ -594,7 +594,7 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){ return(0); }; - LOG2("Found \n",tlvState); // unlike pair-setup, out-of-sequencing can be handled gracefully for pair-verify (HAP requirement). No need to keep track of pairStatus + LOG1("Found \n",tlvState); // unlike pair-setup, out-of-sequencing can be handled gracefully for pair-verify (HAP requirement). No need to keep track of pairStatus switch(tlvState){ // Pair-Verify STATE received -- process request! (HAP Section 5.7) @@ -756,7 +756,7 @@ int HAPClient::postPairingsURL(uint8_t *content, size_t len){ iosTLV.print(); LOG2("------------ END TLVS! ------------\n"); - LOG1("In Post Pairings #%d (%s)...",conNum,client.remoteIP().toString().c_str()); + LOG1("In Post Pairings #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); auto itState=iosTLV.find(kTLVType_State); auto itMethod=iosTLV.find(kTLVType_Method); @@ -882,7 +882,7 @@ int HAPClient::getAccessoriesURL(){ return(0); } - LOG1("In Get Accessories #%d (%s)...\n",conNum,client.remoteIP().toString().c_str()); + LOG1("In Get Accessories #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); homeSpan.printfAttributes(); size_t nBytes=hapOut.getSize(); @@ -910,7 +910,7 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){ return(0); } - LOG1("In Get Characteristics #%d (%s)...\n",conNum,client.remoteIP().toString().c_str()); + LOG1("In Get Characteristics #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); int len=strlen(urlBuf); // determine number of IDs specified by counting commas in URL int numIDs=1; @@ -978,7 +978,7 @@ int HAPClient::putCharacteristicsURL(char *json){ return(0); } - LOG1("In Put Characteristics #%d (%s)...\n",conNum,client.remoteIP().toString().c_str()); + LOG1("In Put Characteristics #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); int n=homeSpan.countCharacteristics(json); // count number of objects in JSON request if(n==0) // if no objects found, return @@ -1031,7 +1031,7 @@ int HAPClient::putPrepareURL(char *json){ return(0); } - LOG1("In Put Prepare #%d (%s)...\n",conNum,client.remoteIP().toString().c_str()); + LOG1("In Put Prepare #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); char ttlToken[]="\"ttl\":"; char pidToken[]="\"pid\":"; @@ -1237,7 +1237,6 @@ void HAPClient::checkTimedWrites(){ else tw++; } - } ////////////////////////////////////// diff --git a/src/HAP.h b/src/HAP.h index 0a9e6fe..0aa42b8 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -93,7 +93,6 @@ struct HAPClient { WiFiClient client; // handle to client Controller *cPair=NULL; // pointer to info on current, session-verified Paired Controller (NULL=un-verified, and therefore un-encrypted, connection) - boolean isConnected=false; // flag to indicate client is connect // These temporary Curve25519 keys are generated in the first call to pair-verify and used in the second call to pair-verify so must persist for a short period diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 780c430..fd3a155 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -47,6 +47,8 @@ using namespace Utils; HapOut hapOut; // Specialized output stream that can both print to serial monitor and encrypt/transmit to HAP Clients with minimal memory usage (global-scoped variable) HAPClient **hap; // HAP Client structure containing HTTP client connections, parsing routines, and state variables (global-scoped variable) +list> hapList; // linked-list of HAP Client structures containing HTTP client connections, parsing routines, and state variables (global-scoped variable) + Span homeSpan; // HAP Attributes database and all related control functions for this Accessory (global-scoped variable) HapCharacteristics hapChars; // Instantiation of all HAP Characteristics used to create SpanCharacteristics (global-scoped variable) @@ -229,43 +231,36 @@ void Span::pollTask() { processSerialCommand(cBuf); } - WiFiClient newClient; - - if(newClient=hapServer->available()){ // found new client + if(hapServer->hasClient()){ + + auto it=hapList.emplace(hapList.begin()); + (*it).client=hapServer->available(); + +// homeSpan.clearNotify(socket); // clear all notification requests for this connection - int socket=newClient.fd()-LWIP_SOCKET_OFFSET; // get socket number (starting at zero) - - if(hap[socket]==NULL) // create HAPClient at that socket if it does not alreay exist - hap[socket]=new HAPClient; - - hap[socket]->client=newClient; // copy new client handle - hap[socket]->isConnected=true; // set isConnected flag - hap[socket]->cPair=NULL; // reset pointer to verified ID - homeSpan.clearNotify(socket); // clear all notification requests for this connection HAPClient::pairStatus=pairState_M1; // reset starting PAIR STATE (which may be needed if Accessory failed in middle of pair-setup) LOG2("=======================================\n"); - LOG1("** Client #%d Connected (%lu sec): %s\n",socket,millis()/1000,newClient.remoteIP().toString().c_str()); + LOG1("** Client #%d Connected (%lu sec): %s\n",(*it).client.fd()-LWIP_SOCKET_OFFSET,millis()/1000,(*it).client.remoteIP().toString().c_str()); LOG2("\n"); } - - for(int i=0;iclient){ // if the client is connected - if(hap[i]->client.available()){ // if client has data available - HAPClient::conNum=i; // set connection number - homeSpan.lastClientIP=hap[i]->client.remoteIP().toString(); // store IP Address for web logging - hap[i]->processRequest(); // PROCESS HAP REQUEST - homeSpan.lastClientIP="0.0.0.0"; // reset stored IP address to show "0.0.0.0" if homeSpan.getClientIP() is used in any other context - } - } - else if(hap[i]->isConnected){ // if client is not connected, but HAPClient thinks it is - LOG1("** Client #%d DISCONNECTED (%lu sec)\n",i,millis()/1000); - hap[i]->isConnected=false; - hap[i]->client.stop(); - delay(1); + auto it=hapList.begin(); + while(it!=hapList.end()){ + + if((*it).client.connected()){ // if the client is connected + if((*it).client.available()){ // if client has data available +// HAPClient::conNum=i; // set connection number + homeSpan.lastClientIP=(*it).client.remoteIP().toString(); // store IP Address for web logging + (*it).processRequest(); // PROCESS HAP REQUEST + homeSpan.lastClientIP="0.0.0.0"; // reset stored IP address to show "0.0.0.0" if homeSpan.getClientIP() is used in any other context } + it++; + } else { + LOG1("** Client #%d DISCONNECTED (%lu sec)\n",(*it).client.fd()-LWIP_SOCKET_OFFSET,millis()/1000); + (*it).client.stop(); + delay(5); + it=hapList.erase(it); } } @@ -277,7 +272,7 @@ void Span::pollTask() { for(auto it=PushButtons.begin();it!=PushButtons.end();it++) // check for SpanButton presses (*it)->check(); - HAPClient::checkNotifications(); +////// HAPClient::checkNotifications(); HAPClient::checkTimedWrites(); if(spanOTA.enabled) @@ -584,26 +579,20 @@ void Span::processSerialCommand(const char *c){ HAPClient::printControllers(); LOG0("\n"); - for(int i=0;iclient){ - LOG0(" %s",hap[i]->client.remoteIP().toString().c_str()); - - if(hap[i]->cPair){ - LOG0(" ID="); - HAPClient::charPrintRow(hap[i]->cPair->getID(),36); - LOG0(hap[i]->cPair->isAdmin()?" (admin)":" (regular)"); - } else { - LOG0(" (unverified)"); - } - } else { - LOG0(" unconnected"); - } - LOG0("\n"); + for(auto it=hapList.begin(); it!=hapList.end(); ++it){ + LOG0("Client #%d: %s",(*it).client.fd()-LWIP_SOCKET_OFFSET,(*it).client.remoteIP().toString().c_str()); + if((*it).cPair){ + LOG0(" ID="); + HAPClient::charPrintRow((*it).cPair->getID(),36); + LOG0((*it).cPair->isAdmin()?" (admin)":" (regular)\n"); + } else { + LOG0(" (unverified)\n"); } } + + if(hapList.empty()) + LOG0("No Client Connections!\n"); + LOG0("\n*** End Status ***\n\n"); } break;