Helper.cpp 9.6 KB

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