read_wlanini.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. fclose(pFile);
  199. return false;
  200. }
  201. else {
  202. line = std::string(zw);
  203. }
  204. while ((line.size() > 0) || !(feof(pFile)))
  205. {
  206. //ESP_LOGD(TAG, "ChangeHostName: line: %s", line.c_str());
  207. splitted = ZerlegeZeileWLAN(line, "=");
  208. splitted[0] = trim(splitted[0], " ");
  209. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "HOSTNAME") || (toUpper(splitted[0]) == ";HOSTNAME"))){
  210. line = "hostname = \"" + _newhostname + "\"\n";
  211. found = true;
  212. }
  213. neuesfile.push_back(line);
  214. if (fgets(zw, sizeof(zw), pFile) == NULL)
  215. {
  216. line = "";
  217. }
  218. else
  219. {
  220. line = std::string(zw);
  221. }
  222. }
  223. if (!found)
  224. {
  225. line = "\n;++++++++++++++++++++++++++++++++++\n";
  226. line += "; Hostname: Name of device in network\n";
  227. line += "; This parameter can be configured via WebUI configuration\n";
  228. line += "; Default: \"watermeter\", if nothing is configured\n\n";
  229. line = "hostname = \"" + _newhostname + "\"\n";
  230. neuesfile.push_back(line);
  231. }
  232. fclose(pFile);
  233. pFile = fopen(fn.c_str(), "w+");
  234. if (pFile == NULL) {
  235. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (write)");
  236. return false;
  237. }
  238. for (int i = 0; i < neuesfile.size(); ++i)
  239. {
  240. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  241. fputs(neuesfile[i].c_str(), pFile);
  242. }
  243. fclose(pFile);
  244. ESP_LOGD(TAG, "ChangeHostName done");
  245. return true;
  246. }
  247. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  248. bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
  249. {
  250. if (wlan_config.rssi_threshold == _newrssithreshold)
  251. return false;
  252. std::string line = "";
  253. std::vector<string> splitted;
  254. std::vector<string> neuesfile;
  255. bool found = false;
  256. FILE* pFile = NULL;
  257. fn = FormatFileName(fn);
  258. pFile = fopen(fn.c_str(), "r");
  259. if (pFile == NULL) {
  260. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (read)");
  261. return false;
  262. }
  263. ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
  264. char zw[256];
  265. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  266. line = "";
  267. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: File opened, but empty or content not readable");
  268. fclose(pFile);
  269. return false;
  270. }
  271. else {
  272. line = std::string(zw);
  273. }
  274. while ((line.size() > 0) || !(feof(pFile)))
  275. {
  276. ESP_LOGD(TAG, "%s", line.c_str());
  277. splitted = ZerlegeZeileWLAN(line, "=");
  278. splitted[0] = trim(splitted[0], " ");
  279. /* Workaround to eliminate line with typo "RSSIThreashold" or "rssi" if existing */
  280. if (((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")) ||
  281. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSITHREASHOLD")) ||
  282. ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSI")) ||
  283. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSI"))) {
  284. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  285. line = "";
  286. }
  287. else {
  288. line = std::string(zw);
  289. }
  290. continue;
  291. }
  292. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "RSSITHRESHOLD") || (toUpper(splitted[0]) == ";RSSITHRESHOLD"))) {
  293. line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  294. found = true;
  295. }
  296. neuesfile.push_back(line);
  297. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  298. line = "";
  299. }
  300. else {
  301. line = std::string(zw);
  302. }
  303. }
  304. if (!found)
  305. {
  306. line = "\n;++++++++++++++++++++++++++++++++++\n";
  307. line += "; WIFI Roaming:\n";
  308. line += "; Network assisted roaming protocol is activated by default\n";
  309. line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
  310. line += ";\n";
  311. line += "; Optional feature (usually not neccessary):\n";
  312. line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
  313. line += "; Note: This parameter can be configured via WebUI configuration\n";
  314. line += "; Default: 0 = Disable client requested roaming query\n\n";
  315. line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  316. neuesfile.push_back(line);
  317. }
  318. fclose(pFile);
  319. pFile = fopen(fn.c_str(), "w+");
  320. if (pFile == NULL) {
  321. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (write)");
  322. return false;
  323. }
  324. for (int i = 0; i < neuesfile.size(); ++i)
  325. {
  326. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  327. fputs(neuesfile[i].c_str(), pFile);
  328. }
  329. fclose(pFile);
  330. ESP_LOGD(TAG, "ChangeRSSIThreshold done");
  331. return true;
  332. }
  333. #endif