Helper.cpp 21 KB

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