Helper.cpp 21 KB

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