read_lanini.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #if defined(BOARD_ESP32_S3_ALEKSEI)
  2. #include "read_lanini.h"
  3. #include "Helper.h"
  4. #include "connect_lan.h"
  5. #include <fstream>
  6. #include <string>
  7. #include <vector>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <string.h>
  11. #include "esp_log.h"
  12. #include "ClassLogFile.h"
  13. #include "../../include/defines.h"
  14. static const char *TAG = "LANINI";
  15. std::vector<string> ZerlegeZeileLAN(std::string input, std::string _delimiter = "")
  16. {
  17. std::vector<string> Output;
  18. std::string delimiter = " =,";
  19. if (_delimiter.length() > 0){
  20. delimiter = _delimiter;
  21. }
  22. input = trim(input, delimiter);
  23. size_t pos = findDelimiterPos(input, delimiter);
  24. std::string token;
  25. if (pos != std::string::npos) // splitted only up to first equal sign !!! Special case for LAN.ini
  26. {
  27. token = input.substr(0, pos);
  28. token = trim(token, delimiter);
  29. Output.push_back(token);
  30. input.erase(0, pos + 1);
  31. input = trim(input, delimiter);
  32. }
  33. Output.push_back(input);
  34. return Output;
  35. }
  36. int LoadLanFromFile(std::string fn)
  37. {
  38. std::string line = "";
  39. std::string tmp = "";
  40. std::vector<string> splitted;
  41. fn = FormatFileName(fn);
  42. FILE* pFile = fopen(fn.c_str(), "r");
  43. if (pFile == NULL) {
  44. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to open file (read). Device init aborted!");
  45. return -1;
  46. }
  47. ESP_LOGD(TAG, "LoadLanFromFile: lan.ini opened");
  48. char zw[256];
  49. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  50. line = "";
  51. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "file opened, but empty or content not readable. Device init aborted!");
  52. fclose(pFile);
  53. return -1;
  54. }
  55. else {
  56. line = std::string(zw);
  57. }
  58. while ((line.size() > 0) || !(feof(pFile)))
  59. {
  60. //ESP_LOGD(TAG, "line: %s", line.c_str());
  61. if (line[0] != ';') { // Skip lines which starts with ';'
  62. splitted = ZerlegeZeileLAN(line, "=");
  63. splitted[0] = trim(splitted[0], " ");
  64. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
  65. tmp = trim(splitted[1]);
  66. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  67. tmp = tmp.substr(1, tmp.length()-2);
  68. }
  69. wlan_config.hostname = tmp;
  70. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Hostname: " + wlan_config.hostname);
  71. }
  72. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "IP")){
  73. tmp = splitted[1];
  74. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  75. tmp = tmp.substr(1, tmp.length()-2);
  76. }
  77. wlan_config.ipaddress = tmp;
  78. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "IP-Address: " + wlan_config.ipaddress);
  79. }
  80. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "GATEWAY")){
  81. tmp = splitted[1];
  82. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  83. tmp = tmp.substr(1, tmp.length()-2);
  84. }
  85. wlan_config.gateway = tmp;
  86. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Gateway: " + wlan_config.gateway);
  87. }
  88. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "NETMASK")){
  89. tmp = splitted[1];
  90. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  91. tmp = tmp.substr(1, tmp.length()-2);
  92. }
  93. wlan_config.netmask = tmp;
  94. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Netmask: " + wlan_config.netmask);
  95. }
  96. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "DNS")){
  97. tmp = splitted[1];
  98. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  99. tmp = tmp.substr(1, tmp.length()-2);
  100. }
  101. wlan_config.dns = tmp;
  102. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "DNS: " + wlan_config.dns);
  103. }
  104. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HTTP_USERNAME")){
  105. tmp = splitted[1];
  106. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  107. tmp = tmp.substr(1, tmp.length()-2);
  108. }
  109. wlan_config.http_username = tmp;
  110. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_USERNAME: " + wlan_config.http_username);
  111. }
  112. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HTTP_PASSWORD")){
  113. tmp = splitted[1];
  114. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  115. tmp = tmp.substr(1, tmp.length()-2);
  116. }
  117. wlan_config.http_password = tmp;
  118. #ifndef __HIDE_PASSWORD
  119. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: " + wlan_config.http_password);
  120. #else
  121. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: XXXXXXXX");
  122. #endif
  123. }
  124. }
  125. /* read next line */
  126. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  127. line = "";
  128. }
  129. else {
  130. line = std::string(zw);
  131. }
  132. }
  133. fclose(pFile);
  134. return 0;
  135. }
  136. bool ChangeLanHostName(std::string fn, std::string _newhostname)
  137. {
  138. if (_newhostname == wlan_config.hostname)
  139. return false;
  140. std::string line = "";
  141. std::vector<string> splitted;
  142. std::vector<string> neuesfile;
  143. bool found = false;
  144. FILE* pFile = NULL;
  145. fn = FormatFileName(fn);
  146. pFile = fopen(fn.c_str(), "r");
  147. if (pFile == NULL) {
  148. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file lan.ini (read)");
  149. return false;
  150. }
  151. ESP_LOGD(TAG, "ChangeHostName: lan.ini opened");
  152. char zw[256];
  153. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  154. line = "";
  155. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: File opened, but empty or content not readable");
  156. return false;
  157. }
  158. else {
  159. line = std::string(zw);
  160. }
  161. while ((line.size() > 0) || !(feof(pFile)))
  162. {
  163. //ESP_LOGD(TAG, "ChangeHostName: line: %s", line.c_str());
  164. splitted = ZerlegeZeileLAN(line, "=");
  165. splitted[0] = trim(splitted[0], " ");
  166. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "HOSTNAME") || (toUpper(splitted[0]) == ";HOSTNAME"))){
  167. line = "hostname = \"" + _newhostname + "\"\n";
  168. found = true;
  169. }
  170. neuesfile.push_back(line);
  171. if (fgets(zw, sizeof(zw), pFile) == NULL)
  172. {
  173. line = "";
  174. }
  175. else
  176. {
  177. line = std::string(zw);
  178. }
  179. }
  180. if (!found)
  181. {
  182. line = "\n;++++++++++++++++++++++++++++++++++\n";
  183. line += "; Hostname: Name of device in network\n";
  184. line += "; This parameter can be configured via WebUI configuration\n";
  185. line += "; Default: \"watermeter\", if nothing is configured\n\n";
  186. line = "hostname = \"" + _newhostname + "\"\n";
  187. neuesfile.push_back(line);
  188. }
  189. fclose(pFile);
  190. pFile = fopen(fn.c_str(), "w+");
  191. if (pFile == NULL) {
  192. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (write)");
  193. return false;
  194. }
  195. for (int i = 0; i < neuesfile.size(); ++i)
  196. {
  197. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  198. fputs(neuesfile[i].c_str(), pFile);
  199. }
  200. fclose(pFile);
  201. ESP_LOGD(TAG, "ChangeLanHostName done");
  202. return true;
  203. }
  204. #endif