SDInterface.cpp 6.2 KB

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