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);
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;i<maxConnections;i++)
hap[i]=new HAPClient;
hapServer=new WiFiServer(tcpPortNum);
delay(2000);
Serial.print("\n************************************************************\n"
@ -135,7 +141,6 @@ void Span::poll() {
statusLED.start(LED_WIFI_NEEDED);
} else {
homeSpan.statusLED.start(LED_WIFI_CONNECTING);
hapServer=new WiFiServer(tcpPortNum,maxConnections+1);
}
controlButton.reset();
@ -159,7 +164,7 @@ void Span::poll() {
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
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,7 +552,10 @@ 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=");

View File

@ -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
@ -49,5 +53,5 @@ void loop(){
//////////////////////////////////////
void wifiEstablished(){
Serial.println("\n\nIN CALLBACK FUNCTION\n\n");
Serial.print("IN CALLBACK FUNCTION\n\n");
}