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 Wii controller
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_CONN_COMPLETE 0x02
65 #define HCI_FLAG_DISCONN_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_cmd_complete (hci_event_flag & HCI_FLAG_CMD_COMPLETE)
75 #define hci_connect_complete (hci_event_flag & HCI_FLAG_CONN_COMPLETE)
76 #define hci_disconnect_complete (hci_event_flag & HCI_FLAG_DISCONN_COMPLETE)
77 #define hci_remote_name_complete (hci_event_flag & HCI_FLAG_REMOTE_NAME_COMPLETE)
78 #define hci_incoming_connect_request (hci_event_flag & HCI_FLAG_INCOMING_REQUEST)
79 #define hci_read_bdaddr_complete (hci_event_flag & HCI_FLAG_READ_BDADDR)
80 #define hci_read_version_complete (hci_event_flag & HCI_FLAG_READ_VERSION)
81 #define hci_device_found (hci_event_flag & HCI_FLAG_DEVICE_FOUND)
82 #define hci_connect_event (hci_event_flag & HCI_FLAG_CONNECT_EVENT)
83 
84 /* HCI Events managed */
85 #define EV_INQUIRY_COMPLETE 0x01
86 #define EV_INQUIRY_RESULT 0x02
87 #define EV_CONNECT_COMPLETE 0x03
88 #define EV_INCOMING_CONNECT 0x04
89 #define EV_DISCONNECT_COMPLETE 0x05
90 #define EV_AUTHENTICATION_COMPLETE 0x06
91 #define EV_REMOTE_NAME_COMPLETE 0x07
92 #define EV_ENCRYPTION_CHANGE 0x08
93 #define EV_CHANGE_CONNECTION_LINK 0x09
94 #define EV_ROLE_CHANGED 0x12
95 #define EV_NUM_COMPLETE_PKT 0x13
96 #define EV_PIN_CODE_REQUEST 0x16
97 #define EV_LINK_KEY_REQUEST 0x17
98 #define EV_LINK_KEY_NOTIFICATION 0x18
99 #define EV_DATA_BUFFER_OVERFLOW 0x1A
100 #define EV_MAX_SLOTS_CHANGE 0x1B
101 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
102 #define EV_QOS_SETUP_COMPLETE 0x0D
103 #define EV_COMMAND_COMPLETE 0x0E
104 #define EV_COMMAND_STATUS 0x0F
105 #define EV_LOOPBACK_COMMAND 0x19
106 #define EV_PAGE_SCAN_REP_MODE 0x20
107 
108 /* L2CAP signaling commands */
109 #define L2CAP_CMD_COMMAND_REJECT 0x01
110 #define L2CAP_CMD_CONNECTION_REQUEST 0x02
111 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
112 #define L2CAP_CMD_CONFIG_REQUEST 0x04
113 #define L2CAP_CMD_CONFIG_RESPONSE 0x05
114 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
115 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
116 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
117 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
118 
119 // Used For Connection Response - Remember to Include High Byte
120 #define PENDING 0x01
121 #define SUCCESSFUL 0x00
122 
123 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
124 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
125 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
126 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
127 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
128 
129 // Used to determine if it is a Bluetooth dongle
130 #define WI_SUBCLASS_RF 0x01 // RF Controller
131 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
132 
133 #define BTD_MAX_ENDPOINTS 4
134 #define BTD_NUMSERVICES 4 // Max number of Bluetooth services - if you need more than four simply increase this number
135 
136 #define PAIR 1
137 
140 public:
145  virtual void ACLData(uint8_t* ACLData);
147  virtual void Run();
149  virtual void Reset();
151  virtual void disconnect();
152 };
153 
158 class BTD : public USBDeviceConfig, public UsbConfigXtracter {
159 public:
164  BTD(USB *p);
165 
174  virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
182  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
187  virtual uint8_t Release();
192  virtual uint8_t Poll();
193 
198  virtual uint8_t GetAddress() {
199  return bAddress;
200  };
201 
206  virtual bool isReady() {
207  return bPollEnable;
208  };
214  virtual boolean DEVCLASSOK(uint8_t klass) { return (klass == USB_CLASS_WIRELESS_CTRL); }
215 
223  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
224  if (vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID)
225  return true;
226  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
227  if (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID))
228  return true;
229  }
230  return false;
231  };
243  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
247  void disconnect() {
248  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++)
249  if(btService[i])
250  btService[i]->disconnect();
251  };
252 
259  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++) {
260  if(!btService[i]) {
261  btService[i] = pService;
262  return i; // Return ID
263  }
264  }
265  return -1; // ErrorregisterServiceClass
266  };
267 
274  void HCI_Command(uint8_t* data, uint16_t nbytes);
276  void hci_reset();
278  void hci_read_bdaddr();
285  void hci_set_local_name(const char* name);
287  void hci_write_scan_enable();
289  void hci_write_scan_disable();
291  void hci_remote_name();
293  void hci_accept_connection();
298  void hci_disconnect(uint16_t handle);
315  void hci_inquiry();
317  void hci_inquiry_cancel();
319  void hci_connect();
324  void hci_connect(uint8_t *bdaddr);
338  void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
346  void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
355  void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
362  void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
369  void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
377  void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
385  void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
392  void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
403 
405  const char* btdName;
407  const char* btdPin;
408 
410  uint8_t my_bdaddr[6];
412  uint16_t hci_handle;
414  uint8_t disc_bdaddr[6];
416  uint8_t remote_name[30];
422  uint8_t hci_version;
423 
426  pairWithWii = true;
427  hci_state = HCI_CHECK_DEVICE_SERVICE;
428  };
430  bool connectToWii;
439 
441  void pairWithHID() {
442  pairWithHIDDevice = true;
443  hci_state = HCI_CHECK_DEVICE_SERVICE;
444  };
446  bool connectToHIDDevice;
451 
456  uint8_t readPollInterval() {
457  return pollInterval;
458  };
459 
460 protected:
462  USB *pUsb;
464  uint8_t bAddress;
467 
469  uint8_t bConfNum;
471  uint8_t bNumEP;
473  uint32_t qNextPollTime;
474 
476  static const uint8_t BTD_CONTROL_PIPE;
478  static const uint8_t BTD_EVENT_PIPE;
480  static const uint8_t BTD_DATAIN_PIPE;
482  static const uint8_t BTD_DATAOUT_PIPE;
483 
489 
490 private:
491  void clearAllVariables(); // Set all variables, endpoint structs etc. to default values
492  BluetoothService* btService[BTD_NUMSERVICES];
493 
494  uint16_t PID, VID; // PID and VID of device connected
495 
496  bool bPollEnable;
497  uint8_t pollInterval;
498 
499  /* Variables used by high level HCI task */
500  uint8_t hci_state; //current state of bluetooth hci connection
501  uint16_t hci_counter; // counter used for bluetooth hci reset loops
502  uint8_t hci_num_reset_loops; // this value indicate how many times it should read before trying to reset
503  uint16_t hci_event_flag; // hci flags of received bluetooth events
504  uint8_t inquiry_counter;
505 
506  uint8_t hcibuf[BULK_MAXPKTSIZE]; //General purpose buffer for hci data
507  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; //General purpose buffer for l2cap in data
508  uint8_t l2capoutbuf[14]; //General purpose buffer for l2cap out data
509 
510  /* State machines */
511  void HCI_event_task(); // Poll the HCI event pipe
512  void HCI_task(); // HCI state machine
513  void ACL_event_task(); // ACL input pipe
514 
515  /* Used to set the Bluetooth Address internally to the PS3 Controllers */
516  void setBdaddr(uint8_t* BDADDR);
517  void setMoveBdaddr(uint8_t* BDADDR);
518 };
519 #endif
static const uint8_t BTD_DATAOUT_PIPE
Definition: BTD.h:482
bool incomingWii
Definition: BTD.h:432
void hci_connect()
Definition: BTD.cpp:1033
uint8_t bNumEP
Definition: BTD.h:471
const char * btdName
Definition: BTD.h:405
void hci_reset()
Definition: BTD.cpp:917
void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid, uint8_t result)
Definition: BTD.cpp:1222
void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t *scid, uint16_t psm)
Definition: BTD.cpp:1209
bool sdpConnectionClaimed
Definition: BTD.h:400
Definition: BTD.h:158
virtual boolean DEVCLASSOK(uint8_t klass)
Definition: BTD.h:214
void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1275
bool rfcommConnectionClaimed
Definition: BTD.h:402
uint8_t hci_version
Definition: BTD.h:422
void hci_inquiry()
Definition: BTD.cpp:1011
static const uint8_t BTD_EVENT_PIPE
Definition: BTD.h:478
#define PS3MOVE_PID
Definition: BTD.h:27
bool pairWithWii
Definition: BTD.h:434
void hci_write_scan_disable()
Definition: BTD.cpp:939
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
Definition: BTD.cpp:318
virtual uint8_t GetAddress()
Definition: BTD.h:198
const char * btdPin
Definition: BTD.h:407
bool motionPlusInside
Definition: BTD.h:436
void hci_remote_name()
Definition: BTD.cpp:980
uint8_t remote_name[30]
Definition: BTD.h:416
#define PS3_VID
Definition: BTD.h:24
BTD(USB *p)
Definition: BTD.cpp:27
uint8_t readPollInterval()
Definition: BTD.h:456
virtual void Reset()
static const uint8_t BTD_DATAIN_PIPE
Definition: BTD.h:480
void hci_set_local_name(const char *name)
Definition: BTD.cpp:999
void hci_write_scan_enable()
Definition: BTD.cpp:926
virtual uint8_t Release()
Definition: BTD.cpp:369
void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1288
#define USB_CLASS_WIRELESS_CTRL
Definition: UsbCore.h:55
bool watingForConnection
Definition: BTD.h:396
bool connectToHIDDevice
Definition: BTD.h:444
uint8_t bAddress
Definition: BTD.h:464
bool incomingHIDDevice
Definition: BTD.h:448
bool pairWithHIDDevice
Definition: BTD.h:450
uint32_t qNextPollTime
Definition: BTD.h:473
void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR *ep_ptr)
Definition: BTD.cpp:350
#define IOGEAR_GBU521_PID
Definition: BTD.h:30
bool connectToWii
Definition: BTD.h:428
virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:48
bool wiiUProController
Definition: BTD.h:438
uint16_t hci_handle
Definition: BTD.h:412
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: BTD.cpp:121
Definition: address.h:32
void pairWithHID()
Definition: BTD.h:441
void hci_disconnect(uint16_t handle)
Definition: BTD.cpp:1133
void hci_read_bdaddr()
Definition: BTD.cpp:948
void hci_inquiry_cancel()
Definition: BTD.cpp:1025
uint8_t my_bdaddr[6]
Definition: BTD.h:410
virtual uint8_t Poll()
Definition: BTD.cpp:375
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: BTD.h:223
static const uint8_t BTD_CONTROL_PIPE
Definition: BTD.h:476
#define BULK_MAXPKTSIZE
Definition: BTD.h:33
void disconnect()
Definition: BTD.h:247
uint8_t disc_bdaddr[6]
Definition: BTD.h:414
int8_t registerServiceClass(BluetoothService *pService)
Definition: BTD.h:258
bool l2capConnectionClaimed
Definition: BTD.h:398
EpInfo epInfo[BTD_MAX_ENDPOINTS]
Definition: BTD.h:466
#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:133
#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:1301
void hci_write_class_of_device()
Definition: BTD.cpp:1145
void hci_pin_code_negative_request_reply()
Definition: BTD.cpp:1095
#define HCI_CHECK_DEVICE_SERVICE
Definition: BTD.h:48
Definition: UsbCore.h:152
virtual void Run()
uint8_t bConfNum
Definition: BTD.h:469
void hci_link_key_request_negative_reply()
Definition: BTD.cpp:1109
void pairWithWiimote()
Definition: BTD.h:425
virtual bool isReady()
Definition: BTD.h:206
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1181
#define BTD_NUMSERVICES
Definition: BTD.h:134
void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t *scid)
Definition: BTD.cpp:1256
void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t *dcid)
Definition: BTD.cpp:1239
void hci_pin_code_request_reply()
Definition: BTD.cpp:1059
virtual void disconnect()
USB * pUsb
Definition: BTD.h:458
void hci_authentication_request()
Definition: BTD.cpp:1123
void hci_read_local_version_information()
Definition: BTD.cpp:956
void hci_accept_connection()
Definition: BTD.cpp:964
void HCI_Command(uint8_t *data, uint16_t nbytes)
Definition: BTD.cpp:912