選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1521 行
55KB

  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, EV_INQUIRY_RESULTS_WITH_RSSI=0x22, EV_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(false);
  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 EV_INQUIRY_RESULTS_WITH_RSSI:
  379. handle_hci_inquiry_result(true);
  380. break;
  381. case EV_EXTENDED_INQUIRY_RESULT:
  382. handle_hci_extended_inquiry_result();
  383. break;
  384. default:
  385. break;
  386. }
  387. // Start read at start of buffer.
  388. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  389. } else {
  390. // Continue the read - Todo - maybe verify len == rx_size_
  391. queue_Data_Transfer(rxpipe_, buffer + rx_size_, rx_size_, this);
  392. return; // Don't process the message yet as we still have data to receive.
  393. }
  394. }
  395. //===================================================================
  396. // Called when an HCI command completes.
  397. void BluetoothController::handle_hci_command_complete()
  398. {
  399. uint16_t hci_command = rxbuf_[3] + (rxbuf_[4] << 8);
  400. uint8_t buffer_index;
  401. if(!rxbuf_[5]) {
  402. VDBGPrintf(" Command Completed! \n");
  403. } else {
  404. VDBGPrintf(" Command(%x) Completed - Error: %d! \n", hci_command, rxbuf_[5]);
  405. // BUGBUG:: probably need to queue something?
  406. }
  407. switch (hci_command) {
  408. case HCI_OP_REMOTE_NAME_REQ:
  409. break;
  410. case HCI_RESET: //0x0c03
  411. if (!rxbuf_[5]) pending_control_ = PC_WRITE_CLASS_DEVICE;
  412. // If it fails, will retry. maybe should have repeat max...
  413. break;
  414. case HCI_Set_Event_Filter_Clear: //0x0c05
  415. break;
  416. case HCI_Read_Local_Name: //0x0c14
  417. // received name back...
  418. {
  419. //BUGBUG:: probably want to grab string object and copy to
  420. USBHDBGSerial.printf(" Local name: %s\n", &rxbuf_[6]);
  421. /*
  422. uint8_t len = rxbuf_[1]+2; // Length field +2 for total bytes read
  423. for (uint8_t i=6; i < len; i++) {
  424. if (rxbuf_[i] == 0) {
  425. break;
  426. }
  427. USBHDBGSerial.printf("%c", rxbuf_[i]);
  428. }
  429. USBHDBGSerial.printf("\n"); */
  430. }
  431. break;
  432. case Write_Connection_Accept_Timeout: //0x0c16
  433. break;
  434. case HCI_READ_CLASS_OF_DEVICE: // 0x0c23
  435. break;
  436. case HCI_Read_Voice_Setting: //0x0c25
  437. break;
  438. case HCI_Read_Number_Of_Supported_IAC: //0x0c38
  439. break;
  440. case HCI_Read_Current_IAC_LAP: //0x0c39
  441. break;
  442. case HCI_WRITE_INQUIRY_MODE: //0x0c45
  443. break;
  444. case HCI_Read_Inquiry_Response_Transmit_Power_Level: //0x0c58
  445. break;
  446. case HCI_Read_Local_Supported_Features: //0x1003
  447. // Remember the features supported by local...
  448. for (buffer_index = 0; buffer_index < 8; buffer_index++) {
  449. features[buffer_index] = rxbuf_[buffer_index+6];
  450. }
  451. break;
  452. case HCI_Read_Buffer_Size: // 0x1005
  453. break;
  454. case HCI_Read_BD_ADDR: //0x1009
  455. {
  456. DBGPrintf(" BD Addr");
  457. for(uint8_t i = 0; i < 6; i++) {
  458. my_bdaddr_[i] = rxbuf_[6 + i];
  459. DBGPrintf(":%x", my_bdaddr_[i]);
  460. }
  461. DBGPrintf("\n");
  462. }
  463. break;
  464. case HCI_Read_Local_Version_Information: //0x1001
  465. hciVersion = rxbuf_[6]; // Should do error checking above...
  466. DBGPrintf(" Local Version: %x\n", hciVersion);
  467. pending_control_ = (do_pair_device_)? PC_SEND_WRITE_INQUIRE_MODE : PC_WRITE_SCAN_PAGE;
  468. break;
  469. case HCI_Read_Local_Supported_Commands: //0x1002
  470. break;
  471. case HCI_LE_Read_Buffer_Size: //0x2002
  472. break;
  473. case HCI_LE_Read_Local_supported_Features: //0x2003
  474. break;
  475. case HCI_LE_Supported_States: //0x201c
  476. break;
  477. case HCI_Read_Local_Extended_Features: //0x1004
  478. break;
  479. case HCI_Set_Event_Mask: //0x0c01
  480. break;
  481. case HCI_Read_Stored_Link_Key: //0x0c0d
  482. break;
  483. case HCI_Write_Default_Link_Policy_Settings: //0x080f
  484. break;
  485. case HCI_Read_Page_Scan_Activity: //0x0c1b
  486. break;
  487. case HCI_Read_Page_Scan_Type: //0x0c46
  488. break;
  489. case HCI_LE_SET_EVENT_MASK: //0x2001
  490. break;
  491. case HCI_LE_READ_ADV_TX_POWER: //0x2007
  492. break;
  493. case HCI_LE_READ_WHITE_LIST_SIZE: //0x200f
  494. break;
  495. case HCI_LE_CLEAR_WHITE_LIST: //0x2010
  496. break;
  497. case HCI_DELETE_STORED_LINK_KEY: //0x0c12
  498. break;
  499. case HCI_WRITE_LOCAL_NAME: //0x0c13
  500. break;
  501. case HCI_WRITE_SCAN_ENABLE: //0x0c1a
  502. DBGPrintf("Write_Scan_enable Completed\n");
  503. // See if we have driver and a remote
  504. if (device_driver_ && connection_complete_) { // We have a driver call their
  505. device_driver_->connectionComplete();
  506. connection_complete_ = false; // only call once
  507. }
  508. break;
  509. case HCI_WRITE_SSP_MODE: //0x0c56
  510. break;
  511. case HCI_WRITE_EIR: //0x0c52
  512. break;
  513. case HCI_WRITE_LE_HOST_SUPPORTED: //0x0c6d
  514. break;
  515. case HCI_LE_SET_SCAN_RSP_DATA: //0x2009
  516. break;
  517. }
  518. // And queue up the next command
  519. queue_next_hci_command();
  520. }
  521. void BluetoothController::queue_next_hci_command()
  522. {
  523. // Ok We completed a command now see if we need to queue another command
  524. // Still probably need to reorganize...
  525. switch (pending_control_) {
  526. // Initial setup states.
  527. case PC_RESET:
  528. sendResetHCI();
  529. break;
  530. case PC_WRITE_CLASS_DEVICE:
  531. sendHDCWriteClassOfDev();
  532. pending_control_++;
  533. break;
  534. case PC_READ_BDADDR:
  535. sendHCIReadBDAddr();
  536. pending_control_++;
  537. break;
  538. case PC_READ_LOCAL_VERSION:
  539. sendHCIReadLocalVersionInfo();
  540. //pending_control_++;
  541. break;
  542. // These are used when we are pairing.
  543. case PC_SEND_WRITE_INQUIRE_MODE:
  544. sendHCIHCIWriteInquiryMode(2); // lets set into extended inquire mode
  545. pending_control_++;
  546. break;
  547. case PC_SEND_SET_EVENT_MASK:
  548. sendHCISetEventMask(); // Set the event mask to include extend inquire event
  549. pending_control_++;
  550. break;
  551. case PC_SEND_INQUIRE:
  552. sendHCI_INQUIRY();
  553. pending_control_++;
  554. break;
  555. case PC_INQUIRE_CANCEL:
  556. // lets try to create a connection...
  557. sendHCICreateConnection();
  558. pending_control_++;
  559. break;
  560. case PC_AUTHENTICATION_REQUESTED:
  561. break;
  562. case PC_LINK_KEY_NEGATIVE:
  563. break;
  564. case PC_PIN_CODE_REPLY:
  565. break;
  566. // None Pair mode
  567. case PC_WRITE_SCAN_PAGE:
  568. sendHCIWriteScanEnable(2);
  569. pending_control_ = 0; //
  570. break;
  571. default:
  572. break;
  573. }
  574. }
  575. void BluetoothController::handle_hci_command_status()
  576. {
  577. // <event type><param count><status><num packets allowed to be sent><CMD><CMD>
  578. #ifdef DEBUG_BT
  579. uint16_t hci_command = rxbuf_[4] + (rxbuf_[5] << 8);
  580. if (rxbuf_[2]) {
  581. DBGPrintf(" Command %x Status %x - ", hci_command, rxbuf_[2]);
  582. switch (rxbuf_[2]) {
  583. case 0x01: DBGPrintf("Unknown HCI Command\n"); break;
  584. case 0x02: DBGPrintf("Unknown Connection Identifier\n"); break;
  585. case 0x03: DBGPrintf("Hardware Failure\n"); break;
  586. case 0x04: DBGPrintf("Page Timeout\n"); break;
  587. case 0x05: DBGPrintf("Authentication Failure\n"); break;
  588. case 0x06: DBGPrintf("PIN or Key Missing\n"); break;
  589. case 0x07: DBGPrintf("Memory Capacity Exceeded\n"); break;
  590. case 0x08: DBGPrintf("Connection Timeout\n"); break;
  591. case 0x09: DBGPrintf("Connection Limit Exceeded\n"); break;
  592. case 0x0A: DBGPrintf("Synchronous Connection Limit To A Device Exceeded\n"); break;
  593. case 0x0B: DBGPrintf("Connection Already Exists\n"); break;
  594. case 0x0C: DBGPrintf("Command Disallowed\n"); break;
  595. case 0x0D: DBGPrintf("Connection Rejected due to Limited Resources\n"); break;
  596. case 0x0E: DBGPrintf("Connection Rejected Due To Security Reasons\n"); break;
  597. case 0x0F: DBGPrintf("Connection Rejected due to Unacceptable BD_ADDR\n"); break;
  598. default: DBGPrintf("???\n"); break;
  599. }
  600. } else {
  601. VDBGPrintf(" Command %x Status %x\n", hci_command, rxbuf_[2]);
  602. }
  603. #endif
  604. }
  605. void BluetoothController::handle_hci_inquiry_result(bool fRSSI)
  606. {
  607. // result - versus result with RSSI
  608. // | Diverge here...
  609. // 2 f 1 79 22 23 a c5 cc 1 2 0 40 25 0 3b 2
  610. // 22 f 1 79 22 23 a c5 cc 1 2 40 25 0 3e 31 d2
  611. // Wondered if multiple items if all of the BDADDR are first then next field...
  612. // looks like it is that way...
  613. // Section 7.7.2
  614. if (fRSSI) DBGPrintf(" Inquiry Result with RSSI - Count: %d\n", rxbuf_[2]);
  615. else DBGPrintf(" Inquiry Result - Count: %d\n", rxbuf_[2]);
  616. for (uint8_t i=0; i < rxbuf_[2]; i++) {
  617. uint8_t index_bd = 3 + (i*6);
  618. uint8_t index_ps = 3 + (6*rxbuf_[2]) + i;
  619. uint8_t index_class = 3 + (9*rxbuf_[2]) + i;
  620. uint8_t index_clock_offset = 3 + (12*rxbuf_[2]) + i;
  621. if (fRSSI) {
  622. // Handle the differences in offsets here...
  623. index_class = 3 + (8*rxbuf_[2]) + i;
  624. index_clock_offset = 3 + (11*rxbuf_[2]) + i;
  625. }
  626. uint32_t bluetooth_class = rxbuf_[index_class] + ((uint32_t)rxbuf_[index_class+1] << 8) + ((uint32_t)rxbuf_[index_class+2] << 16);
  627. DBGPrintf(" BD:%x:%x:%x:%x:%x:%x, PS:%d, class: %x\n",
  628. rxbuf_[index_bd],rxbuf_[index_bd+1],rxbuf_[index_bd+2],rxbuf_[index_bd+3],rxbuf_[index_bd+4],rxbuf_[index_bd+5],
  629. rxbuf_[index_ps], bluetooth_class);
  630. // See if we know the class
  631. if (((bluetooth_class & 0xff00) == 0x2500) || ((bluetooth_class & 0xff00) == 0x500)) {
  632. DBGPrintf(" Peripheral device\n");
  633. if (bluetooth_class & 0x80) DBGPrintf(" Mouse\n");
  634. if (bluetooth_class & 0x40) DBGPrintf(" Keyboard\n");
  635. switch(bluetooth_class & 0x3c) {
  636. case 4: DBGPrintf(" Joystick\n"); break;
  637. case 8: DBGPrintf(" Gamepad\n"); break;
  638. case 0xc: DBGPrintf(" Remote Control\n"); break;
  639. }
  640. // BUGBUG, lets hard code to go to new state...
  641. for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = rxbuf_[index_bd+i];
  642. device_class_ = bluetooth_class;
  643. device_driver_ = find_driver(device_class_);
  644. device_ps_repetion_mode_ = rxbuf_[index_ps]; // mode
  645. device_clock_offset_[0] = rxbuf_[index_clock_offset];
  646. device_clock_offset_[1] = rxbuf_[index_clock_offset+1];
  647. // Now we need to bail from inquiry and setup to try to connect...
  648. sendHCIInquiryCancel();
  649. pending_control_ = PC_INQUIRE_CANCEL;
  650. break;
  651. }
  652. }
  653. }
  654. void BluetoothController::handle_hci_extended_inquiry_result()
  655. {
  656. DBGPrintf(" Extended Inquiry Result - Count: %d\n", rxbuf_[2]);
  657. // Should always be only one result here.
  658. uint8_t index_bd = 3;
  659. uint8_t index_ps = 9;
  660. uint8_t index_class = 11;
  661. uint8_t index_clock_offset = 14;
  662. //uint8_t index_rssi = 16;
  663. uint8_t index_eir_data = 17;
  664. uint8_t index_local_name = 0;
  665. uint8_t size_local_name = 0;
  666. uint32_t bluetooth_class = rxbuf_[index_class] + ((uint32_t)rxbuf_[index_class+1] << 8) + ((uint32_t)rxbuf_[index_class+2] << 16);
  667. DBGPrintf(" BD:%x:%x:%x:%x:%x:%x, PS:%d, class: %x\n",
  668. rxbuf_[index_bd],rxbuf_[index_bd+1],rxbuf_[index_bd+2],rxbuf_[index_bd+3],rxbuf_[index_bd+4],rxbuf_[index_bd+5],
  669. rxbuf_[index_ps], bluetooth_class);
  670. // Lets see if we can find a name
  671. while (index_eir_data < 256) {
  672. if (rxbuf_[index_eir_data] == 0) break; // no more data
  673. switch (rxbuf_[index_eir_data+1]) {
  674. case 0x08: // Shortened local name
  675. case 0x09: // complete local name
  676. index_local_name = index_eir_data+2;
  677. size_local_name = rxbuf_[index_eir_data]-1;
  678. break;
  679. }
  680. index_eir_data += rxbuf_[index_eir_data] + 1; // point to the next item
  681. }
  682. if (index_local_name && size_local_name) {
  683. // Hack lets null teminate the string
  684. rxbuf_[index_local_name+size_local_name] = 0;
  685. DBGPrintf(" Local Name: %s\n", &rxbuf_[index_local_name]);
  686. }
  687. // See if we know the class
  688. if (((bluetooth_class & 0xff00) == 0x2500) || ((bluetooth_class & 0xff00) == 0x500)) {
  689. DBGPrintf(" Peripheral device\n");
  690. if (bluetooth_class & 0x80) DBGPrintf(" Mouse\n");
  691. if (bluetooth_class & 0x40) DBGPrintf(" Keyboard\n");
  692. switch(bluetooth_class & 0x3c) {
  693. case 4: DBGPrintf(" Joystick\n"); break;
  694. case 8: DBGPrintf(" Gamepad\n"); break;
  695. case 0xc: DBGPrintf(" Remote Control\n"); break;
  696. }
  697. // BUGBUG, lets hard code to go to new state...
  698. for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = rxbuf_[index_bd+i];
  699. device_class_ = bluetooth_class;
  700. device_driver_ = find_driver(device_class_, index_local_name? &rxbuf_[index_local_name] : nullptr);
  701. device_ps_repetion_mode_ = rxbuf_[index_ps]; // mode
  702. device_clock_offset_[0] = rxbuf_[index_clock_offset];
  703. device_clock_offset_[1] = rxbuf_[index_clock_offset+1];
  704. // and if we found a driver, save away the name
  705. if (device_driver_ && index_local_name && size_local_name) {
  706. uint8_t buffer_index;
  707. for (buffer_index = 0; size_local_name && (buffer_index < BTHIDInput::REMOTE_NAME_SIZE-1); buffer_index++) {
  708. device_driver_->remote_name_[buffer_index] = rxbuf_[index_local_name+buffer_index];
  709. size_local_name--;
  710. }
  711. device_driver_->remote_name_[buffer_index] = 0; // make sure null terminated
  712. }
  713. // Now we need to bail from inquiry and setup to try to connect...
  714. sendHCIInquiryCancel();
  715. pending_control_ = PC_INQUIRE_CANCEL;
  716. }
  717. }
  718. void BluetoothController::handle_hci_inquiry_complete() {
  719. VDBGPrintf(" Inquiry Complete - status: %d\n", rxbuf_[2]);
  720. }
  721. void BluetoothController::handle_hci_connection_complete() {
  722. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  723. // ST CH CH BD BD BD BD BD BD LT EN
  724. // 03 0b 04 00 00 40 25 00 58 4b 00 01 00
  725. device_connection_handle_ = rxbuf_[3]+ (uint16_t)(rxbuf_[4]<<8);
  726. DBGPrintf(" Connection Complete - ST:%x LH:%x\n", rxbuf_[2], device_connection_handle_);
  727. if (do_pair_device_) {
  728. sendHCIAuthenticationRequested();
  729. pending_control_ = PC_AUTHENTICATION_REQUESTED;
  730. } else if (device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_NEED_CONNECT)) {
  731. DBGPrintf(" Needs connect to device(PS4?)\n");
  732. // The PS4 requires a connection request to it.
  733. delay(1);
  734. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
  735. #if 0
  736. delay(1);
  737. uint8_t packet[2];
  738. memset(packet, 0, sizeof(packet));
  739. packet[0] = 0x43;
  740. packet[1] = 0x02; // Report ID
  741. USBHDBGSerial.printf("SixAxis Command Issued!\r\n");
  742. sendL2CapCommand(packet, sizeof(packet), 0x40);
  743. #endif
  744. }
  745. }
  746. void BluetoothController::handle_hci_incoming_connect() {
  747. // BD BD BD BD BD BD CL CL CL LT
  748. // 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x40 0x05 0x00 0x01
  749. uint32_t class_of_device = rxbuf_[8] + (uint16_t)(rxbuf_[9]<<8) + (uint32_t)(rxbuf_[10]<<16);
  750. DBGPrintf(" Event: Incoming Connect - %x:%x:%x:%x:%x:%x CL:%x LT:%x\n",
  751. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], class_of_device, rxbuf_[11]);
  752. if (((class_of_device & 0xff00) == 0x2500) || ((class_of_device & 0xff00) == 0x500)) {
  753. DBGPrintf(" Peripheral device\n");
  754. if (class_of_device & 0x80) DBGPrintf(" Mouse\n");
  755. if (class_of_device & 0x40) DBGPrintf(" Keyboard\n");
  756. switch(class_of_device & 0x3c) {
  757. case 4: DBGPrintf(" Joystick\n"); break;
  758. case 8: DBGPrintf(" Gamepad\n"); break;
  759. case 0xc: DBGPrintf(" Remote Control\n"); break;
  760. }
  761. device_driver_ = find_driver(class_of_device);
  762. // We need to save away the BDADDR and class link type?
  763. for(uint8_t i=0; i<6; i++) device_bdaddr_[i] = rxbuf_[i+2];
  764. device_class_ = class_of_device;
  765. sendHCIRemoteNameRequest();
  766. }
  767. // sendHCIAuthenticationRequested();
  768. // pending_control_ = PC_AUTHENTICATION_REQUESTED;
  769. }
  770. void BluetoothController::handle_hci_pin_code_request() {
  771. // 0x16 0x06 0x79 0x22 0x23 0x0A 0xC5 0xCC
  772. DBGPrintf(" Event: Pin Code Request %x:%x:%x:%x:%x:%x\n",
  773. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  774. sendHCIPinCodeReply();
  775. pending_control_ = PC_PIN_CODE_REPLY;
  776. }
  777. void BluetoothController::handle_hci_link_key_request() {
  778. // 17 6 79 22 23 a c5 cc
  779. DBGPrintf(" Event: Link Key Request %x:%x:%x:%x:%x:%x\n",
  780. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7]);
  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. sendHCILinkKeyNegativeReply();
  784. pending_control_ = PC_LINK_KEY_NEGATIVE;
  785. }
  786. void BluetoothController::handle_hci_link_key_notification() {
  787. // 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
  788. // 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
  789. DBGPrintf(" Event: Link Key Notificaton %x:%x:%x:%x:%x:%x Type:%x\n key:",
  790. rxbuf_[2], rxbuf_[3], rxbuf_[4], rxbuf_[5], rxbuf_[6], rxbuf_[7], rxbuf_[24]);
  791. for (uint8_t i = 8; i < 24; i++) DBGPrintf("%02x ", rxbuf_[i]);
  792. DBGPrintf("\n");
  793. // Now here is where we need to decide to say we have key or tell them to
  794. // cancel key... right now hard code to cancel...
  795. }
  796. void BluetoothController::handle_hci_disconnect_complete()
  797. {
  798. //5 4 0 48 0 13
  799. DBGPrintf(" Event: HCI Disconnect complete(%d): handle: %x, reason:%x\n", rxbuf_[2],
  800. rxbuf_[3]+(rxbuf_[4]<<8), rxbuf_[5]);
  801. if (device_driver_) {
  802. device_driver_->release_bluetooth();
  803. device_driver_->remote_name_[0] = 0;
  804. device_driver_ = nullptr;
  805. // Restore to normal...
  806. control_dcid_ = 0x70;
  807. interrupt_dcid_ = 0x71;
  808. }
  809. // Probably should clear out connection data.
  810. device_connection_handle_ = 0;
  811. device_class_ = 0;
  812. memset(device_bdaddr_, 0, sizeof(device_bdaddr_));
  813. }
  814. void BluetoothController::handle_hci_authentication_complete()
  815. {
  816. // 6 3 13 48 0
  817. DBGPrintf(" Event: HCI Authentication complete(%d): handle: %x\n", rxbuf_[2],
  818. rxbuf_[3]+(rxbuf_[4]<<8));
  819. // Start up lcap connection...
  820. connection_rxid_ = 0;
  821. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
  822. }
  823. void BluetoothController::handle_hci_remote_name_complete() {
  824. // STAT bd bd bd bd bd bd
  825. // 0x07 0xFF 0x00 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x42 0x6C 0x75 0x65 0x74 0x6F 0x6F ...
  826. DBGPrintf(" Event: handle_hci_remote_name_complete(%d)\n", rxbuf_[2]);
  827. if (rxbuf_[2] == 0) {
  828. DBGPrintf(" Remote Name: ");
  829. for (uint8_t *psz = &rxbuf_[9]; *psz; psz++) DBGPrintf("%c", *psz);
  830. DBGPrintf("\n");
  831. }
  832. if (device_driver_) {
  833. if (!device_driver_->remoteNameComplete(&rxbuf_[9])) {
  834. device_driver_->release_bluetooth();
  835. device_driver_ = nullptr;
  836. }
  837. }
  838. if (!device_driver_) {
  839. device_driver_ = find_driver(device_class_, &rxbuf_[9]);
  840. // not sure I should call remote name again, but they already process...
  841. if (device_driver_) {
  842. device_driver_->remoteNameComplete(&rxbuf_[9]);
  843. }
  844. }
  845. if (device_driver_) {
  846. // lets save away the string.
  847. uint8_t buffer_index;
  848. for (buffer_index = 0; buffer_index < BTHIDInput::REMOTE_NAME_SIZE-1; buffer_index++) {
  849. device_driver_->remote_name_[buffer_index] = rxbuf_[9+buffer_index];
  850. if (!device_driver_->remote_name_[buffer_index]) break;
  851. }
  852. device_driver_->remote_name_[buffer_index] = 0; // make sure null terminated
  853. if (device_driver_->special_process_required & BTHIDInput::SP_PS3_IDS) {
  854. // Real hack see if PS3...
  855. control_dcid_ = 0x40;
  856. interrupt_dcid_ = 0x41;
  857. }
  858. }
  859. // If we are in the connection complete mode, then this is a pairing state and needed to call
  860. // get remote name later.
  861. if (connection_complete_) {
  862. if (device_driver_) { // We have a driver call their
  863. device_driver_->connectionComplete();
  864. connection_complete_ = false; // only call once
  865. }
  866. } else {
  867. sendHCIAcceptConnectionRequest();
  868. }
  869. }
  870. void BluetoothController::handle_hci_remote_version_information_complete() {
  871. // STAT bd bd bd bd bd bd
  872. //c 8 0 48 0 5 45 0 0 0
  873. remote_ver_ = rxbuf_[6];
  874. remote_man_ = rxbuf_[7]+((uint16_t)rxbuf_[8]<< 8);
  875. remote_subv_ = rxbuf_[9];
  876. DBGPrintf(" Event: handle_hci_remote_version_information_complete(%d): ", rxbuf_[2]);
  877. DBGPrintf(" Handle: %x, Ver:%x, Man: %x, SV: %x\n",
  878. rxbuf_[3]+((uint16_t)rxbuf_[4]<< 8), remote_ver_, remote_man_, remote_subv_);
  879. // Lets now try to accept the connection.
  880. sendHCIAcceptConnectionRequest();
  881. }
  882. void BluetoothController::rx2_data(const Transfer_t *transfer)
  883. {
  884. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  885. DBGPrintf("\n=====================\nBT rx2_data(%d): ", len);
  886. uint8_t *buffer = (uint8_t*)transfer->buffer;
  887. for (uint8_t i=0; i < len; i++) DBGPrintf("%x ", buffer[i]);
  888. DBGPrintf("\n");
  889. // call backs. See if this is an L2CAP reply. example
  890. // HCI | l2cap
  891. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  892. // BUGBUG need to do more verification, like the handle
  893. uint16_t hci_length = buffer[2] + ((uint16_t)buffer[3]<<8);
  894. uint16_t l2cap_length = buffer[4] + ((uint16_t)buffer[5]<<8);
  895. // uint16_t rsp_packet_length = buffer[10] + ((uint16_t)buffer[11]<<8);
  896. if ((hci_length == (l2cap_length + 4)) /*&& (hci_length == (rsp_packet_length+8))*/) {
  897. // All the lengths appear to be correct... need to do more...
  898. switch (buffer[8]) {
  899. case L2CAP_CMD_CONNECTION_REQUEST:
  900. process_l2cap_connection_request(&buffer[8]);
  901. break;
  902. case L2CAP_CMD_CONNECTION_RESPONSE:
  903. process_l2cap_connection_response(&buffer[8]);
  904. break;
  905. case L2CAP_CMD_CONFIG_REQUEST:
  906. process_l2cap_config_request(&buffer[8]);
  907. break;
  908. case L2CAP_CMD_CONFIG_RESPONSE:
  909. process_l2cap_config_response(&buffer[8]);
  910. break;
  911. case HID_THDR_DATA_INPUT:
  912. handleHIDTHDRData(buffer); // Pass the whole buffer...
  913. break;
  914. case L2CAP_CMD_COMMAND_REJECT:
  915. process_l2cap_command_reject(&buffer[8]);
  916. break;
  917. case L2CAP_CMD_DISCONNECT_REQUEST:
  918. process_l2cap_disconnect_request(&buffer[8]);
  919. break;
  920. }
  921. }
  922. // Queue up for next read...
  923. queue_Data_Transfer(rx2pipe_, rx2buf_, rx2_size_, this);
  924. }
  925. void BluetoothController::sendHCICommand(uint16_t hciCommand, uint16_t cParams, const uint8_t* data)
  926. {
  927. txbuf_[0] = hciCommand & 0xff;
  928. txbuf_[1] = (hciCommand >> 8) & 0xff;
  929. txbuf_[2] = cParams;
  930. if (cParams) {
  931. memcpy(&txbuf_[3], data, cParams); // copy in the commands parameters.
  932. }
  933. uint8_t nbytes = cParams+3;
  934. for (uint8_t i=0; i< nbytes; i++) DBGPrintf("%02x ", txbuf_[i]);
  935. DBGPrintf(")\n");
  936. mk_setup(setup, 0x20, 0x0, 0, 0, nbytes);
  937. queue_Control_Transfer(device, &setup, txbuf_, this);
  938. }
  939. //---------------------------------------------
  940. void BluetoothController::sendHCIHCIWriteInquiryMode(uint8_t inquiry_mode) {
  941. // Setup Inquiry mode
  942. DBGPrintf("HCI_WRITE_INQUIRY_MODE called (");
  943. sendHCICommand(HCI_WRITE_INQUIRY_MODE, 1, &inquiry_mode);
  944. }
  945. void BluetoothController::sendHCISetEventMask() {
  946. // Setup Inquiry mode
  947. DBGPrintf("HCI_Set_Event_Mask called (");
  948. static const uint8_t hci_event_mask_data[8] = {
  949. // Default: 0x0000 1FFF FFFF FFFF
  950. 0xff,0xff, 0xff,0xff, 0xff,0x5f, 0x00,0x00}; // default plus extended inquiry mode
  951. sendHCICommand(HCI_Set_Event_Mask, sizeof(hci_event_mask_data), hci_event_mask_data);
  952. }
  953. //---------------------------------------------
  954. void BluetoothController::sendHCI_INQUIRY() {
  955. // Start unlimited inqury, set timeout to max and
  956. DBGPrintf("HCI_INQUIRY called (");
  957. static const uint8_t hci_inquiry_data[ ] = {
  958. 0x33, 0x8B, 0x9E, // Bluetooth assigned number LAP 0x9E8B33 General/unlimited inquiry Access mode
  959. 0x30, 0xa}; // Max inquiry time little over minute and up to 10 responses
  960. sendHCICommand(HCI_INQUIRY, sizeof(hci_inquiry_data), hci_inquiry_data);
  961. }
  962. //---------------------------------------------
  963. void BluetoothController::sendHCIInquiryCancel() {
  964. DBGPrintf("HCI_INQUIRY_CANCEL called (");
  965. sendHCICommand(HCI_INQUIRY_CANCEL, 0, nullptr);
  966. }
  967. //---------------------------------------------
  968. void BluetoothController::sendHCICreateConnection() {
  969. DBGPrintf("HCI_CREATE_CONNECTION called (");
  970. uint8_t connection_data[13];
  971. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  972. // BD BD BD BD BD BD PT PT PRS 0 CS CS ARS
  973. //0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  974. //0x05 0x04 0x0D 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x18 0xCC 0x01 0x00 0x00 0x00 0x00
  975. // 05 04 0d 40 25 00 c4 01 00 18 cc 01 00 00 00 00
  976. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  977. connection_data[6] = 0x18; //DM1/DH1
  978. connection_data[7] = 0xcc; //
  979. connection_data[8] = device_ps_repetion_mode_; // from device
  980. connection_data[9] = 0; //
  981. connection_data[10] = 0; // clock offset
  982. connection_data[11] = 0; // clock offset
  983. connection_data[12] = 0; // allow role swith no
  984. sendHCICommand(HCI_CREATE_CONNECTION, sizeof(connection_data), connection_data);
  985. }
  986. //---------------------------------------------
  987. void BluetoothController::sendHCIAcceptConnectionRequest() {
  988. DBGPrintf("HCI_OP_ACCEPT_CONN_REQ called (");
  989. uint8_t connection_data[7];
  990. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  991. // BD BD BD BD BD BD role
  992. //0x79 0x22 0x23 0x0A 0xC5 0xCC 0x00
  993. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  994. connection_data[6] = 0; // Role as master
  995. sendHCICommand(HCI_OP_ACCEPT_CONN_REQ, sizeof(connection_data), connection_data);
  996. }
  997. //---------------------------------------------
  998. void BluetoothController::sendHCIAuthenticationRequested() {
  999. DBGPrintf("HCI_AUTH_REQUESTED called (");
  1000. uint8_t connection_data[2];
  1001. connection_data[0] = device_connection_handle_ & 0xff;
  1002. connection_data[1] = (device_connection_handle_>>8) & 0xff;
  1003. sendHCICommand(HCI_AUTH_REQUESTED, sizeof(connection_data), connection_data);
  1004. }
  1005. //---------------------------------------------
  1006. void BluetoothController::sendHCILinkKeyNegativeReply() {
  1007. DBGPrintf("HCI_LINK_KEY_NEG_REPLY called (");
  1008. uint8_t connection_data[6];
  1009. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  1010. sendHCICommand(HCI_LINK_KEY_NEG_REPLY, sizeof(connection_data), connection_data);
  1011. }
  1012. //---------------------------------------------
  1013. // BUGBUG:: hard code string for this pass.
  1014. void BluetoothController::sendHCIPinCodeReply() {
  1015. // 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
  1016. DBGPrintf("HCI_PIN_CODE_REPLY called (");
  1017. uint8_t connection_data[23];
  1018. uint8_t i;
  1019. for (i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  1020. for (i=0; pair_pincode_[i] !=0; i++) connection_data[7+i] = pair_pincode_[i];
  1021. connection_data[6] = i; // remember the length
  1022. for (uint8_t i=7+connection_data[6]; i<23; i++) connection_data[i] = 0;
  1023. sendHCICommand(HCI_PIN_CODE_REPLY, sizeof(connection_data), connection_data);
  1024. }
  1025. //---------------------------------------------
  1026. void BluetoothController::sendResetHCI() {
  1027. DBGPrintf("HCI_RESET called (");
  1028. sendHCICommand(HCI_RESET, 0, nullptr);
  1029. }
  1030. void BluetoothController::sendHDCWriteClassOfDev() {
  1031. // 0x24 0x0C 0x03 0x04 0x08 0x00
  1032. const static uint8_t device_class_data[] = {BT_CLASS_DEVICE & 0xff, (BT_CLASS_DEVICE >> 8) & 0xff, (BT_CLASS_DEVICE >> 16) & 0xff};
  1033. DBGPrintf("HCI_WRITE_CLASS_OF_DEV called (");
  1034. sendHCICommand(HCI_WRITE_CLASS_OF_DEV, sizeof(device_class_data), device_class_data);
  1035. }
  1036. void BluetoothController::sendHCIReadBDAddr() {
  1037. DBGPrintf("HCI_Read_BD_ADDR called (");
  1038. sendHCICommand(HCI_Read_BD_ADDR, 0, nullptr);
  1039. }
  1040. void BluetoothController::sendHCIReadLocalVersionInfo() {
  1041. DBGPrintf("HCI_Read_Local_Version_Information called (");
  1042. sendHCICommand(HCI_Read_Local_Version_Information, 0, nullptr);
  1043. }
  1044. void BluetoothController::sendHCIWriteScanEnable(uint8_t scan_op) {// 0x0c1a
  1045. // 0x1A 0x0C 0x01 0x02
  1046. DBGPrintf("HCI_WRITE_SCAN_ENABLE called(");
  1047. sendHCICommand(HCI_WRITE_SCAN_ENABLE, 1, &scan_op);
  1048. }
  1049. void BluetoothController::sendHCIRemoteNameRequest() { // 0x0419
  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_REMOTE_NAME_REQ called (");
  1053. uint8_t connection_data[10];
  1054. for (uint8_t i=0; i<6; i++) connection_data[i] = device_bdaddr_[i];
  1055. connection_data[6] = 1; // page scan repeat mode...
  1056. connection_data[7] = 0; // 0
  1057. connection_data[8] = 0; // Clk offset
  1058. connection_data[9] = 0;
  1059. sendHCICommand(HCI_OP_REMOTE_NAME_REQ, sizeof(connection_data), connection_data);
  1060. }
  1061. void BluetoothController::sendHCIRemoteVersionInfoRequest() { // 0x041D
  1062. // BD BD BD BD BD BD PS 0 CLK CLK
  1063. //0x19 0x04 0x0A 0x79 0x22 0x23 0x0A 0xC5 0xCC 0x01 0x00 0x00 0x00
  1064. DBGPrintf("HCI_OP_READ_REMOTE_VERSION_INFORMATION called (");
  1065. uint8_t connection_data[2];
  1066. connection_data[0] = device_connection_handle_ & 0xff;
  1067. connection_data[1] = (device_connection_handle_>>8) & 0xff;
  1068. sendHCICommand(HCI_OP_READ_REMOTE_VERSION_INFORMATION, sizeof(connection_data), connection_data);
  1069. }
  1070. // l2cap support functions.
  1071. void BluetoothController::sendl2cap_ConnectionResponse(uint16_t handle, uint8_t rxid, uint16_t dcid, uint16_t scid, uint8_t result) {
  1072. uint8_t l2capbuf[12];
  1073. l2capbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
  1074. l2capbuf[1] = rxid; // Identifier
  1075. l2capbuf[2] = 0x08; // Length
  1076. l2capbuf[3] = 0x00;
  1077. l2capbuf[4] = dcid & 0xff; // Destination CID
  1078. l2capbuf[5] = dcid >> 8;
  1079. l2capbuf[6] = scid & 0xff; // Source CID
  1080. l2capbuf[7] = scid >> 8;
  1081. l2capbuf[8] = result; // Result: Pending or Success
  1082. l2capbuf[9] = 0x00;
  1083. l2capbuf[10] = 0x00; // No further information
  1084. l2capbuf[11] = 0x00;
  1085. DBGPrintf("L2CAP_CMD_CONNECTION_RESPONSE called(");
  1086. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1087. }
  1088. void BluetoothController::sendl2cap_ConnectionRequest(uint16_t handle, uint8_t rxid, uint16_t scid, uint16_t psm) {
  1089. uint8_t l2capbuf[8];
  1090. l2capbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
  1091. l2capbuf[1] = rxid; // Identifier
  1092. l2capbuf[2] = 0x04; // Length
  1093. l2capbuf[3] = 0x00;
  1094. l2capbuf[4] = (uint8_t)(psm & 0xff); // PSM
  1095. l2capbuf[5] = (uint8_t)(psm >> 8);
  1096. l2capbuf[6] = scid & 0xff; // Source CID
  1097. l2capbuf[7] = (scid >> 8) & 0xff;
  1098. DBGPrintf("ConnectionRequest called(");
  1099. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1100. }
  1101. void BluetoothController::sendl2cap_ConfigRequest(uint16_t handle, uint8_t rxid, uint16_t dcid) {
  1102. uint8_t l2capbuf[12];
  1103. l2capbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
  1104. l2capbuf[1] = rxid; // Identifier
  1105. l2capbuf[2] = 0x08; // Length
  1106. l2capbuf[3] = 0x00;
  1107. l2capbuf[4] = dcid & 0xff; // Destination CID
  1108. l2capbuf[5] = (dcid >> 8) & 0xff;
  1109. l2capbuf[6] = 0x00; // Flags
  1110. l2capbuf[7] = 0x00;
  1111. l2capbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
  1112. l2capbuf[9] = 0x02; // Config Opt: length
  1113. l2capbuf[10] = 0xFF; // MTU
  1114. l2capbuf[11] = 0xFF;
  1115. DBGPrintf("L2CAP_ConfigRequest called(");
  1116. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1117. }
  1118. void BluetoothController::sendl2cap_ConfigResponse(uint16_t handle, uint8_t rxid, uint16_t scid) {
  1119. uint8_t l2capbuf[14];
  1120. l2capbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
  1121. l2capbuf[1] = rxid; // Identifier
  1122. l2capbuf[2] = 0x0A; // Length
  1123. l2capbuf[3] = 0x00;
  1124. l2capbuf[4] = scid & 0xff; // Source CID
  1125. l2capbuf[5] = (scid >> 8) & 0xff;
  1126. l2capbuf[6] = 0x00; // Flag
  1127. l2capbuf[7] = 0x00;
  1128. l2capbuf[8] = 0x00; // Result
  1129. l2capbuf[9] = 0x00;
  1130. l2capbuf[10] = 0x01; // Config
  1131. l2capbuf[11] = 0x02;
  1132. l2capbuf[12] = 0xA0;
  1133. l2capbuf[13] = 0x02;
  1134. DBGPrintf("L2CAP_ConfigResponse called(");
  1135. sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
  1136. }
  1137. //*******************************************************************
  1138. //*******************************************************************
  1139. void BluetoothController::tx_data(const Transfer_t *transfer)
  1140. {
  1141. println(" tx_data(bluetooth) ", pending_control_, HEX);
  1142. #ifdef DEBUG_BT_VERBOSE
  1143. DBGPrintf("tx_data callback (bluetooth): %d : ", pending_control_tx_);
  1144. uint8_t *buffer = (uint8_t*)transfer->buffer;
  1145. for (uint8_t i=0; i < transfer->length; i++) DBGPrintf("%x ", buffer[i]);
  1146. DBGPrintf("\n");
  1147. #endif
  1148. switch (pending_control_tx_) {
  1149. case STATE_TX_SEND_CONNECT_INT:
  1150. delay(1);
  1151. connection_rxid_++;
  1152. sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, interrupt_dcid_, HID_INTR_PSM);
  1153. pending_control_tx_ = 0;
  1154. break;
  1155. case STATE_TX_SEND_CONECT_RSP_SUCCESS:
  1156. delay(1);
  1157. // Tell the device we are ready
  1158. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, control_dcid_, control_scid_, SUCCESSFUL);
  1159. pending_control_tx_ = STATE_TX_SEND_CONFIG_REQ;
  1160. break;
  1161. case STATE_TX_SEND_CONFIG_REQ:
  1162. delay(1);
  1163. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, control_scid_);
  1164. pending_control_tx_ = 0;
  1165. break;
  1166. case STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS:
  1167. delay(1);
  1168. // Tell the device we are ready
  1169. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, interrupt_dcid_, interrupt_scid_, SUCCESSFUL);
  1170. pending_control_tx_ = STATE_TX_SEND_CONFIG_ISR_REQ;
  1171. break;
  1172. case STATE_TX_SEND_CONFIG_ISR_REQ:
  1173. delay(1);
  1174. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, interrupt_scid_);
  1175. pending_control_tx_ = 0;
  1176. break;
  1177. }
  1178. }
  1179. //*******************************************************************
  1180. //
  1181. // HCI ACL Packets
  1182. // HCI Handle Low, HCI_Handle_High (PB, BC), Total length low, TLH - HCI ACL Data packet
  1183. // length Low, length high, channel id low, channel id high - L2CAP header
  1184. // code, identifier, length, ... - Control-frame
  1185. /************************************************************/
  1186. /* L2CAP Commands */
  1187. // Public wrrapper function
  1188. void BluetoothController::sendL2CapCommand(uint8_t* data, uint8_t nbytes, int channel) {
  1189. uint16_t channel_out;
  1190. switch (channel) {
  1191. case CONTROL_SCID:
  1192. channel_out = control_scid_;
  1193. break;
  1194. default:
  1195. channel_out = (uint16_t)channel;
  1196. }
  1197. DBGPrintf("sendL2CapCommand: %x %d %x\n", (uint32_t)data, nbytes, channel, channel_out);
  1198. sendL2CapCommand (device_connection_handle_, data, nbytes, channel_out & 0xff, (channel_out >> 8) & 0xff);
  1199. }
  1200. void BluetoothController::sendL2CapCommand(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow, uint8_t channelHigh)
  1201. {
  1202. txbuf_[0] = handle & 0xff; // HCI handle with PB,BC flag
  1203. txbuf_[1] = (((handle >> 8) & 0x0f) | 0x20);
  1204. txbuf_[2] = (uint8_t)((4 + nbytes) & 0xff); // HCI ACL total data length
  1205. txbuf_[3] = (uint8_t)((4 + nbytes) >> 8);
  1206. txbuf_[4] = (uint8_t)(nbytes & 0xff); // L2CAP header: Length
  1207. txbuf_[5] = (uint8_t)(nbytes >> 8);
  1208. txbuf_[6] = channelLow;
  1209. txbuf_[7] = channelHigh;
  1210. if (nbytes) {
  1211. memcpy(&txbuf_[8], data, nbytes); // copy in the commands parameters.
  1212. }
  1213. nbytes = nbytes+8;
  1214. for (uint8_t i=0; i< nbytes; i++) DBGPrintf("%02x ", txbuf_[i]);
  1215. DBGPrintf(")\n");
  1216. if (!queue_Data_Transfer(txpipe_, txbuf_, nbytes, this)) {
  1217. println("sendL2CapCommand failed");
  1218. }
  1219. }
  1220. void BluetoothController::process_l2cap_connection_request(uint8_t *data) {
  1221. // ID LEN LEN PSM PSM SCID SCID
  1222. // 0x02 0x02 0x04 0x00 0x11 0x00 0x43 0x00
  1223. uint16_t psm = data[4]+((uint16_t)data[5] << 8);
  1224. uint16_t scid = data[6]+((uint16_t)data[7] << 8);
  1225. connection_rxid_ = data[1];
  1226. DBGPrintf(" L2CAP Connection Request: ID: %d, PSM: %x, SCID: %x\n",connection_rxid_, psm, scid);
  1227. // Assuming not pair mode Send response like:
  1228. // RXID Len LEN DCID DCID SCID SCID RES 0 0 0
  1229. // 0x03 0x02 0x08 0x00 0x70 0x00 0x43 0x00 0x01 0x00 0x00 0x00
  1230. if (psm == HID_CTRL_PSM) {
  1231. control_scid_ = scid;
  1232. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, control_dcid_, control_scid_, PENDING);
  1233. pending_control_tx_ = STATE_TX_SEND_CONECT_RSP_SUCCESS;
  1234. } else if (psm == HID_INTR_PSM) {
  1235. interrupt_scid_ = scid;
  1236. sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, interrupt_dcid_, interrupt_scid_, PENDING);
  1237. pending_control_tx_ = STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS;
  1238. }
  1239. }
  1240. // Process the l2cap_connection_response...
  1241. void BluetoothController::process_l2cap_connection_response(uint8_t *data) {
  1242. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1243. uint16_t dcid = data[6]+((uint16_t)data[7] << 8);
  1244. DBGPrintf(" L2CAP Connection Response: ID: %d, Dest:%x, Source:%x, Result:%x, Status: %x\n",
  1245. data[1], scid, dcid,
  1246. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1247. //48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
  1248. if (dcid == interrupt_dcid_) {
  1249. interrupt_scid_ = scid;
  1250. DBGPrintf(" Interrupt Response\n");
  1251. connection_rxid_++;
  1252. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1253. } else if (dcid == control_dcid_) {
  1254. control_scid_ = scid;
  1255. DBGPrintf(" Control Response\n");
  1256. sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
  1257. }
  1258. }
  1259. void BluetoothController::process_l2cap_config_request(uint8_t *data) {
  1260. //48 20 10 0 c 0 1 0 *4 2 8 0 70 0 0 0 1 2 30 0
  1261. uint16_t dcid = data[4]+((uint16_t)data[5] << 8);
  1262. DBGPrintf(" L2CAP config Request: ID: %d, Dest:%x, Flags:%x, Options: %x %x %x %x\n",
  1263. data[1], dcid, data[6]+((uint16_t)data[7] << 8),
  1264. data[8], data[9], data[10], data[11]);
  1265. // Now see which dest was specified
  1266. if (dcid == control_dcid_) {
  1267. DBGPrintf(" Control Configuration request\n");
  1268. sendl2cap_ConfigResponse(device_connection_handle_, data[1], control_scid_);
  1269. } else if (dcid == interrupt_dcid_) {
  1270. DBGPrintf(" Interrupt Configuration request\n");
  1271. sendl2cap_ConfigResponse(device_connection_handle_, data[1], interrupt_scid_);
  1272. }
  1273. }
  1274. void BluetoothController::process_l2cap_config_response(uint8_t *data) {
  1275. // 48 20 12 0 e 0 1 0 5 0 a 0 70 0 0 0 0 0 1 2 30 0
  1276. uint16_t scid = data[4]+((uint16_t)data[5] << 8);
  1277. DBGPrintf(" L2CAP config Response: ID: %d, Source:%x, Flags:%x, Result:%x, Config: %x\n",
  1278. data[1], scid, data[6]+((uint16_t)data[7] << 8),
  1279. data[8]+((uint16_t)data[9] << 8), data[10]+((uint16_t)data[11] << 8));
  1280. if (scid == control_dcid_) {
  1281. // Set HID Boot mode
  1282. // Don't do if PS3...
  1283. if (!(device_driver_->special_process_required & BTHIDInput::SP_PS3_IDS)) {
  1284. setHIDProtocol(HID_BOOT_PROTOCOL); //
  1285. }
  1286. //setHIDProtocol(HID_RPT_PROTOCOL); //HID_RPT_PROTOCOL
  1287. if (do_pair_device_ && !(device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_DONT_NEED_CONNECT))) {
  1288. pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
  1289. } else if (device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_NEED_CONNECT)) {
  1290. DBGPrintf(" Needs connect to device INT(PS4?)\n");
  1291. // The PS4 requires a connection request to it.
  1292. pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
  1293. } else {
  1294. pending_control_ = 0;
  1295. }
  1296. } else if (scid == interrupt_dcid_) {
  1297. // Enable SCan to page mode
  1298. connection_complete_ = true;
  1299. sendHCIWriteScanEnable(2);
  1300. }
  1301. }
  1302. void BluetoothController::process_l2cap_command_reject(uint8_t *data) {
  1303. // 48 20 b 0 7 0 70 0 *1 0 0 0 2 0 4
  1304. DBGPrintf(" L2CAP command reject: ID: %d, length:%x, Reason:%x, Data: %x %x \n",
  1305. data[1], data[2] + ((uint16_t)data[3] << 8), data[4], data[5], data[6]);
  1306. }
  1307. void BluetoothController::process_l2cap_disconnect_request(uint8_t *data) {
  1308. uint16_t dcid = data[4]+((uint16_t)data[5] << 8);
  1309. uint16_t scid = data[6]+((uint16_t)data[7] << 8);
  1310. DBGPrintf(" L2CAP disconnect request: ID: %d, Length:%x, Dest:%x, Source:%x\n",
  1311. data[1], data[2] + ((uint16_t)data[3] << 8), dcid, scid);
  1312. }
  1313. void BluetoothController::setHIDProtocol(uint8_t protocol) {
  1314. // Should verify protocol is boot or report
  1315. uint8_t l2capbuf[1];
  1316. l2capbuf[0] = 0x70 | protocol; // Set Protocol, see Bluetooth HID specs page 33
  1317. DBGPrintf("Set HID Protocol %d (", protocol);
  1318. sendL2CapCommand(device_connection_handle_, l2capbuf, sizeof(l2capbuf), control_scid_ & 0xff, control_scid_ >> 8);
  1319. }
  1320. void BluetoothController::handleHIDTHDRData(uint8_t *data) {
  1321. // Example
  1322. // T HID data
  1323. //48 20 d 0 9 0 71 0 a1 3 8a cc c5 a 23 22 79
  1324. uint16_t len = data[4] + ((uint16_t)data[5] << 8);
  1325. DBGPrintf("HID HDR Data: len: %d, Type: %d\n", len, data[9]);
  1326. // ??? How to parse??? Use HID object???
  1327. if (device_driver_) {
  1328. device_driver_->process_bluetooth_HID_data(&data[9], len-1); // We skip the first byte...
  1329. } else {
  1330. switch (data[9]) {
  1331. case 1:
  1332. DBGPrintf(" Keyboard report type\n");
  1333. break;
  1334. case 2:
  1335. DBGPrintf(" Mouse report type\n");
  1336. break;
  1337. case 3:
  1338. DBGPrintf(" Combo keyboard/pointing\n");
  1339. break;
  1340. default:
  1341. DBGPrintf(" Unknown report\n");
  1342. }
  1343. }
  1344. }