Helper.cpp 21 KB

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