quac_settings.c 6.7 KB

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