xremote_storage.c 5.2 KB

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