Helper.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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, bool silent)
  124. {
  125. FILE *pfile;
  126. ESP_LOGD(TAG, "open file %s in mode %s", nm, _mode);
  127. if ((pfile = fopen(nm, _mode)) != NULL) {
  128. if (!silent) ESP_LOGE(TAG, "File %s successfully opened", nm);
  129. }
  130. else {
  131. if (!silent) ESP_LOGE(TAG, "Error: file %s does not exist!", nm);
  132. return NULL;
  133. }
  134. return pfile;
  135. }
  136. std::string FormatFileName(std::string input)
  137. {
  138. #ifdef ISWINDOWS_TRUE
  139. input.erase(0, 1);
  140. std::string os = "/";
  141. std::string ns = "\\";
  142. FindReplace(input, os, ns);
  143. #endif
  144. return input;
  145. }
  146. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  147. const size_t oldSize = oldString.length();
  148. // do nothing if line is shorter than the string to find
  149. if (oldSize > line.length()) return;
  150. const size_t newSize = newString.length();
  151. for (size_t pos = 0; ; pos += newSize) {
  152. // Locate the substring to replace
  153. pos = line.find(oldString, pos);
  154. if (pos == std::string::npos) return;
  155. if (oldSize == newSize) {
  156. // if they're same size, use std::string::replace
  157. line.replace(pos, oldSize, newString);
  158. }
  159. else {
  160. // if not same size, replace by erasing and inserting
  161. line.erase(pos, oldSize);
  162. line.insert(pos, newString);
  163. }
  164. }
  165. }
  166. bool MakeDir(std::string _what)
  167. {
  168. int mk_ret = mkdir(_what.c_str(), 0775);
  169. if (mk_ret)
  170. {
  171. ESP_LOGD(TAG, "error with mkdir %s ret %d", _what.c_str(), mk_ret);
  172. return false;
  173. }
  174. return true;
  175. }
  176. bool ctype_space(const char c, string adddelimiter)
  177. {
  178. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  179. {
  180. return true;
  181. }
  182. if (adddelimiter.find(c) != string::npos)
  183. return true;
  184. return false;
  185. }
  186. string trim(string istring, string adddelimiter)
  187. {
  188. bool trimmed = false;
  189. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  190. {
  191. istring.erase(istring.length() - 1);
  192. trimmed = true;
  193. }
  194. if (ctype_space(istring[0], adddelimiter))
  195. {
  196. istring.erase(0, 1);
  197. trimmed = true;
  198. }
  199. if ((trimmed == false) || (istring.size() == 0))
  200. {
  201. return istring;
  202. }
  203. else
  204. {
  205. return trim(istring, adddelimiter);
  206. }
  207. }
  208. size_t findDelimiterPos(string input, string delimiter)
  209. {
  210. size_t pos = std::string::npos;
  211. size_t zw;
  212. string akt_del;
  213. for (int anz = 0; anz < delimiter.length(); ++anz)
  214. {
  215. akt_del = delimiter[anz];
  216. if ((zw = input.find(akt_del)) != std::string::npos)
  217. {
  218. if (pos != std::string::npos)
  219. {
  220. if (zw < pos)
  221. pos = zw;
  222. }
  223. else
  224. pos = zw;
  225. }
  226. }
  227. return pos;
  228. }
  229. bool RenameFile(string from, string to)
  230. {
  231. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  232. /* Delete file */
  233. FILE* fpSourceFile = OpenFileAndWait(from.c_str(), "rb");
  234. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  235. {
  236. ESP_LOGE(TAG, "DeleteFile: File %s existiert nicht!", from.c_str());
  237. return false;
  238. }
  239. fclose(fpSourceFile);
  240. rename(from.c_str(), to.c_str());
  241. return true;
  242. }
  243. bool DeleteFile(string fn)
  244. {
  245. // ESP_LOGI(logTag, "Deleting file : %s", fn.c_str());
  246. /* Delete file */
  247. FILE* fpSourceFile = OpenFileAndWait(fn.c_str(), "rb");
  248. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  249. {
  250. ESP_LOGD(TAG, "DeleteFile: File %s existiert nicht!", fn.c_str());
  251. return false;
  252. }
  253. fclose(fpSourceFile);
  254. unlink(fn.c_str());
  255. return true;
  256. }
  257. bool CopyFile(string input, string output)
  258. {
  259. input = FormatFileName(input);
  260. output = FormatFileName(output);
  261. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  262. {
  263. ESP_LOGD(TAG, "wlan.ini kann nicht kopiert werden!");
  264. return false;
  265. }
  266. char cTemp;
  267. FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb");
  268. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  269. {
  270. ESP_LOGD(TAG, "File %s existiert nicht!", input.c_str());
  271. return false;
  272. }
  273. FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb");
  274. // Code Section
  275. // Read From The Source File - "Copy"
  276. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  277. {
  278. // Write To The Target File - "Paste"
  279. fwrite(&cTemp, 1, 1, fpTargetFile);
  280. }
  281. // Close The Files
  282. fclose(fpSourceFile);
  283. fclose(fpTargetFile);
  284. ESP_LOGD(TAG, "File copied: %s to %s", input.c_str(), output.c_str());
  285. return true;
  286. }
  287. string getFileFullFileName(string filename)
  288. {
  289. size_t lastpos = filename.find_last_of('/');
  290. if (lastpos == string::npos)
  291. return "";
  292. // ESP_LOGD(TAG, "Last position: %d", lastpos);
  293. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  294. return zw;
  295. }
  296. string getDirectory(string filename)
  297. {
  298. size_t lastpos = filename.find('/');
  299. if (lastpos == string::npos)
  300. lastpos = filename.find('\\');
  301. if (lastpos == string::npos)
  302. return "";
  303. // ESP_LOGD(TAG, "Directory: %d", lastpos);
  304. string zw = filename.substr(0, lastpos - 1);
  305. return zw;
  306. }
  307. string getFileType(string filename)
  308. {
  309. size_t lastpos = filename.rfind(".", filename.length());
  310. size_t neu_pos;
  311. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  312. {
  313. lastpos = neu_pos;
  314. }
  315. if (lastpos == string::npos)
  316. return "";
  317. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  318. zw = toUpper(zw);
  319. return zw;
  320. }
  321. /* recursive mkdir */
  322. int mkdir_r(const char *dir, const mode_t mode) {
  323. char tmp[PATH_MAX_STRING_SIZE];
  324. char *p = NULL;
  325. struct stat sb;
  326. size_t len;
  327. /* copy path */
  328. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  329. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  330. return -1;
  331. }
  332. memcpy (tmp, dir, len);
  333. tmp[len] = '\0';
  334. /* remove trailing slash */
  335. if(tmp[len - 1] == '/') {
  336. tmp[len - 1] = '\0';
  337. }
  338. /* check if path exists and is a directory */
  339. if (stat (tmp, &sb) == 0) {
  340. if (S_ISDIR (sb.st_mode)) {
  341. return 0;
  342. }
  343. }
  344. /* recursive mkdir */
  345. for(p = tmp + 1; *p; p++) {
  346. if(*p == '/') {
  347. *p = 0;
  348. /* test path */
  349. if (stat(tmp, &sb) != 0) {
  350. /* path does not exist - create directory */
  351. if (mkdir(tmp, mode) < 0) {
  352. return -1;
  353. }
  354. } else if (!S_ISDIR(sb.st_mode)) {
  355. /* not a directory */
  356. return -1;
  357. }
  358. *p = '/';
  359. }
  360. }
  361. /* test path */
  362. if (stat(tmp, &sb) != 0) {
  363. /* path does not exist - create directory */
  364. if (mkdir(tmp, mode) < 0) {
  365. return -1;
  366. }
  367. } else if (!S_ISDIR(sb.st_mode)) {
  368. /* not a directory */
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. string toUpper(string in)
  374. {
  375. for (int i = 0; i < in.length(); ++i)
  376. in[i] = toupper(in[i]);
  377. return in;
  378. }
  379. string toLower(string in)
  380. {
  381. for (int i = 0; i < in.length(); ++i)
  382. in[i] = tolower(in[i]);
  383. return in;
  384. }
  385. // CPU Temp
  386. extern "C" uint8_t temprature_sens_read();
  387. float temperatureRead()
  388. {
  389. return (temprature_sens_read() - 32) / 1.8;
  390. }
  391. time_t addDays(time_t startTime, int days) {
  392. struct tm* tm = localtime(&startTime);
  393. tm->tm_mday += days;
  394. return mktime(tm);
  395. }
  396. int removeFolder(const char* folderPath, const char* logTag) {
  397. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  398. DIR *dir = opendir(folderPath);
  399. if (!dir) {
  400. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  401. return -1;
  402. }
  403. struct dirent *entry;
  404. int deleted = 0;
  405. while ((entry = readdir(dir)) != NULL) {
  406. std::string path = string(folderPath) + "/" + entry->d_name;
  407. if (entry->d_type == DT_REG) {
  408. if (unlink(path.c_str()) == 0) {
  409. deleted ++;
  410. } else {
  411. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  412. }
  413. } else if (entry->d_type == DT_DIR) {
  414. deleted += removeFolder(path.c_str(), logTag);
  415. }
  416. }
  417. closedir(dir);
  418. if (rmdir(folderPath) != 0) {
  419. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  420. }
  421. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  422. return deleted;
  423. }
  424. std::vector<string> HelperZerlegeZeile(std::string input, std::string _delimiter = "")
  425. {
  426. std::vector<string> Output;
  427. std::string delimiter = " =,";
  428. if (_delimiter.length() > 0){
  429. delimiter = _delimiter;
  430. }
  431. return ZerlegeZeile(input, delimiter);
  432. }
  433. std::vector<string> ZerlegeZeile(std::string input, std::string delimiter)
  434. {
  435. std::vector<string> Output;
  436. input = trim(input, delimiter);
  437. /* The input can have multiple formats:
  438. * - key = value
  439. * - key = value1 value2 value3 ...
  440. * - key value1 value2 value3 ...
  441. *
  442. * Examples:
  443. * - ImageSize = VGA
  444. * - IO0 = input disabled 10 false false
  445. * - main.dig1 28 144 55 100 false
  446. *
  447. * This causes issues eg. if a password key has a whitespace or equal sign in its value.
  448. * As a workaround and to not break any legacy usage, we enforce to only use the
  449. * equal sign, if the key is "password"
  450. */
  451. if (input.find("password") != string::npos) { // Line contains a password, use the equal sign as the only delimiter and only split on first occurrence
  452. size_t pos = input.find("=");
  453. Output.push_back(trim(input.substr(0, pos), ""));
  454. Output.push_back(trim(input.substr(pos +1, string::npos), ""));
  455. }
  456. else { // Legacy Mode
  457. size_t pos = findDelimiterPos(input, delimiter);
  458. std::string token;
  459. while (pos != std::string::npos) {
  460. token = input.substr(0, pos);
  461. token = trim(token, delimiter);
  462. Output.push_back(token);
  463. input.erase(0, pos + 1);
  464. input = trim(input, delimiter);
  465. pos = findDelimiterPos(input, delimiter);
  466. }
  467. Output.push_back(input);
  468. }
  469. return Output;
  470. }
  471. /* Source: https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git/tree/lsmmc.c */
  472. /* SD Card Manufacturer Database */
  473. struct SDCard_Manufacturer_database {
  474. string type;
  475. int id;
  476. string manufacturer;
  477. };
  478. /* Source: https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git/tree/lsmmc.c */
  479. /* SD Card Manufacturer Database */
  480. struct SDCard_Manufacturer_database database[] = {
  481. {
  482. .type = "sd",
  483. .id = 0x01,
  484. .manufacturer = "Panasonic",
  485. },
  486. {
  487. .type = "sd",
  488. .id = 0x02,
  489. .manufacturer = "Toshiba/Kingston/Viking",
  490. },
  491. {
  492. .type = "sd",
  493. .id = 0x03,
  494. .manufacturer = "SanDisk",
  495. },
  496. {
  497. .type = "sd",
  498. .id = 0x08,
  499. .manufacturer = "Silicon Power",
  500. },
  501. {
  502. .type = "sd",
  503. .id = 0x18,
  504. .manufacturer = "Infineon",
  505. },
  506. {
  507. .type = "sd",
  508. .id = 0x1b,
  509. .manufacturer = "Transcend/Samsung",
  510. },
  511. {
  512. .type = "sd",
  513. .id = 0x1c,
  514. .manufacturer = "Transcend",
  515. },
  516. {
  517. .type = "sd",
  518. .id = 0x1d,
  519. .manufacturer = "Corsair/AData",
  520. },
  521. {
  522. .type = "sd",
  523. .id = 0x1e,
  524. .manufacturer = "Transcend",
  525. },
  526. {
  527. .type = "sd",
  528. .id = 0x1f,
  529. .manufacturer = "Kingston",
  530. },
  531. {
  532. .type = "sd",
  533. .id = 0x27,
  534. .manufacturer = "Delkin/Phison",
  535. },
  536. {
  537. .type = "sd",
  538. .id = 0x28,
  539. .manufacturer = "Lexar",
  540. },
  541. {
  542. .type = "sd",
  543. .id = 0x30,
  544. .manufacturer = "SanDisk",
  545. },
  546. {
  547. .type = "sd",
  548. .id = 0x31,
  549. .manufacturer = "Silicon Power",
  550. },
  551. {
  552. .type = "sd",
  553. .id = 0x33,
  554. .manufacturer = "STMicroelectronics",
  555. },
  556. {
  557. .type = "sd",
  558. .id = 0x41,
  559. .manufacturer = "Kingston",
  560. },
  561. {
  562. .type = "sd",
  563. .id = 0x6f,
  564. .manufacturer = "STMicroelectronics",
  565. },
  566. {
  567. .type = "sd",
  568. .id = 0x74,
  569. .manufacturer = "Transcend",
  570. },
  571. {
  572. .type = "sd",
  573. .id = 0x76,
  574. .manufacturer = "Patriot",
  575. },
  576. {
  577. .type = "sd",
  578. .id = 0x82,
  579. .manufacturer = "Gobe/Sony",
  580. },
  581. {
  582. .type = "sd",
  583. .id = 0x89,
  584. .manufacturer = "Unknown",
  585. }
  586. };
  587. /* Parse SD Card Manufacturer Database */
  588. string SDCardParseManufacturerIDs(int id)
  589. {
  590. unsigned int id_cnt = sizeof(database) / sizeof(struct SDCard_Manufacturer_database);
  591. string ret_val = "";
  592. for (int i = 0; i < id_cnt; i++) {
  593. if (database[i].id == id) {
  594. return database[i].manufacturer;
  595. }
  596. else {
  597. ret_val = "ID unknown (not in DB)";
  598. }
  599. }
  600. return ret_val;
  601. }
  602. string RundeOutput(double _in, int _anzNachkomma)
  603. {
  604. std::stringstream stream;
  605. int _zw = _in;
  606. // ESP_LOGD(TAG, "AnzNachkomma: %d", _anzNachkomma);
  607. if (_anzNachkomma < 0) {
  608. _anzNachkomma = 0;
  609. }
  610. if (_anzNachkomma > 0)
  611. {
  612. stream << std::fixed << std::setprecision(_anzNachkomma) << _in;
  613. return stream.str();
  614. }
  615. else
  616. {
  617. stream << _zw;
  618. }
  619. return stream.str();
  620. }
  621. string getMac(void) {
  622. uint8_t macInt[6];
  623. char macFormated[6*2 + 5 + 1]; // AA:BB:CC:DD:EE:FF
  624. esp_read_mac(macInt, ESP_MAC_WIFI_STA);
  625. sprintf(macFormated, "%02X:%02X:%02X:%02X:%02X:%02X", macInt[0], macInt[1], macInt[2], macInt[3], macInt[4], macInt[5]);
  626. return macFormated;
  627. }
  628. string getResetReason(void) {
  629. std::string reasonText;
  630. switch(esp_reset_reason()) {
  631. case ESP_RST_POWERON: reasonText = "Power-on event"; break; //!< Reset due to power-on event
  632. case ESP_RST_EXT: reasonText = "External pin"; break; //!< Reset by external pin (not applicable for ESP32)
  633. case ESP_RST_SW: reasonText = "Via esp_restart"; break; //!< Software reset via esp_restart
  634. case ESP_RST_PANIC: reasonText = "Exception/panic"; break; //!< Software reset due to exception/panic
  635. case ESP_RST_INT_WDT: reasonText = "Interrupt watchdog"; break; //!< Reset (software or hardware) due to interrupt watchdog
  636. case ESP_RST_TASK_WDT: reasonText = "Task watchdog"; break; //!< Reset due to task watchdog
  637. case ESP_RST_WDT: reasonText = "Other watchdogs"; break; //!< Reset due to other watchdogs
  638. case ESP_RST_DEEPSLEEP: reasonText = "Exiting deep sleep mode"; break; //!< Reset after exiting deep sleep mode
  639. case ESP_RST_BROWNOUT: reasonText = "Brownout"; break; //!< Brownout reset (software or hardware)
  640. case ESP_RST_SDIO: reasonText = "SDIO"; break; //!< Reset over SDIO
  641. case ESP_RST_UNKNOWN: //!< Reset reason can not be determined
  642. default:
  643. reasonText = "Unknown";
  644. }
  645. return reasonText;
  646. }