quac_settings.c 7.7 KB

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