read_wlanini.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. static const char *TAG = "read_wlanini";
  12. std::vector<string> ZerlegeZeileWLAN(std::string input, std::string _delimiter = "")
  13. {
  14. std::vector<string> Output;
  15. std::string delimiter = " =,";
  16. if (_delimiter.length() > 0){
  17. delimiter = _delimiter;
  18. }
  19. input = trim(input, delimiter);
  20. size_t pos = findDelimiterPos(input, delimiter);
  21. std::string token;
  22. if (pos != std::string::npos) // Zerlegt nur bis ersten Gleichheitszeichen !!! Sonderfall für WLAN.ini
  23. {
  24. token = input.substr(0, pos);
  25. token = trim(token, delimiter);
  26. Output.push_back(token);
  27. input.erase(0, pos + 1);
  28. input = trim(input, delimiter);
  29. }
  30. Output.push_back(input);
  31. return Output;
  32. }
  33. void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns)
  34. {
  35. std::string ssid = "";
  36. std::string passphrase = "";
  37. std::string ipaddress = "";
  38. std::string gw = "";
  39. std::string netmask = "";
  40. std::string dns = "";
  41. std::string line = "";
  42. std::vector<string> zerlegt;
  43. hostname = std_hostname;
  44. FILE* pFile;
  45. fn = FormatFileName(fn);
  46. pFile = OpenFileAndWait(fn.c_str(), "r");
  47. ESP_LOGD(TAG, "file loaded");
  48. if (pFile == NULL)
  49. return;
  50. char zw[1024];
  51. fgets(zw, 1024, pFile);
  52. line = std::string(zw);
  53. while ((line.size() > 0) || !(feof(pFile)))
  54. {
  55. // ESP_LOGD(TAG, "%s", line.c_str());
  56. zerlegt = ZerlegeZeileWLAN(line, "=");
  57. zerlegt[0] = trim(zerlegt[0], " ");
  58. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  59. hostname = trim(zerlegt[1]);
  60. if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
  61. hostname = hostname.substr(1, hostname.length()-2);
  62. }
  63. }
  64. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  65. ssid = trim(zerlegt[1]);
  66. if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
  67. ssid = ssid.substr(1, ssid.length()-2);
  68. }
  69. }
  70. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  71. passphrase = zerlegt[1];
  72. if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
  73. passphrase = passphrase.substr(1, passphrase.length()-2);
  74. }
  75. }
  76. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  77. ipaddress = zerlegt[1];
  78. if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
  79. ipaddress = ipaddress.substr(1, ipaddress.length()-2);
  80. }
  81. }
  82. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  83. gw = zerlegt[1];
  84. if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
  85. gw = gw.substr(1, gw.length()-2);
  86. }
  87. }
  88. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  89. netmask = zerlegt[1];
  90. if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
  91. netmask = netmask.substr(1, netmask.length()-2);
  92. }
  93. }
  94. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  95. dns = zerlegt[1];
  96. if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
  97. dns = dns.substr(1, dns.length()-2);
  98. }
  99. }
  100. if (fgets(zw, 1024, pFile) == NULL)
  101. {
  102. line = "";
  103. }
  104. else
  105. {
  106. line = std::string(zw);
  107. }
  108. }
  109. fclose(pFile);
  110. // Check if Hostname was empty in .ini if yes set to std_hostname
  111. if(hostname.length() == 0){
  112. hostname = std_hostname;
  113. }
  114. _hostname = new char[hostname.length() + 1];
  115. strcpy(_hostname, hostname.c_str());
  116. _ssid = new char[ssid.length() + 1];
  117. strcpy(_ssid, ssid.c_str());
  118. _password = new char[passphrase.length() + 1];
  119. strcpy(_password, passphrase.c_str());
  120. if (ipaddress.length() > 0)
  121. {
  122. _ipadr = new char[ipaddress.length() + 1];
  123. strcpy(_ipadr, ipaddress.c_str());
  124. }
  125. else
  126. _ipadr = NULL;
  127. if (gw.length() > 0)
  128. {
  129. _gw = new char[gw.length() + 1];
  130. strcpy(_gw, gw.c_str());
  131. }
  132. else
  133. _gw = NULL;
  134. if (netmask.length() > 0)
  135. {
  136. _netmask = new char[netmask.length() + 1];
  137. strcpy(_netmask, netmask.c_str());
  138. }
  139. else
  140. _netmask = NULL;
  141. if (dns.length() > 0)
  142. {
  143. _dns = new char[dns.length() + 1];
  144. strcpy(_dns, dns.c_str());
  145. }
  146. else
  147. _dns = NULL;
  148. }
  149. bool ChangeHostName(std::string fn, std::string _newhostname)
  150. {
  151. if (_newhostname == hostname)
  152. return false;
  153. string line = "";
  154. std::vector<string> zerlegt;
  155. bool found = false;
  156. std::vector<string> neuesfile;
  157. FILE* pFile;
  158. fn = FormatFileName(fn);
  159. pFile = OpenFileAndWait(fn.c_str(), "r");
  160. ESP_LOGD(TAG, "file loaded\n");
  161. if (pFile == NULL)
  162. return false;
  163. char zw[1024];
  164. fgets(zw, 1024, pFile);
  165. line = std::string(zw);
  166. while ((line.size() > 0) || !(feof(pFile)))
  167. {
  168. ESP_LOGD(TAG, "%s", line.c_str());
  169. zerlegt = ZerlegeZeileWLAN(line, "=");
  170. zerlegt[0] = trim(zerlegt[0], " ");
  171. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  172. line = "hostname = \"" + _newhostname + "\"\n";
  173. found = true;
  174. }
  175. neuesfile.push_back(line);
  176. if (fgets(zw, 1024, pFile) == NULL)
  177. {
  178. line = "";
  179. }
  180. else
  181. {
  182. line = std::string(zw);
  183. }
  184. }
  185. if (!found)
  186. {
  187. line = "\nhostname = \"" + _newhostname + "\"\n";
  188. neuesfile.push_back(line);
  189. }
  190. fclose(pFile);
  191. pFile = OpenFileAndWait(fn.c_str(), "w+");
  192. for (int i = 0; i < neuesfile.size(); ++i)
  193. {
  194. ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  195. fputs(neuesfile[i].c_str(), pFile);
  196. }
  197. fclose(pFile);
  198. ESP_LOGD(TAG, "*** Hostname update done ***");
  199. return true;
  200. }