config.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "config.h"
  2. #include "rf_link.h" // g_rxWin (poll RX window persisted here)
  3. #include "haptics.h" // g_hapticBlockOn, g_hapticBlockMs
  4. #include <Adafruit_LittleFS.h>
  5. #include <InternalFileSystem.h>
  6. #include <string.h>
  7. #include <stddef.h> // offsetof (CFG_LEN_MIN, the short-file accept threshold)
  8. using namespace Adafruit_LittleFS_Namespace;
  9. uint8_t g_usbMode = 0;
  10. bool g_xbox = false;
  11. uint8_t g_chordBtn[3] = {
  12. MODE_LIZARD, MODE_XBOX, MODE_SW_PRO
  13. }; // back4+B/X/Y -> these modes (A always STEAM); Y defaults to Switch Pro
  14. // back4+D-pad (left/up/right/down). Defaults are the console personalities that ship WITHOUT a config
  15. // interface -- those modes can't be entered from the panel-less side any other way, and back4+A still returns
  16. // to Steam. Configurable like g_chordBtn (WebUSB fields 34..37).
  17. uint8_t g_chordDpad[4] = { MODE_PS3, MODE_DS4_GAME, MODE_PS5_GAME,
  18. MODE_SW_HORI };
  19. bool g_persistMode = false;
  20. uint8_t g_bootMode = 0xFF;
  21. bool g_debugCdcThisBoot = false;
  22. // persisted one-shot arm, stored in Cfg.rsvd0 (1 = keep CDC for the next boot)
  23. static uint8_t g_debugCdc = 0;
  24. int g_mDiv = 64, g_mFric = 94;
  25. // Per-type button config. back default {5,6,7,8} = L4->LB R4->RB L5->L3 R5->R3 (0..11 buttons, 12..15 D-pad,
  26. // 16/17 PS touch/mute, 18 Switch Capture). Switch differs: QAM defaults to Capture(18), A/B swap on, and
  27. // trackpad haptics off. qamMap 0 = unmapped (hardcoded per-mode behavior). ledBright 0 = no override.
  28. // rumble 1 = enabled (default), 0 = host rumble silenced for that type.
  29. TypeCfg g_type[ET_COUNT] = {
  30. /* ET_XBOX */ { { 5, 6, 7, 8 }, 0, 0, 1, 0, 1 },
  31. /* ET_SWITCH */ { { 5, 6, 7, 8 }, 18, 1, 0, 0, 1 },
  32. /* ET_DS4 */ { { 5, 6, 7, 8 }, 0, 0, 1, 0, 1 },
  33. /* ET_DS5 */ { { 5, 6, 7, 8 }, 0, 0, 1, 0, 1 },
  34. };
  35. uint8_t g_etype = ET_NONE;
  36. // Live mirrors of the active type (puck modes use the harmless defaults below).
  37. uint8_t g_abSwap = 0;
  38. uint8_t g_back[4] = { 5, 6, 7, 8 };
  39. uint8_t g_qamMap = 0;
  40. uint8_t g_padHaptics = 1;
  41. uint8_t g_rumble = 1;
  42. uint8_t g_ledBright = 0;
  43. void applyActiveType()
  44. {
  45. g_etype = etypeForMode(g_usbMode);
  46. if (g_etype >=
  47. ET_COUNT) { // puck mode (Steam/Lizard): no remap, haptics on
  48. g_back[0] = 5;
  49. g_back[1] = 6;
  50. g_back[2] = 7;
  51. g_back[3] = 8;
  52. g_qamMap = 0;
  53. g_abSwap = 0;
  54. g_padHaptics = 1;
  55. g_rumble = 1;
  56. g_ledBright = 0;
  57. return;
  58. }
  59. const TypeCfg &t = g_type[g_etype];
  60. for (int i = 0; i < 4; i++)
  61. g_back[i] = t.back[i];
  62. g_qamMap = t.qamMap;
  63. g_abSwap = t.abSwap;
  64. g_padHaptics = t.padHaptics;
  65. g_rumble = t.rumble;
  66. g_ledBright = t.ledBright;
  67. }
  68. // rumble strength % (200 = double); adjustable from the WebUSB panel
  69. uint8_t g_rumbleScale = 200;
  70. // poll rate defaults to POLL_US_DEFAULT (250 Hz), matching the real Valve puck (see config.h). The
  71. // delivered report rate equals the poll rate (fresh IMU in every reply). Live-adjustable via console
  72. // "PR<hz>" for on-HW sweeps; session-only, so any rate persisted by an older build is ignored and boot
  73. // always starts at the default (see loadCfg).
  74. uint32_t g_pollUs = POLL_US_DEFAULT;
  75. #define CFG_FILE "/cfg.bin"
  76. // Struct layout/semantics changed (TypeCfg gained rumble byte); bump so old flash format is discarded ->
  77. // clean defaults once.
  78. #define CFG_MAGIC 0xCF
  79. struct Cfg {
  80. uint8_t magic, mode, mDiv, mFric, rsvd0, pollU100, persistMode,
  81. bootMode, chordBtn[3], rumbleScale;
  82. // rxWin10: legacy RF tunable slot (window now fixed; ignored). lizKeep: the id9=0 hold enable (see
  83. // haptics.h LIZKEEP_MS). landAll87: the verbatim-0x87-relay experiment toggle (haptics.h g_landAll87).
  84. uint8_t rxWin10, lizKeep, landAll87;
  85. TypeCfg type[ET_COUNT]; // per-emulated-type back/qam/abSwap/padHaptics
  86. // TAIL (appended after CFG_MAGIC 0xCF shipped): back4+D-pad mode assignments. New tail fields go HERE, at
  87. // the end, and loadCfg accepts a short file so an upgrade keeps every existing setting -- see CFG_LEN_MIN.
  88. uint8_t chordDpad[4];
  89. }; // rsvd0 = ex-padSmooth, now the one-shot debug-CDC arm
  90. // Shortest cfg.bin we still accept: the layout as of CFG_MAGIC 0xCF, i.e. everything before the appended tail.
  91. // A file that stops anywhere in the tail leaves those bytes at the 0xFF prefill loadCfg() applies, which every
  92. // tail field treats as "unset" and replaces with its default.
  93. #define CFG_LEN_MIN (offsetof(struct Cfg, chordDpad))
  94. void saveCfg()
  95. {
  96. Cfg c = { CFG_MAGIC,
  97. g_usbMode,
  98. (uint8_t)g_mDiv,
  99. (uint8_t)g_mFric,
  100. g_debugCdc,
  101. (uint8_t)(g_pollUs / 100),
  102. (uint8_t)(g_persistMode ? 1 : 0),
  103. g_bootMode,
  104. { g_chordBtn[0], g_chordBtn[1], g_chordBtn[2] },
  105. g_rumbleScale,
  106. (uint8_t)(g_rxWin / 10),
  107. g_lizKeep,
  108. g_landAll87,
  109. {},
  110. { g_chordDpad[0], g_chordDpad[1], g_chordDpad[2],
  111. g_chordDpad[3] } };
  112. for (int i = 0; i < ET_COUNT; i++)
  113. c.type[i] = g_type[i];
  114. InternalFS.remove(CFG_FILE);
  115. File f(InternalFS);
  116. if (f.open(CFG_FILE, FILE_O_WRITE)) {
  117. f.write((uint8_t *)&c, sizeof c);
  118. f.close();
  119. }
  120. }
  121. void loadCfg()
  122. {
  123. Cfg c;
  124. // 0xFF prefill: bytes a SHORT (pre-tail) cfg.bin never wrote stay 0xFF, which is not a valid mode/flag, so
  125. // each tail field below falls back to its compiled default instead of reading whatever was on the stack.
  126. memset(&c, 0xFF, sizeof c);
  127. File f(InternalFS);
  128. bool consume = false;
  129. if (f.open(CFG_FILE, FILE_O_READ)) {
  130. int got = f.read((uint8_t *)&c, sizeof c);
  131. // Accept a file that is short only in the appended tail (>= CFG_LEN_MIN): an upgrade from a build
  132. // predating the tail keeps mode/paddles/chords instead of silently reverting to factory defaults.
  133. // Anything shorter, or a stale magic, is a real layout change -> discard and use defaults.
  134. if (got >= (int)CFG_LEN_MIN && c.magic == CFG_MAGIC) {
  135. g_mDiv = c.mDiv ? c.mDiv : 64;
  136. g_mFric = c.mFric;
  137. for (int i = 0; i < ET_COUNT; i++)
  138. g_type[i] = c.type[i];
  139. g_persistMode = c.persistMode ? true : false;
  140. // one-shot debug-CDC (Cfg.rsvd0): honor for THIS boot, then consume so the next boot reverts to normal.
  141. g_debugCdcThisBoot = c.rsvd0 ? true : false;
  142. if (c.rsvd0) {
  143. g_debugCdc = 0;
  144. consume = true;
  145. }
  146. // poll rate is fixed; rewrite cfg so the persisted byte matches the new default.
  147. if (c.pollU100 != (uint8_t)(POLL_US_DEFAULT / 100))
  148. consume = true;
  149. // boot-mode policy: a one-shot bootMode (explicit switch when !persist) wins once then clears;
  150. // otherwise persist->last mode, else->Steam.
  151. if (c.bootMode != 0xFF) {
  152. g_usbMode = modeValid(c.bootMode) ? c.bootMode :
  153. 0;
  154. consume = true;
  155. } else
  156. g_usbMode = g_persistMode ? (modeValid(c.mode) ?
  157. c.mode :
  158. 0) :
  159. 0;
  160. static const uint8_t CHORD_DEF[3] = { MODE_LIZARD,
  161. MODE_XBOX,
  162. MODE_SW_PRO };
  163. for (int i = 0; i < 3; i++)
  164. g_chordBtn[i] = modeValid(c.chordBtn[i]) ?
  165. c.chordBtn[i] :
  166. CHORD_DEF[i];
  167. // D-pad chords: 0xFF (short pre-tail file) or any invalid mode keeps the compiled default.
  168. for (int i = 0; i < 4; i++)
  169. if (modeValid(c.chordDpad[i]))
  170. g_chordDpad[i] = c.chordDpad[i];
  171. // grow a short file to the current layout on the next save
  172. if (got < (int)sizeof c)
  173. consume = true;
  174. // 0 is a valid setting (rumble off)
  175. g_rumbleScale = c.rumbleScale;
  176. // lizard-suppression keepalive enable (0/1; anything else = a pre-0xCE cfg leaked
  177. // through -> keep the on default)
  178. if (c.lizKeep <= 1)
  179. g_lizKeep = c.lizKeep;
  180. // verbatim-0x87-relay experiment toggle (0/1; default off)
  181. if (c.landAll87 <= 1)
  182. g_landAll87 = c.landAll87;
  183. // The poll RX window is now FIXED (g_rxWin is const) -- any persisted rxWin10 is ignored.
  184. }
  185. f.close();
  186. }
  187. // resolve the active emulated type's settings into the live mirrors the mode builders read
  188. applyActiveType();
  189. // clear the one-shot so the NEXT cold boot reverts to the default/persist policy
  190. if (consume) {
  191. g_bootMode = 0xFF;
  192. saveCfg();
  193. }
  194. }
  195. void saveMode(uint8_t m)
  196. {
  197. if (g_persistMode) {
  198. g_usbMode = m;
  199. g_bootMode = 0xFF;
  200. } else {
  201. g_bootMode = m;
  202. }
  203. saveCfg();
  204. }
  205. void armDebugCdcNextBoot()
  206. {
  207. g_debugCdc = 1;
  208. saveCfg();
  209. } // next boot keeps CDC; loadCfg() consumes it after
  210. // FULL factory wipe: reformat the internal LittleFS, erasing cfg.bin (modes/tunables/chords) AND bonds.bin
  211. // (paired-controller record). Caller reboots: next boot finds no files and falls back to clean defaults, and
  212. // the controller must be re-paired. Irreversible -- gated behind explicit confirmation at every call site.
  213. void factoryErase()
  214. {
  215. // ensure mounted before we reformat (no-op if already up)
  216. InternalFS.begin();
  217. InternalFS.format();
  218. }
  219. // One-time factory reset for the -DOPK_FACTORY_RESET recovery build: clear a bad config/bond ONCE (first boot
  220. // after flashing) then persist normally. "Already reset" is tracked by a tag file holding the build's git hash,
  221. // written AFTER the wipe (so it survives in the freshly-formatted FS):
  222. // - tag missing or != this build's hash -> wipe, then stamp the tag. Next boot persists.
  223. // - tag == this build's hash -> already reset for this build: skip, boot normally.
  224. // Keying the tag to the git hash means flashing a DIFFERENT build re-triggers the wipe. buildTag is OPK_GIT_HASH.
  225. #define RESET_TAG_FILE "/rsttag"
  226. void factoryResetOnce(const char *buildTag)
  227. {
  228. char tag[24] = { 0 };
  229. {
  230. File f(InternalFS);
  231. if (f.open(RESET_TAG_FILE, FILE_O_READ)) {
  232. int n = f.read((uint8_t *)tag, sizeof tag - 1);
  233. if (n > 0)
  234. tag[n] = 0;
  235. f.close();
  236. }
  237. }
  238. if (strncmp(tag, buildTag, sizeof tag - 1) == 0)
  239. return; // this build already did its one-time reset -> persist
  240. factoryErase(); // wipe cfg.bin + bonds.bin + the old tag
  241. InternalFS.begin(); // remount the fresh FS
  242. File g(InternalFS); // stamp the tag so subsequent boots skip the wipe
  243. if (g.open(RESET_TAG_FILE, FILE_O_WRITE)) {
  244. g.write((const uint8_t *)buildTag, strlen(buildTag));
  245. g.close();
  246. }
  247. }