Helper.cpp 19 KB

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