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
This commit is contained in:
Gregg 2021-02-13 14:28:10 -06:00
parent 7400383b0a
commit f1c3684e1c
2 changed files with 33 additions and 17 deletions

View File

@ -54,10 +54,16 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
controlButton.init(controlPin); controlButton.init(controlPin);
statusLED.init(statusPin); statusLED.init(statusPin);
int maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled;
if(maxConnections>maxLimit)
maxConnections=maxLimit;
hap=(HAPClient **)calloc(maxConnections,sizeof(HAPClient *)); hap=(HAPClient **)calloc(maxConnections,sizeof(HAPClient *));
for(int i=0;i<maxConnections;i++) for(int i=0;i<maxConnections;i++)
hap[i]=new HAPClient; hap[i]=new HAPClient;
hapServer=new WiFiServer(tcpPortNum);
delay(2000); delay(2000);
Serial.print("\n************************************************************\n" Serial.print("\n************************************************************\n"
@ -135,7 +141,6 @@ void Span::poll() {
statusLED.start(LED_WIFI_NEEDED); statusLED.start(LED_WIFI_NEEDED);
} else { } else {
homeSpan.statusLED.start(LED_WIFI_CONNECTING); homeSpan.statusLED.start(LED_WIFI_CONNECTING);
hapServer=new WiFiServer(tcpPortNum,maxConnections+1);
} }
controlButton.reset(); controlButton.reset();
@ -159,7 +164,7 @@ void Span::poll() {
WiFiClient newClient; WiFiClient newClient;
if(hapServer && (newClient=hapServer->available())){ // found a new HTTP client if(newClient=hapServer->available()){ // found a new HTTP client
int freeSlot=getFreeSlot(); // get next free slot int freeSlot=getFreeSlot(); // get next free slot
if(freeSlot==-1){ // no available free slots if(freeSlot==-1){ // no available free slots
@ -184,6 +189,10 @@ void Span::poll() {
LOG1(millis()/1000); LOG1(millis()/1000);
LOG1(" sec) "); LOG1(" sec) ");
LOG1(hap[freeSlot]->client.remoteIP()); 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"); LOG1("\n");
LOG2("\n"); LOG2("\n");
@ -400,7 +409,7 @@ void Span::checkConnect(){
Serial.print(modelName); Serial.print(modelName);
Serial.print("\nSetup ID: "); Serial.print("\nSetup ID: ");
Serial.print(qrID); Serial.print(qrID);
Serial.print("\n"); Serial.print("\n\n");
MDNS.begin(hostName); // set server host name (.local implied) MDNS.begin(hostName); // set server host name (.local implied)
MDNS.setInstanceName(displayName); // set server display name 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 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 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(otaEnabled){
if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){
ArduinoOTA.setHostname(hostName); 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"); Serial.print("\n");
if(!HAPClient::nAdminControllers()){ if(!HAPClient::nAdminControllers()){
@ -543,14 +552,17 @@ void Span::processSerialCommand(const char *c){
if(hap[i]->client){ if(hap[i]->client){
Serial.print(hap[i]->client.remoteIP()); 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){ if(hap[i]->cPair){
Serial.print("ID="); Serial.print(" ID=");
HAPClient::charPrintRow(hap[i]->cPair->ID,36); HAPClient::charPrintRow(hap[i]->cPair->ID,36);
Serial.print(hap[i]->cPair->admin?" (admin)":" (regular)"); Serial.print(hap[i]->cPair->admin?" (admin)":" (regular)");
} else { } else {
Serial.print("(unverified)"); Serial.print(" (unverified)");
} }
} else { } else {

View File

@ -12,14 +12,18 @@ void setup() {
// homeSpan.setHostNameSuffix(""); // homeSpan.setHostNameSuffix("");
homeSpan.setPortNum(1200); homeSpan.setPortNum(1200);
homeSpan.setMaxConnections(4); homeSpan.setMaxConnections(6);
// homeSpan.setQRID("One1"); // homeSpan.setQRID("One1");
homeSpan.enableOTA(); homeSpan.enableOTA();
homeSpan.setSketchVersion("Test 1.2.4"); homeSpan.setSketchVersion("Test 1.2.6");
homeSpan.setWifiCallback(wifiEstablished); homeSpan.setWifiCallback(wifiEstablished);
homeSpan.begin(Category::Lighting,"HomeSpanTest"); 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 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 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(){ void loop(){
homeSpan.poll(); homeSpan.poll();
} // end of loop() } // end of loop()
////////////////////////////////////// //////////////////////////////////////
void wifiEstablished(){ void wifiEstablished(){
Serial.println("\n\nIN CALLBACK FUNCTION\n\n"); Serial.print("IN CALLBACK FUNCTION\n\n");
} }