read_wlanini.cpp 8.2 KB

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