From f1c3684e1cfc544011aeebcceea44a58619a3c5b Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 13 Feb 2021 14:28:10 -0600 Subject: [PATCH] Updated maxConnections logic and added Socket numbers to output homeSpan.begin() automatically resizes maxConnections to ensure it is at or below maxLimit, where maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled. maxConnections still defaults to 8 if unspecified, which means it is reduced to 7 if OTA is enabled. Users should set maxConnections to a lower number is they have implemented other socket-based services in their sketch --- src/HomeSpan.cpp | 38 +++++++++++++++++++++++++------------- src/src.ino | 12 ++++++++---- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index f3cc6d5..2c87099 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -54,10 +54,16 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa controlButton.init(controlPin); statusLED.init(statusPin); + int maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled; + if(maxConnections>maxLimit) + maxConnections=maxLimit; + hap=(HAPClient **)calloc(maxConnections,sizeof(HAPClient *)); for(int i=0;iavailable())){ // found a new HTTP client + if(newClient=hapServer->available()){ // found a new HTTP client int freeSlot=getFreeSlot(); // get next free slot if(freeSlot==-1){ // no available free slots @@ -184,6 +189,10 @@ void Span::poll() { LOG1(millis()/1000); LOG1(" sec) "); LOG1(hap[freeSlot]->client.remoteIP()); + LOG1(" on Socket "); + LOG1(hap[freeSlot]->client.fd()-LWIP_SOCKET_OFFSET+1); + LOG1("/"); + LOG1(CONFIG_LWIP_MAX_SOCKETS); LOG1("\n"); LOG2("\n"); @@ -400,7 +409,7 @@ void Span::checkConnect(){ Serial.print(modelName); Serial.print("\nSetup ID: "); Serial.print(qrID); - Serial.print("\n"); + Serial.print("\n\n"); MDNS.begin(hostName); // set server host name (.local implied) MDNS.setInstanceName(displayName); // set server display name @@ -440,11 +449,6 @@ void Span::checkConnect(){ mbedtls_base64_encode((uint8_t *)setupHash,9,&len,hashOutput,4); // Step 3: Encode the first 4 bytes of hashOutput in base64, which results in an 8-character, null-terminated, setupHash mdns_service_txt_item_set("_hap","_tcp","sh",setupHash); // Step 4: broadcast the resulting Setup Hash - Serial.print("\nStarting Web (HTTP) Server supporting up to "); - Serial.print(maxConnections); - Serial.print(" simultaneous connections...\n"); - hapServer->begin(); - if(otaEnabled){ if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ ArduinoOTA.setHostname(hostName); @@ -487,6 +491,11 @@ void Span::checkConnect(){ } } + Serial.print("Starting Web (HTTP) Server supporting up to "); + Serial.print(maxConnections); + Serial.print(" simultaneous connections...\n"); + hapServer->begin(); + Serial.print("\n"); if(!HAPClient::nAdminControllers()){ @@ -543,14 +552,17 @@ void Span::processSerialCommand(const char *c){ if(hap[i]->client){ Serial.print(hap[i]->client.remoteIP()); - Serial.print(" "); - + Serial.print(" on Socket "); + Serial.print(hap[i]->client.fd()-LWIP_SOCKET_OFFSET+1); + Serial.print("/"); + Serial.print(CONFIG_LWIP_MAX_SOCKETS); + if(hap[i]->cPair){ - Serial.print("ID="); + Serial.print(" ID="); HAPClient::charPrintRow(hap[i]->cPair->ID,36); Serial.print(hap[i]->cPair->admin?" (admin)":" (regular)"); } else { - Serial.print("(unverified)"); + Serial.print(" (unverified)"); } } else { diff --git a/src/src.ino b/src/src.ino index df7c1d1..d1cc9df 100644 --- a/src/src.ino +++ b/src/src.ino @@ -12,14 +12,18 @@ void setup() { // homeSpan.setHostNameSuffix(""); homeSpan.setPortNum(1200); - homeSpan.setMaxConnections(4); + homeSpan.setMaxConnections(6); // homeSpan.setQRID("One1"); homeSpan.enableOTA(); - homeSpan.setSketchVersion("Test 1.2.4"); + homeSpan.setSketchVersion("Test 1.2.6"); homeSpan.setWifiCallback(wifiEstablished); homeSpan.begin(Category::Lighting,"HomeSpanTest"); + Serial.printf("\nFD_SETSIZE: %d\n",FD_SETSIZE); + Serial.printf("CONFIG_LWIP_MAX_SOCKETS: %d\n",CONFIG_LWIP_MAX_SOCKETS); + Serial.printf("LWIP_SOCKET_OFFSET: %d\n\n",LWIP_SOCKET_OFFSET); + new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments new Service::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics @@ -43,11 +47,11 @@ void setup() { void loop(){ homeSpan.poll(); - + } // end of loop() ////////////////////////////////////// void wifiEstablished(){ - Serial.println("\n\nIN CALLBACK FUNCTION\n\n"); + Serial.print("IN CALLBACK FUNCTION\n\n"); }