Helper.cpp 21 KB

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