PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #include "keylayouts.h" // from Teensyduino core library
  26. typedef struct {
  27. KEYCODE_TYPE code;
  28. uint8_t ascii;
  29. } keycode_extra_t;
  30. typedef struct {
  31. KEYCODE_TYPE code;
  32. KEYCODE_TYPE codeNumlockOff;
  33. uint8_t charNumlockOn; // We will assume when num lock is on we have all characters...
  34. } keycode_numlock_t;
  35. typedef struct {
  36. uint16_t idVendor; // vendor id of keyboard
  37. uint16_t idProduct; // product id - 0 implies all of the ones from vendor;
  38. } keyboard_force_boot_protocol_t; // list of products to force into boot protocol
  39. #ifdef M
  40. #undef M
  41. #endif
  42. #define M(n) ((n) & KEYCODE_MASK)
  43. static const keycode_extra_t keycode_extras[] = {
  44. {M(KEY_ENTER), '\n'},
  45. {M(KEY_ESC), 0x1b},
  46. {M(KEY_TAB), 0x9 },
  47. {M(KEY_UP), KEYD_UP },
  48. {M(KEY_DOWN), KEYD_DOWN },
  49. {M(KEY_LEFT), KEYD_LEFT },
  50. {M(KEY_RIGHT), KEYD_RIGHT },
  51. {M(KEY_INSERT), KEYD_INSERT },
  52. {M(KEY_DELETE), KEYD_DELETE },
  53. {M(KEY_PAGE_UP), KEYD_PAGE_UP },
  54. {M(KEY_PAGE_DOWN), KEYD_PAGE_DOWN },
  55. {M(KEY_HOME), KEYD_HOME },
  56. {M(KEY_END), KEYD_END },
  57. {M(KEY_F1), KEYD_F1 },
  58. {M(KEY_F2), KEYD_F2 },
  59. {M(KEY_F3), KEYD_F3 },
  60. {M(KEY_F4), KEYD_F4 },
  61. {M(KEY_F5), KEYD_F5 },
  62. {M(KEY_F6), KEYD_F6 },
  63. {M(KEY_F7), KEYD_F7 },
  64. {M(KEY_F8), KEYD_F8 },
  65. {M(KEY_F9), KEYD_F9 },
  66. {M(KEY_F10), KEYD_F10 },
  67. {M(KEY_F11), KEYD_F11 },
  68. {M(KEY_F12), KEYD_F12 }
  69. };
  70. // Some of these mapped to key + shift.
  71. static const keycode_numlock_t keycode_numlock[] = {
  72. {M(KEYPAD_SLASH), '/', '/'},
  73. {M(KEYPAD_ASTERIX), '*', '*'},
  74. {M(KEYPAD_MINUS), '-', '-'},
  75. {M(KEYPAD_PLUS), '+', '+'},
  76. {M(KEYPAD_ENTER), '\n', '\n'},
  77. {M(KEYPAD_1), 0x80 | M(KEY_END), '1'},
  78. {M(KEYPAD_2), 0x80 | M(KEY_DOWN), '2'},
  79. {M(KEYPAD_3), 0x80 | M(KEY_PAGE_DOWN), '3'},
  80. {M(KEYPAD_4), 0x80 | M(KEY_LEFT), '4'},
  81. {M(KEYPAD_5), 0x00, '5'},
  82. {M(KEYPAD_6), 0x80 | M(KEY_RIGHT), '6'},
  83. {M(KEYPAD_7), 0x80 | M(KEY_HOME), '7'},
  84. {M(KEYPAD_8), 0x80 | M(KEY_UP), '8'},
  85. {M(KEYPAD_9), 0x80 | M(KEY_PAGE_UP), '9'},
  86. {M(KEYPAD_0), 0x80 | M(KEY_INSERT), '0'},
  87. {M(KEYPAD_PERIOD), 0x80 | M(KEY_DELETE), '.'}
  88. };
  89. static const keyboard_force_boot_protocol_t keyboard_forceBootMode[] = {
  90. {0x04D9, 0}
  91. };
  92. #define print USBHost::print_
  93. #define println USBHost::println_
  94. void KeyboardController::init()
  95. {
  96. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  97. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  98. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  99. driver_ready_for_device(this);
  100. USBHIDParser::driver_ready_for_hid_collection(this);
  101. BluetoothController::driver_ready_for_bluetooth(this);
  102. force_boot_protocol = false; // start off assuming not
  103. }
  104. bool KeyboardController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  105. {
  106. println("KeyboardController claim this=", (uint32_t)this, HEX);
  107. // only claim at interface level
  108. if (type != 1) return false;
  109. if (len < 9+9+7) return false;
  110. print_hexbytes(descriptors, len);
  111. uint32_t numendpoint = descriptors[4];
  112. if (numendpoint < 1) return false;
  113. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  114. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  115. if (descriptors[7] != 1) return false; // bInterfaceProtocol, 1 = Keyboard
  116. if (descriptors[9] != 9) return false;
  117. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  118. if (descriptors[18] != 7) return false;
  119. if (descriptors[19] != 5) return false; // endpoint descriptor
  120. uint32_t endpoint = descriptors[20];
  121. println("ep = ", endpoint, HEX);
  122. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  123. endpoint &= 0x0F;
  124. if (endpoint == 0) return false;
  125. if (descriptors[21] != 3) return false; // must be interrupt type
  126. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  127. println("packet size = ", size);
  128. if ((size < 8) || (size > 64)) {
  129. return false; // Keyboard Boot Protocol is 8 bytes, but maybe others have longer...
  130. }
  131. #ifdef USBHS_KEYBOARD_INTERVAL
  132. uint32_t interval = USBHS_KEYBOARD_INTERVAL;
  133. #else
  134. uint32_t interval = descriptors[24];
  135. #endif
  136. println("polling interval = ", interval);
  137. datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);
  138. datapipe->callback_function = callback;
  139. queue_Data_Transfer(datapipe, report, 8, this);
  140. // see if this device in list of devices that need to be set in
  141. // boot protocol mode
  142. bool in_forceBoot_mode_list = false;
  143. for (uint8_t i = 0; i < sizeof(keyboard_forceBootMode)/sizeof(keyboard_forceBootMode[0]); i++) {
  144. if (dev->idVendor == keyboard_forceBootMode[i].idVendor) {
  145. if ((dev->idProduct == keyboard_forceBootMode[i].idProduct) ||
  146. (keyboard_forceBootMode[i].idProduct == 0)) {
  147. in_forceBoot_mode_list = true;
  148. break;
  149. }
  150. }
  151. }
  152. if (in_forceBoot_mode_list) {
  153. println("SET_PROTOCOL Boot");
  154. mk_setup(setup, 0x21, 11, 0, 0, 0); // 11=SET_PROTOCOL BOOT
  155. } else {
  156. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  157. }
  158. queue_Control_Transfer(dev, &setup, NULL, this);
  159. control_queued = true;
  160. return true;
  161. }
  162. void KeyboardController::control(const Transfer_t *transfer)
  163. {
  164. println("control callback (keyboard)");
  165. control_queued = false;
  166. print_hexbytes(transfer->buffer, transfer->length);
  167. // To decode hex dump to human readable HID report summary:
  168. // http://eleccelerator.com/usbdescreqparser/
  169. uint32_t mesg = transfer->setup.word1;
  170. println(" mesg = ", mesg, HEX);
  171. if (mesg == 0x00B21 && transfer->length == 0) { // SET_PROTOCOL
  172. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  173. control_queued = true;
  174. queue_Control_Transfer(device, &setup, NULL, this);
  175. } else if (force_boot_protocol) {
  176. forceBootProtocol(); // lets setup to do the boot protocol
  177. force_boot_protocol = false; // turn back off
  178. }
  179. }
  180. void KeyboardController::callback(const Transfer_t *transfer)
  181. {
  182. //println("KeyboardController Callback (static)");
  183. if (transfer->driver) {
  184. ((KeyboardController *)(transfer->driver))->new_data(transfer);
  185. }
  186. }
  187. void KeyboardController::forceBootProtocol()
  188. {
  189. if (device && !control_queued) {
  190. mk_setup(setup, 0x21, 11, 0, 0, 0); // 11=SET_PROTOCOL BOOT
  191. control_queued = true;
  192. queue_Control_Transfer(device, &setup, NULL, this);
  193. } else {
  194. force_boot_protocol = true; // let system know we want to force this.
  195. }
  196. }
  197. void KeyboardController::disconnect()
  198. {
  199. // TODO: free resources
  200. }
  201. // Arduino defined this static weak symbol callback, and their
  202. // examples use it as the only way to detect new key presses,
  203. // so unfortunate as static weak callbacks are, it probably
  204. // needs to be supported for compatibility
  205. extern "C" {
  206. void __keyboardControllerEmptyCallback() { }
  207. }
  208. void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  209. void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  210. static bool contains(uint8_t b, const uint8_t *data)
  211. {
  212. if (data[2] == b || data[3] == b || data[4] == b) return true;
  213. if (data[5] == b || data[6] == b || data[7] == b) return true;
  214. return false;
  215. }
  216. void KeyboardController::new_data(const Transfer_t *transfer)
  217. {
  218. println("KeyboardController Callback (member)");
  219. print(" KB Data: ");
  220. print_hexbytes(transfer->buffer, 8);
  221. for (int i=2; i < 8; i++) {
  222. uint32_t key = prev_report[i];
  223. if (key >= 4 && !contains(key, report)) {
  224. key_release(prev_report[0], key);
  225. if (rawKeyReleasedFunction) {
  226. rawKeyReleasedFunction(key);
  227. }
  228. }
  229. }
  230. if (rawKeyReleasedFunction) {
  231. // each modifier key is represented by a bit in the first byte
  232. for (int i = 0; i < 8; ++i)
  233. {
  234. uint8_t keybit = 1 << i;
  235. if ((prev_report[0] & keybit) && !(report[0] & keybit)) {
  236. rawKeyReleasedFunction(103 + i);
  237. }
  238. }
  239. }
  240. for (int i=2; i < 8; i++) {
  241. uint32_t key = report[i];
  242. if (key >= 4 && !contains(key, prev_report)) {
  243. key_press(report[0], key);
  244. if (rawKeyPressedFunction) {
  245. rawKeyPressedFunction(key);
  246. }
  247. }
  248. }
  249. if (rawKeyPressedFunction) {
  250. for (int i = 0; i < 8; ++i)
  251. {
  252. uint8_t keybit = 1 << i;
  253. if (!(prev_report[0] & keybit) && (report[0] & keybit)) {
  254. rawKeyPressedFunction(103 + i);
  255. }
  256. }
  257. }
  258. memcpy(prev_report, report, 8);
  259. queue_Data_Transfer(datapipe, report, 8, this);
  260. }
  261. void KeyboardController::numLock(bool f) {
  262. if (leds_.numLock != f) {
  263. leds_.numLock = f;
  264. updateLEDS();
  265. }
  266. }
  267. void KeyboardController::capsLock(bool f) {
  268. if (leds_.capsLock != f) {
  269. leds_.capsLock = f;
  270. updateLEDS();
  271. }
  272. }
  273. void KeyboardController::scrollLock(bool f) {
  274. if (leds_.scrollLock != f) {
  275. leds_.scrollLock = f;
  276. updateLEDS();
  277. }
  278. }
  279. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  280. {
  281. // TODO: queue events, perform callback from Task
  282. println(" press, key=", key);
  283. modifiers = mod;
  284. keyOEM = key;
  285. keyCode = convert_to_unicode(mod, key);
  286. println(" unicode = ", keyCode);
  287. if (keyPressedFunction) {
  288. keyPressedFunction(keyCode);
  289. } else {
  290. keyPressed();
  291. }
  292. }
  293. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  294. {
  295. // TODO: queue events, perform callback from Task
  296. println(" release, key=", key);
  297. modifiers = mod;
  298. keyOEM = key;
  299. // Look for modifier keys
  300. if (key == M(KEY_NUM_LOCK)) {
  301. numLock(!leds_.numLock);
  302. // Lets toggle Numlock
  303. } else if (key == M(KEY_CAPS_LOCK)) {
  304. capsLock(!leds_.capsLock);
  305. } else if (key == M(KEY_SCROLL_LOCK)) {
  306. scrollLock(!leds_.scrollLock);
  307. } else {
  308. keyCode = convert_to_unicode(mod, key);
  309. if (keyReleasedFunction) {
  310. keyReleasedFunction(keyCode);
  311. } else {
  312. keyReleased();
  313. }
  314. }
  315. }
  316. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  317. {
  318. // WIP: special keys
  319. // TODO: dead key sequences
  320. if (key & SHIFT_MASK) {
  321. // Many of these keys will look like they are other keys with shift mask...
  322. // Check for any of our mapped extra keys
  323. for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
  324. if (keycode_numlock[i].code == key) {
  325. // See if the user is using numlock or not...
  326. if (leds_.numLock) {
  327. return keycode_numlock[i].charNumlockOn;
  328. } else {
  329. key = keycode_numlock[i].codeNumlockOff;
  330. if (!(key & 0x80)) return key; // we have hard coded value
  331. key &= 0x7f; // mask off the extra and break out to process as other characters...
  332. break;
  333. }
  334. }
  335. }
  336. }
  337. // Check for any of our mapped extra keys - Done early as some of these keys are
  338. // above and some below the SHIFT_MASK value
  339. for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
  340. if (keycode_extras[i].code == key) {
  341. return keycode_extras[i].ascii;
  342. }
  343. }
  344. // If we made it here without doing something then return 0;
  345. if (key & SHIFT_MASK) return 0;
  346. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  347. if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
  348. for (int i=0; i < 96; i++) {
  349. if (keycodes_ascii[i] == key) {
  350. if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
  351. return i + 32;
  352. }
  353. }
  354. #ifdef ISO_8859_1_A0
  355. for (int i=0; i < 96; i++) {
  356. if (keycodes_iso_8859_1[i] == key) return i + 160;
  357. }
  358. #endif
  359. return 0;
  360. }
  361. void KeyboardController::LEDS(uint8_t leds) {
  362. println("Keyboard setLEDS ", leds, HEX);
  363. leds_.byte = leds;
  364. updateLEDS();
  365. }
  366. void KeyboardController::updateLEDS() {
  367. // Now lets tell keyboard new state.
  368. if (device != nullptr) {
  369. // Only do it this way if we are a standard USB device
  370. mk_setup(setup, 0x21, 9, 0x200, 0, sizeof(leds_.byte)); // hopefully this sets leds
  371. queue_Control_Transfer(device, &setup, &leds_.byte, this);
  372. } else {
  373. // Bluetooth, need to setup back channel to Bluetooth controller.
  374. }
  375. }
  376. //=============================================================================
  377. // Keyboard Extras - Combined from other object
  378. //=============================================================================
  379. #define TOPUSAGE_SYS_CONTROL 0x10080
  380. #define TOPUSAGE_CONSUMER_CONTROL 0x0c0001
  381. hidclaim_t KeyboardController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  382. {
  383. // Lets try to claim a few specific Keyboard related collection/reports
  384. //USBHDBGSerial.printf("KBH Claim %x\n", topusage);
  385. if ((topusage != TOPUSAGE_SYS_CONTROL)
  386. && (topusage != TOPUSAGE_CONSUMER_CONTROL)
  387. ) return CLAIM_NO;
  388. // only claim from one physical device
  389. //USBHDBGSerial.println("KeyboardController claim collection");
  390. // Lets only claim if this is the same device as claimed Keyboard...
  391. if (dev != device) return CLAIM_NO;
  392. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  393. mydevice = dev;
  394. collections_claimed_++;
  395. return CLAIM_REPORT;
  396. }
  397. void KeyboardController::disconnect_collection(Device_t *dev)
  398. {
  399. if (--collections_claimed_ == 0) {
  400. mydevice = NULL;
  401. }
  402. }
  403. void KeyboardController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  404. {
  405. //USBHDBGSerial.printf("KPC:hid_input_begin TUSE: %x TYPE: %x Range:%x %x\n", topusage, type, lgmin, lgmax);
  406. topusage_ = topusage; // remember which report we are processing.
  407. hid_input_begin_ = true;
  408. hid_input_data_ = false;
  409. }
  410. void KeyboardController::hid_input_data(uint32_t usage, int32_t value)
  411. {
  412. // Hack ignore 0xff00 high words as these are user values...
  413. if ((usage & 0xffff0000) == 0xff000000) return;
  414. //USBHDBGSerial.printf("KeyboardController: topusage= %x usage=%X, value=%d\n", topusage_, usage, value);
  415. // See if the value is in our keys_down list
  416. usage &= 0xffff; // only keep the actual key
  417. if (usage == 0) return; // lets not process 0, if only 0 happens, we will handle it on the end to remove existing pressed items.
  418. // Remember if we have received any logical key up events. Some keyboard appear to send them
  419. // others do no...
  420. hid_input_data_ = true;
  421. uint8_t key_index;
  422. for (key_index = 0; key_index < count_keys_down_; key_index++) {
  423. if (keys_down[key_index] == usage) {
  424. if (value) return; // still down
  425. if (extrasKeyReleasedFunction) {
  426. extrasKeyReleasedFunction(topusage_, usage);
  427. }
  428. // Remove from list
  429. count_keys_down_--;
  430. for (;key_index < count_keys_down_; key_index++) {
  431. keys_down[key_index] = keys_down[key_index+1];
  432. }
  433. return;
  434. }
  435. }
  436. // Was not in list
  437. if (!value) return; // still 0
  438. if (extrasKeyPressedFunction) {
  439. extrasKeyPressedFunction(topusage_, usage);
  440. }
  441. if (count_keys_down_ < MAX_KEYS_DOWN) {
  442. keys_down[count_keys_down_++] = usage;
  443. }
  444. }
  445. void KeyboardController::hid_input_end()
  446. {
  447. //USBHDBGSerial.println("KPC:hid_input_end");
  448. if (hid_input_begin_) {
  449. // See if we received any data from parser if not, assume all keys released...
  450. if (!hid_input_data_ ) {
  451. if (extrasKeyReleasedFunction) {
  452. while (count_keys_down_) {
  453. count_keys_down_--;
  454. extrasKeyReleasedFunction(topusage_, keys_down[count_keys_down_]);
  455. }
  456. }
  457. count_keys_down_ = 0;
  458. }
  459. hid_input_begin_ = false;
  460. }
  461. }
  462. bool KeyboardController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  463. {
  464. USBHDBGSerial.printf("Keyboard Controller::claim_bluetooth - Class %x\n", bluetooth_class);
  465. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && (bluetooth_class & 0x40)) {
  466. if (remoteName && (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0)) {
  467. USBHDBGSerial.printf("KeyboardController::claim_bluetooth Reject PS3 hack\n");
  468. return false;
  469. }
  470. USBHDBGSerial.printf("KeyboardController::claim_bluetooth TRUE\n");
  471. //btdevice = driver;
  472. return true;
  473. }
  474. return false;
  475. }
  476. bool KeyboardController::remoteNameComplete(const uint8_t *remoteName)
  477. {
  478. // Real Hack some PS3 controllers bluetoot class is keyboard...
  479. if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  480. USBHDBGSerial.printf(" KeyboardController::remoteNameComplete %s - Oops PS3 unclaim\n", remoteName);
  481. return false;
  482. }
  483. return true;
  484. }
  485. bool KeyboardController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  486. {
  487. // Example DATA from bluetooth keyboard:
  488. // 0 1 2 3 4 5 6 7 8 910 1 2 3 4 5 6 7
  489. // LEN D
  490. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  491. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 4 0 0 0 0 0
  492. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  493. // So Len=9 passed in data starting at report ID=1...
  494. USBHDBGSerial.printf("KeyboardController::process_bluetooth_HID_data\n");
  495. if (data[0] != 1) return false;
  496. print(" KB Data: ");
  497. print_hexbytes(data, length);
  498. for (int i=2; i < length; i++) {
  499. uint32_t key = prev_report[i];
  500. if (key >= 4 && !contains(key, report)) {
  501. key_release(prev_report[0], key);
  502. }
  503. }
  504. for (int i=2; i < 8; i++) {
  505. uint32_t key = data[i];
  506. if (key >= 4 && !contains(key, prev_report)) {
  507. key_press(data[1], key);
  508. }
  509. }
  510. // Save away the data.. But shift down one byte... Don't need the report number
  511. memcpy(prev_report, &data[1], 8);
  512. return true;
  513. }
  514. void KeyboardController::release_bluetooth()
  515. {
  516. //btdevice = nullptr;
  517. }
  518. //*****************************************************************************
  519. // Some simple query functions depend on which interface we are using...
  520. //*****************************************************************************
  521. uint16_t KeyboardController::idVendor()
  522. {
  523. if (device != nullptr) return device->idVendor;
  524. if (mydevice != nullptr) return mydevice->idVendor;
  525. if (btdevice != nullptr) return btdevice->idVendor;
  526. return 0;
  527. }
  528. uint16_t KeyboardController::idProduct()
  529. {
  530. if (device != nullptr) return device->idProduct;
  531. if (mydevice != nullptr) return mydevice->idProduct;
  532. if (btdevice != nullptr) return btdevice->idProduct;
  533. return 0;
  534. }
  535. const uint8_t *KeyboardController::manufacturer()
  536. {
  537. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  538. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  539. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  540. return nullptr;
  541. }
  542. const uint8_t *KeyboardController::product()
  543. {
  544. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  545. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  546. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  547. return nullptr;
  548. }
  549. const uint8_t *KeyboardController::serialNumber()
  550. {
  551. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  552. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  553. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  554. return nullptr;
  555. }