From 18ac6ff484dbc2cb16f2b40af4d5915cce454a18 Mon Sep 17 00:00:00 2001 From: lorow Date: Wed, 23 Nov 2022 01:23:51 +0100 Subject: [PATCH] Fix wifi command not updating/adding new networks --- ESP/lib/src/data/config/project_config.cpp | 55 +++++++++++---------- ESP/lib/src/network/api/baseAPI/baseAPI.cpp | 8 ++- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/ESP/lib/src/data/config/project_config.cpp b/ESP/lib/src/data/config/project_config.cpp index ca9a2b9..e944b20 100644 --- a/ESP/lib/src/data/config/project_config.cpp +++ b/ESP/lib/src/data/config/project_config.cpp @@ -242,40 +242,45 @@ void ProjectConfig::setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t void ProjectConfig::setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify) { - WiFiConfig_t *networkToUpdate = nullptr; - // we store the ADHOC flag as false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP, and here we're just updating them size_t size = this->config.networks.size(); - if (size > 0) - { - for (int i = 0; i < size; i++) - { - if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0) - networkToUpdate = &this->config.networks[i]; - //! push_back creates a copy of the object, so we need to use emplace_back - if (networkToUpdate != nullptr) - { - this->config.networks.emplace_back( - networkName, - ssid, - password, - *channel, - false); - } - log_d("Updating wifi config"); - } - } - else - { - //! push_back creates a copy of the object, so we need to use emplace_back + // we're allowing to store up to three additional networks + if (size == 0) { + Serial.println("No networks, We're adding a new network"); + this->config.networks.emplace_back( + networkName, + ssid, + password, + *channel, + false); + } + + int networkToUpdate = -1; + for (int i = 0; i < size; i++){ + if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0){ + // we've found a preexisting network, let's upate it + networkToUpdate = i; + break; + } + } + + if (networkToUpdate >= 0) { + this->config.networks[networkToUpdate].name = networkName; + this->config.networks[networkToUpdate].ssid = ssid; + this->config.networks[networkToUpdate].password = password; + this->config.networks[networkToUpdate].channel = *channel; + this->config.networks[networkToUpdate].adhoc = false; + } else if (size < 3) { + Serial.println("We're adding a new network"); + // we don't have that network yet, we can add it as we still have some space + // we're using emplace_back as push_back will create a copy of it, we want to avoid that this->config.networks.emplace_back( networkName, ssid, password, *channel, false); - networkToUpdate = &this->config.networks[0]; } if (shouldNotify) diff --git a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp index d60259a..6e30d29 100644 --- a/ESP/lib/src/network/api/baseAPI/baseAPI.cpp +++ b/ESP/lib/src/network/api/baseAPI/baseAPI.cpp @@ -69,7 +69,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request) case POST: { int params = request->params(); - + std::string networkName; std::string ssid; std::string password; uint8_t channel = 0; @@ -79,6 +79,10 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request) for (int i = 0; i < params; i++) { AsyncWebParameter *param = request->getParam(i); + if (param->name() == "networkName") + { + networkName = param->value().c_str(); + } if (param->name() == "ssid") { ssid = param->value().c_str(); @@ -99,7 +103,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request) log_i("%s[%s]: %s\n", _networkMethodsMap[request->method()].c_str(), param->name().c_str(), param->value().c_str()); } // note: We're passing empty params by design, this is done to reset specific fields - projectConfig->setWifiConfig(ssid, ssid, password, &channel, adhoc, true); + projectConfig->setWifiConfig(networkName,ssid, password, &channel, adhoc, true); /* if (WiFiStateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC) {