USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BTD.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #ifndef _btd_h_
19 #define _btd_h_
20 
21 #include "Usb.h"
22 
23 //PID and VID of the Sony PS3 devices
24 #define PS3_VID 0x054C // Sony Corporation
25 #define PS3_PID 0x0268 // PS3 Controller DualShock 3
26 #define PS3NAVIGATION_PID 0x042F // Navigation controller
27 #define PS3MOVE_PID 0x03D5 // Motion controller
28 
29 #define IOGEAR_GBU521_VID 0x0A5C // The IOGEAR GBU521 dongle does not presents itself correctly, so we have to check for it manually
30 #define IOGEAR_GBU521_PID 0x21E8
31 
32 /* Bluetooth dongle data taken from descriptors */
33 #define BULK_MAXPKTSIZE 64 // Max size for ACL data
34 
35 // Used in control endpoint header for HCI Commands
36 #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
37 // Used in control endpoint header for HID Commands
38 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
39 #define HID_REQUEST_SET_REPORT 0x09
40 
41 /* Bluetooth HCI states for hci_task() */
42 #define HCI_INIT_STATE 0
43 #define HCI_RESET_STATE 1
44 #define HCI_CLASS_STATE 2
45 #define HCI_BDADDR_STATE 3
46 #define HCI_LOCAL_VERSION_STATE 4
47 #define HCI_SET_NAME_STATE 5
48 #define HCI_CHECK_DEVICE_SERVICE 6
49 
50 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device
51 #define HCI_CONNECT_DEVICE_STATE 8
52 #define HCI_CONNECTED_DEVICE_STATE 9
53 
54 #define HCI_SCANNING_STATE 10
55 #define HCI_CONNECT_IN_STATE 11
56 #define HCI_REMOTE_NAME_STATE 12
57 #define HCI_CONNECTED_STATE 13
58 #define HCI_DISABLE_SCAN_STATE 14
59 #define HCI_DONE_STATE 15
60 #define HCI_DISCONNECT_STATE 16
61 
62 /* HCI event flags*/
63 #define HCI_FLAG_CMD_COMPLETE 0x01
64 #define HCI_FLAG_CONNECT_COMPLETE 0x02
65 #define HCI_FLAG_DISCONNECT_COMPLETE 0x04
66 #define HCI_FLAG_REMOTE_NAME_COMPLETE 0x08
67 #define HCI_FLAG_INCOMING_REQUEST 0x10
68 #define HCI_FLAG_READ_BDADDR 0x20
69 #define HCI_FLAG_READ_VERSION 0x40
70 #define HCI_FLAG_DEVICE_FOUND 0x80
71 #define HCI_FLAG_CONNECT_EVENT 0x100
72 
73 /* Macros for HCI event flag tests */
74 #define hci_check_flag(flag) (hci_event_flag & (flag))
75 #define hci_set_flag(flag) (hci_event_flag |= (flag))
76 #define hci_clear_flag(flag) (hci_event_flag &= ~(flag))
77 
78 /* HCI Events managed */
79 #define EV_INQUIRY_COMPLETE 0x01
80 #define EV_INQUIRY_RESULT 0x02
81 #define EV_CONNECT_COMPLETE 0x03
82 #define EV_INCOMING_CONNECT 0x04
83 #define EV_DISCONNECT_COMPLETE 0x05
84 #define EV_AUTHENTICATION_COMPLETE 0x06
85 #define EV_REMOTE_NAME_COMPLETE 0x07
86 #define EV_ENCRYPTION_CHANGE 0x08
87 #define EV_CHANGE_CONNECTION_LINK 0x09
88 #define EV_ROLE_CHANGED 0x12
89 #define EV_NUM_COMPLETE_PKT 0x13
90 #define EV_PIN_CODE_REQUEST 0x16
91 #define EV_LINK_KEY_REQUEST 0x17
92 #define EV_LINK_KEY_NOTIFICATION 0x18
93 #define EV_DATA_BUFFER_OVERFLOW 0x1A
94 #define EV_MAX_SLOTS_CHANGE 0x1B
95 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
96 #define EV_QOS_SETUP_COMPLETE 0x0D
97 #define EV_COMMAND_COMPLETE 0x0E
98 #define EV_COMMAND_STATUS 0x0F
99 #define EV_LOOPBACK_COMMAND 0x19
100 #define EV_PAGE_SCAN_REP_MODE 0x20
101 
102 /* Bluetooth states for the different Bluetooth drivers */
103 #define L2CAP_WAIT 0
104 #define L2CAP_DONE 1
105 
106 /* Used for HID Control channel */
107 #define L2CAP_CONTROL_CONNECT_REQUEST 2
108 #define L2CAP_CONTROL_CONFIG_REQUEST 3
109 #define L2CAP_CONTROL_SUCCESS 4
110 #define L2CAP_CONTROL_DISCONNECT 5
111 
112 /* Used for HID Interrupt channel */
113 #define L2CAP_INTERRUPT_SETUP 6
114 #define L2CAP_INTERRUPT_CONNECT_REQUEST 7
115 #define L2CAP_INTERRUPT_CONFIG_REQUEST 8
116 #define L2CAP_INTERRUPT_DISCONNECT 9
117 
118 /* Used for SDP channel */
119 #define L2CAP_SDP_WAIT 10
120 #define L2CAP_SDP_SUCCESS 11
121 
122 /* Used for RFCOMM channel */
123 #define L2CAP_RFCOMM_WAIT 12
124 #define L2CAP_RFCOMM_SUCCESS 13
125 
126 #define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel
127 
128 /* Bluetooth states used by some drivers */
129 #define TURN_ON_LED 17
130 #define PS3_ENABLE_SIXAXIS 18
131 #define WII_CHECK_MOTION_PLUS_STATE 19
132 #define WII_CHECK_EXTENSION_STATE 20
133 #define WII_INIT_MOTION_PLUS_STATE 21
134 
135 /* L2CAP event flags for HID Control channel */
136 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x00000001
137 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x00000002
138 #define L2CAP_FLAG_CONTROL_CONNECTED 0x00000004
139 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x00000008
140 
141 /* L2CAP event flags for HID Interrupt channel */
142 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x00000010
143 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x00000020
144 #define L2CAP_FLAG_INTERRUPT_CONNECTED 0x00000040
145 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x00000080
146 
147 /* L2CAP event flags for SDP channel */
148 #define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x00000100
149 #define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000200
150 #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000400
151 
152 /* L2CAP event flags for RFCOMM channel */
153 #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00000800
154 #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00001000
155 #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x00002000
156 
157 #define L2CAP_FLAG_DISCONNECT_RESPONSE 0x00004000
158 
159 /* Macros for L2CAP event flag tests */
160 #define l2cap_check_flag(flag) (l2cap_event_flag & (flag))
161 #define l2cap_set_flag(flag) (l2cap_event_flag |= (flag))
162 #define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag))
163 
164 /* L2CAP signaling commands */
165 #define L2CAP_CMD_COMMAND_REJECT 0x01
166 #define L2CAP_CMD_CONNECTION_REQUEST 0x02
167 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
168 #define L2CAP_CMD_CONFIG_REQUEST 0x04
169 #define L2CAP_CMD_CONFIG_RESPONSE 0x05
170 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
171 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
172 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
173 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
174 
175 // Used For Connection Response - Remember to Include High Byte
176 #define PENDING 0x01
177 #define SUCCESSFUL 0x00
178 
179 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
180 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
181 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
182 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
183 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
184 
185 // Used to determine if it is a Bluetooth dongle
186 #define WI_SUBCLASS_RF 0x01 // RF Controller
187 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
188 
189 #define BTD_MAX_ENDPOINTS 4
190 #define BTD_NUM_SERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number
191 
192 #define PAIR 1
193 
194 /* acl_handle_ok or it's a new connection */
195 #if 0
196 #define UHS_ACL_HANDLE_OK(x, y) ((uint16_t)(x[0]) | (uint16_t)(x[1] << 8)) == (y | 0x2000U)
197 #else
198 /*
199  * Better implementation.
200  * o One place for this code, it is reused four times in the source.
201  * Perhaps it is better as a function.
202  * o This should be faster since the && operation can early exit, this means
203  * the shift would only be performed if the first byte matches.
204  * o Casting is eliminated.
205  * o How does this compare in code size? No difference. It is a free optimization.
206  */
207 #define UHS_ACL_HANDLE_OK(x, y) ((x[0] == (y & 0xff)) && (x[1] == ((y >> 8) | 0x20)))
208 #endif
209 
212 public:
217  virtual void ACLData(uint8_t* ACLData);
219  virtual void Run();
221  virtual void Reset();
223  virtual void disconnect();
224 };
225 
230 class BTD : public USBDeviceConfig, public UsbConfigXtracter {
231 public:
236  BTD(USB *p);
237 
246  virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
254  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
259  virtual uint8_t Release();
264  virtual uint8_t Poll();
265 
270  virtual uint8_t GetAddress() {
271  return bAddress;
272  };
273 
278  virtual bool isReady() {
279  return bPollEnable;
280  };
281 
287  virtual boolean DEVCLASSOK(uint8_t klass) {
288  return (klass == USB_CLASS_WIRELESS_CTRL);
289  };
290 
298  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
299  if(vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID)
300  return true;
301  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
302  if(vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID))
303  return true;
304  }
305  return false;
306  };
318  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
322  void disconnect() {
323  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
324  if(btService[i])
325  btService[i]->disconnect();
326  };
327 
334  for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) {
335  if(!btService[i]) {
336  btService[i] = pService;
337  return i; // Return ID
338  }
339  }
340  return -1; // ErrorregisterServiceClass
341  };
342 
349  void HCI_Command(uint8_t* data, uint16_t nbytes);
351  void hci_reset();
353  void hci_read_bdaddr();
360  void hci_set_local_name(const char* name);
362  void hci_write_scan_enable();
364  void hci_write_scan_disable();
366  void hci_remote_name();
368  void hci_accept_connection();
373  void hci_disconnect(uint16_t handle);
390  void hci_inquiry();
392  void hci_inquiry_cancel();
394  void hci_connect();
399  void hci_connect(uint8_t *bdaddr);
413  void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
421  void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
430  void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
437  void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
444  void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
452  void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
460  void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
467  void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
478 
480  const char* btdName;
482  const char* btdPin;
483 
485  uint8_t my_bdaddr[6];
487  uint16_t hci_handle;
489  uint8_t disc_bdaddr[6];
491  char remote_name[30];
497  uint8_t hci_version;
498 
501  pairWithWii = true;
502  hci_state = HCI_CHECK_DEVICE_SERVICE;
503  };
505  bool connectToWii;
514 
516  void pairWithHID() {
517  pairWithHIDDevice = true;
518  hci_state = HCI_CHECK_DEVICE_SERVICE;
519  };
521  bool connectToHIDDevice;
526 
531  uint8_t readPollInterval() {
532  return pollInterval;
533  };
534 
535 protected:
537  USB *pUsb;
539  uint8_t bAddress;
542 
544  uint8_t bConfNum;
546  uint8_t bNumEP;
548  uint32_t qNextPollTime;
549 
551  static const uint8_t BTD_CONTROL_PIPE;
553  static const uint8_t BTD_EVENT_PIPE;
555  static const uint8_t BTD_DATAIN_PIPE;
557  static const uint8_t BTD_DATAOUT_PIPE;
558 
564 
565 private:
566  void Initialize(); // Set all variables, endpoint structs etc. to default values
568 
569  uint16_t PID, VID; // PID and VID of device connected
570 
571  uint8_t pollInterval;
572  bool bPollEnable;
573 
574  bool incomingPS4; // True if a PS4 controller is connecting
575  uint8_t classOfDevice[3]; // Class of device of last device
576 
577  /* Variables used by high level HCI task */
578  uint8_t hci_state; // Current state of Bluetooth HCI connection
579  uint16_t hci_counter; // Counter used for Bluetooth HCI reset loops
580  uint16_t hci_num_reset_loops; // This value indicate how many times it should read before trying to reset
581  uint16_t hci_event_flag; // HCI flags of received Bluetooth events
582  uint8_t inquiry_counter;
583 
584  uint8_t hcibuf[BULK_MAXPKTSIZE]; // General purpose buffer for HCI data
585  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
586  uint8_t l2capoutbuf[14]; // General purpose buffer for L2CAP out data
587 
588  /* State machines */
589  void HCI_event_task(); // Poll the HCI event pipe
590  void HCI_task(); // HCI state machine
591  void ACL_event_task(); // ACL input pipe
592 
593  /* Used to set the Bluetooth Address internally to the PS3 Controllers */
594  void setBdaddr(uint8_t* BDADDR);
595  void setMoveBdaddr(uint8_t* BDADDR);
596 };
597 #endif
static const uint8_t BTD_DATAOUT_PIPE
Definition: BTD.h:557
bool incomingWii
Definition: BTD.h:507
void hci_connect()
Definition: BTD.cpp:1042
uint8_t bNumEP
Definition: BTD.h:546
const char * btdName
Definition: BTD.h:480
void hci_reset()
Definition: BTD.cpp:924
void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid, uint8_t result)
Definition: BTD.cpp:1231
void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t *scid, uint16_t psm)
Definition: BTD.cpp:1218
bool sdpConnectionClaimed
Definition: BTD.h:475
Definition: BTD.h:230
virtual boolean DEVCLASSOK(uint8_t klass)
Definition: BTD.h:287
void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1284
bool rfcommConnectionClaimed
Definition: BTD.h:477
uint8_t hci_version
Definition: BTD.h:497
void hci_inquiry()
Definition: BTD.cpp:1020
static const uint8_t BTD_EVENT_PIPE
Definition: BTD.h:553
#define PS3MOVE_PID
Definition: BTD.h:27
bool pairWithWii
Definition: BTD.h:509
void hci_write_scan_disable()
Definition: BTD.cpp:946
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: BTD.cpp:314
virtual uint8_t GetAddress()
Definition: BTD.h:270
#define BTD_NUM_SERVICES
Definition: BTD.h:190
const char * btdPin
Definition: BTD.h:482
bool motionPlusInside
Definition: BTD.h:511
void hci_remote_name()
Definition: BTD.cpp:989
#define PS3_VID
Definition: BTD.h:24
BTD(USB *p)
Definition: BTD.cpp:27
char remote_name[30]
Definition: BTD.h:491
uint8_t readPollInterval()
Definition: BTD.h:531
virtual void Reset()
static const uint8_t BTD_DATAIN_PIPE
Definition: BTD.h:555
void hci_set_local_name(const char *name)
Definition: BTD.cpp:1008
void hci_write_scan_enable()
Definition: BTD.cpp:933
virtual uint8_t Release()
Definition: BTD.cpp:365
void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1297
#define USB_CLASS_WIRELESS_CTRL
Definition: UsbCore.h:55
bool watingForConnection
Definition: BTD.h:471
bool connectToHIDDevice
Definition: BTD.h:519
uint8_t bAddress
Definition: BTD.h:539
bool incomingHIDDevice
Definition: BTD.h:523
bool pairWithHIDDevice
Definition: BTD.h:525
uint32_t qNextPollTime
Definition: BTD.h:548
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: BTD.cpp:346
#define IOGEAR_GBU521_PID
Definition: BTD.h:30
bool connectToWii
Definition: BTD.h:503
virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:48
bool wiiUProController
Definition: BTD.h:513
uint16_t hci_handle
Definition: BTD.h:487
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:122
Definition: address.h:32
void pairWithHID()
Definition: BTD.h:516
void hci_disconnect(uint16_t handle)
Definition: BTD.cpp:1142
void hci_read_bdaddr()
Definition: BTD.cpp:955
void hci_inquiry_cancel()
Definition: BTD.cpp:1034
uint8_t my_bdaddr[6]
Definition: BTD.h:485
virtual uint8_t Poll()
Definition: BTD.cpp:371
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: BTD.h:298
static const uint8_t BTD_CONTROL_PIPE
Definition: BTD.h:551
#define BULK_MAXPKTSIZE
Definition: BTD.h:33
void disconnect()
Definition: BTD.h:322
uint8_t disc_bdaddr[6]
Definition: BTD.h:489
int8_t registerServiceClass(BluetoothService *pService)
Definition: BTD.h:333
bool l2capConnectionClaimed
Definition: BTD.h:473
EpInfo epInfo[BTD_MAX_ENDPOINTS]
Definition: BTD.h:541
#define IOGEAR_GBU521_VID
Definition: BTD.h:29
virtual void ACLData(uint8_t *ACLData)
#define PS3_PID
Definition: BTD.h:25
#define BTD_MAX_ENDPOINTS
Definition: BTD.h:189
#define PS3NAVIGATION_PID
Definition: BTD.h:26
void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh)
Definition: BTD.cpp:1310
void hci_write_class_of_device()
Definition: BTD.cpp:1154
void hci_pin_code_negative_request_reply()
Definition: BTD.cpp:1104
#define HCI_CHECK_DEVICE_SERVICE
Definition: BTD.h:48
Definition: UsbCore.h:176
virtual void Run()
uint8_t bConfNum
Definition: BTD.h:544
void hci_link_key_request_negative_reply()
Definition: BTD.cpp:1118
void pairWithWiimote()
Definition: BTD.h:500
virtual bool isReady()
Definition: BTD.h:278
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1190
void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t *scid)
Definition: BTD.cpp:1265
void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t *dcid)
Definition: BTD.cpp:1248
void hci_pin_code_request_reply()
Definition: BTD.cpp:1068
virtual void disconnect()
USB * pUsb
Definition: BTD.h:533
void hci_authentication_request()
Definition: BTD.cpp:1132
void hci_read_local_version_information()
Definition: BTD.cpp:964
void hci_accept_connection()
Definition: BTD.cpp:973
void HCI_Command(uint8_t *data, uint16_t nbytes)
Definition: BTD.cpp:919