Library to use Arduino USB host shield on mbed
ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。
Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です
シールドについて
3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください
接続例
使い方
Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。
仕様
- Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。
main.cpp
#include "mbed.h" #include <PS3BT.h> #include <usbhub.h> Serial pc(USBTX, USBRX, 115200); //Nucleo f303k8用 USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr BTD Btd(&Usb); PS3BT PS3(&Btd); int main() { bool printAngle = false; if (Usb.Init() == -1) { pc.printf("\r\nOSC did not start"); while (1); // Halt } pc.printf("\r\nPS3 USB Library Started"); while (1) { Usb.Task(); if (PS3.PS3Connected || PS3.PS3NavigationConnected) { if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) { pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX)); pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY)); if (PS3.PS3Connected) { // The Navigation controller only have one joystick pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX)); pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY)); } } // Analog button values can be read from almost all buttons if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) { pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2)); if (!PS3.PS3NavigationConnected) { pc.printf("\tR2: %d", PS3.getAnalogButton(R2)); } } if (PS3.getButtonClick(PS)) { PS3.disconnect(); pc.printf("\r\nPS"); } if (PS3.getButtonClick(TRIANGLE)) pc.printf("\r\nTriangle"); if (PS3.getButtonClick(CIRCLE)) pc.printf("\r\nCircle"); if (PS3.getButtonClick(CROSS)) pc.printf("\r\nCross"); if (PS3.getButtonClick(SQUARE)) pc.printf("\r\nSquare"); if (PS3.getButtonClick(UP)) { pc.printf("\r\nUp"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED4); } if (PS3.getButtonClick(RIGHT)) { pc.printf("\r\nRight"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED1); } if (PS3.getButtonClick(DOWN)) { pc.printf("\r\nDown"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED2); } if (PS3.getButtonClick(LEFT)) { pc.printf("\r\nLeft"); PS3.setLedOff(); PS3.setLedOn(CONTROLLER_LED3); } if (PS3.getButtonClick(L1)) pc.printf("\r\nL1"); if (PS3.getButtonClick(L3)) pc.printf("\r\nL3"); if (PS3.getButtonClick(R1)) pc.printf("\r\nR1"); if (PS3.getButtonClick(R3)) pc.printf("\r\nR3"); if (PS3.getButtonClick(SELECT)) { pc.printf("\r\nSelect - "); PS3.printStatusString(); } if (PS3.getButtonClick(START)) { pc.printf("\r\nStart"); printAngle = !printAngle; } if (printAngle) { pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch)); pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll)); } } else { pc.printf("not connect\n"); } } }
USB_Host/BTD.h@1:da31140f2a1c, 2020-05-02 (annotated)
- Committer:
- robo_ichinoseki_a
- Date:
- Sat May 02 05:56:48 2020 +0000
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. |
kotakku | 0:b1ce54272580 | 2 | |
kotakku | 0:b1ce54272580 | 3 | This software may be distributed and modified under the terms of the GNU |
kotakku | 0:b1ce54272580 | 4 | General Public License version 2 (GPL2) as published by the Free Software |
kotakku | 0:b1ce54272580 | 5 | Foundation and appearing in the file GPL2.TXT included in the packaging of |
kotakku | 0:b1ce54272580 | 6 | this file. Please note that GPL2 Section 2[b] requires that all works based |
kotakku | 0:b1ce54272580 | 7 | on this software must also be made publicly available under the terms of |
kotakku | 0:b1ce54272580 | 8 | the GPL2 ("Copyleft"). |
kotakku | 0:b1ce54272580 | 9 | |
kotakku | 0:b1ce54272580 | 10 | Contact information |
kotakku | 0:b1ce54272580 | 11 | ------------------- |
kotakku | 0:b1ce54272580 | 12 | |
kotakku | 0:b1ce54272580 | 13 | Kristian Lauszus, TKJ Electronics |
kotakku | 0:b1ce54272580 | 14 | Web : http://www.tkjelectronics.com |
kotakku | 0:b1ce54272580 | 15 | e-mail : kristianl@tkjelectronics.com |
kotakku | 0:b1ce54272580 | 16 | */ |
kotakku | 0:b1ce54272580 | 17 | |
kotakku | 0:b1ce54272580 | 18 | #ifndef _btd_h_ |
kotakku | 0:b1ce54272580 | 19 | #define _btd_h_ |
kotakku | 0:b1ce54272580 | 20 | |
kotakku | 0:b1ce54272580 | 21 | #include "Usb.h" |
kotakku | 0:b1ce54272580 | 22 | #include "usbhid.h" |
kotakku | 0:b1ce54272580 | 23 | |
kotakku | 0:b1ce54272580 | 24 | //PID and VID of the Sony PS3 devices |
kotakku | 0:b1ce54272580 | 25 | #define PS3_VID 0x054C // Sony Corporation |
kotakku | 0:b1ce54272580 | 26 | #define PS3_PID 0x0268 // PS3 Controller DualShock 3 |
kotakku | 0:b1ce54272580 | 27 | #define PS3NAVIGATION_PID 0x042F // Navigation controller |
kotakku | 0:b1ce54272580 | 28 | #define PS3MOVE_PID 0x03D5 // Motion controller |
kotakku | 0:b1ce54272580 | 29 | |
kotakku | 0:b1ce54272580 | 30 | // These dongles do not present themselves correctly, so we have to check for them manually |
kotakku | 0:b1ce54272580 | 31 | #define IOGEAR_GBU521_VID 0x0A5C |
kotakku | 0:b1ce54272580 | 32 | #define IOGEAR_GBU521_PID 0x21E8 |
kotakku | 0:b1ce54272580 | 33 | #define BELKIN_F8T065BF_VID 0x050D |
kotakku | 0:b1ce54272580 | 34 | #define BELKIN_F8T065BF_PID 0x065A |
kotakku | 0:b1ce54272580 | 35 | |
kotakku | 0:b1ce54272580 | 36 | /* Bluetooth dongle data taken from descriptors */ |
kotakku | 0:b1ce54272580 | 37 | #define BULK_MAXPKTSIZE 64 // Max size for ACL data |
kotakku | 0:b1ce54272580 | 38 | |
kotakku | 0:b1ce54272580 | 39 | // Used in control endpoint header for HCI Commands |
kotakku | 0:b1ce54272580 | 40 | #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE |
kotakku | 0:b1ce54272580 | 41 | |
kotakku | 0:b1ce54272580 | 42 | /* Bluetooth HCI states for hci_task() */ |
kotakku | 0:b1ce54272580 | 43 | #define HCI_INIT_STATE 0 |
kotakku | 0:b1ce54272580 | 44 | #define HCI_RESET_STATE 1 |
kotakku | 0:b1ce54272580 | 45 | #define HCI_CLASS_STATE 2 |
kotakku | 0:b1ce54272580 | 46 | #define HCI_BDADDR_STATE 3 |
kotakku | 0:b1ce54272580 | 47 | #define HCI_LOCAL_VERSION_STATE 4 |
kotakku | 0:b1ce54272580 | 48 | #define HCI_SET_NAME_STATE 5 |
kotakku | 0:b1ce54272580 | 49 | #define HCI_CHECK_DEVICE_SERVICE 6 |
kotakku | 0:b1ce54272580 | 50 | |
kotakku | 0:b1ce54272580 | 51 | #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device |
kotakku | 0:b1ce54272580 | 52 | #define HCI_CONNECT_DEVICE_STATE 8 |
kotakku | 0:b1ce54272580 | 53 | #define HCI_CONNECTED_DEVICE_STATE 9 |
kotakku | 0:b1ce54272580 | 54 | |
kotakku | 0:b1ce54272580 | 55 | #define HCI_SCANNING_STATE 10 |
kotakku | 0:b1ce54272580 | 56 | #define HCI_CONNECT_IN_STATE 11 |
kotakku | 0:b1ce54272580 | 57 | #define HCI_REMOTE_NAME_STATE 12 |
kotakku | 0:b1ce54272580 | 58 | #define HCI_CONNECTED_STATE 13 |
kotakku | 0:b1ce54272580 | 59 | #define HCI_DISABLE_SCAN_STATE 14 |
kotakku | 0:b1ce54272580 | 60 | #define HCI_DONE_STATE 15 |
kotakku | 0:b1ce54272580 | 61 | #define HCI_DISCONNECT_STATE 16 |
kotakku | 0:b1ce54272580 | 62 | |
kotakku | 0:b1ce54272580 | 63 | /* HCI event flags*/ |
kotakku | 0:b1ce54272580 | 64 | #define HCI_FLAG_CMD_COMPLETE (1UL << 0) |
kotakku | 0:b1ce54272580 | 65 | #define HCI_FLAG_CONNECT_COMPLETE (1UL << 1) |
kotakku | 0:b1ce54272580 | 66 | #define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2) |
kotakku | 0:b1ce54272580 | 67 | #define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3) |
kotakku | 0:b1ce54272580 | 68 | #define HCI_FLAG_INCOMING_REQUEST (1UL << 4) |
kotakku | 0:b1ce54272580 | 69 | #define HCI_FLAG_READ_BDADDR (1UL << 5) |
kotakku | 0:b1ce54272580 | 70 | #define HCI_FLAG_READ_VERSION (1UL << 6) |
kotakku | 0:b1ce54272580 | 71 | #define HCI_FLAG_DEVICE_FOUND (1UL << 7) |
kotakku | 0:b1ce54272580 | 72 | #define HCI_FLAG_CONNECT_EVENT (1UL << 8) |
kotakku | 0:b1ce54272580 | 73 | |
kotakku | 0:b1ce54272580 | 74 | /* Macros for HCI event flag tests */ |
kotakku | 0:b1ce54272580 | 75 | #define hci_check_flag(flag) (hci_event_flag & (flag)) |
kotakku | 0:b1ce54272580 | 76 | #define hci_set_flag(flag) (hci_event_flag |= (flag)) |
kotakku | 0:b1ce54272580 | 77 | #define hci_clear_flag(flag) (hci_event_flag &= ~(flag)) |
kotakku | 0:b1ce54272580 | 78 | |
kotakku | 0:b1ce54272580 | 79 | /* HCI Events managed */ |
kotakku | 0:b1ce54272580 | 80 | #define EV_INQUIRY_COMPLETE 0x01 |
kotakku | 0:b1ce54272580 | 81 | #define EV_INQUIRY_RESULT 0x02 |
kotakku | 0:b1ce54272580 | 82 | #define EV_CONNECT_COMPLETE 0x03 |
kotakku | 0:b1ce54272580 | 83 | #define EV_INCOMING_CONNECT 0x04 |
kotakku | 0:b1ce54272580 | 84 | #define EV_DISCONNECT_COMPLETE 0x05 |
kotakku | 0:b1ce54272580 | 85 | #define EV_AUTHENTICATION_COMPLETE 0x06 |
kotakku | 0:b1ce54272580 | 86 | #define EV_REMOTE_NAME_COMPLETE 0x07 |
kotakku | 0:b1ce54272580 | 87 | #define EV_ENCRYPTION_CHANGE 0x08 |
kotakku | 0:b1ce54272580 | 88 | #define EV_CHANGE_CONNECTION_LINK 0x09 |
kotakku | 0:b1ce54272580 | 89 | #define EV_ROLE_CHANGED 0x12 |
kotakku | 0:b1ce54272580 | 90 | #define EV_NUM_COMPLETE_PKT 0x13 |
kotakku | 0:b1ce54272580 | 91 | #define EV_PIN_CODE_REQUEST 0x16 |
kotakku | 0:b1ce54272580 | 92 | #define EV_LINK_KEY_REQUEST 0x17 |
kotakku | 0:b1ce54272580 | 93 | #define EV_LINK_KEY_NOTIFICATION 0x18 |
kotakku | 0:b1ce54272580 | 94 | #define EV_DATA_BUFFER_OVERFLOW 0x1A |
kotakku | 0:b1ce54272580 | 95 | #define EV_MAX_SLOTS_CHANGE 0x1B |
kotakku | 0:b1ce54272580 | 96 | #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C |
kotakku | 0:b1ce54272580 | 97 | #define EV_QOS_SETUP_COMPLETE 0x0D |
kotakku | 0:b1ce54272580 | 98 | #define EV_COMMAND_COMPLETE 0x0E |
kotakku | 0:b1ce54272580 | 99 | #define EV_COMMAND_STATUS 0x0F |
kotakku | 0:b1ce54272580 | 100 | #define EV_LOOPBACK_COMMAND 0x19 |
kotakku | 0:b1ce54272580 | 101 | #define EV_PAGE_SCAN_REP_MODE 0x20 |
kotakku | 0:b1ce54272580 | 102 | |
kotakku | 0:b1ce54272580 | 103 | /* Bluetooth states for the different Bluetooth drivers */ |
kotakku | 0:b1ce54272580 | 104 | #define L2CAP_WAIT 0 |
kotakku | 0:b1ce54272580 | 105 | #define L2CAP_DONE 1 |
kotakku | 0:b1ce54272580 | 106 | |
kotakku | 0:b1ce54272580 | 107 | /* Used for HID Control channel */ |
kotakku | 0:b1ce54272580 | 108 | #define L2CAP_CONTROL_CONNECT_REQUEST 2 |
kotakku | 0:b1ce54272580 | 109 | #define L2CAP_CONTROL_CONFIG_REQUEST 3 |
kotakku | 0:b1ce54272580 | 110 | #define L2CAP_CONTROL_SUCCESS 4 |
kotakku | 0:b1ce54272580 | 111 | #define L2CAP_CONTROL_DISCONNECT 5 |
kotakku | 0:b1ce54272580 | 112 | |
kotakku | 0:b1ce54272580 | 113 | /* Used for HID Interrupt channel */ |
kotakku | 0:b1ce54272580 | 114 | #define L2CAP_INTERRUPT_SETUP 6 |
kotakku | 0:b1ce54272580 | 115 | #define L2CAP_INTERRUPT_CONNECT_REQUEST 7 |
kotakku | 0:b1ce54272580 | 116 | #define L2CAP_INTERRUPT_CONFIG_REQUEST 8 |
kotakku | 0:b1ce54272580 | 117 | #define L2CAP_INTERRUPT_DISCONNECT 9 |
kotakku | 0:b1ce54272580 | 118 | |
kotakku | 0:b1ce54272580 | 119 | /* Used for SDP channel */ |
kotakku | 0:b1ce54272580 | 120 | #define L2CAP_SDP_WAIT 10 |
kotakku | 0:b1ce54272580 | 121 | #define L2CAP_SDP_SUCCESS 11 |
kotakku | 0:b1ce54272580 | 122 | |
kotakku | 0:b1ce54272580 | 123 | /* Used for RFCOMM channel */ |
kotakku | 0:b1ce54272580 | 124 | #define L2CAP_RFCOMM_WAIT 12 |
kotakku | 0:b1ce54272580 | 125 | #define L2CAP_RFCOMM_SUCCESS 13 |
kotakku | 0:b1ce54272580 | 126 | |
kotakku | 0:b1ce54272580 | 127 | #define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel |
kotakku | 0:b1ce54272580 | 128 | |
kotakku | 0:b1ce54272580 | 129 | /* Bluetooth states used by some drivers */ |
kotakku | 0:b1ce54272580 | 130 | #define TURN_ON_LED 17 |
kotakku | 0:b1ce54272580 | 131 | #define PS3_ENABLE_SIXAXIS 18 |
kotakku | 0:b1ce54272580 | 132 | #define WII_CHECK_MOTION_PLUS_STATE 19 |
kotakku | 0:b1ce54272580 | 133 | #define WII_CHECK_EXTENSION_STATE 20 |
kotakku | 0:b1ce54272580 | 134 | #define WII_INIT_MOTION_PLUS_STATE 21 |
kotakku | 0:b1ce54272580 | 135 | |
kotakku | 0:b1ce54272580 | 136 | /* L2CAP event flags for HID Control channel */ |
kotakku | 0:b1ce54272580 | 137 | #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST (1UL << 0) |
kotakku | 0:b1ce54272580 | 138 | #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS (1UL << 1) |
kotakku | 0:b1ce54272580 | 139 | #define L2CAP_FLAG_CONTROL_CONNECTED (1UL << 2) |
kotakku | 0:b1ce54272580 | 140 | #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE (1UL << 3) |
kotakku | 0:b1ce54272580 | 141 | |
kotakku | 0:b1ce54272580 | 142 | /* L2CAP event flags for HID Interrupt channel */ |
kotakku | 0:b1ce54272580 | 143 | #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST (1UL << 4) |
kotakku | 0:b1ce54272580 | 144 | #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS (1UL << 5) |
kotakku | 0:b1ce54272580 | 145 | #define L2CAP_FLAG_INTERRUPT_CONNECTED (1UL << 6) |
kotakku | 0:b1ce54272580 | 146 | #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE (1UL << 7) |
kotakku | 0:b1ce54272580 | 147 | |
kotakku | 0:b1ce54272580 | 148 | /* L2CAP event flags for SDP channel */ |
kotakku | 0:b1ce54272580 | 149 | #define L2CAP_FLAG_CONNECTION_SDP_REQUEST (1UL << 8) |
kotakku | 0:b1ce54272580 | 150 | #define L2CAP_FLAG_CONFIG_SDP_SUCCESS (1UL << 9) |
kotakku | 0:b1ce54272580 | 151 | #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST (1UL << 10) |
kotakku | 0:b1ce54272580 | 152 | |
kotakku | 0:b1ce54272580 | 153 | /* L2CAP event flags for RFCOMM channel */ |
kotakku | 0:b1ce54272580 | 154 | #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST (1UL << 11) |
kotakku | 0:b1ce54272580 | 155 | #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS (1UL << 12) |
kotakku | 0:b1ce54272580 | 156 | #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST (1UL << 13) |
kotakku | 0:b1ce54272580 | 157 | |
kotakku | 0:b1ce54272580 | 158 | #define L2CAP_FLAG_DISCONNECT_RESPONSE (1UL << 14) |
kotakku | 0:b1ce54272580 | 159 | |
kotakku | 0:b1ce54272580 | 160 | /* Macros for L2CAP event flag tests */ |
kotakku | 0:b1ce54272580 | 161 | #define l2cap_check_flag(flag) (l2cap_event_flag & (flag)) |
kotakku | 0:b1ce54272580 | 162 | #define l2cap_set_flag(flag) (l2cap_event_flag |= (flag)) |
kotakku | 0:b1ce54272580 | 163 | #define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag)) |
kotakku | 0:b1ce54272580 | 164 | |
kotakku | 0:b1ce54272580 | 165 | /* L2CAP signaling commands */ |
kotakku | 0:b1ce54272580 | 166 | #define L2CAP_CMD_COMMAND_REJECT 0x01 |
kotakku | 0:b1ce54272580 | 167 | #define L2CAP_CMD_CONNECTION_REQUEST 0x02 |
kotakku | 0:b1ce54272580 | 168 | #define L2CAP_CMD_CONNECTION_RESPONSE 0x03 |
kotakku | 0:b1ce54272580 | 169 | #define L2CAP_CMD_CONFIG_REQUEST 0x04 |
kotakku | 0:b1ce54272580 | 170 | #define L2CAP_CMD_CONFIG_RESPONSE 0x05 |
kotakku | 0:b1ce54272580 | 171 | #define L2CAP_CMD_DISCONNECT_REQUEST 0x06 |
kotakku | 0:b1ce54272580 | 172 | #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07 |
kotakku | 0:b1ce54272580 | 173 | #define L2CAP_CMD_INFORMATION_REQUEST 0x0A |
kotakku | 0:b1ce54272580 | 174 | #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B |
kotakku | 0:b1ce54272580 | 175 | |
kotakku | 0:b1ce54272580 | 176 | // Used For Connection Response - Remember to Include High Byte |
kotakku | 0:b1ce54272580 | 177 | #define PENDING 0x01 |
kotakku | 0:b1ce54272580 | 178 | #define SUCCESSFUL 0x00 |
kotakku | 0:b1ce54272580 | 179 | |
kotakku | 0:b1ce54272580 | 180 | /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */ |
kotakku | 0:b1ce54272580 | 181 | #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value |
kotakku | 0:b1ce54272580 | 182 | #define RFCOMM_PSM 0x03 // RFCOMM PSM Value |
kotakku | 0:b1ce54272580 | 183 | #define HID_CTRL_PSM 0x11 // HID_Control PSM Value |
kotakku | 0:b1ce54272580 | 184 | #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value |
kotakku | 0:b1ce54272580 | 185 | |
kotakku | 0:b1ce54272580 | 186 | // Used to determine if it is a Bluetooth dongle |
kotakku | 0:b1ce54272580 | 187 | #define WI_SUBCLASS_RF 0x01 // RF Controller |
kotakku | 0:b1ce54272580 | 188 | #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface |
kotakku | 0:b1ce54272580 | 189 | |
kotakku | 0:b1ce54272580 | 190 | #define BTD_MAX_ENDPOINTS 4 |
kotakku | 0:b1ce54272580 | 191 | #define BTD_NUM_SERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number |
kotakku | 0:b1ce54272580 | 192 | |
kotakku | 0:b1ce54272580 | 193 | #define PAIR 1 |
kotakku | 0:b1ce54272580 | 194 | |
kotakku | 0:b1ce54272580 | 195 | class BluetoothService; |
kotakku | 0:b1ce54272580 | 196 | |
kotakku | 0:b1ce54272580 | 197 | /** |
kotakku | 0:b1ce54272580 | 198 | * The Bluetooth Dongle class will take care of all the USB communication |
kotakku | 0:b1ce54272580 | 199 | * and then pass the data to the BluetoothService classes. |
kotakku | 0:b1ce54272580 | 200 | */ |
kotakku | 0:b1ce54272580 | 201 | class BTD : public USBDeviceConfig, public UsbConfigXtracter { |
kotakku | 0:b1ce54272580 | 202 | public: |
kotakku | 0:b1ce54272580 | 203 | /** |
kotakku | 0:b1ce54272580 | 204 | * Constructor for the BTD class. |
kotakku | 0:b1ce54272580 | 205 | * @param p Pointer to USB class instance. |
kotakku | 0:b1ce54272580 | 206 | */ |
kotakku | 0:b1ce54272580 | 207 | BTD(USB *p); |
kotakku | 0:b1ce54272580 | 208 | |
kotakku | 0:b1ce54272580 | 209 | /** @name USBDeviceConfig implementation */ |
kotakku | 0:b1ce54272580 | 210 | /** |
kotakku | 0:b1ce54272580 | 211 | * Address assignment and basic initialization is done here. |
kotakku | 0:b1ce54272580 | 212 | * @param parent Hub number. |
kotakku | 0:b1ce54272580 | 213 | * @param port Port number on the hub. |
kotakku | 0:b1ce54272580 | 214 | * @param lowspeed Speed of the device. |
kotakku | 0:b1ce54272580 | 215 | * @return 0 on success. |
kotakku | 0:b1ce54272580 | 216 | */ |
kotakku | 0:b1ce54272580 | 217 | uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 218 | /** |
kotakku | 0:b1ce54272580 | 219 | * Initialize the Bluetooth dongle. |
kotakku | 0:b1ce54272580 | 220 | * @param parent Hub number. |
kotakku | 0:b1ce54272580 | 221 | * @param port Port number on the hub. |
kotakku | 0:b1ce54272580 | 222 | * @param lowspeed Speed of the device. |
kotakku | 0:b1ce54272580 | 223 | * @return 0 on success. |
kotakku | 0:b1ce54272580 | 224 | */ |
kotakku | 0:b1ce54272580 | 225 | uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); |
kotakku | 0:b1ce54272580 | 226 | /** |
kotakku | 0:b1ce54272580 | 227 | * Release the USB device. |
kotakku | 0:b1ce54272580 | 228 | * @return 0 on success. |
kotakku | 0:b1ce54272580 | 229 | */ |
kotakku | 0:b1ce54272580 | 230 | uint8_t Release(); |
kotakku | 0:b1ce54272580 | 231 | /** |
kotakku | 0:b1ce54272580 | 232 | * Poll the USB Input endpoints and run the state machines. |
kotakku | 0:b1ce54272580 | 233 | * @return 0 on success. |
kotakku | 0:b1ce54272580 | 234 | */ |
kotakku | 0:b1ce54272580 | 235 | uint8_t Poll(); |
kotakku | 0:b1ce54272580 | 236 | |
kotakku | 0:b1ce54272580 | 237 | /** |
kotakku | 0:b1ce54272580 | 238 | * Get the device address. |
kotakku | 0:b1ce54272580 | 239 | * @return The device address. |
kotakku | 0:b1ce54272580 | 240 | */ |
kotakku | 0:b1ce54272580 | 241 | virtual uint8_t GetAddress() { |
kotakku | 0:b1ce54272580 | 242 | return bAddress; |
kotakku | 0:b1ce54272580 | 243 | }; |
kotakku | 0:b1ce54272580 | 244 | |
kotakku | 0:b1ce54272580 | 245 | /** |
kotakku | 0:b1ce54272580 | 246 | * Used to check if the dongle has been initialized. |
kotakku | 0:b1ce54272580 | 247 | * @return True if it's ready. |
kotakku | 0:b1ce54272580 | 248 | */ |
kotakku | 0:b1ce54272580 | 249 | virtual bool isReady() { |
kotakku | 0:b1ce54272580 | 250 | return bPollEnable; |
kotakku | 0:b1ce54272580 | 251 | }; |
kotakku | 0:b1ce54272580 | 252 | |
kotakku | 0:b1ce54272580 | 253 | /** |
kotakku | 0:b1ce54272580 | 254 | * Used by the USB core to check what this driver support. |
kotakku | 0:b1ce54272580 | 255 | * @param klass The device's USB class. |
kotakku | 0:b1ce54272580 | 256 | * @return Returns true if the device's USB class matches this driver. |
kotakku | 0:b1ce54272580 | 257 | */ |
kotakku | 0:b1ce54272580 | 258 | virtual bool DEVCLASSOK(uint8_t klass) { |
kotakku | 0:b1ce54272580 | 259 | return (klass == USB_CLASS_WIRELESS_CTRL); |
kotakku | 0:b1ce54272580 | 260 | }; |
kotakku | 0:b1ce54272580 | 261 | |
kotakku | 0:b1ce54272580 | 262 | /** |
kotakku | 0:b1ce54272580 | 263 | * Used by the USB core to check what this driver support. |
kotakku | 0:b1ce54272580 | 264 | * Used to set the Bluetooth address into the PS3 controllers. |
kotakku | 0:b1ce54272580 | 265 | * @param vid The device's VID. |
kotakku | 0:b1ce54272580 | 266 | * @param pid The device's PID. |
kotakku | 0:b1ce54272580 | 267 | * @return Returns true if the device's VID and PID matches this driver. |
kotakku | 0:b1ce54272580 | 268 | */ |
kotakku | 0:b1ce54272580 | 269 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
kotakku | 0:b1ce54272580 | 270 | if((vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID) || (vid == BELKIN_F8T065BF_VID && pid == BELKIN_F8T065BF_PID)) |
kotakku | 0:b1ce54272580 | 271 | return true; |
kotakku | 0:b1ce54272580 | 272 | 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 |
kotakku | 0:b1ce54272580 | 273 | if(vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID)) |
kotakku | 0:b1ce54272580 | 274 | return true; |
kotakku | 0:b1ce54272580 | 275 | } |
kotakku | 0:b1ce54272580 | 276 | return false; |
kotakku | 0:b1ce54272580 | 277 | }; |
kotakku | 0:b1ce54272580 | 278 | /**@}*/ |
kotakku | 0:b1ce54272580 | 279 | |
kotakku | 0:b1ce54272580 | 280 | /** @name UsbConfigXtracter implementation */ |
kotakku | 0:b1ce54272580 | 281 | /** |
kotakku | 0:b1ce54272580 | 282 | * UsbConfigXtracter implementation, used to extract endpoint information. |
kotakku | 0:b1ce54272580 | 283 | * @param conf Configuration value. |
kotakku | 0:b1ce54272580 | 284 | * @param iface Interface number. |
kotakku | 0:b1ce54272580 | 285 | * @param alt Alternate setting. |
kotakku | 0:b1ce54272580 | 286 | * @param proto Interface Protocol. |
kotakku | 0:b1ce54272580 | 287 | * @param ep Endpoint Descriptor. |
kotakku | 0:b1ce54272580 | 288 | */ |
kotakku | 0:b1ce54272580 | 289 | void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); |
kotakku | 0:b1ce54272580 | 290 | /**@}*/ |
kotakku | 0:b1ce54272580 | 291 | |
kotakku | 0:b1ce54272580 | 292 | /** Disconnects both the L2CAP Channel and the HCI Connection for all Bluetooth services. */ |
kotakku | 0:b1ce54272580 | 293 | void disconnect(); |
kotakku | 0:b1ce54272580 | 294 | |
kotakku | 0:b1ce54272580 | 295 | /** |
kotakku | 0:b1ce54272580 | 296 | * Register Bluetooth dongle members/services. |
kotakku | 0:b1ce54272580 | 297 | * @param pService Pointer to BluetoothService class instance. |
kotakku | 0:b1ce54272580 | 298 | * @return The service ID on success or -1 on fail. |
kotakku | 0:b1ce54272580 | 299 | */ |
kotakku | 0:b1ce54272580 | 300 | int8_t registerBluetoothService(BluetoothService *pService) { |
kotakku | 0:b1ce54272580 | 301 | for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) { |
kotakku | 0:b1ce54272580 | 302 | if(!btService[i]) { |
kotakku | 0:b1ce54272580 | 303 | btService[i] = pService; |
kotakku | 0:b1ce54272580 | 304 | return i; // Return ID |
kotakku | 0:b1ce54272580 | 305 | } |
kotakku | 0:b1ce54272580 | 306 | } |
kotakku | 0:b1ce54272580 | 307 | return -1; // Error registering BluetoothService |
kotakku | 0:b1ce54272580 | 308 | }; |
kotakku | 0:b1ce54272580 | 309 | |
kotakku | 0:b1ce54272580 | 310 | /** @name HCI Commands */ |
kotakku | 0:b1ce54272580 | 311 | /** |
kotakku | 0:b1ce54272580 | 312 | * Used to send a HCI Command. |
kotakku | 0:b1ce54272580 | 313 | * @param data Data to send. |
kotakku | 0:b1ce54272580 | 314 | * @param nbytes Number of bytes to send. |
kotakku | 0:b1ce54272580 | 315 | */ |
kotakku | 0:b1ce54272580 | 316 | void HCI_Command(uint8_t* data, uint16_t nbytes); |
kotakku | 0:b1ce54272580 | 317 | /** Reset the Bluetooth dongle. */ |
kotakku | 0:b1ce54272580 | 318 | void hci_reset(); |
kotakku | 0:b1ce54272580 | 319 | /** Read the Bluetooth address of the dongle. */ |
kotakku | 0:b1ce54272580 | 320 | void hci_read_bdaddr(); |
kotakku | 0:b1ce54272580 | 321 | /** Read the HCI Version of the Bluetooth dongle. */ |
kotakku | 0:b1ce54272580 | 322 | void hci_read_local_version_information(); |
kotakku | 0:b1ce54272580 | 323 | /** |
kotakku | 0:b1ce54272580 | 324 | * Set the local name of the Bluetooth dongle. |
kotakku | 0:b1ce54272580 | 325 | * @param name Desired name. |
kotakku | 0:b1ce54272580 | 326 | */ |
kotakku | 0:b1ce54272580 | 327 | void hci_set_local_name(const char* name); |
kotakku | 0:b1ce54272580 | 328 | /** Enable visibility to other Bluetooth devices. */ |
kotakku | 0:b1ce54272580 | 329 | void hci_write_scan_enable(); |
kotakku | 0:b1ce54272580 | 330 | /** Disable visibility to other Bluetooth devices. */ |
kotakku | 0:b1ce54272580 | 331 | void hci_write_scan_disable(); |
kotakku | 0:b1ce54272580 | 332 | /** Read the remote devices name. */ |
kotakku | 0:b1ce54272580 | 333 | void hci_remote_name(); |
kotakku | 0:b1ce54272580 | 334 | /** Accept the connection with the Bluetooth device. */ |
kotakku | 0:b1ce54272580 | 335 | void hci_accept_connection(); |
kotakku | 0:b1ce54272580 | 336 | /** |
kotakku | 0:b1ce54272580 | 337 | * Disconnect the HCI connection. |
kotakku | 0:b1ce54272580 | 338 | * @param handle The HCI Handle for the connection. |
kotakku | 0:b1ce54272580 | 339 | */ |
kotakku | 0:b1ce54272580 | 340 | void hci_disconnect(uint16_t handle); |
kotakku | 0:b1ce54272580 | 341 | /** |
kotakku | 0:b1ce54272580 | 342 | * Respond with the pin for the connection. |
kotakku | 0:b1ce54272580 | 343 | * The pin is automatically set for the Wii library, |
kotakku | 0:b1ce54272580 | 344 | * but can be customized for the SPP library. |
kotakku | 0:b1ce54272580 | 345 | */ |
kotakku | 0:b1ce54272580 | 346 | void hci_pin_code_request_reply(); |
kotakku | 0:b1ce54272580 | 347 | /** Respons when no pin was set. */ |
kotakku | 0:b1ce54272580 | 348 | void hci_pin_code_negative_request_reply(); |
kotakku | 0:b1ce54272580 | 349 | /** |
kotakku | 0:b1ce54272580 | 350 | * Command is used to reply to a Link Key Request event from the BR/EDR Controller |
kotakku | 0:b1ce54272580 | 351 | * if the Host does not have a stored Link Key for the connection. |
kotakku | 0:b1ce54272580 | 352 | */ |
kotakku | 0:b1ce54272580 | 353 | void hci_link_key_request_negative_reply(); |
kotakku | 0:b1ce54272580 | 354 | /** Used to try to authenticate with the remote device. */ |
kotakku | 0:b1ce54272580 | 355 | void hci_authentication_request(); |
kotakku | 0:b1ce54272580 | 356 | /** Start a HCI inquiry. */ |
kotakku | 0:b1ce54272580 | 357 | void hci_inquiry(); |
kotakku | 0:b1ce54272580 | 358 | /** Cancel a HCI inquiry. */ |
kotakku | 0:b1ce54272580 | 359 | void hci_inquiry_cancel(); |
kotakku | 0:b1ce54272580 | 360 | /** Connect to last device communicated with. */ |
kotakku | 0:b1ce54272580 | 361 | void hci_connect(); |
kotakku | 0:b1ce54272580 | 362 | /** |
kotakku | 0:b1ce54272580 | 363 | * Connect to device. |
kotakku | 0:b1ce54272580 | 364 | * @param bdaddr Bluetooth address of the device. |
kotakku | 0:b1ce54272580 | 365 | */ |
kotakku | 0:b1ce54272580 | 366 | void hci_connect(uint8_t *bdaddr); |
kotakku | 0:b1ce54272580 | 367 | /** Used to a set the class of the device. */ |
kotakku | 0:b1ce54272580 | 368 | void hci_write_class_of_device(); |
kotakku | 0:b1ce54272580 | 369 | /**@}*/ |
kotakku | 0:b1ce54272580 | 370 | |
kotakku | 0:b1ce54272580 | 371 | /** @name L2CAP Commands */ |
kotakku | 0:b1ce54272580 | 372 | /** |
kotakku | 0:b1ce54272580 | 373 | * Used to send L2CAP Commands. |
kotakku | 0:b1ce54272580 | 374 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 375 | * @param data Data to send. |
kotakku | 0:b1ce54272580 | 376 | * @param nbytes Number of bytes to send. |
kotakku | 0:b1ce54272580 | 377 | * @param channelLow,channelHigh Low and high byte of channel to send to. |
kotakku | 0:b1ce54272580 | 378 | * If argument is omitted then the Standard L2CAP header: Channel ID (0x01) for ACL-U will be used. |
kotakku | 0:b1ce54272580 | 379 | */ |
kotakku | 0:b1ce54272580 | 380 | void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00); |
kotakku | 0:b1ce54272580 | 381 | /** |
kotakku | 0:b1ce54272580 | 382 | * L2CAP Connection Request. |
kotakku | 0:b1ce54272580 | 383 | * @param handle HCI handle. |
kotakku | 0:b1ce54272580 | 384 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 385 | * @param scid Source Channel Identifier. |
kotakku | 0:b1ce54272580 | 386 | * @param psm Protocol/Service Multiplexer - see: https://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm. |
kotakku | 0:b1ce54272580 | 387 | */ |
kotakku | 0:b1ce54272580 | 388 | void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm); |
kotakku | 0:b1ce54272580 | 389 | /** |
kotakku | 0:b1ce54272580 | 390 | * L2CAP Connection Response. |
kotakku | 0:b1ce54272580 | 391 | * @param handle HCI handle. |
kotakku | 0:b1ce54272580 | 392 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 393 | * @param dcid Destination Channel Identifier. |
kotakku | 0:b1ce54272580 | 394 | * @param scid Source Channel Identifier. |
kotakku | 0:b1ce54272580 | 395 | * @param result Result - First send ::PENDING and then ::SUCCESSFUL. |
kotakku | 0:b1ce54272580 | 396 | */ |
kotakku | 0:b1ce54272580 | 397 | void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result); |
kotakku | 0:b1ce54272580 | 398 | /** |
kotakku | 0:b1ce54272580 | 399 | * L2CAP Config Request. |
kotakku | 0:b1ce54272580 | 400 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 401 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 402 | * @param dcid Destination Channel Identifier. |
kotakku | 0:b1ce54272580 | 403 | */ |
kotakku | 0:b1ce54272580 | 404 | void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid); |
kotakku | 0:b1ce54272580 | 405 | /** |
kotakku | 0:b1ce54272580 | 406 | * L2CAP Config Response. |
kotakku | 0:b1ce54272580 | 407 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 408 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 409 | * @param scid Source Channel Identifier. |
kotakku | 0:b1ce54272580 | 410 | */ |
kotakku | 0:b1ce54272580 | 411 | void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid); |
kotakku | 0:b1ce54272580 | 412 | /** |
kotakku | 0:b1ce54272580 | 413 | * L2CAP Disconnection Request. |
kotakku | 0:b1ce54272580 | 414 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 415 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 416 | * @param dcid Device Channel Identifier. |
kotakku | 0:b1ce54272580 | 417 | * @param scid Source Channel Identifier. |
kotakku | 0:b1ce54272580 | 418 | */ |
kotakku | 0:b1ce54272580 | 419 | void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid); |
kotakku | 0:b1ce54272580 | 420 | /** |
kotakku | 0:b1ce54272580 | 421 | * L2CAP Disconnection Response. |
kotakku | 0:b1ce54272580 | 422 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 423 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 424 | * @param dcid Device Channel Identifier. |
kotakku | 0:b1ce54272580 | 425 | * @param scid Source Channel Identifier. |
kotakku | 0:b1ce54272580 | 426 | */ |
kotakku | 0:b1ce54272580 | 427 | void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid); |
kotakku | 0:b1ce54272580 | 428 | /** |
kotakku | 0:b1ce54272580 | 429 | * L2CAP Information Response. |
kotakku | 0:b1ce54272580 | 430 | * @param handle HCI Handle. |
kotakku | 0:b1ce54272580 | 431 | * @param rxid Identifier. |
kotakku | 0:b1ce54272580 | 432 | * @param infoTypeLow,infoTypeHigh Infotype. |
kotakku | 0:b1ce54272580 | 433 | */ |
kotakku | 0:b1ce54272580 | 434 | void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh); |
kotakku | 0:b1ce54272580 | 435 | /**@}*/ |
kotakku | 0:b1ce54272580 | 436 | |
kotakku | 0:b1ce54272580 | 437 | /** Use this to see if it is waiting for a incoming connection. */ |
kotakku | 0:b1ce54272580 | 438 | bool waitingForConnection; |
kotakku | 0:b1ce54272580 | 439 | /** This is used by the service to know when to store the device information. */ |
kotakku | 0:b1ce54272580 | 440 | bool l2capConnectionClaimed; |
kotakku | 0:b1ce54272580 | 441 | /** This is used by the SPP library to claim the current SDP incoming request. */ |
kotakku | 0:b1ce54272580 | 442 | bool sdpConnectionClaimed; |
kotakku | 0:b1ce54272580 | 443 | /** This is used by the SPP library to claim the current RFCOMM incoming request. */ |
kotakku | 0:b1ce54272580 | 444 | bool rfcommConnectionClaimed; |
kotakku | 0:b1ce54272580 | 445 | |
kotakku | 0:b1ce54272580 | 446 | /** The name you wish to make the dongle show up as. It is set automatically by the SPP library. */ |
kotakku | 0:b1ce54272580 | 447 | const char* btdName; |
kotakku | 0:b1ce54272580 | 448 | /** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP and BTHID library. */ |
kotakku | 0:b1ce54272580 | 449 | const char* btdPin; |
kotakku | 0:b1ce54272580 | 450 | |
kotakku | 0:b1ce54272580 | 451 | /** The bluetooth dongles Bluetooth address. */ |
kotakku | 0:b1ce54272580 | 452 | uint8_t my_bdaddr[6]; |
kotakku | 0:b1ce54272580 | 453 | /** HCI handle for the last connection. */ |
kotakku | 0:b1ce54272580 | 454 | uint16_t hci_handle; |
kotakku | 0:b1ce54272580 | 455 | /** Last incoming devices Bluetooth address. */ |
kotakku | 0:b1ce54272580 | 456 | uint8_t disc_bdaddr[6]; |
kotakku | 0:b1ce54272580 | 457 | /** First 30 chars of last remote name. */ |
kotakku | 0:b1ce54272580 | 458 | char remote_name[30]; |
kotakku | 0:b1ce54272580 | 459 | /** |
kotakku | 0:b1ce54272580 | 460 | * The supported HCI Version read from the Bluetooth dongle. |
kotakku | 0:b1ce54272580 | 461 | * Used by the PS3BT library to check the HCI Version of the Bluetooth dongle, |
kotakku | 0:b1ce54272580 | 462 | * it should be at least 3 to work properly with the library. |
kotakku | 0:b1ce54272580 | 463 | */ |
kotakku | 0:b1ce54272580 | 464 | uint8_t hci_version; |
kotakku | 0:b1ce54272580 | 465 | |
kotakku | 0:b1ce54272580 | 466 | /** Call this function to pair with a Wiimote */ |
kotakku | 0:b1ce54272580 | 467 | void pairWithWiimote() { |
kotakku | 0:b1ce54272580 | 468 | pairWithWii = true; |
kotakku | 0:b1ce54272580 | 469 | hci_state = HCI_CHECK_DEVICE_SERVICE; |
kotakku | 0:b1ce54272580 | 470 | }; |
kotakku | 0:b1ce54272580 | 471 | /** Used to only send the ACL data to the Wiimote. */ |
kotakku | 0:b1ce54272580 | 472 | bool connectToWii; |
kotakku | 0:b1ce54272580 | 473 | /** True if a Wiimote is connecting. */ |
kotakku | 0:b1ce54272580 | 474 | bool incomingWii; |
kotakku | 0:b1ce54272580 | 475 | /** True when it should pair with a Wiimote. */ |
kotakku | 0:b1ce54272580 | 476 | bool pairWithWii; |
kotakku | 0:b1ce54272580 | 477 | /** True if it's the new Wiimote with the Motion Plus Inside or a Wii U Pro Controller. */ |
kotakku | 0:b1ce54272580 | 478 | bool motionPlusInside; |
kotakku | 0:b1ce54272580 | 479 | /** True if it's a Wii U Pro Controller. */ |
kotakku | 0:b1ce54272580 | 480 | bool wiiUProController; |
kotakku | 0:b1ce54272580 | 481 | |
kotakku | 0:b1ce54272580 | 482 | /** Call this function to pair with a HID device */ |
kotakku | 0:b1ce54272580 | 483 | void pairWithHID() { |
kotakku | 0:b1ce54272580 | 484 | waitingForConnection = false; |
kotakku | 0:b1ce54272580 | 485 | pairWithHIDDevice = true; |
kotakku | 0:b1ce54272580 | 486 | hci_state = HCI_CHECK_DEVICE_SERVICE; |
kotakku | 0:b1ce54272580 | 487 | }; |
kotakku | 0:b1ce54272580 | 488 | /** Used to only send the ACL data to the HID device. */ |
kotakku | 0:b1ce54272580 | 489 | bool connectToHIDDevice; |
kotakku | 0:b1ce54272580 | 490 | /** True if a HID device is connecting. */ |
kotakku | 0:b1ce54272580 | 491 | bool incomingHIDDevice; |
kotakku | 0:b1ce54272580 | 492 | /** True when it should pair with a device like a mouse or keyboard. */ |
kotakku | 0:b1ce54272580 | 493 | bool pairWithHIDDevice; |
kotakku | 0:b1ce54272580 | 494 | |
kotakku | 0:b1ce54272580 | 495 | /** |
kotakku | 0:b1ce54272580 | 496 | * Read the poll interval taken from the endpoint descriptors. |
kotakku | 0:b1ce54272580 | 497 | * @return The poll interval in ms. |
kotakku | 0:b1ce54272580 | 498 | */ |
kotakku | 0:b1ce54272580 | 499 | uint8_t readPollInterval() { |
kotakku | 0:b1ce54272580 | 500 | return pollInterval; |
kotakku | 0:b1ce54272580 | 501 | }; |
kotakku | 0:b1ce54272580 | 502 | |
kotakku | 0:b1ce54272580 | 503 | protected: |
kotakku | 0:b1ce54272580 | 504 | /** Pointer to USB class instance. */ |
kotakku | 0:b1ce54272580 | 505 | USB *pUsb; |
kotakku | 0:b1ce54272580 | 506 | /** Device address. */ |
kotakku | 0:b1ce54272580 | 507 | uint8_t bAddress; |
kotakku | 0:b1ce54272580 | 508 | /** Endpoint info structure. */ |
kotakku | 0:b1ce54272580 | 509 | EpInfo epInfo[BTD_MAX_ENDPOINTS]; |
kotakku | 0:b1ce54272580 | 510 | |
kotakku | 0:b1ce54272580 | 511 | /** Configuration number. */ |
kotakku | 0:b1ce54272580 | 512 | uint8_t bConfNum; |
kotakku | 0:b1ce54272580 | 513 | /** Total number of endpoints in the configuration. */ |
kotakku | 0:b1ce54272580 | 514 | uint8_t bNumEP; |
kotakku | 0:b1ce54272580 | 515 | /** Next poll time based on poll interval taken from the USB descriptor. */ |
kotakku | 0:b1ce54272580 | 516 | uint32_t qNextPollTime; |
kotakku | 0:b1ce54272580 | 517 | |
kotakku | 0:b1ce54272580 | 518 | /** Bluetooth dongle control endpoint. */ |
kotakku | 0:b1ce54272580 | 519 | static const uint8_t BTD_CONTROL_PIPE; |
kotakku | 0:b1ce54272580 | 520 | /** HCI event endpoint index. */ |
kotakku | 0:b1ce54272580 | 521 | static const uint8_t BTD_EVENT_PIPE; |
kotakku | 0:b1ce54272580 | 522 | /** ACL In endpoint index. */ |
kotakku | 0:b1ce54272580 | 523 | static const uint8_t BTD_DATAIN_PIPE; |
kotakku | 0:b1ce54272580 | 524 | /** ACL Out endpoint index. */ |
kotakku | 0:b1ce54272580 | 525 | static const uint8_t BTD_DATAOUT_PIPE; |
kotakku | 0:b1ce54272580 | 526 | |
kotakku | 0:b1ce54272580 | 527 | /** |
kotakku | 0:b1ce54272580 | 528 | * Used to print the USB Endpoint Descriptor. |
kotakku | 0:b1ce54272580 | 529 | * @param ep_ptr Pointer to USB Endpoint Descriptor. |
kotakku | 0:b1ce54272580 | 530 | */ |
kotakku | 0:b1ce54272580 | 531 | void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); |
kotakku | 0:b1ce54272580 | 532 | |
kotakku | 0:b1ce54272580 | 533 | private: |
kotakku | 0:b1ce54272580 | 534 | void Initialize(); // Set all variables, endpoint structs etc. to default values |
kotakku | 0:b1ce54272580 | 535 | BluetoothService *btService[BTD_NUM_SERVICES]; |
kotakku | 0:b1ce54272580 | 536 | |
kotakku | 0:b1ce54272580 | 537 | uint16_t PID, VID; // PID and VID of device connected |
kotakku | 0:b1ce54272580 | 538 | |
kotakku | 0:b1ce54272580 | 539 | uint8_t pollInterval; |
kotakku | 0:b1ce54272580 | 540 | bool bPollEnable; |
kotakku | 0:b1ce54272580 | 541 | |
kotakku | 0:b1ce54272580 | 542 | bool pairWiiUsingSync; // True if pairing was done using the Wii SYNC button. |
kotakku | 0:b1ce54272580 | 543 | bool checkRemoteName; // Used to check remote device's name before connecting. |
kotakku | 0:b1ce54272580 | 544 | bool incomingPS4; // True if a PS4 controller is connecting |
kotakku | 0:b1ce54272580 | 545 | uint8_t classOfDevice[3]; // Class of device of last device |
kotakku | 0:b1ce54272580 | 546 | |
kotakku | 0:b1ce54272580 | 547 | /* Variables used by high level HCI task */ |
kotakku | 0:b1ce54272580 | 548 | uint8_t hci_state; // Current state of Bluetooth HCI connection |
kotakku | 0:b1ce54272580 | 549 | uint16_t hci_counter; // Counter used for Bluetooth HCI reset loops |
kotakku | 0:b1ce54272580 | 550 | uint16_t hci_num_reset_loops; // This value indicate how many times it should read before trying to reset |
kotakku | 0:b1ce54272580 | 551 | uint16_t hci_event_flag; // HCI flags of received Bluetooth events |
kotakku | 0:b1ce54272580 | 552 | uint8_t inquiry_counter; |
kotakku | 0:b1ce54272580 | 553 | |
kotakku | 0:b1ce54272580 | 554 | uint8_t hcibuf[BULK_MAXPKTSIZE]; // General purpose buffer for HCI data |
kotakku | 0:b1ce54272580 | 555 | uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data |
kotakku | 0:b1ce54272580 | 556 | uint8_t l2capoutbuf[14]; // General purpose buffer for L2CAP out data |
kotakku | 0:b1ce54272580 | 557 | |
kotakku | 0:b1ce54272580 | 558 | /* State machines */ |
kotakku | 0:b1ce54272580 | 559 | void HCI_event_task(); // Poll the HCI event pipe |
kotakku | 0:b1ce54272580 | 560 | void HCI_task(); // HCI state machine |
kotakku | 0:b1ce54272580 | 561 | void ACL_event_task(); // ACL input pipe |
kotakku | 0:b1ce54272580 | 562 | |
kotakku | 0:b1ce54272580 | 563 | /* Used to set the Bluetooth Address internally to the PS3 Controllers */ |
kotakku | 0:b1ce54272580 | 564 | void setBdaddr(uint8_t* BDADDR); |
kotakku | 0:b1ce54272580 | 565 | void setMoveBdaddr(uint8_t* BDADDR); |
kotakku | 0:b1ce54272580 | 566 | }; |
kotakku | 0:b1ce54272580 | 567 | |
kotakku | 0:b1ce54272580 | 568 | /** All Bluetooth services should inherit this class. */ |
kotakku | 0:b1ce54272580 | 569 | class BluetoothService { |
kotakku | 0:b1ce54272580 | 570 | public: |
kotakku | 0:b1ce54272580 | 571 | BluetoothService(BTD *p) : pBtd(p) { |
kotakku | 0:b1ce54272580 | 572 | if(pBtd) |
kotakku | 0:b1ce54272580 | 573 | pBtd->registerBluetoothService(this); // Register it as a Bluetooth service |
kotakku | 0:b1ce54272580 | 574 | }; |
kotakku | 0:b1ce54272580 | 575 | /** |
kotakku | 0:b1ce54272580 | 576 | * Used to pass acldata to the Bluetooth service. |
kotakku | 0:b1ce54272580 | 577 | * @param ACLData Pointer to the incoming acldata. |
kotakku | 0:b1ce54272580 | 578 | */ |
kotakku | 0:b1ce54272580 | 579 | virtual void ACLData(uint8_t* ACLData) = 0; |
kotakku | 0:b1ce54272580 | 580 | /** Used to run the different state machines in the Bluetooth service. */ |
kotakku | 0:b1ce54272580 | 581 | virtual void Run() = 0; |
kotakku | 0:b1ce54272580 | 582 | /** Used to reset the Bluetooth service. */ |
kotakku | 0:b1ce54272580 | 583 | virtual void Reset() = 0; |
kotakku | 0:b1ce54272580 | 584 | /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */ |
kotakku | 0:b1ce54272580 | 585 | virtual void disconnect() = 0; |
kotakku | 0:b1ce54272580 | 586 | |
kotakku | 0:b1ce54272580 | 587 | /** |
kotakku | 0:b1ce54272580 | 588 | * Used to call your own function when the device is successfully initialized. |
kotakku | 0:b1ce54272580 | 589 | * @param funcOnInit Function to call. |
kotakku | 0:b1ce54272580 | 590 | */ |
kotakku | 0:b1ce54272580 | 591 | void attachOnInit(void (*funcOnInit)(void)) { |
kotakku | 0:b1ce54272580 | 592 | pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times |
kotakku | 0:b1ce54272580 | 593 | }; |
kotakku | 0:b1ce54272580 | 594 | |
kotakku | 0:b1ce54272580 | 595 | protected: |
kotakku | 0:b1ce54272580 | 596 | /** |
kotakku | 0:b1ce54272580 | 597 | * Called when a device is successfully initialized. |
kotakku | 0:b1ce54272580 | 598 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. |
kotakku | 0:b1ce54272580 | 599 | * This is useful for instance if you want to set the LEDs in a specific way. |
kotakku | 0:b1ce54272580 | 600 | */ |
kotakku | 0:b1ce54272580 | 601 | virtual void onInit() = 0; |
kotakku | 0:b1ce54272580 | 602 | |
kotakku | 0:b1ce54272580 | 603 | /** Used to check if the incoming L2CAP data matches the HCI Handle */ |
kotakku | 0:b1ce54272580 | 604 | bool checkHciHandle(uint8_t *buf, uint16_t handle) { |
kotakku | 0:b1ce54272580 | 605 | return (buf[0] == (handle & 0xFF)) && (buf[1] == ((handle >> 8) | 0x20)); |
kotakku | 0:b1ce54272580 | 606 | } |
kotakku | 0:b1ce54272580 | 607 | |
kotakku | 0:b1ce54272580 | 608 | /** Pointer to function called in onInit(). */ |
kotakku | 0:b1ce54272580 | 609 | void (*pFuncOnInit)(void); |
kotakku | 0:b1ce54272580 | 610 | |
kotakku | 0:b1ce54272580 | 611 | /** Pointer to BTD instance. */ |
kotakku | 0:b1ce54272580 | 612 | BTD *pBtd; |
kotakku | 0:b1ce54272580 | 613 | |
kotakku | 0:b1ce54272580 | 614 | /** The HCI Handle for the connection. */ |
kotakku | 0:b1ce54272580 | 615 | uint16_t hci_handle; |
kotakku | 0:b1ce54272580 | 616 | |
kotakku | 0:b1ce54272580 | 617 | /** L2CAP flags of received Bluetooth events. */ |
kotakku | 0:b1ce54272580 | 618 | uint32_t l2cap_event_flag; |
kotakku | 0:b1ce54272580 | 619 | |
kotakku | 0:b1ce54272580 | 620 | /** Identifier for L2CAP commands. */ |
kotakku | 0:b1ce54272580 | 621 | uint8_t identifier; |
kotakku | 0:b1ce54272580 | 622 | }; |
kotakku | 0:b1ce54272580 | 623 | |
kotakku | 0:b1ce54272580 | 624 | #endif |
kotakku | 0:b1ce54272580 | 625 |