read_wlanini.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include "read_wlanini.h"
  2. #include "Helper.h"
  3. #include "connect_wlan.h"
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8. #include <iostream>
  9. #include <string.h>
  10. #include "esp_log.h"
  11. #include "ClassLogFile.h"
  12. #include "../../include/defines.h"
  13. static const char *TAG = "WLANINI";
  14. struct wlan_config wlan_config = {};
  15. std::vector<string> ZerlegeZeileWLAN(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 WLAN.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 LoadWlanFromFile(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, "LoadWlanFromFile: wlan.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 = ZerlegeZeileWLAN(line, "=");
  63. splitted[0] = trim(splitted[0], " ");
  64. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "SSID")){
  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.ssid = tmp;
  70. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SSID: " + wlan_config.ssid);
  71. }
  72. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "PASSWORD")){
  73. tmp = splitted[1];
  74. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  75. tmp = tmp.substr(1, tmp.length()-2);
  76. }
  77. wlan_config.password = tmp;
  78. #ifndef __HIDE_PASSWORD
  79. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: " + wlan_config.password);
  80. #else
  81. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Password: XXXXXXXX");
  82. #endif
  83. }
  84. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
  85. tmp = trim(splitted[1]);
  86. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  87. tmp = tmp.substr(1, tmp.length()-2);
  88. }
  89. wlan_config.hostname = tmp;
  90. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Hostname: " + wlan_config.hostname);
  91. }
  92. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "IP")){
  93. tmp = splitted[1];
  94. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  95. tmp = tmp.substr(1, tmp.length()-2);
  96. }
  97. wlan_config.ipaddress = tmp;
  98. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "IP-Address: " + wlan_config.ipaddress);
  99. }
  100. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "GATEWAY")){
  101. tmp = splitted[1];
  102. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  103. tmp = tmp.substr(1, tmp.length()-2);
  104. }
  105. wlan_config.gateway = tmp;
  106. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Gateway: " + wlan_config.gateway);
  107. }
  108. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "NETMASK")){
  109. tmp = splitted[1];
  110. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  111. tmp = tmp.substr(1, tmp.length()-2);
  112. }
  113. wlan_config.netmask = tmp;
  114. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Netmask: " + wlan_config.netmask);
  115. }
  116. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "DNS")){
  117. tmp = splitted[1];
  118. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  119. tmp = tmp.substr(1, tmp.length()-2);
  120. }
  121. wlan_config.dns = tmp;
  122. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "DNS: " + wlan_config.dns);
  123. }
  124. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HTTP_USERNAME")){
  125. tmp = splitted[1];
  126. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  127. tmp = tmp.substr(1, tmp.length()-2);
  128. }
  129. wlan_config.http_username = tmp;
  130. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_USERNAME: " + wlan_config.http_username);
  131. }
  132. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HTTP_PASSWORD")){
  133. tmp = splitted[1];
  134. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  135. tmp = tmp.substr(1, tmp.length()-2);
  136. }
  137. wlan_config.http_password = tmp;
  138. #ifndef __HIDE_PASSWORD
  139. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: " + wlan_config.http_password);
  140. #else
  141. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "HTTP_PASSWORD: XXXXXXXX");
  142. #endif
  143. }
  144. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  145. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHRESHOLD")){
  146. tmp = trim(splitted[1]);
  147. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  148. tmp = tmp.substr(1, tmp.length()-2);
  149. }
  150. wlan_config.rssi_threshold = atoi(tmp.c_str());
  151. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "RSSIThreshold: " + std::to_string(wlan_config.rssi_threshold));
  152. }
  153. #endif
  154. }
  155. /* read next line */
  156. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  157. line = "";
  158. }
  159. else {
  160. line = std::string(zw);
  161. }
  162. }
  163. fclose(pFile);
  164. /* Check if SSID is empty (mandatory parameter) */
  165. if (wlan_config.ssid.empty()) {
  166. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SSID empty. Device init aborted!");
  167. return -2;
  168. }
  169. /* Check if password is empty (mandatory parameter) */
  170. /* Disabled see issue #2393
  171. if (wlan_config.password.empty()) {
  172. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Password empty. Device init aborted!");
  173. return -2;
  174. }
  175. */
  176. return 0;
  177. }
  178. bool ChangeHostName(std::string fn, std::string _newhostname)
  179. {
  180. if (_newhostname == wlan_config.hostname)
  181. return false;
  182. std::string line = "";
  183. std::vector<string> splitted;
  184. std::vector<string> neuesfile;
  185. bool found = false;
  186. FILE* pFile = NULL;
  187. fn = FormatFileName(fn);
  188. pFile = fopen(fn.c_str(), "r");
  189. if (pFile == NULL) {
  190. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (read)");
  191. return false;
  192. }
  193. ESP_LOGD(TAG, "ChangeHostName: wlan.ini opened");
  194. char zw[256];
  195. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  196. line = "";
  197. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: File opened, but empty or content not readable");
  198. return false;
  199. }
  200. else {
  201. line = std::string(zw);
  202. }
  203. while ((line.size() > 0) || !(feof(pFile)))
  204. {
  205. //ESP_LOGD(TAG, "ChangeHostName: line: %s", line.c_str());
  206. splitted = ZerlegeZeileWLAN(line, "=");
  207. splitted[0] = trim(splitted[0], " ");
  208. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "HOSTNAME") || (toUpper(splitted[0]) == ";HOSTNAME"))){
  209. line = "hostname = \"" + _newhostname + "\"\n";
  210. found = true;
  211. }
  212. neuesfile.push_back(line);
  213. if (fgets(zw, sizeof(zw), pFile) == NULL)
  214. {
  215. line = "";
  216. }
  217. else
  218. {
  219. line = std::string(zw);
  220. }
  221. }
  222. if (!found)
  223. {
  224. line = "\n;++++++++++++++++++++++++++++++++++\n";
  225. line += "; Hostname: Name of device in network\n";
  226. line += "; This parameter can be configured via WebUI configuration\n";
  227. line += "; Default: \"watermeter\", if nothing is configured\n\n";
  228. line = "hostname = \"" + _newhostname + "\"\n";
  229. neuesfile.push_back(line);
  230. }
  231. fclose(pFile);
  232. pFile = fopen(fn.c_str(), "w+");
  233. if (pFile == NULL) {
  234. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (write)");
  235. return false;
  236. }
  237. for (int i = 0; i < neuesfile.size(); ++i)
  238. {
  239. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  240. fputs(neuesfile[i].c_str(), pFile);
  241. }
  242. fclose(pFile);
  243. ESP_LOGD(TAG, "ChangeHostName done");
  244. return true;
  245. }
  246. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  247. bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
  248. {
  249. if (wlan_config.rssi_threshold == _newrssithreshold)
  250. return false;
  251. std::string line = "";
  252. std::vector<string> splitted;
  253. std::vector<string> neuesfile;
  254. bool found = false;
  255. FILE* pFile = NULL;
  256. fn = FormatFileName(fn);
  257. pFile = fopen(fn.c_str(), "r");
  258. if (pFile == NULL) {
  259. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (read)");
  260. return false;
  261. }
  262. ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
  263. char zw[256];
  264. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  265. line = "";
  266. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: File opened, but empty or content not readable");
  267. return false;
  268. }
  269. else {
  270. line = std::string(zw);
  271. }
  272. while ((line.size() > 0) || !(feof(pFile)))
  273. {
  274. ESP_LOGD(TAG, "%s", line.c_str());
  275. splitted = ZerlegeZeileWLAN(line, "=");
  276. splitted[0] = trim(splitted[0], " ");
  277. /* Workaround to eliminate line with typo "RSSIThreashold" or "rssi" if existing */
  278. if (((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")) ||
  279. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSITHREASHOLD")) ||
  280. ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSI")) ||
  281. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSI"))) {
  282. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  283. line = "";
  284. }
  285. else {
  286. line = std::string(zw);
  287. }
  288. continue;
  289. }
  290. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "RSSITHRESHOLD") || (toUpper(splitted[0]) == ";RSSITHRESHOLD"))) {
  291. line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  292. found = true;
  293. }
  294. neuesfile.push_back(line);
  295. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  296. line = "";
  297. }
  298. else {
  299. line = std::string(zw);
  300. }
  301. }
  302. if (!found)
  303. {
  304. line = "\n;++++++++++++++++++++++++++++++++++\n";
  305. line += "; WIFI Roaming:\n";
  306. line += "; Network assisted roaming protocol is activated by default\n";
  307. line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
  308. line += ";\n";
  309. line += "; Optional feature (usually not neccessary):\n";
  310. line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
  311. line += "; Note: This parameter can be configured via WebUI configuration\n";
  312. line += "; Default: 0 = Disable client requested roaming query\n\n";
  313. line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  314. neuesfile.push_back(line);
  315. }
  316. fclose(pFile);
  317. pFile = fopen(fn.c_str(), "w+");
  318. if (pFile == NULL) {
  319. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (write)");
  320. return false;
  321. }
  322. for (int i = 0; i < neuesfile.size(); ++i)
  323. {
  324. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  325. fputs(neuesfile[i].c_str(), pFile);
  326. }
  327. fclose(pFile);
  328. ESP_LOGD(TAG, "ChangeRSSIThreshold done");
  329. return true;
  330. }
  331. #endif