read_wlanini.cpp 12 KB

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