Helper.cpp 8.8 KB

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