Helper.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 <iomanip>
  8. #include <sstream>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #include <dirent.h>
  13. #ifdef __cplusplus
  14. }
  15. #endif
  16. #include <string.h>
  17. #include <esp_log.h>
  18. #include "ClassLogFile.h"
  19. #include "esp_vfs_fat.h"
  20. #include "esp_mac.h"
  21. static const char* TAG = "helper";
  22. //#define ISWINDOWS_TRUE
  23. #define PATH_MAX_STRING_SIZE 256
  24. using namespace std;
  25. sdmmc_cid_t SDCardCid;
  26. sdmmc_csd_t SDCardCsd;
  27. /////////////////////////////////////////////////////////////////////////////////////////////
  28. string getESPHeapInfo(){
  29. string espInfoResultStr = "";
  30. char aMsgBuf[80];
  31. multi_heap_info_t aMultiHead_info ;
  32. heap_caps_get_info (&aMultiHead_info,MALLOC_CAP_8BIT);
  33. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  34. size_t aMinFreeHeadSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  35. size_t aMinFreeHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
  36. size_t aHeapLargestFreeBlockSize = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
  37. sprintf(aMsgBuf," Free Heap Size: %ld", (long) aFreeHeapSize);
  38. size_t aFreeSPIHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_SPIRAM);
  39. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  40. size_t aMinFreeInternalHeapSize = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  41. sprintf(aMsgBuf," Heap: %ld", (long) aFreeHeapSize);
  42. espInfoResultStr += string(aMsgBuf);
  43. sprintf(aMsgBuf," Min Free: %ld", (long) aMinFreeHeapSize);
  44. espInfoResultStr += string(aMsgBuf);
  45. sprintf(aMsgBuf," larg. Block: %ld", (long) aHeapLargestFreeBlockSize);
  46. espInfoResultStr += string(aMsgBuf);
  47. sprintf(aMsgBuf," SPI Heap: %ld", (long) aFreeSPIHeapSize);
  48. espInfoResultStr += string(aMsgBuf);
  49. sprintf(aMsgBuf," Min Free Heap Size: %ld", (long) aMinFreeHeadSize);
  50. sprintf(aMsgBuf," NOT_SPI Heap: %ld", (long) (aFreeHeapSize - aFreeSPIHeapSize));
  51. espInfoResultStr += string(aMsgBuf);
  52. sprintf(aMsgBuf," largest Block Size: %ld", (long) aHeapLargestFreeBlockSize);
  53. sprintf(aMsgBuf," Internal Heap: %ld", (long) (aFreeInternalHeapSize));
  54. espInfoResultStr += string(aMsgBuf);
  55. sprintf(aMsgBuf," Internal Min Heap free: %ld", (long) (aMinFreeInternalHeapSize));
  56. espInfoResultStr += string(aMsgBuf);
  57. return espInfoResultStr;
  58. }
  59. size_t getESPHeapSize(){
  60. size_t aFreeHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  61. return aFreeHeapSize;
  62. }
  63. size_t getInternalESPHeapSize() {
  64. size_t aFreeInternalHeapSize = heap_caps_get_free_size(MALLOC_CAP_8BIT| MALLOC_CAP_INTERNAL);
  65. return aFreeInternalHeapSize;
  66. }
  67. string getSDCardPartitionSize(){
  68. FATFS *fs;
  69. uint32_t fre_clust, tot_sect;
  70. /* Get volume information and free clusters of drive 0 */
  71. f_getfree("0:", (DWORD *)&fre_clust, &fs);
  72. tot_sect = ((fs->n_fatent - 2) * fs->csize) /1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB
  73. printf("%d MB total drive space (Sector size [bytes]: %d)\n", (int)tot_sect, (int)fs->csize*512);
  74. return std::to_string(tot_sect);
  75. }
  76. string getSDCardFreePartitionSpace(){
  77. FATFS *fs;
  78. uint32_t fre_clust, fre_sect;
  79. /* Get volume information and free clusters of drive 0 */
  80. f_getfree("0:", (DWORD *)&fre_clust, &fs);
  81. fre_sect = (fre_clust * fs->csize) / 1024 /(1024/SDCardCsd.sector_size); //corrected by SD Card sector size (usually 512 bytes) and convert to MB
  82. printf("%d MB free drive space (Sector size [bytes]: %d)\n", (int)fre_sect, (int)fs->ssize);
  83. return std::to_string(fre_sect);
  84. }
  85. string getSDCardPartitionAllocationSize(){
  86. FATFS *fs;
  87. uint32_t fre_clust, allocation_size;
  88. /* Get volume information and free clusters of drive 0 */
  89. f_getfree("0:", (DWORD *)&fre_clust, &fs);
  90. allocation_size = fs->ssize;
  91. #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
  92. printf("SD Card Partition Allocation Size (bytes): %lu)\n", allocation_size);
  93. #else
  94. printf("SD Card Partition Allocation Size (bytes): %zu)\n", allocation_size);
  95. #endif
  96. return std::to_string(allocation_size);
  97. }
  98. void SaveSDCardInfo(sdmmc_card_t* card) {
  99. SDCardCid = card->cid;
  100. SDCardCsd = card->csd;
  101. }
  102. string getSDCardManufacturer(){
  103. string SDCardManufacturer = SDCardParseManufacturerIDs(SDCardCid.mfg_id);
  104. printf("SD Card Manufactuer: %s\n", SDCardManufacturer.c_str());
  105. return (SDCardManufacturer + " (ID: " + std::to_string(SDCardCid.mfg_id) + ")");
  106. }
  107. string getSDCardName(){
  108. char *SDCardName = SDCardCid.name;
  109. printf("SD Card Name: %s\n", SDCardName);
  110. return std::string(SDCardName);
  111. }
  112. string getSDCardCapacity(){
  113. int SDCardCapacity = SDCardCsd.capacity / (1024/SDCardCsd.sector_size) / 1024; // total sectors * sector size --> Byte to MB (1024*1024)
  114. printf("SD Card Capacity: %s\n", std::to_string(SDCardCapacity).c_str());
  115. return std::to_string(SDCardCapacity);
  116. }
  117. string getSDCardSectorSize(){
  118. int SDCardSectorSize = SDCardCsd.sector_size;
  119. printf("SD Card Sector Size: %s\n", std::to_string(SDCardSectorSize).c_str());
  120. return std::to_string(SDCardSectorSize);
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////////////////////
  123. void memCopyGen(uint8_t* _source, uint8_t* _target, int _size)
  124. {
  125. for (int i = 0; i < _size; ++i)
  126. *(_target + i) = *(_source + i);
  127. }
  128. FILE* OpenFileAndWait(const char* nm, const char* _mode, int _waitsec)
  129. {
  130. FILE *pfile;
  131. ESP_LOGD(TAG, "open file %s in mode %s", nm, _mode);
  132. if ((pfile = fopen(nm, _mode)) != NULL) {
  133. ESP_LOGD(TAG, "File %s successfully opened", nm);
  134. }
  135. else {
  136. ESP_LOGD(TAG, "Error: file %s does not exist!", nm);
  137. return NULL;
  138. }
  139. /*
  140. if (pfile == NULL)
  141. {
  142. TickType_t xDelay;
  143. xDelay = _waitsec * 1000 / portTICK_PERIOD_MS;
  144. std::string zw = "File is locked: " + std::string(nm) + " - wait for " + std::to_string(_waitsec) + " seconds";
  145. LogFile.WriteToFile(ESP_LOG_INFO, zw);
  146. vTaskDelay( xDelay );
  147. pfile = fopen(nm, _mode);
  148. }
  149. */
  150. return pfile;
  151. }
  152. std::string FormatFileName(std::string input)
  153. {
  154. #ifdef ISWINDOWS_TRUE
  155. input.erase(0, 1);
  156. std::string os = "/";
  157. std::string ns = "\\";
  158. FindReplace(input, os, ns);
  159. #endif
  160. return input;
  161. }
  162. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  163. const size_t oldSize = oldString.length();
  164. // do nothing if line is shorter than the string to find
  165. if (oldSize > line.length()) return;
  166. const size_t newSize = newString.length();
  167. for (size_t pos = 0; ; pos += newSize) {
  168. // Locate the substring to replace
  169. pos = line.find(oldString, pos);
  170. if (pos == std::string::npos) return;
  171. if (oldSize == newSize) {
  172. // if they're same size, use std::string::replace
  173. line.replace(pos, oldSize, newString);
  174. }
  175. else {
  176. // if not same size, replace by erasing and inserting
  177. line.erase(pos, oldSize);
  178. line.insert(pos, newString);
  179. }
  180. }
  181. }
  182. void MakeDir(std::string _what)
  183. {
  184. int mk_ret = mkdir(_what.c_str(), 0775);
  185. if (mk_ret)
  186. ESP_LOGD(TAG, "error with mkdir %s ret %d", _what.c_str(), mk_ret);
  187. }
  188. bool ctype_space(const char c, string adddelimiter)
  189. {
  190. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  191. {
  192. return true;
  193. }
  194. if (adddelimiter.find(c) != string::npos)
  195. return true;
  196. return false;
  197. }
  198. string trim(string istring, string adddelimiter)
  199. {
  200. bool trimmed = false;
  201. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  202. {
  203. istring.erase(istring.length() - 1);
  204. trimmed = true;
  205. }
  206. if (ctype_space(istring[0], adddelimiter))
  207. {
  208. istring.erase(0, 1);
  209. trimmed = true;
  210. }
  211. if ((trimmed == false) || (istring.size() == 0))
  212. {
  213. return istring;
  214. }
  215. else
  216. {
  217. return trim(istring, adddelimiter);
  218. }
  219. }
  220. size_t findDelimiterPos(string input, string delimiter)
  221. {
  222. size_t pos = std::string::npos;
  223. size_t zw;
  224. string akt_del;
  225. for (int anz = 0; anz < delimiter.length(); ++anz)
  226. {
  227. akt_del = delimiter[anz];
  228. if ((zw = input.find(akt_del)) != std::string::npos)
  229. {
  230. if (pos != std::string::npos)
  231. {
  232. if (zw < pos)
  233. pos = zw;
  234. }
  235. else
  236. pos = zw;
  237. }
  238. }
  239. return pos;
  240. }
  241. void RenameFile(string from, string to)
  242. {
  243. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  244. /* Delete file */
  245. FILE* fpSourceFile = OpenFileAndWait(from.c_str(), "rb");
  246. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  247. {
  248. ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", from.c_str());
  249. return;
  250. }
  251. fclose(fpSourceFile);
  252. rename(from.c_str(), to.c_str());
  253. }
  254. void DeleteFile(string fn)
  255. {
  256. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  257. /* Delete file */
  258. FILE* fpSourceFile = OpenFileAndWait(fn.c_str(), "rb");
  259. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  260. {
  261. ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", fn.c_str());
  262. return;
  263. }
  264. fclose(fpSourceFile);
  265. unlink(fn.c_str());
  266. }
  267. void CopyFile(string input, string output)
  268. {
  269. input = FormatFileName(input);
  270. output = FormatFileName(output);
  271. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  272. {
  273. ESP_LOGD(TAG, "wlan.ini kann nicht kopiert werden!");
  274. return;
  275. }
  276. char cTemp;
  277. FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb");
  278. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  279. {
  280. ESP_LOGD(TAG, "File %s existiert nicht!", input.c_str());
  281. return;
  282. }
  283. FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb");
  284. // Code Section
  285. // Read From The Source File - "Copy"
  286. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  287. {
  288. // Write To The Target File - "Paste"
  289. fwrite(&cTemp, 1, 1, fpTargetFile);
  290. }
  291. // Close The Files
  292. fclose(fpSourceFile);
  293. fclose(fpTargetFile);
  294. ESP_LOGD(TAG, "File copied: %s to %s", input.c_str(), output.c_str());
  295. }
  296. string getFileFullFileName(string filename)
  297. {
  298. size_t lastpos = filename.find_last_of('/');
  299. if (lastpos == string::npos)
  300. return "";
  301. // ESP_LOGD(TAG, "Last position: %d", lastpos);
  302. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  303. return zw;
  304. }
  305. string getDirectory(string filename)
  306. {
  307. size_t lastpos = filename.find('/');
  308. if (lastpos == string::npos)
  309. lastpos = filename.find('\\');
  310. if (lastpos == string::npos)
  311. return "";
  312. // ESP_LOGD(TAG, "Directory: %d", lastpos);
  313. string zw = filename.substr(0, lastpos - 1);
  314. return zw;
  315. }
  316. string getFileType(string filename)
  317. {
  318. size_t lastpos = filename.rfind(".", filename.length());
  319. size_t neu_pos;
  320. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  321. {
  322. lastpos = neu_pos;
  323. }
  324. if (lastpos == string::npos)
  325. return "";
  326. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  327. zw = toUpper(zw);
  328. return zw;
  329. }
  330. /* recursive mkdir */
  331. int mkdir_r(const char *dir, const mode_t mode) {
  332. char tmp[PATH_MAX_STRING_SIZE];
  333. char *p = NULL;
  334. struct stat sb;
  335. size_t len;
  336. /* copy path */
  337. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  338. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  339. return -1;
  340. }
  341. memcpy (tmp, dir, len);
  342. tmp[len] = '\0';
  343. /* remove trailing slash */
  344. if(tmp[len - 1] == '/') {
  345. tmp[len - 1] = '\0';
  346. }
  347. /* check if path exists and is a directory */
  348. if (stat (tmp, &sb) == 0) {
  349. if (S_ISDIR (sb.st_mode)) {
  350. return 0;
  351. }
  352. }
  353. /* recursive mkdir */
  354. for(p = tmp + 1; *p; p++) {
  355. if(*p == '/') {
  356. *p = 0;
  357. /* test path */
  358. if (stat(tmp, &sb) != 0) {
  359. /* path does not exist - create directory */
  360. if (mkdir(tmp, mode) < 0) {
  361. return -1;
  362. }
  363. } else if (!S_ISDIR(sb.st_mode)) {
  364. /* not a directory */
  365. return -1;
  366. }
  367. *p = '/';
  368. }
  369. }
  370. /* test path */
  371. if (stat(tmp, &sb) != 0) {
  372. /* path does not exist - create directory */
  373. if (mkdir(tmp, mode) < 0) {
  374. return -1;
  375. }
  376. } else if (!S_ISDIR(sb.st_mode)) {
  377. /* not a directory */
  378. return -1;
  379. }
  380. return 0;
  381. }
  382. string toUpper(string in)
  383. {
  384. for (int i = 0; i < in.length(); ++i)
  385. in[i] = toupper(in[i]);
  386. return in;
  387. }
  388. string toLower(string in)
  389. {
  390. for (int i = 0; i < in.length(); ++i)
  391. in[i] = tolower(in[i]);
  392. return in;
  393. }
  394. // CPU Temp
  395. extern "C" uint8_t temprature_sens_read();
  396. float temperatureRead()
  397. {
  398. return (temprature_sens_read() - 32) / 1.8;
  399. }
  400. time_t addDays(time_t startTime, int days) {
  401. struct tm* tm = localtime(&startTime);
  402. tm->tm_mday += days;
  403. return mktime(tm);
  404. }
  405. int removeFolder(const char* folderPath, const char* logTag) {
  406. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  407. DIR *dir = opendir(folderPath);
  408. if (!dir) {
  409. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  410. return -1;
  411. }
  412. struct dirent *entry;
  413. int deleted = 0;
  414. while ((entry = readdir(dir)) != NULL) {
  415. std::string path = string(folderPath) + "/" + entry->d_name;
  416. if (entry->d_type == DT_REG) {
  417. if (unlink(path.c_str()) == 0) {
  418. deleted ++;
  419. } else {
  420. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  421. }
  422. } else if (entry->d_type == DT_DIR) {
  423. deleted += removeFolder(path.c_str(), logTag);
  424. }
  425. }
  426. closedir(dir);
  427. if (rmdir(folderPath) != 0) {
  428. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  429. }
  430. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  431. return deleted;
  432. }
  433. std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter = "")
  434. {
  435. std::vector<string> Output;
  436. std::string delimiter = " =,";
  437. if (_delimiter.length() > 0){
  438. delimiter = _delimiter;
  439. }
  440. input = trim(input, delimiter);
  441. size_t pos = findDelimiterPos(input, delimiter);
  442. std::string token;
  443. while (pos != std::string::npos) {
  444. token = input.substr(0, pos);
  445. token = trim(token, delimiter);
  446. Output.push_back(token);
  447. input.erase(0, pos + 1);
  448. input = trim(input, delimiter);
  449. pos = findDelimiterPos(input, delimiter);
  450. }
  451. Output.push_back(input);
  452. return Output;
  453. }
  454. /* Source: https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git/tree/lsmmc.c */
  455. /* SD Card Manufacturer Database */
  456. struct SDCard_Manufacturer_database {
  457. string type;
  458. int id;
  459. string manufacturer;
  460. };
  461. /* Source: https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git/tree/lsmmc.c */
  462. /* SD Card Manufacturer Database */
  463. struct SDCard_Manufacturer_database database[] = {
  464. {
  465. .type = "sd",
  466. .id = 0x01,
  467. .manufacturer = "Panasonic",
  468. },
  469. {
  470. .type = "sd",
  471. .id = 0x02,
  472. .manufacturer = "Toshiba/Kingston/Viking",
  473. },
  474. {
  475. .type = "sd",
  476. .id = 0x03,
  477. .manufacturer = "SanDisk",
  478. },
  479. {
  480. .type = "sd",
  481. .id = 0x08,
  482. .manufacturer = "Silicon Power",
  483. },
  484. {
  485. .type = "sd",
  486. .id = 0x18,
  487. .manufacturer = "Infineon",
  488. },
  489. {
  490. .type = "sd",
  491. .id = 0x1b,
  492. .manufacturer = "Transcend/Samsung",
  493. },
  494. {
  495. .type = "sd",
  496. .id = 0x1c,
  497. .manufacturer = "Transcend",
  498. },
  499. {
  500. .type = "sd",
  501. .id = 0x1d,
  502. .manufacturer = "Corsair/AData",
  503. },
  504. {
  505. .type = "sd",
  506. .id = 0x1e,
  507. .manufacturer = "Transcend",
  508. },
  509. {
  510. .type = "sd",
  511. .id = 0x1f,
  512. .manufacturer = "Kingston",
  513. },
  514. {
  515. .type = "sd",
  516. .id = 0x27,
  517. .manufacturer = "Delkin/Phison",
  518. },
  519. {
  520. .type = "sd",
  521. .id = 0x28,
  522. .manufacturer = "Lexar",
  523. },
  524. {
  525. .type = "sd",
  526. .id = 0x30,
  527. .manufacturer = "SanDisk",
  528. },
  529. {
  530. .type = "sd",
  531. .id = 0x31,
  532. .manufacturer = "Silicon Power",
  533. },
  534. {
  535. .type = "sd",
  536. .id = 0x33,
  537. .manufacturer = "STMicroelectronics",
  538. },
  539. {
  540. .type = "sd",
  541. .id = 0x41,
  542. .manufacturer = "Kingston",
  543. },
  544. {
  545. .type = "sd",
  546. .id = 0x6f,
  547. .manufacturer = "STMicroelectronics",
  548. },
  549. {
  550. .type = "sd",
  551. .id = 0x74,
  552. .manufacturer = "Transcend",
  553. },
  554. {
  555. .type = "sd",
  556. .id = 0x76,
  557. .manufacturer = "Patriot",
  558. },
  559. {
  560. .type = "sd",
  561. .id = 0x82,
  562. .manufacturer = "Gobe/Sony",
  563. },
  564. {
  565. .type = "sd",
  566. .id = 0x89,
  567. .manufacturer = "Unknown",
  568. }
  569. };
  570. /* Parse SD Card Manufacturer Database */
  571. string SDCardParseManufacturerIDs(int id)
  572. {
  573. unsigned int id_cnt = sizeof(database) / sizeof(struct SDCard_Manufacturer_database);
  574. string ret_val = "";
  575. for (int i = 0; i < id_cnt; i++) {
  576. if (database[i].id == id) {
  577. return database[i].manufacturer;
  578. }
  579. else {
  580. ret_val = "ID unknown (not in DB)";
  581. }
  582. }
  583. return ret_val;
  584. }
  585. string RundeOutput(double _in, int _anzNachkomma)
  586. {
  587. std::stringstream stream;
  588. int _zw = _in;
  589. // ESP_LOGD(TAG, "AnzNachkomma: %d", _anzNachkomma);
  590. if (_anzNachkomma < 0) {
  591. _anzNachkomma = 0;
  592. }
  593. if (_anzNachkomma > 0)
  594. {
  595. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  596. return stream.str();
  597. }
  598. else
  599. {
  600. stream << _zw;
  601. }
  602. return stream.str();
  603. }
  604. string getMac(void) {
  605. uint8_t macInt[6];
  606. char macFormated[6*2 + 5 + 1]; // AA:BB:CC:DD:EE:FF
  607. esp_read_mac(macInt, ESP_MAC_WIFI_STA);
  608. sprintf(macFormated, "%02X:%02X:%02X:%02X:%02X:%02X", macInt[0], macInt[1], macInt[2], macInt[3], macInt[4], macInt[5]);
  609. return macFormated;
  610. }
  611. string getResetReason(void) {
  612. std::string reasonText;
  613. switch(esp_reset_reason()) {
  614. case ESP_RST_POWERON: reasonText = "Power-on event"; break; //!< Reset due to power-on event
  615. case ESP_RST_EXT: reasonText = "External pin"; break; //!< Reset by external pin (not applicable for ESP32)
  616. case ESP_RST_SW: reasonText = "Via esp_restart"; break; //!< Software reset via esp_restart
  617. case ESP_RST_PANIC: reasonText = "Exception/panic"; break; //!< Software reset due to exception/panic
  618. case ESP_RST_INT_WDT: reasonText = "Interrupt watchdog"; break; //!< Reset (software or hardware) due to interrupt watchdog
  619. case ESP_RST_TASK_WDT: reasonText = "Task watchdog"; break; //!< Reset due to task watchdog
  620. case ESP_RST_WDT: reasonText = "Other watchdogs"; break; //!< Reset due to other watchdogs
  621. case ESP_RST_DEEPSLEEP: reasonText = "Exiting deep sleep mode"; break; //!< Reset after exiting deep sleep mode
  622. case ESP_RST_BROWNOUT: reasonText = "Brownout"; break; //!< Brownout reset (software or hardware)
  623. case ESP_RST_SDIO: reasonText = "SDIO"; break; //!< Reset over SDIO
  624. case ESP_RST_UNKNOWN: //!< Reset reason can not be determined
  625. default:
  626. reasonText = "Unknown";
  627. }
  628. return reasonText;
  629. }