Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. }
  226. }
  227. for (int i=2; i < 8; i++) {
  228. uint32_t key = report[i];
  229. if (key >= 4 && !contains(key, prev_report)) {
  230. key_press(report[0], key);
  231. }
  232. }
  233. memcpy(prev_report, report, 8);
  234. queue_Data_Transfer(datapipe, report, 8, this);
  235. }
  236. void KeyboardController::numLock(bool f) {
  237. if (leds_.numLock != f) {
  238. leds_.numLock = f;
  239. updateLEDS();
  240. }
  241. }
  242. void KeyboardController::capsLock(bool f) {
  243. if (leds_.capsLock != f) {
  244. leds_.capsLock = f;
  245. updateLEDS();
  246. }
  247. }
  248. void KeyboardController::scrollLock(bool f) {
  249. if (leds_.scrollLock != f) {
  250. leds_.scrollLock = f;
  251. updateLEDS();
  252. }
  253. }
  254. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  255. {
  256. // TODO: queue events, perform callback from Task
  257. println(" press, key=", key);
  258. modifiers = mod;
  259. keyOEM = key;
  260. keyCode = convert_to_unicode(mod, key);
  261. println(" unicode = ", keyCode);
  262. if (keyPressedFunction) {
  263. keyPressedFunction(keyCode);
  264. } else {
  265. keyPressed();
  266. }
  267. }
  268. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  269. {
  270. // TODO: queue events, perform callback from Task
  271. println(" release, key=", key);
  272. modifiers = mod;
  273. keyOEM = key;
  274. // Look for modifier keys
  275. if (key == M(KEY_NUM_LOCK)) {
  276. numLock(!leds_.numLock);
  277. // Lets toggle Numlock
  278. } else if (key == M(KEY_CAPS_LOCK)) {
  279. capsLock(!leds_.capsLock);
  280. } else if (key == M(KEY_SCROLL_LOCK)) {
  281. scrollLock(!leds_.scrollLock);
  282. } else {
  283. keyCode = convert_to_unicode(mod, key);
  284. if (keyReleasedFunction) {
  285. keyReleasedFunction(keyCode);
  286. } else {
  287. keyReleased();
  288. }
  289. }
  290. }
  291. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  292. {
  293. // WIP: special keys
  294. // TODO: dead key sequences
  295. if (key & SHIFT_MASK) {
  296. // Many of these keys will look like they are other keys with shift mask...
  297. // Check for any of our mapped extra keys
  298. for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
  299. if (keycode_numlock[i].code == key) {
  300. // See if the user is using numlock or not...
  301. if (leds_.numLock) {
  302. return keycode_numlock[i].charNumlockOn;
  303. } else {
  304. key = keycode_numlock[i].codeNumlockOff;
  305. if (!(key & 0x80)) return key; // we have hard coded value
  306. key &= 0x7f; // mask off the extra and break out to process as other characters...
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. // Check for any of our mapped extra keys - Done early as some of these keys are
  313. // above and some below the SHIFT_MASK value
  314. for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
  315. if (keycode_extras[i].code == key) {
  316. return keycode_extras[i].ascii;
  317. }
  318. }
  319. // If we made it here without doing something then return 0;
  320. if (key & SHIFT_MASK) return 0;
  321. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  322. if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
  323. for (int i=0; i < 96; i++) {
  324. if (keycodes_ascii[i] == key) {
  325. if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
  326. return i + 32;
  327. }
  328. }
  329. #ifdef ISO_8859_1_A0
  330. for (int i=0; i < 96; i++) {
  331. if (keycodes_iso_8859_1[i] == key) return i + 160;
  332. }
  333. #endif
  334. return 0;
  335. }
  336. void KeyboardController::LEDS(uint8_t leds) {
  337. println("Keyboard setLEDS ", leds, HEX);
  338. leds_.byte = leds;
  339. updateLEDS();
  340. }
  341. void KeyboardController::updateLEDS() {
  342. // Now lets tell keyboard new state.
  343. if (device != nullptr) {
  344. // Only do it this way if we are a standard USB device
  345. mk_setup(setup, 0x21, 9, 0x200, 0, sizeof(leds_.byte)); // hopefully this sets leds
  346. queue_Control_Transfer(device, &setup, &leds_.byte, this);
  347. } else {
  348. // Bluetooth, need to setup back channel to Bluetooth controller.
  349. }
  350. }
  351. //=============================================================================
  352. // Keyboard Extras - Combined from other object
  353. //=============================================================================
  354. #define TOPUSAGE_SYS_CONTROL 0x10080
  355. #define TOPUSAGE_CONSUMER_CONTROL 0x0c0001
  356. hidclaim_t KeyboardController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  357. {
  358. // Lets try to claim a few specific Keyboard related collection/reports
  359. //USBHDBGSerial.printf("KBH Claim %x\n", topusage);
  360. if ((topusage != TOPUSAGE_SYS_CONTROL)
  361. && (topusage != TOPUSAGE_CONSUMER_CONTROL)
  362. ) return CLAIM_NO;
  363. // only claim from one physical device
  364. //USBHDBGSerial.println("KeyboardController claim collection");
  365. // Lets only claim if this is the same device as claimed Keyboard...
  366. if (dev != device) return CLAIM_NO;
  367. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  368. mydevice = dev;
  369. collections_claimed_++;
  370. return CLAIM_REPORT;
  371. }
  372. void KeyboardController::disconnect_collection(Device_t *dev)
  373. {
  374. if (--collections_claimed_ == 0) {
  375. mydevice = NULL;
  376. }
  377. }
  378. void KeyboardController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  379. {
  380. //USBHDBGSerial.printf("KPC:hid_input_begin TUSE: %x TYPE: %x Range:%x %x\n", topusage, type, lgmin, lgmax);
  381. topusage_ = topusage; // remember which report we are processing.
  382. hid_input_begin_ = true;
  383. hid_input_data_ = false;
  384. }
  385. void KeyboardController::hid_input_data(uint32_t usage, int32_t value)
  386. {
  387. // Hack ignore 0xff00 high words as these are user values...
  388. if ((usage & 0xffff0000) == 0xff000000) return;
  389. //USBHDBGSerial.printf("KeyboardController: topusage= %x usage=%X, value=%d\n", topusage_, usage, value);
  390. // See if the value is in our keys_down list
  391. usage &= 0xffff; // only keep the actual key
  392. if (usage == 0) return; // lets not process 0, if only 0 happens, we will handle it on the end to remove existing pressed items.
  393. // Remember if we have received any logical key up events. Some keyboard appear to send them
  394. // others do no...
  395. hid_input_data_ = true;
  396. uint8_t key_index;
  397. for (key_index = 0; key_index < count_keys_down_; key_index++) {
  398. if (keys_down[key_index] == usage) {
  399. if (value) return; // still down
  400. if (extrasKeyReleasedFunction) {
  401. extrasKeyReleasedFunction(topusage_, usage);
  402. }
  403. // Remove from list
  404. count_keys_down_--;
  405. for (;key_index < count_keys_down_; key_index++) {
  406. keys_down[key_index] = keys_down[key_index+1];
  407. }
  408. return;
  409. }
  410. }
  411. // Was not in list
  412. if (!value) return; // still 0
  413. if (extrasKeyPressedFunction) {
  414. extrasKeyPressedFunction(topusage_, usage);
  415. }
  416. if (count_keys_down_ < MAX_KEYS_DOWN) {
  417. keys_down[count_keys_down_++] = usage;
  418. }
  419. }
  420. void KeyboardController::hid_input_end()
  421. {
  422. //USBHDBGSerial.println("KPC:hid_input_end");
  423. if (hid_input_begin_) {
  424. // See if we received any data from parser if not, assume all keys released...
  425. if (!hid_input_data_ ) {
  426. if (extrasKeyReleasedFunction) {
  427. while (count_keys_down_) {
  428. count_keys_down_--;
  429. extrasKeyReleasedFunction(topusage_, keys_down[count_keys_down_]);
  430. }
  431. }
  432. count_keys_down_ = 0;
  433. }
  434. hid_input_begin_ = false;
  435. }
  436. }
  437. bool KeyboardController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  438. {
  439. USBHDBGSerial.printf("Keyboard Controller::claim_bluetooth - Class %x\n", bluetooth_class);
  440. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && (bluetooth_class & 0x40)) {
  441. if (remoteName && (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0)) {
  442. USBHDBGSerial.printf("KeyboardController::claim_bluetooth Reject PS3 hack\n");
  443. return false;
  444. }
  445. USBHDBGSerial.printf("KeyboardController::claim_bluetooth TRUE\n");
  446. //btdevice = driver;
  447. return true;
  448. }
  449. return false;
  450. }
  451. bool KeyboardController::remoteNameComplete(const uint8_t *remoteName)
  452. {
  453. // Real Hack some PS3 controllers bluetoot class is keyboard...
  454. if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  455. USBHDBGSerial.printf(" KeyboardController::remoteNameComplete %s - Oops PS3 unclaim\n", remoteName);
  456. return false;
  457. }
  458. return true;
  459. }
  460. bool KeyboardController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  461. {
  462. // Example DATA from bluetooth keyboard:
  463. // 0 1 2 3 4 5 6 7 8 910 1 2 3 4 5 6 7
  464. // LEN D
  465. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  466. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 4 0 0 0 0 0
  467. //BT rx2_data(18): 48 20 e 0 a 0 70 0 a1 1 2 0 0 0 0 0 0 0
  468. // So Len=9 passed in data starting at report ID=1...
  469. USBHDBGSerial.printf("KeyboardController::process_bluetooth_HID_data\n");
  470. if (data[0] != 1) return false;
  471. print(" KB Data: ");
  472. print_hexbytes(data, length);
  473. for (int i=2; i < length; i++) {
  474. uint32_t key = prev_report[i];
  475. if (key >= 4 && !contains(key, report)) {
  476. key_release(prev_report[0], key);
  477. }
  478. }
  479. for (int i=2; i < 8; i++) {
  480. uint32_t key = data[i];
  481. if (key >= 4 && !contains(key, prev_report)) {
  482. key_press(data[1], key);
  483. }
  484. }
  485. // Save away the data.. But shift down one byte... Don't need the report number
  486. memcpy(prev_report, &data[1], 8);
  487. return true;
  488. }
  489. void KeyboardController::release_bluetooth()
  490. {
  491. //btdevice = nullptr;
  492. }
  493. //*****************************************************************************
  494. // Some simple query functions depend on which interface we are using...
  495. //*****************************************************************************
  496. uint16_t KeyboardController::idVendor()
  497. {
  498. if (device != nullptr) return device->idVendor;
  499. if (mydevice != nullptr) return mydevice->idVendor;
  500. if (btdevice != nullptr) return btdevice->idVendor;
  501. return 0;
  502. }
  503. uint16_t KeyboardController::idProduct()
  504. {
  505. if (device != nullptr) return device->idProduct;
  506. if (mydevice != nullptr) return mydevice->idProduct;
  507. if (btdevice != nullptr) return btdevice->idProduct;
  508. return 0;
  509. }
  510. const uint8_t *KeyboardController::manufacturer()
  511. {
  512. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  513. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  514. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  515. return nullptr;
  516. }
  517. const uint8_t *KeyboardController::product()
  518. {
  519. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  520. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  521. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  522. return nullptr;
  523. }
  524. const uint8_t *KeyboardController::serialNumber()
  525. {
  526. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  527. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  528. if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  529. return nullptr;
  530. }