cvonk / OPNpool

Integrates the functionality of a Pentair pool controller into the smart home using ESP32 SoC.
GNU General Public License v3.0
75 stars 5 forks source link

Lifetime bug in interface/main/main.c:135:_connect2wifi_and_start_httpd when using hardcoded wifi credentials #9

Closed bishopdynamics closed 1 year ago

bishopdynamics commented 1 year ago

When using hardcoded wifi credentials, I noticed an issue where the config object goes out of scope, but a pointer to the config is still passed to wifi_connect_start().

Here is a potential fix:

/*
 * Connect to WiFi accesspoint.
 * Register callbacks when connected or disconnected.
 */

static void
_connect2wifi_and_start_httpd(ipc_t * const ipc)
{
    static wifi_connect_priv_t priv = {};
    priv.ipc = ipc;

    wifi_connect_config_t wifi_connect_config = {
        .onConnect = _wifi_connect_cb,
        .onDisconnect = _wifi_disconnect_cb,
        .priv = &priv,
    };
    ESP_ERROR_CHECK(wifi_connect_init(&wifi_connect_config));
    esp_err_t err = NULL;
#ifdef CONFIG_OPNPOOL_HARDCODED_WIFI_CREDENTIALS
    if (strlen(CONFIG_OPNPOOL_HARDCODED_WIFI_SSID)) {
        ESP_LOGW(TAG, "Using SSID from Kconfig");
        wifi_config_t wifi_config = {
            .sta = {
                .ssid = CONFIG_OPNPOOL_HARDCODED_WIFI_SSID,
                .password = CONFIG_OPNPOOL_HARDCODED_WIFI_PASSWD,
            }
        };
        err = wifi_connect_start(&wifi_config);
    } else
#endif
    {
        ESP_LOGW(TAG, "Using SSID from nvram");
        err = wifi_connect_start(NULL);
    }
    if (err == ESP_ERR_WIFI_SSID) {
        ESP_LOGE(TAG, "Wi-Fi SSID/passwd not provisioned");
        _delete_task();
    }
    ESP_ERROR_CHECK(err);
}
cvonk commented 1 year ago

Thanks so much, and sorry for the delay. I incorporated your fix in v1.2.5.