| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- #include "defines.h"
- #include <fstream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <iostream>
- #include <string.h>
- #include "read_network_config.h"
- #include "Helper.h"
- #include "connect_wifi_sta.h"
- #include "esp_log.h"
- #include "ClassLogFile.h"
- static const char *TAG = "NETWORK CONFIG";
- struct network_config_t network_config = {};
- struct network_config_t network_config_temp = {};
- int LoadNetworkFromFile(std::string filename)
- {
- filename = format_filename(filename);
- FILE *pFile = fopen(filename.c_str(), "r");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "unable to open wlan.ini (read)!");
- return -1;
- }
- ESP_LOGD(TAG, "LoadNetworkFromFile: wlan.ini opened");
- std::string line = "";
- char temp_bufer[256];
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "wlan.ini opened, but empty or content not readable!");
- fclose(pFile);
- return -1;
- }
- else
- {
- line = std::string(temp_bufer);
- }
- int _fix_ipaddress = 3;
- std::vector<std::string> splitted;
- while ((line.size() > 0) || !(feof(pFile)))
- {
- if (line[0] != ';')
- {
- splitted = split_line(line);
- if (splitted.size() > 1)
- {
- std::string _param = to_upper(splitted[0]);
- if (_param == "CONNECTIONTYPE")
- {
- network_config.connection_type = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connection Type: " + network_config.connection_type);
- }
- else if (_param == "SSID")
- {
- network_config.ssid = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SSID: " + network_config.ssid);
- }
- else if (_param == "EAPID")
- {
- network_config.eapid = decrypt_pw_string(splitted[1]);
- if (network_config.eapid.empty())
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: eapid_not_set");
- }
- #ifndef __HIDE_PASSWORD
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: " + network_config.eapid);
- }
- #else
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: eapid_hiden");
- }
- #endif
- }
- else if (_param == "PASSWORD")
- {
- network_config.password = decrypt_pw_string(splitted[1]);
- if (network_config.password.empty())
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: wifi_pw_not_set");
- }
- #ifndef __HIDE_PASSWORD
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: " + network_config.password);
- }
- #else
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: wifi_pw_hiden");
- }
- #endif
- }
- else if (_param == "HOSTNAME")
- {
- network_config.hostname = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Hostname: " + network_config.hostname);
- }
- else if (_param == "IP")
- {
- network_config.ipaddress = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "IP-Address: " + network_config.ipaddress);
- _fix_ipaddress--;
- }
- else if (_param == "GATEWAY")
- {
- network_config.gateway = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Gateway: " + network_config.gateway);
- _fix_ipaddress--;
- }
- else if (_param == "NETMASK")
- {
- network_config.netmask = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Netmask: " + network_config.netmask);
- _fix_ipaddress--;
- }
- else if (_param == "DNS")
- {
- network_config.dns = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "DNS: " + network_config.dns);
- }
- else if (_param == "HTTP_AUTH")
- {
- network_config.http_auth = alphanumeric_to_boolean(splitted[1]);
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_AUTH: " + std::to_string(network_config.http_auth));
- }
- else if (_param == "HTTP_USERNAME")
- {
- network_config.http_username = splitted[1];
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_USERNAME: " + network_config.http_username);
- }
- else if (_param == "HTTP_PASSWORD")
- {
- network_config.http_password = decrypt_pw_string(splitted[1]);
- if (network_config.http_password.empty())
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_Password: http_pw_not_set");
- }
- #ifndef __HIDE_PASSWORD
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: " + network_config.http_password);
- }
- #else
- else
- {
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_Password: http_pw_hiden");
- }
- #endif
- }
- #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
- else if (_param == "RSSITHRESHOLD")
- {
- network_config.rssi_threshold = atoi(splitted[1].c_str());
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "RSSIThreshold: " + std::to_string(network_config.rssi_threshold));
- }
- #endif
- }
- }
- /* read next line */
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- }
- else
- {
- line = std::string(temp_bufer);
- }
- }
- fclose(pFile);
- memcpy(&network_config_temp, &network_config, sizeof(network_config));
- /* Check if SSID is empty (mandatory parameter) */
- #if (CONFIG_ETH_ENABLED && CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500)
- if ((network_config.connection_type.empty()) || ((network_config.connection_type != NETWORK_CONNECTION_WIFI_AP_SETUP) && (network_config.connection_type != NETWORK_CONNECTION_WIFI_AP) &&
- (network_config.connection_type != NETWORK_CONNECTION_WIFI_STA) && (network_config.connection_type != NETWORK_CONNECTION_ETH) && (network_config.connection_type != NETWORK_CONNECTION_DISCONNECT)))
- #else
- if ((network_config.connection_type.empty()) || ((network_config.connection_type != NETWORK_CONNECTION_WIFI_AP_SETUP) && (network_config.connection_type != NETWORK_CONNECTION_WIFI_AP) &&
- (network_config.connection_type != NETWORK_CONNECTION_WIFI_STA) && (network_config.connection_type != NETWORK_CONNECTION_DISCONNECT)))
- #endif
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection Type empty. It set to WiFi AP setup!");
- network_config.connection_type = NETWORK_CONNECTION_WIFI_AP_SETUP;
- }
- /* Check if SSID is empty (mandatory parameter) */
- if (network_config.ssid.empty() && network_config.connection_type == NETWORK_CONNECTION_WIFI_STA)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SSID empty!");
- return -2;
- }
- if (_fix_ipaddress == 0)
- {
- network_config.fix_ipaddress_used = true;
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "fix ipaddress used");
- }
- else
- {
- network_config.fix_ipaddress_used = false;
- }
- return 0;
- }
- bool ChangeHostName(std::string filename, std::string _newhostname)
- {
- if (_newhostname == network_config.hostname)
- {
- return false;
- }
- filename = format_filename(filename);
- FILE *pFile = fopen(filename.c_str(), "r");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: unable to open wlan.ini (read)");
- return ESP_FAIL;
- }
- ESP_LOGD(TAG, "ChangeHostName: wlan.ini opened");
- char temp_bufer[256];
- std::string line = "";
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: wlan.ini opened, but empty or content not readable");
- fclose(pFile);
- return ESP_FAIL;
- }
- else
- {
- line = std::string(temp_bufer);
- }
- bool found = false;
- std::vector<string> splitted;
- std::vector<string> new_file;
- while ((line.size() > 0) || !(feof(pFile)))
- {
- splitted = split_line(line);
- if (splitted.size() > 1)
- {
- std::string _param = to_upper(splitted[0]);
- if (to_upper(splitted[0]) == "HOSTNAME" || to_upper(splitted[0]) == ";HOSTNAME")
- {
- line = "hostname = \"" + _newhostname + "\"\n";
- found = true;
- }
- new_file.push_back(line);
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- }
- else
- {
- line = std::string(temp_bufer);
- }
- }
- }
- if (!found)
- {
- line = "\n;++++++++++++++++++++++++++++++++++\n";
- line += "; Hostname: Name of device in network\n";
- line += "; This parameter can be configured via WebUI configuration\n";
- line += "; Default: \"watermeter\", if nothing is configured\n\n";
- line = "hostname = \"" + _newhostname + "\"\n";
- new_file.push_back(line);
- }
- fclose(pFile);
- pFile = fopen(filename.c_str(), "w+");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: unable to open wlan.ini (write)");
- return false;
- }
- for (int i = 0; i < new_file.size(); ++i)
- {
- fputs(new_file[i].c_str(), pFile);
- }
- fclose(pFile);
- ESP_LOGD(TAG, "ChangeHostName done");
- return true;
- }
- #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
- bool ChangeRSSIThreshold(std::string filename, int _newrssithreshold)
- {
- if (network_config.rssi_threshold == _newrssithreshold)
- {
- return false;
- }
- filename = format_filename(filename);
- FILE *pFile = fopen(filename.c_str(), "r");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: unable to open wlan.ini (read)");
- fclose(pFile);
- return ESP_FAIL;
- }
- ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
- char temp_bufer[256];
- std::string line = "";
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: wlan.ini opened, but empty or content not readable");
- fclose(pFile);
- return ESP_FAIL;
- }
- else
- {
- line = std::string(temp_bufer);
- }
- bool found = false;
- std::vector<string> splitted;
- std::vector<string> new_file;
- while ((line.size() > 0) || !(feof(pFile)))
- {
- splitted = split_line(line);
- if (splitted.size() > 1)
- {
- std::string _param = to_upper(splitted[0]);
- if (_param == "RSSITHRESHOLD" || _param == ";RSSITHRESHOLD")
- {
- line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
- found = true;
- }
- new_file.push_back(line);
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- }
- else
- {
- line = std::string(temp_bufer);
- }
- }
- }
- if (!found)
- {
- line = "\n;++++++++++++++++++++++++++++++++++\n";
- line += "; WIFI Roaming:\n";
- line += "; Network assisted roaming protocol is activated by default\n";
- line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
- line += ";\n";
- line += "; Optional feature (usually not neccessary):\n";
- line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
- line += "; Note: This parameter can be configured via WebUI configuration\n";
- line += "; Default: 0 = Disable client requested roaming query\n\n";
- line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
- new_file.push_back(line);
- }
- fclose(pFile);
- pFile = fopen(filename.c_str(), "w+");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: unable to open wlan.ini (write)");
- return false;
- }
- for (int i = 0; i < new_file.size(); ++i)
- {
- fputs(new_file[i].c_str(), pFile);
- }
- fclose(pFile);
- ESP_LOGD(TAG, "ChangeRSSIThreshold done");
- return true;
- }
- #endif
- esp_err_t GetAuthWebUIConfig(std::string filename)
- {
- filename = format_filename(filename);
- FILE *pFile = fopen(filename.c_str(), "r");
- if (pFile == NULL)
- {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GetAuthWebUIConfig: unable to open wlan.ini (read)");
- return ESP_FAIL;
- }
- ESP_LOGD(TAG, "GetAuthWebUIConfig: wlan.ini opened");
- char temp_bufer[256];
- std::string line = "";
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GetAuthWebUIConfig: wlan.ini opened, but empty or content not readable");
- fclose(pFile);
- return ESP_FAIL;
- }
- else
- {
- line = std::string(temp_bufer);
- }
- std::vector<string> splitted;
- while ((line.size() > 0) || !(feof(pFile)))
- {
- splitted = split_line(line);
- if (splitted.size() > 1)
- {
- std::string _param = to_upper(splitted[0]);
- if ((_param == "HTTP_AUTH") || (_param == ";HTTP_AUTH"))
- {
- network_config.http_auth = alphanumeric_to_boolean(splitted[1]);
- }
- else if ((_param == "HTTP_USERNAME") || (_param == ";HTTP_USERNAME"))
- {
- network_config.http_username = splitted[1];
- }
- else if ((_param == "HTTP_PASSWORD") || (_param == ";HTTP_PASSWORD"))
- {
- network_config.http_password = decrypt_pw_string(splitted[1]);
- }
- }
- /* read next line */
- if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
- {
- line = "";
- }
- else
- {
- line = std::string(temp_bufer);
- }
- }
- fclose(pFile);
- return ESP_OK;
- }
|