flip_weather_storage.c 3.1 KB

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