flip_library_storage.h 3.3 KB

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