You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. e-mail : [email protected]
  13. */
  14. #ifndef _btd_h_
  15. #define _btd_h_
  16. #include "Usb.h"
  17. #include "hid.h"
  18. //PID and VID of the Sony PS3 devices
  19. #define PS3_VID 0x054C // Sony Corporation
  20. #define PS3_PID 0x0268 // PS3 Controller DualShock 3
  21. #define PS3NAVIGATION_PID 0x042F // Navigation controller
  22. #define PS3MOVE_PID 0x03D5 // Motion controller
  23. #define IOGEAR_GBU521_VID 0x0A5C // The IOGEAR GBU521 dongle does not presents itself correctly, so we have to check for it manually
  24. #define IOGEAR_GBU521_PID 0x21E8
  25. /* Bluetooth dongle data taken from descriptors */
  26. #define BULK_MAXPKTSIZE 64 // Max size for ACL data
  27. // Used in control endpoint header for HCI Commands
  28. #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  29. /* Bluetooth HCI states for hci_task() */
  30. #define HCI_INIT_STATE 0
  31. #define HCI_RESET_STATE 1
  32. #define HCI_CLASS_STATE 2
  33. #define HCI_BDADDR_STATE 3
  34. #define HCI_LOCAL_VERSION_STATE 4
  35. #define HCI_SET_NAME_STATE 5
  36. #define HCI_CHECK_DEVICE_SERVICE 6
  37. #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device
  38. #define HCI_CONNECT_DEVICE_STATE 8
  39. #define HCI_CONNECTED_DEVICE_STATE 9
  40. #define HCI_SCANNING_STATE 10
  41. #define HCI_CONNECT_IN_STATE 11
  42. #define HCI_REMOTE_NAME_STATE 12
  43. #define HCI_CONNECTED_STATE 13
  44. #define HCI_DISABLE_SCAN_STATE 14
  45. #define HCI_DONE_STATE 15
  46. #define HCI_DISCONNECT_STATE 16
  47. /* HCI event flags*/
  48. #define HCI_FLAG_CMD_COMPLETE (1UL << 0)
  49. #define HCI_FLAG_CONNECT_COMPLETE (1UL << 1)
  50. #define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2)
  51. #define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3)
  52. #define HCI_FLAG_INCOMING_REQUEST (1UL << 4)
  53. #define HCI_FLAG_READ_BDADDR (1UL << 5)
  54. #define HCI_FLAG_READ_VERSION (1UL << 6)
  55. #define HCI_FLAG_DEVICE_FOUND (1UL << 7)
  56. #define HCI_FLAG_CONNECT_EVENT (1UL << 8)
  57. /* Macros for HCI event flag tests */
  58. #define hci_check_flag(flag) (hci_event_flag & (flag))
  59. #define hci_set_flag(flag) (hci_event_flag |= (flag))
  60. #define hci_clear_flag(flag) (hci_event_flag &= ~(flag))
  61. /* HCI Events managed */
  62. #define EV_INQUIRY_COMPLETE 0x01
  63. #define EV_INQUIRY_RESULT 0x02
  64. #define EV_CONNECT_COMPLETE 0x03
  65. #define EV_INCOMING_CONNECT 0x04
  66. #define EV_DISCONNECT_COMPLETE 0x05
  67. #define EV_AUTHENTICATION_COMPLETE 0x06
  68. #define EV_REMOTE_NAME_COMPLETE 0x07
  69. #define EV_ENCRYPTION_CHANGE 0x08
  70. #define EV_CHANGE_CONNECTION_LINK 0x09
  71. #define EV_ROLE_CHANGED 0x12
  72. #define EV_NUM_COMPLETE_PKT 0x13
  73. #define EV_PIN_CODE_REQUEST 0x16
  74. #define EV_LINK_KEY_REQUEST 0x17
  75. #define EV_LINK_KEY_NOTIFICATION 0x18
  76. #define EV_DATA_BUFFER_OVERFLOW 0x1A
  77. #define EV_MAX_SLOTS_CHANGE 0x1B
  78. #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
  79. #define EV_QOS_SETUP_COMPLETE 0x0D
  80. #define EV_COMMAND_COMPLETE 0x0E
  81. #define EV_COMMAND_STATUS 0x0F
  82. #define EV_LOOPBACK_COMMAND 0x19
  83. #define EV_PAGE_SCAN_REP_MODE 0x20
  84. /* Bluetooth states for the different Bluetooth drivers */
  85. #define L2CAP_WAIT 0
  86. #define L2CAP_DONE 1
  87. /* Used for HID Control channel */
  88. #define L2CAP_CONTROL_CONNECT_REQUEST 2
  89. #define L2CAP_CONTROL_CONFIG_REQUEST 3
  90. #define L2CAP_CONTROL_SUCCESS 4
  91. #define L2CAP_CONTROL_DISCONNECT 5
  92. /* Used for HID Interrupt channel */
  93. #define L2CAP_INTERRUPT_SETUP 6
  94. #define L2CAP_INTERRUPT_CONNECT_REQUEST 7
  95. #define L2CAP_INTERRUPT_CONFIG_REQUEST 8
  96. #define L2CAP_INTERRUPT_DISCONNECT 9
  97. /* Used for SDP channel */
  98. #define L2CAP_SDP_WAIT 10
  99. #define L2CAP_SDP_SUCCESS 11
  100. /* Used for RFCOMM channel */
  101. #define L2CAP_RFCOMM_WAIT 12
  102. #define L2CAP_RFCOMM_SUCCESS 13
  103. #define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel
  104. /* Bluetooth states used by some drivers */
  105. #define TURN_ON_LED 17
  106. #define PS3_ENABLE_SIXAXIS 18
  107. #define WII_CHECK_MOTION_PLUS_STATE 19
  108. #define WII_CHECK_EXTENSION_STATE 20
  109. #define WII_INIT_MOTION_PLUS_STATE 21
  110. /* L2CAP event flags for HID Control channel */
  111. #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST (1UL << 0)
  112. #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS (1UL << 1)
  113. #define L2CAP_FLAG_CONTROL_CONNECTED (1UL << 2)
  114. #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE (1UL << 3)
  115. /* L2CAP event flags for HID Interrupt channel */
  116. #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST (1UL << 4)
  117. #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS (1UL << 5)
  118. #define L2CAP_FLAG_INTERRUPT_CONNECTED (1UL << 6)
  119. #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE (1UL << 7)
  120. /* L2CAP event flags for SDP channel */
  121. #define L2CAP_FLAG_CONNECTION_SDP_REQUEST (1UL << 8)
  122. #define L2CAP_FLAG_CONFIG_SDP_SUCCESS (1UL << 9)
  123. #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST (1UL << 10)
  124. /* L2CAP event flags for RFCOMM channel */
  125. #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST (1UL << 11)
  126. #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS (1UL << 12)
  127. #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST (1UL << 13)
  128. #define L2CAP_FLAG_DISCONNECT_RESPONSE (1UL << 14)
  129. /* Macros for L2CAP event flag tests */
  130. #define l2cap_check_flag(flag) (l2cap_event_flag & (flag))
  131. #define l2cap_set_flag(flag) (l2cap_event_flag |= (flag))
  132. #define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag))
  133. /* L2CAP signaling commands */
  134. #define L2CAP_CMD_COMMAND_REJECT 0x01
  135. #define L2CAP_CMD_CONNECTION_REQUEST 0x02
  136. #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
  137. #define L2CAP_CMD_CONFIG_REQUEST 0x04
  138. #define L2CAP_CMD_CONFIG_RESPONSE 0x05
  139. #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
  140. #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
  141. #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
  142. #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
  143. // Used For Connection Response - Remember to Include High Byte
  144. #define PENDING 0x01
  145. #define SUCCESSFUL 0x00
  146. /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
  147. #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
  148. #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
  149. #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
  150. #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
  151. // Used to determine if it is a Bluetooth dongle
  152. #define WI_SUBCLASS_RF 0x01 // RF Controller
  153. #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
  154. #define BTD_MAX_ENDPOINTS 4
  155. #define BTD_NUM_SERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number
  156. #define PAIR 1
  157. class BluetoothService;
  158. /**
  159. * The Bluetooth Dongle class will take care of all the USB communication
  160. * and then pass the data to the BluetoothService classes.
  161. */
  162. class BTD : public USBDeviceConfig, public UsbConfigXtracter {
  163. public:
  164. /**
  165. * Constructor for the BTD class.
  166. * @param p Pointer to USB class instance.
  167. */
  168. BTD(USB *p);
  169. /** @name USBDeviceConfig implementation */
  170. /**
  171. * Address assignment and basic initialization is done here.
  172. * @param parent Hub number.
  173. * @param port Port number on the hub.
  174. * @param lowspeed Speed of the device.
  175. * @return 0 on success.
  176. */
  177. uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
  178. /**
  179. * Initialize the Bluetooth dongle.
  180. * @param parent Hub number.
  181. * @param port Port number on the hub.
  182. * @param lowspeed Speed of the device.
  183. * @return 0 on success.
  184. */
  185. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  186. /**
  187. * Release the USB device.
  188. * @return 0 on success.
  189. */
  190. uint8_t Release();
  191. /**
  192. * Poll the USB Input endpoints and run the state machines.
  193. * @return 0 on success.
  194. */
  195. uint8_t Poll();
  196. /**
  197. * Get the device address.
  198. * @return The device address.
  199. */
  200. virtual uint8_t GetAddress() {
  201. return bAddress;
  202. };
  203. /**
  204. * Used to check if the dongle has been initialized.
  205. * @return True if it's ready.
  206. */
  207. virtual bool isReady() {
  208. return bPollEnable;
  209. };
  210. /**
  211. * Used by the USB core to check what this driver support.
  212. * @param klass The device's USB class.
  213. * @return Returns true if the device's USB class matches this driver.
  214. */
  215. virtual bool DEVCLASSOK(uint8_t klass) {
  216. return (klass == USB_CLASS_WIRELESS_CTRL);
  217. };
  218. /**
  219. * Used by the USB core to check what this driver support.
  220. * Used to set the Bluetooth address into the PS3 controllers.
  221. * @param vid The device's VID.
  222. * @param pid The device's PID.
  223. * @return Returns true if the device's VID and PID matches this driver.
  224. */
  225. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  226. if(vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID)
  227. return true;
  228. if(my_bdaddr[0] != 0x00 || my_bdaddr[1] != 0x00 || my_bdaddr[2] != 0x00 || my_bdaddr[3] != 0x00 || my_bdaddr[4] != 0x00 || my_bdaddr[5] != 0x00) { // Check if Bluetooth address is set
  229. if(vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID))
  230. return true;
  231. }
  232. return false;
  233. };
  234. /**@}*/
  235. /** @name UsbConfigXtracter implementation */
  236. /**
  237. * UsbConfigXtracter implementation, used to extract endpoint information.
  238. * @param conf Configuration value.
  239. * @param iface Interface number.
  240. * @param alt Alternate setting.
  241. * @param proto Interface Protocol.
  242. * @param ep Endpoint Descriptor.
  243. */
  244. void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
  245. /**@}*/
  246. /** Disconnects both the L2CAP Channel and the HCI Connection for all Bluetooth services. */
  247. void disconnect();
  248. /**
  249. * Register Bluetooth dongle members/services.
  250. * @param pService Pointer to BluetoothService class instance.
  251. * @return The service ID on success or -1 on fail.
  252. */
  253. int8_t registerBluetoothService(BluetoothService *pService) {
  254. for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) {
  255. if(!btService[i]) {
  256. btService[i] = pService;
  257. return i; // Return ID
  258. }
  259. }
  260. return -1; // Error registering BluetoothService
  261. };
  262. /** @name HCI Commands */
  263. /**
  264. * Used to send a HCI Command.
  265. * @param data Data to send.
  266. * @param nbytes Number of bytes to send.
  267. */
  268. void HCI_Command(uint8_t* data, uint16_t nbytes);
  269. /** Reset the Bluetooth dongle. */
  270. void hci_reset();
  271. /** Read the Bluetooth address of the dongle. */
  272. void hci_read_bdaddr();
  273. /** Read the HCI Version of the Bluetooth dongle. */
  274. void hci_read_local_version_information();
  275. /**
  276. * Set the local name of the Bluetooth dongle.
  277. * @param name Desired name.
  278. */
  279. void hci_set_local_name(const char* name);
  280. /** Enable visibility to other Bluetooth devices. */
  281. void hci_write_scan_enable();
  282. /** Disable visibility to other Bluetooth devices. */
  283. void hci_write_scan_disable();
  284. /** Read the remote devices name. */
  285. void hci_remote_name();
  286. /** Accept the connection with the Bluetooth device. */
  287. void hci_accept_connection();
  288. /**
  289. * Disconnect the HCI connection.
  290. * @param handle The HCI Handle for the connection.
  291. */
  292. void hci_disconnect(uint16_t handle);
  293. /**
  294. * Respond with the pin for the connection.
  295. * The pin is automatically set for the Wii library,
  296. * but can be customized for the SPP library.
  297. */
  298. void hci_pin_code_request_reply();
  299. /** Respons when no pin was set. */
  300. void hci_pin_code_negative_request_reply();
  301. /**
  302. * Command is used to reply to a Link Key Request event from the BR/EDR Controller
  303. * if the Host does not have a stored Link Key for the connection.
  304. */
  305. void hci_link_key_request_negative_reply();
  306. /** Used to try to authenticate with the remote device. */
  307. void hci_authentication_request();
  308. /** Start a HCI inquiry. */
  309. void hci_inquiry();
  310. /** Cancel a HCI inquiry. */
  311. void hci_inquiry_cancel();
  312. /** Connect to last device communicated with. */
  313. void hci_connect();
  314. /**
  315. * Connect to device.
  316. * @param bdaddr Bluetooth address of the device.
  317. */
  318. void hci_connect(uint8_t *bdaddr);
  319. /** Used to a set the class of the device. */
  320. void hci_write_class_of_device();
  321. /**@}*/
  322. /** @name L2CAP Commands */
  323. /**
  324. * Used to send L2CAP Commands.
  325. * @param handle HCI Handle.
  326. * @param data Data to send.
  327. * @param nbytes Number of bytes to send.
  328. * @param channelLow,channelHigh Low and high byte of channel to send to.
  329. * If argument is omitted then the Standard L2CAP header: Channel ID (0x01) for ACL-U will be used.
  330. */
  331. void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
  332. /**
  333. * L2CAP Connection Request.
  334. * @param handle HCI handle.
  335. * @param rxid Identifier.
  336. * @param scid Source Channel Identifier.
  337. * @param psm Protocol/Service Multiplexer - see: https://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm.
  338. */
  339. void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
  340. /**
  341. * L2CAP Connection Response.
  342. * @param handle HCI handle.
  343. * @param rxid Identifier.
  344. * @param dcid Destination Channel Identifier.
  345. * @param scid Source Channel Identifier.
  346. * @param result Result - First send ::PENDING and then ::SUCCESSFUL.
  347. */
  348. void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
  349. /**
  350. * L2CAP Config Request.
  351. * @param handle HCI Handle.
  352. * @param rxid Identifier.
  353. * @param dcid Destination Channel Identifier.
  354. */
  355. void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
  356. /**
  357. * L2CAP Config Response.
  358. * @param handle HCI Handle.
  359. * @param rxid Identifier.
  360. * @param scid Source Channel Identifier.
  361. */
  362. void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
  363. /**
  364. * L2CAP Disconnection Request.
  365. * @param handle HCI Handle.
  366. * @param rxid Identifier.
  367. * @param dcid Device Channel Identifier.
  368. * @param scid Source Channel Identifier.
  369. */
  370. void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
  371. /**
  372. * L2CAP Disconnection Response.
  373. * @param handle HCI Handle.
  374. * @param rxid Identifier.
  375. * @param dcid Device Channel Identifier.
  376. * @param scid Source Channel Identifier.
  377. */
  378. void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
  379. /**
  380. * L2CAP Information Response.
  381. * @param handle HCI Handle.
  382. * @param rxid Identifier.
  383. * @param infoTypeLow,infoTypeHigh Infotype.
  384. */
  385. void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
  386. /**@}*/
  387. /** Use this to see if it is waiting for a incoming connection. */
  388. bool watingForConnection;
  389. /** This is used by the service to know when to store the device information. */
  390. bool l2capConnectionClaimed;
  391. /** This is used by the SPP library to claim the current SDP incoming request. */
  392. bool sdpConnectionClaimed;
  393. /** This is used by the SPP library to claim the current RFCOMM incoming request. */
  394. bool rfcommConnectionClaimed;
  395. /** The name you wish to make the dongle show up as. It is set automatically by the SPP library. */
  396. const char* btdName;
  397. /** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP and BTHID library. */
  398. const char* btdPin;
  399. /** The bluetooth dongles Bluetooth address. */
  400. uint8_t my_bdaddr[6];
  401. /** HCI handle for the last connection. */
  402. uint16_t hci_handle;
  403. /** Last incoming devices Bluetooth address. */
  404. uint8_t disc_bdaddr[6];
  405. /** First 30 chars of last remote name. */
  406. char remote_name[30];
  407. /**
  408. * The supported HCI Version read from the Bluetooth dongle.
  409. * Used by the PS3BT library to check the HCI Version of the Bluetooth dongle,
  410. * it should be at least 3 to work properly with the library.
  411. */
  412. uint8_t hci_version;
  413. /** Call this function to pair with a Wiimote */
  414. void pairWithWiimote() {
  415. pairWithWii = true;
  416. hci_state = HCI_CHECK_DEVICE_SERVICE;
  417. };
  418. /** Used to only send the ACL data to the Wiimote. */
  419. bool connectToWii;
  420. /** True if a Wiimote is connecting. */
  421. bool incomingWii;
  422. /** True when it should pair with a Wiimote. */
  423. bool pairWithWii;
  424. /** True if it's the new Wiimote with the Motion Plus Inside or a Wii U Pro Controller. */
  425. bool motionPlusInside;
  426. /** True if it's a Wii U Pro Controller. */
  427. bool wiiUProController;
  428. /** Call this function to pair with a Wiimote */
  429. void pairWithHID() {
  430. pairWithHIDDevice = true;
  431. hci_state = HCI_CHECK_DEVICE_SERVICE;
  432. };
  433. /** Used to only send the ACL data to the Wiimote. */
  434. bool connectToHIDDevice;
  435. /** True if a Wiimote is connecting. */
  436. bool incomingHIDDevice;
  437. /** True when it should pair with a device like a mouse or keyboard. */
  438. bool pairWithHIDDevice;
  439. /**
  440. * Read the poll interval taken from the endpoint descriptors.
  441. * @return The poll interval in ms.
  442. */
  443. uint8_t readPollInterval() {
  444. return pollInterval;
  445. };
  446. protected:
  447. /** Pointer to USB class instance. */
  448. USB *pUsb;
  449. /** Device address. */
  450. uint8_t bAddress;
  451. /** Endpoint info structure. */
  452. EpInfo epInfo[BTD_MAX_ENDPOINTS];
  453. /** Configuration number. */
  454. uint8_t bConfNum;
  455. /** Total number of endpoints in the configuration. */
  456. uint8_t bNumEP;
  457. /** Next poll time based on poll interval taken from the USB descriptor. */
  458. uint32_t qNextPollTime;
  459. /** Bluetooth dongle control endpoint. */
  460. static const uint8_t BTD_CONTROL_PIPE;
  461. /** HCI event endpoint index. */
  462. static const uint8_t BTD_EVENT_PIPE;
  463. /** ACL In endpoint index. */
  464. static const uint8_t BTD_DATAIN_PIPE;
  465. /** ACL Out endpoint index. */
  466. static const uint8_t BTD_DATAOUT_PIPE;
  467. /**
  468. * Used to print the USB Endpoint Descriptor.
  469. * @param ep_ptr Pointer to USB Endpoint Descriptor.
  470. */
  471. void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
  472. private:
  473. void Initialize(); // Set all variables, endpoint structs etc. to default values
  474. BluetoothService *btService[BTD_NUM_SERVICES];
  475. uint16_t PID, VID; // PID and VID of device connected
  476. uint8_t pollInterval;
  477. bool bPollEnable;
  478. bool pairWiiUsingSync; // True if paring was done using the Wii SYNC button.
  479. bool checkRemoteName; // Used to check remote device's name before connecting.
  480. bool incomingPS4; // True if a PS4 controller is connecting
  481. uint8_t classOfDevice[3]; // Class of device of last device
  482. /* Variables used by high level HCI task */
  483. uint8_t hci_state; // Current state of Bluetooth HCI connection
  484. uint16_t hci_counter; // Counter used for Bluetooth HCI reset loops
  485. uint16_t hci_num_reset_loops; // This value indicate how many times it should read before trying to reset
  486. uint16_t hci_event_flag; // HCI flags of received Bluetooth events
  487. uint8_t inquiry_counter;
  488. uint8_t hcibuf[BULK_MAXPKTSIZE]; // General purpose buffer for HCI data
  489. uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
  490. uint8_t l2capoutbuf[14]; // General purpose buffer for L2CAP out data
  491. /* State machines */
  492. void HCI_event_task(); // Poll the HCI event pipe
  493. void HCI_task(); // HCI state machine
  494. void ACL_event_task(); // ACL input pipe
  495. /* Used to set the Bluetooth Address internally to the PS3 Controllers */
  496. void setBdaddr(uint8_t* BDADDR);
  497. void setMoveBdaddr(uint8_t* BDADDR);
  498. };
  499. /** All Bluetooth services should inherit this class. */
  500. class BluetoothService {
  501. public:
  502. BluetoothService(BTD *p) : pBtd(p) {
  503. if(pBtd)
  504. pBtd->registerBluetoothService(this); // Register it as a Bluetooth service
  505. };
  506. /**
  507. * Used to pass acldata to the Bluetooth service.
  508. * @param ACLData Pointer to the incoming acldata.
  509. */
  510. virtual void ACLData(uint8_t* ACLData) = 0;
  511. /** Used to run the different state machines in the Bluetooth service. */
  512. virtual void Run() = 0;
  513. /** Used to reset the Bluetooth service. */
  514. virtual void Reset() = 0;
  515. /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */
  516. virtual void disconnect() = 0;
  517. /**
  518. * Used to call your own function when the device is successfully initialized.
  519. * @param funcOnInit Function to call.
  520. */
  521. void attachOnInit(void (*funcOnInit)(void)) {
  522. pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times
  523. };
  524. protected:
  525. /**
  526. * Called when a device is successfully initialized.
  527. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  528. * This is useful for instance if you want to set the LEDs in a specific way.
  529. */
  530. virtual void onInit() = 0;
  531. /** Used to check if the incoming L2CAP data matches the HCI Handle */
  532. bool checkHciHandle(uint8_t *buf, uint16_t handle) {
  533. return (buf[0] == (handle & 0xFF)) && (buf[1] == ((handle >> 8) | 0x20));
  534. }
  535. /** Pointer to function called in onInit(). */
  536. void (*pFuncOnInit)(void);
  537. /** Pointer to BTD instance. */
  538. BTD *pBtd;
  539. /** The HCI Handle for the connection. */
  540. uint16_t hci_handle;
  541. /** L2CAP flags of received Bluetooth events. */
  542. uint32_t l2cap_event_flag;
  543. /** Identifier for L2CAP commands. */
  544. uint8_t identifier;
  545. };
  546. #endif