xremote_storage.c 4.8 KB

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