Helper.cpp 10 KB

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