serial_console.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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: %s%s poll on ch%u (param=%02X). F1 seen=%lu\n",
  177. g_connOn ? "ON" : "off",
  178. g_e7announce ? "E7-awake[00 00] -> " : "",
  179. g_pollGet ? "E3+GET-report-0x45" :
  180. "bare E3",
  181. g_rfCh, g_getParam,
  182. (unsigned long)g_connF1);
  183. } else if (line[0] == 'q') {
  184. g_getParam = g_getParam ? 0x00 : 0x2D;
  185. Serial.printf("# GET-0x45 param=%02X\n",
  186. g_getParam);
  187. } else if (line[0] == 'v') {
  188. g_connVerbose = !g_connVerbose;
  189. Serial.printf(
  190. "# conn verbose %s (F1 seen=%lu)\n",
  191. g_connVerbose ? "ON" : "off",
  192. (unsigned long)g_connF1);
  193. } else if (line[0] == 'C') {
  194. g_sessCh = strtoul(line + 1, 0, 10);
  195. Serial.printf(
  196. "# session channel=%u (re-pair/reconnect to apply)\n",
  197. g_sessCh);
  198. } else if (line[0] == 'E') {
  199. g_mDiv = strtoul(line + 1, 0, 10);
  200. if (g_mDiv < 4)
  201. g_mDiv = 4;
  202. saveCfg();
  203. Serial.printf(
  204. "# xbox-mouse sensitivity divisor=%d (lower=faster)\n",
  205. g_mDiv);
  206. } else if (line[0] == 'F') {
  207. g_mFric = strtoul(line + 1, 0, 10);
  208. if (g_mFric > 99)
  209. g_mFric = 99;
  210. saveCfg();
  211. Serial.printf(
  212. "# xbox-mouse friction=%d%% (higher=more glide/momentum)\n",
  213. g_mFric);
  214. } else if (line[0] == 'W') {
  215. g_abSwap = !g_abSwap;
  216. saveCfg();
  217. Serial.printf(
  218. "# A/B + X/Y swap %s (Nintendo layout)\n",
  219. g_abSwap ? "ON" : "off");
  220. } else if (line[0] == 'K') {
  221. int i = line[1] - '0';
  222. uint8_t code = strtoul(line + 2, 0, 10);
  223. if (i >= 0 && i < 4) {
  224. g_back[i] = code;
  225. saveCfg();
  226. Serial.printf(
  227. "# 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",
  228. i,
  229. (const char *[]){ "L4", "R4",
  230. "L5",
  231. "R5" }[i],
  232. code);
  233. } else
  234. Serial.println(
  235. "# usage: K<0-3> <code> (0=L4 1=R4 2=L5 3=R5)");
  236. } else if (line[0] == 'Q') {
  237. uint8_t code = strtoul(line + 1, 0, 10);
  238. g_qamMap = code;
  239. saveCfg();
  240. Serial.printf(
  241. "# 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",
  242. code);
  243. } else if (line[0] == 'J') {
  244. char *sp = 0;
  245. uint8_t id = strtoul(line + 1, &sp, 0);
  246. // inject SET-SETTINGS to controller: report 0x87 [id][val u16 LE]
  247. uint16_t val = sp ? strtoul(sp, 0, 0) : 0;
  248. uint8_t pl[3] = { id, (uint8_t)(val & 0xFF),
  249. (uint8_t)(val >> 8) };
  250. relayEnqueue(0x87, pl, 3);
  251. Serial.printf(
  252. "# queued SET-SETTINGS id=0x%02X val=%u (relay 0x87) — watch new=/s\n",
  253. id, val);
  254. } else if (line[0] == 'G') {
  255. g_sniff = !g_sniff;
  256. g_sniffPh = 0;
  257. g_rfHost = g_connOn = g_rfCap = g_rfAuto =
  258. g_rfReplay = false;
  259. if (g_sniff)
  260. rfSniffStart();
  261. Serial.printf(
  262. "# SESSION SNIFF %s (phase0: catch host frame on ibex/ch2; phase1 PARK ch=%s)\n",
  263. g_sniff ? "ON" : "off",
  264. g_sniffPark ? "override" : "primary");
  265. } else if (line[0] == 'g') {
  266. g_sniffPark = strtoul(line + 1, 0, 10);
  267. if (g_sniffPh == 1)
  268. rfSniffStart();
  269. Serial.printf(
  270. "# sniff park channel=%u (0=learned primary)\n",
  271. g_sniffPark);
  272. } else if (line[0] == 'V') {
  273. g_e7b = strtoul(line + 1, 0, 10);
  274. Serial.printf(
  275. "# E7 protocol-version B-byte=%u (0=awake/slow, 1=try v1-fast; watch new=/s; revert if input stops)\n",
  276. g_e7b);
  277. } else if (line[0] == 'O') {
  278. g_relayOp = strtoul(line + 1, 0, 16);
  279. Serial.printf("# relay opcode=%02X\n",
  280. g_relayOp);
  281. } else if (line[0] == 'o') {
  282. g_relaySub = strtoul(line + 1, 0, 16);
  283. Serial.printf("# relay sub-type=%02X\n",
  284. g_relaySub);
  285. } else if (line[0] == 'h') {
  286. uint8_t nc = strtoul(line + 1, 0, 10);
  287. Serial.printf(
  288. "# HOP %u->%u (advertise on current ch, then poll new). Watch F1=/s\n",
  289. g_sessCh, nc);
  290. rfHopTo(nc);
  291. } else if (line[0] == 'z') {
  292. g_qos = !g_qos;
  293. g_hopIdx = 0;
  294. g_qosBad = 0;
  295. g_qosCheckMs = millis();
  296. Serial.printf(
  297. "# QoS adaptive channel hopping %s (candidates 18,46,76,22,68)\n",
  298. g_qos ? "ON" : "off");
  299. } else if (line[0] == 'r') {
  300. g_rxWin = strtoul(line + 1, 0, 10);
  301. if (g_rxWin < 150)
  302. g_rxWin = 150;
  303. Serial.printf(
  304. "# poll RX-window=%lu us (poll rate caps ~%lu/s)\n",
  305. (unsigned long)g_rxWin,
  306. (unsigned long)(1000000 / g_rxWin));
  307. } else if (line[0] == 'e') {
  308. g_e3mode = strtoul(line + 1, 0, 10);
  309. Serial.printf(
  310. "# E3 poll PID mode=%u (0=fixed07, 1=cyclePID+noack1, 2=cyclePID+noack0) - watch new=/s\n",
  311. g_e3mode);
  312. } else if (line[0] == 'd') {
  313. g_pollGet = !g_pollGet;
  314. Serial.printf(
  315. "# poll = %s (real puck sends BARE E3) - watch F1=/s new=/s\n",
  316. g_pollGet ? "E3 + GET-report-0x45 TLV (legacy)" :
  317. "bare E3");
  318. } else if (line[0] == 'n') {
  319. g_e7announce = !g_e7announce;
  320. Serial.printf(
  321. "# E7 awake-announce %s (real puck never sends E7) - watch F1=/s\n",
  322. g_e7announce ? "ON (legacy)" : "off");
  323. } else if (line[0] == 'm') {
  324. g_e1keepalive = !g_e1keepalive;
  325. Serial.printf(
  326. "# session-channel E1 keepalive %s (real puck sends none; needed for shared addr) - watch F1=/s\n",
  327. g_e1keepalive ? "ON" : "off");
  328. } else if (line[0] == 't') {
  329. // inject n test haptics (output 0x82 [01 01 F7]) over the relay
  330. uint8_t n = line[1] ? strtoul(line + 1, 0, 10) :
  331. 40;
  332. g_testHaptic = n;
  333. Serial.printf(
  334. "# test-haptic burst x%u queued (relay 0x82 01 01 F7 via op=%02X sub=%02X)\n",
  335. n, g_relayOp, g_relaySub);
  336. }
  337. // dump the captured OUTPUT-report history (oldest->newest)
  338. else if (line[0] == 'H') {
  339. hapticDumpLog();
  340. } else if (line[0] == 'j') {
  341. g_connType = strtoul(line + 1, 0, 16);
  342. Serial.printf("# connType=%02X\n", g_connType);
  343. } else if (line[0] == 'Q') {
  344. g_connLen = strtoul(line + 1, 0, 16);
  345. Serial.printf("# connLen=%02X\n", g_connLen);
  346. } else if (line[0] == 'A') {
  347. g_balen = strtoul(line + 1, 0, 16);
  348. g_pcnf1 = 0;
  349. Serial.printf("# balen=%u\n", g_balen);
  350. } else if (line[0] == 'a') {
  351. uint32_t v = strtoul(line + 1, 0, 16);
  352. g_rfBase[0] = v >> 24;
  353. g_rfBase[1] = v >> 16;
  354. g_rfBase[2] = v >> 8;
  355. g_rfBase[3] = v;
  356. Serial.printf("# base=%08lX\n",
  357. (unsigned long)v);
  358. if (g_rfListen)
  359. rfListenStart();
  360. } else if (line[0] == 'b') {
  361. for (int i = 0; i < NSLOT; i++)
  362. if (g_slot[i].used) {
  363. Serial.printf("slot%d ", i);
  364. for (int j = 0; j < 24; j++)
  365. Serial.printf(
  366. "%02X",
  367. g_slot[i].rec[j]);
  368. Serial.println();
  369. }
  370. } else if (line[0] == 'D') {
  371. armDebugCdcNextBoot();
  372. Serial.println(
  373. "# debug-CDC armed: reset to KEEP the serial console one more boot (auto-reverts after; the wake mouse is dropped that boot)");
  374. } else if (line[0] == 'X') {
  375. g_rfCap = !g_rfCap;
  376. g_rfHost = g_rfAuto = g_rfBeacon = g_rfListen =
  377. g_rfRaw = g_rfSweep = g_rfRespond =
  378. false;
  379. g_rfCh = 2;
  380. g_capV = 0;
  381. if (g_rfCap)
  382. rfCapStart(g_rfCh);
  383. Serial.printf(
  384. "# CAPTURE(listen) %s ch%u on \"ibex\", cycling 1M/2M x BALEN3/4 (use cN to set ch)\n",
  385. g_rfCap ? "ON" : "off", g_rfCh);
  386. } else if (line[0] == 'S') {
  387. g_rfAuto = !g_rfAuto;
  388. g_rfHost = g_rfAuto;
  389. g_rfBeacon = g_rfListen = g_rfRaw = g_rfSweep =
  390. false;
  391. g_rfCh = 2;
  392. g_cfgIdx = 0;
  393. if (g_rfAuto)
  394. applyCfg(0);
  395. Serial.printf("# AUTOSWEEP %s\n",
  396. g_rfAuto ? "ON" : "off");
  397. }
  398. li = 0;
  399. } else if (li < sizeof line - 1)
  400. line[li++] = c;
  401. }
  402. }