Helper.cpp 21 KB

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