read_wlanini.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  125. else if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHRESHOLD")){
  126. tmp = trim(splitted[1]);
  127. if ((tmp[0] == '"') && (tmp[tmp.length()-1] == '"')){
  128. tmp = tmp.substr(1, tmp.length()-2);
  129. }
  130. wlan_config.rssi_threshold = atoi(tmp.c_str());
  131. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "RSSIThreshold: " + std::to_string(wlan_config.rssi_threshold));
  132. }
  133. #endif
  134. }
  135. /* read next line */
  136. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  137. line = "";
  138. }
  139. else {
  140. line = std::string(zw);
  141. }
  142. }
  143. fclose(pFile);
  144. /* Check if SSID is empty (mandatory parameter) */
  145. if (wlan_config.ssid.empty()) {
  146. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SSID empty. Device init aborted!");
  147. return -2;
  148. }
  149. /* Check if password is empty (mandatory parameter) */
  150. /* Disabled see issue #2393
  151. if (wlan_config.password.empty()) {
  152. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Password empty. Device init aborted!");
  153. return -2;
  154. }
  155. */
  156. return 0;
  157. }
  158. bool ChangeHostName(std::string fn, std::string _newhostname)
  159. {
  160. if (_newhostname == wlan_config.hostname)
  161. return false;
  162. std::string line = "";
  163. std::vector<string> splitted;
  164. std::vector<string> neuesfile;
  165. bool found = false;
  166. FILE* pFile = NULL;
  167. fn = FormatFileName(fn);
  168. pFile = fopen(fn.c_str(), "r");
  169. if (pFile == NULL) {
  170. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (read)");
  171. return false;
  172. }
  173. ESP_LOGD(TAG, "ChangeHostName: wlan.ini opened");
  174. char zw[256];
  175. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  176. line = "";
  177. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: File opened, but empty or content not readable");
  178. return false;
  179. }
  180. else {
  181. line = std::string(zw);
  182. }
  183. while ((line.size() > 0) || !(feof(pFile)))
  184. {
  185. //ESP_LOGD(TAG, "ChangeHostName: line: %s", line.c_str());
  186. splitted = ZerlegeZeileWLAN(line, "=");
  187. splitted[0] = trim(splitted[0], " ");
  188. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "HOSTNAME") || (toUpper(splitted[0]) == ";HOSTNAME"))){
  189. line = "hostname = \"" + _newhostname + "\"\n";
  190. found = true;
  191. }
  192. neuesfile.push_back(line);
  193. if (fgets(zw, sizeof(zw), pFile) == NULL)
  194. {
  195. line = "";
  196. }
  197. else
  198. {
  199. line = std::string(zw);
  200. }
  201. }
  202. if (!found)
  203. {
  204. line = "\n;++++++++++++++++++++++++++++++++++\n";
  205. line += "; Hostname: Name of device in network\n";
  206. line += "; This parameter can be configured via WebUI configuration\n";
  207. line += "; Default: \"watermeter\", if nothing is configured\n\n";
  208. line = "hostname = \"" + _newhostname + "\"\n";
  209. neuesfile.push_back(line);
  210. }
  211. fclose(pFile);
  212. pFile = fopen(fn.c_str(), "w+");
  213. if (pFile == NULL) {
  214. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeHostName: Unable to open file wlan.ini (write)");
  215. return false;
  216. }
  217. for (int i = 0; i < neuesfile.size(); ++i)
  218. {
  219. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  220. fputs(neuesfile[i].c_str(), pFile);
  221. }
  222. fclose(pFile);
  223. ESP_LOGD(TAG, "ChangeHostName done");
  224. return true;
  225. }
  226. #if (defined WLAN_USE_ROAMING_BY_SCANNING || (defined WLAN_USE_MESH_ROAMING && defined WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES))
  227. bool ChangeRSSIThreshold(std::string fn, int _newrssithreshold)
  228. {
  229. if (wlan_config.rssi_threshold == _newrssithreshold)
  230. return false;
  231. std::string line = "";
  232. std::vector<string> splitted;
  233. std::vector<string> neuesfile;
  234. bool found = false;
  235. FILE* pFile = NULL;
  236. fn = FormatFileName(fn);
  237. pFile = fopen(fn.c_str(), "r");
  238. if (pFile == NULL) {
  239. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (read)");
  240. return false;
  241. }
  242. ESP_LOGD(TAG, "ChangeRSSIThreshold: wlan.ini opened");
  243. char zw[256];
  244. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  245. line = "";
  246. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: File opened, but empty or content not readable");
  247. return false;
  248. }
  249. else {
  250. line = std::string(zw);
  251. }
  252. while ((line.size() > 0) || !(feof(pFile)))
  253. {
  254. ESP_LOGD(TAG, "%s", line.c_str());
  255. splitted = ZerlegeZeileWLAN(line, "=");
  256. splitted[0] = trim(splitted[0], " ");
  257. /* Workaround to eliminate line with typo "RSSIThreashold" or "rssi" if existing */
  258. if (((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")) ||
  259. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSITHREASHOLD")) ||
  260. ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSI")) ||
  261. ((splitted.size() > 1) && (toUpper(splitted[0]) == ";RSSI"))) {
  262. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  263. line = "";
  264. }
  265. else {
  266. line = std::string(zw);
  267. }
  268. continue;
  269. }
  270. if ((splitted.size() > 1) && ((toUpper(splitted[0]) == "RSSITHRESHOLD") || (toUpper(splitted[0]) == ";RSSITHRESHOLD"))) {
  271. line = "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  272. found = true;
  273. }
  274. neuesfile.push_back(line);
  275. if (fgets(zw, sizeof(zw), pFile) == NULL) {
  276. line = "";
  277. }
  278. else {
  279. line = std::string(zw);
  280. }
  281. }
  282. if (!found)
  283. {
  284. line = "\n;++++++++++++++++++++++++++++++++++\n";
  285. line += "; WIFI Roaming:\n";
  286. line += "; Network assisted roaming protocol is activated by default\n";
  287. line += "; AP / mesh system needs to support roaming protocol 802.11k/v\n";
  288. line += ";\n";
  289. line += "; Optional feature (usually not neccessary):\n";
  290. line += "; RSSI Threshold for client requested roaming query (RSSI < RSSIThreshold)\n";
  291. line += "; Note: This parameter can be configured via WebUI configuration\n";
  292. line += "; Default: 0 = Disable client requested roaming query\n\n";
  293. line += "RSSIThreshold = " + to_string(_newrssithreshold) + "\n";
  294. neuesfile.push_back(line);
  295. }
  296. fclose(pFile);
  297. pFile = fopen(fn.c_str(), "w+");
  298. if (pFile == NULL) {
  299. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ChangeRSSIThreshold: Unable to open file wlan.ini (write)");
  300. return false;
  301. }
  302. for (int i = 0; i < neuesfile.size(); ++i)
  303. {
  304. //ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  305. fputs(neuesfile[i].c_str(), pFile);
  306. }
  307. fclose(pFile);
  308. ESP_LOGD(TAG, "ChangeRSSIThreshold done");
  309. return true;
  310. }
  311. #endif