Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1347 lines
47KB

  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. * information about the BlueTooth HCI comes from logic analyzer captures
  24. * plus... http://affon.narod.ru/BT/bluetooth_app_c10.pdf
  25. */
  26. #include <Arduino.h>
  27. #include "USBHost_t36.h" // Read this header first for key info
  28. #define print USBHost::print_
  29. #define println USBHost::println_
  30. /************************************************************/
  31. // Define HCI Commands OGF HIgh byte OCF is low byte...
  32. // Actually shifted values...
  33. /************************************************************/
  34. #define HCI_INQUIRY 0x0401
  35. #define HCI_INQUIRY_CANCEL 0x0402
  36. #define HCI_CREATE_CONNECTION 0x0405
  37. #define HCI_LINK_KEY_NEG_REPLY 0x040C
  38. #define HCI_PIN_CODE_REPLY 0x040D
  39. #define HCI_AUTH_REQUESTED 0x0411
  40. #define HCI_Write_Default_Link_Policy_Settings 0x080f
  41. #define HCI_Set_Event_Mask 0x0c01
  42. #define HCI_RESET 0x0c03
  43. #define HCI_Set_Event_Filter_Clear 0x0c05
  44. #define HCI_Read_Local_Name 0x0c14
  45. #define HCI_Read_Stored_Link_Key 0x0c0d
  46. #define HCI_DELETE_STORED_LINK_KEY 0x0c12
  47. #define HCI_WRITE_LOCAL_NAME 0x0c13
  48. #define Write_Connection_Accept_Timeout 0x0c16
  49. #define HCI_WRITE_SCAN_ENABLE 0x0c1a
  50. #define HCI_Read_Page_Scan_Activity 0x0c1b
  51. #define HCI_READ_CLASS_OF_DEVICE 0x0c23
  52. #define HCI_WRITE_CLASS_OF_DEV 0x0C24
  53. #define HCI_Read_Voice_Setting 0x0c25
  54. #define HCI_Read_Number_Of_Supported_IAC 0x0c38
  55. #define HCI_Read_Current_IAC_LAP 0x0c39
  56. #define HCI_WRITE_INQUIRY_MODE 0x0c45
  57. #define HCI_Read_Page_Scan_Type 0x0c46
  58. #define HCI_WRITE_EIR 0x0c52
  59. #define HCI_WRITE_SSP_MODE 0x0c56
  60. #define HCI_Read_Inquiry_Response_Transmit_Power_Level 0x0c58
  61. #define HCI_WRITE_LE_HOST_SUPPORTED 0x0c6d
  62. #define HCI_Read_Local_Supported_Features 0x1003
  63. #define HCI_Read_Local_Extended_Features 0x1004
  64. #define HCI_Read_Buffer_Size 0x1005
  65. #define HCI_Read_BD_ADDR 0x1009
  66. #define HCI_Read_Local_Version_Information 0x1001
  67. #define HCI_Read_Local_Supported_Commands 0x1002
  68. #define HCI_LE_SET_EVENT_MASK 0x2001
  69. #define HCI_LE_Read_Buffer_Size 0x2002
  70. #define HCI_LE_Read_Local_supported_Features 0x2003
  71. #define HCI_LE_READ_ADV_TX_POWER 0x2007
  72. #define HCI_LE_SET_ADV_DATA 0x2008
  73. #define HCI_LE_SET_SCAN_RSP_DATA 0x2009
  74. #define HCI_LE_READ_WHITE_LIST_SIZE 0x200f
  75. #define HCI_LE_CLEAR_WHITE_LIST 0x2010
  76. #define HCI_LE_Supported_States 0x201c
  77. /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
  78. #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
  79. #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
  80. /* L2CAP signaling commands */
  81. #define L2CAP_CMD_COMMAND_REJECT 0x01
  82. #define L2CAP_CMD_CONNECTION_REQUEST 0x02
  83. #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
  84. #define L2CAP_CMD_CONFIG_REQUEST 0x04
  85. #define L2CAP_CMD_CONFIG_RESPONSE 0x05
  86. #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
  87. #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
  88. #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
  89. #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
  90. #define HID_THDR_DATA_INPUT 0xa1
  91. // HID stuff
  92. #define HID_BOOT_PROTOCOL 0x00
  93. #define HID_RPT_PROTOCOL 0x01
  94. // different modes
  95. enum {PC_INQUIRE_CANCEL=100, PC_AUTHENTICATION_REQUESTED=110, PC_LINK_KEY_NEGATIVE=120, PC_PIN_CODE_REPLY=130};
  96. // Setup some states for the TX pipe where we need to chain messages
  97. enum {STATE_TX_SEND_CONNECT_INT=200};
  98. /************************************************************/
  99. // Initialization and claiming of devices & interfaces
  100. /************************************************************/
  101. void BluetoothController::init()
  102. {
  103. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  104. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  105. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  106. driver_ready_for_device(this);
  107. }
  108. bool BluetoothController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  109. {
  110. // only claim at device level
  111. println("BluetoothController claim this=", (uint32_t)this, HEX);
  112. // Lets try to support the main USB Bluetooth class...
  113. // http://www.usb.org/developers/defined_class/#BaseClassE0h
  114. if (dev->bDeviceClass != 0xe0) return false; // not base class wireless controller
  115. if ((dev->bDeviceSubClass != 1) || (dev->bDeviceProtocol != 1)) return false; // Bluetooth Programming Interface
  116. if (type != 0) return false;
  117. Serial.printf("BluetoothController claim this=%x vid:pid=%x:%x\n ", (uint32_t)this, dev->idVendor, dev->idProduct);
  118. if (len > 512) {
  119. Serial.printf(" Descriptor length %d only showing first 512\n ");
  120. len = 512;
  121. }
  122. for (uint16_t i=0; i < len; i++) {
  123. Serial.printf("%x ", descriptors[i]);
  124. if ((i & 0x3f) == 0x3f) Serial.printf("\n ");
  125. }
  126. Serial.printf("\n ");
  127. // Lets try to process the first Interface and get the end points...
  128. // Some common stuff for both XBoxs
  129. uint32_t count_end_points = descriptors[4];
  130. if (count_end_points < 2) return false;
  131. uint32_t rxep = 0;
  132. uint32_t rx2ep = 0;
  133. uint32_t txep = 0;
  134. uint8_t rx_interval = 0;
  135. uint8_t rx2_interval = 0;
  136. uint8_t tx_interval = 0;
  137. rx_size_ = 0;
  138. rx2_size_ = 0;
  139. tx_size_ = 0;
  140. uint32_t descriptor_index = 9;
  141. while (count_end_points-- /*&& ((rxep == 0) || txep == 0) */) {
  142. if (descriptors[descriptor_index] != 7) return false; // length 7
  143. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  144. if ((descriptors[descriptor_index+4] <= 64)
  145. && (descriptors[descriptor_index+5] == 0)) {
  146. // have a bulk EP size
  147. if (descriptors[descriptor_index+2] & 0x80 ) {
  148. if (descriptors[descriptor_index+3] == 3) { // Interrupt
  149. rxep = descriptors[descriptor_index+2];
  150. rx_size_ = descriptors[descriptor_index+4];
  151. rx_interval = descriptors[descriptor_index+6];
  152. } else if (descriptors[descriptor_index+3] == 2) { // bulk
  153. rx2ep = descriptors[descriptor_index+2];
  154. rx2_size_ = descriptors[descriptor_index+4];
  155. rx2_interval = descriptors[descriptor_index+6];
  156. }
  157. } else {
  158. txep = descriptors[descriptor_index+2];
  159. tx_size_ = descriptors[descriptor_index+4];
  160. tx_interval = descriptors[descriptor_index+6];
  161. }
  162. }
  163. descriptor_index += 7; // setup to look at next one...
  164. }
  165. if ((rxep == 0) || (txep == 0)) {
  166. Serial.printf("Bluetooth end points not found: %d %d\n", rxep, txep);
  167. return false; // did not find two end points.
  168. }
  169. Serial.printf(" rxep=%d(%d) txep=%d(%d) rx2ep=%d(%d)\n", rxep&15, rx_size_, txep, tx_size_,
  170. rx2ep&15, rx2_size_);
  171. print("BluetoothController, rxep=", rxep & 15);
  172. print("(", rx_size_);
  173. print("), txep=", txep);
  174. print("(", tx_size_);
  175. println(")");
  176. rxpipe_ = new_Pipe(dev, 3, rxep & 15, 1, rx_size_, rx_interval);
  177. if (!rxpipe_) return false;
  178. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  179. if (!txpipe_) {
  180. //free_Pipe(rxpipe_);
  181. return false;
  182. }
  183. rx2pipe_ = new_Pipe(dev, 2, rx2ep & 15, 1, rx2_size_, rx2_interval);
  184. if (!rx2pipe_) {
  185. // Free other pipes...
  186. return false;
  187. }
  188. rxpipe_->callback_function = rx_callback;
  189. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  190. rx2pipe_->callback_function = rx2_callback;
  191. queue_Data_Transfer(rx2pipe_, rx2buf_, rx2_size_, this);
  192. txpipe_->callback_function = tx_callback;
  193. // Send out the reset
  194. device = dev; // yes this is normally done on return from this but should not hurt if we do it here.
  195. sendResetHCI();
  196. pending_control_ = 1; // not sure yet on what we need...
  197. return true;
  198. }
  199. void BluetoothController::disconnect()
  200. {
  201. }
  202. void BluetoothController::control(const Transfer_t *transfer)
  203. {
  204. println(" control callback (bluetooth) ", pending_control_, HEX);
  205. Serial.printf("control callback (bluetooth): %d : ", pending_control_);
  206. uint8_t *buffer = (uint8_t*)transfer->buffer;
  207. for (uint8_t i=0; i < transfer->length; i++) Serial.printf("%x ", buffer[i]);
  208. Serial.printf("\n");
  209. }
  210. /************************************************************/
  211. // Interrupt-based Data Movement
  212. /************************************************************/
  213. void BluetoothController::rx_callback(const Transfer_t *transfer)
  214. {
  215. if (!transfer->driver) return;
  216. ((BluetoothController *)(transfer->driver))->rx_data(transfer);
  217. }
  218. void BluetoothController::rx2_callback(const Transfer_t *transfer)
  219. {
  220. if (!transfer->driver) return;
  221. ((BluetoothController *)(transfer->driver))->rx2_data(transfer);
  222. }
  223. void BluetoothController::tx_callback(const Transfer_t *transfer)
  224. {
  225. if (!transfer->driver) return;
  226. ((BluetoothController *)(transfer->driver))->tx_data(transfer);
  227. }
  228. void BluetoothController::rx_data(const Transfer_t *transfer)
  229. {
  230. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  231. print_hexbytes((uint8_t*)transfer->buffer, len);
  232. Serial.printf("BT rx_data(%d): ", len);
  233. uint8_t *buffer = (uint8_t*)transfer->buffer;
  234. for (uint8_t i=0; i < len; i++) Serial.printf("%x ", buffer[i]);
  235. Serial.printf("\n");
  236. // Note the logical packets returned from the device may be larger
  237. // than can fit in one of our packets, so we will detect this and
  238. // the next read will be continue in or rx_buf_ in the next logical
  239. // location. We will only go into process the next logical state
  240. // when we have the full response read in...
  241. if (rx_packet_data_remaining == 0) { // Previous command was fully handled
  242. rx_packet_data_remaining = rxbuf_[1] + 2; // length of data plus the two bytes at start...
  243. }
  244. // Now see if the data
  245. rx_packet_data_remaining -= len; // remove the length of this packet from length
  246. if (rx_packet_data_remaining == 0) { // read started at beginning of packet so get the total length of packet
  247. switch(rxbuf_[0]) { // Switch on event type
  248. case EV_COMMAND_COMPLETE: //0x0e
  249. handle_hci_command_complete();// Check if command succeeded
  250. break;
  251. case EV_COMMAND_STATUS: //0x0f
  252. handle_hci_command_status();
  253. break;
  254. case EV_INQUIRY_COMPLETE: // 0x01
  255. handle_hci_inquiry_complete();
  256. break;
  257. case EV_INQUIRY_RESULT: // 0x02
  258. handle_hci_inquiry_result();
  259. break;
  260. case EV_CONNECT_COMPLETE: // 0x03
  261. handle_hci_connection_complete();
  262. break;
  263. case EV_INCOMING_CONNECT: // 0x04
  264. handle_hci_incoming_connect();
  265. break;
  266. case EV_DISCONNECT_COMPLETE: // 0x05
  267. handle_hci_disconnect_complete();
  268. break;
  269. case EV_AUTHENTICATION_COMPLETE:// 0x06
  270. handle_hci_authentication_complete();
  271. break;
  272. case EV_PIN_CODE_REQUEST: // 0x16
  273. handle_hci_pin_code_request();
  274. break;
  275. case EV_LINK_KEY_REQUEST: // 0x17
  276. handle_hci_link_key_request();
  277. break;
  278. case EV_LINK_KEY_NOTIFICATION: // 0x18
  279. handle_hci_link_key_notification();
  280. default:
  281. break;
  282. }
  283. // Start read at start of buffer.
  284. //Serial.printf("Queue Start: %x\n", (uint32_t)(rxbuf_));
  285. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  286. } else {
  287. // Continue the read - Todo - maybe verify len == rx_size_
  288. //Serial.printf("Queue Continue: %d %x\n", rx_packet_data_remaining, (uint32_t)(buffer + rx_size_));
  289. queue_Data_Transfer(rxpipe_, buffer + rx_size_, rx_size_, this);
  290. return; // Don't process the message yet as we still have data to receive.
  291. }
  292. }
  293. //===================================================================
  294. // Called when an HCI command completes.
  295. void BluetoothController::handle_hci_command_complete()
  296. {
  297. uint16_t hci_command = rxbuf_[3] + (rxbuf_[4] << 8);
  298. uint8_t buffer_index;
  299. if(!rxbuf_[5]) {
  300. Serial.printf(" Command Completed! \n");
  301. switch (hci_command) {
  302. case HCI_RESET: //0x0c03
  303. break;
  304. case HCI_Set_Event_Filter_Clear: //0x0c05
  305. break;
  306. case HCI_Read_Local_Name: //0x0c14
  307. // received name back... Not sure yet if we received
  308. // full name or just start of it...
  309. {
  310. Serial.printf(" Local name: ");
  311. uint8_t len = rxbuf_[1]+2; // Length field +2 for total bytes read
  312. for (uint8_t i=6; i < len; i++) {
  313. if (rxbuf_[i] == 0) {
  314. break;
  315. }
  316. Serial.printf("%c", rxbuf_[i]);
  317. }
  318. Serial.printf("\n");
  319. }
  320. break;
  321. case Write_Connection_Accept_Timeout: //0x0c16
  322. break;
  323. case HCI_READ_CLASS_OF_DEVICE: // 0x0c23
  324. break;
  325. case HCI_Read_Voice_Setting: //0x0c25
  326. break;
  327. case HCI_Read_Number_Of_Supported_IAC: //0x0c38
  328. break;
  329. case HCI_Read_Current_IAC_LAP: //0x0c39
  330. break;
  331. case HCI_WRITE_INQUIRY_MODE: //0x0c45
  332. break;
  333. case HCI_Read_Inquiry_Response_Transmit_Power_Level: //0x0c58
  334. break;
  335. case HCI_Read_Local_Supported_Features: //0x1003
  336. // Remember the features supported by local...
  337. for (buffer_index = 0; buffer_index < 8; buffer_index++) {
  338. features[buffer_index] = rxbuf_[buffer_index+6];
  339. }
  340. break;
  341. case HCI_Read_Buffer_Size: // 0x1005
  342. break;
  343. case HCI_Read_BD_ADDR: //0x1009
  344. {
  345. Serial.printf(" BD Addr");
  346. for(uint8_t i = 0; i < 6; i++) {
  347. my_bdaddr[i] = rxbuf_[6 + i];
  348. Serial.printf(":%x", my_bdaddr[i]);
  349. }
  350. Serial.printf("\n");
  351. }
  352. break;
  353. case HCI_Read_Local_Version_Information: //0x1001
  354. hciVersion = rxbuf_[6]; // Should do error checking above...
  355. Serial.printf(" Local Version: %x\n", hciVersion);
  356. break;
  357. case HCI_Read_Local_Supported_Commands: //0x1002
  358. break;
  359. case HCI_LE_Read_Buffer_Size: //0x2002
  360. break;
  361. case HCI_LE_Read_Local_supported_Features: //0x2003
  362. break;
  363. case HCI_LE_Supported_States: //0x201c
  364. break;
  365. case HCI_Read_Local_Extended_Features: //0x1004
  366. break;
  367. case HCI_Set_Event_Mask: //0x0c01
  368. break;
  369. case HCI_Read_Stored_Link_Key: //0x0c0d
  370. break;
  371. case HCI_Write_Default_Link_Policy_Settings: //0x080f
  372. break;
  373. case HCI_Read_Page_Scan_Activity: //0x0c1b
  374. break;
  375. case HCI_Read_Page_Scan_Type: //0x0c46
  376. break;
  377. case HCI_LE_SET_EVENT_MASK: //0x2001
  378. break;
  379. case HCI_LE_READ_ADV_TX_POWER: //0x2007
  380. break;
  381. case HCI_LE_READ_WHITE_LIST_SIZE: //0x200f
  382. break;
  383. case HCI_LE_CLEAR_WHITE_LIST: //0x2010
  384. break;
  385. case HCI_DELETE_STORED_LINK_KEY: //0x0c12
  386. break;
  387. case HCI_WRITE_LOCAL_NAME: //0x0c13
  388. break;
  389. case HCI_WRITE_SCAN_ENABLE: //0x0c1a
  390. break;
  391. case HCI_WRITE_SSP_MODE: //0x0c56
  392. break;
  393. case HCI_WRITE_EIR: //0x0c52
  394. break;
  395. case HCI_WRITE_LE_HOST_SUPPORTED: //0x0c6d
  396. break;
  397. case HCI_LE_SET_SCAN_RSP_DATA: //0x2009
  398. break;
  399. }
  400. // And queue up the next command
  401. queue_next_hci_command();
  402. } else {
  403. Serial.printf(" Command(%x) Completed - Error: %d! \n", hci_command, rxbuf_[5]);
  404. // BUGBUG:: probably need to queue something?
  405. }
  406. }
  407. void BluetoothController::queue_next_hci_command()
  408. {
  409. // Ok We completed a command now see if we need to queue another command
  410. // Still probably need to reorganize...
  411. switch (pending_control_) {
  412. case 1:
  413. sendHDCWriteClassOfDev();
  414. pending_control_++;
  415. break;
  416. case 2:
  417. sendHCIReadBDAddr();
  418. pending_control_++;
  419. break;
  420. case 3:
  421. sendHCIReadLocalVersionInfo();
  422. pending_control_++;
  423. break;
  424. case 4:
  425. sendHCI_INQUIRY();
  426. pending_control_++;
  427. break;
  428. case PC_INQUIRE_CANCEL:
  429. // lets try to create a connection...
  430. sendHCICreateConnection();
  431. pending_control_++;
  432. break;
  433. case PC_AUTHENTICATION_REQUESTED:
  434. break;
  435. case PC_LINK_KEY_NEGATIVE:
  436. break;
  437. case PC_PIN_CODE_REPLY:
  438. #if 0
  439. sendHCIReadLocalSupportedFeatures();
  440. pending_control_++;
  441. break;
  442. case 4:
  443. sendHCIReadBufferSize();
  444. pending_control_++;
  445. break;
  446. case 5:
  447. sendHCIReadClassOfDevice();
  448. pending_control_++;
  449. break;
  450. case 6:
  451. sendHCIReadLocalName();
  452. pending_control_++;
  453. break;
  454. case 7:
  455. sendHCIReadVoiceSettings();
  456. pending_control_++; // go to next state
  457. break;
  458. case 8:
  459. sendHCICommandReadNumberSupportedIAC();
  460. pending_control_++;
  461. break;
  462. case 9:
  463. sendHCICommandReadCurrentIACLAP();
  464. pending_control_++;
  465. break;
  466. case 10:
  467. sendHCIClearAllEventFilters();
  468. pending_control_++;
  469. break;
  470. case 11:
  471. sendHCIWriteConnectionAcceptTimeout();
  472. pending_control_++;
  473. break;
  474. case 12:
  475. // 0x02 0x20 0x00 => 0x0E 0x07 0x01 0x02 0x20 0x00 0x00 0x00 0x00 (OGF=8???)
  476. sendHCILEReadBufferSize();
  477. pending_control_++;
  478. break;
  479. case 13:
  480. // 0x03 0x20 0x00 => 0x0E 0x0C 0x01 0x03 0x20 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  481. sendHCILEReadLocalSupportedFeatures();
  482. pending_control_++;
  483. break;
  484. case 14:
  485. // 0x1C 0x20 0x00 => 0x0E 0x0C 0x01 0x1C 0x20 0x00 0xFF 0xFF 0xFF 0x1F 0x00 0x00 0x00 0x00
  486. sendHCILEReadSupportedStates();
  487. pending_control_++;
  488. break;
  489. case 15:
  490. sendHCIReadLocalSupportedCommands();
  491. pending_control_++;
  492. break;
  493. // Try skipping...
  494. // 0x52 0x0C 0xF1 0x00 0x00 ... <several lines of 0x00
  495. case 16:
  496. // 0x45 0x0C 0x01 0x02
  497. sendHCIWriteInquiryMode();
  498. pending_control_++;
  499. break;
  500. case 17:
  501. sendHCIReadInquiryResponseTransmitPowerLevel();
  502. pending_control_++;
  503. break;
  504. case 18:
  505. sendHCIReadLocalExtendedFeatures(1);// 0x1004
  506. pending_control_++;
  507. break;
  508. case 19:
  509. sendHCISetEventMask();// 0x0c01
  510. pending_control_++;
  511. break;
  512. case 20:
  513. sendHCIReadStoredLinkKey();// 0x0c0d
  514. pending_control_++;
  515. break;
  516. case 21:
  517. sendHCIWriteDefaultLinkPolicySettings();// 0x080f
  518. pending_control_++;
  519. break;
  520. case 22:
  521. sendHCIReadPageScanActivity();// 0x0c1b
  522. pending_control_++;
  523. break;
  524. case 23:
  525. sendHCIReadPageScanType();// 0x0c46
  526. pending_control_++;
  527. break;
  528. case 24:
  529. sendHCILESetEventMask();// 0x2001
  530. pending_control_++;
  531. break;
  532. case 25:
  533. sendHCILEReadADVTXPower();// 0x2007
  534. pending_control_++;
  535. break;
  536. case 26:
  537. sendHCIEReadWhiteListSize();// 0x200f
  538. pending_control_++;
  539. break;
  540. case 27:
  541. sendHCILEClearWhiteList();// 0x2010
  542. pending_control_++;
  543. break;
  544. case 28:
  545. sendHCIDeleteStoredLinkKey();// 0x0c12
  546. pending_control_++;
  547. break;
  548. case 29:
  549. sendHCIWriteLocalName();// 0x0c13
  550. pending_control_++;
  551. break;
  552. case 30:
  553. sendHCIWriteScanEnable(2);// 0x0c1a (0=off, 1=scan, 2=page)
  554. pending_control_++;
  555. break;
  556. case 31:
  557. sendHCIWriteSSPMode(1);// 0x0c56
  558. pending_control_++;
  559. break;
  560. case 32:
  561. sendHCIWriteEIR();// 0x0c52
  562. pending_control_++;
  563. break;
  564. case 33:
  565. sendHCIWriteLEHostSupported();// 0x0c6d
  566. pending_control_++;
  567. break;
  568. case 34:
  569. sendHCILESetAdvData();// 0x2008
  570. pending_control_++;
  571. break;
  572. case 35:
  573. sendHCILESetScanRSPData();// 0x2009
  574. pending_control_++;
  575. break;
  576. case 36:
  577. #endif
  578. // 0x09 0x20 0x20 0x0D 0x0C 0x09 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x00 0x00 0...
  579. default:
  580. break;
  581. }
  582. }
  583. void BluetoothController::handle_hci_command_status()
  584. {
  585. // <event type><param count><status><num packets allowed to be sent><CMD><CMD>
  586. uint16_t hci_command = rxbuf_[4] + (rxbuf_[5] << 8);
  587. Serial.printf(" Command %x Status %x\n", hci_command, rxbuf_[2]);
  588. /* switch (hci_command) {
  589. case HCI_RESET: //0x0c03
  590. break;
  591. case HCI_INQUIRY:
  592. // will probably need to check status and break out of here if error
  593. //BT rx_data(6): f 4 0 1 1 4
  594. }
  595. */
  596. }
  597. void BluetoothController::handle_hci_inquiry_result()
  598. {
  599. // 2 f 1 79 22 23 a c5 cc 1 2 0 40 25 0 3b 2
  600. // Wondered if multiple items if all of the BDADDR are first then next field...
  601. // looks like it is that way...
  602. // Section 7.7.2
  603. Serial.printf(" Inquiry Result - Count: %d\n", rxbuf_[2]);
  604. for (uint8_t i=0; i < rxbuf_[2]; i++) {
  605. uint8_t index_bd = 3 + (i*6);
  606. uint8_t index_ps = 3 + (6*rxbuf_[2]) + i;
  607. uint8_t index_class = 3 + (9*rxbuf_[2]) + i;
  608. uint8_t index_clock_offset = 3 + (12*rxbuf_[2]) + i;
  609. uint32_t bluetooth_class = rxbuf_[index_class] + ((uint32_t)rxbuf_[index_class+1] << 8) + ((uint32_t)rxbuf_[index_class+2] << 16);
  610. Serial.printf(" BD:%x:%x:%x:%x:%x:%x, PS:%d, class: %x\n",
  611. rxbuf_[index_bd],rxbuf_[index_bd+1],rxbuf_[index_bd+2],rxbuf_[index_bd+3],rxbuf_[index_bd+4],rxbuf_[index_bd+5],
  612. rxbuf_[index_ps], bluetooth_class);
  613. // See if we know the class
  614. if ((bluetooth_class & 0xff00) == 0x2500) {
  615. Serial.printf(" Peripheral device\n");
  616. if (bluetooth_class & 0x80) Serial.printf(" Mouse\n");
  617. if (bluetooth_class & 0x40) Serial.printf(" Keyboard\n");
  618. // BUGBUG, lets hard code to go to new state...
  619. for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = rxbuf_[index_bd+i];
  620. device_class_ = bluetooth_class;
  621. device_ps_repetion_mode_ = rxbuf_[index_ps]; // mode
  622. device_clock_offset_[0] = rxbuf_[index_clock_offset];
  623. device_clock_offset_[1] = rxbuf_[index_clock_offset+1];
  624. // Now we need to bail from inquiry and setup to try to connect...
  625. sendHCIInquiryCancel();
  626. pending_control_ = PC_INQUIRE_CANCEL;
  627. break;
  628. }
  629. }
  630. }
  631. void BluetoothController::handle_hci_inquiry_complete() {
  632. Serial.printf(" Inquiry Complete - status: %d\n", rxbuf_[2]);
  633. //
  634. }
  635. void BluetoothController::handle_hci_connection_complete() {
  636. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  637. // ST CH CH BD BD BD BD BD BD LT EN
  638. // 03 0b 04 00 00 40 25 00 58 4b 00 01 00
  639. device_connection_handle_ = rxbuf_[3]+ (uint16_t)(rxbuf_[4]<<8);
  640. Serial.printf(" Connection Complete - ST:%x LH:%x\n", rxbuf_[2], device_connection_handle_);
  641. sendHCIAuthenticationRequested();
  642. pending_control_ = PC_AUTHENTICATION_REQUESTED;
  643. }
  644. void BluetoothController::handle_hci_incoming_connect() {
  645. // BD BD BD BD BD BD CL CL CL LT
  646. // 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x40 0x05 0x00 0x01
  647. uint32_t class_of_device = rxbuf_[8] + (uint16_t)(rxbuf_[9]<<8) + (uint32_t)(rxbuf_[10]<<16);
  648. Serial.printf(" Event: Incoming Connect - %x:%x:%x:%x:%x:%x CL:%x LT:%x\n",
  649. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], class_of_device, rxbuf_[11]);
  650. if (((class_of_device & 0xff00) == 0x2500) || ((class_of_device & 0xff00) == 0x500)) {
  651. Serial.printf(" Peripheral device\n");
  652. if (class_of_device & 0x80) Serial.printf(" Mouse\n");
  653. if (class_of_device & 0x40) Serial.printf(" Keyboard\n");
  654. }
  655. // sendHCIAuthenticationRequested();
  656. // pending_control_ = PC_AUTHENTICATION_REQUESTED;
  657. }
  658. void BluetoothController::handle_hci_pin_code_request() {
  659. // 0x16 0x06 0x79 0x22 0x23 0x0A 0xC5 0xCC
  660. Serial.printf(" Event: Pin Code Request %x:%x:%x:%x:%x:%x\n",
  661. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  662. sendHCIPinCodeReply();
  663. pending_control_ = PC_PIN_CODE_REPLY;
  664. }
  665. void BluetoothController::handle_hci_link_key_request() {
  666. // 17 6 79 22 23 a c5 cc
  667. Serial.printf(" Event: Link Key Request %x:%x:%x:%x:%x:%x\n",
  668. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  669. // Now here is where we need to decide to say we have key or tell them to
  670. // cancel key... right now hard code to cancel...
  671. sendHCILinkKeyNegativeReply();
  672. pending_control_ = PC_LINK_KEY_NEGATIVE;
  673. }
  674. void BluetoothController::handle_hci_link_key_notification() {
  675. // 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 4
  676. // 18 17 79 22 23 a c5 cc 5e 98 d4 5e bb 15 66 da 67 fe 4f 87 2b 61 46 b4 0
  677. Serial.printf(" Event: Link Key Notificaton %x:%x:%x:%x:%x:%x Type:%x\n key:",
  678. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], rxbuf_[24]);
  679. for (uint8_t i = 8; i < 24; i++) Serial.printf("%02x ", rxbuf_[i]);
  680. Serial.printf("\n");
  681. // Now here is where we need to decide to say we have key or tell them to
  682. // cancel key... right now hard code to cancel...
  683. }
  684. void BluetoothController::handle_hci_disconnect_complete()
  685. {
  686. //5 4 0 48 0 13
  687. Serial.printf(" Event: HCI Disconnect complete(%d): handle: %x, reason:%x\n", rxbuf_[2],
  688. rxbuf_[3]+(rxbuf_[4]<<8), rxbuf_[5]);
  689. }
  690. void BluetoothController::handle_hci_authentication_complete()
  691. {
  692. // 6 3 13 48 0
  693. Serial.printf(" Event: HCI Authentication complete(%d): handle: %x\n", rxbuf_[2],
  694. rxbuf_[3]+(rxbuf_[4]<<8));
  695. // Start up lcap connection...
  696. connection_rxid_ = 0;
  697. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
  698. }
  699. void BluetoothController::rx2_data(const Transfer_t *transfer)
  700. {
  701. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  702. Serial.printf("\n=====================\nBT rx2_data(%d): ", len);
  703. uint8_t *buffer = (uint8_t*)transfer->buffer;
  704. for (uint8_t i=0; i < len; i++) Serial.printf("%x ", buffer[i]);
  705. Serial.printf("\n");
  706. // call backs. See if this is an L2CAP reply. example
  707. // HCI | l2cap
  708. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  709. // BUGBUG need to do more verification, like the handle
  710. uint16_t hci_length = buffer[2] + ((uint16_t)buffer[3]<<8);
  711. uint16_t l2cap_length = buffer[4] + ((uint16_t)buffer[5]<<8);
  712. uint16_t rsp_packet_length = buffer[10] + ((uint16_t)buffer[11]<<8);
  713. if ((hci_length == (l2cap_length + 4)) /*&& (hci_length == (rsp_packet_length+8))*/) {
  714. // All the lengths appear to be correct... need to do more...
  715. switch (buffer[8]) {
  716. case L2CAP_CMD_CONNECTION_RESPONSE:
  717. process_l2cap_connection_response(&buffer[8]);
  718. break;
  719. case L2CAP_CMD_CONFIG_REQUEST:
  720. process_l2cap_config_reequest(&buffer[8]);
  721. break;
  722. case L2CAP_CMD_CONFIG_RESPONSE:
  723. process_l2cap_config_response(&buffer[8]);
  724. break;
  725. case HID_THDR_DATA_INPUT:
  726. handleHIDTHDRData(buffer); // Pass the whole buffer...
  727. break;
  728. }
  729. }
  730. // Queue up for next read...
  731. queue_Data_Transfer(rx2pipe_, rx2buf_, rx2_size_, this);
  732. }
  733. void BluetoothController::sendHCICommand(uint16_t hciCommand, uint16_t cParams, const uint8_t* data)
  734. {
  735. txbuf_[0] = hciCommand & 0xff;
  736. txbuf_[1] = (hciCommand >> 8) & 0xff;
  737. txbuf_[2] = cParams;
  738. if (cParams) {
  739. memcpy(&txbuf_[3], data, cParams); // copy in the commands parameters.
  740. }
  741. uint8_t nbytes = cParams+3;
  742. for (uint8_t i=0; i< nbytes; i++) Serial.printf("%02x ", txbuf_[i]);
  743. Serial.printf(")\n");
  744. mk_setup(setup, 0x20, 0x0, 0, 0, nbytes);
  745. queue_Control_Transfer(device, &setup, txbuf_, this);
  746. }
  747. //---------------------------------------------
  748. void BluetoothController::sendHCI_INQUIRY() {
  749. // Start unlimited inqury, set timeout to max and
  750. Serial.printf("HCI_INQUIRY called (");
  751. static const uint8_t hci_inquiry_data[ ] = {
  752. 0x33, 0x8B, 0x9E, // Bluetooth assigned number LAP 0x9E8B33 General/unlimited inquiry Access mode
  753. 0x30, 0xa}; // Max inquiry time little over minute and up to 10 responses
  754. sendHCICommand(HCI_INQUIRY, sizeof(hci_inquiry_data), hci_inquiry_data);
  755. }
  756. //---------------------------------------------
  757. void BluetoothController::sendHCIInquiryCancel() {
  758. Serial.printf("HCI_INQUIRY_CANCEL called (");
  759. sendHCICommand(HCI_INQUIRY_CANCEL, 0, nullptr);
  760. }
  761. //---------------------------------------------
  762. void BluetoothController::sendHCICreateConnection() {
  763. Serial.printf("HCI_CREATE_CONNECTION called (");
  764. uint8_t connection_data[13];
  765. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  766. // BD BD BD BD BD BD PT PT PRS 0 CS CS ARS
  767. //0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  768. //0x05 0x04 0x0D 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  769. // 05 04 0d 40 25 00 c4 01 00 18 cc 01 00 00 00 00
  770. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  771. connection_data[6] = 0x18; //DM1/DH1
  772. connection_data[7] = 0xcc; //
  773. connection_data[8] = device_ps_repetion_mode_; // from device
  774. connection_data[9] = 0; //
  775. connection_data[10] = 0; // clock offset
  776. connection_data[11] = 0; // clock offset
  777. connection_data[12] = 0; // allow role swith no
  778. sendHCICommand(HCI_CREATE_CONNECTION, sizeof(connection_data), connection_data);
  779. }
  780. //---------------------------------------------
  781. void BluetoothController::sendHCIAuthenticationRequested() {
  782. Serial.printf("HCI_AUTH_REQUESTED called (");
  783. uint8_t connection_data[2];
  784. connection_data[0] = device_connection_handle_ & 0xff;
  785. connection_data[1] = (device_connection_handle_>>8) & 0xff;
  786. sendHCICommand(HCI_AUTH_REQUESTED, sizeof(connection_data), connection_data);
  787. }
  788. //---------------------------------------------
  789. void BluetoothController::sendHCILinkKeyNegativeReply() {
  790. Serial.printf("HCI_LINK_KEY_NEG_REPLY called (");
  791. uint8_t connection_data[6];
  792. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  793. sendHCICommand(HCI_LINK_KEY_NEG_REPLY, sizeof(connection_data), connection_data);
  794. }
  795. //---------------------------------------------
  796. // BUGBUG:: hard code string for this pass.
  797. uint8_t pin_code_[] = "0000";
  798. void BluetoothController::sendHCIPinCodeReply() {
  799. // 0x0D 0x04 0x17 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x04 0x30 0x30 0x30 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  800. Serial.printf("HCI_PIN_CODE_REPLY called (");
  801. uint8_t connection_data[23];
  802. uint8_t i;
  803. for (i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  804. for (i=0; pin_code_[i] !=0; i++) connection_data[7+i] = pin_code_[i];
  805. connection_data[6] = i; // remember the length
  806. for (uint8_t i=7+connection_data[6]; i<23; i++) connection_data[i] = 0;
  807. sendHCICommand(HCI_PIN_CODE_REPLY, sizeof(connection_data), connection_data);
  808. }
  809. //---------------------------------------------
  810. void BluetoothController::sendResetHCI() {
  811. Serial.printf("HCI_RESET called (");
  812. sendHCICommand(HCI_RESET, 0, nullptr);
  813. }
  814. void BluetoothController::sendHDCWriteClassOfDev() {
  815. // 0x24 0x0C 0x03 0x04 0x08 0x00
  816. const static uint8_t device_class_data[] = {BT_CLASS_DEVICE & 0xff, (BT_CLASS_DEVICE >> 8) & 0xff, (BT_CLASS_DEVICE >> 16) & 0xff};
  817. Serial.printf("HCI_WRITE_CLASS_OF_DEV called (");
  818. sendHCICommand(HCI_WRITE_CLASS_OF_DEV, sizeof(device_class_data), device_class_data);
  819. }
  820. void BluetoothController::sendHCIClearAllEventFilters() {
  821. static uint8_t clear_filters = 0;
  822. Serial.printf("HCI_Set_Event_Filter_Clear called (");
  823. sendHCICommand(HCI_Set_Event_Filter_Clear, 1, &clear_filters);
  824. }
  825. void BluetoothController::sendHCIReadLocalName() {
  826. Serial.printf("HCI_Read_Local_Name called (");
  827. sendHCICommand(HCI_Read_Local_Name, 0, nullptr);
  828. }
  829. void BluetoothController::sendHCIWriteConnectionAcceptTimeout() {
  830. // BUGBUG: Hard coded timeout here...
  831. Serial.printf("Write_Connection_Accept_Timeout called (");
  832. static const uint8_t timeout[] = {0, 0x7d}; // OCF=16, OGF=3<<2, Parameters=2, 0, 7d
  833. sendHCICommand(Write_Connection_Accept_Timeout, sizeof(timeout), timeout);
  834. }
  835. void BluetoothController::sendHCIReadClassOfDevice() {
  836. Serial.printf("HCI_READ_CLASS_OF_DEVICE called (");
  837. sendHCICommand(HCI_READ_CLASS_OF_DEVICE, 0, nullptr);
  838. }
  839. void BluetoothController::sendHCIReadVoiceSettings() {
  840. Serial.printf("HCI_Read_Voice_Setting called (");
  841. sendHCICommand(HCI_Read_Voice_Setting, 0, nullptr);
  842. }
  843. void BluetoothController::sendHCICommandReadNumberSupportedIAC() {
  844. Serial.printf("HCI_Read_Number_Of_Supported_IAC\n");
  845. sendHCICommand(HCI_Read_Number_Of_Supported_IAC, 0, nullptr);
  846. }
  847. void BluetoothController::sendHCICommandReadCurrentIACLAP() {
  848. Serial.printf("HCI_Read_Current_IAC_LAP called (");
  849. sendHCICommand(HCI_Read_Current_IAC_LAP, 0, nullptr);
  850. }
  851. void BluetoothController::sendHCIReadLocalSupportedFeatures() {
  852. Serial.printf("HCI_Read_Local_Supported_Features (");
  853. sendHCICommand(HCI_Read_Local_Supported_Features, 0, nullptr); // Read local supported features. ?? look up
  854. }
  855. void BluetoothController::sendHCIReadBufferSize() {
  856. Serial.printf("HCI_Read_Buffer_Size called (");
  857. sendHCICommand(HCI_Read_Buffer_Size, 0, nullptr);
  858. }
  859. void BluetoothController::sendHCIReadBDAddr() {
  860. Serial.printf("HCI_Read_BD_ADDR called (");
  861. sendHCICommand(HCI_Read_BD_ADDR, 0, nullptr);
  862. }
  863. void BluetoothController::sendHCIReadLocalVersionInfo() {
  864. Serial.printf("HCI_Read_Local_Version_Information called (");
  865. sendHCICommand(HCI_Read_Local_Version_Information, 0, nullptr);
  866. }
  867. // 0x02 0x10 0x00 => 0x0E 0x44 0x01 0x02 0x10 0x00 0xFF 0xFF 0xFF 0x03 0xFE 0xFF 0xFF 0xFF 0xFF 0xFF
  868. // 0xFF 0xFF 0xF3 0x0F 0xE8 0xFE 0x3F 0xF7 0x83 0xFF 0x1C 0x00 0x00 0x00 0x61 0xF7
  869. // 0xFF 0xFF 0x7F 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  870. // 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  871. // 0x00 0x00 0x00 0x00 0x00 0x00
  872. void BluetoothController::sendHCIReadLocalSupportedCommands() {
  873. Serial.printf("HCI_Read_Local_Supported_Commands called (");
  874. sendHCICommand(HCI_Read_Local_Supported_Commands, 0, nullptr);
  875. }
  876. // 0x02 0x20 0x00 => 0x0E 0x07 0x01 0x02 0x20 0x00 0x00 0x00 0x00 (OGF=8???)
  877. void BluetoothController::sendHCILEReadBufferSize() {
  878. Serial.printf("HCI_LE_Read_Buffer_Size called (");
  879. sendHCICommand(HCI_LE_Read_Buffer_Size, 0, nullptr);
  880. }
  881. // 0x03 0x20 0x00 => 0x0E 0x0C 0x01 0x03 0x20 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  882. void BluetoothController::sendHCILEReadLocalSupportedFeatures(){
  883. Serial.printf("HCI_LE_Read_Local_supported_Features called (");
  884. sendHCICommand(HCI_LE_Read_Local_supported_Features, 0, nullptr);
  885. }
  886. // 0x1C 0x20 0x00 => 0x0E 0x0C 0x01 0x1C 0x20 0x00 0xFF 0xFF 0xFF 0x1F 0x00 0x00 0x00 0x00
  887. void BluetoothController::sendHCILEReadSupportedStates() {
  888. Serial.printf("HCI_LE_Supported_States called (");
  889. sendHCICommand(HCI_LE_Supported_States, 0, nullptr);
  890. }
  891. void BluetoothController::sendHCIWriteInquiryMode() {
  892. static const uint8_t inquiry_mode[] = {2};
  893. Serial.printf("HCI_WRITE_INQUIRY_MODE called (");
  894. sendHCICommand(HCI_WRITE_INQUIRY_MODE, sizeof(inquiry_mode), inquiry_mode);
  895. }
  896. void BluetoothController::sendHCIReadInquiryResponseTransmitPowerLevel() {
  897. Serial.printf("HCI_Read_Inquiry_Response_Transmit_Power_Level called (");
  898. sendHCICommand(HCI_Read_Inquiry_Response_Transmit_Power_Level, 0, nullptr);
  899. }
  900. void BluetoothController::sendHCIReadLocalExtendedFeatures(uint8_t page) {// 0x1004
  901. // 0x04 0x10 0x01 0x01 = 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  902. Serial.printf("HCI_Read_Local_Extended_Features called(");
  903. sendHCICommand(HCI_Read_Local_Extended_Features, 1, &page);
  904. }
  905. void BluetoothController::sendHCISetEventMask() {// 0x0c01
  906. // 0x01 0x0C 0x08 0xFF 0xFF 0xFB 0xFF 0x07 0xF8 0xBF 0x3D
  907. static const uint8_t sent_event_mask[ ] = {0xFF, 0xFF, 0xFB, 0xFF, 0x07, 0xF8, 0xBF, 0x3D};
  908. Serial.printf("HCI_Set_Event_Mask called(");
  909. sendHCICommand(HCI_Set_Event_Mask, sizeof(sent_event_mask), sent_event_mask);
  910. }
  911. void BluetoothController::sendHCIReadStoredLinkKey() {// 0x0c0d
  912. // 0x0D 0x0C 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x01
  913. static const uint8_t read_link[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  914. Serial.printf("HCI_Read_Stored_Link_Key called(");
  915. sendHCICommand(HCI_Read_Stored_Link_Key, sizeof(read_link), read_link);
  916. }
  917. void BluetoothController::sendHCIWriteDefaultLinkPolicySettings() {// 0x080f
  918. // 0x0F 0x08 0x02 0x0F 0x00
  919. static const uint8_t policy[] = {0x0f, 0x00};
  920. Serial.printf("HCI_Write_Default_Link_Policy_Settings called(");
  921. sendHCICommand(HCI_Write_Default_Link_Policy_Settings, sizeof(policy), policy);
  922. }
  923. void BluetoothController::sendHCIReadPageScanActivity() {// 0x0c1b
  924. // 0x1B 0x0C 0x00
  925. Serial.printf("HCI_Read_Page_Scan_Activity called(");
  926. sendHCICommand(HCI_Read_Page_Scan_Activity, 0, nullptr);
  927. }
  928. void BluetoothController::sendHCIReadPageScanType() {// 0x0c46
  929. // 0x46 0x0C 0x00
  930. Serial.printf("HCI_Read_Page_Scan_Type called(");
  931. sendHCICommand(HCI_Read_Page_Scan_Type, 0, nullptr);
  932. }
  933. void BluetoothController::sendHCILESetEventMask() {// 0x2001
  934. // 0x01 0x20 0x08 0x1F 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  935. Serial.printf("HCI_LE_SET_EVENT_MASK called(");
  936. static const uint8_t mask[] = {0, 0, 0, 0, 0, 0};
  937. sendHCICommand(HCI_LE_SET_EVENT_MASK, sizeof(mask), mask);
  938. }
  939. void BluetoothController::sendHCILEReadADVTXPower() {// 0x2007
  940. // 0x07 0x20 0x00
  941. Serial.printf("HCI_LE_READ_ADV_TX_POWER called(");
  942. sendHCICommand(HCI_LE_READ_ADV_TX_POWER, 0, nullptr);
  943. }
  944. void BluetoothController::sendHCIEReadWhiteListSize() {// 0x200f
  945. // 0x0F 0x20 0x00
  946. Serial.printf("HCI_LE_READ_WHITE_LIST_SIZE called(");
  947. sendHCICommand(HCI_LE_READ_WHITE_LIST_SIZE, 0, nullptr);
  948. }
  949. void BluetoothController::sendHCILEClearWhiteList() {// 0x2010
  950. // 0x10 0x20 0x00
  951. Serial.printf("HCI_LE_CLEAR_WHITE_LIST called(");
  952. sendHCICommand(HCI_LE_CLEAR_WHITE_LIST, 0, nullptr);
  953. }
  954. void BluetoothController::sendHCIDeleteStoredLinkKey() {// 0x0c12
  955. // 0x12 0x0C 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x01
  956. static const uint8_t delete_all[] = {0, 0, 0, 0, 0, 0, 0, 1};
  957. Serial.printf("HCI_DELETE_STORED_LINK_KEY called(");
  958. sendHCICommand(HCI_DELETE_STORED_LINK_KEY, sizeof(delete_all), delete_all);
  959. }
  960. void BluetoothController::sendHCIWriteLocalName() {// 0x0c13
  961. // 0x13 0x0C 0xF8 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x00 0x00 ...
  962. Serial.printf("HCI_WRITE_LOCAL_NAME called(");
  963. sendHCICommand(HCI_WRITE_LOCAL_NAME, 0, nullptr);
  964. }
  965. void BluetoothController::sendHCIWriteScanEnable(uint8_t scan_op) {// 0x0c1a
  966. // 0x1A 0x0C 0x01 0x02
  967. Serial.printf("HCI_WRITE_SCAN_ENABLE called(");
  968. sendHCICommand(HCI_WRITE_SCAN_ENABLE, 1, &scan_op);
  969. }
  970. void BluetoothController::sendHCIWriteSSPMode(uint8_t ssp_mode) {// 0x0c56
  971. // 0x56 0x0C 0x01 0x01
  972. Serial.printf("HCI_WRITE_SSP_MODE called(");
  973. sendHCICommand(HCI_WRITE_SSP_MODE, 1, &ssp_mode);
  974. }
  975. void BluetoothController::sendHCIWriteEIR() {// 0x0c52
  976. // 0x52 0x0C 0xF1 0x00 0x0C 0x09 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x02 0x0A 0x04 0x09 0x10 0x02 0x00 0x6B 0x1D 0x46 0x02 0x17 0x05 0x09 0x03 0x00 0x18 0x01 0x18 0x0E 0x11 0x0C 0x11 0x00 0x00 ...
  977. Serial.printf("HCI_WRITE_EIR called(");
  978. sendHCICommand(HCI_WRITE_EIR, 0, nullptr);
  979. }
  980. void BluetoothController::sendHCIWriteLEHostSupported() {// 0x0c6d
  981. // 0x6D 0x0C 0x02 0x01 0x00
  982. Serial.printf("HCI_WRITE_LE_HOST_SUPPORTED called(");
  983. static const uint8_t le_data[] = {0x1, 0x0};
  984. sendHCICommand(HCI_WRITE_LE_HOST_SUPPORTED, sizeof(le_data), le_data);
  985. }
  986. void BluetoothController::sendHCILESetAdvData () { // 0x2008
  987. // 0x08 0x20 0x20 0x03 0x02 0x0A 0x08 0x00 0x00 0x00 0x00 ...
  988. Serial.printf("HCI_LE_SET_ADV_DATA called(");
  989. sendHCICommand(HCI_LE_SET_ADV_DATA, 0, nullptr);
  990. }
  991. void BluetoothController::sendHCILESetScanRSPData() {// 0x2009
  992. // 0x09 0x20 0x20 0x0D 0x0C 0x09 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x00 0x00 0...
  993. Serial.printf("HCI_LE_SET_SCAN_RSP_DATA called(");
  994. sendHCICommand(HCI_LE_SET_SCAN_RSP_DATA, 0, nullptr);
  995. }
  996. // 0x04 0x10 0x01 0x01 = 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  997. // 0x01 0x0C 0x08 0xFF 0xFF 0xFB 0xFF 0x07 0xF8 0xBF 0x3D
  998. // 0x0D 0x0C 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x01
  999. // 0x0F 0x08 0x02 0x0F 0x00
  1000. // 0x1B 0x0C 0x00
  1001. // 0x46 0x0C 0x00
  1002. // 0x01 0x20 0x08 0x1F 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  1003. // 0x07 0x20 0x00
  1004. // 0x0F 0x20 0x00
  1005. // 0x10 0x20 0x00
  1006. // 0x12 0x0C 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x01
  1007. // 0x13 0x0C 0xF8 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x00 0x00 ...
  1008. // 0x1A 0x0C 0x01 0x02
  1009. // 0x56 0x0C 0x01 0x01
  1010. // 0x52 0x0C 0xF1 0x00 0x0C 0x09 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x02 0x0A 0x04 0x09 0x10 0x02 0x00 0x6B 0x1D 0x46 0x02 0x17 0x05 0x09 0x03 0x00 0x18 0x01 0x18 0x0E 0x11 0x0C 0x11 0x00 0x00 ...
  1011. // 0x6D 0x0C 0x02 0x01 0x00
  1012. // 0x08 0x20 0x20 0x03 0x02 0x0A 0x08 0x00 0x00 0x00 0x00 ...
  1013. // 0x09 0x20 0x20 0x0D 0x0C 0x09 0x72 0x61 0x73 0x70 0x62 0x65 0x72 0x72 0x79 0x70 0x69 0x00 0x00 0...
  1014. // l2cap support functions.
  1015. void BluetoothController::sendl2cap_ConnectionRequest(uint16_t handle, uint8_t rxid, uint16_t scid, uint16_t psm) {
  1016. uint8_t l2capbuf[8];
  1017. l2capbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
  1018. l2capbuf[1] = rxid; // Identifier
  1019. l2capbuf[2] = 0x04; // Length
  1020. l2capbuf[3] = 0x00;
  1021. l2capbuf[4] = (uint8_t)(psm & 0xff); // PSM
  1022. l2capbuf[5] = (uint8_t)(psm >> 8);
  1023. l2capbuf[6] = scid & 0xff; // Source CID
  1024. l2capbuf[7] = (scid >> 8) & 0xff;
  1025. Serial.printf("L2CAP_ConnectionRequest called(");
  1026. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1027. }
  1028. void BluetoothController::sendl2cap_ConfigRequest(uint16_t handle, uint8_t rxid, uint16_t dcid) {
  1029. uint8_t l2capbuf[12];
  1030. l2capbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
  1031. l2capbuf[1] = rxid; // Identifier
  1032. l2capbuf[2] = 0x08; // Length
  1033. l2capbuf[3] = 0x00;
  1034. l2capbuf[4] = dcid & 0xff; // Destination CID
  1035. l2capbuf[5] = (dcid >> 8) & 0xff;
  1036. l2capbuf[6] = 0x00; // Flags
  1037. l2capbuf[7] = 0x00;
  1038. l2capbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
  1039. l2capbuf[9] = 0x02; // Config Opt: length
  1040. l2capbuf[10] = 0xFF; // MTU
  1041. l2capbuf[11] = 0xFF;
  1042. Serial.printf("L2CAP_ConfigRequest called(");
  1043. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1044. }
  1045. void BluetoothController::sendl2cap_ConfigResponse(uint16_t handle, uint8_t rxid, uint16_t scid) {
  1046. uint8_t l2capbuf[14];
  1047. l2capbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
  1048. l2capbuf[1] = rxid; // Identifier
  1049. l2capbuf[2] = 0x0A; // Length
  1050. l2capbuf[3] = 0x00;
  1051. l2capbuf[4] = scid & 0xff; // Source CID
  1052. l2capbuf[5] = (scid >> 8) & 0xff;
  1053. l2capbuf[6] = 0x00; // Flag
  1054. l2capbuf[7] = 0x00;
  1055. l2capbuf[8] = 0x00; // Result
  1056. l2capbuf[9] = 0x00;
  1057. l2capbuf[10] = 0x01; // Config
  1058. l2capbuf[11] = 0x02;
  1059. l2capbuf[12] = 0xA0;
  1060. l2capbuf[13] = 0x02;
  1061. Serial.printf("L2CAP_ConfigResponse called(");
  1062. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1063. }
  1064. //*******************************************************************
  1065. //*******************************************************************
  1066. void BluetoothController::tx_data(const Transfer_t *transfer)
  1067. {
  1068. println(" tx_data(bluetooth) ", pending_control_, HEX);
  1069. Serial.printf("tx_data callback (bluetooth): %d : ", pending_control_tx_);
  1070. uint8_t *buffer = (uint8_t*)transfer->buffer;
  1071. for (uint8_t i=0; i < transfer->length; i++) Serial.printf("%x ", buffer[i]);
  1072. Serial.printf("\n");
  1073. switch (pending_control_tx_) {
  1074. case STATE_TX_SEND_CONNECT_INT:
  1075. connection_rxid_++;
  1076. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, interrupt_dcid_, HID_INTR_PSM);
  1077. pending_control_tx_ = 0;
  1078. break;
  1079. }
  1080. }
  1081. //*******************************************************************
  1082. //
  1083. // HCI ACL Packets
  1084. // HCI Handle Low, HCI_Handle_High (PB, BC), Total length low, TLH - HCI ACL Data packet
  1085. // length Low, length high, channel id low, channel id high - L2CAP header
  1086. // code, identifier, length, ... - Control-frame
  1087. /************************************************************/
  1088. /* L2CAP Commands */
  1089. void BluetoothController::sendL2CapCommand(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh)
  1090. {
  1091. txbuf_[0] = handle & 0xff; // HCI handle with PB,BC flag
  1092. txbuf_[1] = (((handle >> 8) & 0x0f) | 0x20);
  1093. txbuf_[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
  1094. txbuf_[3] = (uint8_t)((4 + nbytes) >> 8);
  1095. txbuf_[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
  1096. txbuf_[5] = (uint8_t)(nbytes >> 8);
  1097. txbuf_[6] = channelLow;
  1098. txbuf_[7] = channelHigh;
  1099. if (nbytes) {
  1100. memcpy(&txbuf_[8], data, nbytes); // copy in the commands parameters.
  1101. }
  1102. nbytes = nbytes+8;
  1103. for (uint8_t i=0; i< nbytes; i++) Serial.printf("%02x ", txbuf_[i]);
  1104. Serial.printf(")\n");
  1105. if (!queue_Data_Transfer(txpipe_, txbuf_, nbytes, this)) {
  1106. println("sendL2CapCommand failed");
  1107. }
  1108. }
  1109. // Process the l2cap_connection_response...
  1110. void BluetoothController::process_l2cap_connection_response(uint8_t *data) {
  1111. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1112. uint16_t dcid = data[6]+((uint16_t)data[7] << 8);
  1113. Serial.printf(" L2CAP Connection Response: ID: %d, Dest:%x, Source:%x, Result:%x, Status: %x\n",
  1114. data[1], scid, dcid,
  1115. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1116. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  1117. if (dcid == interrupt_dcid_) {
  1118. interrupt_scid_ = scid;
  1119. Serial.printf(" Interrupt Response\n");
  1120. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1121. } else if (dcid == control_dcid_) {
  1122. control_scid_ = scid;
  1123. Serial.printf(" Control Response\n");
  1124. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1125. }
  1126. }
  1127. void BluetoothController::process_l2cap_config_reequest(uint8_t *data) {
  1128. //48 20 10 0 c 0 1 0 *4 2 8 0 70 0 0 0 1 2 30 0
  1129. uint16_t dcid = data[4]+((uint16_t)data[5] << 8);
  1130. Serial.printf(" L2CAP config Request: ID: %d, Dest:%x, Flags:%x, Options: %x %x %x %x\n",
  1131. data[1], dcid, data[6]+((uint16_t)data[7] << 8),
  1132. data[8], data[9], data[10], data[11]);
  1133. // Now see which dest was specified
  1134. if (dcid == control_dcid_) {
  1135. Serial.printf(" Control Configuration request\n");
  1136. sendl2cap_ConfigResponse(device_connection_handle_, data[1], control_scid_);
  1137. } else if (dcid == interrupt_dcid_) {
  1138. Serial.printf(" Interrupt Configuration request\n");
  1139. sendl2cap_ConfigResponse(device_connection_handle_, data[1], interrupt_scid_);
  1140. }
  1141. }
  1142. void BluetoothController::process_l2cap_config_response(uint8_t *data) {
  1143. // 48 20 12 0 e 0 1 0 5 0 a 0 70 0 0 0 0 0 1 2 30 0
  1144. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1145. Serial.printf(" L2CAP config Response: ID: %d, Source:%x, Flags:%x, Result:%x, Config: %x\n",
  1146. data[1], scid, data[6]+((uint16_t)data[7] << 8),
  1147. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1148. if (scid == control_dcid_) {
  1149. // Set HID Boot mode
  1150. setHIDProtocol(HID_BOOT_PROTOCOL);
  1151. // Tell system we will next need to setup connection for the interrupt
  1152. pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
  1153. } else if (scid == interrupt_dcid_) {
  1154. // Enable SCan to page mode
  1155. sendHCIWriteScanEnable(2);
  1156. }
  1157. }
  1158. void BluetoothController::setHIDProtocol(uint8_t protocol) {
  1159. // Should verify protocol is boot or report
  1160. uint8_t l2capbuf[1];
  1161. l2capbuf[0] = 0x70 | protocol; // Set Protocol, see Bluetooth HID specs page 33
  1162. Serial.printf("Set HID Protocol %d (", protocol);
  1163. sendL2CapCommand(device_connection_handle_, l2capbuf, sizeof(l2capbuf), control_scid_ & 0xff, control_scid_ >> 8);
  1164. }
  1165. void BluetoothController::handleHIDTHDRData(uint8_t *data) {
  1166. // Example
  1167. // T HID data
  1168. //48 20 d 0 9 0 71 0 a1 3 8a cc c5 a 23 22 79
  1169. uint16_t len = data[4] + ((uint16_t)data[5] << 8);
  1170. Serial.printf("HID HDR Data: len: %d, Type: %d\n", len, data[9]);
  1171. // ??? How to parse??? Use HID object???
  1172. switch (data[9]) {
  1173. case 1:
  1174. Serial.printf(" Keyboard report type\n");
  1175. break;
  1176. case 2:
  1177. Serial.printf(" Mouse report type\n");
  1178. break;
  1179. case 3:
  1180. Serial.printf(" Combo keyboard/pointing\n");
  1181. break;
  1182. default:
  1183. Serial.printf(" Unknown report\n");
  1184. }
  1185. }