Helper.cpp 18 KB

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