evil_portal_storage.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "evil_portal_storage.h"
  2. static Storage *evil_portal_open_storage() {
  3. return furi_record_open(RECORD_STORAGE);
  4. }
  5. static void evil_portal_close_storage() { furi_record_close(RECORD_STORAGE); }
  6. void evil_portal_read_index_html(void *context) {
  7. Evil_PortalApp *app = context;
  8. Storage *storage = evil_portal_open_storage();
  9. FileInfo fi;
  10. if (storage_common_stat(storage, EVIL_PORTAL_INDEX_SAVE_PATH, &fi) ==
  11. FSE_OK) {
  12. File *index_html = storage_file_alloc(storage);
  13. if (storage_file_open(index_html, EVIL_PORTAL_INDEX_SAVE_PATH, FSAM_READ,
  14. FSOM_OPEN_EXISTING)) {
  15. app->index_html = malloc((size_t)fi.size);
  16. uint8_t *buf_ptr = app->index_html;
  17. size_t read = 0;
  18. while (read < fi.size) {
  19. size_t to_read = fi.size - read;
  20. if (to_read > UINT16_MAX)
  21. to_read = UINT16_MAX;
  22. uint16_t now_read =
  23. storage_file_read(index_html, buf_ptr, (uint16_t)to_read);
  24. read += now_read;
  25. buf_ptr += now_read;
  26. }
  27. free(buf_ptr);
  28. }
  29. storage_file_close(index_html);
  30. storage_file_free(index_html);
  31. } else {
  32. char * html_error = "Something went wrong with reading the html file.\n"
  33. "Is the SD Card set up correctly?";
  34. app->index_html = (uint8_t *)html_error;
  35. }
  36. evil_portal_close_storage();
  37. }
  38. void evil_portal_read_ap_name(void *context) {
  39. Evil_PortalApp *app = context;
  40. Storage *storage = evil_portal_open_storage();
  41. FileInfo fi;
  42. if (storage_common_stat(storage, EVIL_PORTAL_AP_SAVE_PATH, &fi) == FSE_OK) {
  43. File *ap_name = storage_file_alloc(storage);
  44. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_READ,
  45. FSOM_OPEN_EXISTING)) {
  46. app->ap_name = malloc((size_t)fi.size);
  47. uint8_t *buf_ptr = app->ap_name;
  48. size_t read = 0;
  49. while (read < fi.size) {
  50. size_t to_read = fi.size - read;
  51. if (to_read > UINT16_MAX)
  52. to_read = UINT16_MAX;
  53. uint16_t now_read =
  54. storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  55. read += now_read;
  56. buf_ptr += now_read;
  57. }
  58. free(buf_ptr);
  59. }
  60. storage_file_close(ap_name);
  61. storage_file_free(ap_name);
  62. } else {
  63. char * app_default = "Evil Portal";
  64. app->ap_name = (uint8_t *)app_default;
  65. }
  66. evil_portal_close_storage();
  67. }
  68. char *sequential_file_resolve_path(Storage *storage, const char *dir,
  69. const char *prefix, const char *extension) {
  70. if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  71. return NULL;
  72. }
  73. char file_path[256];
  74. int file_index = 0;
  75. do {
  76. if (snprintf(file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix,
  77. file_index, extension) < 0) {
  78. return NULL;
  79. }
  80. file_index++;
  81. } while (storage_file_exists(storage, file_path));
  82. return strdup(file_path);
  83. }
  84. void write_logs(char *portal_logs) {
  85. Storage *storage = evil_portal_open_storage();
  86. char *seq_file_path = sequential_file_resolve_path(
  87. storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  88. File *file = storage_file_alloc(storage);
  89. if (storage_file_open(file, seq_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  90. storage_file_write(file, portal_logs, strlen(portal_logs));
  91. }
  92. storage_file_close(file);
  93. storage_file_free(file);
  94. evil_portal_close_storage();
  95. portal_logs = "";
  96. }