read_network_config.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "defines.h"
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <string.h>
  8. #include "read_network_config.h"
  9. #include "Helper.h"
  10. #include "connect_wifi_sta.h"
  11. #include "esp_log.h"
  12. #include "ClassLogFile.h"
  13. static const char *TAG = "NETWORK CONFIG";
  14. struct network_config_t network_config = {};
  15. struct network_config_t network_config_temp = {};
  16. int LoadNetworkFromFile(std::string filename)
  17. {
  18. filename = format_filename(filename);
  19. FILE *pFile = fopen(filename.c_str(), "r");
  20. if (pFile == NULL)
  21. {
  22. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "unable to open wlan.ini (read)!");
  23. return -1;
  24. }
  25. ESP_LOGD(TAG, "LoadNetworkFromFile: wlan.ini opened");
  26. std::string line = "";
  27. char temp_bufer[256];
  28. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  29. {
  30. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "wlan.ini opened, but empty or content not readable!");
  31. fclose(pFile);
  32. return -1;
  33. }
  34. else
  35. {
  36. line = std::string(temp_bufer);
  37. }
  38. int _fix_ipaddress = 3;
  39. std::vector<std::string> splitted;
  40. while ((line.size() > 0) || !(feof(pFile)))
  41. {
  42. if (line[0] != ';')
  43. {
  44. splitted = split_line(line);
  45. if (splitted.size() > 1)
  46. {
  47. std::string _param = to_upper(splitted[0]);
  48. if (_param == "CONNECTIONTYPE")
  49. {
  50. network_config.connection_type = splitted[1];
  51. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Connection Type: " + network_config.connection_type);
  52. }
  53. else if (_param == "SSID")
  54. {
  55. network_config.ssid = splitted[1];
  56. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SSID: " + network_config.ssid);
  57. }
  58. else if (_param == "EAPID")
  59. {
  60. network_config.eapid = decrypt_pw_string(splitted[1]);
  61. if (network_config.eapid.empty())
  62. {
  63. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: eapid_not_set");
  64. }
  65. #ifndef __HIDE_PASSWORD
  66. else
  67. {
  68. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: " + network_config.eapid);
  69. }
  70. #else
  71. else
  72. {
  73. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "EPA-ID: eapid_hiden");
  74. }
  75. #endif
  76. }
  77. else if (_param == "PASSWORD")
  78. {
  79. network_config.password = decrypt_pw_string(splitted[1]);
  80. if (network_config.password.empty())
  81. {
  82. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: wifi_pw_not_set");
  83. }
  84. #ifndef __HIDE_PASSWORD
  85. else
  86. {
  87. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: " + network_config.password);
  88. }
  89. #else
  90. else
  91. {
  92. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: wifi_pw_hiden");
  93. }
  94. #endif
  95. }
  96. else if (_param == "HOSTNAME")
  97. {
  98. network_config.hostname = splitted[1];
  99. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Hostname: " + network_config.hostname);
  100. }
  101. else if (_param == "IP")
  102. {
  103. network_config.ipaddress = splitted[1];
  104. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "IP-Address: " + network_config.ipaddress);
  105. _fix_ipaddress--;
  106. }
  107. else if (_param == "GATEWAY")
  108. {
  109. network_config.gateway = splitted[1];
  110. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Gateway: " + network_config.gateway);
  111. _fix_ipaddress--;
  112. }
  113. else if (_param == "NETMASK")
  114. {
  115. network_config.netmask = splitted[1];
  116. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Netmask: " + network_config.netmask);
  117. _fix_ipaddress--;
  118. }
  119. else if (_param == "DNS")
  120. {
  121. network_config.dns = splitted[1];
  122. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "DNS: " + network_config.dns);
  123. }
  124. else if (_param == "HTTP_AUTH")
  125. {
  126. network_config.http_auth = alphanumeric_to_boolean(splitted[1]);
  127. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_AUTH: " + std::to_string(network_config.http_auth));
  128. }
  129. else if (_param == "HTTP_USERNAME")
  130. {
  131. network_config.http_username = splitted[1];
  132. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_USERNAME: " + network_config.http_username);
  133. }
  134. else if (_param == "HTTP_PASSWORD")
  135. {
  136. network_config.http_password = decrypt_pw_string(splitted[1]);
  137. if (network_config.http_password.empty())
  138. {
  139. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_Password: http_pw_not_set");
  140. }
  141. #ifndef __HIDE_PASSWORD
  142. else
  143. {
  144. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: " + network_config.http_password);
  145. }
  146. #else
  147. else
  148. {
  149. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_Password: http_pw_hiden");
  150. }
  151. #endif
  152. }
  153. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  154. else if (_param == "RSSITHRESHOLD")
  155. {
  156. network_config.rssi_threshold = atoi(splitted[1].c_str());
  157. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "RSSIThreshold: " + std::to_string(network_config.rssi_threshold));
  158. }
  159. #endif
  160. }
  161. }
  162. /* read next line */
  163. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  164. {
  165. line = "";
  166. }
  167. else
  168. {
  169. line = std::string(temp_bufer);
  170. }
  171. }
  172. fclose(pFile);
  173. memcpy(&network_config_temp, &network_config, sizeof(network_config));
  174. /* Check if SSID is empty (mandatory parameter) */
  175. #if (CONFIG_ETH_ENABLED && CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500)
  176. if ((network_config.connection_type.empty()) || ((network_config.connection_type != NETWORK_CONNECTION_WIFI_AP_SETUP) && (network_config.connection_type != NETWORK_CONNECTION_WIFI_AP) &&
  177. (network_config.connection_type != NETWORK_CONNECTION_WIFI_STA) && (network_config.connection_type != NETWORK_CONNECTION_ETH) && (network_config.connection_type != NETWORK_CONNECTION_DISCONNECT)))
  178. #else
  179. if ((network_config.connection_type.empty()) || ((network_config.connection_type != NETWORK_CONNECTION_WIFI_AP_SETUP) && (network_config.connection_type != NETWORK_CONNECTION_WIFI_AP) &&
  180. (network_config.connection_type != NETWORK_CONNECTION_WIFI_STA) && (network_config.connection_type != NETWORK_CONNECTION_DISCONNECT)))
  181. #endif
  182. {
  183. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Connection Type empty. It set to WiFi AP setup!");
  184. network_config.connection_type = NETWORK_CONNECTION_WIFI_AP_SETUP;
  185. }
  186. /* Check if SSID is empty (mandatory parameter) */
  187. if (network_config.ssid.empty() && network_config.connection_type == NETWORK_CONNECTION_WIFI_STA)
  188. {
  189. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SSID empty!");
  190. return -2;
  191. }
  192. if (_fix_ipaddress == 0)
  193. {
  194. network_config.fix_ipaddress_used = true;
  195. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "fix ipaddress used");
  196. }
  197. else
  198. {
  199. network_config.fix_ipaddress_used = false;
  200. }
  201. return 0;
  202. }
  203. bool ChangeHostName(std::string filename, std::string _newhostname)
  204. {
  205. if (_newhostname == network_config.hostname)
  206. {
  207. return false;
  208. }
  209. filename = format_filename(filename);
  210. FILE *pFile = fopen(filename.c_str(), "r");
  211. if (pFile == NULL)
  212. {
  213. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: unable to open wlan.ini (read)");
  214. return ESP_FAIL;
  215. }
  216. ESP_LOGD(TAG, "ChangeHostName: wlan.ini opened");
  217. char temp_bufer[256];
  218. std::string line = "";
  219. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  220. {
  221. line = "";
  222. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: wlan.ini opened, but empty or content not readable");
  223. fclose(pFile);
  224. return ESP_FAIL;
  225. }
  226. else
  227. {
  228. line = std::string(temp_bufer);
  229. }
  230. bool found = false;
  231. std::vector<string> splitted;
  232. std::vector<string> new_file;
  233. while ((line.size() > 0) || !(feof(pFile)))
  234. {
  235. splitted = split_line(line);
  236. if (splitted.size() > 1)
  237. {
  238. std::string _param = to_upper(splitted[0]);
  239. if (to_upper(splitted[0]) == "HOSTNAME" || to_upper(splitted[0]) == ";HOSTNAME")
  240. {
  241. line = "hostname = \"" + _newhostname + "\"\n";
  242. found = true;
  243. }
  244. new_file.push_back(line);
  245. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  246. {
  247. line = "";
  248. }
  249. else
  250. {
  251. line = std::string(temp_bufer);
  252. }
  253. }
  254. }
  255. if (!found)
  256. {
  257. line = "\n;++++++++++++++++++++++++++++++++++\n";
  258. line += "; Hostname: Name of device in network\n";
  259. line += "; This parameter can be configured via WebUI configuration\n";
  260. line += "; Default: \"watermeter\", if nothing is configured\n\n";
  261. line = "hostname = \"" + _newhostname + "\"\n";
  262. new_file.push_back(line);
  263. }
  264. fclose(pFile);
  265. pFile = fopen(filename.c_str(), "w+");
  266. if (pFile == NULL)
  267. {
  268. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: unable to open wlan.ini (write)");
  269. return false;
  270. }
  271. for (int i = 0; i < new_file.size(); ++i)
  272. {
  273. fputs(new_file[i].c_str(), pFile);
  274. }
  275. fclose(pFile);
  276. ESP_LOGD(TAG, "ChangeHostName done");
  277. return true;
  278. }
  279. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  280. bool ChangeRSSIThreshold(std::string filename, int _newrssithreshold)
  281. {
  282. if (network_config.rssi_threshold == _newrssithreshold)
  283. {
  284. return false;
  285. }
  286. filename = format_filename(filename);
  287. FILE *pFile = fopen(filename.c_str(), "r");
  288. if (pFile == NULL)
  289. {
  290. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: unable to open wlan.ini (read)");
  291. fclose(pFile);
  292. return ESP_FAIL;
  293. }
  294. ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
  295. char temp_bufer[256];
  296. std::string line = "";
  297. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  298. {
  299. line = "";
  300. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: wlan.ini opened, but empty or content not readable");
  301. fclose(pFile);
  302. return ESP_FAIL;
  303. }
  304. else
  305. {
  306. line = std::string(temp_bufer);
  307. }
  308. bool found = false;
  309. std::vector<string> splitted;
  310. std::vector<string> new_file;
  311. while ((line.size() > 0) || !(feof(pFile)))
  312. {
  313. splitted = split_line(line);
  314. if (splitted.size() > 1)
  315. {
  316. std::string _param = to_upper(splitted[0]);
  317. if (_param == "RSSITHRESHOLD" || _param == ";RSSITHRESHOLD")
  318. {
  319. line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  320. found = true;
  321. }
  322. new_file.push_back(line);
  323. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  324. {
  325. line = "";
  326. }
  327. else
  328. {
  329. line = std::string(temp_bufer);
  330. }
  331. }
  332. }
  333. if (!found)
  334. {
  335. line = "\n;++++++++++++++++++++++++++++++++++\n";
  336. line += "; WIFI Roaming:\n";
  337. line += "; Network assisted roaming protocol is activated by default\n";
  338. line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
  339. line += ";\n";
  340. line += "; Optional feature (usually not neccessary):\n";
  341. line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
  342. line += "; Note: This parameter can be configured via WebUI configuration\n";
  343. line += "; Default: 0 = Disable client requested roaming query\n\n";
  344. line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  345. new_file.push_back(line);
  346. }
  347. fclose(pFile);
  348. pFile = fopen(filename.c_str(), "w+");
  349. if (pFile == NULL)
  350. {
  351. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: unable to open wlan.ini (write)");
  352. return false;
  353. }
  354. for (int i = 0; i < new_file.size(); ++i)
  355. {
  356. fputs(new_file[i].c_str(), pFile);
  357. }
  358. fclose(pFile);
  359. ESP_LOGD(TAG, "ChangeRSSIThreshold done");
  360. return true;
  361. }
  362. #endif
  363. esp_err_t GetAuthWebUIConfig(std::string filename)
  364. {
  365. filename = format_filename(filename);
  366. FILE *pFile = fopen(filename.c_str(), "r");
  367. if (pFile == NULL)
  368. {
  369. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GetAuthWebUIConfig: unable to open wlan.ini (read)");
  370. return ESP_FAIL;
  371. }
  372. ESP_LOGD(TAG, "GetAuthWebUIConfig: wlan.ini opened");
  373. char temp_bufer[256];
  374. std::string line = "";
  375. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  376. {
  377. line = "";
  378. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "GetAuthWebUIConfig: wlan.ini opened, but empty or content not readable");
  379. fclose(pFile);
  380. return ESP_FAIL;
  381. }
  382. else
  383. {
  384. line = std::string(temp_bufer);
  385. }
  386. std::vector<string> splitted;
  387. while ((line.size() > 0) || !(feof(pFile)))
  388. {
  389. splitted = split_line(line);
  390. if (splitted.size() > 1)
  391. {
  392. std::string _param = to_upper(splitted[0]);
  393. if ((_param == "HTTP_AUTH") || (_param == ";HTTP_AUTH"))
  394. {
  395. network_config.http_auth = alphanumeric_to_boolean(splitted[1]);
  396. }
  397. else if ((_param == "HTTP_USERNAME") || (_param == ";HTTP_USERNAME"))
  398. {
  399. network_config.http_username = splitted[1];
  400. }
  401. else if ((_param == "HTTP_PASSWORD") || (_param == ";HTTP_PASSWORD"))
  402. {
  403. network_config.http_password = decrypt_pw_string(splitted[1]);
  404. }
  405. }
  406. /* read next line */
  407. if (fgets(temp_bufer, sizeof(temp_bufer), pFile) == NULL)
  408. {
  409. line = "";
  410. }
  411. else
  412. {
  413. line = std::string(temp_bufer);
  414. }
  415. }
  416. fclose(pFile);
  417. return ESP_OK;
  418. }