serial_console.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "serial_console.h"
  2. #include "radio.h"
  3. #include "rf_diag.h"
  4. #include "rf_link.h"
  5. #include "haptics.h"
  6. #include "bonds.h"
  7. #include "config.h"
  8. #include "fault_diag.h"
  9. #include <Adafruit_TinyUSB.h>
  10. #include <Arduino.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. // CDC commands: l=listen, s=stop, cN=channel, p<hex>=prefix, a<8hex>=base addr, b=bonds, etc.
  14. // Command order matters: else-if chain, FIRST matching letter wins. 'C' and 'H' appear twice; the second
  15. // occurrence is unreachable.
  16. void serialConsolePoll()
  17. {
  18. static char line[24];
  19. static uint8_t li = 0;
  20. while (Serial.available()) {
  21. char c = Serial.read();
  22. if (c == '\n' || c == '\r') {
  23. line[li] = 0;
  24. // FULL factory wipe -- requires the exact word "ERASE-ALL" so it can't be fat-fingered. Reformats the
  25. // internal FS (cfg.bin + bonds.bin gone), reboots into clean defaults; controller must be re-paired.
  26. if (!strcmp(line, "ERASE-ALL")) {
  27. Serial.println(
  28. "# ERASING ALL persistent storage (config + bonds)...");
  29. factoryErase();
  30. Serial.println(
  31. "# done -- rebooting into clean defaults; re-pair the controller");
  32. delay(40);
  33. faultDiagArmIntentionalReset();
  34. NVIC_SystemReset();
  35. } else if (line[0] == 'l')
  36. rfListenStart();
  37. else if (line[0] == 'B') {
  38. g_rfBeacon = !g_rfBeacon;
  39. g_rfListen = false;
  40. Serial.printf("# beacon %s ch%u\n",
  41. g_rfBeacon ? "ON" : "off",
  42. g_rfCh);
  43. } else if (line[0] == 'L') {
  44. g_plen = strtol(line + 1, 0, 16);
  45. Serial.printf("# plen=%02X\n", g_plen);
  46. } else if (line[0] == '1') {
  47. g_s1incl = !g_s1incl;
  48. Serial.printf("# s1incl=%u\n", g_s1incl);
  49. } else if (line[0] == 'R') {
  50. g_rfCh = atoi(line + 1);
  51. g_rfRaw = true;
  52. g_rfListen = false;
  53. g_rfBeacon = false;
  54. rfRawStart(g_rfCh);
  55. Serial.printf("# raw cap ch%u\n", g_rfCh);
  56. } else if (line[0] == 'Z') {
  57. g_rfRaw = true;
  58. g_rfSweep = true;
  59. g_rfListen = false;
  60. g_rfBeacon = false;
  61. g_rfCh = 2;
  62. rfRawStart(g_rfCh);
  63. Serial.println("# raw SWEEP");
  64. } else if (line[0] == 'H') {
  65. g_rfHost = !g_rfHost;
  66. g_rfBeacon = false;
  67. g_rfListen = false;
  68. g_rfRaw = false;
  69. g_rfSweep = false;
  70. g_rfCh = 2;
  71. Serial.printf(
  72. "# HOSTFRAME beacon %s base %02X%02X%02X%02X (slot0 uuids %02X%02X%02X%02X/%02X%02X%02X%02X)\n",
  73. g_rfHost ? "ON" : "off", g_rfBase[0],
  74. g_rfBase[1], g_rfBase[2], g_rfBase[3],
  75. g_slot[0].rec[0], g_slot[0].rec[1],
  76. g_slot[0].rec[2], g_slot[0].rec[3],
  77. g_slot[0].rec[4], g_slot[0].rec[5],
  78. g_slot[0].rec[6], g_slot[0].rec[7]);
  79. } else if (line[0] == 'C') {
  80. g_rfBeacon = g_rfListen = g_rfRaw = g_rfSweep =
  81. g_rfHost = false;
  82. rfRespondStart();
  83. } else if (line[0] == 's') {
  84. g_rfListen = false;
  85. g_rfBeacon = false;
  86. g_rfRaw = false;
  87. g_rfSweep = false;
  88. g_rfHost = false;
  89. g_rfRespond = false;
  90. NRF_RADIO->TASKS_DISABLE = 1;
  91. Serial.println("# RF off");
  92. }
  93. // switch USB mode: 0=steam 1=xbox 2=hori 3=lizard 4=swpro 5=ps5 6=hidgyro 7=ps5-game/clean 8=ds4-game/clean 9=ps3(dualshock3)
  94. else if (line[0] == 'x') {
  95. uint8_t m = strtoul(line + 1, 0, 10);
  96. if (modeValid(m)) {
  97. if (USBDevice.suspended()) {
  98. Serial.println(
  99. "# mode change blocked: host suspended");
  100. } else {
  101. Serial.printf(
  102. "# switch mode %u (reboot)\n",
  103. m);
  104. delay(20);
  105. saveMode(m);
  106. delay(40);
  107. faultDiagArmIntentionalReset();
  108. NVIC_SystemReset();
  109. }
  110. }
  111. } else if (line[0] == 'c') {
  112. g_rfCh = atoi(line + 1);
  113. Serial.printf("# ch=%u\n", g_rfCh);
  114. if (g_rfListen)
  115. rfListenStart();
  116. } else if (line[0] == 'p') {
  117. g_rfPrefix = strtol(line + 1, 0, 16);
  118. Serial.printf("# prefix=%02X\n", g_rfPrefix);
  119. if (g_rfListen)
  120. rfListenStart();
  121. } else if (line[0] == 'i') {
  122. g_crcinit = strtoul(line + 1, 0, 16);
  123. Serial.printf("# crcinit=%06lX\n",
  124. (unsigned long)g_crcinit);
  125. } else if (line[0] == 'w') {
  126. g_whiteiv = atoi(line + 1);
  127. Serial.printf("# whiteiv=%u\n", g_whiteiv);
  128. } else if (line[0] == '0') {
  129. g_pcnf0 = strtoul(line + 1, 0, 16);
  130. Serial.printf("# pcnf0=%08lX\n",
  131. (unsigned long)g_pcnf0);
  132. } else if (line[0] == '2') {
  133. g_pcnf1 = strtoul(line + 1, 0, 16);
  134. Serial.printf("# pcnf1=%08lX\n",
  135. (unsigned long)g_pcnf1);
  136. } else if (line[0] == 'M') {
  137. g_mode = strtoul(line + 1, 0, 16);
  138. Serial.printf(
  139. "# mode=%u (3=Ble1M,4=Ble2M,0/2=Nrf)\n",
  140. g_mode);
  141. } else if (line[0] == 'I') {
  142. g_crcinit = strtoul(line + 1, 0, 16);
  143. Serial.printf("# crcinit=%08lX\n",
  144. (unsigned long)g_crcinit);
  145. } else if (line[0] == 'P') {
  146. g_crcpoly = strtoul(line + 1, 0, 16);
  147. Serial.printf("# crcpoly=%lX\n",
  148. (unsigned long)g_crcpoly);
  149. } else if (line[0] == 'N') {
  150. g_crccnf = strtoul(line + 1, 0, 16);
  151. Serial.printf("# crccnf=%X\n", g_crccnf);
  152. } else if (line[0] == 'Y') {
  153. g_rfCap = true;
  154. g_rfCapOne = true;
  155. g_rfReplay = g_rfHost = g_rfAuto = false;
  156. g_rfCh = 2;
  157. rfCapStart(2);
  158. Serial.println(
  159. "# capture ONE frame for replay...");
  160. } else if (line[0] == 'y') {
  161. g_rfReplay = !g_rfReplay;
  162. g_rfCap = g_rfHost = g_rfAuto = false;
  163. Serial.printf("# REPLAY %s (%uB)\n",
  164. g_rfReplay ? "ON" : "off",
  165. g_replayLen);
  166. } else if (line[0] == 'T') {
  167. g_statlen = strtoul(line + 1, 0, 16);
  168. g_pcnf1 = 0;
  169. Serial.printf("# statlen=%02X\n", g_statlen);
  170. } else if (line[0] == 'k') {
  171. g_connOn = !g_connOn;
  172. if (g_connOn) {
  173. g_connSt = 0;
  174. g_connStep = 0;
  175. g_connPoll = 0;
  176. g_connF1 = 0;
  177. }
  178. Serial.printf(
  179. "# CONN mode %s: %s%s poll on ch%u (param=%02X). F1 seen=%lu\n",
  180. g_connOn ? "ON" : "off",
  181. g_e7announce ? "E7-awake[00 00] -> " :
  182. "",
  183. g_pollGet ? "E3+GET-report-0x45" :
  184. "bare E3",
  185. g_rfCh, g_getParam,
  186. (unsigned long)g_connF1);
  187. } else if (line[0] == 'q') {
  188. g_getParam = g_getParam ? 0x00 : 0x2D;
  189. Serial.printf("# GET-0x45 param=%02X\n",
  190. g_getParam);
  191. } else if (line[0] == 'v') {
  192. g_connVerbose = !g_connVerbose;
  193. Serial.printf(
  194. "# conn verbose %s (F1 seen=%lu)\n",
  195. g_connVerbose ? "ON" : "off",
  196. (unsigned long)g_connF1);
  197. } else if (line[0] == 'C') {
  198. g_sessCh = strtoul(line + 1, 0, 10);
  199. Serial.printf(
  200. "# session channel=%u (re-pair/reconnect to apply)\n",
  201. g_sessCh);
  202. } else if (line[0] == 'E') {
  203. g_mDiv = strtoul(line + 1, 0, 10);
  204. if (g_mDiv < 4)
  205. g_mDiv = 4;
  206. saveCfg();
  207. Serial.printf(
  208. "# xbox-mouse sensitivity divisor=%d (lower=faster)\n",
  209. g_mDiv);
  210. } else if (line[0] == 'F') {
  211. g_mFric = strtoul(line + 1, 0, 10);
  212. if (g_mFric > 99)
  213. g_mFric = 99;
  214. saveCfg();
  215. Serial.printf(
  216. "# xbox-mouse friction=%d%% (higher=more glide/momentum)\n",
  217. g_mFric);
  218. } else if (line[0] == 'W') {
  219. // A/B swap edits the ACTIVE emulated type (Xbox/Switch/DS4/DS5); puck modes have none.
  220. if (g_etype < ET_COUNT) {
  221. g_type[g_etype].abSwap =
  222. !g_type[g_etype].abSwap;
  223. applyActiveType();
  224. saveCfg();
  225. Serial.printf(
  226. "# A/B + X/Y swap %s (Nintendo layout) [type %u]\n",
  227. g_abSwap ? "ON" : "off",
  228. g_etype);
  229. } else
  230. Serial.println(
  231. "# A/B swap N/A in this mode (Steam/Lizard are not emulated types)");
  232. } else if (line[0] == 'K') {
  233. int i = line[1] - '0';
  234. uint8_t code = strtoul(line + 2, 0, 10);
  235. // back paddles edit the ACTIVE emulated type. code 18 = Capture/Screenshot (Switch only).
  236. if (i >= 0 && i < 4 && g_etype < ET_COUNT) {
  237. g_type[g_etype].back[i] = code;
  238. applyActiveType();
  239. saveCfg();
  240. Serial.printf(
  241. "# back[%d] (%s) -> code %u [type %u] [0=none 1=A 2=B 3=X 4=Y 5=LB 6=RB 7=L3 8=R3 9=Back 10=QAM 11=Guide 12=Dup 13=Ddown 14=Dleft 15=Dright 16=TouchClick 17=Mute 18=Capture(Switch) 19=LT/L2/ZL 20=RT/R2/ZR]\n",
  242. i,
  243. (const char *[]){ "L4", "R4",
  244. "L5",
  245. "R5" }[i],
  246. code, g_etype);
  247. } else if (g_etype >= ET_COUNT)
  248. Serial.println(
  249. "# back map N/A in this mode (Steam/Lizard are not emulated types)");
  250. else
  251. Serial.println(
  252. "# usage: K<0-3> <code> (0=L4 1=R4 2=L5 3=R5)");
  253. } else if (line[0] == 'Q') {
  254. uint8_t code = strtoul(line + 1, 0, 10);
  255. if (g_etype < ET_COUNT) {
  256. g_type[g_etype].qamMap = code;
  257. applyActiveType();
  258. saveCfg();
  259. Serial.printf(
  260. "# QAM -> code %u [type %u] [0=default 1=A 2=B 3=X 4=Y 5=LB 6=RB 7=L3 8=R3 9=Back 10=QAM 11=Guide 12=Dup 13=Ddown 14=Dleft 15=Dright 16=TouchClick 17=Mute 18=Capture(Switch) 19=LT/L2/ZL 20=RT/R2/ZR]\n",
  261. code, g_etype);
  262. } else
  263. Serial.println(
  264. "# QAM map N/A in this mode (Steam/Lizard are not emulated types)");
  265. } else if (line[0] == 'J') {
  266. char *sp = 0;
  267. uint8_t id = strtoul(line + 1, &sp, 0);
  268. // inject SET-SETTINGS to controller: report 0x87 [id][val u16 LE]
  269. uint16_t val = sp ? strtoul(sp, 0, 0) : 0;
  270. uint8_t pl[3] = { id, (uint8_t)(val & 0xFF),
  271. (uint8_t)(val >> 8) };
  272. // console-injected writes go to all connected controllers (no specific slot in scope)
  273. relayEnqueue(0x87, pl, 3, 0xFF);
  274. Serial.printf(
  275. "# queued SET-SETTINGS id=0x%02X val=%u (relay 0x87) — watch new=/s\n",
  276. id, val);
  277. } else if (line[0] == 'G') {
  278. g_sniff = !g_sniff;
  279. g_sniffPh = 0;
  280. g_rfHost = g_connOn = g_rfCap = g_rfAuto =
  281. g_rfReplay = false;
  282. if (g_sniff)
  283. rfSniffStart();
  284. Serial.printf(
  285. "# SESSION SNIFF %s (phase0: catch host frame on ibex/ch2; phase1 PARK ch=%s)\n",
  286. g_sniff ? "ON" : "off",
  287. g_sniffPark ? "override" : "primary");
  288. } else if (line[0] == 'g') {
  289. g_sniffPark = strtoul(line + 1, 0, 10);
  290. if (g_sniffPh == 1)
  291. rfSniffStart();
  292. Serial.printf(
  293. "# sniff park channel=%u (0=learned primary)\n",
  294. g_sniffPark);
  295. } else if (line[0] == 'V') {
  296. g_e7b = strtoul(line + 1, 0, 10);
  297. Serial.printf(
  298. "# E7 protocol-version B-byte=%u (0=awake/slow, 1=try v1-fast; watch new=/s; revert if input stops)\n",
  299. g_e7b);
  300. } else if (line[0] == 'O') {
  301. g_relayOp = strtoul(line + 1, 0, 16);
  302. Serial.printf("# relay opcode=%02X\n",
  303. g_relayOp);
  304. } else if (line[0] == 'o') {
  305. g_relaySub = strtoul(line + 1, 0, 16);
  306. Serial.printf("# relay sub-type=%02X\n",
  307. g_relaySub);
  308. } else if (line[0] == 'h') {
  309. uint8_t nc = strtoul(line + 1, 0, 10);
  310. Serial.printf(
  311. "# HOP %u->%u (advertise on current ch, then poll new). Watch F1=/s\n",
  312. g_sessCh, nc);
  313. rfHopTo(nc);
  314. } else if (line[0] == 'z') {
  315. g_qos = !g_qos;
  316. g_hopIdx = 0;
  317. g_qosBad = 0;
  318. g_qosCheckMs = millis();
  319. Serial.printf(
  320. "# QoS adaptive channel hopping %s (candidates 18,46,76,22,68)\n",
  321. g_qos ? "ON" : "off");
  322. } else if (line[0] == 'U') {
  323. // Per-slot debug dump: which controllers are connected, their RSSI, haptic-block
  324. // state, and session address. Quick at-a-glance view for multi-controller testing.
  325. Serial.printf("# session ch%u, curSlot=%d\n",
  326. g_sessCh, g_curSlot);
  327. for (int s = 0; s < NSLOT; s++) {
  328. bool live = g_slot[s].used &&
  329. g_connReplyMs[s] != 0;
  330. unsigned long age =
  331. live ? (unsigned long)(millis() -
  332. g_connReplyMs
  333. [s]) :
  334. 0;
  335. int rssi =
  336. g_linkRssi[s] ?
  337. (int)g_linkRssi[s] -
  338. RSSI_DBM_OFFSET :
  339. 0;
  340. unsigned long now_ms = millis();
  341. unsigned long blk =
  342. (g_hapticBlockUntil[s] &&
  343. (int32_t)(g_hapticBlockUntil[s] -
  344. now_ms) > 0) ?
  345. (g_hapticBlockUntil[s] -
  346. now_ms) :
  347. 0;
  348. Serial.printf(
  349. "# slot %d: %s addr %02X%02X%02X%02X/%02X rssi=%ddBm conn=%lums ago batt=%u%% haptic-block=%lums uuid %02X%02X%02X%02X %02X%02X%02X%02X\n",
  350. s,
  351. g_slot[s].used ?
  352. (live ? "LIVE" :
  353. "BONDED") :
  354. "empty",
  355. g_sessBase[s][0],
  356. g_sessBase[s][1],
  357. g_sessBase[s][2],
  358. g_sessBase[s][3],
  359. g_sessPrefix[s], rssi, age,
  360. g_battery[s], blk,
  361. g_slot[s].rec[0],
  362. g_slot[s].rec[1],
  363. g_slot[s].rec[2],
  364. g_slot[s].rec[3],
  365. g_slot[s].rec[4],
  366. g_slot[s].rec[5],
  367. g_slot[s].rec[6],
  368. g_slot[s].rec[7]);
  369. }
  370. } else if (line[0] == 'r') {
  371. // poll RX-window is FIXED (not configurable) -- report only.
  372. Serial.printf(
  373. "# poll RX-window=%lu us (FIXED; poll rate caps ~%lu/s)\n",
  374. (unsigned long)g_rxWin,
  375. (unsigned long)(1000000 / g_rxWin));
  376. } else if (line[0] == 'e') {
  377. g_e3mode = strtoul(line + 1, 0, 10);
  378. Serial.printf(
  379. "# E3 poll PID mode=%u (0=fixed07, 1=cyclePID+noack1, 2=cyclePID+noack0) - watch new=/s\n",
  380. g_e3mode);
  381. } else if (line[0] == 'd') {
  382. g_pollGet = !g_pollGet;
  383. Serial.printf(
  384. "# poll = %s (real puck sends BARE E3) - watch F1=/s new=/s\n",
  385. g_pollGet ?
  386. "E3 + GET-report-0x45 TLV (legacy)" :
  387. "bare E3");
  388. } else if (line[0] == 'n') {
  389. g_e7announce = !g_e7announce;
  390. Serial.printf(
  391. "# E7 awake-announce %s (real puck never sends E7) - watch F1=/s\n",
  392. g_e7announce ? "ON (legacy)" : "off");
  393. } else if (line[0] == 'm') {
  394. g_e1keepalive = !g_e1keepalive;
  395. Serial.printf(
  396. "# session-channel E1 keepalive %s (real puck sends none; needed for shared addr) - watch F1=/s\n",
  397. g_e1keepalive ? "ON" : "off");
  398. } else if (line[0] == 't') {
  399. // inject n test haptics (output 0x82 [01 01 F7]) over the relay
  400. uint8_t n = line[1] ? strtoul(line + 1, 0, 10) :
  401. 40;
  402. g_testHaptic = n;
  403. Serial.printf(
  404. "# test-haptic burst x%u queued (relay 0x82 01 01 F7 via op=%02X sub=%02X)\n",
  405. n, g_relayOp, g_relaySub);
  406. }
  407. // dump the captured OUTPUT-report history (oldest->newest)
  408. else if (line[0] == 'H') {
  409. hapticDumpLog();
  410. } else if (line[0] == 'j') {
  411. g_connType = strtoul(line + 1, 0, 16);
  412. Serial.printf("# connType=%02X\n", g_connType);
  413. } else if (line[0] == 'Q') {
  414. g_connLen = strtoul(line + 1, 0, 16);
  415. Serial.printf("# connLen=%02X\n", g_connLen);
  416. } else if (line[0] == 'A') {
  417. g_balen = strtoul(line + 1, 0, 16);
  418. g_pcnf1 = 0;
  419. Serial.printf("# balen=%u\n", g_balen);
  420. } else if (line[0] == 'a') {
  421. uint32_t v = strtoul(line + 1, 0, 16);
  422. g_rfBase[0] = v >> 24;
  423. g_rfBase[1] = v >> 16;
  424. g_rfBase[2] = v >> 8;
  425. g_rfBase[3] = v;
  426. Serial.printf("# base=%08lX\n",
  427. (unsigned long)v);
  428. if (g_rfListen)
  429. rfListenStart();
  430. } else if (line[0] == 'b') {
  431. for (int i = 0; i < NSLOT; i++)
  432. if (g_slot[i].used) {
  433. Serial.printf("slot%d ", i);
  434. for (int j = 0; j < 24; j++)
  435. Serial.printf(
  436. "%02X",
  437. g_slot[i].rec[j]);
  438. Serial.println();
  439. }
  440. } else if (line[0] == 'D') {
  441. armDebugCdcNextBoot();
  442. Serial.println(
  443. "# debug-CDC armed: reset to KEEP the serial console one more boot (auto-reverts after; the wake mouse is dropped that boot)");
  444. } else if (line[0] == 'X') {
  445. g_rfCap = !g_rfCap;
  446. g_rfHost = g_rfAuto = g_rfBeacon = g_rfListen =
  447. g_rfRaw = g_rfSweep = g_rfRespond =
  448. false;
  449. g_rfCh = 2;
  450. g_capV = 0;
  451. if (g_rfCap)
  452. rfCapStart(g_rfCh);
  453. Serial.printf(
  454. "# CAPTURE(listen) %s ch%u on \"ibex\", cycling 1M/2M x BALEN3/4 (use cN to set ch)\n",
  455. g_rfCap ? "ON" : "off", g_rfCh);
  456. } else if (line[0] == 'S') {
  457. g_rfAuto = !g_rfAuto;
  458. g_rfHost = g_rfAuto;
  459. g_rfBeacon = g_rfListen = g_rfRaw = g_rfSweep =
  460. false;
  461. g_rfCh = 2;
  462. g_cfgIdx = 0;
  463. if (g_rfAuto)
  464. applyCfg(0);
  465. Serial.printf("# AUTOSWEEP %s\n",
  466. g_rfAuto ? "ON" : "off");
  467. }
  468. li = 0;
  469. } else if (li < sizeof line - 1)
  470. line[li++] = c;
  471. }
  472. }