Helper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //#pragma warning(disable : 4996)
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "Helper.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <dirent.h>
  11. #ifdef __cplusplus
  12. }
  13. #endif
  14. #include <string.h>
  15. #include <esp_log.h>
  16. #include "ClassLogFile.h"
  17. //#include "ClassLogFile.h"
  18. static const char* TAG = "helper";
  19. //#define ISWINDOWS_TRUE
  20. #define PATH_MAX_STRING_SIZE 256
  21. using namespace std;
  22. /////////////////////////////////////////////////////////////////////////////////////////////
  23. string getESPHeapInfo(){
  24. string espInfoResultStr = "";
  25. char aMsgBuf[80];
  26. multi_heap_info_t aMultiHead_info ;
  27. heap_caps_get_info (&aMultiHead_info,MALLOC_CAP_8BIT);
  28. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  29. size_t aMinFreeHeadSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  30. size_t aMinFreeHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  31. size_t aHeapLargestFreeBlockSize = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
  32. sprintf(aMsgBuf," Free Heap Size: %ld", (long) aFreeHeapSize);
  33. size_t aFreeSPIHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_SPIRAM);
  34. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  35. size_t aMinFreeInternalHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  36. sprintf(aMsgBuf," Heap: %ld", (long) aFreeHeapSize);
  37. espInfoResultStr += string(aMsgBuf);
  38. sprintf(aMsgBuf," Min Free: %ld", (long) aMinFreeHeapSize);
  39. espInfoResultStr += string(aMsgBuf);
  40. sprintf(aMsgBuf," larg. Block: %ld", (long) aHeapLargestFreeBlockSize);
  41. espInfoResultStr += string(aMsgBuf);
  42. sprintf(aMsgBuf," SPI Heap: %ld", (long) aFreeSPIHeapSize);
  43. espInfoResultStr += string(aMsgBuf);
  44. sprintf(aMsgBuf," Min Free Heap Size: %ld", (long) aMinFreeHeadSize);
  45. sprintf(aMsgBuf," NOT_SPI Heap: %ld", (long) (aFreeHeapSize - aFreeSPIHeapSize));
  46. espInfoResultStr += string(aMsgBuf);
  47. sprintf(aMsgBuf," largest Block Size: %ld", (long) aHeapLargestFreeBlockSize);
  48. sprintf(aMsgBuf," Internal Heap: %ld", (long) (aFreeInternalHeapSize));
  49. espInfoResultStr += string(aMsgBuf);
  50. sprintf(aMsgBuf," Internal Min Heap free: %ld", (long) (aMinFreeInternalHeapSize));
  51. espInfoResultStr += string(aMsgBuf);
  52. return espInfoResultStr;
  53. }
  54. size_t getESPHeapSize(){
  55. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  56. return aFreeHeapSize;
  57. }
  58. size_t getInternalESPHeapSize() {
  59. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  60. return aFreeInternalHeapSize;
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////////////////////
  63. void memCopyGen(uint8_t* _source, uint8_t* _target, int _size)
  64. {
  65. for (int i = 0; i < _size; ++i)
  66. *(_target + i) = *(_source + i);
  67. }
  68. FILE* OpenFileAndWait(const char* nm, const char* _mode, int _waitsec)
  69. {
  70. FILE *pfile;
  71. ESP_LOGD(TAG, "open file %s in mode %s", nm, _mode);
  72. if ((pfile = fopen(nm, _mode)) != NULL) {
  73. ESP_LOGD(TAG, "File %s successfully opened", nm);
  74. }
  75. else {
  76. ESP_LOGD(TAG, "Error: file %s does not exist!", nm);
  77. return NULL;
  78. }
  79. /*
  80. if (pfile == NULL)
  81. {
  82. TickType_t xDelay;
  83. xDelay = _waitsec * 1000 / portTICK_PERIOD_MS;
  84. std::string zw = "File is locked: " + std::string(nm) + " - wait for " + std::to_string(_waitsec) + " seconds";
  85. LogFile.WriteToFile(zw);
  86. vTaskDelay( xDelay );
  87. pfile = fopen(nm, _mode);
  88. }
  89. */
  90. return pfile;
  91. }
  92. std::string FormatFileName(std::string input)
  93. {
  94. #ifdef ISWINDOWS_TRUE
  95. input.erase(0, 1);
  96. std::string os = "/";
  97. std::string ns = "\\";
  98. FindReplace(input, os, ns);
  99. #endif
  100. return input;
  101. }
  102. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  103. const size_t oldSize = oldString.length();
  104. // do nothing if line is shorter than the string to find
  105. if (oldSize > line.length()) return;
  106. const size_t newSize = newString.length();
  107. for (size_t pos = 0; ; pos += newSize) {
  108. // Locate the substring to replace
  109. pos = line.find(oldString, pos);
  110. if (pos == std::string::npos) return;
  111. if (oldSize == newSize) {
  112. // if they're same size, use std::string::replace
  113. line.replace(pos, oldSize, newString);
  114. }
  115. else {
  116. // if not same size, replace by erasing and inserting
  117. line.erase(pos, oldSize);
  118. line.insert(pos, newString);
  119. }
  120. }
  121. }
  122. void MakeDir(std::string _what)
  123. {
  124. // chdir(_where.c_str());
  125. if (mkdir(_what.c_str(), S_IRWXU|S_IRWXG|S_IROTH))
  126. ESP_LOGD(TAG, "Problem with MakeDir: %s", _what.c_str());
  127. }
  128. bool ctype_space(const char c, string adddelimiter)
  129. {
  130. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  131. {
  132. return true;
  133. }
  134. if (adddelimiter.find(c) != string::npos)
  135. return true;
  136. return false;
  137. }
  138. string trim(string istring, string adddelimiter)
  139. {
  140. bool trimmed = false;
  141. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  142. {
  143. istring.erase(istring.length() - 1);
  144. trimmed = true;
  145. }
  146. if (ctype_space(istring[0], adddelimiter))
  147. {
  148. istring.erase(0, 1);
  149. trimmed = true;
  150. }
  151. if ((trimmed == false) || (istring.size() == 0))
  152. {
  153. return istring;
  154. }
  155. else
  156. {
  157. return trim(istring, adddelimiter);
  158. }
  159. }
  160. size_t findDelimiterPos(string input, string delimiter)
  161. {
  162. size_t pos = std::string::npos;
  163. size_t zw;
  164. string akt_del;
  165. for (int anz = 0; anz < delimiter.length(); ++anz)
  166. {
  167. akt_del = delimiter[anz];
  168. if ((zw = input.find(akt_del)) != std::string::npos)
  169. {
  170. if (pos != std::string::npos)
  171. {
  172. if (zw < pos)
  173. pos = zw;
  174. }
  175. else
  176. pos = zw;
  177. }
  178. }
  179. return pos;
  180. }
  181. void RenameFile(string from, string to)
  182. {
  183. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  184. /* Delete file */
  185. FILE* fpSourceFile = OpenFileAndWait(from.c_str(), "rb");
  186. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  187. {
  188. ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", from.c_str());
  189. return;
  190. }
  191. fclose(fpSourceFile);
  192. rename(from.c_str(), to.c_str());
  193. }
  194. void DeleteFile(string fn)
  195. {
  196. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  197. /* Delete file */
  198. FILE* fpSourceFile = OpenFileAndWait(fn.c_str(), "rb");
  199. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  200. {
  201. ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", fn.c_str());
  202. return;
  203. }
  204. fclose(fpSourceFile);
  205. unlink(fn.c_str());
  206. }
  207. void CopyFile(string input, string output)
  208. {
  209. input = FormatFileName(input);
  210. output = FormatFileName(output);
  211. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  212. {
  213. ESP_LOGD(TAG, "wlan.ini kann nicht kopiert werden!");
  214. return;
  215. }
  216. char cTemp;
  217. FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb");
  218. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  219. {
  220. ESP_LOGD(TAG, "File %s existiert nicht!", input.c_str());
  221. return;
  222. }
  223. FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb");
  224. // Code Section
  225. // Read From The Source File - "Copy"
  226. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  227. {
  228. // Write To The Target File - "Paste"
  229. fwrite(&cTemp, 1, 1, fpTargetFile);
  230. }
  231. // Close The Files
  232. fclose(fpSourceFile);
  233. fclose(fpTargetFile);
  234. ESP_LOGD(TAG, "File copied: %s to %s", input.c_str(), output.c_str());
  235. }
  236. string getFileFullFileName(string filename)
  237. {
  238. size_t lastpos = filename.find_last_of('/');
  239. if (lastpos == string::npos)
  240. return "";
  241. // ESP_LOGD(TAG, "Last position: %d", lastpos);
  242. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  243. return zw;
  244. }
  245. string getDirectory(string filename)
  246. {
  247. size_t lastpos = filename.find('/');
  248. if (lastpos == string::npos)
  249. lastpos = filename.find('\\');
  250. if (lastpos == string::npos)
  251. return "";
  252. // ESP_LOGD(TAG, "Directory: %d", lastpos);
  253. string zw = filename.substr(0, lastpos - 1);
  254. return zw;
  255. }
  256. string getFileType(string filename)
  257. {
  258. size_t lastpos = filename.rfind(".", filename.length());
  259. size_t neu_pos;
  260. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  261. {
  262. lastpos = neu_pos;
  263. }
  264. if (lastpos == string::npos)
  265. return "";
  266. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  267. zw = toUpper(zw);
  268. return zw;
  269. }
  270. /* recursive mkdir */
  271. int mkdir_r(const char *dir, const mode_t mode) {
  272. char tmp[PATH_MAX_STRING_SIZE];
  273. char *p = NULL;
  274. struct stat sb;
  275. size_t len;
  276. /* copy path */
  277. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  278. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  279. return -1;
  280. }
  281. memcpy (tmp, dir, len);
  282. tmp[len] = '\0';
  283. /* remove trailing slash */
  284. if(tmp[len - 1] == '/') {
  285. tmp[len - 1] = '\0';
  286. }
  287. /* check if path exists and is a directory */
  288. if (stat (tmp, &sb) == 0) {
  289. if (S_ISDIR (sb.st_mode)) {
  290. return 0;
  291. }
  292. }
  293. /* recursive mkdir */
  294. for(p = tmp + 1; *p; p++) {
  295. if(*p == '/') {
  296. *p = 0;
  297. /* test path */
  298. if (stat(tmp, &sb) != 0) {
  299. /* path does not exist - create directory */
  300. if (mkdir(tmp, mode) < 0) {
  301. return -1;
  302. }
  303. } else if (!S_ISDIR(sb.st_mode)) {
  304. /* not a directory */
  305. return -1;
  306. }
  307. *p = '/';
  308. }
  309. }
  310. /* test path */
  311. if (stat(tmp, &sb) != 0) {
  312. /* path does not exist - create directory */
  313. if (mkdir(tmp, mode) < 0) {
  314. return -1;
  315. }
  316. } else if (!S_ISDIR(sb.st_mode)) {
  317. /* not a directory */
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. string toUpper(string in)
  323. {
  324. for (int i = 0; i < in.length(); ++i)
  325. in[i] = toupper(in[i]);
  326. return in;
  327. }
  328. string toLower(string in)
  329. {
  330. for (int i = 0; i < in.length(); ++i)
  331. in[i] = tolower(in[i]);
  332. return in;
  333. }
  334. // CPU Temp
  335. extern "C" uint8_t temprature_sens_read();
  336. float temperatureRead()
  337. {
  338. return (temprature_sens_read() - 32) / 1.8;
  339. }
  340. time_t addDays(time_t startTime, int days) {
  341. struct tm* tm = localtime(&startTime);
  342. tm->tm_mday += days;
  343. return mktime(tm);
  344. }
  345. int removeFolder(const char* folderPath, const char* logTag) {
  346. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  347. DIR *dir = opendir(folderPath);
  348. if (!dir) {
  349. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  350. return -1;
  351. }
  352. struct dirent *entry;
  353. int deleted = 0;
  354. while ((entry = readdir(dir)) != NULL) {
  355. std::string path = string(folderPath) + "/" + entry->d_name;
  356. if (entry->d_type == DT_REG) {
  357. if (unlink(path.c_str()) == 0) {
  358. deleted ++;
  359. } else {
  360. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  361. }
  362. } else if (entry->d_type == DT_DIR) {
  363. deleted += removeFolder(path.c_str(), logTag);
  364. }
  365. }
  366. closedir(dir);
  367. if (rmdir(folderPath) != 0) {
  368. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  369. }
  370. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  371. return deleted;
  372. }
  373. std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter = "")
  374. {
  375. std::vector<string> Output;
  376. std::string delimiter = " =,";
  377. if (_delimiter.length() > 0){
  378. delimiter = _delimiter;
  379. }
  380. input = trim(input, delimiter);
  381. size_t pos = findDelimiterPos(input, delimiter);
  382. std::string token;
  383. while (pos != std::string::npos) {
  384. token = input.substr(0, pos);
  385. token = trim(token, delimiter);
  386. Output.push_back(token);
  387. input.erase(0, pos + 1);
  388. input = trim(input, delimiter);
  389. pos = findDelimiterPos(input, delimiter);
  390. }
  391. Output.push_back(input);
  392. return Output;
  393. }