Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UsbHostMAX3421E_Hello
BTD.h
00001 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. 00002 00003 This software may be distributed and modified under the terms of the GNU 00004 General Public License version 2 (GPL2) as published by the Free Software 00005 Foundation and appearing in the file GPL2.TXT included in the packaging of 00006 this file. Please note that GPL2 Section 2[b] requires that all works based 00007 on this software must also be made publicly available under the terms of 00008 the GPL2 ("Copyleft"). 00009 00010 Contact information 00011 ------------------- 00012 00013 Kristian Lauszus, TKJ Electronics 00014 Web : http://www.tkjelectronics.com 00015 e-mail : kristianl@tkjelectronics.com 00016 */ 00017 00018 #ifndef _btd_h_ 00019 #define _btd_h_ 00020 00021 #include "Usb.h" 00022 #include "usbhid.h" 00023 #include "mbed.h" 00024 00025 //PID and VID of the Sony PS3 devices 00026 #define PS3_VID 0x054C // Sony Corporation 00027 #define PS3_PID 0x0268 // PS3 Controller DualShock 3 00028 #define PS3NAVIGATION_PID 0x042F // Navigation controller 00029 #define PS3MOVE_PID 0x03D5 // Motion controller 00030 00031 // These dongles do not present themselves correctly, so we have to check for them manually 00032 #define IOGEAR_GBU521_VID 0x0A5C 00033 #define IOGEAR_GBU521_PID 0x21E8 00034 #define BELKIN_F8T065BF_VID 0x050D 00035 #define BELKIN_F8T065BF_PID 0x065A 00036 00037 /* Bluetooth dongle data taken from descriptors */ 00038 #define BULK_MAXPKTSIZE 64 // Max size for ACL data 00039 00040 // Used in control endpoint header for HCI Commands 00041 #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE 00042 00043 /* Bluetooth HCI states for hci_task() */ 00044 #define HCI_INIT_STATE 0 00045 #define HCI_RESET_STATE 1 00046 #define HCI_CLASS_STATE 2 00047 #define HCI_BDADDR_STATE 3 00048 #define HCI_LOCAL_VERSION_STATE 4 00049 #define HCI_SET_NAME_STATE 5 00050 #define HCI_CHECK_DEVICE_SERVICE 6 00051 00052 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device 00053 #define HCI_CONNECT_DEVICE_STATE 8 00054 #define HCI_CONNECTED_DEVICE_STATE 9 00055 00056 #define HCI_SCANNING_STATE 10 00057 #define HCI_CONNECT_IN_STATE 11 00058 #define HCI_REMOTE_NAME_STATE 12 00059 #define HCI_CONNECTED_STATE 13 00060 #define HCI_DISABLE_SCAN_STATE 14 00061 #define HCI_DONE_STATE 15 00062 #define HCI_DISCONNECT_STATE 16 00063 00064 /* HCI event flags*/ 00065 #define HCI_FLAG_CMD_COMPLETE (1UL << 0) 00066 #define HCI_FLAG_CONNECT_COMPLETE (1UL << 1) 00067 #define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2) 00068 #define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3) 00069 #define HCI_FLAG_INCOMING_REQUEST (1UL << 4) 00070 #define HCI_FLAG_READ_BDADDR (1UL << 5) 00071 #define HCI_FLAG_READ_VERSION (1UL << 6) 00072 #define HCI_FLAG_DEVICE_FOUND (1UL << 7) 00073 #define HCI_FLAG_CONNECT_EVENT (1UL << 8) 00074 00075 /* Macros for HCI event flag tests */ 00076 #define hci_check_flag(flag) (hci_event_flag & (flag)) 00077 #define hci_set_flag(flag) (hci_event_flag |= (flag)) 00078 #define hci_clear_flag(flag) (hci_event_flag &= ~(flag)) 00079 00080 /* HCI Events managed */ 00081 #define EV_INQUIRY_COMPLETE 0x01 00082 #define EV_INQUIRY_RESULT 0x02 00083 #define EV_CONNECT_COMPLETE 0x03 00084 #define EV_INCOMING_CONNECT 0x04 00085 #define EV_DISCONNECT_COMPLETE 0x05 00086 #define EV_AUTHENTICATION_COMPLETE 0x06 00087 #define EV_REMOTE_NAME_COMPLETE 0x07 00088 #define EV_ENCRYPTION_CHANGE 0x08 00089 #define EV_CHANGE_CONNECTION_LINK 0x09 00090 #define EV_ROLE_CHANGED 0x12 00091 #define EV_NUM_COMPLETE_PKT 0x13 00092 #define EV_PIN_CODE_REQUEST 0x16 00093 #define EV_LINK_KEY_REQUEST 0x17 00094 #define EV_LINK_KEY_NOTIFICATION 0x18 00095 #define EV_DATA_BUFFER_OVERFLOW 0x1A 00096 #define EV_MAX_SLOTS_CHANGE 0x1B 00097 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C 00098 #define EV_QOS_SETUP_COMPLETE 0x0D 00099 #define EV_COMMAND_COMPLETE 0x0E 00100 #define EV_COMMAND_STATUS 0x0F 00101 #define EV_LOOPBACK_COMMAND 0x19 00102 #define EV_PAGE_SCAN_REP_MODE 0x20 00103 00104 /* Bluetooth states for the different Bluetooth drivers */ 00105 #define L2CAP_WAIT 0 00106 #define L2CAP_DONE 1 00107 00108 /* Used for HID Control channel */ 00109 #define L2CAP_CONTROL_CONNECT_REQUEST 2 00110 #define L2CAP_CONTROL_CONFIG_REQUEST 3 00111 #define L2CAP_CONTROL_SUCCESS 4 00112 #define L2CAP_CONTROL_DISCONNECT 5 00113 00114 /* Used for HID Interrupt channel */ 00115 #define L2CAP_INTERRUPT_SETUP 6 00116 #define L2CAP_INTERRUPT_CONNECT_REQUEST 7 00117 #define L2CAP_INTERRUPT_CONFIG_REQUEST 8 00118 #define L2CAP_INTERRUPT_DISCONNECT 9 00119 00120 /* Used for SDP channel */ 00121 #define L2CAP_SDP_WAIT 10 00122 #define L2CAP_SDP_SUCCESS 11 00123 00124 /* Used for RFCOMM channel */ 00125 #define L2CAP_RFCOMM_WAIT 12 00126 #define L2CAP_RFCOMM_SUCCESS 13 00127 00128 #define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel 00129 00130 /* Bluetooth states used by some drivers */ 00131 #define TURN_ON_LED 17 00132 #define PS3_ENABLE_SIXAXIS 18 00133 #define WII_CHECK_MOTION_PLUS_STATE 19 00134 #define WII_CHECK_EXTENSION_STATE 20 00135 #define WII_INIT_MOTION_PLUS_STATE 21 00136 00137 /* L2CAP event flags for HID Control channel */ 00138 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST (1UL << 0) 00139 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS (1UL << 1) 00140 #define L2CAP_FLAG_CONTROL_CONNECTED (1UL << 2) 00141 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE (1UL << 3) 00142 00143 /* L2CAP event flags for HID Interrupt channel */ 00144 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST (1UL << 4) 00145 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS (1UL << 5) 00146 #define L2CAP_FLAG_INTERRUPT_CONNECTED (1UL << 6) 00147 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE (1UL << 7) 00148 00149 /* L2CAP event flags for SDP channel */ 00150 #define L2CAP_FLAG_CONNECTION_SDP_REQUEST (1UL << 8) 00151 #define L2CAP_FLAG_CONFIG_SDP_SUCCESS (1UL << 9) 00152 #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST (1UL << 10) 00153 00154 /* L2CAP event flags for RFCOMM channel */ 00155 #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST (1UL << 11) 00156 #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS (1UL << 12) 00157 #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST (1UL << 13) 00158 00159 #define L2CAP_FLAG_DISCONNECT_RESPONSE (1UL << 14) 00160 00161 /* Macros for L2CAP event flag tests */ 00162 #define l2cap_check_flag(flag) (l2cap_event_flag & (flag)) 00163 #define l2cap_set_flag(flag) (l2cap_event_flag |= (flag)) 00164 #define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag)) 00165 00166 /* L2CAP signaling commands */ 00167 #define L2CAP_CMD_COMMAND_REJECT 0x01 00168 #define L2CAP_CMD_CONNECTION_REQUEST 0x02 00169 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03 00170 #define L2CAP_CMD_CONFIG_REQUEST 0x04 00171 #define L2CAP_CMD_CONFIG_RESPONSE 0x05 00172 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06 00173 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07 00174 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A 00175 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B 00176 00177 // Used For Connection Response - Remember to Include High Byte 00178 #define PENDING 0x01 00179 #define SUCCESSFUL 0x00 00180 00181 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */ 00182 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value 00183 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value 00184 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value 00185 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value 00186 00187 // Used to determine if it is a Bluetooth dongle 00188 #define WI_SUBCLASS_RF 0x01 // RF Controller 00189 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface 00190 00191 #define BTD_MAX_ENDPOINTS 4 00192 #define BTD_NUM_SERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number 00193 00194 #define PAIR 1 00195 00196 class BluetoothService; 00197 00198 /** 00199 * The Bluetooth Dongle class will take care of all the USB communication 00200 * and then pass the data to the BluetoothService classes. 00201 */ 00202 class BTD : public USBDeviceConfig, public UsbConfigXtracter { 00203 public: 00204 /** 00205 * Constructor for the BTD class. 00206 * @param p Pointer to USB class instance. 00207 */ 00208 BTD(Usb *p); 00209 00210 /** @name USBDeviceConfig implementation */ 00211 /** 00212 * Address assignment and basic initialization is done here. 00213 * @param parent Hub number. 00214 * @param port Port number on the hub. 00215 * @param lowspeed Speed of the device. 00216 * @return 0 on success. 00217 */ 00218 uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed); 00219 /** 00220 * Initialize the Bluetooth dongle. 00221 * @param parent Hub number. 00222 * @param port Port number on the hub. 00223 * @param lowspeed Speed of the device. 00224 * @return 0 on success. 00225 */ 00226 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); 00227 /** 00228 * Release the USB device. 00229 * @return 0 on success. 00230 */ 00231 uint8_t Release(); 00232 /** 00233 * Poll the USB Input endpoints and run the state machines. 00234 * @return 0 on success. 00235 */ 00236 uint8_t Poll(); 00237 00238 /** 00239 * Get the device address. 00240 * @return The device address. 00241 */ 00242 virtual uint8_t GetAddress() { 00243 return bAddress; 00244 }; 00245 00246 /** 00247 * Used to check if the dongle has been initialized. 00248 * @return True if it's ready. 00249 */ 00250 virtual bool isReady() { 00251 return bPollEnable; 00252 }; 00253 00254 /** 00255 * Used by the USB core to check what this driver support. 00256 * @param klass The device's USB class. 00257 * @return Returns true if the device's USB class matches this driver. 00258 */ 00259 virtual bool DEVCLASSOK(uint8_t klass) { 00260 return (klass == USB_CLASS_WIRELESS_CTRL); 00261 }; 00262 00263 /** 00264 * Used by the USB core to check what this driver support. 00265 * Used to set the Bluetooth address into the PS3 controllers. 00266 * @param vid The device's VID. 00267 * @param pid The device's PID. 00268 * @return Returns true if the device's VID and PID matches this driver. 00269 */ 00270 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { 00271 if((vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID) || (vid == BELKIN_F8T065BF_VID && pid == BELKIN_F8T065BF_PID)) 00272 return true; 00273 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 00274 if(vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID)) 00275 return true; 00276 } 00277 return false; 00278 }; 00279 /**@}*/ 00280 00281 /** @name UsbConfigXtracter implementation */ 00282 /** 00283 * UsbConfigXtracter implementation, used to extract endpoint information. 00284 * @param conf Configuration value. 00285 * @param iface Interface number. 00286 * @param alt Alternate setting. 00287 * @param proto Interface Protocol. 00288 * @param ep Endpoint Descriptor. 00289 */ 00290 void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); 00291 /**@}*/ 00292 00293 /** Disconnects both the L2CAP Channel and the HCI Connection for all Bluetooth services. */ 00294 void disconnect(); 00295 00296 /** 00297 * Register Bluetooth dongle members/services. 00298 * @param pService Pointer to BluetoothService class instance. 00299 * @return The service ID on success or -1 on fail. 00300 */ 00301 int8_t registerBluetoothService(BluetoothService *pService) { 00302 for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) { 00303 if(!btService[i]) { 00304 btService[i] = pService; 00305 return i; // Return ID 00306 } 00307 } 00308 return -1; // Error registering BluetoothService 00309 }; 00310 00311 /** @name HCI Commands */ 00312 /** 00313 * Used to send a HCI Command. 00314 * @param data Data to send. 00315 * @param nbytes Number of bytes to send. 00316 */ 00317 void HCI_Command(uint8_t* data, uint16_t nbytes); 00318 /** Reset the Bluetooth dongle. */ 00319 void hci_reset(); 00320 /** Read the Bluetooth address of the dongle. */ 00321 void hci_read_bdaddr(); 00322 /** Read the HCI Version of the Bluetooth dongle. */ 00323 void hci_read_local_version_information(); 00324 /** 00325 * Set the local name of the Bluetooth dongle. 00326 * @param name Desired name. 00327 */ 00328 void hci_set_local_name(const char* name); 00329 /** Enable visibility to other Bluetooth devices. */ 00330 void hci_write_scan_enable(); 00331 /** Disable visibility to other Bluetooth devices. */ 00332 void hci_write_scan_disable(); 00333 /** Read the remote devices name. */ 00334 void hci_remote_name(); 00335 /** Accept the connection with the Bluetooth device. */ 00336 void hci_accept_connection(); 00337 /** 00338 * Disconnect the HCI connection. 00339 * @param handle The HCI Handle for the connection. 00340 */ 00341 void hci_disconnect(uint16_t handle); 00342 /** 00343 * Respond with the pin for the connection. 00344 * The pin is automatically set for the Wii library, 00345 * but can be customized for the SPP library. 00346 */ 00347 void hci_pin_code_request_reply(); 00348 /** Respons when no pin was set. */ 00349 void hci_pin_code_negative_request_reply(); 00350 /** 00351 * Command is used to reply to a Link Key Request event from the BR/EDR Controller 00352 * if the Host does not have a stored Link Key for the connection. 00353 */ 00354 void hci_link_key_request_negative_reply(); 00355 /** Used to try to authenticate with the remote device. */ 00356 void hci_authentication_request(); 00357 /** Start a HCI inquiry. */ 00358 void hci_inquiry(); 00359 /** Cancel a HCI inquiry. */ 00360 void hci_inquiry_cancel(); 00361 /** Connect to last device communicated with. */ 00362 void hci_connect(); 00363 /** 00364 * Connect to device. 00365 * @param bdaddr Bluetooth address of the device. 00366 */ 00367 void hci_connect(uint8_t *bdaddr); 00368 /** Used to a set the class of the device. */ 00369 void hci_write_class_of_device(); 00370 /**@}*/ 00371 00372 /** @name L2CAP Commands */ 00373 /** 00374 * Used to send L2CAP Commands. 00375 * @param handle HCI Handle. 00376 * @param data Data to send. 00377 * @param nbytes Number of bytes to send. 00378 * @param channelLow,channelHigh Low and high byte of channel to send to. 00379 * If argument is omitted then the Standard L2CAP header: Channel ID (0x01) for ACL-U will be used. 00380 */ 00381 void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00); 00382 /** 00383 * L2CAP Connection Request. 00384 * @param handle HCI handle. 00385 * @param rxid Identifier. 00386 * @param scid Source Channel Identifier. 00387 * @param psm Protocol/Service Multiplexer - see: https://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm. 00388 */ 00389 void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm); 00390 /** 00391 * L2CAP Connection Response. 00392 * @param handle HCI handle. 00393 * @param rxid Identifier. 00394 * @param dcid Destination Channel Identifier. 00395 * @param scid Source Channel Identifier. 00396 * @param result Result - First send ::PENDING and then ::SUCCESSFUL. 00397 */ 00398 void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result); 00399 /** 00400 * L2CAP Config Request. 00401 * @param handle HCI Handle. 00402 * @param rxid Identifier. 00403 * @param dcid Destination Channel Identifier. 00404 */ 00405 void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid); 00406 /** 00407 * L2CAP Config Response. 00408 * @param handle HCI Handle. 00409 * @param rxid Identifier. 00410 * @param scid Source Channel Identifier. 00411 */ 00412 void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid); 00413 /** 00414 * L2CAP Disconnection Request. 00415 * @param handle HCI Handle. 00416 * @param rxid Identifier. 00417 * @param dcid Device Channel Identifier. 00418 * @param scid Source Channel Identifier. 00419 */ 00420 void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid); 00421 /** 00422 * L2CAP Disconnection Response. 00423 * @param handle HCI Handle. 00424 * @param rxid Identifier. 00425 * @param dcid Device Channel Identifier. 00426 * @param scid Source Channel Identifier. 00427 */ 00428 void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid); 00429 /** 00430 * L2CAP Information Response. 00431 * @param handle HCI Handle. 00432 * @param rxid Identifier. 00433 * @param infoTypeLow,infoTypeHigh Infotype. 00434 */ 00435 void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh); 00436 /**@}*/ 00437 00438 /** Use this to see if it is waiting for a incoming connection. */ 00439 bool waitingForConnection; 00440 /** This is used by the service to know when to store the device information. */ 00441 bool l2capConnectionClaimed; 00442 /** This is used by the SPP library to claim the current SDP incoming request. */ 00443 bool sdpConnectionClaimed; 00444 /** This is used by the SPP library to claim the current RFCOMM incoming request. */ 00445 bool rfcommConnectionClaimed; 00446 00447 /** The name you wish to make the dongle show up as. It is set automatically by the SPP library. */ 00448 const char* btdName; 00449 /** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP and BTHID library. */ 00450 const char* btdPin; 00451 00452 /** The bluetooth dongles Bluetooth address. */ 00453 uint8_t my_bdaddr[6]; 00454 /** HCI handle for the last connection. */ 00455 uint16_t hci_handle; 00456 /** Last incoming devices Bluetooth address. */ 00457 uint8_t disc_bdaddr[6]; 00458 /** First 30 chars of last remote name. */ 00459 char remote_name[30]; 00460 /** 00461 * The supported HCI Version read from the Bluetooth dongle. 00462 * Used by the PS3BT library to check the HCI Version of the Bluetooth dongle, 00463 * it should be at least 3 to work properly with the library. 00464 */ 00465 uint8_t hci_version; 00466 00467 /** Call this function to pair with a Wiimote */ 00468 void pairWithWiimote() { 00469 pairWithWii = true; 00470 hci_state = HCI_CHECK_DEVICE_SERVICE; 00471 }; 00472 /** Used to only send the ACL data to the Wiimote. */ 00473 bool connectToWii; 00474 /** True if a Wiimote is connecting. */ 00475 bool incomingWii; 00476 /** True when it should pair with a Wiimote. */ 00477 bool pairWithWii; 00478 /** True if it's the new Wiimote with the Motion Plus Inside or a Wii U Pro Controller. */ 00479 bool motionPlusInside; 00480 /** True if it's a Wii U Pro Controller. */ 00481 bool wiiUProController; 00482 00483 /** Call this function to pair with a HID device */ 00484 void pairWithHID() { 00485 waitingForConnection = false; 00486 pairWithHIDDevice = true; 00487 hci_state = HCI_CHECK_DEVICE_SERVICE; 00488 }; 00489 /** Used to only send the ACL data to the HID device. */ 00490 bool connectToHIDDevice; 00491 /** True if a HID device is connecting. */ 00492 bool incomingHIDDevice; 00493 /** True when it should pair with a device like a mouse or keyboard. */ 00494 bool pairWithHIDDevice; 00495 00496 /** 00497 * Read the poll interval taken from the endpoint descriptors. 00498 * @return The poll interval in ms. 00499 */ 00500 uint8_t readPollInterval() { 00501 return pollInterval; 00502 }; 00503 00504 protected: 00505 /** Pointer to USB class instance. */ 00506 Usb *pUsb; 00507 /** Device address. */ 00508 uint8_t bAddress; 00509 /** Endpoint info structure. */ 00510 EpInfo epInfo[BTD_MAX_ENDPOINTS]; 00511 00512 /** Configuration number. */ 00513 uint8_t bConfNum; 00514 /** Total number of endpoints in the configuration. */ 00515 uint8_t bNumEP; 00516 /** Next poll time based on poll interval taken from the USB descriptor. */ 00517 uint32_t qNextPollTime; 00518 00519 /** Bluetooth dongle control endpoint. */ 00520 static const uint8_t BTD_CONTROL_PIPE; 00521 /** HCI event endpoint index. */ 00522 static const uint8_t BTD_EVENT_PIPE; 00523 /** ACL In endpoint index. */ 00524 static const uint8_t BTD_DATAIN_PIPE; 00525 /** ACL Out endpoint index. */ 00526 static const uint8_t BTD_DATAOUT_PIPE; 00527 00528 /** 00529 * Used to print the USB Endpoint Descriptor. 00530 * @param ep_ptr Pointer to USB Endpoint Descriptor. 00531 */ 00532 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); 00533 00534 private: 00535 void Initialize(); // Set all variables, endpoint structs etc. to default values 00536 BluetoothService *btService[BTD_NUM_SERVICES]; 00537 00538 uint16_t PID, VID; // PID and VID of device connected 00539 00540 uint8_t pollInterval; 00541 bool bPollEnable; 00542 00543 bool pairWiiUsingSync; // True if pairing was done using the Wii SYNC button. 00544 bool checkRemoteName; // Used to check remote device's name before connecting. 00545 bool incomingPS4; // True if a PS4 controller is connecting 00546 uint8_t classOfDevice[3]; // Class of device of last device 00547 00548 /* Variables used by high level HCI task */ 00549 uint8_t hci_state; // Current state of Bluetooth HCI connection 00550 uint16_t hci_counter; // Counter used for Bluetooth HCI reset loops 00551 uint16_t hci_num_reset_loops; // This value indicate how many times it should read before trying to reset 00552 uint16_t hci_event_flag; // HCI flags of received Bluetooth events 00553 uint8_t inquiry_counter; 00554 00555 uint8_t hcibuf[BULK_MAXPKTSIZE]; // General purpose buffer for HCI data 00556 uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data 00557 uint8_t l2capoutbuf[14]; // General purpose buffer for L2CAP out data 00558 00559 /* State machines */ 00560 void HCI_event_task(); // Poll the HCI event pipe 00561 void HCI_task(); // HCI state machine 00562 void ACL_event_task(); // ACL input pipe 00563 00564 /* Used to set the Bluetooth Address internally to the PS3 Controllers */ 00565 void setBdaddr(uint8_t* BDADDR); 00566 void setMoveBdaddr(uint8_t* BDADDR); 00567 }; 00568 00569 /** All Bluetooth services should inherit this class. */ 00570 class BluetoothService { 00571 public: 00572 BluetoothService(BTD *p) : pBtd(p) { 00573 if(pBtd) 00574 pBtd->registerBluetoothService(this); // Register it as a Bluetooth service 00575 }; 00576 /** 00577 * Used to pass acldata to the Bluetooth service. 00578 * @param ACLData Pointer to the incoming acldata. 00579 */ 00580 virtual void ACLData(uint8_t* ACLData) = 0; 00581 /** Used to run the different state machines in the Bluetooth service. */ 00582 virtual void Run() = 0; 00583 /** Used to reset the Bluetooth service. */ 00584 virtual void Reset() = 0; 00585 /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */ 00586 virtual void disconnect() = 0; 00587 00588 /** 00589 * Used to call your own function when the device is successfully initialized. 00590 * @param funcOnInit Function to call. 00591 */ 00592 void attachOnInit(void (*funcOnInit)(void)) { 00593 pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times 00594 }; 00595 00596 protected: 00597 /** 00598 * Called when a device is successfully initialized. 00599 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. 00600 * This is useful for instance if you want to set the LEDs in a specific way. 00601 */ 00602 virtual void onInit() = 0; 00603 00604 /** Used to check if the incoming L2CAP data matches the HCI Handle */ 00605 bool checkHciHandle(uint8_t *buf, uint16_t handle) { 00606 return (buf[0] == (handle & 0xFF)) && (buf[1] == ((handle >> 8) | 0x20)); 00607 } 00608 00609 /** Pointer to function called in onInit(). */ 00610 void (*pFuncOnInit)(void); 00611 00612 /** Pointer to BTD instance. */ 00613 BTD *pBtd; 00614 00615 /** The HCI Handle for the connection. */ 00616 uint16_t hci_handle; 00617 00618 /** L2CAP flags of received Bluetooth events. */ 00619 uint32_t l2cap_event_flag; 00620 00621 /** Identifier for L2CAP commands. */ 00622 uint8_t identifier; 00623 }; 00624 00625 #endif
Generated on Tue Jul 12 2022 18:12:04 by
1.7.2