25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

136 lines
3.0 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include "esp_system.h"
  8. #include "spiffs/spiffs_vfs.h"
  9. #include "esp_log.h"
  10. static const char *TAG = "reader_storage";
  11. static const char* filename_stored = "stored.dat";
  12. static const char* filename_position = "position.dat";
  13. static int check_storage_ready()
  14. {
  15. if (spiffs_is_mounted == 0) {
  16. ESP_LOGE(TAG, "Reader storage is not mounted.");
  17. }
  18. return spiffs_is_mounted;
  19. }
  20. void reader_storage_get_filename(char* buf, int len)
  21. {
  22. if (check_storage_ready() == 0) {
  23. return;
  24. }
  25. strcpy(buf, SPIFFS_BASE_PATH);
  26. strcat(buf, "/");
  27. strcat(buf, filename_stored);
  28. }
  29. long reader_storage_get_length()
  30. {
  31. if (check_storage_ready() == 0) {
  32. return -1;
  33. }
  34. char path[64];
  35. reader_storage_get_filename(path, sizeof(path));
  36. long length = -1;
  37. struct stat stats;
  38. if (stat(path, &stats) == 0) {
  39. length = stats.st_size;
  40. }
  41. ESP_LOGI(TAG, "Getting length: %ld", length);
  42. return length;
  43. }
  44. void reader_storage_store_file(char* source_path)
  45. {
  46. if (check_storage_ready() == 0) {
  47. return;
  48. }
  49. char dest_path[64];
  50. reader_storage_get_filename(dest_path, sizeof(dest_path));
  51. ESP_LOGI(TAG, "Storing file %s into %s", source_path, dest_path);
  52. FILE* src = fopen(source_path, "r");
  53. if (src == NULL) {
  54. ESP_LOGE(TAG, "Failed opening source file.");
  55. return;
  56. }
  57. FILE* dst = fopen(dest_path, "w");
  58. if (dst == NULL) {
  59. ESP_LOGE(TAG, "Failed opening destination file.");
  60. return;
  61. }
  62. char buf[32];
  63. int len = 0;
  64. do {
  65. len = fread(buf, 1, sizeof(buf), src);
  66. if (len > 0) {
  67. len = fwrite(buf, 1, sizeof(buf), dst);
  68. }
  69. } while (len > 0);
  70. fclose(dst);
  71. fclose(src);
  72. ESP_LOGI(TAG, "File stored.");
  73. }
  74. long reader_storage_get_position()
  75. {
  76. if (check_storage_ready() == 0) {
  77. return -1;
  78. }
  79. long position = 0;
  80. char* path[64];
  81. strcpy(path, SPIFFS_BASE_PATH);
  82. strcat(path, "/");
  83. strcat(path, filename_position);
  84. FILE* f = fopen(path, "r");
  85. if (f == NULL) {
  86. ESP_LOGE(TAG, "Failed opening position file for reading.");
  87. return;
  88. }
  89. if (fread(&position, sizeof(position), 1, f) != 1) {
  90. ESP_LOGE(TAG, "Failed reading position.");
  91. position = -1;
  92. }
  93. fclose(f);
  94. ESP_LOGI(TAG, "Getting position: %ld", position);
  95. return position;
  96. }
  97. void reader_storage_set_position(long position)
  98. {
  99. if (check_storage_ready() == 0) {
  100. return;
  101. }
  102. ESP_LOGI(TAG, "Setting position: %ld", position);
  103. char* path[64];
  104. strcpy(path, SPIFFS_BASE_PATH);
  105. strcat(path, "/");
  106. strcat(path, filename_position);
  107. FILE* f = fopen(path, "w");
  108. if (f == NULL) {
  109. ESP_LOGE(TAG, "Failed opening position file for writing.");
  110. return;
  111. }
  112. if (fwrite(&position, sizeof(position), 1, f) != 1) {
  113. ESP_LOGE(TAG, "Failed writing position.");
  114. }
  115. fclose(f);
  116. }