read_wlanini.cpp 6.2 KB

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