SDInterface.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "SDInterface.h"
  2. #include "lang_var.h"
  3. bool SDInterface::initSD() {
  4. String display_string = "";
  5. /*#ifdef KIT
  6. pinMode(SD_DET, INPUT);
  7. if (digitalRead(SD_DET) == LOW) {
  8. Serial.println(F("SD Card Detect Pin Detected"));
  9. }
  10. else {
  11. Serial.println(F("SD Card Detect Pin Not Detected"));
  12. this->supported = false;
  13. return false;
  14. }
  15. #endif
  16. pinMode(SD_CS, OUTPUT);
  17. delay(10);*/
  18. if (!SD_MMC.begin("/sdcard", true, false, SDMMC_FREQ_DEFAULT)) {
  19. Serial.println(F("Failed to mount SD Card"));
  20. this->supported = false;
  21. return false;
  22. }
  23. uint8_t cardType = SD_MMC.cardType();
  24. if (cardType == CARD_NONE) {
  25. Serial.println("No MicroSD Card found");
  26. this->supported = false;
  27. return false;
  28. } else {
  29. this->supported = true;
  30. this->cardType = cardType;
  31. //if (cardType == CARD_MMC)
  32. // Serial.println(F("SD: MMC Mounted"));
  33. //else if(cardType == CARD_SD)
  34. // Serial.println(F("SD: SDSC Mounted"));
  35. //else if(cardType == CARD_SDHC)
  36. // Serial.println(F("SD: SDHC Mounted"));
  37. //else
  38. // Serial.println(F("SD: UNKNOWN Card Mounted"));
  39. this->cardSizeMB = SD_MMC.cardSize() / (1024 * 1024);
  40. //Serial.printf("SD Card Size: %lluMB\n", this->cardSizeMB);
  41. if (this->supported) {
  42. const int NUM_DIGITS = log10(this->cardSizeMB) + 1;
  43. char sz[NUM_DIGITS + 1];
  44. sz[NUM_DIGITS] = 0;
  45. for (size_t i = NUM_DIGITS; i--; this->cardSizeMB /= 10) {
  46. sz[i] = '0' + (this->cardSizeMB % 10);
  47. display_string.concat((String)sz[i]);
  48. }
  49. this->card_sz = sz;
  50. }
  51. buffer_obj = Buffer();
  52. if (!SD_MMC.exists("/SCRIPTS")) {
  53. Serial.println("/SCRIPTS does not exist. Creating...");
  54. SD_MMC.mkdir("/SCRIPTS");
  55. Serial.println("/SCRIPTS created");
  56. }
  57. return true;
  58. }
  59. }
  60. void SDInterface::addPacket(uint8_t* buf, uint32_t len) {
  61. if ((this->supported) && (this->do_save)) {
  62. buffer_obj.addPacket(buf, len);
  63. }
  64. }
  65. void SDInterface::openCapture(String file_name) {
  66. if (this->supported)
  67. buffer_obj.createPcapFile(&SD_MMC, file_name);
  68. buffer_obj.open();
  69. }
  70. void SDInterface::runUpdate() {
  71. #ifdef HAS_SCREEN
  72. display_obj.tft.setTextWrap(false);
  73. display_obj.tft.setFreeFont(NULL);
  74. display_obj.tft.setCursor(0, 100);
  75. display_obj.tft.setTextSize(1);
  76. display_obj.tft.setTextColor(TFT_WHITE);
  77. display_obj.tft.println(F(text15));
  78. #endif
  79. File updateBin = SD_MMC.open("/update.bin");
  80. if (updateBin) {
  81. if(updateBin.isDirectory()){
  82. #ifdef HAS_SCREEN
  83. display_obj.tft.setTextColor(TFT_RED);
  84. display_obj.tft.println(F(text_table2[0]));
  85. #endif
  86. Serial.println(F("Error, could not find \"update.bin\""));
  87. #ifdef HAS_SCREEN
  88. display_obj.tft.setTextColor(TFT_WHITE);
  89. #endif
  90. updateBin.close();
  91. return;
  92. }
  93. size_t updateSize = updateBin.size();
  94. if (updateSize > 0) {
  95. #ifdef HAS_SCREEN
  96. display_obj.tft.println(F(text_table2[1]));
  97. #endif
  98. Serial.println(F("Starting update over SD. Please wait..."));
  99. this->performUpdate(updateBin, updateSize);
  100. }
  101. else {
  102. #ifdef HAS_SCREEN
  103. display_obj.tft.setTextColor(TFT_RED);
  104. display_obj.tft.println(F(text_table2[2]));
  105. #endif
  106. Serial.println(F("Error, file is empty"));
  107. #ifdef HAS_SCREEN
  108. display_obj.tft.setTextColor(TFT_WHITE);
  109. #endif
  110. return;
  111. }
  112. updateBin.close();
  113. // whe finished remove the binary from sd card to indicate end of the process
  114. #ifdef HAS_SCREEN
  115. display_obj.tft.println(F(text_table2[3]));
  116. #endif
  117. Serial.println(F("rebooting..."));
  118. //SD.remove("/update.bin");
  119. delay(1000);
  120. ESP.restart();
  121. }
  122. else {
  123. #ifdef HAS_SCREEN
  124. display_obj.tft.setTextColor(TFT_RED);
  125. display_obj.tft.println(F(text_table2[4]));
  126. #endif
  127. Serial.println(F("Could not load update.bin from sd root"));
  128. #ifdef HAS_SCREEN
  129. display_obj.tft.setTextColor(TFT_WHITE);
  130. #endif
  131. }
  132. }
  133. void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
  134. if (Update.begin(updateSize)) {
  135. #ifdef HAS_SCREEN
  136. display_obj.tft.println(text_table2[5] + String(updateSize));
  137. display_obj.tft.println(F(text_table2[6]));
  138. #endif
  139. size_t written = Update.writeStream(updateSource);
  140. if (written == updateSize) {
  141. #ifdef HAS_SCREEN
  142. display_obj.tft.println(text_table2[7] + String(written) + text_table2[10]);
  143. #endif
  144. Serial.println("Written : " + String(written) + " successfully");
  145. }
  146. else {
  147. #ifdef HAS_SCREEN
  148. display_obj.tft.println(text_table2[8] + String(written) + "/" + String(updateSize) + text_table2[9]);
  149. #endif
  150. Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  151. }
  152. if (Update.end()) {
  153. Serial.println("OTA done!");
  154. if (Update.isFinished()) {
  155. #ifdef HAS_SCREEN
  156. display_obj.tft.println(F(text_table2[11]));
  157. #endif
  158. Serial.println(F("Update successfully completed. Rebooting."));
  159. }
  160. else {
  161. #ifdef HAS_SCREEN
  162. display_obj.tft.setTextColor(TFT_RED);
  163. display_obj.tft.println(text_table2[12]);
  164. #endif
  165. Serial.println("Update not finished? Something went wrong!");
  166. #ifdef HAS_SCREEN
  167. display_obj.tft.setTextColor(TFT_WHITE);
  168. #endif
  169. }
  170. }
  171. else {
  172. #ifdef HAS_SCREEN
  173. display_obj.tft.println(text_table2[13] + String(Update.getError()));
  174. #endif
  175. Serial.println("Error Occurred. Error #: " + String(Update.getError()));
  176. }
  177. }
  178. else
  179. {
  180. #ifdef HAS_SCREEN
  181. display_obj.tft.println(text_table2[14]);
  182. #endif
  183. Serial.println("Not enough space to begin OTA");
  184. }
  185. }
  186. bool SDInterface::checkDetectPin() {
  187. /* #ifdef KIT
  188. if (digitalRead(SD_DET) == LOW)
  189. return true;
  190. else
  191. return false;
  192. #endif*/
  193. return false;
  194. }
  195. void SDInterface::main() {
  196. if ((this->supported) && (this->do_save)) {
  197. //Serial.println("Saving packet...");
  198. buffer_obj.forceSave(&SD_MMC);
  199. }
  200. else if (!this->supported) {
  201. if (checkDetectPin()) {
  202. delay(100);
  203. this->initSD();
  204. }
  205. }
  206. }