sdcard_check.cpp 5.5 KB

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