quac_settings.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "quac_settings.h"
  2. #include <flipper_format/flipper_format.h>
  3. // Quac Settings File Info
  4. // "/ext/apps_data/quac/.quac.conf"
  5. #define QUAC_SETTINGS_FILENAME QUAC_DATA_PATH "/.quac.conf"
  6. #define QUAC_SETTINGS_FILE_TYPE "Quac Settings File"
  7. #define QUAC_SETTINGS_FILE_VERSION 1
  8. void quac_set_default_settings(App* app) {
  9. app->settings.rfid_duration = 2500;
  10. app->settings.layout = QUAC_APP_LANDSCAPE;
  11. app->settings.show_icons = true;
  12. app->settings.show_headers = true;
  13. app->settings.subghz_use_ext_antenna = false;
  14. }
  15. void quac_load_settings(App* app) {
  16. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  17. FuriString* temp_str;
  18. temp_str = furi_string_alloc();
  19. uint32_t temp_data32 = 0;
  20. FURI_LOG_I(TAG, "SETTINGS: Reading: %s", QUAC_SETTINGS_FILENAME);
  21. bool successful = false;
  22. do {
  23. if(!flipper_format_file_open_existing(fff_settings, QUAC_SETTINGS_FILENAME)) {
  24. FURI_LOG_I(TAG, "SETTINGS: File not found, loading defaults");
  25. break;
  26. }
  27. if(!flipper_format_read_header(fff_settings, temp_str, &temp_data32)) {
  28. FURI_LOG_E(TAG, "SETTINGS: Missing or incorrect header");
  29. break;
  30. }
  31. if((!strcmp(furi_string_get_cstr(temp_str), QUAC_SETTINGS_FILE_TYPE)) &&
  32. (temp_data32 == QUAC_SETTINGS_FILE_VERSION)) {
  33. } else {
  34. FURI_LOG_E(TAG, "SETTINGS: Type or version mismatch");
  35. break;
  36. }
  37. // Now read actual values we care about
  38. if(!flipper_format_read_string(fff_settings, "Layout", temp_str)) {
  39. FURI_LOG_E(TAG, "SETTINGS: Missing Layout");
  40. break;
  41. }
  42. if(!strcmp(furi_string_get_cstr(temp_str), "Landscape")) {
  43. app->settings.layout = QUAC_APP_LANDSCAPE;
  44. } else if(!strcmp(furi_string_get_cstr(temp_str), "Portrait")) {
  45. app->settings.layout = QUAC_APP_PORTRAIT;
  46. } else {
  47. FURI_LOG_E(TAG, "SETTINGS: Invalid Layout");
  48. break;
  49. }
  50. if(!flipper_format_read_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  51. FURI_LOG_E(TAG, "SETTINGS: Missing 'Show Icons'");
  52. break;
  53. }
  54. app->settings.show_icons = (temp_data32 == 0) ? false : true;
  55. if(!flipper_format_read_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  56. FURI_LOG_E(TAG, "SETTINGS: Missing 'Show Headers'");
  57. break;
  58. }
  59. app->settings.show_headers = (temp_data32 == 0) ? false : true;
  60. if(!flipper_format_read_uint32(fff_settings, "RFID Duration", &temp_data32, 1)) {
  61. FURI_LOG_E(TAG, "SETTINGS: Missing 'RFID Duration'");
  62. break;
  63. }
  64. app->settings.rfid_duration = temp_data32;
  65. if(!flipper_format_read_uint32(fff_settings, "SubGHz Ext Antenna", &temp_data32, 1)) {
  66. FURI_LOG_E(TAG, "SETTINGS: Missing 'SubGHz Ext Antenna'");
  67. break;
  68. }
  69. app->settings.subghz_use_ext_antenna = (temp_data32 == 1) ? true : false;
  70. successful = true;
  71. } while(false);
  72. if(!successful) {
  73. quac_set_default_settings(app);
  74. }
  75. furi_string_free(temp_str);
  76. flipper_format_free(fff_settings);
  77. }
  78. void quac_save_settings(App* app) {
  79. FlipperFormat* fff_settings = flipper_format_file_alloc(app->storage);
  80. uint32_t temp_data32;
  81. FURI_LOG_I(TAG, "SETTINGS: Saving");
  82. bool successful = false;
  83. do {
  84. if(!flipper_format_file_open_always(fff_settings, QUAC_SETTINGS_FILENAME)) {
  85. FURI_LOG_E(TAG, "SETTINGS: Unable to open file for save!!");
  86. break;
  87. }
  88. if(!flipper_format_write_header_cstr(
  89. fff_settings, QUAC_SETTINGS_FILE_TYPE, QUAC_SETTINGS_FILE_VERSION)) {
  90. FURI_LOG_E(TAG, "SETTINGS: Failed writing file type and version");
  91. break;
  92. }
  93. // layout, icons, headers, duration
  94. if(!flipper_format_write_string_cstr(
  95. fff_settings,
  96. "Layout",
  97. app->settings.layout == QUAC_APP_LANDSCAPE ? "Landscape" : "Portrait")) {
  98. FURI_LOG_E(TAG, "SETTINGS: Failed to write Layout");
  99. break;
  100. }
  101. temp_data32 = app->settings.show_icons ? 1 : 0;
  102. if(!flipper_format_write_uint32(fff_settings, "Show Icons", &temp_data32, 1)) {
  103. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Icons'");
  104. break;
  105. }
  106. temp_data32 = app->settings.show_headers ? 1 : 0;
  107. if(!flipper_format_write_uint32(fff_settings, "Show Headers", &temp_data32, 1)) {
  108. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'Show Headers'");
  109. break;
  110. }
  111. if(!flipper_format_write_uint32(
  112. fff_settings, "RFID Duration", &app->settings.rfid_duration, 1)) {
  113. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'RFID Duration'");
  114. break;
  115. }
  116. temp_data32 = app->settings.subghz_use_ext_antenna ? 1 : 0;
  117. if(!flipper_format_write_uint32(fff_settings, "SubGHz Ext Antenna", &temp_data32, 1)) {
  118. FURI_LOG_E(TAG, "SETTINGS: Failed to write 'SubGHz Ext Antenna'");
  119. break;
  120. }
  121. successful = true;
  122. } while(false);
  123. if(!successful) {
  124. FURI_LOG_E(TAG, "SETTINGS: Failed to save settings!!");
  125. }
  126. flipper_format_file_close(fff_settings);
  127. flipper_format_free(fff_settings);
  128. }