xremote_storage.c 4.8 KB

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