sdcard_check.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "sdcard_check.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <inttypes.h>
  7. #include <sys/stat.h>
  8. #include "esp_rom_crc.h"
  9. #include "ClassLogFile.h"
  10. static const char *TAG = "SDCARD";
  11. int SDCardCheckRW(void)
  12. {
  13. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Basic R/W check started...");
  14. FILE* pFile = NULL;
  15. int iCRCMessage = 0;
  16. pFile = fopen("/sdcard/sdcheck.txt","w");
  17. if (pFile == NULL) {
  18. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E1) No able to open file to write");
  19. return -1;
  20. }
  21. else {
  22. std::string sMessage = "This message is used for a SD-Card basic check!";
  23. iCRCMessage = esp_rom_crc16_le(0, (uint8_t*)sMessage.c_str(), sMessage.length());
  24. if (fwrite(sMessage.c_str(), sMessage.length(), 1, pFile) == 0 ) {
  25. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E2) Not able to write file");
  26. fclose(pFile);
  27. unlink("/sdcard/sdcheck.txt");
  28. return -2;
  29. }
  30. fclose(pFile);
  31. }
  32. pFile = fopen("/sdcard/sdcheck.txt","r");
  33. if (pFile == NULL) {
  34. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E3) Not able to open file to read back");
  35. unlink("/sdcard/sdcheck.txt");
  36. return -3;
  37. }
  38. else {
  39. char cReadBuf[50];
  40. if (fgets(cReadBuf, sizeof(cReadBuf), pFile) == 0) {
  41. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E4) Not able to read file back");
  42. fclose(pFile);
  43. unlink("/sdcard/sdcheck.txt");
  44. return -4;
  45. }
  46. else {
  47. if (esp_rom_crc16_le(0, (uint8_t*)cReadBuf, strlen(cReadBuf)) != iCRCMessage) {
  48. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E5) Read back, but wrong CRC");
  49. fclose(pFile);
  50. unlink("/sdcard/sdcheck.txt");
  51. return -5;
  52. }
  53. }
  54. fclose(pFile);
  55. }
  56. if (unlink("/sdcard/sdcheck.txt") != 0) {
  57. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E6) Unable to delete the file");
  58. return -6;
  59. }
  60. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Basic R/W check successful");
  61. return 0;
  62. }
  63. bool SDCardCheckFolderFilePresence()
  64. {
  65. struct stat sb;
  66. bool bRetval = true;
  67. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Folder/file presence check started...");
  68. /* check if folder exists: config */
  69. if (stat("/sdcard/config", &sb) != 0) {
  70. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /config not found");
  71. bRetval = false;
  72. }
  73. /* check if folder exists: html */
  74. if (stat("/sdcard/html", &sb) != 0) {
  75. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /html not found");
  76. bRetval = false;
  77. }
  78. /* check if folder exists: firmware */
  79. if (stat("/sdcard/firmware", &sb) != 0) {
  80. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /firmware not found");
  81. bRetval = false;
  82. }
  83. /* check if folder exists: img_tmp */
  84. if (stat("/sdcard/img_tmp", &sb) != 0) {
  85. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /img_tmp not found");
  86. bRetval = false;
  87. }
  88. /* check if folder exists: log */
  89. if (stat("/sdcard/log", &sb) != 0) {
  90. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /log not found");
  91. bRetval = false;
  92. }
  93. /* check if folder exists: demo */
  94. if (stat("/sdcard/demo", &sb) != 0) {
  95. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /demo not found");
  96. bRetval = false;
  97. }
  98. /* check if file exists: wlan.ini */
  99. if (stat("/sdcard/wlan.ini", &sb) != 0) {
  100. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /wlan.ini not found");
  101. bRetval = false;
  102. }
  103. /* check if file exists: config.ini */
  104. if (stat("/sdcard/config/config.ini", &sb) != 0) {
  105. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /config/config.ini not found");
  106. bRetval = false;
  107. }
  108. /* check if file exists: index.html */
  109. if (stat("/sdcard/html/index.html", &sb) != 0) {
  110. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/index.html not found");
  111. bRetval = false;
  112. }
  113. /* check if file exists: ota.html */
  114. if (stat("/sdcard/html/ota_page.html", &sb) != 0) {
  115. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/ota.html not found");
  116. bRetval = false;
  117. }
  118. /* check if file exists: log.html */
  119. if (stat("/sdcard/html/log.html", &sb) != 0) {
  120. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/log.html not found");
  121. bRetval = false;
  122. }
  123. /* check if file exists: common.js */
  124. if (stat("/sdcard/html/common.js", &sb) != 0) {
  125. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/common.js not found");
  126. bRetval = false;
  127. }
  128. /* check if file exists: gethost.js */
  129. if (stat("/sdcard/html/gethost.js", &sb) != 0) {
  130. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/gethost.js not found");
  131. bRetval = false;
  132. }
  133. /* check if file exists: version.txt */
  134. if (stat("/sdcard/html/version.txt", &sb) != 0) {
  135. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/version.txt not found");
  136. bRetval = false;
  137. }
  138. if (bRetval)
  139. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Folder/file presence check successful");
  140. return bRetval;
  141. }