ESP32/components/wifi_cfg/wifi_cfg.c

212 lines
6.3 KiB
C

/*
* wifi_cfg.c
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "wifi_cfg.h"
#include <string.h>
#include "esp_log.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "nvs.h"
static const char *TAG = "WIFI_CFG";
static const char *NVS_NS = "storage"; // Shared namespace for all settings
// --- Defaults ---
#define DEFAULT_MONITOR_CHANNEL 6
// --- Initialization ---
void wifi_cfg_init(void) {
// NVS is usually initialized in app_main, but we can double check here
// or just leave it empty if no specific module init is needed.
}
// --- Apply Configuration ---
bool wifi_cfg_apply_from_nvs(void) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READONLY, &h) != ESP_OK) {
return false; // No config found
}
// 1. Load SSID/Pass
size_t len = 0;
if (nvs_get_str(h, "wifi_ssid", NULL, &len) == ESP_OK && len > 0) {
char *ssid = malloc(len);
char *pass = NULL;
nvs_get_str(h, "wifi_ssid", ssid, &len);
if (nvs_get_str(h, "wifi_pass", NULL, &len) == ESP_OK && len > 0) {
pass = malloc(len);
nvs_get_str(h, "wifi_pass", pass, &len);
}
wifi_config_t wifi_config = {0};
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid) - 1);
wifi_config.sta.ssid[sizeof(wifi_config.sta.ssid) - 1] = '\0';
if (pass) {
strncpy((char *)wifi_config.sta.password, pass, sizeof(wifi_config.sta.password) - 1);
wifi_config.sta.password[sizeof(wifi_config.sta.password) - 1] = '\0';
}
ESP_LOGI(TAG, "Applying WiFi Config: SSID=%s", ssid);
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
free(ssid);
if (pass) free(pass);
}
nvs_close(h);
return true;
}
// --- Getters ---
bool wifi_cfg_get_mode(char *mode_out, uint8_t *channel_out) {
// This function seems to be used to retrieve saved "mode" strings
// For now, we default to whatever is implicit, or implement saving "wifi_mode" later.
// Returning false implies default behavior.
return false;
}
// --- Setters (Credentials) ---
bool wifi_cfg_set_ssid(const char *ssid) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) != ESP_OK) return false;
esp_err_t err = nvs_set_str(h, "wifi_ssid", ssid);
if (err == ESP_OK) nvs_commit(h);
nvs_close(h);
return (err == ESP_OK);
}
bool wifi_cfg_set_password(const char *password) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) != ESP_OK) return false;
esp_err_t err;
if (password && strlen(password) > 0) {
err = nvs_set_str(h, "wifi_pass", password);
} else {
err = nvs_erase_key(h, "wifi_pass"); // Clear if empty
}
if (err == ESP_OK) nvs_commit(h);
nvs_close(h);
return (err == ESP_OK);
}
// --- Monitor Channel Settings ---
bool wifi_cfg_set_monitor_channel(uint8_t channel) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) != ESP_OK) return false;
esp_err_t err = nvs_set_u8(h, "mon_chan", channel);
if (err == ESP_OK) nvs_commit(h);
nvs_close(h);
return (err == ESP_OK);
}
void wifi_cfg_clear_monitor_channel(void) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) == ESP_OK) {
nvs_erase_key(h, "mon_chan");
nvs_commit(h);
nvs_close(h);
}
}
bool wifi_cfg_monitor_channel_is_unsaved(uint8_t current_val) {
nvs_handle_t h;
uint8_t saved_val = DEFAULT_MONITOR_CHANNEL;
if (nvs_open(NVS_NS, NVS_READONLY, &h) == ESP_OK) {
nvs_get_u8(h, "mon_chan", &saved_val);
nvs_close(h);
}
return (saved_val != current_val);
}
// ... existing code ...
// --- IP Configuration ---
bool wifi_cfg_set_ipv4(const char *ip, const char *mask, const char *gw) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) != ESP_OK) return false;
nvs_set_str(h, "static_ip", ip);
nvs_set_str(h, "static_mask", mask);
nvs_set_str(h, "static_gw", gw);
nvs_commit(h);
nvs_close(h);
return true;
}
bool wifi_cfg_get_ipv4(char *ip, char *mask, char *gw) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READONLY, &h) != ESP_OK) return false;
size_t len = 16;
bool exists = (nvs_get_str(h, "static_ip", ip, &len) == ESP_OK);
len = 16; nvs_get_str(h, "static_mask", mask, &len);
len = 16; nvs_get_str(h, "static_gw", gw, &len);
nvs_close(h);
return exists;
}
bool wifi_cfg_set_dhcp(bool enable) {
nvs_handle_t h;
if (nvs_open(NVS_NS, NVS_READWRITE, &h) != ESP_OK) return false;
nvs_set_u8(h, "dhcp_en", enable ? 1 : 0);
nvs_commit(h);
nvs_close(h);
return true;
}
bool wifi_cfg_get_dhcp(void) {
nvs_handle_t h;
uint8_t val = 1; // Default to Enabled
if (nvs_open(NVS_NS, NVS_READONLY, &h) == ESP_OK) {
nvs_get_u8(h, "dhcp_en", &val);
nvs_close(h);
}
return (val != 0);
}