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

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