flip_library_storage.c 3.1 KB

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