Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1508 lines
54KB

  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_//#define DEBUG_BT
  30. #define DEBUG_BT
  31. #define DEBUG_BT_VERBOSE
  32. #ifndef DEBUG_BT
  33. #undef DEBUG_BT_VERBOSE
  34. void inline DBGPrintf(...) {};
  35. #else
  36. #define DBGPrintf USBHDBGSerial.printf
  37. #endif
  38. #ifndef DEBUG_BT_VERBOSE
  39. void inline VDBGPrintf(...) {};
  40. #else
  41. #define VDBGPrintf USBHDBGSerial.printf
  42. #endif
  43. /************************************************************/
  44. // Define HCI Commands OGF HIgh byte OCF is low byte...
  45. // Actually shifted values...
  46. /************************************************************/
  47. #define HCI_INQUIRY 0x0401
  48. #define HCI_INQUIRY_CANCEL 0x0402
  49. #define HCI_CREATE_CONNECTION 0x0405
  50. #define HCI_OP_ACCEPT_CONN_REQ 0x0409
  51. #define HCI_LINK_KEY_NEG_REPLY 0x040C
  52. #define HCI_PIN_CODE_REPLY 0x040D
  53. #define HCI_AUTH_REQUESTED 0x0411
  54. #define HCI_OP_REMOTE_NAME_REQ 0x0419
  55. #define HCI_OP_REMOTE_NAME_REQ_CANCEL 0x041a
  56. #define HCI_OP_READ_REMOTE_FEATURES 0x041b
  57. #define HCI_OP_READ_REMOTE_VERSION_INFORMATION 0x041D
  58. #define HCI_Write_Default_Link_Policy_Settings 0x080f
  59. #define HCI_Set_Event_Mask 0x0c01
  60. #define HCI_RESET 0x0c03
  61. #define HCI_Set_Event_Filter_Clear 0x0c05
  62. #define HCI_Read_Local_Name 0x0c14
  63. #define HCI_Read_Stored_Link_Key 0x0c0d
  64. #define HCI_DELETE_STORED_LINK_KEY 0x0c12
  65. #define HCI_WRITE_LOCAL_NAME 0x0c13
  66. #define Write_Connection_Accept_Timeout 0x0c16
  67. #define HCI_WRITE_SCAN_ENABLE 0x0c1a
  68. #define HCI_Read_Page_Scan_Activity 0x0c1b
  69. #define HCI_READ_CLASS_OF_DEVICE 0x0c23
  70. #define HCI_WRITE_CLASS_OF_DEV 0x0C24
  71. #define HCI_Read_Voice_Setting 0x0c25
  72. #define HCI_Read_Number_Of_Supported_IAC 0x0c38
  73. #define HCI_Read_Current_IAC_LAP 0x0c39
  74. #define HCI_WRITE_INQUIRY_MODE 0x0c45
  75. #define HCI_Read_Page_Scan_Type 0x0c46
  76. #define HCI_WRITE_EIR 0x0c52
  77. #define HCI_WRITE_SSP_MODE 0x0c56
  78. #define HCI_Read_Inquiry_Response_Transmit_Power_Level 0x0c58
  79. #define HCI_WRITE_LE_HOST_SUPPORTED 0x0c6d
  80. #define HCI_Read_Local_Supported_Features 0x1003
  81. #define HCI_Read_Local_Extended_Features 0x1004
  82. #define HCI_Read_Buffer_Size 0x1005
  83. #define HCI_Read_BD_ADDR 0x1009
  84. #define HCI_Read_Local_Version_Information 0x1001
  85. #define HCI_Read_Local_Supported_Commands 0x1002
  86. #define HCI_LE_SET_EVENT_MASK 0x2001
  87. #define HCI_LE_Read_Buffer_Size 0x2002
  88. #define HCI_LE_Read_Local_supported_Features 0x2003
  89. #define HCI_LE_READ_ADV_TX_POWER 0x2007
  90. #define HCI_LE_SET_ADV_DATA 0x2008
  91. #define HCI_LE_SET_SCAN_RSP_DATA 0x2009
  92. #define HCI_LE_READ_WHITE_LIST_SIZE 0x200f
  93. #define HCI_LE_CLEAR_WHITE_LIST 0x2010
  94. #define HCI_LE_Supported_States 0x201c
  95. /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
  96. #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
  97. #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
  98. // Used For Connection Response
  99. #define PENDING 0x01
  100. #define SUCCESSFUL 0x00
  101. /* L2CAP signaling commands */
  102. #define L2CAP_CMD_COMMAND_REJECT 0x01
  103. #define L2CAP_CMD_CONNECTION_REQUEST 0x02
  104. #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
  105. #define L2CAP_CMD_CONFIG_REQUEST 0x04
  106. #define L2CAP_CMD_CONFIG_RESPONSE 0x05
  107. #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
  108. #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
  109. #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
  110. #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
  111. #define HID_THDR_DATA_INPUT 0xa1
  112. // HID stuff
  113. #define HID_BOOT_PROTOCOL 0x00
  114. #define HID_RPT_PROTOCOL 0x01
  115. /* HCI Events */
  116. enum {EV_INQUIRY_COMPLETE= 0x01,EV_INQUIRY_RESULT= 0x02,EV_CONNECT_COMPLETE= 0x03,EV_INCOMING_CONNECT= 0x04,EV_DISCONNECT_COMPLETE= 0x05
  117. ,EV_AUTHENTICATION_COMPLETE= 0x06,EV_REMOTE_NAME_COMPLETE= 0x07,EV_ENCRYPTION_CHANGE= 0x08,EV_CHANGE_CONNECTION_LINK= 0x09,EV_ROLE_CHANGED= 0x12
  118. ,EV_NUM_COMPLETE_PKT= 0x13,EV_PIN_CODE_REQUEST= 0x16,EV_LINK_KEY_REQUEST= 0x17,EV_LINK_KEY_NOTIFICATION= 0x18,EV_DATA_BUFFER_OVERFLOW= 0x1A
  119. ,EV_MAX_SLOTS_CHANGE= 0x1B,EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE= 0x0C,EV_QOS_SETUP_COMPLETE= 0x0D,EV_COMMAND_COMPLETE= 0x0E,EV_COMMAND_STATUS= 0x0F
  120. ,EV_LOOPBACK_COMMAND= 0x19,EV_PAGE_SCAN_REP_MODE= 0x20, HCI_Extended_Inquiry_Result=0x2F };
  121. // different modes
  122. enum {PC_RESET = 1, PC_WRITE_CLASS_DEVICE, PC_READ_BDADDR, PC_READ_LOCAL_VERSION,
  123. PC_SEND_WRITE_INQUIRE_MODE, PC_SEND_SET_EVENT_MASK, PC_SEND_INQUIRE,
  124. PC_INQUIRE_CANCEL=100, PC_AUTHENTICATION_REQUESTED=110, PC_LINK_KEY_NEGATIVE=120, PC_PIN_CODE_REPLY=130,
  125. PC_WRITE_SCAN_PAGE=200};
  126. //////////////
  127. //////////////
  128. // Setup some states for the TX pipe where we need to chain messages
  129. enum {STATE_TX_SEND_CONNECT_INT=200, STATE_TX_SEND_CONECT_RSP_SUCCESS, STATE_TX_SEND_CONFIG_REQ, STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS, STATE_TX_SEND_CONFIG_ISR_REQ};
  130. // This is a list of all the drivers inherited from the BTHIDInput class.
  131. // Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
  132. // on this list even when they have claimed a top level collection.
  133. BTHIDInput * BluetoothController::available_bthid_drivers_list = NULL;
  134. void BluetoothController::driver_ready_for_bluetooth(BTHIDInput *driver)
  135. {
  136. driver->next = NULL;
  137. if (available_bthid_drivers_list == NULL) {
  138. available_bthid_drivers_list = driver;
  139. } else {
  140. BTHIDInput *last = available_bthid_drivers_list;
  141. while (last->next) last = last->next;
  142. last->next = driver;
  143. }
  144. }
  145. // When a new top level collection is found, this function asks drivers
  146. // if they wish to claim it. The driver taking ownership of the
  147. // collection is returned, or NULL if no driver wants it.
  148. BTHIDInput * BluetoothController::find_driver(uint32_t device_type, uint8_t *remoteName)
  149. {
  150. USBHDBGSerial.printf("BluetoothController::find_driver");
  151. BTHIDInput *driver = available_bthid_drivers_list;
  152. while (driver) {
  153. USBHDBGSerial.printf(" driver %x\n", (uint32_t)driver);
  154. if (driver->claim_bluetooth(this, device_type, remoteName)) {
  155. USBHDBGSerial.printf(" *** Claimed ***\n");
  156. return driver;
  157. }
  158. driver = driver->next;
  159. }
  160. return NULL;
  161. }
  162. //12 01 00 02 FF 01 01 40 5C 0A E8 21 12 01 01 02 03 01
  163. //VendorID = 0A5C, ProductID = 21E8, Version = 0112
  164. //Class/Subclass/Protocol = 255 / 1 / 1
  165. BluetoothController::product_vendor_mapping_t BluetoothController::pid_vid_mapping[] = {
  166. { 0xA5C, 0x21E8 }};
  167. /************************************************************/
  168. // Initialization and claiming of devices & interfaces
  169. /************************************************************/
  170. void BluetoothController::init()
  171. {
  172. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  173. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  174. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  175. driver_ready_for_device(this);
  176. }
  177. bool BluetoothController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  178. {
  179. // only claim at device level
  180. println("BluetoothController claim this=", (uint32_t)this, HEX);
  181. if (type != 0) return false; // claim at the device level
  182. // Lets try to support the main USB Bluetooth class...
  183. // http://www.usb.org/developers/defined_class/#BaseClassE0h
  184. if (dev->bDeviceClass != 0xe0) {
  185. bool special_case_device = false;
  186. for (uint8_t i=0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  187. if ((pid_vid_mapping[i].idVendor == dev->idVendor) && (pid_vid_mapping[i].idProduct == dev->idProduct)) {
  188. special_case_device = true;
  189. break;
  190. }
  191. }
  192. if (!special_case_device) return false;
  193. }
  194. if ((dev->bDeviceSubClass != 1) || (dev->bDeviceProtocol != 1)) return false; // Bluetooth Programming Interface
  195. DBGPrintf("BluetoothController claim this=%x vid:pid=%x:%x\n ", (uint32_t)this, dev->idVendor, dev->idProduct);
  196. if (len > 512) {
  197. DBGPrintf(" Descriptor length %d only showing first 512\n ");
  198. len = 512;
  199. }
  200. for (uint16_t i=0; i < len; i++) {
  201. DBGPrintf("%x ", descriptors[i]);
  202. if ((i & 0x3f) == 0x3f) DBGPrintf("\n ");
  203. }
  204. DBGPrintf("\n ");
  205. // Lets try to process the first Interface and get the end points...
  206. // Some common stuff for both XBoxs
  207. uint32_t count_end_points = descriptors[4];
  208. if (count_end_points < 2) return false;
  209. uint32_t rxep = 0;
  210. uint32_t rx2ep = 0;
  211. uint32_t txep = 0;
  212. uint8_t rx_interval = 0;
  213. uint8_t rx2_interval = 0;
  214. uint8_t tx_interval = 0;
  215. rx_size_ = 0;
  216. rx2_size_ = 0;
  217. tx_size_ = 0;
  218. uint32_t descriptor_index = 9;
  219. while (count_end_points-- /*&& ((rxep == 0) || txep == 0) */) {
  220. if (descriptors[descriptor_index] != 7) return false; // length 7
  221. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  222. if ((descriptors[descriptor_index+4] <= 64)
  223. && (descriptors[descriptor_index+5] == 0)) {
  224. // have a bulk EP size
  225. if (descriptors[descriptor_index+2] & 0x80 ) {
  226. if (descriptors[descriptor_index+3] == 3) { // Interrupt
  227. rxep = descriptors[descriptor_index+2];
  228. rx_size_ = descriptors[descriptor_index+4];
  229. rx_interval = descriptors[descriptor_index+6];
  230. } else if (descriptors[descriptor_index+3] == 2) { // bulk
  231. rx2ep = descriptors[descriptor_index+2];
  232. rx2_size_ = descriptors[descriptor_index+4];
  233. rx2_interval = descriptors[descriptor_index+6];
  234. }
  235. } else {
  236. txep = descriptors[descriptor_index+2];
  237. tx_size_ = descriptors[descriptor_index+4];
  238. tx_interval = descriptors[descriptor_index+6];
  239. }
  240. }
  241. descriptor_index += 7; // setup to look at next one...
  242. }
  243. if ((rxep == 0) || (txep == 0)) {
  244. USBHDBGSerial.printf("Bluetooth end points not found: %d %d\n", rxep, txep);
  245. return false; // did not find two end points.
  246. }
  247. DBGPrintf(" rxep=%d(%d) txep=%d(%d) rx2ep=%d(%d)\n", rxep&15, rx_size_, txep, tx_size_,
  248. rx2ep&15, rx2_size_);
  249. print("BluetoothController, rxep=", rxep & 15);
  250. print("(", rx_size_);
  251. print("), txep=", txep);
  252. print("(", tx_size_);
  253. println(")");
  254. rxpipe_ = new_Pipe(dev, 3, rxep & 15, 1, rx_size_, rx_interval);
  255. if (!rxpipe_) return false;
  256. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  257. if (!txpipe_) {
  258. //free_Pipe(rxpipe_);
  259. return false;
  260. }
  261. rx2pipe_ = new_Pipe(dev, 2, rx2ep & 15, 1, rx2_size_, rx2_interval);
  262. if (!rx2pipe_) {
  263. // Free other pipes...
  264. return false;
  265. }
  266. rxpipe_->callback_function = rx_callback;
  267. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  268. rx2pipe_->callback_function = rx2_callback;
  269. queue_Data_Transfer(rx2pipe_, rx2buf_, rx2_size_, this);
  270. txpipe_->callback_function = tx_callback;
  271. // Send out the reset
  272. device = dev; // yes this is normally done on return from this but should not hurt if we do it here.
  273. sendResetHCI();
  274. pending_control_ = PC_RESET;
  275. pending_control_tx_ = 0; //
  276. return true;
  277. }
  278. void BluetoothController::disconnect()
  279. {
  280. USBHDBGSerial.printf("Bluetooth Disconnect");
  281. if (device_driver_) {
  282. device_driver_->release_bluetooth();
  283. device_driver_->remote_name_[0] = 0;
  284. device_driver_ = nullptr;
  285. }
  286. connection_complete_ = false;
  287. }
  288. void BluetoothController::timer_event(USBDriverTimer *whichTimer)
  289. {
  290. }
  291. void BluetoothController::control(const Transfer_t *transfer)
  292. {
  293. println(" control callback (bluetooth) ", pending_control_, HEX);
  294. #ifdef DEBUG_BT_VERBOSE
  295. DBGPrintf(" Control callback (bluetooth): %d : ", pending_control_);
  296. uint8_t *buffer = (uint8_t*)transfer->buffer;
  297. for (uint8_t i=0; i < transfer->length; i++) DBGPrintf("%x ", buffer[i]);
  298. DBGPrintf("\n");
  299. #endif
  300. }
  301. /************************************************************/
  302. // Interrupt-based Data Movement
  303. /************************************************************/
  304. void BluetoothController::rx_callback(const Transfer_t *transfer)
  305. {
  306. if (!transfer->driver) return;
  307. ((BluetoothController *)(transfer->driver))->rx_data(transfer);
  308. }
  309. void BluetoothController::rx2_callback(const Transfer_t *transfer)
  310. {
  311. if (!transfer->driver) return;
  312. ((BluetoothController *)(transfer->driver))->rx2_data(transfer);
  313. }
  314. void BluetoothController::tx_callback(const Transfer_t *transfer)
  315. {
  316. if (!transfer->driver) return;
  317. ((BluetoothController *)(transfer->driver))->tx_data(transfer);
  318. }
  319. void BluetoothController::rx_data(const Transfer_t *transfer)
  320. {
  321. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  322. print_hexbytes((uint8_t*)transfer->buffer, len);
  323. DBGPrintf("BT rx_data(%d): ", len);
  324. uint8_t *buffer = (uint8_t*)transfer->buffer;
  325. for (uint8_t i=0; i < len; i++) DBGPrintf("%x ", buffer[i]);
  326. DBGPrintf("\n");
  327. // Note the logical packets returned from the device may be larger
  328. // than can fit in one of our packets, so we will detect this and
  329. // the next read will be continue in or rx_buf_ in the next logical
  330. // location. We will only go into process the next logical state
  331. // when we have the full response read in...
  332. if (rx_packet_data_remaining == 0) { // Previous command was fully handled
  333. rx_packet_data_remaining = rxbuf_[1] + 2; // length of data plus the two bytes at start...
  334. }
  335. // Now see if the data
  336. rx_packet_data_remaining -= len; // remove the length of this packet from length
  337. if (rx_packet_data_remaining == 0) { // read started at beginning of packet so get the total length of packet
  338. switch(rxbuf_[0]) { // Switch on event type
  339. case EV_COMMAND_COMPLETE: //0x0e
  340. handle_hci_command_complete();// Check if command succeeded
  341. break;
  342. case EV_COMMAND_STATUS: //0x0f
  343. handle_hci_command_status();
  344. break;
  345. case EV_INQUIRY_COMPLETE: // 0x01
  346. handle_hci_inquiry_complete();
  347. break;
  348. case EV_INQUIRY_RESULT: // 0x02
  349. handle_hci_inquiry_result();
  350. break;
  351. case EV_CONNECT_COMPLETE: // 0x03
  352. handle_hci_connection_complete();
  353. break;
  354. case EV_INCOMING_CONNECT: // 0x04
  355. handle_hci_incoming_connect();
  356. break;
  357. case EV_DISCONNECT_COMPLETE: // 0x05
  358. handle_hci_disconnect_complete();
  359. break;
  360. case EV_AUTHENTICATION_COMPLETE:// 0x06
  361. handle_hci_authentication_complete();
  362. break;
  363. case EV_REMOTE_NAME_COMPLETE: // 0x07
  364. handle_hci_remote_name_complete();
  365. break;
  366. case EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE:
  367. handle_hci_remote_version_information_complete();
  368. break;
  369. case EV_PIN_CODE_REQUEST: // 0x16
  370. handle_hci_pin_code_request();
  371. break;
  372. case EV_LINK_KEY_REQUEST: // 0x17
  373. handle_hci_link_key_request();
  374. break;
  375. case EV_LINK_KEY_NOTIFICATION: // 0x18
  376. handle_hci_link_key_notification();
  377. break;
  378. case HCI_Extended_Inquiry_Result:
  379. handle_hci_extended_inquiry_result();
  380. break;
  381. default:
  382. break;
  383. }
  384. // Start read at start of buffer.
  385. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  386. } else {
  387. // Continue the read - Todo - maybe verify len == rx_size_
  388. queue_Data_Transfer(rxpipe_, buffer + rx_size_, rx_size_, this);
  389. return; // Don't process the message yet as we still have data to receive.
  390. }
  391. }
  392. //===================================================================
  393. // Called when an HCI command completes.
  394. void BluetoothController::handle_hci_command_complete()
  395. {
  396. uint16_t hci_command = rxbuf_[3] + (rxbuf_[4] << 8);
  397. uint8_t buffer_index;
  398. if(!rxbuf_[5]) {
  399. VDBGPrintf(" Command Completed! \n");
  400. } else {
  401. VDBGPrintf(" Command(%x) Completed - Error: %d! \n", hci_command, rxbuf_[5]);
  402. // BUGBUG:: probably need to queue something?
  403. }
  404. switch (hci_command) {
  405. case HCI_OP_REMOTE_NAME_REQ:
  406. break;
  407. case HCI_RESET: //0x0c03
  408. if (!rxbuf_[5]) pending_control_ = PC_WRITE_CLASS_DEVICE;
  409. // If it fails, will retry. maybe should have repeat max...
  410. break;
  411. case HCI_Set_Event_Filter_Clear: //0x0c05
  412. break;
  413. case HCI_Read_Local_Name: //0x0c14
  414. // received name back...
  415. {
  416. //BUGBUG:: probably want to grab string object and copy to
  417. USBHDBGSerial.printf(" Local name: %s\n", &rxbuf_[6]);
  418. /*
  419. uint8_t len = rxbuf_[1]+2; // Length field +2 for total bytes read
  420. for (uint8_t i=6; i < len; i++) {
  421. if (rxbuf_[i] == 0) {
  422. break;
  423. }
  424. USBHDBGSerial.printf("%c", rxbuf_[i]);
  425. }
  426. USBHDBGSerial.printf("\n"); */
  427. }
  428. break;
  429. case Write_Connection_Accept_Timeout: //0x0c16
  430. break;
  431. case HCI_READ_CLASS_OF_DEVICE: // 0x0c23
  432. break;
  433. case HCI_Read_Voice_Setting: //0x0c25
  434. break;
  435. case HCI_Read_Number_Of_Supported_IAC: //0x0c38
  436. break;
  437. case HCI_Read_Current_IAC_LAP: //0x0c39
  438. break;
  439. case HCI_WRITE_INQUIRY_MODE: //0x0c45
  440. break;
  441. case HCI_Read_Inquiry_Response_Transmit_Power_Level: //0x0c58
  442. break;
  443. case HCI_Read_Local_Supported_Features: //0x1003
  444. // Remember the features supported by local...
  445. for (buffer_index = 0; buffer_index < 8; buffer_index++) {
  446. features[buffer_index] = rxbuf_[buffer_index+6];
  447. }
  448. break;
  449. case HCI_Read_Buffer_Size: // 0x1005
  450. break;
  451. case HCI_Read_BD_ADDR: //0x1009
  452. {
  453. DBGPrintf(" BD Addr");
  454. for(uint8_t i = 0; i < 6; i++) {
  455. my_bdaddr_[i] = rxbuf_[6 + i];
  456. DBGPrintf(":%x", my_bdaddr_[i]);
  457. }
  458. DBGPrintf("\n");
  459. }
  460. break;
  461. case HCI_Read_Local_Version_Information: //0x1001
  462. hciVersion = rxbuf_[6]; // Should do error checking above...
  463. DBGPrintf(" Local Version: %x\n", hciVersion);
  464. pending_control_ = (do_pair_device_)? PC_SEND_WRITE_INQUIRE_MODE : PC_WRITE_SCAN_PAGE;
  465. break;
  466. case HCI_Read_Local_Supported_Commands: //0x1002
  467. break;
  468. case HCI_LE_Read_Buffer_Size: //0x2002
  469. break;
  470. case HCI_LE_Read_Local_supported_Features: //0x2003
  471. break;
  472. case HCI_LE_Supported_States: //0x201c
  473. break;
  474. case HCI_Read_Local_Extended_Features: //0x1004
  475. break;
  476. case HCI_Set_Event_Mask: //0x0c01
  477. break;
  478. case HCI_Read_Stored_Link_Key: //0x0c0d
  479. break;
  480. case HCI_Write_Default_Link_Policy_Settings: //0x080f
  481. break;
  482. case HCI_Read_Page_Scan_Activity: //0x0c1b
  483. break;
  484. case HCI_Read_Page_Scan_Type: //0x0c46
  485. break;
  486. case HCI_LE_SET_EVENT_MASK: //0x2001
  487. break;
  488. case HCI_LE_READ_ADV_TX_POWER: //0x2007
  489. break;
  490. case HCI_LE_READ_WHITE_LIST_SIZE: //0x200f
  491. break;
  492. case HCI_LE_CLEAR_WHITE_LIST: //0x2010
  493. break;
  494. case HCI_DELETE_STORED_LINK_KEY: //0x0c12
  495. break;
  496. case HCI_WRITE_LOCAL_NAME: //0x0c13
  497. break;
  498. case HCI_WRITE_SCAN_ENABLE: //0x0c1a
  499. DBGPrintf("Write_Scan_enable Completed\n");
  500. // See if we have driver and a remote
  501. if (device_driver_ && connection_complete_) { // We have a driver call their
  502. device_driver_->connectionComplete();
  503. connection_complete_ = false; // only call once
  504. }
  505. break;
  506. case HCI_WRITE_SSP_MODE: //0x0c56
  507. break;
  508. case HCI_WRITE_EIR: //0x0c52
  509. break;
  510. case HCI_WRITE_LE_HOST_SUPPORTED: //0x0c6d
  511. break;
  512. case HCI_LE_SET_SCAN_RSP_DATA: //0x2009
  513. break;
  514. }
  515. // And queue up the next command
  516. queue_next_hci_command();
  517. }
  518. void BluetoothController::queue_next_hci_command()
  519. {
  520. // Ok We completed a command now see if we need to queue another command
  521. // Still probably need to reorganize...
  522. switch (pending_control_) {
  523. // Initial setup states.
  524. case PC_RESET:
  525. sendResetHCI();
  526. break;
  527. case PC_WRITE_CLASS_DEVICE:
  528. sendHDCWriteClassOfDev();
  529. pending_control_++;
  530. break;
  531. case PC_READ_BDADDR:
  532. sendHCIReadBDAddr();
  533. pending_control_++;
  534. break;
  535. case PC_READ_LOCAL_VERSION:
  536. sendHCIReadLocalVersionInfo();
  537. //pending_control_++;
  538. break;
  539. // These are used when we are pairing.
  540. case PC_SEND_WRITE_INQUIRE_MODE:
  541. sendHCIHCIWriteInquiryMode(2); // lets set into extended inquire mode
  542. pending_control_++;
  543. break;
  544. case PC_SEND_SET_EVENT_MASK:
  545. sendHCISetEventMask(); // Set the event mask to include extend inquire event
  546. pending_control_++;
  547. break;
  548. case PC_SEND_INQUIRE:
  549. sendHCI_INQUIRY();
  550. pending_control_++;
  551. break;
  552. case PC_INQUIRE_CANCEL:
  553. // lets try to create a connection...
  554. sendHCICreateConnection();
  555. pending_control_++;
  556. break;
  557. case PC_AUTHENTICATION_REQUESTED:
  558. break;
  559. case PC_LINK_KEY_NEGATIVE:
  560. break;
  561. case PC_PIN_CODE_REPLY:
  562. break;
  563. // None Pair mode
  564. case PC_WRITE_SCAN_PAGE:
  565. sendHCIWriteScanEnable(2);
  566. pending_control_ = 0; //
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. void BluetoothController::handle_hci_command_status()
  573. {
  574. // <event type><param count><status><num packets allowed to be sent><CMD><CMD>
  575. #ifdef DEBUG_BT
  576. uint16_t hci_command = rxbuf_[4] + (rxbuf_[5] << 8);
  577. if (rxbuf_[2]) {
  578. DBGPrintf(" Command %x Status %x - ", hci_command, rxbuf_[2]);
  579. switch (rxbuf_[2]) {
  580. case 0x01: DBGPrintf("Unknown HCI Command\n"); break;
  581. case 0x02: DBGPrintf("Unknown Connection Identifier\n"); break;
  582. case 0x03: DBGPrintf("Hardware Failure\n"); break;
  583. case 0x04: DBGPrintf("Page Timeout\n"); break;
  584. case 0x05: DBGPrintf("Authentication Failure\n"); break;
  585. case 0x06: DBGPrintf("PIN or Key Missing\n"); break;
  586. case 0x07: DBGPrintf("Memory Capacity Exceeded\n"); break;
  587. case 0x08: DBGPrintf("Connection Timeout\n"); break;
  588. case 0x09: DBGPrintf("Connection Limit Exceeded\n"); break;
  589. case 0x0A: DBGPrintf("Synchronous Connection Limit To A Device Exceeded\n"); break;
  590. case 0x0B: DBGPrintf("Connection Already Exists\n"); break;
  591. case 0x0C: DBGPrintf("Command Disallowed\n"); break;
  592. case 0x0D: DBGPrintf("Connection Rejected due to Limited Resources\n"); break;
  593. case 0x0E: DBGPrintf("Connection Rejected Due To Security Reasons\n"); break;
  594. case 0x0F: DBGPrintf("Connection Rejected due to Unacceptable BD_ADDR\n"); break;
  595. default: DBGPrintf("???\n"); break;
  596. }
  597. } else {
  598. VDBGPrintf(" Command %x Status %x\n", hci_command, rxbuf_[2]);
  599. }
  600. #endif
  601. }
  602. void BluetoothController::handle_hci_inquiry_result()
  603. {
  604. // 2 f 1 79 22 23 a c5 cc 1 2 0 40 25 0 3b 2
  605. // Wondered if multiple items if all of the BDADDR are first then next field...
  606. // looks like it is that way...
  607. // Section 7.7.2
  608. DBGPrintf(" Inquiry Result - Count: %d\n", rxbuf_[2]);
  609. for (uint8_t i=0; i < rxbuf_[2]; i++) {
  610. uint8_t index_bd = 3 + (i*6);
  611. uint8_t index_ps = 3 + (6*rxbuf_[2]) + i;
  612. uint8_t index_class = 3 + (9*rxbuf_[2]) + i;
  613. uint8_t index_clock_offset = 3 + (12*rxbuf_[2]) + i;
  614. uint32_t bluetooth_class = rxbuf_[index_class] + ((uint32_t)rxbuf_[index_class+1] << 8) + ((uint32_t)rxbuf_[index_class+2] << 16);
  615. DBGPrintf(" BD:%x:%x:%x:%x:%x:%x, PS:%d, class: %x\n",
  616. rxbuf_[index_bd],rxbuf_[index_bd+1],rxbuf_[index_bd+2],rxbuf_[index_bd+3],rxbuf_[index_bd+4],rxbuf_[index_bd+5],
  617. rxbuf_[index_ps], bluetooth_class);
  618. // See if we know the class
  619. if (((bluetooth_class & 0xff00) == 0x2500) || ((bluetooth_class & 0xff00) == 0x500)) {
  620. DBGPrintf(" Peripheral device\n");
  621. if (bluetooth_class & 0x80) DBGPrintf(" Mouse\n");
  622. if (bluetooth_class & 0x40) DBGPrintf(" Keyboard\n");
  623. switch(bluetooth_class & 0x3c) {
  624. case 4: DBGPrintf(" Joystick\n"); break;
  625. case 8: DBGPrintf(" Gamepad\n"); break;
  626. case 0xc: DBGPrintf(" Remote Control\n"); break;
  627. }
  628. // BUGBUG, lets hard code to go to new state...
  629. for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = rxbuf_[index_bd+i];
  630. device_class_ = bluetooth_class;
  631. device_driver_ = find_driver(device_class_);
  632. device_ps_repetion_mode_ = rxbuf_[index_ps]; // mode
  633. device_clock_offset_[0] = rxbuf_[index_clock_offset];
  634. device_clock_offset_[1] = rxbuf_[index_clock_offset+1];
  635. // Now we need to bail from inquiry and setup to try to connect...
  636. sendHCIInquiryCancel();
  637. pending_control_ = PC_INQUIRE_CANCEL;
  638. break;
  639. }
  640. }
  641. }
  642. void BluetoothController::handle_hci_extended_inquiry_result()
  643. {
  644. DBGPrintf(" Extended Inquiry Result - Count: %d\n", rxbuf_[2]);
  645. // Should always be only one result here.
  646. uint8_t index_bd = 3;
  647. uint8_t index_ps = 9;
  648. uint8_t index_class = 11;
  649. uint8_t index_clock_offset = 14;
  650. //uint8_t index_rssi = 16;
  651. uint8_t index_eir_data = 17;
  652. uint8_t index_local_name = 0;
  653. uint8_t size_local_name = 0;
  654. uint32_t bluetooth_class = rxbuf_[index_class] + ((uint32_t)rxbuf_[index_class+1] << 8) + ((uint32_t)rxbuf_[index_class+2] << 16);
  655. DBGPrintf(" BD:%x:%x:%x:%x:%x:%x, PS:%d, class: %x\n",
  656. rxbuf_[index_bd],rxbuf_[index_bd+1],rxbuf_[index_bd+2],rxbuf_[index_bd+3],rxbuf_[index_bd+4],rxbuf_[index_bd+5],
  657. rxbuf_[index_ps], bluetooth_class);
  658. // Lets see if we can find a name
  659. while (index_eir_data < 256) {
  660. if (rxbuf_[index_eir_data] == 0) break; // no more data
  661. switch (rxbuf_[index_eir_data+1]) {
  662. case 0x08: // Shortened local name
  663. case 0x09: // complete local name
  664. index_local_name = index_eir_data+2;
  665. size_local_name = rxbuf_[index_eir_data]-1;
  666. break;
  667. }
  668. index_eir_data += rxbuf_[index_eir_data] + 1; // point to the next item
  669. }
  670. if (index_local_name && size_local_name) {
  671. // Hack lets null teminate the string
  672. rxbuf_[index_local_name+size_local_name] = 0;
  673. DBGPrintf(" Local Name: %s\n", &rxbuf_[index_local_name]);
  674. }
  675. // See if we know the class
  676. if (((bluetooth_class & 0xff00) == 0x2500) || ((bluetooth_class & 0xff00) == 0x500)) {
  677. DBGPrintf(" Peripheral device\n");
  678. if (bluetooth_class & 0x80) DBGPrintf(" Mouse\n");
  679. if (bluetooth_class & 0x40) DBGPrintf(" Keyboard\n");
  680. switch(bluetooth_class & 0x3c) {
  681. case 4: DBGPrintf(" Joystick\n"); break;
  682. case 8: DBGPrintf(" Gamepad\n"); break;
  683. case 0xc: DBGPrintf(" Remote Control\n"); break;
  684. }
  685. // BUGBUG, lets hard code to go to new state...
  686. for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = rxbuf_[index_bd+i];
  687. device_class_ = bluetooth_class;
  688. device_driver_ = find_driver(device_class_, index_local_name? &rxbuf_[index_local_name] : nullptr);
  689. device_ps_repetion_mode_ = rxbuf_[index_ps]; // mode
  690. device_clock_offset_[0] = rxbuf_[index_clock_offset];
  691. device_clock_offset_[1] = rxbuf_[index_clock_offset+1];
  692. // and if we found a driver, save away the name
  693. if (device_driver_ && index_local_name && size_local_name) {
  694. uint8_t buffer_index;
  695. for (buffer_index = 0; size_local_name && (buffer_index < BTHIDInput::REMOTE_NAME_SIZE-1); buffer_index++) {
  696. device_driver_->remote_name_[buffer_index] = rxbuf_[index_local_name+buffer_index];
  697. size_local_name--;
  698. }
  699. device_driver_->remote_name_[buffer_index] = 0; // make sure null terminated
  700. }
  701. // Now we need to bail from inquiry and setup to try to connect...
  702. sendHCIInquiryCancel();
  703. pending_control_ = PC_INQUIRE_CANCEL;
  704. }
  705. }
  706. void BluetoothController::handle_hci_inquiry_complete() {
  707. VDBGPrintf(" Inquiry Complete - status: %d\n", rxbuf_[2]);
  708. }
  709. void BluetoothController::handle_hci_connection_complete() {
  710. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  711. // ST CH CH BD BD BD BD BD BD LT EN
  712. // 03 0b 04 00 00 40 25 00 58 4b 00 01 00
  713. device_connection_handle_ = rxbuf_[3]+ (uint16_t)(rxbuf_[4]<<8);
  714. DBGPrintf(" Connection Complete - ST:%x LH:%x\n", rxbuf_[2], device_connection_handle_);
  715. if (do_pair_device_) {
  716. sendHCIAuthenticationRequested();
  717. pending_control_ = PC_AUTHENTICATION_REQUESTED;
  718. } else if (device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_NEED_CONNECT)) {
  719. DBGPrintf(" Needs connect to device(PS4?)\n");
  720. // The PS4 requires a connection request to it.
  721. delay(1);
  722. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
  723. #if 0
  724. delay(1);
  725. uint8_t packet[2];
  726. memset(packet, 0, sizeof(packet));
  727. packet[0] = 0x43;
  728. packet[1] = 0x02; // Report ID
  729. USBHDBGSerial.printf("SixAxis Command Issued!\r\n");
  730. sendL2CapCommand(packet, sizeof(packet), 0x40);
  731. #endif
  732. }
  733. }
  734. void BluetoothController::handle_hci_incoming_connect() {
  735. // BD BD BD BD BD BD CL CL CL LT
  736. // 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x40 0x05 0x00 0x01
  737. uint32_t class_of_device = rxbuf_[8] + (uint16_t)(rxbuf_[9]<<8) + (uint32_t)(rxbuf_[10]<<16);
  738. DBGPrintf(" Event: Incoming Connect - %x:%x:%x:%x:%x:%x CL:%x LT:%x\n",
  739. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], class_of_device, rxbuf_[11]);
  740. if (((class_of_device & 0xff00) == 0x2500) || ((class_of_device & 0xff00) == 0x500)) {
  741. DBGPrintf(" Peripheral device\n");
  742. if (class_of_device & 0x80) DBGPrintf(" Mouse\n");
  743. if (class_of_device & 0x40) DBGPrintf(" Keyboard\n");
  744. switch(class_of_device & 0x3c) {
  745. case 4: DBGPrintf(" Joystick\n"); break;
  746. case 8: DBGPrintf(" Gamepad\n"); break;
  747. case 0xc: DBGPrintf(" Remote Control\n"); break;
  748. }
  749. device_driver_ = find_driver(class_of_device);
  750. // We need to save away the BDADDR and class link type?
  751. for(uint8_t i=0; i<6; i++) device_bdaddr_[i] = rxbuf_[i+2];
  752. device_class_ = class_of_device;
  753. sendHCIRemoteNameRequest();
  754. }
  755. // sendHCIAuthenticationRequested();
  756. // pending_control_ = PC_AUTHENTICATION_REQUESTED;
  757. }
  758. void BluetoothController::handle_hci_pin_code_request() {
  759. // 0x16 0x06 0x79 0x22 0x23 0x0A 0xC5 0xCC
  760. DBGPrintf(" Event: Pin Code Request %x:%x:%x:%x:%x:%x\n",
  761. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  762. sendHCIPinCodeReply();
  763. pending_control_ = PC_PIN_CODE_REPLY;
  764. }
  765. void BluetoothController::handle_hci_link_key_request() {
  766. // 17 6 79 22 23 a c5 cc
  767. DBGPrintf(" Event: Link Key Request %x:%x:%x:%x:%x:%x\n",
  768. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  769. // Now here is where we need to decide to say we have key or tell them to
  770. // cancel key... right now hard code to cancel...
  771. sendHCILinkKeyNegativeReply();
  772. pending_control_ = PC_LINK_KEY_NEGATIVE;
  773. }
  774. void BluetoothController::handle_hci_link_key_notification() {
  775. // 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
  776. // 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
  777. DBGPrintf(" Event: Link Key Notificaton %x:%x:%x:%x:%x:%x Type:%x\n key:",
  778. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], rxbuf_[24]);
  779. for (uint8_t i = 8; i < 24; i++) DBGPrintf("%02x ", rxbuf_[i]);
  780. DBGPrintf("\n");
  781. // Now here is where we need to decide to say we have key or tell them to
  782. // cancel key... right now hard code to cancel...
  783. }
  784. void BluetoothController::handle_hci_disconnect_complete()
  785. {
  786. //5 4 0 48 0 13
  787. DBGPrintf(" Event: HCI Disconnect complete(%d): handle: %x, reason:%x\n", rxbuf_[2],
  788. rxbuf_[3]+(rxbuf_[4]<<8), rxbuf_[5]);
  789. if (device_driver_) {
  790. device_driver_->release_bluetooth();
  791. device_driver_->remote_name_[0] = 0;
  792. device_driver_ = nullptr;
  793. // Restore to normal...
  794. control_dcid_ = 0x70;
  795. interrupt_dcid_ = 0x71;
  796. }
  797. // Probably should clear out connection data.
  798. device_connection_handle_ = 0;
  799. device_class_ = 0;
  800. memset(device_bdaddr_, 0, sizeof(device_bdaddr_));
  801. }
  802. void BluetoothController::handle_hci_authentication_complete()
  803. {
  804. // 6 3 13 48 0
  805. DBGPrintf(" Event: HCI Authentication complete(%d): handle: %x\n", rxbuf_[2],
  806. rxbuf_[3]+(rxbuf_[4]<<8));
  807. // Start up lcap connection...
  808. connection_rxid_ = 0;
  809. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
  810. }
  811. void BluetoothController::handle_hci_remote_name_complete() {
  812. // STAT bd bd bd bd bd bd
  813. // 0x07 0xFF 0x00 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x42 0x6C 0x75 0x65 0x74 0x6F 0x6F ...
  814. DBGPrintf(" Event: handle_hci_remote_name_complete(%d)\n", rxbuf_[2]);
  815. if (rxbuf_[2] == 0) {
  816. DBGPrintf(" Remote Name: ");
  817. for (uint8_t *psz = &rxbuf_[9]; *psz; psz++) DBGPrintf("%c", *psz);
  818. DBGPrintf("\n");
  819. }
  820. if (device_driver_) {
  821. if (!device_driver_->remoteNameComplete(&rxbuf_[9])) {
  822. device_driver_->release_bluetooth();
  823. device_driver_ = nullptr;
  824. }
  825. }
  826. if (!device_driver_) {
  827. device_driver_ = find_driver(device_class_, &rxbuf_[9]);
  828. // not sure I should call remote name again, but they already process...
  829. if (device_driver_) {
  830. device_driver_->remoteNameComplete(&rxbuf_[9]);
  831. }
  832. }
  833. if (device_driver_) {
  834. // lets save away the string.
  835. uint8_t buffer_index;
  836. for (buffer_index = 0; buffer_index < BTHIDInput::REMOTE_NAME_SIZE-1; buffer_index++) {
  837. device_driver_->remote_name_[buffer_index] = rxbuf_[9+buffer_index];
  838. if (!device_driver_->remote_name_[buffer_index]) break;
  839. }
  840. device_driver_->remote_name_[buffer_index] = 0; // make sure null terminated
  841. if (device_driver_->special_process_required & BTHIDInput::SP_PS3_IDS) {
  842. // Real hack see if PS3...
  843. control_dcid_ = 0x40;
  844. interrupt_dcid_ = 0x41;
  845. }
  846. }
  847. // If we are in the connection complete mode, then this is a pairing state and needed to call
  848. // get remote name later.
  849. if (connection_complete_) {
  850. if (device_driver_) { // We have a driver call their
  851. device_driver_->connectionComplete();
  852. connection_complete_ = false; // only call once
  853. }
  854. } else {
  855. sendHCIAcceptConnectionRequest();
  856. }
  857. }
  858. void BluetoothController::handle_hci_remote_version_information_complete() {
  859. // STAT bd bd bd bd bd bd
  860. //c 8 0 48 0 5 45 0 0 0
  861. remote_ver_ = rxbuf_[6];
  862. remote_man_ = rxbuf_[7]+((uint16_t)rxbuf_[8]<< 8);
  863. remote_subv_ = rxbuf_[9];
  864. DBGPrintf(" Event: handle_hci_remote_version_information_complete(%d): ", rxbuf_[2]);
  865. DBGPrintf(" Handle: %x, Ver:%x, Man: %x, SV: %x\n",
  866. rxbuf_[3]+((uint16_t)rxbuf_[4]<< 8), remote_ver_, remote_man_, remote_subv_);
  867. // Lets now try to accept the connection.
  868. sendHCIAcceptConnectionRequest();
  869. }
  870. void BluetoothController::rx2_data(const Transfer_t *transfer)
  871. {
  872. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  873. DBGPrintf("\n=====================\nBT rx2_data(%d): ", len);
  874. uint8_t *buffer = (uint8_t*)transfer->buffer;
  875. for (uint8_t i=0; i < len; i++) DBGPrintf("%x ", buffer[i]);
  876. DBGPrintf("\n");
  877. // call backs. See if this is an L2CAP reply. example
  878. // HCI | l2cap
  879. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  880. // BUGBUG need to do more verification, like the handle
  881. uint16_t hci_length = buffer[2] + ((uint16_t)buffer[3]<<8);
  882. uint16_t l2cap_length = buffer[4] + ((uint16_t)buffer[5]<<8);
  883. // uint16_t rsp_packet_length = buffer[10] + ((uint16_t)buffer[11]<<8);
  884. if ((hci_length == (l2cap_length + 4)) /*&& (hci_length == (rsp_packet_length+8))*/) {
  885. // All the lengths appear to be correct... need to do more...
  886. switch (buffer[8]) {
  887. case L2CAP_CMD_CONNECTION_REQUEST:
  888. process_l2cap_connection_request(&buffer[8]);
  889. break;
  890. case L2CAP_CMD_CONNECTION_RESPONSE:
  891. process_l2cap_connection_response(&buffer[8]);
  892. break;
  893. case L2CAP_CMD_CONFIG_REQUEST:
  894. process_l2cap_config_request(&buffer[8]);
  895. break;
  896. case L2CAP_CMD_CONFIG_RESPONSE:
  897. process_l2cap_config_response(&buffer[8]);
  898. break;
  899. case HID_THDR_DATA_INPUT:
  900. handleHIDTHDRData(buffer); // Pass the whole buffer...
  901. break;
  902. case L2CAP_CMD_COMMAND_REJECT:
  903. process_l2cap_command_reject(&buffer[8]);
  904. break;
  905. case L2CAP_CMD_DISCONNECT_REQUEST:
  906. process_l2cap_disconnect_request(&buffer[8]);
  907. break;
  908. }
  909. }
  910. // Queue up for next read...
  911. queue_Data_Transfer(rx2pipe_, rx2buf_, rx2_size_, this);
  912. }
  913. void BluetoothController::sendHCICommand(uint16_t hciCommand, uint16_t cParams, const uint8_t* data)
  914. {
  915. txbuf_[0] = hciCommand & 0xff;
  916. txbuf_[1] = (hciCommand >> 8) & 0xff;
  917. txbuf_[2] = cParams;
  918. if (cParams) {
  919. memcpy(&txbuf_[3], data, cParams); // copy in the commands parameters.
  920. }
  921. uint8_t nbytes = cParams+3;
  922. for (uint8_t i=0; i< nbytes; i++) DBGPrintf("%02x ", txbuf_[i]);
  923. DBGPrintf(")\n");
  924. mk_setup(setup, 0x20, 0x0, 0, 0, nbytes);
  925. queue_Control_Transfer(device, &setup, txbuf_, this);
  926. }
  927. //---------------------------------------------
  928. void BluetoothController::sendHCIHCIWriteInquiryMode(uint8_t inquiry_mode) {
  929. // Setup Inquiry mode
  930. DBGPrintf("HCI_WRITE_INQUIRY_MODE called (");
  931. sendHCICommand(HCI_WRITE_INQUIRY_MODE, 1, &inquiry_mode);
  932. }
  933. void BluetoothController::sendHCISetEventMask() {
  934. // Setup Inquiry mode
  935. DBGPrintf("HCI_Set_Event_Mask called (");
  936. static const uint8_t hci_event_mask_data[8] = {
  937. // Default: 0x0000 1FFF FFFF FFFF
  938. 0xff,0xff, 0xff,0xff, 0xff,0x5f, 0x00,0x00}; // default plus extended inquiry mode
  939. sendHCICommand(HCI_Set_Event_Mask, sizeof(hci_event_mask_data), hci_event_mask_data);
  940. }
  941. //---------------------------------------------
  942. void BluetoothController::sendHCI_INQUIRY() {
  943. // Start unlimited inqury, set timeout to max and
  944. DBGPrintf("HCI_INQUIRY called (");
  945. static const uint8_t hci_inquiry_data[ ] = {
  946. 0x33, 0x8B, 0x9E, // Bluetooth assigned number LAP 0x9E8B33 General/unlimited inquiry Access mode
  947. 0x30, 0xa}; // Max inquiry time little over minute and up to 10 responses
  948. sendHCICommand(HCI_INQUIRY, sizeof(hci_inquiry_data), hci_inquiry_data);
  949. }
  950. //---------------------------------------------
  951. void BluetoothController::sendHCIInquiryCancel() {
  952. DBGPrintf("HCI_INQUIRY_CANCEL called (");
  953. sendHCICommand(HCI_INQUIRY_CANCEL, 0, nullptr);
  954. }
  955. //---------------------------------------------
  956. void BluetoothController::sendHCICreateConnection() {
  957. DBGPrintf("HCI_CREATE_CONNECTION called (");
  958. uint8_t connection_data[13];
  959. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  960. // BD BD BD BD BD BD PT PT PRS 0 CS CS ARS
  961. //0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  962. //0x05 0x04 0x0D 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  963. // 05 04 0d 40 25 00 c4 01 00 18 cc 01 00 00 00 00
  964. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  965. connection_data[6] = 0x18; //DM1/DH1
  966. connection_data[7] = 0xcc; //
  967. connection_data[8] = device_ps_repetion_mode_; // from device
  968. connection_data[9] = 0; //
  969. connection_data[10] = 0; // clock offset
  970. connection_data[11] = 0; // clock offset
  971. connection_data[12] = 0; // allow role swith no
  972. sendHCICommand(HCI_CREATE_CONNECTION, sizeof(connection_data), connection_data);
  973. }
  974. //---------------------------------------------
  975. void BluetoothController::sendHCIAcceptConnectionRequest() {
  976. DBGPrintf("HCI_OP_ACCEPT_CONN_REQ called (");
  977. uint8_t connection_data[7];
  978. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  979. // BD BD BD BD BD BD role
  980. //0x79 0x22 0x23 0x0A 0xC5 0xCC 0x00
  981. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  982. connection_data[6] = 0; // Role as master
  983. sendHCICommand(HCI_OP_ACCEPT_CONN_REQ, sizeof(connection_data), connection_data);
  984. }
  985. //---------------------------------------------
  986. void BluetoothController::sendHCIAuthenticationRequested() {
  987. DBGPrintf("HCI_AUTH_REQUESTED called (");
  988. uint8_t connection_data[2];
  989. connection_data[0] = device_connection_handle_ & 0xff;
  990. connection_data[1] = (device_connection_handle_>>8) & 0xff;
  991. sendHCICommand(HCI_AUTH_REQUESTED, sizeof(connection_data), connection_data);
  992. }
  993. //---------------------------------------------
  994. void BluetoothController::sendHCILinkKeyNegativeReply() {
  995. DBGPrintf("HCI_LINK_KEY_NEG_REPLY called (");
  996. uint8_t connection_data[6];
  997. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  998. sendHCICommand(HCI_LINK_KEY_NEG_REPLY, sizeof(connection_data), connection_data);
  999. }
  1000. //---------------------------------------------
  1001. // BUGBUG:: hard code string for this pass.
  1002. void BluetoothController::sendHCIPinCodeReply() {
  1003. // 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
  1004. DBGPrintf("HCI_PIN_CODE_REPLY called (");
  1005. uint8_t connection_data[23];
  1006. uint8_t i;
  1007. for (i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  1008. for (i=0; pair_pincode_[i] !=0; i++) connection_data[7+i] = pair_pincode_[i];
  1009. connection_data[6] = i; // remember the length
  1010. for (uint8_t i=7+connection_data[6]; i<23; i++) connection_data[i] = 0;
  1011. sendHCICommand(HCI_PIN_CODE_REPLY, sizeof(connection_data), connection_data);
  1012. }
  1013. //---------------------------------------------
  1014. void BluetoothController::sendResetHCI() {
  1015. DBGPrintf("HCI_RESET called (");
  1016. sendHCICommand(HCI_RESET, 0, nullptr);
  1017. }
  1018. void BluetoothController::sendHDCWriteClassOfDev() {
  1019. // 0x24 0x0C 0x03 0x04 0x08 0x00
  1020. const static uint8_t device_class_data[] = {BT_CLASS_DEVICE & 0xff, (BT_CLASS_DEVICE >> 8) & 0xff, (BT_CLASS_DEVICE >> 16) & 0xff};
  1021. DBGPrintf("HCI_WRITE_CLASS_OF_DEV called (");
  1022. sendHCICommand(HCI_WRITE_CLASS_OF_DEV, sizeof(device_class_data), device_class_data);
  1023. }
  1024. void BluetoothController::sendHCIReadBDAddr() {
  1025. DBGPrintf("HCI_Read_BD_ADDR called (");
  1026. sendHCICommand(HCI_Read_BD_ADDR, 0, nullptr);
  1027. }
  1028. void BluetoothController::sendHCIReadLocalVersionInfo() {
  1029. DBGPrintf("HCI_Read_Local_Version_Information called (");
  1030. sendHCICommand(HCI_Read_Local_Version_Information, 0, nullptr);
  1031. }
  1032. void BluetoothController::sendHCIWriteScanEnable(uint8_t scan_op) {// 0x0c1a
  1033. // 0x1A 0x0C 0x01 0x02
  1034. DBGPrintf("HCI_WRITE_SCAN_ENABLE called(");
  1035. sendHCICommand(HCI_WRITE_SCAN_ENABLE, 1, &scan_op);
  1036. }
  1037. void BluetoothController::sendHCIRemoteNameRequest() { // 0x0419
  1038. // BD BD BD BD BD BD PS 0 CLK CLK
  1039. //0x19 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x01 0x00 0x00 0x00
  1040. DBGPrintf("HCI_OP_REMOTE_NAME_REQ called (");
  1041. uint8_t connection_data[10];
  1042. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  1043. connection_data[6] = 1; // page scan repeat mode...
  1044. connection_data[7] = 0; // 0
  1045. connection_data[8] = 0; // Clk offset
  1046. connection_data[9] = 0;
  1047. sendHCICommand(HCI_OP_REMOTE_NAME_REQ, sizeof(connection_data), connection_data);
  1048. }
  1049. void BluetoothController::sendHCIRemoteVersionInfoRequest() { // 0x041D
  1050. // BD BD BD BD BD BD PS 0 CLK CLK
  1051. //0x19 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x01 0x00 0x00 0x00
  1052. DBGPrintf("HCI_OP_READ_REMOTE_VERSION_INFORMATION called (");
  1053. uint8_t connection_data[2];
  1054. connection_data[0] = device_connection_handle_ & 0xff;
  1055. connection_data[1] = (device_connection_handle_>>8) & 0xff;
  1056. sendHCICommand(HCI_OP_READ_REMOTE_VERSION_INFORMATION, sizeof(connection_data), connection_data);
  1057. }
  1058. // l2cap support functions.
  1059. void BluetoothController::sendl2cap_ConnectionResponse(uint16_t handle, uint8_t rxid, uint16_t dcid, uint16_t scid, uint8_t result) {
  1060. uint8_t l2capbuf[12];
  1061. l2capbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
  1062. l2capbuf[1] = rxid; // Identifier
  1063. l2capbuf[2] = 0x08; // Length
  1064. l2capbuf[3] = 0x00;
  1065. l2capbuf[4] = dcid & 0xff; // Destination CID
  1066. l2capbuf[5] = dcid >> 8;
  1067. l2capbuf[6] = scid & 0xff; // Source CID
  1068. l2capbuf[7] = scid >> 8;
  1069. l2capbuf[8] = result; // Result: Pending or Success
  1070. l2capbuf[9] = 0x00;
  1071. l2capbuf[10] = 0x00; // No further information
  1072. l2capbuf[11] = 0x00;
  1073. DBGPrintf("L2CAP_CMD_CONNECTION_RESPONSE called(");
  1074. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1075. }
  1076. void BluetoothController::sendl2cap_ConnectionRequest(uint16_t handle, uint8_t rxid, uint16_t scid, uint16_t psm) {
  1077. uint8_t l2capbuf[8];
  1078. l2capbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
  1079. l2capbuf[1] = rxid; // Identifier
  1080. l2capbuf[2] = 0x04; // Length
  1081. l2capbuf[3] = 0x00;
  1082. l2capbuf[4] = (uint8_t)(psm & 0xff); // PSM
  1083. l2capbuf[5] = (uint8_t)(psm >> 8);
  1084. l2capbuf[6] = scid & 0xff; // Source CID
  1085. l2capbuf[7] = (scid >> 8) & 0xff;
  1086. DBGPrintf("ConnectionRequest called(");
  1087. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1088. }
  1089. void BluetoothController::sendl2cap_ConfigRequest(uint16_t handle, uint8_t rxid, uint16_t dcid) {
  1090. uint8_t l2capbuf[12];
  1091. l2capbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
  1092. l2capbuf[1] = rxid; // Identifier
  1093. l2capbuf[2] = 0x08; // Length
  1094. l2capbuf[3] = 0x00;
  1095. l2capbuf[4] = dcid & 0xff; // Destination CID
  1096. l2capbuf[5] = (dcid >> 8) & 0xff;
  1097. l2capbuf[6] = 0x00; // Flags
  1098. l2capbuf[7] = 0x00;
  1099. l2capbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
  1100. l2capbuf[9] = 0x02; // Config Opt: length
  1101. l2capbuf[10] = 0xFF; // MTU
  1102. l2capbuf[11] = 0xFF;
  1103. DBGPrintf("L2CAP_ConfigRequest called(");
  1104. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1105. }
  1106. void BluetoothController::sendl2cap_ConfigResponse(uint16_t handle, uint8_t rxid, uint16_t scid) {
  1107. uint8_t l2capbuf[14];
  1108. l2capbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
  1109. l2capbuf[1] = rxid; // Identifier
  1110. l2capbuf[2] = 0x0A; // Length
  1111. l2capbuf[3] = 0x00;
  1112. l2capbuf[4] = scid & 0xff; // Source CID
  1113. l2capbuf[5] = (scid >> 8) & 0xff;
  1114. l2capbuf[6] = 0x00; // Flag
  1115. l2capbuf[7] = 0x00;
  1116. l2capbuf[8] = 0x00; // Result
  1117. l2capbuf[9] = 0x00;
  1118. l2capbuf[10] = 0x01; // Config
  1119. l2capbuf[11] = 0x02;
  1120. l2capbuf[12] = 0xA0;
  1121. l2capbuf[13] = 0x02;
  1122. DBGPrintf("L2CAP_ConfigResponse called(");
  1123. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1124. }
  1125. //*******************************************************************
  1126. //*******************************************************************
  1127. void BluetoothController::tx_data(const Transfer_t *transfer)
  1128. {
  1129. println(" tx_data(bluetooth) ", pending_control_, HEX);
  1130. #ifdef DEBUG_BT_VERBOSE
  1131. DBGPrintf("tx_data callback (bluetooth): %d : ", pending_control_tx_);
  1132. uint8_t *buffer = (uint8_t*)transfer->buffer;
  1133. for (uint8_t i=0; i < transfer->length; i++) DBGPrintf("%x ", buffer[i]);
  1134. DBGPrintf("\n");
  1135. #endif
  1136. switch (pending_control_tx_) {
  1137. case STATE_TX_SEND_CONNECT_INT:
  1138. delay(1);
  1139. connection_rxid_++;
  1140. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, interrupt_dcid_, HID_INTR_PSM);
  1141. pending_control_tx_ = 0;
  1142. break;
  1143. case STATE_TX_SEND_CONECT_RSP_SUCCESS:
  1144. delay(1);
  1145. // Tell the device we are ready
  1146. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, control_dcid_, control_scid_, SUCCESSFUL);
  1147. pending_control_tx_ = STATE_TX_SEND_CONFIG_REQ;
  1148. break;
  1149. case STATE_TX_SEND_CONFIG_REQ:
  1150. delay(1);
  1151. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, control_scid_);
  1152. pending_control_tx_ = 0;
  1153. break;
  1154. case STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS:
  1155. delay(1);
  1156. // Tell the device we are ready
  1157. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, interrupt_dcid_, interrupt_scid_, SUCCESSFUL);
  1158. pending_control_tx_ = STATE_TX_SEND_CONFIG_ISR_REQ;
  1159. break;
  1160. case STATE_TX_SEND_CONFIG_ISR_REQ:
  1161. delay(1);
  1162. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, interrupt_scid_);
  1163. pending_control_tx_ = 0;
  1164. break;
  1165. }
  1166. }
  1167. //*******************************************************************
  1168. //
  1169. // HCI ACL Packets
  1170. // HCI Handle Low, HCI_Handle_High (PB, BC), Total length low, TLH - HCI ACL Data packet
  1171. // length Low, length high, channel id low, channel id high - L2CAP header
  1172. // code, identifier, length, ... - Control-frame
  1173. /************************************************************/
  1174. /* L2CAP Commands */
  1175. // Public wrrapper function
  1176. void BluetoothController::sendL2CapCommand(uint8_t* data, uint8_t nbytes, int channel) {
  1177. uint16_t channel_out;
  1178. switch (channel) {
  1179. case CONTROL_SCID:
  1180. channel_out = control_scid_;
  1181. break;
  1182. default:
  1183. channel_out = (uint16_t)channel;
  1184. }
  1185. DBGPrintf("sendL2CapCommand: %x %d %x\n", (uint32_t)data, nbytes, channel, channel_out);
  1186. sendL2CapCommand (device_connection_handle_, data, nbytes, channel_out & 0xff, (channel_out >> 8) & 0xff);
  1187. }
  1188. void BluetoothController::sendL2CapCommand(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh)
  1189. {
  1190. txbuf_[0] = handle & 0xff; // HCI handle with PB,BC flag
  1191. txbuf_[1] = (((handle >> 8) & 0x0f) | 0x20);
  1192. txbuf_[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
  1193. txbuf_[3] = (uint8_t)((4 + nbytes) >> 8);
  1194. txbuf_[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
  1195. txbuf_[5] = (uint8_t)(nbytes >> 8);
  1196. txbuf_[6] = channelLow;
  1197. txbuf_[7] = channelHigh;
  1198. if (nbytes) {
  1199. memcpy(&txbuf_[8], data, nbytes); // copy in the commands parameters.
  1200. }
  1201. nbytes = nbytes+8;
  1202. for (uint8_t i=0; i< nbytes; i++) DBGPrintf("%02x ", txbuf_[i]);
  1203. DBGPrintf(")\n");
  1204. if (!queue_Data_Transfer(txpipe_, txbuf_, nbytes, this)) {
  1205. println("sendL2CapCommand failed");
  1206. }
  1207. }
  1208. void BluetoothController::process_l2cap_connection_request(uint8_t *data) {
  1209. // ID LEN LEN PSM PSM SCID SCID
  1210. // 0x02 0x02 0x04 0x00 0x11 0x00 0x43 0x00
  1211. uint16_t psm = data[4]+((uint16_t)data[5] << 8);
  1212. uint16_t scid = data[6]+((uint16_t)data[7] << 8);
  1213. connection_rxid_ = data[1];
  1214. DBGPrintf(" L2CAP Connection Request: ID: %d, PSM: %x, SCID: %x\n",connection_rxid_, psm, scid);
  1215. // Assuming not pair mode Send response like:
  1216. // RXID Len LEN DCID DCID SCID SCID RES 0 0 0
  1217. // 0x03 0x02 0x08 0x00 0x70 0x00 0x43 0x00 0x01 0x00 0x00 0x00
  1218. if (psm == HID_CTRL_PSM) {
  1219. control_scid_ = scid;
  1220. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, control_dcid_, control_scid_, PENDING);
  1221. pending_control_tx_ = STATE_TX_SEND_CONECT_RSP_SUCCESS;
  1222. } else if (psm == HID_INTR_PSM) {
  1223. interrupt_scid_ = scid;
  1224. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, interrupt_dcid_, interrupt_scid_, PENDING);
  1225. pending_control_tx_ = STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS;
  1226. }
  1227. }
  1228. // Process the l2cap_connection_response...
  1229. void BluetoothController::process_l2cap_connection_response(uint8_t *data) {
  1230. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1231. uint16_t dcid = data[6]+((uint16_t)data[7] << 8);
  1232. DBGPrintf(" L2CAP Connection Response: ID: %d, Dest:%x, Source:%x, Result:%x, Status: %x\n",
  1233. data[1], scid, dcid,
  1234. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1235. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  1236. if (dcid == interrupt_dcid_) {
  1237. interrupt_scid_ = scid;
  1238. DBGPrintf(" Interrupt Response\n");
  1239. connection_rxid_++;
  1240. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1241. } else if (dcid == control_dcid_) {
  1242. control_scid_ = scid;
  1243. DBGPrintf(" Control Response\n");
  1244. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1245. }
  1246. }
  1247. void BluetoothController::process_l2cap_config_request(uint8_t *data) {
  1248. //48 20 10 0 c 0 1 0 *4 2 8 0 70 0 0 0 1 2 30 0
  1249. uint16_t dcid = data[4]+((uint16_t)data[5] << 8);
  1250. DBGPrintf(" L2CAP config Request: ID: %d, Dest:%x, Flags:%x, Options: %x %x %x %x\n",
  1251. data[1], dcid, data[6]+((uint16_t)data[7] << 8),
  1252. data[8], data[9], data[10], data[11]);
  1253. // Now see which dest was specified
  1254. if (dcid == control_dcid_) {
  1255. DBGPrintf(" Control Configuration request\n");
  1256. sendl2cap_ConfigResponse(device_connection_handle_, data[1], control_scid_);
  1257. } else if (dcid == interrupt_dcid_) {
  1258. DBGPrintf(" Interrupt Configuration request\n");
  1259. sendl2cap_ConfigResponse(device_connection_handle_, data[1], interrupt_scid_);
  1260. }
  1261. }
  1262. void BluetoothController::process_l2cap_config_response(uint8_t *data) {
  1263. // 48 20 12 0 e 0 1 0 5 0 a 0 70 0 0 0 0 0 1 2 30 0
  1264. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1265. DBGPrintf(" L2CAP config Response: ID: %d, Source:%x, Flags:%x, Result:%x, Config: %x\n",
  1266. data[1], scid, data[6]+((uint16_t)data[7] << 8),
  1267. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1268. if (scid == control_dcid_) {
  1269. // Set HID Boot mode
  1270. // Don't do if PS3...
  1271. if (!(device_driver_->special_process_required & BTHIDInput::SP_PS3_IDS)) {
  1272. setHIDProtocol(HID_BOOT_PROTOCOL); //
  1273. }
  1274. //setHIDProtocol(HID_RPT_PROTOCOL); //HID_RPT_PROTOCOL
  1275. if (do_pair_device_) {
  1276. pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
  1277. } else if (device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_NEED_CONNECT)) {
  1278. DBGPrintf(" Needs connect to device INT(PS4?)\n");
  1279. // The PS4 requires a connection request to it.
  1280. pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
  1281. } else {
  1282. pending_control_ = 0;
  1283. }
  1284. } else if (scid == interrupt_dcid_) {
  1285. // Enable SCan to page mode
  1286. connection_complete_ = true;
  1287. sendHCIWriteScanEnable(2);
  1288. }
  1289. }
  1290. void BluetoothController::process_l2cap_command_reject(uint8_t *data) {
  1291. // 48 20 b 0 7 0 70 0 *1 0 0 0 2 0 4
  1292. DBGPrintf(" L2CAP command reject: ID: %d, length:%x, Reason:%x, Data: %x %x \n",
  1293. data[1], data[2] + ((uint16_t)data[3] << 8), data[4], data[5], data[6]);
  1294. }
  1295. void BluetoothController::process_l2cap_disconnect_request(uint8_t *data) {
  1296. uint16_t dcid = data[4]+((uint16_t)data[5] << 8);
  1297. uint16_t scid = data[6]+((uint16_t)data[7] << 8);
  1298. DBGPrintf(" L2CAP disconnect request: ID: %d, Length:%x, Dest:%x, Source:%x\n",
  1299. data[1], data[2] + ((uint16_t)data[3] << 8), dcid, scid);
  1300. }
  1301. void BluetoothController::setHIDProtocol(uint8_t protocol) {
  1302. // Should verify protocol is boot or report
  1303. uint8_t l2capbuf[1];
  1304. l2capbuf[0] = 0x70 | protocol; // Set Protocol, see Bluetooth HID specs page 33
  1305. DBGPrintf("Set HID Protocol %d (", protocol);
  1306. sendL2CapCommand(device_connection_handle_, l2capbuf, sizeof(l2capbuf), control_scid_ & 0xff, control_scid_ >> 8);
  1307. }
  1308. void BluetoothController::handleHIDTHDRData(uint8_t *data) {
  1309. // Example
  1310. // T HID data
  1311. //48 20 d 0 9 0 71 0 a1 3 8a cc c5 a 23 22 79
  1312. uint16_t len = data[4] + ((uint16_t)data[5] << 8);
  1313. DBGPrintf("HID HDR Data: len: %d, Type: %d\n", len, data[9]);
  1314. // ??? How to parse??? Use HID object???
  1315. if (device_driver_) {
  1316. device_driver_->process_bluetooth_HID_data(&data[9], len-1); // We skip the first byte...
  1317. } else {
  1318. switch (data[9]) {
  1319. case 1:
  1320. DBGPrintf(" Keyboard report type\n");
  1321. break;
  1322. case 2:
  1323. DBGPrintf(" Mouse report type\n");
  1324. break;
  1325. case 3:
  1326. DBGPrintf(" Combo keyboard/pointing\n");
  1327. break;
  1328. default:
  1329. DBGPrintf(" Unknown report\n");
  1330. }
  1331. }
  1332. }