Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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