web_crawler_storage.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifndef WEB_CRAWLER_STORAGE_H
  2. #define WEB_CRAWLER_STORAGE_H
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #define SETTINGS_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/settings.bin"
  6. #define RECEIVED_DATA_PATH STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt"
  7. #define MAX_RECEIVED_DATA_SIZE 1024
  8. #define SHOW_MAX_FILE_SIZE 2048
  9. // Define the truncation notice
  10. #define TRUNCATION_NOTICE "\n\n[Data truncated due to size limits]"
  11. // Function to save settings: path, SSID, and password
  12. static void save_settings(const char *path, const char *ssid, const char *password)
  13. {
  14. // Create the directory for saving settings
  15. char directory_path[256];
  16. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler_app");
  17. // Create the directory
  18. Storage *storage = furi_record_open(RECORD_STORAGE);
  19. storage_common_mkdir(storage, directory_path);
  20. // Open the settings file
  21. File *file = storage_file_alloc(storage);
  22. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  23. {
  24. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  25. storage_file_free(file);
  26. furi_record_close(RECORD_STORAGE);
  27. return;
  28. }
  29. // Save the path length and data
  30. size_t path_length = strlen(path) + 1; // Include null terminator
  31. if (storage_file_write(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  32. storage_file_write(file, path, path_length) != path_length)
  33. {
  34. FURI_LOG_E(TAG, "Failed to write path");
  35. }
  36. // Save the SSID length and data
  37. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  38. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  39. storage_file_write(file, ssid, ssid_length) != ssid_length)
  40. {
  41. FURI_LOG_E(TAG, "Failed to write SSID");
  42. }
  43. // Save the password length and data
  44. size_t password_length = strlen(password) + 1; // Include null terminator
  45. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  46. storage_file_write(file, password, password_length) != password_length)
  47. {
  48. FURI_LOG_E(TAG, "Failed to write password");
  49. }
  50. FURI_LOG_I(TAG, "Settings saved: path=%s, ssid=%s, password=%s", path, ssid, password);
  51. storage_file_close(file);
  52. storage_file_free(file);
  53. furi_record_close(RECORD_STORAGE);
  54. }
  55. // Function to load settings: path, SSID, and password
  56. static bool load_settings(char *path, size_t path_size, char *ssid, size_t ssid_size, char *password, size_t password_size, WebCrawlerApp *app)
  57. {
  58. Storage *storage = furi_record_open(RECORD_STORAGE);
  59. File *file = storage_file_alloc(storage);
  60. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  61. {
  62. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  63. storage_file_free(file);
  64. furi_record_close(RECORD_STORAGE);
  65. return false; // Return false if the file does not exist
  66. }
  67. // Load the path
  68. size_t path_length;
  69. if (storage_file_read(file, &path_length, sizeof(size_t)) != sizeof(size_t) || path_length > path_size ||
  70. storage_file_read(file, path, path_length) != path_length)
  71. {
  72. FURI_LOG_E(TAG, "Failed to read path");
  73. storage_file_close(file);
  74. storage_file_free(file);
  75. furi_record_close(RECORD_STORAGE);
  76. return false;
  77. }
  78. path[path_length - 1] = '\0'; // Ensure null-termination
  79. // Load the SSID
  80. size_t ssid_length;
  81. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  82. storage_file_read(file, ssid, ssid_length) != ssid_length)
  83. {
  84. FURI_LOG_E(TAG, "Failed to read SSID");
  85. storage_file_close(file);
  86. storage_file_free(file);
  87. furi_record_close(RECORD_STORAGE);
  88. return false;
  89. }
  90. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  91. // Load the password
  92. size_t password_length;
  93. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  94. storage_file_read(file, password, password_length) != password_length)
  95. {
  96. FURI_LOG_E(TAG, "Failed to read password");
  97. storage_file_close(file);
  98. storage_file_free(file);
  99. furi_record_close(RECORD_STORAGE);
  100. return false;
  101. }
  102. password[password_length - 1] = '\0'; // Ensure null-termination
  103. // set the path, ssid, and password
  104. strncpy(app->path, path, path_size);
  105. strncpy(app->ssid, ssid, ssid_size);
  106. strncpy(app->password, password, password_size);
  107. storage_file_close(file);
  108. storage_file_free(file);
  109. furi_record_close(RECORD_STORAGE);
  110. return true;
  111. }
  112. static bool text_show_read_lines(File *file, FuriString *str_result)
  113. {
  114. // Reset the FuriString to ensure it's empty before reading
  115. furi_string_reset(str_result);
  116. // Define a buffer to hold the read data
  117. uint8_t buffer[SHOW_MAX_FILE_SIZE];
  118. // Read data into the buffer
  119. size_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
  120. if (storage_file_get_error(file) != FSE_OK)
  121. {
  122. FURI_LOG_E(TAG, "Error reading from file.");
  123. return false;
  124. }
  125. // Append each byte to the FuriString
  126. for (size_t i = 0; i < read_count; i++)
  127. {
  128. furi_string_push_back(str_result, buffer[i]);
  129. }
  130. return true;
  131. }
  132. static bool load_received_data()
  133. {
  134. if (!app_instance)
  135. {
  136. FURI_LOG_E(TAG, "App instance is NULL");
  137. return false;
  138. }
  139. if (!app_instance->textbox)
  140. {
  141. FURI_LOG_E(TAG, "Textbox is NULL");
  142. return false;
  143. }
  144. // Open the storage record
  145. Storage *storage = furi_record_open(RECORD_STORAGE);
  146. if (!storage)
  147. {
  148. FURI_LOG_E(TAG, "Failed to open storage record");
  149. return false;
  150. }
  151. // Allocate a file handle
  152. File *file = storage_file_alloc(storage);
  153. if (!file)
  154. {
  155. FURI_LOG_E(TAG, "Failed to allocate storage file");
  156. furi_record_close(RECORD_STORAGE);
  157. return false;
  158. }
  159. // Open the file for reading
  160. if (!storage_file_open(file, RECEIVED_DATA_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  161. {
  162. FURI_LOG_E(TAG, "Failed to open received data file for reading: %s", RECEIVED_DATA_PATH);
  163. storage_file_free(file);
  164. furi_record_close(RECORD_STORAGE);
  165. return false; // Return false if the file does not exist
  166. }
  167. // Allocate a FuriString to hold the received data
  168. FuriString *str_result = furi_string_alloc();
  169. if (!str_result)
  170. {
  171. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  172. storage_file_close(file);
  173. storage_file_free(file);
  174. furi_record_close(RECORD_STORAGE);
  175. return false;
  176. }
  177. // Read data into the FuriString
  178. bool read_success = text_show_read_lines(file, str_result);
  179. if (!read_success)
  180. {
  181. FURI_LOG_E(TAG, "Failed to read data from file");
  182. furi_string_free(str_result);
  183. storage_file_close(file);
  184. storage_file_free(file);
  185. furi_record_close(RECORD_STORAGE);
  186. return false;
  187. }
  188. // Check if there is more data beyond the maximum size
  189. char extra_byte;
  190. storage_file_read(file, &extra_byte, 1);
  191. // Retrieve the C-string from FuriString
  192. const char *data_cstr = furi_string_get_cstr(str_result);
  193. // Set the text box with the received data
  194. widget_reset(app_instance->textbox);
  195. FURI_LOG_D(TAG, "Received data: %s", data_cstr);
  196. if (str_result != NULL)
  197. {
  198. widget_add_text_scroll_element(
  199. app_instance->textbox,
  200. 0,
  201. 0,
  202. 128,
  203. 64, data_cstr);
  204. }
  205. else
  206. {
  207. widget_add_text_scroll_element(
  208. app_instance->textbox,
  209. 0,
  210. 0,
  211. 128,
  212. 64, "File is empty.");
  213. }
  214. // Clean up
  215. furi_string_free(str_result);
  216. storage_file_close(file);
  217. storage_file_free(file);
  218. furi_record_close(RECORD_STORAGE);
  219. return true;
  220. }
  221. #endif // WEB_CRAWLER_STORAGE_H