read_wlanini.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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) // splitted only up to first equal sign !!! Special case for 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, int &_rssithreashold)
  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. int rssithreshold = 0;
  43. std::string line = "";
  44. std::vector<string> splitted;
  45. hostname = std_hostname;
  46. FILE* pFile;
  47. fn = FormatFileName(fn);
  48. pFile = fopen(fn.c_str(), "r");
  49. ESP_LOGD(TAG, "file loaded");
  50. if (pFile == NULL)
  51. return;
  52. char zw[1024];
  53. fgets(zw, 1024, pFile);
  54. line = std::string(zw);
  55. while ((line.size() > 0) || !(feof(pFile)))
  56. {
  57. // ESP_LOGD(TAG, "%s", line.c_str());
  58. splitted = ZerlegeZeileWLAN(line, "=");
  59. splitted[0] = trim(splitted[0], " ");
  60. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
  61. hostname = trim(splitted[1]);
  62. if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
  63. hostname = hostname.substr(1, hostname.length()-2);
  64. }
  65. }
  66. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "SSID")){
  67. ssid = trim(splitted[1]);
  68. if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
  69. ssid = ssid.substr(1, ssid.length()-2);
  70. }
  71. }
  72. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")){
  73. string _s = trim(splitted[1]);
  74. if ((_s[0] == '"') && (_s[_s.length()-1] == '"')){
  75. _s = _s.substr(1, ssid.length()-2);
  76. }
  77. rssithreshold = atoi(_s.c_str());
  78. }
  79. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "PASSWORD")){
  80. passphrase = splitted[1];
  81. if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
  82. passphrase = passphrase.substr(1, passphrase.length()-2);
  83. }
  84. }
  85. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "IP")){
  86. ipaddress = splitted[1];
  87. if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
  88. ipaddress = ipaddress.substr(1, ipaddress.length()-2);
  89. }
  90. }
  91. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "GATEWAY")){
  92. gw = splitted[1];
  93. if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
  94. gw = gw.substr(1, gw.length()-2);
  95. }
  96. }
  97. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "NETMASK")){
  98. netmask = splitted[1];
  99. if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
  100. netmask = netmask.substr(1, netmask.length()-2);
  101. }
  102. }
  103. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "DNS")){
  104. dns = splitted[1];
  105. if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
  106. dns = dns.substr(1, dns.length()-2);
  107. }
  108. }
  109. if (fgets(zw, 1024, pFile) == NULL)
  110. {
  111. line = "";
  112. }
  113. else
  114. {
  115. line = std::string(zw);
  116. }
  117. }
  118. fclose(pFile);
  119. // Check if Hostname was empty in .ini if yes set to std_hostname
  120. if(hostname.length() == 0){
  121. hostname = std_hostname;
  122. }
  123. _hostname = new char[hostname.length() + 1];
  124. strcpy(_hostname, hostname.c_str());
  125. _ssid = new char[ssid.length() + 1];
  126. strcpy(_ssid, ssid.c_str());
  127. _password = new char[passphrase.length() + 1];
  128. strcpy(_password, passphrase.c_str());
  129. if (ipaddress.length() > 0)
  130. {
  131. _ipadr = new char[ipaddress.length() + 1];
  132. strcpy(_ipadr, ipaddress.c_str());
  133. }
  134. else
  135. _ipadr = NULL;
  136. if (gw.length() > 0)
  137. {
  138. _gw = new char[gw.length() + 1];
  139. strcpy(_gw, gw.c_str());
  140. }
  141. else
  142. _gw = NULL;
  143. if (netmask.length() > 0)
  144. {
  145. _netmask = new char[netmask.length() + 1];
  146. strcpy(_netmask, netmask.c_str());
  147. }
  148. else
  149. _netmask = NULL;
  150. if (dns.length() > 0)
  151. {
  152. _dns = new char[dns.length() + 1];
  153. strcpy(_dns, dns.c_str());
  154. }
  155. else
  156. _dns = NULL;
  157. _rssithreashold = rssithreshold;
  158. RSSIThreashold = rssithreshold;
  159. }
  160. bool ChangeHostName(std::string fn, std::string _newhostname)
  161. {
  162. if (_newhostname == hostname)
  163. return false;
  164. string line = "";
  165. std::vector<string> splitted;
  166. bool found = false;
  167. std::vector<string> neuesfile;
  168. FILE* pFile;
  169. fn = FormatFileName(fn);
  170. pFile = fopen(fn.c_str(), "r");
  171. ESP_LOGD(TAG, "file loaded\n");
  172. if (pFile == NULL)
  173. return false;
  174. char zw[1024];
  175. fgets(zw, 1024, pFile);
  176. line = std::string(zw);
  177. while ((line.size() > 0) || !(feof(pFile)))
  178. {
  179. ESP_LOGD(TAG, "%s", line.c_str());
  180. splitted = ZerlegeZeileWLAN(line, "=");
  181. splitted[0] = trim(splitted[0], " ");
  182. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "HOSTNAME")){
  183. line = "hostname = \"" + _newhostname + "\"\n";
  184. found = true;
  185. }
  186. neuesfile.push_back(line);
  187. if (fgets(zw, 1024, pFile) == NULL)
  188. {
  189. line = "";
  190. }
  191. else
  192. {
  193. line = std::string(zw);
  194. }
  195. }
  196. if (!found)
  197. {
  198. line = "\nhostname = \"" + _newhostname + "\"\n";
  199. neuesfile.push_back(line);
  200. }
  201. fclose(pFile);
  202. pFile = fopen(fn.c_str(), "w+");
  203. for (int i = 0; i < neuesfile.size(); ++i)
  204. {
  205. ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  206. fputs(neuesfile[i].c_str(), pFile);
  207. }
  208. fclose(pFile);
  209. ESP_LOGD(TAG, "*** Hostname update done ***");
  210. return true;
  211. }
  212. bool ChangeRSSIThreashold(std::string fn, int _newrssithreashold)
  213. {
  214. if (RSSIThreashold == _newrssithreashold)
  215. return false;
  216. string line = "";
  217. std::vector<string> splitted;
  218. bool found = false;
  219. std::vector<string> neuesfile;
  220. FILE* pFile;
  221. fn = FormatFileName(fn);
  222. pFile = fopen(fn.c_str(), "r");
  223. ESP_LOGD(TAG, "file loaded\n");
  224. if (pFile == NULL)
  225. return false;
  226. char zw[1024];
  227. fgets(zw, 1024, pFile);
  228. line = std::string(zw);
  229. while ((line.size() > 0) || !(feof(pFile)))
  230. {
  231. ESP_LOGD(TAG, "%s", line.c_str());
  232. splitted = ZerlegeZeileWLAN(line, "=");
  233. splitted[0] = trim(splitted[0], " ");
  234. if ((splitted.size() > 1) && (toUpper(splitted[0]) == "RSSITHREASHOLD")){
  235. line = "RSSIThreashold = " + to_string(_newrssithreashold) + "\n";
  236. found = true;
  237. }
  238. neuesfile.push_back(line);
  239. if (fgets(zw, 1024, pFile) == NULL)
  240. {
  241. line = "";
  242. }
  243. else
  244. {
  245. line = std::string(zw);
  246. }
  247. }
  248. if (!found)
  249. {
  250. line = "RSSIThreashold = " + to_string(_newrssithreashold) + "\n";
  251. neuesfile.push_back(line);
  252. }
  253. fclose(pFile);
  254. pFile = fopen(fn.c_str(), "w+");
  255. for (int i = 0; i < neuesfile.size(); ++i)
  256. {
  257. ESP_LOGD(TAG, "%s", neuesfile[i].c_str());
  258. fputs(neuesfile[i].c_str(), pFile);
  259. }
  260. fclose(pFile);
  261. ESP_LOGD(TAG, "*** RSSIThreashold update done ***");
  262. return true;
  263. }