Helper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 DeleteFile(string fn)
  170. {
  171. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  172. /* Delete file */
  173. FILE* fpSourceFile = OpenFileAndWait(fn.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", fn.c_str());
  177. return;
  178. }
  179. fclose(fpSourceFile);
  180. unlink(fn.c_str());
  181. }
  182. void CopyFile(string input, string output)
  183. {
  184. input = FormatFileName(input);
  185. output = FormatFileName(output);
  186. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  187. {
  188. printf("wlan.ini kann nicht kopiert werden!\n");
  189. return;
  190. }
  191. char cTemp;
  192. FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb");
  193. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  194. {
  195. printf("File %s existiert nicht!\n", input.c_str());
  196. return;
  197. }
  198. FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb");
  199. // Code Section
  200. // Read From The Source File - "Copy"
  201. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  202. {
  203. // Write To The Target File - "Paste"
  204. fwrite(&cTemp, 1, 1, fpTargetFile);
  205. }
  206. // Close The Files
  207. fclose(fpSourceFile);
  208. fclose(fpTargetFile);
  209. printf("File copied: %s to %s", input.c_str(), output.c_str());
  210. }
  211. string getFileFullFileName(string filename)
  212. {
  213. size_t lastpos = filename.find_last_of('/');
  214. if (lastpos == string::npos)
  215. return "";
  216. // printf("Last position: %d\n", lastpos);
  217. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  218. return zw;
  219. }
  220. string getDirectory(string filename)
  221. {
  222. size_t lastpos = filename.find('/');
  223. if (lastpos == string::npos)
  224. return "";
  225. // printf("Directory: %d\n", lastpos);
  226. string zw = filename.substr(0, lastpos - 1);
  227. return zw;
  228. }
  229. string getFileType(string filename)
  230. {
  231. size_t lastpos = filename.find(".", 0);
  232. size_t neu_pos;
  233. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  234. {
  235. lastpos = neu_pos;
  236. }
  237. if (lastpos == string::npos)
  238. return "";
  239. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  240. zw = toUpper(zw);
  241. return zw;
  242. }
  243. /* recursive mkdir */
  244. int mkdir_r(const char *dir, const mode_t mode) {
  245. char tmp[PATH_MAX_STRING_SIZE];
  246. char *p = NULL;
  247. struct stat sb;
  248. size_t len;
  249. /* copy path */
  250. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  251. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  252. return -1;
  253. }
  254. memcpy (tmp, dir, len);
  255. tmp[len] = '\0';
  256. /* remove trailing slash */
  257. if(tmp[len - 1] == '/') {
  258. tmp[len - 1] = '\0';
  259. }
  260. /* check if path exists and is a directory */
  261. if (stat (tmp, &sb) == 0) {
  262. if (S_ISDIR (sb.st_mode)) {
  263. return 0;
  264. }
  265. }
  266. /* recursive mkdir */
  267. for(p = tmp + 1; *p; p++) {
  268. if(*p == '/') {
  269. *p = 0;
  270. /* test path */
  271. if (stat(tmp, &sb) != 0) {
  272. /* path does not exist - create directory */
  273. if (mkdir(tmp, mode) < 0) {
  274. return -1;
  275. }
  276. } else if (!S_ISDIR(sb.st_mode)) {
  277. /* not a directory */
  278. return -1;
  279. }
  280. *p = '/';
  281. }
  282. }
  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. return 0;
  294. }
  295. string toUpper(string in)
  296. {
  297. for (int i = 0; i < in.length(); ++i)
  298. in[i] = toupper(in[i]);
  299. return in;
  300. }
  301. string toLower(string in)
  302. {
  303. for (int i = 0; i < in.length(); ++i)
  304. in[i] = tolower(in[i]);
  305. return in;
  306. }
  307. // CPU Temp
  308. extern "C" uint8_t temprature_sens_read();
  309. float temperatureRead()
  310. {
  311. return (temprature_sens_read() - 32) / 1.8;
  312. }
  313. time_t addDays(time_t startTime, int days) {
  314. struct tm* tm = localtime(&startTime);
  315. tm->tm_mday += days;
  316. return mktime(tm);
  317. }
  318. int removeFolder(const char* folderPath, const char* logTag) {
  319. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  320. DIR *dir = opendir(folderPath);
  321. if (!dir) {
  322. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  323. return -1;
  324. }
  325. struct dirent *entry;
  326. int deleted = 0;
  327. while ((entry = readdir(dir)) != NULL) {
  328. std::string path = string(folderPath) + "/" + entry->d_name;
  329. if (entry->d_type == DT_REG) {
  330. if (unlink(path.c_str()) == 0) {
  331. deleted ++;
  332. } else {
  333. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  334. }
  335. } else if (entry->d_type == DT_DIR) {
  336. deleted += removeFolder(path.c_str(), logTag);
  337. }
  338. }
  339. closedir(dir);
  340. if (rmdir(folderPath) != 0) {
  341. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  342. }
  343. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  344. return deleted;
  345. }
  346. std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter = "")
  347. {
  348. std::vector<string> Output;
  349. std::string delimiter = " =,";
  350. if (_delimiter.length() > 0){
  351. delimiter = _delimiter;
  352. }
  353. input = trim(input, delimiter);
  354. size_t pos = findDelimiterPos(input, delimiter);
  355. std::string token;
  356. while (pos != std::string::npos) {
  357. token = input.substr(0, pos);
  358. token = trim(token, delimiter);
  359. Output.push_back(token);
  360. input.erase(0, pos + 1);
  361. input = trim(input, delimiter);
  362. pos = findDelimiterPos(input, delimiter);
  363. }
  364. Output.push_back(input);
  365. return Output;
  366. }