serial_console.cpp 14 KB

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