Takumi Odashima / ArduinoUsbHostShield

Dependents:   USBHOST_PS5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BTD.h Source File

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