Helper.cpp 11 KB

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