flip_library_storage.h 3.3 KB

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