xremote_storage.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. flipper_format_write_uint32(fff_file, XREMOTE_SETTINGS_KEY_SG_TIMING, &app->sg_timing, 1);
  53. if(!flipper_format_rewind(fff_file)) {
  54. xremote_close_config_file(fff_file);
  55. FURI_LOG_E(TAG, "Rewind error");
  56. xremote_close_storage();
  57. return;
  58. }
  59. xremote_close_config_file(fff_file);
  60. xremote_close_storage();
  61. }
  62. void xremote_read_settings(void* context) {
  63. XRemote* app = context;
  64. Storage* storage = xremote_open_storage();
  65. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  66. if(storage_common_stat(storage, XREMOTE_SETTINGS_SAVE_PATH, NULL) != FSE_OK) {
  67. xremote_close_config_file(fff_file);
  68. xremote_close_storage();
  69. return;
  70. }
  71. uint32_t file_version;
  72. FuriString* temp_str = furi_string_alloc();
  73. if(!flipper_format_file_open_existing(fff_file, XREMOTE_SETTINGS_SAVE_PATH)) {
  74. FURI_LOG_E(TAG, "Cannot open file %s", XREMOTE_SETTINGS_SAVE_PATH);
  75. xremote_close_config_file(fff_file);
  76. xremote_close_storage();
  77. return;
  78. }
  79. if(!flipper_format_read_header(fff_file, temp_str, &file_version)) {
  80. FURI_LOG_E(TAG, "Missing Header Data");
  81. xremote_close_config_file(fff_file);
  82. xremote_close_storage();
  83. return;
  84. }
  85. if(file_version < XREMOTE_SETTINGS_FILE_VERSION) {
  86. FURI_LOG_I(TAG, "old config version, will be removed.");
  87. xremote_close_config_file(fff_file);
  88. xremote_close_storage();
  89. return;
  90. }
  91. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_HAPTIC, &app->haptic, 1);
  92. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_SPEAKER, &app->speaker, 1);
  93. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_LED, &app->led, 1);
  94. flipper_format_read_uint32(
  95. fff_file, XREMOTE_SETTINGS_KEY_SAVE_SETTINGS, &app->save_settings, 1);
  96. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_IR_TIMING, &app->ir_timing, 1);
  97. flipper_format_read_uint32(fff_file, XREMOTE_SETTINGS_KEY_SG_TIMING, &app->sg_timing, 1);
  98. flipper_format_rewind(fff_file);
  99. furi_string_free(temp_str);
  100. xremote_close_config_file(fff_file);
  101. xremote_close_storage();
  102. }