xremote_storage.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "xremote_storage.h"
  2. static Storage* xremote_open_storage() {
  3. return furi_record_open(RECORD_STORAGE);
  4. }
  5. static void xremote_close_storage() {
  6. furi_record_close(RECORD_STORAGE);
  7. }
  8. static void xremote_close_config_file(FlipperFormat* file) {
  9. if(file == NULL) return;
  10. flipper_format_file_close(file);
  11. flipper_format_free(file);
  12. }
  13. void xremote_save_settings(void* context) {
  14. XRemote* app = context;
  15. if(app->save_settings == 0) {
  16. return;
  17. }
  18. FURI_LOG_D(TAG, "Saving Settings");
  19. Storage* storage = xremote_open_storage();
  20. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  21. // Overwrite wont work, so delete first
  22. if(storage_file_exists(storage, XREMOTE_SETTINGS_SAVE_PATH)) {
  23. storage_simply_remove(storage, XREMOTE_SETTINGS_SAVE_PATH);
  24. }
  25. // Open File, create if not exists
  26. if(!storage_common_stat(storage, XREMOTE_SETTINGS_SAVE_PATH, NULL) == FSE_OK) {
  27. FURI_LOG_D(
  28. TAG, "Config file %s is not found. Will create new.", XREMOTE_SETTINGS_SAVE_PATH);
  29. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  30. FURI_LOG_D(
  31. TAG, "Directory %s doesn't exist. Will create new.", CONFIG_FILE_DIRECTORY_PATH);
  32. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  33. FURI_LOG_E(TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  34. }
  35. }
  36. }
  37. if(!flipper_format_file_open_new(fff_file, XREMOTE_SETTINGS_SAVE_PATH)) {
  38. //totp_close_config_file(fff_file);
  39. FURI_LOG_E(TAG, "Error creating new file %s", XREMOTE_SETTINGS_SAVE_PATH);
  40. xremote_close_storage();
  41. return;
  42. }
  43. // Store Settings
  44. flipper_format_write_header_cstr(
  45. fff_file, XREMOTE_SETTINGS_HEADER, XREMOTE_SETTINGS_FILE_VERSION);
  46. flipper_format_write_uint32(fff_file, XREMOTE_SETTINGS_KEY_HAPTIC, &app->haptic, 1);
  47. flipper_format_write_uint32(fff_file, XREMOTE_SETTINGS_KEY_SPEAKER, &app->speaker, 1);
  48. flipper_format_write_uint32(fff_file, XREMOTE_SETTINGS_KEY_LED, &app->led, 1);
  49. flipper_format_write_uint32(
  50. fff_file, XREMOTE_SETTINGS_KEY_SAVE_SETTINGS, &app->save_settings, 1);
  51. flipper_format_write_uint32(fff_file, XREMOTE_SETTINGS_KEY_IR_TIMING, &app->ir_timing, 1);
  52. if(!flipper_format_rewind(fff_file)) {
  53. xremote_close_config_file(fff_file);
  54. FURI_LOG_E(TAG, "Rewind error");
  55. xremote_close_storage();
  56. return;
  57. }
  58. xremote_close_config_file(fff_file);
  59. xremote_close_storage();
  60. }
  61. void xremote_read_settings(void* context) {
  62. XRemote* app = context;
  63. Storage* storage = xremote_open_storage();
  64. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  65. if(storage_common_stat(storage, XREMOTE_SETTINGS_SAVE_PATH, NULL) != FSE_OK) {
  66. xremote_close_config_file(fff_file);
  67. xremote_close_storage();
  68. return;
  69. }
  70. uint32_t file_version;
  71. FuriString* temp_str = furi_string_alloc();
  72. if(!flipper_format_file_open_existing(fff_file, XREMOTE_SETTINGS_SAVE_PATH)) {
  73. FURI_LOG_E(TAG, "Cannot open file %s", XREMOTE_SETTINGS_SAVE_PATH);
  74. xremote_close_config_file(fff_file);
  75. xremote_close_storage();
  76. return;
  77. }
  78. if(!flipper_format_read_header(fff_file, temp_str, &file_version)) {
  79. FURI_LOG_E(TAG, "Missing Header Data");
  80. xremote_close_config_file(fff_file);
  81. xremote_close_storage();
  82. return;
  83. }
  84. if(file_version < XREMOTE_SETTINGS_FILE_VERSION) {
  85. FURI_LOG_I(TAG, "old config version, will be removed.");
  86. xremote_close_config_file(fff_file);
  87. xremote_close_storage();
  88. return;
  89. }
  90. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_HAPTIC, &app->haptic, 1);
  91. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_SPEAKER, &app->speaker, 1);
  92. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_LED, &app->led, 1);
  93. flipper_format_read_uint32(
  94. fff_file, XREMOTE_SETTINGS_KEY_SAVE_SETTINGS, &app->save_settings, 1);
  95. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_IR_TIMING, &app->ir_timing, 1);
  96. flipper_format_rewind(fff_file);
  97. furi_string_free(temp_str);
  98. xremote_close_config_file(fff_file);
  99. xremote_close_storage();
  100. }