Helper.cpp 11 KB

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