SDInterface.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. File SDInterface::getFile(String path) {
  62. if (this->supported) {
  63. File file = SD_MMC.open(path, FILE_READ);
  64. //if (file)
  65. return file;
  66. }
  67. }
  68. void SDInterface::listDir(String str_dir){
  69. if (this->supported) {
  70. File dir = SD_MMC.open(str_dir);
  71. while (true)
  72. {
  73. File entry = dir.openNextFile();
  74. if (! entry)
  75. {
  76. break;
  77. }
  78. //for (uint8_t i = 0; i < numTabs; i++)
  79. //{
  80. // Serial.print('\t');
  81. //}
  82. Serial.print(entry.name());
  83. Serial.print("\t");
  84. Serial.println(entry.size());
  85. entry.close();
  86. }
  87. }
  88. }
  89. void SDInterface::addPacket(uint8_t* buf, uint32_t len, bool log) {
  90. if ((this->supported) && (this->do_save)) {
  91. buffer_obj.addPacket(buf, len, log);
  92. }
  93. }
  94. void SDInterface::openCapture(String file_name) {
  95. bool save_pcap = settings_obj.loadSetting<bool>("SavePCAP");
  96. if ((this->supported) && (save_pcap)) {
  97. buffer_obj.createPcapFile(&SD_MMC, file_name);
  98. buffer_obj.open();
  99. }
  100. }
  101. void SDInterface::openLog(String file_name) {
  102. bool save_pcap = settings_obj.loadSetting<bool>("SavePCAP");
  103. if ((this->supported) && (save_pcap)) {
  104. buffer_obj.createPcapFile(&SD_MMC, file_name, true);
  105. buffer_obj.open(true);
  106. }
  107. }
  108. void SDInterface::runUpdate() {
  109. #ifdef HAS_SCREEN
  110. display_obj.tft.setTextWrap(false);
  111. display_obj.tft.setFreeFont(NULL);
  112. display_obj.tft.setCursor(0, 100);
  113. display_obj.tft.setTextSize(1);
  114. display_obj.tft.setTextColor(TFT_WHITE);
  115. display_obj.tft.println(F(text15));
  116. #endif
  117. File updateBin = SD_MMC.open("/update.bin");
  118. if (updateBin) {
  119. if(updateBin.isDirectory()){
  120. #ifdef HAS_SCREEN
  121. display_obj.tft.setTextColor(TFT_RED);
  122. display_obj.tft.println(F(text_table2[0]));
  123. #endif
  124. Serial.println(F("Error, could not find \"update.bin\""));
  125. #ifdef HAS_SCREEN
  126. display_obj.tft.setTextColor(TFT_WHITE);
  127. #endif
  128. updateBin.close();
  129. return;
  130. }
  131. size_t updateSize = updateBin.size();
  132. if (updateSize > 0) {
  133. #ifdef HAS_SCREEN
  134. display_obj.tft.println(F(text_table2[1]));
  135. #endif
  136. Serial.println(F("Starting update over SD. Please wait..."));
  137. this->performUpdate(updateBin, updateSize);
  138. }
  139. else {
  140. #ifdef HAS_SCREEN
  141. display_obj.tft.setTextColor(TFT_RED);
  142. display_obj.tft.println(F(text_table2[2]));
  143. #endif
  144. Serial.println(F("Error, file is empty"));
  145. #ifdef HAS_SCREEN
  146. display_obj.tft.setTextColor(TFT_WHITE);
  147. #endif
  148. return;
  149. }
  150. updateBin.close();
  151. // whe finished remove the binary from sd card to indicate end of the process
  152. #ifdef HAS_SCREEN
  153. display_obj.tft.println(F(text_table2[3]));
  154. #endif
  155. Serial.println(F("rebooting..."));
  156. //SD.remove("/update.bin");
  157. delay(1000);
  158. ESP.restart();
  159. }
  160. else {
  161. #ifdef HAS_SCREEN
  162. display_obj.tft.setTextColor(TFT_RED);
  163. display_obj.tft.println(F(text_table2[4]));
  164. #endif
  165. Serial.println(F("Could not load update.bin from sd root"));
  166. #ifdef HAS_SCREEN
  167. display_obj.tft.setTextColor(TFT_WHITE);
  168. #endif
  169. }
  170. }
  171. void SDInterface::performUpdate(Stream &updateSource, size_t updateSize) {
  172. if (Update.begin(updateSize)) {
  173. #ifdef HAS_SCREEN
  174. display_obj.tft.println(text_table2[5] + String(updateSize));
  175. display_obj.tft.println(F(text_table2[6]));
  176. #endif
  177. size_t written = Update.writeStream(updateSource);
  178. if (written == updateSize) {
  179. #ifdef HAS_SCREEN
  180. display_obj.tft.println(text_table2[7] + String(written) + text_table2[10]);
  181. #endif
  182. Serial.println("Written : " + String(written) + " successfully");
  183. }
  184. else {
  185. #ifdef HAS_SCREEN
  186. display_obj.tft.println(text_table2[8] + String(written) + "/" + String(updateSize) + text_table2[9]);
  187. #endif
  188. Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
  189. }
  190. if (Update.end()) {
  191. Serial.println("OTA done!");
  192. if (Update.isFinished()) {
  193. #ifdef HAS_SCREEN
  194. display_obj.tft.println(F(text_table2[11]));
  195. #endif
  196. Serial.println(F("Update successfully completed. Rebooting."));
  197. }
  198. else {
  199. #ifdef HAS_SCREEN
  200. display_obj.tft.setTextColor(TFT_RED);
  201. display_obj.tft.println(text_table2[12]);
  202. #endif
  203. Serial.println("Update not finished? Something went wrong!");
  204. #ifdef HAS_SCREEN
  205. display_obj.tft.setTextColor(TFT_WHITE);
  206. #endif
  207. }
  208. }
  209. else {
  210. #ifdef HAS_SCREEN
  211. display_obj.tft.println(text_table2[13] + String(Update.getError()));
  212. #endif
  213. Serial.println("Error Occurred. Error #: " + String(Update.getError()));
  214. }
  215. }
  216. else
  217. {
  218. #ifdef HAS_SCREEN
  219. display_obj.tft.println(text_table2[14]);
  220. #endif
  221. Serial.println("Not enough space to begin OTA");
  222. }
  223. }
  224. bool SDInterface::checkDetectPin() {
  225. /* #ifdef KIT
  226. if (digitalRead(SD_DET) == LOW)
  227. return true;
  228. else
  229. return false;
  230. #endif*/
  231. return false;
  232. }
  233. void SDInterface::main() {
  234. if ((this->supported) && (this->do_save)) {
  235. //Serial.println("Saving packet...");
  236. buffer_obj.forceSave(&SD_MMC);
  237. }
  238. else if (!this->supported) {
  239. if (checkDetectPin()) {
  240. delay(100);
  241. this->initSD();
  242. }
  243. }
  244. }