sdcard_check.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. int iCRCMessage = 0;
  16. FILE *pFile = fopen("/sdcard/sdcheck.txt", "w");
  17. if (pFile == NULL)
  18. {
  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. {
  24. std::string sMessage = "This message is used for a SD-Card basic check!";
  25. iCRCMessage = esp_rom_crc16_le(0, (uint8_t *)sMessage.c_str(), sMessage.length());
  26. if (fwrite(sMessage.c_str(), sMessage.length(), 1, pFile) == 0)
  27. {
  28. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E2) Not able to write file");
  29. fclose(pFile);
  30. unlink("/sdcard/sdcheck.txt");
  31. return -2;
  32. }
  33. fclose(pFile);
  34. }
  35. pFile = fopen("/sdcard/sdcheck.txt", "r");
  36. if (pFile == NULL)
  37. {
  38. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E3) Not able to open file to read back");
  39. unlink("/sdcard/sdcheck.txt");
  40. return -3;
  41. }
  42. else
  43. {
  44. char cReadBuf[50];
  45. if (fgets(cReadBuf, sizeof(cReadBuf), pFile) == 0)
  46. {
  47. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E4) Not able to read file back");
  48. fclose(pFile);
  49. unlink("/sdcard/sdcheck.txt");
  50. return -4;
  51. }
  52. else
  53. {
  54. if (esp_rom_crc16_le(0, (uint8_t *)cReadBuf, strlen(cReadBuf)) != iCRCMessage)
  55. {
  56. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E5) Read back, but wrong CRC");
  57. fclose(pFile);
  58. unlink("/sdcard/sdcheck.txt");
  59. return -5;
  60. }
  61. }
  62. fclose(pFile);
  63. }
  64. if (unlink("/sdcard/sdcheck.txt") != 0)
  65. {
  66. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Basic R/W check: (E6) Unable to delete the file");
  67. return -6;
  68. }
  69. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Basic R/W check successful");
  70. return 0;
  71. }
  72. bool SDCardCheckFolderFilePresence()
  73. {
  74. struct stat sb;
  75. bool bRetval = true;
  76. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Folder/file presence check started...");
  77. /* check if folder exists: config */
  78. if (stat("/sdcard/config", &sb) != 0)
  79. {
  80. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /config not found");
  81. bRetval = false;
  82. }
  83. /* check if folder exists: html */
  84. if (stat("/sdcard/html", &sb) != 0)
  85. {
  86. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /html not found");
  87. bRetval = false;
  88. }
  89. /* check if folder exists: firmware */
  90. if (stat("/sdcard/firmware", &sb) != 0)
  91. {
  92. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /firmware not found");
  93. bRetval = false;
  94. }
  95. /* check if folder exists: img_tmp */
  96. if (stat("/sdcard/img_tmp", &sb) != 0)
  97. {
  98. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /img_tmp not found");
  99. bRetval = false;
  100. }
  101. /* check if folder exists: log */
  102. if (stat("/sdcard/log", &sb) != 0)
  103. {
  104. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /log not found");
  105. bRetval = false;
  106. }
  107. /* check if folder exists: demo */
  108. if (stat("/sdcard/demo", &sb) != 0)
  109. {
  110. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: Folder /demo not found");
  111. bRetval = false;
  112. }
  113. /* check if file exists: wlan.ini */
  114. if ((stat("/sdcard/wlan.ini", &sb) != 0) && (stat("/sdcard/network.ini", &sb) != 0))
  115. {
  116. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /wlan.ini not found");
  117. bRetval = false;
  118. }
  119. /* check if file exists: config.ini */
  120. if (stat("/sdcard/config/config.ini", &sb) != 0)
  121. {
  122. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /config/config.ini not found");
  123. bRetval = false;
  124. }
  125. /* check if file exists: index.html */
  126. if ((stat("/sdcard/html/index.html", &sb) != 0) && (stat("/sdcard/html/index.html.gz", &sb) != 0))
  127. {
  128. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/index.html not found");
  129. bRetval = false;
  130. }
  131. /* check if file exists: overview.html */
  132. if ((stat("/sdcard/html/overview.html", &sb) != 0) && (stat("/sdcard/html/overview.html.gz", &sb) != 0))
  133. {
  134. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/overview.html not found");
  135. bRetval = false;
  136. }
  137. /* check if file exists: ota.html */
  138. if ((stat("/sdcard/html/ota_page.html", &sb) != 0) && (stat("/sdcard/html/ota_page.html.gz", &sb) != 0))
  139. {
  140. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/ota.html not found");
  141. bRetval = false;
  142. }
  143. /* check if file exists: log.html */
  144. if ((stat("/sdcard/html/log.html", &sb) != 0) && (stat("/sdcard/html/log.html.gz", &sb) != 0))
  145. {
  146. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/log.html not found");
  147. bRetval = false;
  148. }
  149. /* check if file exists: common.js */
  150. if ((stat("/sdcard/html/common.js", &sb) != 0) && (stat("/sdcard/html/common.js.gz", &sb) != 0))
  151. {
  152. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/common.js not found");
  153. bRetval = false;
  154. }
  155. /* check if file exists: readconfigcommon.js */
  156. if ((stat("/sdcard/html/readconfigcommon.js", &sb) != 0) && (stat("/sdcard/html/readconfigcommon.js.gz", &sb) != 0))
  157. {
  158. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/readconfigcommon.js not found");
  159. bRetval = false;
  160. }
  161. /* check if file exists: readconfigparam.js */
  162. if ((stat("/sdcard/html/readconfigparam.js", &sb) != 0) && (stat("/sdcard/html/readconfigparam.js.gz", &sb) != 0))
  163. {
  164. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/readconfigparam.js not found");
  165. bRetval = false;
  166. }
  167. /* check if file exists: jquery-3.6.0.min.js */
  168. if ((stat("/sdcard/html/jquery-3.6.0.min.js", &sb) != 0) && (stat("/sdcard/html/jquery-3.6.0.min.js.gz", &sb) != 0))
  169. {
  170. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/jquery-3.6.0.min.js not found");
  171. bRetval = false;
  172. }
  173. /* check if file exists: firework.js */
  174. if ((stat("/sdcard/html/firework.js", &sb) != 0) && (stat("/sdcard/html/firework.js.gz", &sb) != 0))
  175. {
  176. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/firework.js not found");
  177. bRetval = false;
  178. }
  179. /* check if file exists: version.txt */
  180. if (stat("/sdcard/html/version.txt", &sb) != 0)
  181. {
  182. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Folder/file check: File /html/version.txt not found");
  183. // bRetval = false;
  184. }
  185. if (bRetval)
  186. {
  187. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Folder/file presence check successful");
  188. }
  189. return bRetval;
  190. }