web_crawler_storage.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. // Function to save settings: path, SSID, and password
  5. static void save_settings(const char *path, const char *ssid, const char *password)
  6. {
  7. // Create the directory for saving settings
  8. char directory_path[256];
  9. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler_app");
  10. // Create the directory
  11. Storage *storage = furi_record_open(RECORD_STORAGE);
  12. storage_common_mkdir(storage, directory_path);
  13. // Open the settings file
  14. File *file = storage_file_alloc(storage);
  15. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  16. {
  17. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  18. storage_file_free(file);
  19. furi_record_close(RECORD_STORAGE);
  20. return;
  21. }
  22. // Save the path length and data
  23. size_t path_length = strlen(path) + 1; // Include null terminator
  24. if (storage_file_write(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  25. storage_file_write(file, path, path_length) != path_length)
  26. {
  27. FURI_LOG_E(TAG, "Failed to write path");
  28. }
  29. // Save the SSID length and data
  30. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  31. if (storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  32. storage_file_write(file, ssid, ssid_length) != ssid_length)
  33. {
  34. FURI_LOG_E(TAG, "Failed to write SSID");
  35. }
  36. // Save the password length and data
  37. size_t password_length = strlen(password) + 1; // Include null terminator
  38. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  39. storage_file_write(file, password, password_length) != password_length)
  40. {
  41. FURI_LOG_E(TAG, "Failed to write password");
  42. }
  43. FURI_LOG_I(TAG, "Settings saved: path=%s, ssid=%s, password=%s", path, ssid, password);
  44. storage_file_close(file);
  45. storage_file_free(file);
  46. furi_record_close(RECORD_STORAGE);
  47. }
  48. // Function to load settings: path, SSID, and password
  49. static bool load_settings(char *path, size_t path_size, char *ssid, size_t ssid_size, char *password, size_t password_size, WebCrawlerApp *app)
  50. {
  51. Storage *storage = furi_record_open(RECORD_STORAGE);
  52. File *file = storage_file_alloc(storage);
  53. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  54. {
  55. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  56. storage_file_free(file);
  57. furi_record_close(RECORD_STORAGE);
  58. return false; // Return false if the file does not exist
  59. }
  60. // Load the path
  61. size_t path_length;
  62. if (storage_file_read(file, &path_length, sizeof(size_t)) != sizeof(size_t) || path_length > path_size ||
  63. storage_file_read(file, path, path_length) != path_length)
  64. {
  65. FURI_LOG_E(TAG, "Failed to read path");
  66. storage_file_close(file);
  67. storage_file_free(file);
  68. furi_record_close(RECORD_STORAGE);
  69. return false;
  70. }
  71. path[path_length - 1] = '\0'; // Ensure null-termination
  72. // Load the SSID
  73. size_t ssid_length;
  74. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  75. storage_file_read(file, ssid, ssid_length) != ssid_length)
  76. {
  77. FURI_LOG_E(TAG, "Failed to read SSID");
  78. storage_file_close(file);
  79. storage_file_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. return false;
  82. }
  83. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  84. // Load the password
  85. size_t password_length;
  86. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  87. storage_file_read(file, password, password_length) != password_length)
  88. {
  89. FURI_LOG_E(TAG, "Failed to read password");
  90. storage_file_close(file);
  91. storage_file_free(file);
  92. furi_record_close(RECORD_STORAGE);
  93. return false;
  94. }
  95. password[password_length - 1] = '\0'; // Ensure null-termination
  96. FURI_LOG_I(TAG, "Settings loaded: path=%s, ssid=%s, password=%s", path, ssid, password);
  97. // set the path, ssid, and password
  98. strncpy(app->path, path, path_size);
  99. strncpy(app->ssid, ssid, ssid_size);
  100. strncpy(app->password, password, password_size);
  101. storage_file_close(file);
  102. storage_file_free(file);
  103. furi_record_close(RECORD_STORAGE);
  104. return true;
  105. }